forked from matomo-org/plugin-VisitorGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.php
125 lines (99 loc) · 3.73 KB
/
Controller.php
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
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\VisitorGenerator;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Common;
use Piwik\Nonce;
use Piwik\Piwik;
use Piwik\UrlHelper;
use Piwik\Plugin\ControllerAdmin;
use Piwik\Plugins\SitesManager\API as SitesManagerAPI;
use Piwik\Plugins\VisitorGenerator\Generator\VisitsFake;
use Piwik\Plugins\VisitorGenerator\Generator\VisitsFromLogs;
use Piwik\SettingsServer;
use Piwik\SettingsPiwik;
use Piwik\Site;
use Piwik\Timer;
use Piwik\View;
class Controller extends ControllerAdmin
{
private $numFakeVisits = 250;
public function index()
{
Piwik::checkUserHasSuperUserAccess();
$sitesList = SitesManagerAPI::getInstance()->getSitesWithAdminAccess();
$view = new View('@VisitorGenerator/index');
$this->setBasicVariablesView($view);
$view->sitesList = $sitesList;
$view->nonce = Nonce::getNonce('VisitorGenerator.generate');
$view->countMinActionsPerRun = $this->numFakeVisits;
$view->accessLogPath = PIWIK_INCLUDE_PATH . '/plugins/VisitorGenerator/data';
return $view->render();
}
public function generate()
{
Piwik::checkUserHasSuperUserAccess();
$this->checkNonce();
$daysToCompute = $this->checkDays();
$idSite = Common::getRequestVar('idSite', false, 'string', $_POST);
SettingsServer::setMaxExecutionTime(0);
if ('all' == $idSite) {
$idSites = SitesManagerAPI::getInstance()->getSitesIdWithAtLeastViewAccess();
$siteName = Piwik::translate('General_MultiSitesSummary');
} else {
// get idSite from POST with fallback to GET
$idSite = Common::getRequestVar('idSite', false, 'int', $_GET);
$idSite = Common::getRequestVar('idSite', $idSite, 'int', $_POST);
$idSites = array($idSite);
$siteName = Site::getNameFor($idSite);
}
$timer = new Timer;
$time = time() - ($daysToCompute - 1) * 86400;
$nbActionsTotal = 0;
while ($time <= time()) {
$fromLogs = new VisitsFromLogs();
foreach ($idSites as $idSite) {
$nbActionsTotal += $fromLogs->generate($time, $idSite);
}
$fakeVisits = new VisitsFake();
foreach ($idSites as $idSite) {
$nbActionsTotal += $fakeVisits->generate($time, $idSite, $this->numFakeVisits);
}
$time += 86400;
}
$browserArchiving = Rules::isBrowserTriggerEnabled();
$view = new View('@VisitorGenerator/generate');
$this->setBasicVariablesView($view);
$view->browserArchivingEnabled = $browserArchiving;
$view->timer = $timer;
$view->days = $daysToCompute;
$view->nbActionsTotal = $nbActionsTotal;
$view->nbRequestsPerSec = round($nbActionsTotal / $timer->getTime(), 0);
$view->siteName = $siteName;
return $view->render();
}
private function checkDays()
{
$daysToCompute = Common::getRequestVar('daysToCompute', false, 'int');
if ($daysToCompute < 1) {
throw new \Exception('Days to compute must be greater or equal to 1.');
}
return $daysToCompute;
}
private function checkNonce()
{
$nonce = Common::getRequestVar('form_nonce', '', 'string', $_POST);
if (Common::getRequestVar('choice', 'no') != 'yes' ||
!Nonce::verifyNonce('VisitorGenerator.generate', $nonce)
) {
Piwik::redirectToModule('VisitorGenerator', 'index');
}
Nonce::discardNonce('VisitorGenerator.generate');
}
}