This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathoverview.php
346 lines (272 loc) · 10.2 KB
/
overview.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
<?php
$ajax = ( isset( $_REQUEST['ajax'] ) && $_REQUEST['ajax'] == 1 );
$is_archive = false;
$script_i18n = array();
$ua = new SimpleStatsUA();
$field_names = array(
'remote_ip' => __( 'IP address' ),
'search_terms' => __( 'Search terms' ),
'domain' => __( 'Source domain' ),
'referrer' => __( 'Referrer' ),
'resource' => __( 'Page' ),
'country' => __( 'Country' ),
'language' => __( 'Language' ),
'browser' => __( 'Browser' ),
'version' => __( 'Version' ),
'platform' => __( 'Operating system' ),
'source' => __( 'Visit source' )
);
// set up filters
$filters = array();
$has_filters = false;
if ( isset( $_GET['filter_date'] ) && $_GET['filter_date'] != '0' ) {
// parse pretty dates of the form yyyy/mm[/dd]
preg_match( '|(\d{4})-(\d{1,2})(?:-(\d{1,2}))?|', $_GET['filter_date'], $dates );
$filters['yr'] = $dates[1];
$filters['mo'] = $dates[2];
if( isset( $dates[3] ) )
$filters['dy'] = $dates[3];
}
foreach ( array_keys( $field_names ) as $key ) {
if ( isset( $_GET["filter_$key"] ) && $_GET["filter_$key"] != '0' ) {
$has_filters = true;
$filters[$key] = $_GET["filter_$key"];
}
}
$filters['yr'] = isset( $filters['yr'] ) ? valid_yr( $filters['yr'] ) : date( 'Y' );
$filters['mo'] = isset( $filters['mo'] ) ? valid_mo( $filters['mo'] ) : date( 'n' );
if ( isset( $filters['dy'] ) )
$filters['dy'] = valid_dy( $filters['dy'], $filters['mo'], $filters['yr'] );
// go
function render_page() {
global $loaded_data, $filters, $ss;
aggregate_old_data();
// archives or current data?
if( intval( $filters['yr'] ) <= $ss->options['last_aggregated']['yr'] && intval( $filters['mo'] ) <= $ss->options['last_aggregated']['mo'] )
$loaded_data = load_archive( $filters );
else
$loaded_data = load_data( $filters );
include( SIMPLE_STATS_PATH.'/includes/overview_html.php' );
render_page_html();
}
function aggregate_old_data(){
global $ss;
$after = $ss->options['aggregate_after'];
if( $after == 0 )
return;
// start from the earliest month to aggregate
$yr = intval( date('Y') );
$mo = intval( date('n') ) - $after - 1;
while( $mo < 1 ) {
$yr --;
$mo += 12;
}
if( $ss->options['last_aggregated']['yr'] >= $yr && $ss->options['last_aggregated']['mo'] >= $mo )
return; // we're already up to date
$ss->update_option( 'last_aggregated', array( 'yr' => $yr, 'mo' => $mo ) );
$result = $ss->query( "SELECT MIN(`date`) FROM {$ss->tables['visits']} LIMIT 1" );
if( ! $result )
return;
$earliest = mysql_fetch_row( $result );
preg_match( '/^(\d{4})-(\d{2})/', $earliest[0], $matches );
$min_yr = intval( $matches[1] );
$min_mo = intval( $matches[2] );
// is the earliest data within cutoff range?
if( gmmktime( 0, 0, 0, $mo, 1, $yr ) < gmmktime( 0, 0, 0, $min_mo, 1, $min_yr ) )
return;
while( true ) {
$data = load_data( array( 'yr' => $yr, 'mo' => $mo ) );
$data = $ss->esc( gzdeflate( serialize( $data ) ) );
// put into archive
$ss->query( "INSERT INTO `{$ss->tables['archive']}` (`yr`, `mo`, `data`) VALUES ( '$yr', '$mo', '$data' )" );
$endofmonth = "$yr-$mo-" . days_in_month( $mo, $yr );
// delete raw data
$ss->query( "DELETE FROM `{$ss->tables['visits']}` WHERE `date` >= '$yr-$mo-01' AND `date` <= '$endofmonth'" );
if( $yr == $min_yr && $mo == $min_mo )
break;
$mo --;
if( $mo < 1 ) {
$yr --;
$mo += 12;
}
}
$ss->query( "OPTIMIZE TABLE `{$ss->tables['visits']}`" );
}
function load_archive( $_filters ) {
global $ss, $is_archive;
$is_archive = true;
$yr = $_filters['yr'];
$mo = $_filters['mo'];
$result = $ss->query( "SELECT `data` FROM `{$ss->tables['archive']}` WHERE `yr` = '$yr' AND `mo` = '$mo'" );
if( !$result )
return array( 'pages' => array(), 'visits' => array() );
$data = mysql_fetch_row( $result );
return unserialize( gzinflate( $data[0] ) );
}
function load_data( $_filters ) {
global $ss, $field_names;
$fields = array_keys( $field_names );
$yr = intval( $_filters['yr'] );
$mo = intval( $_filters['mo'] );
$dy = isset( $_filters['dy'] ) ? intval( $_filters['dy'] ) : false;
// work out date range
$d0 = $dy ? $dy : 1;
$dn = $dy ? $dy : days_in_month( $mo, $yr );
$start_ts = gmmktime( 0, 0, 0, $mo, $d0, $yr );
$end_ts = gmmktime( 0, 0, 0, $mo, $dn, $yr );
$start_date = gmdate( 'Y-m-d', $start_ts );
$end_date = gmdate( 'Y-m-d', $end_ts );
$date_query = ( $start_date == $end_date ) ? "`date` = '$start_date'" : "`date` >= '$start_date' AND `date` <= '$end_date'";
$query = "SELECT * FROM `{$ss->tables['visits']}` WHERE `browser` != '1' AND $date_query"; // ignore robots for the overview
foreach ( $fields as $key ) {
if( !isset( $_filters[$key] ) )
continue;
$v = $ss->esc( $_filters[$key] );
// resource is tricky
if( $key == 'resource' )
$query .= " AND `$key` LIKE '% $v%'";
else
$query .= " AND `$key` = '$v'";
}
$result = $ss->query( $query );
// we also need date/time data for visits
$extra_fields = isset( $_filters['dy'] ) ? array( 'start_time' ) : array( 'date' );
return parse_data( $result, array_merge( $fields, $extra_fields ), $_filters );
}
function parse_data( $_result, $_fields, $_filters ) {
global $ss;
$visits = $pages = array();
$source = array( 'search_terms' => 0, 'referrer' => 0, 'direct' => 0 );
while ( $row = @mysql_fetch_assoc( $_result ) ) {
// extract individual page info
$resources = explode( "\n", $row['resource'] );
// if filtering by resource, things are a bit more complicated
$filtering_resource = isset( $_filters['resource'] );
$hits = $filtering_resource ? 0 : $row['hits'];
foreach( $resources as $r ) {
if( empty( $r ) )
continue;
list( $time, $resource ) = explode( ' ', $r, 2 );
$resource = trim( $resource );
// if filtering by page then ignore everything else but that page
if( $filtering_resource ) {
if( $resource == $_filters['resource'] )
$hits += 1;
else
continue;
}
if( isset( $pages[$resource] ) )
$pages[$resource] ++;
else
$pages[$resource] = 1;
}
if ( isset( $row['search_terms'] ) && isset( $row['referrer'] ) ) {
if ( ! empty( $row['search_terms'] ) )
$source['search_terms']++;
elseif ( ! empty( $row['referrer'] ) )
$source['referrer']++;
else
$source['direct']++;
}
if( array_sum( $source ) )
$visits['source'] = $source;
// add up info for other fields, with a few tweaks
foreach ( $_fields as $field ) {
if ( !isset( $row[$field] ) || $field == 'resource' ) // resource has been dealt with already
continue;
$value = $row[$field];
if( $field == 'date' || $field == 'start_time' ) { // save both hits as well as visits
if ( isset( $visits[$field][$value] ) ) {
$visits[$field][$value]['visits'] ++;
$visits[$field][$value]['hits'] += $hits;
} else {
$visits[$field][$value] = array( 'hits' => $hits, 'visits' => 1 );
}
}
// these items don't have an "Unknown" category
if( in_array( $field, array( 'search_terms', 'referrer', 'domain' ) ) && empty( $value ) )
continue;
// store version as Browser => array( version => hits )
if ( $field == 'version' ) {
$browser = $row['browser'];
if ( !isset( $visits[$field][$browser] ) )
$visits[$field][$browser] = array();
if ( isset( $visits[$field][$browser][$value] ) )
$visits[$field][$browser][$value] ++;
else
$visits[$field][$browser][$value] = 1;
continue;
}
if ( isset( $visits[$field][$value] ) )
$visits[$field][$value] ++;
else
$visits[$field][$value] = 1;
}
}
return array( 'visits' => $visits, 'pages' => $pages );;
}
function valid_dy( $_dy, $_mo, $_yr ) {
$dy = max( 1, min( date( 'j', gmmktime( 12, 0, 0, $_mo + 1, 0, $_yr ) ), intval( $_dy ) ) );
if ( $_yr == date( 'Y' ) && $_mo == date( 'n' ) )
$dy = min( date( 'j' ), $dy );
return $dy;
}
function valid_mo( $_mo ) {
return max( 1, min( 12, intval( $_mo ) ) );
}
function valid_yr( $_yr ) {
return max( 1970, min( 3000, intval( $_yr ) ) );
}
function days_in_month( $_mo, $_yr ) {
return date( 'j', mktime( 12, 0, 0, $_mo + 1, 0, $_yr ) );
}
function date_label( $_array, $_dy_override = null ) {
$yr = $_array['yr'];
$mo = isset( $_array['mo'] ) ? $_array['mo'] : null;
$dy = isset( $_array['dy'] ) ? $_array['dy'] : null;
if ( $_dy_override === false )
$dy = null;
elseif ( $_dy_override > 0 )
$dy = valid_dy( $_dy_override, $mo, $yr );
if ( $dy != null && $mo != null )
return gmstrftime( '%a %d %b %Y', gmmktime( 12, 0, 0, $mo, $dy, $yr ) );
if ( $mo != null )
return gmstrftime( '%b %Y', gmmktime( 12, 0, 0, $mo, 1, $yr ) );
return $yr;
}
function prev_period( $_query_fields, $_ignore_dy = false ) {
$prev_fields = $_query_fields;
if ( $_ignore_dy )
unset( $prev_fields['dy'] );
if ( !$_ignore_dy && isset( $_query_fields['dy'] ) && isset( $_query_fields['mo'] ) && isset( $_query_fields['yr'] ) ) {
$prev_ts = gmmktime( 12, 0, 0, $_query_fields['mo'], $_query_fields['dy'] - 1, $_query_fields['yr'] );
$prev_fields['dy'] = date( 'j', $prev_ts );
$prev_fields['mo'] = date( 'n', $prev_ts );
$prev_fields['yr'] = date( 'Y', $prev_ts );
} elseif ( isset( $_query_fields['mo'] ) && isset( $_query_fields['yr'] ) ) {
$prev_ts = gmmktime( 12, 0, 0, $_query_fields['mo'] - 1, 1, $_query_fields['yr'] );
$prev_fields['mo'] = date( 'n', $prev_ts );
$prev_fields['yr'] = date( 'Y', $prev_ts );
} elseif ( isset( $_query_fields['yr'] ) ) {
$prev_fields['yr'] = $_query_fields['yr'] - 1;
}
return $prev_fields;
}
function next_period( $_query_fields, $_ignore_dy = false ) {
$next_fields = $_query_fields;
if ( $_ignore_dy )
unset( $next_fields['dy'] );
if ( !$_ignore_dy && isset( $_query_fields['dy'] ) && isset( $_query_fields['mo'] ) && isset( $_query_fields['yr'] ) ) {
$next_ts = gmmktime( 12, 0, 0, $_query_fields['mo'], $_query_fields['dy'] + 1, $_query_fields['yr'] );
$next_fields['dy'] = date( 'j', $next_ts );
$next_fields['mo'] = date( 'n', $next_ts );
$next_fields['yr'] = date( 'Y', $next_ts );
} elseif ( isset( $_query_fields['mo'] ) && isset( $_query_fields['yr'] ) ) {
$next_ts = gmmktime( 12, 0, 0, $_query_fields['mo'] + 1, 1, $_query_fields['yr'] );
$next_fields['mo'] = date( 'n', $next_ts );
$next_fields['yr'] = date( 'Y', $next_ts );
} elseif ( isset( $_query_fields['yr'] ) ) {
$next_fields['yr'] = $_query_fields['yr'] + 1;
}
return $next_fields;
}