forked from YOURLS/YOURLS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyourls-infos.php
More file actions
206 lines (173 loc) · 7.43 KB
/
Copy pathyourls-infos.php
File metadata and controls
206 lines (173 loc) · 7.43 KB
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
<?php
/**
* YOURLS Stats
*
* This file handles the display of the statistics for a short URL. It is
* responsible for gathering the data and displaying it in a user-friendly
* format.
*
* @package YOURLS
* @since 1.0
*/
define( 'YOURLS_INFOS', true );
require_once( dirname( __FILE__ ).'/includes/load-yourls.php' );
yourls_maybe_require_auth();
// Variables should be defined in yourls-loader.php
if ( !isset( $keyword ) ) {
yourls_do_action( 'infos_no_keyword' );
yourls_redirect( YOURLS_SITE, 302 );
exit;
}
// Get basic infos for this shortened URL
$keyword = yourls_sanitize_keyword( $keyword );
$longurl = yourls_get_keyword_longurl( $keyword );
$clicks = yourls_get_keyword_clicks( $keyword );
$timestamp = yourls_get_keyword_timestamp( $keyword );
$title = yourls_get_keyword_title( $keyword );
// Update title if it hasn't been stored yet
if( $title == '' ) {
$title = yourls_get_remote_title( $longurl );
yourls_edit_link_title( $keyword, $title );
}
if ( $longurl === false ) {
yourls_do_action( 'infos_keyword_not_found' );
yourls_redirect( YOURLS_SITE, 302 );
exit;
}
yourls_do_action( 'pre_yourls_infos', $keyword );
if( yourls_do_log_redirect() ) {
$table = YOURLS_DB_TABLE_LOG;
$referrers = array();
$direct = $notdirect = 0;
$countries = array();
$dates = array();
$list_of_days = array();
$list_of_months = array();
$list_of_years = array();
$last_24h = array();
if( yourls_allow_duplicate_longurls() )
$keyword_list = yourls_get_longurl_keywords( $longurl );
$offset = (int)yourls_get_time_offset();
// Define keyword query range : either a single keyword or a list of keywords
if( isset($aggregate) && $aggregate ) {
$keyword_range = 'IN ( :list )';
$keyword_binds = array('list' => $keyword_list, 'offset' => $offset);
} else {
$aggregate = false;
$keyword_range = '= :keyword';
$keyword_binds = array('keyword' => $keyword, 'offset' => $offset);
}
// *** Referrers ***
$sql = "SELECT `referrer`, COUNT(*) AS `count` FROM `$table` WHERE `shorturl` $keyword_range GROUP BY `referrer`;";
$sql = yourls_apply_filter('stat_query_referrer', $sql);
$rows = $ydb->fetchObjects($sql, $keyword_binds);
// Loop through all results and build list of referrers, countries and hits per day
foreach( (array)$rows as $row ) {
if ( $row->referrer == 'direct' ) {
$direct = $row->count;
continue;
}
$host = yourls_get_domain( $row->referrer );
if( !isset( $referrers[$host] ) )
$referrers[$host] = array( );
if( !isset( $referrers[$host][$row->referrer] ) ) {
$referrers[$host][$row->referrer] = $row->count;
$notdirect += $row->count;
} else {
$referrers[$host][$row->referrer] += $row->count;
$notdirect += $row->count;
}
}
// Sort referrers. $referrer_sort is a array of most frequent domains
arsort( $referrers );
$referrer_sort = array();
$number_of_sites = count( array_keys( $referrers ) );
foreach( $referrers as $site => $urls ) {
if( count($urls) > 1 || $number_of_sites == 1 )
$referrer_sort[$site] = array_sum( $urls );
}
arsort($referrer_sort);
// *** Countries ***
$sql = "SELECT `country_code`, COUNT(*) AS `count` FROM `$table` WHERE `shorturl` $keyword_range GROUP BY `country_code`;";
$sql = yourls_apply_filter('stat_query_country', $sql);
$rows = $ydb->fetchObjects($sql, $keyword_binds);
// Loop through all results and build list of countries and hits
foreach( (array)$rows as $row ) {
if ("$row->country_code")
$countries["$row->country_code"] = $row->count;
}
// Sort countries, most frequent first
if ( $countries )
arsort( $countries );
// *** Dates : array of $dates[$year][$month][$day] = number of clicks ***
$sql = "SELECT
DATE_FORMAT(`click_time`, '%Y') AS `year`,
DATE_FORMAT(`click_time`, '%m') AS `month`,
DATE_FORMAT(`click_time`, '%d') AS `day`,
COUNT(*) AS `count`
FROM `$table`
WHERE `shorturl` $keyword_range
GROUP BY `year`, `month`, `day`;";
$sql = yourls_apply_filter('stat_query_dates', $sql);
$rows = $ydb->fetchObjects($sql, $keyword_binds);
// Loop through all results and fill blanks
foreach( (array)$rows as $row ) {
if( !isset( $dates[$row->year] ) )
$dates[$row->year] = array();
if( !isset( $dates[$row->year][$row->month] ) )
$dates[$row->year][$row->month] = array();
if( !isset( $dates[$row->year][$row->month][$row->day] ) )
$dates[$row->year][$row->month][$row->day] = $row->count;
else
$dates[$row->year][$row->month][$row->day] += $row->count;
}
// Sort dates, chronologically from [2007][12][24] to [2009][02][19]
ksort( $dates );
foreach( $dates as $year=>$months ) {
ksort( $dates[$year] );
foreach( $months as $month=>$day ) {
ksort( $dates[$year][$month] );
}
}
// Get $list_of_days, $list_of_months, $list_of_years
reset( $dates );
if( $dates ) {
$_lists = yourls_build_list_of_days( $dates );
$list_of_days = $_lists['list_of_days'];
$list_of_months = $_lists['list_of_months'];
$list_of_years = $_lists['list_of_years'];
unset($_lists);
}
// *** Last 24 hours : array of $last_24h[ $hour ] = number of click ***
$sql = "SELECT
DATE_FORMAT(DATE_ADD(`click_time`, INTERVAL :offset HOUR), '%H %p') AS `time`,
COUNT(*) AS `count`
FROM `$table`
WHERE `shorturl` $keyword_range AND DATE_ADD(`click_time`, INTERVAL :offset HOUR) > (DATE_ADD(CURRENT_TIMESTAMP, INTERVAL :offset HOUR) - INTERVAL 1 DAY)
GROUP BY `time`;";
$sql = yourls_apply_filter('stat_query_last24h', $sql);
$rows = $ydb->fetchObjects($sql, $keyword_binds);
$_last_24h = array_column((array)$rows, 'count', 'time');
$now = time();
$current_hour = (int)date('G', $now + ($offset * 3600));
for ($i = 23; $i >= 0; $i--) {
$hour = ($current_hour - $i + 24) % 24;
$h = sprintf('%02d %s', $hour, $hour < 12 ? 'AM' : 'PM');
// If the $last_24h doesn't have all the hours, insert missing hours with value 0
$last_24h[ $h ] = $_last_24h[ $h ] ?? 0;
}
unset( $_last_24h );
// *** Queries all done, phew ***
// Filter all this junk if applicable. Be warned, some are possibly huge datasets.
$referrers = yourls_apply_filter( 'pre_yourls_info_referrers', $referrers );
$referrer_sort = yourls_apply_filter( 'pre_yourls_info_referrer_sort', $referrer_sort );
$direct = yourls_apply_filter( 'pre_yourls_info_direct', $direct );
$notdirect = yourls_apply_filter( 'pre_yourls_info_notdirect', $notdirect );
$dates = yourls_apply_filter( 'pre_yourls_info_dates', $dates );
$list_of_days = yourls_apply_filter( 'pre_yourls_info_list_of_days', $list_of_days );
$list_of_months = yourls_apply_filter( 'pre_yourls_info_list_of_months', $list_of_months );
$list_of_years = yourls_apply_filter( 'pre_yourls_info_list_of_years', $list_of_years );
$last_24h = yourls_apply_filter( 'pre_yourls_info_last_24h', $last_24h );
$countries = yourls_apply_filter( 'pre_yourls_info_countries', $countries );
}
require_once dirname( __FILE__ ) . '/includes/views/yourls-infos.php';