Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Tracker.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a5fb093b95633d3e608e438d4f6eff3c8a99aba4 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
use Piwik\Config;
use Piwik\Piwik;
use Piwik\Common;
use Piwik\Access;
use Piwik\Translate;

/**
 * Class used by the logging script piwik.php called by the javascript tag.
 * Handles the visitor & his/her actions on the website, saves the data in the DB,
 * saves information in the cookie, etc.
 *
 * We try to include as little files as possible (no dependency on 3rd party modules).
 *
 * @package Piwik
 * @subpackage Piwik_Tracker
 */
class Piwik_Tracker
{
    protected $stateValid = self::STATE_NOTHING_TO_NOTICE;
    /**
     * @var Piwik_Tracker_Db
     */
    protected static $db = null;

    const STATE_NOTHING_TO_NOTICE = 1;
    const STATE_LOGGING_DISABLE = 10;
    const STATE_EMPTY_REQUEST = 11;
    const STATE_NOSCRIPT_REQUEST = 13;

    // We use hex ID that are 16 chars in length, ie. 64 bits IDs
    const LENGTH_HEX_ID_STRING = 16;
    const LENGTH_BINARY_ID = 8;

    // These are also hardcoded in the Javascript
    const MAX_CUSTOM_VARIABLES = 5;
    const MAX_LENGTH_CUSTOM_VARIABLE = 200;

    static protected $forcedDateTime = null;
    static protected $forcedIpString = null;
    static protected $forcedVisitorId = null;

    static protected $pluginsNotToLoad = array();
    static protected $pluginsToLoad = array();

    /**
     * The set of visits to track.
     *
     * @var array
     */
    private $requests = array();

    /**
     * The token auth supplied with a bulk visits POST.
     *
     * @var string
     */
    private $tokenAuth = null;

    /**
     * Whether we're currently using bulk tracking or not.
     *
     * @var bool
     */
    private $usingBulkTracking = false;

    /**
     * The number of requests that have been successfully logged.
     *
     * @var int
     */
    private $countOfLoggedRequests = 0;

    public function clear()
    {
        self::$forcedIpString = null;
        self::$forcedDateTime = null;
        self::$forcedVisitorId = null;
        $this->stateValid = self::STATE_NOTHING_TO_NOTICE;
    }

    public static function setForceIp($ipString)
    {
        self::$forcedIpString = $ipString;
    }

    public static function setForceDateTime($dateTime)
    {
        self::$forcedDateTime = $dateTime;
    }

    public static function setForceVisitorId($visitorId)
    {
        self::$forcedVisitorId = $visitorId;
    }

    /**
     * Do not load the specified plugins (used during testing, to disable Provider plugin)
     * @param array $plugins
     */
    static public function setPluginsNotToLoad($plugins)
    {
        self::$pluginsNotToLoad = $plugins;
    }

    /**
     * Get list of plugins to not load
     *
     * @return array
     */
    static public function getPluginsNotToLoad()
    {
        return self::$pluginsNotToLoad;
    }

    static public function getPluginsToLoad()
    {
        return self::$pluginsToLoad;
    }
    static public function setPluginsToLoad($plugins)
    {
        self::$pluginsToLoad = $plugins;
    }



    /**
     * Update Tracker config
     *
     * @param string $name  Setting name
     * @param mixed $value Value
     */
    static private function updateTrackerConfig($name, $value)
    {
        $section = Config::getInstance()->Tracker;
        $section[$name] = $value;
        Config::getInstance()->Tracker = $section;
    }

    protected function initRequests($args)
    {
        $rawData = file_get_contents("php://input");
        if (!empty($rawData)) {
            $this->usingBulkTracking = strpos($rawData, '"requests"') || strpos($rawData, "'requests'");
            if ($this->usingBulkTracking) {
                return $this->initBulkTrackingRequests($rawData);
            }
        }

        // Not using bulk tracking
        $this->requests = $args ? $args : (!empty($_GET) || !empty($_POST) ? array($_GET + $_POST) : array());
    }

