File: GeoIp2.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (136 lines) | stat: -rw-r--r-- 5,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\GeoIp2;

use Piwik\CliMulti;
use Piwik\Container\StaticContainer;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\Installation\FormDefaultSettings;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Scheduler\Scheduler;

/**
 *
 */
class GeoIp2 extends \Piwik\Plugin
{
    public function registerEvents()
    {
        return array(
            'Translate.getClientSideTranslationKeys'  => 'getClientSideTranslationKeys',
            'Installation.defaultSettingsForm.init'   => 'installationFormInit',
            'Installation.defaultSettingsForm.submit' => 'installationFormSubmit',
        );
    }

    public function isTrackerPlugin()
    {
        return true;
    }

    public function deactivate()
    {
        // switch to default provider if GeoIP2 provider was in use
        if (LocationProvider::getCurrentProvider() instanceof \Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2) {
            LocationProvider::setCurrentProvider(LocationProvider::getDefaultProviderId());
        }
    }

    public function getClientSideTranslationKeys(&$translationKeys)
    {
        $translationKeys[] = "GeoIp2_FatalErrorDuringDownload";
        $translationKeys[] = "GeoIp2_SetupAutomaticUpdatesOfGeoIP";
        $translationKeys[] = "General_Done";
        $translationKeys[] = "General_Save";
        $translationKeys[] = "General_Continue";
        $translationKeys[] = 'GeoIp2_ISPRequiresProviderPlugin';
        $translationKeys[] = 'GeoIp2_UpdaterWasLastRun';
        $translationKeys[] = 'GeoIp2_UpdaterIsNotScheduledToRun';
        $translationKeys[] = 'GeoIp2_GeoIPUpdaterIntro';
        $translationKeys[] = 'GeoIp2_IWantToDownloadFreeGeoIP';
        $translationKeys[] = 'General_GetStarted';
        $translationKeys[] = 'GeoIp2_GeoIPDatabases';
        $translationKeys[] = 'GeoIp2_NotManagingGeoIPDBs';
        $translationKeys[] = 'GeoIp2_IPurchasedGeoIPDBs';
        $translationKeys[] = 'UserCountry_GeoIpDbIpAccuracyNote';
        $translationKeys[] = 'GeoIp2_GeoIPUpdaterInstructions';
        $translationKeys[] = 'GeoIp2_GeoLiteCityLink';
        $translationKeys[] = 'UserCountry_MaxMindLinkExplanation';
        $translationKeys[] = 'GeoIp2_LocationDatabase';
        $translationKeys[] = 'Actions_ColumnDownloadURL';
        $translationKeys[] = 'GeoIp2_LocationDatabaseHint';
        $translationKeys[] = 'GeoIp2_ISPDatabase';
        $translationKeys[] = 'GeoIp2_DownloadNewDatabasesEvery';
        $translationKeys[] = 'GeoIp2_CannotSetupGeoIPAutoUpdating';
        $translationKeys[] = 'GeoIp2_UpdaterHasNotBeenRun';
        $translationKeys[] = 'GeoIp2_UpdaterScheduledForNextRun';
        $translationKeys[] = 'GeoIp2_UpdaterWillRunNext';
        $translationKeys[] = 'GeoIp2_DownloadingDb';
    }

    /**
     * Customize the Installation "default settings" form.
     *
     */
    public function installationFormInit(FormDefaultSettings $form)
    {
        $form->addElement(
            'checkbox',
            'setup_geoip2',
            [
                'disabled',
            ],
            [
                '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'),
            ]
        );

        // default values
        $form->addDataSource(new \HTML_QuickForm2_DataSource_Array([
            'setup_geoip2' => false,
        ]));
    }

    /**
     * Process the submit on the Installation "default settings" form.
     *
     */
    public function installationFormSubmit(FormDefaultSettings $form)
    {
        $setupGeoIp2 = (bool) $form->getSubmitValue('setup_geoip2');

        if ($setupGeoIp2) {
            Option::set(GeoIP2AutoUpdater::AUTO_SETUP_OPTION_NAME, true);
            GeoIP2AutoUpdater::setUpdaterOptions([
                'loc' => \Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2::getDbIpLiteUrl(),
                'period' => GeoIP2AutoUpdater::SCHEDULE_PERIOD_MONTHLY,
            ]);

            $cliMulti = new CliMulti();

            // directly trigger the update task if possible
            // otherwise ensure it will be run soonish as scheduled task
            if ($cliMulti->supportsAsync()) {
                $phpCli = new CliMulti\CliPhp();
                $command = sprintf(
                    '%s %s/console core:run-scheduled-tasks --force "Piwik\Plugins\GeoIp2\GeoIP2AutoUpdater.update" > /dev/null 2>&1 &',
                    $phpCli->findPhpBinary(),
                    PIWIK_INCLUDE_PATH
                );
                shell_exec($command);
            } else {
                /** @var Scheduler $scheduler */
                $scheduler = StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler');
                $scheduler->rescheduleTask(new GeoIP2AutoUpdater());
            }
        }
    }
}