2019-12-31 12:49:25 +00:00
|
|
|
<?php
|
|
|
|
namespace App\Controller\Admin;
|
|
|
|
|
2020-01-24 20:45:45 +00:00
|
|
|
use App\Entity\Repository\SettingsRepository;
|
|
|
|
use App\Entity\Settings;
|
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-03-10 00:19:39 +00:00
|
|
|
use App\Sync\Task\UpdateGeoLiteDatabase;
|
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,
|
2020-03-10 00:19:39 +00:00
|
|
|
GeoLiteSettingsForm $form,
|
|
|
|
UpdateGeoLiteDatabase $syncTask
|
2020-01-08 20:37:56 +00:00
|
|
|
): ResponseInterface {
|
|
|
|
if (false !== $form->process($request)) {
|
2020-03-10 00:19:39 +00:00
|
|
|
|
|
|
|
$flash = $request->getFlash();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$syncTask->updateDatabase();
|
|
|
|
$flash->addMessage(__('Changes saved.'), Flash::SUCCESS);
|
2020-07-08 07:03:50 +00:00
|
|
|
} catch (Exception $e) {
|
2020-03-10 00:19:39 +00:00
|
|
|
$flash->addMessage(__(
|
|
|
|
'An error occurred while downloading the GeoLite database: %s',
|
|
|
|
$e->getMessage() . ' (' . $e->getFile() . ' L' . $e->getLine() . ')'
|
|
|
|
), Flash::ERROR);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
return $request->getView()->renderToResponse($response, 'admin/install_geolite/index', [
|
2019-12-31 12:49:25 +00:00
|
|
|
'form' => $form,
|
|
|
|
'title' => __('Install GeoLite IP Database'),
|
2020-01-24 20:45:45 +00:00
|
|
|
'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,
|
|
|
|
SettingsRepository $settingsRepo,
|
|
|
|
$csrf
|
|
|
|
): ResponseInterface {
|
|
|
|
$request->getCsrf()->verify($csrf, $this->csrf_namespace);
|
|
|
|
|
|
|
|
$settingsRepo->setSetting(Settings::GEOLITE_LICENSE_KEY, '');
|
|
|
|
|
|
|
|
@unlink(GeoLite::getDatabasePath());
|
|
|
|
|
|
|
|
$request->getFlash()->addMessage(__('GeoLite database uninstalled.'), Flash::SUCCESS);
|
|
|
|
return $response->withRedirect($request->getRouter()->fromHere('admin:install_geolite:index'));
|
|
|
|
}
|
2019-12-31 12:49:25 +00:00
|
|
|
}
|