    private function initBulkTrackingRequests($rawData)
    {
        // POST data can be array of string URLs or array of arrays w/ visit info
        $jsonData = Common::json_decode($rawData, $assoc = true);

        if (isset($jsonData['requests'])) {
            $this->requests = $jsonData['requests'];
        }
        $tokenAuth = Common::getRequestVar('token_auth', false, null, $jsonData);
        if (empty($tokenAuth)) {
            throw new Exception(" token_auth must be specified when using Bulk Tracking Import. See <a href='http://piwik.org/docs/tracking-api/reference/'>Tracking Doc</a>");
        }
        if (!empty($this->requests)) {
            $idSiteForAuthentication = 0;

            foreach ($this->requests as &$request) {
                // if a string is sent, we assume its a URL and try to parse it
                if (is_string($request)) {
                    $params = array();

                    $url = @parse_url($request);
                    if (!empty($url)) {
                        @parse_str($url['query'], $params);
                        $request = $params;
                        if (isset($request['idsite']) && !$idSiteForAuthentication) {
                            $idSiteForAuthentication = $request['idsite'];
                        }
                    }
                }
            }

            // a Bulk Tracking request that is not authenticated should fail
            if (!Piwik_Tracker_Request::authenticateSuperUserOrAdmin($tokenAuth, $idSiteForAuthentication)) {
                throw new Exception(" token_auth specified is not valid for site " . intval($idSiteForAuthentication));
            }
        }
        return $tokenAuth;
    }

    /**
     * Main - tracks the visit/action
     *
     * @param array $args Optional Request Array
     */
    public function main($args = null)
    {
        $displayedGIF = false;
        $tokenAuth = $this->initRequests($args);

        $isAuthenticated = false;
        if (!empty($this->requests)) {
            foreach ($this->requests as $params) {
                $request = new Piwik_Tracker_Request($params, $tokenAuth);
                $this->init($request);

                try {
                    if ($this->isVisitValid()) {

                        self::connectDatabaseIfNotConnected();

                        $visit = $this->getNewVisitObject();
                        $request->setForcedVisitorId(self::$forcedVisitorId);
                        $request->setForceDateTime(self::$forcedDateTime);
                        $request->setForceIp(self::$forcedIpString);

                        $visit->setRequest($request);
                        $visit->handle();
                        unset($visit);
                    } else {
                        Common::printDebug("The request is invalid: empty request, or maybe tracking is disabled in the config.ini.php via record_statistics=0");
                    }
                } catch (Piwik_Tracker_Db_Exception $e) {
                    Common::printDebug("<b>" . $e->getMessage() . "</b>");
                    $this->exitWithException($e, $isAuthenticated);
                } catch (Piwik_Tracker_Visit_Excluded $e) {
                } catch (Exception $e) {
                    $this->exitWithException($e, $isAuthenticated);
                }
                $this->clear();

                // increment successfully logged request count. make sure to do this after try-catch,
                // since an excluded visit is considered 'successfully logged'
                ++$this->countOfLoggedRequests;
            }


            // run scheduled task
            try {
                if (!$isAuthenticated // Do not run schedule task if we are importing logs or doing custom tracking (as it could slow down)
                    && $this->shouldRunScheduledTasks()) {
                    self::runScheduledTasks();
                }
            } catch (Exception $e) {
                $this->exitWithException($e);
            }

        } else {
            $this->handleEmptyRequest(new Piwik_Tracker_Request($_GET + $_POST));
        }
        $this->end();
    }

    protected function shouldRunScheduledTasks()
    {
        // don't run scheduled tasks in CLI mode from Tracker, this is the case
        // where we bulk load logs & don't want to lose time with tasks
        return !Common::isPhpCliMode()
            && $this->getState() != self::STATE_LOGGING_DISABLE;
    }

