-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathModule.php
535 lines (474 loc) · 16.2 KB
/
Module.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
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
<?php
namespace wdmg\stats;
/**
* Yii2 Statistics
*
* @category Module
* @version 1.3.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-stats
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
use wdmg\helpers\ArrayHelper;
use Yii;
use wdmg\base\BaseModule;
use wdmg\stats\behaviors\ViewBehavior;
use wdmg\stats\behaviors\ControllerBehavior;
use wdmg\helpers\StringHelper;
use wdmg\validators\SerialValidator;
/**
* Statistics module definition class
*/
class Module extends BaseModule
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'wdmg\stats\controllers';
/**
* {@inheritdoc}
*/
public $defaultRoute = "visitors/index";
/**
* @var string, the name of module
*/
public $name = "Statistics";
/**
* @var string, the description of module
*/
public $description = "Statistic module";
/**
* @var string the module version
*/
private $version = "1.3.0";
/**
* @var integer, priority of initialization
*/
private $priority = 10;
/**
* @var array of strings missing translations
*/
public $missingTranslation;
/**
* Flag, collect statistics
* @var boolean
*/
public $collectStats = true;
/**
* Flag, collect profiling data
* @var boolean
*/
public $collectProfiling = true;
/**
* Flag, detect geo location by IP
* @var boolean
*/
public $detectLocation = false;
/**
* Statistics storage period, days
* @var integer: 0 - infinity
*/
public $storagePeriod = 0;
/**
* Flag, do not collect statistics in dev mode
* @var boolean
*/
public $ignoreDev = false;
/**
* Flag, do not collect statistics for ajax-requests
* @var boolean
*/
public $ignoreAjax = true;
/**
* Flag, use charts for statistic
* @var boolean
*/
public $useChart = true;
/**
* List of ignored routing
* @var array
*/
public $ignoreRoute = ['/admin', '/admin/', '/assets/', '/captcha/'];
/**
* List of ignored IP`s
* @var array
*/
public $ignoreListIp = [];
/**
* List of ignored User Agents
* @var array
*/
public $ignoreListUA = [];
/**
* Cookie name
* @var string
*/
public $cookieName = 'yii2_stats';
/**
* Cookie expire time, 1 year
* @var integer
*/
public $cookieExpire = 3110400;
/**
* MaxMind LicenseKey for GeoLite2 database
* @see https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/
*/
public $maxmindLicenseKey = "";
/**
* Advertising Systems
* @var array
*/
public $advertisingSystems = ["gclid", "yclid", "fbclid"];
/**
* Social Networks
* @var array
*/
public $socialNetworks = ["facebook", "vk", "away.vk.com", "ok", "odnoklassniki", "instagram", "twitter", "linkedin", "pinterest", "tumblr", "tumblr", "tumblr", "flickr", "myspace", "meetup", "tagged", "ask.fm", "meetme", "classmates", "loveplanet", "badoo", "twoo", "tinder", "lovoo"];
/**
* Search Engines
* @var array
*/
public $searchEngines = ["google", "yandex", "mail", "rambler", "yahoo", "bing", "baidu", "aol", "ask", "duckduckgo"];
/**
* Client Platforms
* @var array
*/
public $clientPlatforms = [
'/windows nt 10/i' => [
'title' => 'Windows 10',
'icon' => 'icon-win-10-os'
],
'/windows nt 6.3/i' => [
'title' => 'Windows 8.1',
'icon' => 'icon-win-8-os'
],
'/windows nt 6.2/i' => [
'title' => 'Windows 8',
'icon' => 'icon-win-8-os'
],
'/windows nt 6.1/i' => [
'title' => 'Windows 7',
'icon' => 'icon-win-7-os'
],
'/windows nt 6.0/i' => [
'title' => 'Windows Vista',
'icon' => 'icon-win-7-os'
],
'/windows nt 5.2/i' => [
'title' => 'Windows Server 2003/XP x64',
'icon' => 'icon-win-xp-os'
],
'/windows nt 5.1/i' => [
'title' => 'Windows XP',
'icon' => 'icon-win-xp-os'
],
'/windows xp/i' => [
'title' => 'Windows XP',
'icon' => 'icon-win-xp-os'
],
'/windows nt 5.0/i' => [
'title' => 'Windows 2000',
'icon' => 'icon-win-xp-os'
],
'/windows me/i' => [
'title' => 'Windows ME',
'icon' => 'icon-win-xp-os'
],
'/win98/i' => [
'title' => 'Windows 98',
'icon' => 'icon-win-xp-os'
],
'/win95/i' => [
'title' => 'Windows 95',
'icon' => 'icon-win-xp-os'
],
'/win16/i' => [
'title' => 'Windows 3.11',
'icon' => 'icon-win-xp-os'
],
'/macintosh|mac os x/i' => [
'title' => 'Mac OS X',
'icon' => 'icon-os-x-os'
],
'/mac_powerpc/i' => [
'title' => 'Mac OS 9',
'icon' => 'icon-mac-9-os'
],
'/linux/i' => [
'title' => 'Linux',
'icon' => 'icon-linux-os'
],
'/ubuntu/i' => [
'title' => 'Ubuntu',
'icon' => 'icon-ubuntu-os'
],
'/iphone/i' => [
'title' => 'iPhone',
'icon' => 'icon-ios-os'
],
'/ipod/i' => [
'title' => 'iPod',
'icon' => 'icon-ios-os'
],
'/ipad/i' => [
'title' => 'iPad',
'icon' => 'icon-ios-os'
],
'/android/i' => [
'title' => 'Android',
'icon' => 'icon-android-os'
],
'/blackberry/i' => [
'title' => 'BlackBerry',
'icon' => 'icon-blackberry-os'
],
'/webos/i' => [
'title' => 'webOS',
'icon' => 'icon-web-os'
]
];
/**
* Client Browsers
* @var array
*/
public $clientBrowsers = [
'/msie/i' => [
'title' => 'Internet Explorer',
'icon' => 'icon-ie-browser'
],
'/firefox/i' => [
'title' => 'Firefox',
'icon' => 'icon-firefox-browser'
],
'/safari/i' => [
'title' => 'Safari',
'icon' => 'icon-safari-browser'
],
'/chrome/i' => [
'title' => 'Chrome',
'icon' => 'icon-chrome-browser'
],
'/edge/i' => [
'title' => 'Edge',
'icon' => 'icon-edge-browser'
],
'/opera/i' => [
'title' => 'Opera',
'icon' => 'icon-opera-browser'
],
'/netscape/i' => [
'title' => 'Netscape',
'icon' => 'icon-netscape-browser'
],
'/maxthon/i' => [
'title' => 'Maxthon',
'icon' => 'icon-maxthon-browser'
],
'/konqueror/i' => [
'title' => 'Konqueror',
'icon' => 'icon-konqueror-browser'
],
'/ucbrowser/i' => [
'title' => 'UC Browser',
'icon' => 'icon-uc-browser'
],
'/vivaldi/i' => [
'title' => 'Vivaldi',
'icon' => 'icon-vivaldi-browser'
],
'/yabrowser/i' => [
'title' => 'Yandex.Browser',
'icon' => 'icon-yandex-browser'
]
];
/**
* @var Visitors::class instance (used in collect profiling)
*/
private $_visitor;
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// Set version of current module
$this->setVersion($this->version);
// Set priority of current module
$this->setPriority($this->priority);
// Autoload system params
$attributes = [
'collectStats' => 'boolean',
'collectProfiling' => 'boolean',
'detectLocation' => 'boolean',
'storagePeriod' => 'integer',
'ignoreDev' => 'boolean',
'ignoreAjax' => 'boolean',
'useChart' => 'boolean',
'ignoreRoute' => 'array',
'ignoreListIp' => 'array',
'ignoreListUA' => 'array',
'cookieName' => 'string',
'cookieExpire' => 'integer',
'maxmindLicenseKey' => 'string',
'advertisingSystems' => 'array',
'socialNetworks' => 'array',
'searchEngines' => 'array',
'clientPlatforms' => 'array',
'clientBrowsers' => 'array'
];
foreach ($attributes as $attribute => $type) {
if (isset(Yii::$app->params["stats" . "." . $attribute]) && isset($this->$attribute)) {
if (\gettype($this->$attribute) == $type)
$this->$attribute = Yii::$app->params["stats" . "." . $attribute];
}
}
}
/**
* {@inheritdoc}
*/
public function dashboardNavItems($options = null)
{
$items = [
'label' => $this->name,
'url' => [$this->routePrefix . '/stats/'],
'active' => in_array(\Yii::$app->controller->module->id, ['stats']),
'items' => [
[
'label' => Yii::t('app/modules/stats', 'Visitors'),
'url' => [$this->routePrefix . '/stats/'],
'icon' => 'fa fa-fw fa-user',
'active' => (in_array(\Yii::$app->controller->module->id, ['stats']) && Yii::$app->controller->action->id == 'index'),
],
[
'label' => Yii::t('app/modules/stats', 'Robots'),
'url' => [$this->routePrefix . '/stats/robots'],
'icon' => 'fa fa-fw fa-user-secret',
'active' => (in_array(\Yii::$app->controller->module->id, ['stats']) && Yii::$app->controller->action->id == 'robots'),
],
[
'label' => Yii::t('app/modules/stats', 'Load'),
'url' => [$this->routePrefix . '/stats/load/'],
'icon' => 'fa fa-fw fa-weight',
'active' => (in_array(\Yii::$app->controller->module->id, ['load']) && Yii::$app->controller->action->id == 'index'),
],
]
];
if (!is_null($options)) {
if (isset($options['count'])) {
$items['label'] .= '<span class="badge badge-default float-right">' . $options['count'] . '</span>';
unset($options['count']);
}
if (is_array($options))
$items = ArrayHelper::merge($items, $options);
}
return $items;
}
/**
* {@inheritdoc}
*/
public function bootstrap($app)
{
parent::bootstrap($app);
// Add stats behaviors for web app
if(!($app instanceof \yii\console\Application) && $this->module) {
// View behavior to render counter
$app->get('view')->attachBehavior('behaviors/ViewBehavior', [
'class' => ViewBehavior::class,
]);
// Controller behavior to write stat data
if ($this->collectStats) {
$app->attachBehavior('behaviors/ControllerBehavior', [
'class' => ControllerBehavior::class,
]);
}
// Collect profiling data
if ($this->collectProfiling && $this->collectStats) {
\yii\base\Event::on(\yii\web\Response::class, \yii\web\Response::EVENT_AFTER_SEND, function ($event) {
$db_profiling = Yii::getLogger()->getDbProfiling();
$elapsed_time = Yii::getLogger()->getElapsedTime();
$memory_usage = memory_get_peak_usage() / (1024 * 1024);
$db_dsn = null;
if (isset(Yii::$app->getDb()->dsn))
$db_dsn = Yii::$app->getDb()->dsn;
if (isset(Yii::$app->getDb()->getMaster()->dsn))
$db_dsn = Yii::$app->getDb()->getMaster()->dsn;
if (isset(Yii::$app->getDb()->getSlave()->dsn))
$db_dsn = Yii::$app->getDb()->getSlave()->dsn;
if ($visitor = $this->getVisitor()) {
$params = [];
if (!is_null($visitor->params)) {
if (is_array($visitor->params)) {
$params = $visitor->params;
} else if (is_string($visitor->params) && SerialValidator::isValid($visitor->params)) {
$params = unserialize($visitor->params);
}
}
$params['et'] = ($elapsed_time) ? round($elapsed_time, 4) : 0; // sec.
$params['mu'] = ($memory_usage) ? round($memory_usage, 2) : 0; // MB
$params['dbq'] = ($db_profiling[0]) ? intval($db_profiling[0]) : 0; // queries
$params['dbt'] = ($db_profiling[1]) ? round($db_profiling[1], 4) : 0; // sec.
$params['dsn'] = ($db_dsn) ? $db_dsn : null; // DSN
$visitor->params = serialize($params);
$visitor->update();
}
});
}
}
}
/**
* Updating GeoIP database from geolite.maxmind.com
*/
public function updateGeoIP() {
if ($maxmindLicenseKey = $this->maxmindLicenseKey) {
$geolitePath = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=".$maxmindLicenseKey."&suffix=tar.gz";
$databasePath = __DIR__."/database/";
if (!file_exists($databasePath) && !is_dir($databasePath))
\yii\helpers\FileHelper::createDirectory($databasePath, $mode = 0775, $recursive = true);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
try {
exec('curl -sS "'.$geolitePath.'" > '.$databasePath.'/GeoLite2-Country.tar.gz');
$phar = new \PharData($databasePath."/GeoLite2-Country.tar.gz");
if ($phar->extractTo($databasePath, null, true)) {
$files = \yii\helpers\FileHelper::findFiles($databasePath, [
'only' => ['*.mmdb']
]);
foreach($files as $file) {
$fileName = pathinfo(\yii\helpers\FileHelper::normalizePath($file), PATHINFO_BASENAME);
copy($file, $databasePath.$fileName);
}
}
unlink(__DIR__."/database/GeoLite2-Country.tar.gz");
Yii::info("OK! GeoIP database updated successful.");
return true;
} catch (Exception $e) {
Yii::warning("An error occurred while updating GeoIP database: {error}", ['error' => $e]);
return false;
}
} else {
try {
exec("curl -sS '".$geolitePath."' > ".$databasePath."GeoLite2-Country.tar.gz");
exec("tar -xf ".$databasePath."GeoLite2-Country.tar.gz -C ".$databasePath." --strip-components 1");
unlink(__DIR__."/database/GeoLite2-Country.tar.gz");
Yii::info("OK! GeoIP database updated successful.");
return true;
} catch (Exception $e) {
Yii::warning("An error occurred while updating GeoIP database: {error}", ['error' => $e]);
return false;
}
}
} else {
Yii::warning("Error! You must configure the MaxMind license key first.");
return 0;
}
}
public function setVisitor($visitor = null) {
if ($visitor instanceof \yii\db\ActiveRecord) {
$this->_visitor = $visitor;
}
}
public function getVisitor() {
return $this->_visitor;
}
}