]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-infos.php
Big commit: the individual stat page is just fucking perfect now :)
[Github/YOURLS.git] / yourls-infos.php
1 <?php\r
2 // Require Files\r
3 require_once( dirname(__FILE__).'/includes/config.php' );\r
4 require_once( dirname(__FILE__).'/includes/functions-infos.php' );\r
5 yourls_maybe_require_auth();\r
6 \r
7 if ( !isset( $_GET['id'] ) )\r
8         yourls_redirect( YOURLS_SITE, 307 );\r
9 \r
10 // Get basic infos for this shortened URL\r
11 $keyword = yourls_sanitize_string( $_GET['id'] );\r
12 $longurl = yourls_get_keyword_longurl( $keyword );\r
13 $clicks = yourls_get_keyword_clicks( $keyword );\r
14 $timestamp = yourls_get_keyword_timestamp( $keyword );\r
15 if ( $longurl === false )\r
16         yourls_redirect( YOURLS_SITE, 307 );\r
17         \r
18 // Fetch all information from the table log\r
19 $table = YOURLS_DB_TABLE_LOG;\r
20 $hits = $ydb->get_results( "SELECT `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` = '$keyword';" );\r
21 \r
22 $referrers = array();\r
23 $direct = 0;\r
24 $countries = array();\r
25 $dates = array();\r
26 $list_of_days = array();\r
27 $list_of_months = array();\r
28 $list_of_years = array();\r
29 $last_24h = array();\r
30 \r
31 // Loop through all results and build list of referrers, countries and hits per day\r
32 foreach( (array)$hits as $hit ) {\r
33         extract( (array)$hit );\r
34 \r
35         if ( isset( $country_code ) && $country_code ) {\r
36                 if( !array_key_exists( $country_code, $countries ) )\r
37                         $countries[$country_code] = 0;\r
38                 $countries[$country_code]++;\r
39         }\r
40         \r
41         if( isset( $referrer ) ) {\r
42                 if ( $referrer == 'direct' ) {\r
43                         $direct++;\r
44                 } else {\r
45                         $host = yourls_get_domain( $referrer );\r
46                         if( !array_key_exists( $host, $referrers ) )\r
47                                 $referrers[$host] = array( );\r
48                         if( !array_key_exists( $referrer, $referrers[$host] ) )\r
49                                 $referrers[$host][$referrer] = 0;\r
50                         $referrers[$host][$referrer]++;\r
51                 }\r
52         }\r
53         \r
54         if( isset( $click_time ) ) {\r
55                 $now = intval( date('U') );\r
56 \r
57                 preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches);\r
58                 list( $temp, $year, $month, $day, $hour, $min, $sec ) = $matches;\r
59                 unset( $matches );\r
60                 \r
61                 // Build array of $dates[$year][$month][$day] = number of clicks\r
62                 if( !array_key_exists( $year, $dates ) )\r
63                         $dates[$year] = array();\r
64                 if( !array_key_exists( $month, $dates[$year] ) )\r
65                         $dates[$year][$month] = array();\r
66                 if( !array_key_exists( $day, $dates[$year][$month] ) )\r
67                         $dates[$year][$month][$day] = 0;\r
68                 $dates[$year][$month][$day]++;\r
69                 \r
70                 // Build array of last 24 hours $last_24h[$hour] = number of click\r
71                 $then = strtotime( $click_time);\r
72                 if( ( $now >= $then ) && ( ( $now - $then ) < ( 24 * 60 * 60 ) ) ) {\r
73                         $year = sprintf( "%02d", substr( $year, -1, 2 ) ); // 2009 -> 09\r
74                         $diff = $now - strtotime( $click_time);\r
75                         if( !array_key_exists( "$year-$month-$day-$hour", $last_24h ) )\r
76                                 $last_24h["$year-$month-$day-$hour"] = 0;\r
77                         $last_24h["$year-$month-$day-$hour"]++;\r
78                 }\r
79         }\r
80 }\r
81 \r
82 \r
83 // Sort dates, chronologically from [2007][12][24] to [2009][02][19]\r
84 ksort( $dates );\r
85 foreach( $dates as $year=>$months ) {\r
86         ksort( $dates[$year] );\r
87         foreach( $months as $month=>$day ) {\r
88                 ksort( $dates[$year][$month] );\r
89         }\r
90 }\r
91 \r
92 // Get $list_of_days, $list_of_months, $list_of_years\r
93 if( $dates ) {\r
94         extract( yourls_build_list_of_days( $dates ) );\r
95 \r
96         // If the $last_24h doesn't have all the hours, insert missing hours with value 0\r
97         for ($i = 23; $i >= 0; $i--) {\r
98                 $h = date('y-m-d-H', $now - ($i * 60 * 60) );\r
99                 if( !array_key_exists( $h, $last_24h ) ) {\r
100                         $last_24h[$h] = 0;\r
101                 }\r
102         }\r
103         ksort( $last_24h );\r
104 }\r
105 \r
106 // Sort countries, most frequent first\r
107 if ( $countries )\r
108         arsort( $countries );\r
109 \r
110 // Sort referrers. $referrer_sort is a array of most frequent domains\r
111 arsort( $referrers );\r
112 $referrer_sort = array();\r
113 $number_of_sites = count( array_keys( $referrers ) );\r
114 foreach( $referrers as $site => $urls ) {\r
115         if( count($urls) > 1 || $number_of_sites == 1 )\r
116                 $referrer_sort[$site] = array_sum( $urls );\r
117 }\r
118 arsort($referrer_sort);\r
119 \r
120 /**\r
121 echo "<pre>";\r
122 //echo "referrers: "; print_r( $referrers );\r
123 //echo "referrer sort: "; print_r( $referrer_sort );\r
124 echo "dates: "; print_r( $dates );\r
125 echo "list of days: "; print_r( $list_of_days );\r
126 echo "list_of_months: "; print_r( $list_of_months );\r
127 echo "list_of_years: "; print_r( $list_of_years );\r
128 echo "last_24h: "; print_r( $last_24h );\r
129 //echo "countries: "; print_r( $countries );\r
130 \r
131 die();\r
132 /**/\r
133 \r
134 \r
135 yourls_html_head( 'infos' );\r
136 ?>\r
137 \r
138 <h1>\r
139         <a href="<?php echo YOURLS_SITE; ?>/admin/index.php" title="YOURLS"><span>YOURLS</span>: <span>Y</span>our <span>O</span>wn <span>URL</span> <span>S</span>hortener<br/>\r
140         <img src="<?php echo YOURLS_SITE; ?>/images/yourls-logo.png" alt="YOURLS" title="YOURLS" style="border: 0px;" /></a>\r
141 </h1>\r
142 <?php if ( yourls_is_private() ) { ?>\r
143 <p>Your are logged in as: <strong><?php echo YOURLS_USER; ?></strong>. <a href="?mode=logout" title="Logout">Logout</a></p>\r
144 <?php } ?>\r
145 \r
146 <h2 id="informations">Informations</h2>\r
147 \r
148 <h3>Short URL: <img class="fix_images" src="<?php echo YOURLS_SITE; ?>/images/favicon.gif"/> <?php yourls_html_link( YOURLS_SITE."/$keyword" ); ?></h3>\r
149 <h3 id="longurl">Long URL: <img class="fix_images" src="<?php echo yourls_get_domain( $longurl, true );?>/favicon.ico"/> <?php yourls_html_link( $longurl, '', 'longurl' ); ?></h3>\r
150 \r
151 <div id="tabs">\r
152         <div class="wrap_unfloat">\r
153         <ul id="headers" class="toggle_display stat_tab">\r
154                 <li class="selected"><a href="#stat_tab_stats"><h2>Traffic statistics</h2></a></li>\r
155                 <li><a href="#stat_tab_location"><h2>Traffic location</h2></a></li>\r
156                 <li><a href="#stat_tab_sources"><h2>Traffic sources</h2></a></li>\r
157                 <li><a href="#stat_tab_share"><h2>Share</h2></a></li>\r
158         </ul>\r
159         </div>\r
160 \r
161                         \r
162         <div id="stat_tab_stats" class="tab">\r
163                 <h2>Traffic statistics</h2>\r
164                 \r
165                 <?php if ( $list_of_days ) { ?>\r
166                 \r
167                         <?php\r
168                         $graphs = array(\r
169                                 '24' => 'Last 24 hours',\r
170                                 '7'  => 'Last 7 days',\r
171                                 '30' => 'Last 30 days',\r
172                                 'all'=> 'All time'\r
173                         );\r
174                         \r
175                         // Which graph to generate ?\r
176                         $do_all = $do_30 = $do_7 = $do_24 = false;\r
177                         $hits_all = array_sum( $list_of_days );\r
178                         $hits_30  = array_sum( array_slice( $list_of_days, -30 ) );\r
179                         $hits_7   = array_sum( array_slice( $list_of_days, -7 ) );\r
180                         $hits_24  = array_sum( $last_24h );\r
181                         if( $hits_all > 0 )\r
182                                 $do_all = true; // graph for all days range\r
183                         if( $hits_30 > 0 && count( array_slice( $list_of_days, -30 ) ) == 30 )\r
184                                 $do_30 = true; // graph for last 30 days\r
185                         if( $hits_7 > 0 && count( array_slice( $list_of_days, -7 ) ) == 7 )\r
186                                 $do_7 = true; // graph for last 7 days\r
187                         if( $hits_24 > 0 )\r
188                                 $do_24 = true; // graph for last 24 hours\r
189                         \r
190                         // Which graph to display ?\r
191                         $display_all = $display_30 = $display_7 = $display_24 = false;\r
192                         if( $do_24 ) {\r
193                                 $display_24 = true;\r
194                         } elseif ( $do_7 ) {\r
195                                 $display_7 = true;\r
196                         } elseif ( $do_30 ) {\r
197                                 $display_30 = true;\r
198                         } elseif ( $do_all ) {\r
199                                 $display_all = true;\r
200                         }                               \r
201                         ?>\r
202 \r
203                         <table border="0" cellspacing="2">\r
204                         <tr>\r
205                                 <td valign="top">\r
206                                 <ul id="stats_lines" class="toggle_display stat_line">\r
207                                 <?php\r
208                                 if( $do_24 == true )\r
209                                         echo "<li><a href='#stat_line_24'>Last 24 hours</a>";\r
210                                 if( $do_7 == true )\r
211                                         echo "<li><a href='#stat_line_7'>Last 7 days</a>";\r
212                                 if( $do_30 == true )\r
213                                         echo "<li><a href='#stat_line_30'>Last 30 days</a>";\r
214                                 if( $do_all == true )\r
215                                         echo "<li><a href='#stat_line_all'>All time</a>";\r
216                                 ?>                              \r
217                                 </ul>\r
218                                 <?php\r
219                                 // Generate, and display if applicable, each needed graph\r
220                                 foreach( $graphs as $graph => $title ) {\r
221                                         if( ${'do_'.$graph} == true ) {\r
222                                                 $display = ( ${'display_'.$graph} === true ? 'display:block' : 'display:none' );\r
223                                                 echo "<div id='stat_line_$graph' class='stats_line line' style='$display'>";\r
224                                                 $labels_1 = $labels_2 = array();\r
225                                                 switch( $graph ) {\r
226                                                         case '24':\r
227                                                                 // each key of $last_24h is of type "yy-mm-dd-hh"\r
228                                                                 $first_key = current( array_keys( $last_24h ) );\r
229                                                                 $last_key = end( array_keys( $last_24h ) );\r
230                                                                 // Get "dd/mm" of first and last key\r
231                                                                 $first_label = preg_replace( '/\d\d-(\d\d)-(\d\d)-\d\d/', '$2/$1', $first_key );\r
232                                                                 $last_label = preg_replace( '/\d\d-(\d\d)-(\d\d)-\d\d/', '$2/$1', $last_key );\r
233                                                                 $labels_2 = array( $first_label, $last_label);\r
234                                                                 // Get hh of each key\r
235                                                                 foreach( $last_24h as $k=>$v ) {\r
236                                                                         $labels_1[] = end( explode( '-', $k ) ); // 'hh'\r
237                                                                 }\r
238                                                                 \r
239                                                                 echo "<h3>Number of hits : $title</h3>";\r
240                                                                 yourls_stats_line( $last_24h, $labels_1, $labels_2 );\r
241                                                                 break;\r
242 \r
243                                                         case '7':\r
244                                                         case '30':\r
245                                                                 // each key of $list_of_days is of type "yyyy-mm-dd"\r
246                                                                 $slice = array_slice( $list_of_days, intval( $graph ) * -1 );\r
247                                                                 foreach( $slice as $k=>$v ) {\r
248                                                                         // get "dd"\r
249                                                                         $labels_1[] = preg_replace( '/\d\d\d\d-\d\d-(\d\d)/', '$1', $k );\r
250                                                                 }\r
251                                                                 $first_key = current( array_keys( $slice ) );\r
252                                                                 $last_key = end( array_keys( $slice ) );\r
253                                                                 // Get "dd/mm" of first and last key\r
254                                                                 $first_label = preg_replace( '/\d\d\d\d-(\d\d)-(\d\d)/', '$2/$1', $first_key );\r
255                                                                 $last_label = preg_replace( '/\d\d\d\d-(\d\d)-(\d\d)/', '$2/$1', $last_key );\r
256                                                                 $labels_2 = array( $first_label, $last_label);\r
257                                                                 \r
258                                                                 echo "<h3>Number of hits : $title</h3>";\r
259                                                                 yourls_stats_line( $slice, $labels_1, $labels_2 );\r
260                                                                 unset( $slice );\r
261                                                                 break;\r
262 \r
263                                                         case 'all':\r
264                                                                 // get "yy-mm"\r
265                                                                 foreach( $list_of_days as $k=>$v ) {\r
266                                                                         $labels_1[] = preg_replace( '/\d\d(\d\d)-(\d\d)-\d\d/', '$1-$2', $k );\r
267                                                                 }\r
268                                                                 // take out duplicates\r
269                                                                 $labels_1 = array_unique( $labels_1 );\r
270                                                                 // now get "mm" only so we have all different month\r
271                                                                 foreach( $labels_1 as $k=>$v ) {\r
272                                                                         $labels_1[$k] = preg_replace( '/\d\d-(\d\d)/', '$1', $v );\r
273                                                                 }\r
274                                                                 \r
275                                                                 echo "<h3>Number of hits : $title</h3>";\r
276                                                                 $labels_1 = yourls_array_granularity( $labels_1, 30, false );\r
277                                                                 $labels_2 = yourls_array_granularity( $list_of_years, 30, false );\r
278                                                                 yourls_stats_line( $list_of_days, $labels_1, $labels_2 );\r
279                                                                 break;\r
280                                                 }\r
281                                                 echo "</div>\n";\r
282                                         }                       \r
283                                 } ?>\r
284                                 \r
285                                 </td>\r
286                                 <td valign="top">\r
287                                 <h3>Historical click count</h3>\r
288                                 <?php\r
289                                 $ago = round( (date('U') - strtotime($timestamp)) / (24* 60 * 60 ) );\r
290                                 if( $ago <= 1 ) {\r
291                                         $ago = '';\r
292                                 } else {\r
293                                         $ago = '(about '.$ago .' '.yourls_plural( ' day', $ago ).' ago)';\r
294                                 }\r
295                                 ?>\r
296                                 <p>Short URL created on <?php echo date("F j, Y @ g:i a", strtotime($timestamp)); ?> <?php echo $ago; ?></p>\r
297                                 <div class="wrap_unfloat">\r
298                                         <ul class="no_bullet toggle_display stat_line" id="historical_clicks">\r
299                                         <?php\r
300                                         foreach( $graphs as $graph => $title ) {\r
301                                                 if ( ${'do_'.$graph} ) {\r
302                                                         $link = "<a href='#stat_line_$graph'>$title</a>";\r
303                                                 } else {\r
304                                                         $link = $title;\r
305                                                 }\r
306                                                 $hits = yourls_plural( 'hit', ${'hits_'.$graph} );\r
307                                                 echo "<li><span class='historical_link'>$link</span> ${'hits_'.$graph} $hits</li>\n";\r
308                                         }\r
309                                         ?>\r
310                                         </ul>\r
311                                 </div>\r
312                 \r
313                                 <h3>Best day</h3>\r
314                                 <?php $best = yourls_stats_get_best_day( $list_of_days ); ?>\r
315                                 <p><strong><?php echo $best['max'];?></strong> <?php echo yourls_plural( 'hit', $best['max'] ); ?> on <?php echo date("F j, Y", strtotime($best['day'])); ?>. \r
316                                 <a href="" class='details' id="more_clicks">Click for more details</a></p>\r
317                                 <ul id="details_clicks" style="display:none">\r
318                                         <?php\r
319                                         foreach( $dates as $year=>$months ) {\r
320                                                 if( count( $list_of_years ) > 1 ) {\r
321                                                         $li = "<a href='' class='details' id='more_year$year'>Year $year</a>";\r
322                                                         $display = 'none';\r
323                                                 } else {\r
324                                                         $li = "Year $year";\r
325                                                         $display = 'block';\r
326                                                 }\r
327                                                 echo "<li>$li";\r
328                                                 echo "<ul style='display:$display' id='details_year$year'>";\r
329                                                 foreach( $months as $month=>$days ) {\r
330                                                         $monthname = date("F", mktime(0, 0, 0, $month,1));\r
331                                                         if( count( $list_of_months ) > 1 ) {\r
332                                                                 $li = "<a href='' class='details' id='more_month$year$month'>$monthname</a>";\r
333                                                                 $display = 'none';\r
334                                                         } else {\r
335                                                                 $li = "$monthname";\r
336                                                                 $display = 'block';\r
337                                                         }\r
338                                                         echo "<li>$li";\r
339                                                         echo "<ul style='display:$display' id='details_month$year$month'>";\r
340                                                                 foreach( $days as $day=>$hits ) {\r
341                                                                         $class = ( $hits == $best['max'] ? 'class="bestday"' : '' );\r
342                                                                         echo "<li $class>$day: $hits". yourls_plural( ' hit', $hits) ."</li>\n";\r
343                                                                 }\r
344                                                         echo "</ul>\n";\r
345                                                 }\r
346                                                 echo "</ul>\n";\r
347                                         }\r
348                                         ?>\r
349                                 </ul>\r
350                                 \r
351                                 </td>\r
352                                 \r
353                         </tr>\r
354                         </table>\r
355 \r
356 \r
357                 \r
358                 <?php } else {\r
359                         echo "<p>No traffic yet. Get some clicks first!</p>";\r
360                 } ?>\r
361         </div>\r
362 \r
363 \r
364         <div id="stat_tab_location" class="tab">\r
365                 <h2>Traffic location</h2>\r
366                 \r
367                 <?php if ( $countries ) { ?>\r
368                         \r
369                         <table border="0" cellspacing="2">\r
370                         <tr>\r
371                                 <td valign="top">\r
372                                         <h3>Top 5 countries</h3>\r
373                                         <?php yourls_stats_pie( $countries, 5 ); ?>\r
374                                         <p><a href="" class='details' id="more_countries">Click for more details</a></p>\r
375                                         <ul id="details_countries" style="display:none" class="no_bullet">\r
376                                         <?php\r
377                                         foreach( $countries as $code=>$count ) {\r
378                                                 echo "<li><img src='".yourls_geo_get_flag( $code )."' /> $code (".yourls_geo_countrycode_to_countryname( $code ).") : $count ".yourls_plural('hit', $count)."</li>\n";\r
379                                         }               \r
380                                         ?>\r
381                                         </ul>\r
382 \r
383                                 </td>\r
384                                 <td valign="top">\r
385                                         <h3>Overall traffic</h3>\r
386                                         <?php yourls_stats_countries_map( $countries ); ?>\r
387                                 </td>\r
388                         </tr>\r
389                         </table>\r
390                 \r
391                 <?php } else {\r
392                         echo "<p>No country data.</p>";\r
393                 } ?>\r
394         </div>\r
395                                 \r
396                                 \r
397         <div id="stat_tab_sources" class="tab">\r
398                 <h2>Traffic Sources</h2>\r
399                 \r
400                 <?php if ( $referrers ) { ?>\r
401                         \r
402                         <table border="0" cellspacing="2">\r
403                         <tr>\r
404                                 <td valign="top">\r
405                                         <h3>Referrer shares</h3>\r
406                                         <?php\r
407                                         if ( $number_of_sites > 1 )\r
408                                                 $referrer_sort['Others'] = count( $referrers );\r
409                                         yourls_stats_pie( $referrer_sort, 5, '440x220', '902020,FF6060' );\r
410                                         unset( $referrer_sort['Others'] );\r
411                                         ?>\r
412                                         <h3>Referrers</h3>\r
413                                         <ul class="no_bullet">\r
414                                                 <?php\r
415                                                 $i = 0;\r
416                                                 foreach( $referrer_sort as $site => $count ) {\r
417                                                         $i++;\r
418                                                         echo "<li class='sites_list'><img src='http://$site/favicon.ico' class='fix_images'/> $site: <strong>$count</strong> <a href='' class='details' id='more_url$i'>(details)</a></li>\n";\r
419                                                         echo "<ul id='details_url$i' style='display:none'>";\r
420                                                         foreach( $referrers[$site] as $url => $count ) {\r
421                                                                 echo "<li>"; yourls_html_link($url); echo ": <strong>$count</strong></li>\n";\r
422                                                         }\r
423                                                         echo "</ul>\n";\r
424                                                         unset( $referrers[$site] );\r
425                                                 }\r
426                                                 // Any referrer left? Group in "various"\r
427                                                 if ( $referrers ) {\r
428                                                         echo "<li id='sites_various'>Various: <strong>". count( $referrers ). "</strong> <a href='' class='details' id='more_various'>(details)</a></li>\n";\r
429                                                         echo "<ul id='details_various' style='display:none'>";\r
430                                                         foreach( $referrers as $url ) {\r
431                                                                 echo "<li>"; yourls_html_link(key($url)); echo ": 1</li>\n";    \r
432                                                         }\r
433                                                         echo "</ul>\n";\r
434                                                 }\r
435                                                 ?>\r
436                                                 \r
437                                         </ul>\r
438                                 \r
439                                 </td>\r
440                                 \r
441                                 <td valign="top">\r
442                                         <h3>Direct vs Referrer Traffic</h3>\r
443                                         <?php\r
444                                         $ref_traffic = count($referrer_sort) + count($referrers);\r
445                                         yourls_stats_pie( array('Direct'=>$direct, 'Referrers'=> $ref_traffic), 5, '440x220', '902020,FF6060' );\r
446                                         ?>\r
447                                         <p>Direct traffic: <strong><?php echo $direct; ?></strong> <?php echo yourls_plural( 'hit', $direct ); ?> </p>\r
448                                         <p>Referrer traffic: <strong><?php echo $ref_traffic; ?></strong> <?php echo yourls_plural( 'hit', $ref_traffic ); ?> </p>\r
449 \r
450                                 </td>\r
451                         </tr>\r
452                         </table>\r
453 \r
454                 <?php } else {\r
455                         echo "<p>No referrer data.</p>";\r
456                 } ?>\r
457                         \r
458         </div>\r
459 \r
460         <div id="stat_tab_share" class="tab">\r
461                 <h2>Share</h2>\r
462                 \r
463                 <?php yourls_share_box( $longurl, YOURLS_SITE.'/'.$keyword, '', '', '<h3>Short link</h3>', '<h3>Quick Share</h3>'); ?>\r
464 \r
465         </div>\r
466         \r
467 </div>\r
468 \r
469 \r
470 <?php yourls_html_footer(); ?>