-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
130 lines (115 loc) · 6.18 KB
/
index.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
<?php
/*
* Visitor stats addon for Bear CMS
* https://github.com/bearcms/visitor-stats-addon
* Copyright (c) Amplilabs Ltd.
* Free to use under the MIT license.
*/
use BearCMS\VisitorStats;
use BearCMS\VisitorStats\Internal\Utilities;
use BearFramework\App;
$app = App::get();
$context = $app->contexts->get(__FILE__);
$context->classes
->add('BearCMS\VisitorStats\BeforeApplyEventDetails', 'classes/VisitorStats/BeforeApplyEventDetails.php')
->add('BearCMS\VisitorStats\Internal\Utilities', 'classes/VisitorStats/Internal/Utilities.php')
->add('BearCMS\VisitorStats', 'classes/VisitorStats.php');
$app->bearCMS->addons
->register('bearcms/visitor-stats-addon', function (\BearCMS\Addons\Addon $addon) use ($app): void {
$addon->initialize = function (array $options) use ($app): void {
$context = $app->contexts->get(__FILE__);
$context->assets->addDir('assets');
$app->shortcuts
->add('visitorStats', function () {
return new VisitorStats();
});
$app->localization
->addDictionary('en', function () use ($context) {
return include $context->dir . '/locales/en.php';
})
->addDictionary('bg', function () use ($context) {
return include $context->dir . '/locales/bg.php';
})
->addDictionary('ru', function () use ($context) {
return include $context->dir . '/locales/ru.php';
});
$autoTrackPageviews = isset($options['autoTrackPageviews']) ? (int) $options['autoTrackPageviews'] > 0 : true;
$excludeBotsInPageviews = isset($options['excludeBotsInPageviews']) ? (int) $options['excludeBotsInPageviews'] > 0 : true;
\BearCMS\Internal\Config::$appSpecificServerData['glzm4a4'] = 1;
\BearCMS\Internal\ServerCommands::add('visitorStatsGet', function (array $data) use ($app) {
return $app->visitorStats->getStats($data);
});
\BearCMS\Internal\DataExport::addHandler('visitorStats', function (array $options) use ($app) {
$statsData = $options['data'];
$statsType = $statsData['type'];
$result = $app->visitorStats->getStats([$statsData])[$statsType];
$names = [];
$formaters = [];
if ($statsType === 'pageviewsPerDayCount') {
$names['date'] = __('bearcms.visitor-stats-addon.date');
$formaters['date'] = function ($value) use ($app) {
return $app->localization->formatDate($value, ['date']);
};
$names['count'] = __('bearcms.visitor-stats-addon.pageviewsCount');
}
foreach ($result as $i => $data) {
$temp = [];
foreach ($data as $name => $value) {
$temp[$names[$name]] = isset($formaters[$name]) ? $formaters[$name]($value) : $value;
}
$result[$i] = $temp;
}
return ['filename' => 'visitor-stats', 'data' => $result, 'multiple' => true];
});
$app->routes
->add('POST /-vs-log', function (App\Request $request) use ($app, $excludeBotsInPageviews) {
$action = $request->formData->getValue('a');
$action = $action !== null ? trim((string) urldecode(is_array($action) ? '' : (string) $action)) : '';
$data = $request->formData->getValue('d');
$data = $data !== null ? json_decode(urldecode($data), true) : null;
$userAgent = $request->formData->getValue('u');
$userAgent = $userAgent !== null ? trim(strtolower(str_replace(' ', '', (string) $userAgent))) : '';
$timeZone = $request->formData->getValue('z');
$timeZone = $timeZone !== null ? trim(strtolower((string) $timeZone)) : '';
if (!is_array($data)) {
$data = [];
}
$cancel = false;
if ($action === 'pageview') {
if ($userAgent === '') {
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower(str_replace(' ', '', $_SERVER['HTTP_USER_AGENT'])) : '';
}
if ($excludeBotsInPageviews) {
$bots = ['bingpreview', 'bot', 'spider', 'crawl'];
foreach ($bots as $bot) {
if (strpos($userAgent, $bot) !== false) {
$cancel = true;
break;
}
}
}
if (!$cancel) {
$data['country'] = Utilities::getCountryCode((string)$timeZone);
if ($data['country'] === null) {
unset($data['country']);
}
$data['deviceType'] = strpos($userAgent, 'mobi') !== false ? 'mobile' : 'desktop';
}
}
if (!$cancel) {
$app->visitorStats->log($action, $data);
}
$response = new App\Response\Text('');
$response->headers->set($response->headers->make('X-Robots-Tag', 'noindex, nofollow'));
$response->headers->set($response->headers->make('Cache-Control', 'no-cache, no-store, must-revalidate'));
return $response;
});
if ($autoTrackPageviews) {
$app->addEventListener('beforeSendResponse', function (App\BeforeSendResponseEventDetails $details) use ($app): void {
if ($details->response instanceof App\Response\HTML) {
$app->visitorStats->apply($details->response, ['trackPageview' => true]);
}
});
}
};
});