forked from samnabi/kirby-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.php
129 lines (104 loc) · 3.08 KB
/
stats.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
<?php
// Set options
c::set('stats.roles.ignore','admin');
c::set('stats.days',14);
c::set('stats.session',true);
c::set('stats.date.format','D M d');
c::set('stats.format','absolute');
// Register extensions
$kirby->set('widget', 'stats', __DIR__ . DS . 'widgets' . DS . 'stats');
// Set $page and $site variables
function getCurrentPage() {
// Get the full request path
$path = kirby()->request()->path();
// Strip language prefix from path
foreach (c::get('languages', []) as $l) {
$prefix = substr($l['url'], 1);
if ($prefix and strpos($path, $prefix) === 0) {
$path = substr($path, str::length($prefix));
continue;
}
}
// Add homepage slug to path if it's blank
if ($path == '') $path = c::get('home', '/');
// Build page object
$page = page($path);
// If page doesn't exist, use error page
if (!$page) $page = page('error');
// Return the page object
return $page;
}
$site = site();
$page = getCurrentPage();
$uri = $page->uri();
/* Check if data should be logged for the current user */
// Roles for which nothing is logged
$ignore = c::get('stats.roles.ignore', "");
// Number of days to keep per-day totals for. Ensure that this is positive...
$days = c::get('stats.days', 5);
$days = ($days < 0) ? 5 : $days;
// Date format
$date_format = c::get('stats.date.format', 'd.m.y');
// Check whether to ignore the current user
if ($user = $site->user()) {
// Ignore everybody
if ($ignore == "_all") {
return;
}
// Multiple rules to be ignored, test each of them
if (is_array($ignore)) {
foreach ($ignore as $role) {
if($user->hasRole($role)) {
return;
}
}
}
// Only one rule or empty string if ignoring nobody
if ($user->hasRole($ignore)) {
return;
}
}
/* Session mode */
if (c::get('stats.session', false)) {
s::start();
// Get the already visited pages
$urls = s::get('stats', array());
// User has visited this page already in this session. Do nothing.
if (in_array($uri, $urls)) {
return;
}
// User has never been here. Add the url and put back in the session storage
$urls[] = $uri;
s::set('stats', $urls);
}
// Get or create the kirbystats page
$stats = page('kirbystats');
if (!$stats) {
try {
$stats = $site->pages()->create('kirbystats', 'stats');
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}
// Get data
$data = $stats->pages()->yaml();
$dates = $stats->dates()->yaml();
$date = date($date_format);
if ($data == null) $data = array();
if ($dates == null) $dates = array();
// calculate new values
$val = (array_key_exists($uri, $data)) ? (int) $data[$uri]['count'] + 1 : 1;
$today = (array_key_exists($date, $dates)) ? (int) $dates[$date]['count'] + 1 : 1;
$total = (!$stats->total_stats_count()->isEmpty()) ? $stats->total_stats_count()->int() + 1 : 1;
// update arrays
$data[$uri] = array('count' => $val);
$dates[$date] = array('count' => $today);
// keep only the last $days days
$dates = array_slice($dates, $days * -1, $days, true);
try {
$stats->update(array('pages' => yaml::encode($data), 'dates' => yaml::encode($dates), 'total_stats_count' => $total, ));
} catch (Exception $e) {
echo $e->getMessage();
exit;
}