1<?php
2/**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9namespace Piwik\Plugins\GeoIp2;
10
11use Piwik\CliMulti;
12use Piwik\Container\StaticContainer;
13use Piwik\Option;
14use Piwik\Piwik;
15use Piwik\Plugins\Installation\FormDefaultSettings;
16use Piwik\Plugins\UserCountry\LocationProvider;
17use Piwik\Scheduler\Scheduler;
18
19/**
20 *
21 */
22class GeoIp2 extends \Piwik\Plugin
23{
24    public function registerEvents()
25    {
26        return array(
27            'AssetManager.getJavaScriptFiles'         => 'getJsFiles',
28            'Translate.getClientSideTranslationKeys'  => 'getClientSideTranslationKeys',
29            'Installation.defaultSettingsForm.init'   => 'installationFormInit',
30            'Installation.defaultSettingsForm.submit' => 'installationFormSubmit',
31        );
32    }
33
34    public function isTrackerPlugin()
35    {
36        return true;
37    }
38
39    public function deactivate()
40    {
41        // switch to default provider if GeoIP2 provider was in use
42        if (LocationProvider::getCurrentProvider() instanceof \Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2) {
43            LocationProvider::setCurrentProvider(LocationProvider\DefaultProvider::ID);
44        }
45    }
46
47    public function getJsFiles(&$jsFiles)
48    {
49        $jsFiles[] = "plugins/GeoIp2/angularjs/geoip2-updater/geoip2-updater.controller.js";
50        $jsFiles[] = "plugins/GeoIp2/angularjs/geoip2-updater/geoip2-updater.directive.js";
51    }
52
53    public function getClientSideTranslationKeys(&$translationKeys)
54    {
55        $translationKeys[] = "GeoIp2_FatalErrorDuringDownload";
56        $translationKeys[] = "GeoIp2_SetupAutomaticUpdatesOfGeoIP";
57        $translationKeys[] = "General_Done";
58        $translationKeys[] = "General_Save";
59        $translationKeys[] = "General_Continue";
60    }
61
62    /**
63     * Customize the Installation "default settings" form.
64     *
65     * @param FormDefaultSettings $form
66     */
67    public function installationFormInit(FormDefaultSettings $form)
68    {
69        $form->addElement('checkbox', 'setup_geoip2', null,
70            [
71                'content' => '<div class="form-help">' . Piwik::translate('GeoIp2_AutomaticSetupDescription', ['<a rel="noreferrer noopener" target="_blank" href="https://db-ip.com/db/lite.php?refid=mtm">','</a>']) . '</div> &nbsp;&nbsp;' . Piwik::translate('GeoIp2_AutomaticSetup')
72            ]
73        );
74
75        // default values
76        $form->addDataSource(new \HTML_QuickForm2_DataSource_Array([
77            'setup_geoip2' => true,
78        ]));
79    }
80
81    /**
82     * Process the submit on the Installation "default settings" form.
83     *
84     * @param FormDefaultSettings $form
85     */
86    public function installationFormSubmit(FormDefaultSettings $form)
87    {
88        $setupGeoIp2 = (bool) $form->getSubmitValue('setup_geoip2');
89
90        if ($setupGeoIp2) {
91            Option::set(GeoIP2AutoUpdater::AUTO_SETUP_OPTION_NAME, true);
92            GeoIP2AutoUpdater::setUpdaterOptions([
93                'loc' => \Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2::getDbIpLiteUrl(),
94                'period' => GeoIP2AutoUpdater::SCHEDULE_PERIOD_MONTHLY
95            ]);
96
97            $cliMulti = new CliMulti();
98
99            // directly trigger the update task if possible
100            // otherwise ensure it will be run soonish as scheduled task
101            if ($cliMulti->supportsAsync()) {
102                $phpCli = new CliMulti\CliPhp();
103                $command = sprintf('%s %s/console core:run-scheduled-tasks --force "Piwik\Plugins\GeoIp2\GeoIP2AutoUpdater.update" > /dev/null 2>&1 &',
104                    $phpCli->findPhpBinary(), PIWIK_INCLUDE_PATH);
105                shell_exec($command);
106            } else {
107                /** @var Scheduler $scheduler */
108                $scheduler = StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler');
109                $scheduler->rescheduleTask(new GeoIP2AutoUpdater());
110            }
111        }
112    }
113}
114