    /**
     * Tracker requests will automatically trigger the Scheduled tasks.
     * This is useful for users who don't setup the cron,
     * but still want daily/weekly/monthly PDF reports emailed automatically.
     *
     * This is similar to calling the API CoreAdminHome.runScheduledTasks (see misc/cron/archive.php)
     */
    protected static function runScheduledTasks()
    {
        $now = time();

        // Currently, there is no hourly tasks. When there are some,
        // this could be too agressive minimum interval (some hours would be skipped in case of low traffic)
        $minimumInterval = Config::getInstance()->Tracker['scheduled_tasks_min_interval'];

        // If the user disabled browser archiving, he has already setup a cron
        // To avoid parallel requests triggering the Scheduled Tasks,
        // Get last time tasks started executing
        $cache = Piwik_Tracker_Cache::getCacheGeneral();
        if ($minimumInterval <= 0
            || empty($cache['isBrowserTriggerArchivingEnabled'])
        ) {
            Common::printDebug("-> Scheduled tasks not running in Tracker: Browser archiving is disabled.");
            return;
        }
        $nextRunTime = $cache['lastTrackerCronRun'] + $minimumInterval;
        if ((isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS'])
            || $cache['lastTrackerCronRun'] === false
            || $nextRunTime < $now
        ) {
            $cache['lastTrackerCronRun'] = $now;
            Piwik_Tracker_Cache::setCacheGeneral($cache);
            self::initCorePiwikInTrackerMode();
            Piwik_SetOption('lastTrackerCronRun', $cache['lastTrackerCronRun']);
            Common::printDebug('-> Scheduled Tasks: Starting...');

            // save current user privilege and temporarily assume super user privilege
            $isSuperUser = Piwik::isUserIsSuperUser();

            // Scheduled tasks assume Super User is running
            Piwik::setUserIsSuperUser();

            // While each plugins should ensure that necessary languages are loaded,
            // we ensure English translations at least are loaded
            Translate::getInstance()->loadEnglishTranslation();

            $resultTasks = Piwik_TaskScheduler::runTasks();

            // restore original user privilege
            Piwik::setUserIsSuperUser($isSuperUser);

            Common::printDebug($resultTasks);
            Common::printDebug('Finished Scheduled Tasks.');
        } else {
            Common::printDebug("-> Scheduled tasks not triggered.");
        }
        Common::printDebug("Next run will be from: " . date('Y-m-d H:i:s', $nextRunTime) . ' UTC');
    }

    static public $initTrackerMode = false;

    /**
     * Used to initialize core Piwik components on a piwik.php request
     * Eg. when cache is missed and we will be calling some APIs to generate cache
     */
    static public function initCorePiwikInTrackerMode()
    {
        if (!empty($GLOBALS['PIWIK_TRACKER_MODE'])
            && self::$initTrackerMode === false
        ) {
            self::$initTrackerMode = true;
            require_once PIWIK_INCLUDE_PATH . '/core/Loader.php';
            require_once PIWIK_INCLUDE_PATH . '/core/Option.php';
            
            $access = Access::getInstance();
            $config = Config::getInstance();
            
            try {
                $db = Zend_Registry::get('db');
            } catch (Exception $e) {
                Piwik::createDatabaseObject();
            }

            $pluginsManager = \Piwik\PluginsManager::getInstance();
            $pluginsToLoad = Config::getInstance()->Plugins['Plugins'];
            $pluginsForcedNotToLoad = Piwik_Tracker::getPluginsNotToLoad();
            $pluginsToLoad = array_diff($pluginsToLoad, $pluginsForcedNotToLoad);
            $pluginsToLoad = array_merge($pluginsToLoad, Piwik_Tracker::getPluginsToLoad());
            $pluginsManager->loadPlugins($pluginsToLoad);
        }
    }

    /**
     * Echos an error message & other information, then exits.
     *
     * @param Exception $e
     * @param bool $authenticated
     */
    protected function exitWithException($e, $authenticated = false)
    {
        if ($this->usingBulkTracking) {
            // when doing bulk tracking we return JSON so the caller will know how many succeeded
            $result = array('succeeded' => $this->countOfLoggedRequests);
            // send error when in debug mode or when authenticated (which happens when doing log importing,
            if ((isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) || $authenticated) {
                $result['error'] = Piwik_Tracker_GetErrorMessage($e);
            }
            echo Common::json_encode($result);
            exit;
        }
        if (isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) {
            Common::sendHeader('Content-Type: text/html; charset=utf-8');
            $trailer = '<span style="color: #888888">Backtrace:<br /><pre>' . $e->getTraceAsString() . '</pre></span>';
            $headerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Zeitgeist/templates/simpleLayoutHeader.tpl');
            $footerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Zeitgeist/templates/simpleLayoutFooter.tpl');
            $headerPage = str_replace('{$HTML_TITLE}', 'Piwik &rsaquo; Error', $headerPage);

            echo $headerPage . '<p>' . Piwik_Tracker_GetErrorMessage($e) . '</p>' . $trailer . $footerPage;
        } // If not debug, but running authenticated (eg. during log import) then we display raw errors
        elseif ($authenticated) {
            Common::sendHeader('Content-Type: text/html; charset=utf-8');
            echo Piwik_Tracker_GetErrorMessage($e);
        } else {
            $this->outputTransparentGif();
        }
        exit;
    }

    /**
     * Returns the date in the "Y-m-d H:i:s" PHP format
     *
     * @param int $timestamp
     * @return string
     */
    public static function getDatetimeFromTimestamp($timestamp)
    {
        return date("Y-m-d H:i:s", $timestamp);
    }

    /**
     * Initialization
     */
    protected function init(Piwik_Tracker_Request $request)
    {
        $this->handleTrackingApi($request);
        $this->loadTrackerPlugins($request);
        $this->handleDisabledTracker();
        $this->handleEmptyRequest($request);

        Common::printDebug("Current datetime: " . date("Y-m-d H:i:s", $request->getCurrentTimestamp()));
    }

    /**
     * Cleanup
     */
    protected function end()
    {
        switch ($this->getState()) {
            case self::STATE_LOGGING_DISABLE:
                $this->outputTransparentGif();
                Common::printDebug("Logging disabled, display transparent logo");
                break;

            case self::STATE_EMPTY_REQUEST:
                Common::printDebug("Empty request => Piwik page");
                echo "<a href='/'>Piwik</a> is a free open source web <a href='http://piwik.org'>analytics</a> that lets you keep control of your data.";
                break;

            case self::STATE_NOSCRIPT_REQUEST:
            case self::STATE_NOTHING_TO_NOTICE:
            default:
                $this->outputTransparentGif();
                Common::printDebug("Nothing to notice => default behaviour");
                break;
        }
        Common::printDebug("End of the page.");

        if ($GLOBALS['PIWIK_TRACKER_DEBUG'] === true) {
            if (isset(self::$db)) {
                self::$db->recordProfiling();
                Piwik::printSqlProfilingReportTracker(self::$db);
            }
        }

        self::disconnectDatabase();
    }

    /**
     * Factory to create database objects
     *
     * @param array $configDb Database configuration
     * @throws Exception
     * @return Piwik_Tracker_Db_Mysqli|Piwik_Tracker_Db_Pdo_Mysql
     */
    public static function factory($configDb)
    {
        switch ($configDb['adapter']) {
            case 'PDO_MYSQL':
                require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Db/Pdo/Mysql.php';
                return new Piwik_Tracker_Db_Pdo_Mysql($configDb);

            case 'MYSQLI':
                require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Db/Mysqli.php';
                return new Piwik_Tracker_Db_Mysqli($configDb);
        }

        throw new Exception('Unsupported database adapter ' . $configDb['adapter']);
    }

    public static function connectPiwikTrackerDb()
    {
        $db = null;
        $configDb = Config::getInstance()->database;

        if (!isset($configDb['port'])) {
            // before 0.2.4 there is no port specified in config file
            $configDb['port'] = '3306';
        }

        Piwik_PostEvent('Tracker.getDatabaseConfig', array(&$configDb));

        $db = Piwik_Tracker::factory($configDb);
        $db->connect();

        return $db;
    }

    public static function connectDatabaseIfNotConnected()
    {
        if (!is_null(self::$db)) {
            return;
        }

        try {
            $db = null;
            Piwik_PostEvent('Tracker.createDatabase', array(&$db));
            if (is_null($db)) {
                $db = self::connectPiwikTrackerDb();
            }
            self::$db = $db;
        } catch (Exception $e) {
            throw new Piwik_Tracker_Db_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * @return Piwik_Tracker_Db
     */
    public static function getDatabase()
    {
        return self::$db;
    }

    public static function disconnectDatabase()
    {
        if (isset(self::$db)) {
            self::$db->disconnect();
            self::$db = null;
        }
    }

    /**
     * Returns the Tracker_Visit object.
     * This method can be overwritten to use a different Tracker_Visit object
     *
     * @throws Exception
     * @return Piwik_Tracker_Visit
     */
    protected function getNewVisitObject()
    {
        $visit = null;
        Piwik_PostEvent('Tracker.getNewVisitObject', array(&$visit));

        if (is_null($visit)) {
            $visit = new Piwik_Tracker_Visit();
        } elseif (!($visit instanceof Piwik_Tracker_Visit_Interface)) {
            throw new Exception("The Visit object set in the plugin must implement Piwik_Tracker_Visit_Interface");
        }
        return $visit;
    }

    protected function outputTransparentGif()
    {
        if (!isset($GLOBALS['PIWIK_TRACKER_DEBUG']) || !$GLOBALS['PIWIK_TRACKER_DEBUG']) {
            $trans_gif_64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
            $this->sendHeader('Content-Type: image/gif');

            $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';

            if ($requestMethod !== 'GET') {
                $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
                $this->sendHeader('Access-Control-Allow-Origin: ' . $origin);
                $this->sendHeader('Access-Control-Allow-Credentials: true');
            }

            print(base64_decode($trans_gif_64));
        }
    }

    protected function sendHeader($header)
    {
        Common::sendHeader($header);
    }

    protected function isVisitValid()
    {
        return $this->stateValid !== self::STATE_LOGGING_DISABLE
            && $this->stateValid !== self::STATE_EMPTY_REQUEST;
    }

    protected function getState()
    {
        return $this->stateValid;
    }

    protected function setState($value)
    {
        $this->stateValid = $value;
    }

    protected function loadTrackerPlugins(Piwik_Tracker_Request $request)
    {
        // Adding &dp=1 will disable the provider plugin, if token_auth is used (used to speed up bulk imports)
        $disableProvider = $request->getParam('dp');
        if (!empty($disableProvider)
            && $request->isAuthenticated()
        ) {
            Piwik_Tracker::setPluginsNotToLoad(array('Provider'));
        }

        try {
            $pluginsTracker = Config::getInstance()->Plugins_Tracker['Plugins_Tracker'];
            if (count($pluginsTracker) > 0) {
                $pluginsTracker = array_diff($pluginsTracker, self::getPluginsNotToLoad());
                \Piwik\PluginsManager::getInstance()->doNotLoadAlwaysActivatedPlugins();

                \Piwik\PluginsManager::getInstance()->loadPlugins($pluginsTracker);

                Common::printDebug("Loading plugins: { " . implode(",", $pluginsTracker) . " }");
            }
        } catch (Exception $e) {
            Common::printDebug("ERROR: " . $e->getMessage());
        }
    }

    protected function handleEmptyRequest(Piwik_Tracker_Request $request)
    {
        $countParameters = $request->getParamsCount();
        if ($countParameters == 0) {
            $this->setState(self::STATE_EMPTY_REQUEST);
        }
        if ($countParameters == 1) {
            $this->setState(self::STATE_NOSCRIPT_REQUEST);
        }
    }

    protected function handleDisabledTracker()
    {
        $saveStats = Config::getInstance()->Tracker['record_statistics'];
        if ($saveStats == 0) {
            $this->setState(self::STATE_LOGGING_DISABLE);
        }
    }

    protected function getTokenAuth()
    {
        if (!is_null($this->tokenAuth)) {
            return $this->tokenAuth;
        }

        return Common::getRequestVar('token_auth', false);
    }

    /**
     * This method allows to set custom IP + server time + visitor ID, when using Tracking API.
     * These two attributes can be only set by the Super User (passing token_auth).
     */
    protected function handleTrackingApi(Piwik_Tracker_Request $request)
    {
        if(!$request->isAuthenticated()) {
            return;
        }

        // Custom IP to use for this visitor
        $customIp = $request->getParam('cip');
        if (!empty($customIp)) {
            $this->setForceIp($customIp);
        }

        // Custom server date time to use
        $customDatetime = $request->getParam('cdt');
        if (!empty($customDatetime)) {
            $this->setForceDateTime($customDatetime);
        }

        // Forced Visitor ID to record the visit / action
        $customVisitorId = $request->getParam('cid');
        if (!empty($customVisitorId)) {
            $this->setForceVisitorId($customVisitorId);
        }
    }

    public static function setTestEnvironment($args = null, $requestMethod = null)
    {
        if (is_null($args)) {
            $args = $_GET + $_POST;
        }
        if (is_null($requestMethod)) {
            $requestMethod = $_SERVER['REQUEST_METHOD'];
        }

        // Do not run scheduled tasks during tests
        self::updateTrackerConfig('scheduled_tasks_min_interval', 0);

        // if nothing found in _GET/_POST and we're doing a POST, assume bulk request. in which case,
        // we have to bypass authentication
        if (empty($args) && $requestMethod == 'POST') {
            self::updateTrackerConfig('tracking_requests_require_authentication', 0);
        }

        // Tests can force the use of 3rd party cookie for ID visitor
        if (Common::getRequestVar('forceUseThirdPartyCookie', false, null, $args) == 1) {
            self::updateTrackerConfig('use_third_party_id_cookie', 1);
        }

        // Tests using window_look_back_for_visitor
        if (Common::getRequestVar('forceLargeWindowLookBackForVisitor', false, null, $args) == 1) {
            self::updateTrackerConfig('window_look_back_for_visitor', 2678400);
        }

        // Tests can force the enabling of IP anonymization
        $forceIpAnonymization = false;
        if (Common::getRequestVar('forceIpAnonymization', false, null, $args) == 1) {
            self::updateTrackerConfig('ip_address_mask_length', 2);

            $section = Config::getInstance()->Plugins_Tracker;
            $section['Plugins_Tracker'][] = "AnonymizeIP";
            Config::getInstance()->Plugins_Tracker = $section;

            $forceIpAnonymization = true;
        }

        // Custom IP to use for this visitor
        $customIp = Common::getRequestVar('cip', false, null, $args);
        if (!empty($customIp)) {
            self::setForceIp($customIp);
        }

        // Custom server date time to use
        $customDatetime = Common::getRequestVar('cdt', false, null, $args);
        if (!empty($customDatetime)) {
            self::setForceDateTime($customDatetime);
        }

        // Custom visitor id
        $customVisitorId = Common::getRequestVar('cid', false, null, $args);
        if (!empty($customVisitorId)) {
            self::setForceVisitorId($customVisitorId);
        }
        $pluginsDisabled = array('Provider');
        if (!$forceIpAnonymization) {
            $pluginsDisabled[] = 'AnonymizeIP';
        }

        // Disable provider plugin, because it is so slow to do many reverse ip lookups
        self::setPluginsNotToLoad($pluginsDisabled);

        // we load 'DevicesDetection' in tests only (disabled by default)
        self::setPluginsToLoad( array('DevicesDetection') );
    }
}

/**
 * Gets the error message to output when a tracking request fails.
 *
 * @param Exception $e
 * @return string
 */
function Piwik_Tracker_GetErrorMessage($e)
{
    // Note: duplicated from FormDatabaseSetup.isAccessDenied
    // Avoid leaking the username/db name when access denied
    if ($e->getCode() == 1044 || $e->getCode() == 42000) {
        return "Error while connecting to the Piwik database - please check your credentials in config/config.ini.php file";
    } else {
        return $e->getMessage();
    }
}