2019-12-31 12:49:25 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-12-31 12:49:25 +00:00
|
|
|
namespace App\Controller\Admin;
|
|
|
|
|
2020-12-10 22:46:03 +00:00
|
|
|
use App\Entity\Repository\SettingsRepository;
|
2020-01-08 20:37:56 +00:00
|
|
|
use App\Form\GeoLiteSettingsForm;
|
2019-12-31 12:49:25 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2020-01-24 20:45:45 +00:00
|
|
|
use App\Service\IpGeolocator\GeoLite;
|
2020-02-06 02:35:13 +00:00
|
|
|
use App\Session\Flash;
|
2020-11-30 09:55:30 +00:00
|
|
|
use App\Sync\Task\UpdateGeoLiteTask;
|
2021-07-06 11:36:09 +00:00
|
|
|
use DI\FactoryInterface;
|
2020-07-08 07:03:50 +00:00
|
|
|
use Exception;
|
2019-12-31 12:49:25 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
class InstallGeoLiteController
|
|
|
|
{
|
2020-01-24 20:45:45 +00:00
|
|
|
protected string $csrf_namespace = 'admin_install_geolite';
|
|
|
|
|
2020-01-08 20:37:56 +00:00
|
|
|
public function __invoke(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2021-07-06 11:36:09 +00:00
|
|
|
FactoryInterface $factory,
|
2020-11-30 09:55:30 +00:00
|
|
|
UpdateGeoLiteTask $syncTask
|
2020-01-08 20:37:56 +00:00
|
|
|
): ResponseInterface {
|
2021-07-06 11:36:09 +00:00
|
|
|
$form = $factory->make(GeoLiteSettingsForm::class);
|
|
|
|
|
2020-01-08 20:37:56 +00:00
|
|
|
if (false !== $form->process($request)) {
|
2020-03-10 00:19:39 +00:00
|
|
|
$flash = $request->getFlash();
|
|
|
|
|
|
|
|
try {
|
2021-04-21 21:15:52 +00:00
|
|
|
$settings = $form->getSettingsRepository()->readSettings();
|
2020-12-10 22:46:03 +00:00
|
|
|
$syncTask->updateDatabase($settings->getGeoliteLicenseKey() ?? '');
|
2020-03-10 00:19:39 +00:00
|
|
|
$flash->addMessage(__('Changes saved.'), Flash::SUCCESS);
|
2020-07-08 07:03:50 +00:00
|
|
|
} catch (Exception $e) {
|
2020-12-10 22:46:03 +00:00
|
|
|
$flash->addMessage(
|
|
|
|
__(
|
|
|
|
'An error occurred while downloading the GeoLite database: %s',
|
|
|
|
$e->getMessage() . ' (' . $e->getFile() . ' L' . $e->getLine() . ')'
|
|
|
|
),
|
|
|
|
Flash::ERROR
|
|
|
|
);
|
2020-03-10 00:19:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:37:56 +00:00
|
|
|
return $response->withRedirect($request->getUri()->getPath());
|
2019-12-31 12:49:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 20:45:45 +00:00
|
|
|
$version = GeoLite::getVersion();
|
|
|
|
|
2020-12-10 22:46:03 +00:00
|
|
|
return $request->getView()->renderToResponse(
|
|
|
|
$response,
|
|
|
|
'admin/install_geolite/index',
|
|
|
|
[
|
|
|
|
'form' => $form,
|
|
|
|
'title' => __('Install GeoLite IP Database'),
|
|
|
|
'version' => $version,
|
|
|
|
'csrf' => $request->getCsrf()->generate($this->csrf_namespace),
|
|
|
|
]
|
|
|
|
);
|
2019-12-31 12:49:25 +00:00
|
|
|
}
|
2020-01-24 20:45:45 +00:00
|
|
|
|
|
|
|
public function uninstallAction(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2020-12-10 22:46:03 +00:00
|
|
|
SettingsRepository $settingsRepo,
|
2021-07-19 05:53:45 +00:00
|
|
|
string $csrf
|
2020-01-24 20:45:45 +00:00
|
|
|
): ResponseInterface {
|
|
|
|
$request->getCsrf()->verify($csrf, $this->csrf_namespace);
|
|
|
|
|
2020-12-10 22:46:03 +00:00
|
|
|
$settings = $settingsRepo->readSettings();
|
2020-12-04 08:41:55 +00:00
|
|
|
$settings->setGeoliteLicenseKey(null);
|
2020-12-10 22:46:03 +00:00
|
|
|
$settingsRepo->writeSettings($settings);
|
2020-01-24 20:45:45 +00:00
|
|
|
|
|
|
|
@unlink(GeoLite::getDatabasePath());
|
|
|
|
|
|
|
|
$request->getFlash()->addMessage(__('GeoLite database uninstalled.'), Flash::SUCCESS);
|
2021-07-19 05:53:45 +00:00
|
|
|
return $response->withRedirect((string)$request->getRouter()->fromHere('admin:install_geolite:index'));
|
2020-01-24 20:45:45 +00:00
|
|
|
}
|
2019-12-31 12:49:25 +00:00
|
|
|
}
|