]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-infos.php
Big commit: 1st attempt at stat page & functions
[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 \r
27 // Loop through all results and build list of referrers, countries and hits per day\r
28 foreach( $hits as $hit ) {\r
29         extract( (array)$hit );\r
30 \r
31         if ( isset( $country_code ) && $country_code ) {\r
32                 if( !array_key_exists( $country_code, $countries ) )\r
33                         $countries[$country_code] = 0;\r
34                 $countries[$country_code]++;\r
35         }\r
36         \r
37         if( isset( $referrer ) ) {\r
38                 if ( $referrer == 'direct' ) {\r
39                         $direct++;\r
40                 } else {\r
41                         $parse = parse_url( $referrer );\r
42                         $host = $parse['host'];\r
43                         unset( $parse );\r
44                         if( !array_key_exists( $host, $referrers ) )\r
45                                 $referrers[$host] = array( );\r
46                         if( !array_key_exists( $referrer, $referrers[$host] ) )\r
47                                 $referrers[$host][$referrer] = 0;\r
48                         $referrers[$host][$referrer]++;\r
49                 }\r
50         }\r
51         \r
52         if( isset( $click_time ) ) {\r
53                 preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches);\r
54                 list( $temp, $year, $month, $day ) = $matches;\r
55                 unset( $matches );\r
56                 if( !array_key_exists( $year, $dates ) )\r
57                         $dates[$year] = array();\r
58                 if( !array_key_exists( $month, $dates[$year] ) )\r
59                         $dates[$year][$month] = array();\r
60                 if( !array_key_exists( $day, $dates[$year][$month] ) )\r
61                         $dates[$year][$month][$day] = 0;\r
62                 $dates[$year][$month][$day]++;\r
63         }\r
64 }\r
65 \r
66 // Sort dates, chronologically from [2007][12][24] to [2009][02][19]\r
67 ksort( $dates );\r
68 foreach( $dates as $year=>$months ) {\r
69         ksort( $dates[$year] );\r
70         foreach( $months as $month=>$day ) {\r
71                 ksort( $dates[$year][$month] );\r
72         }\r
73 }\r
74 \r
75 // Sort countries, most frequent first\r
76 if ( $countries )\r
77         arsort( $countries );\r
78 \r
79 // Sort referrers. $referrer_sort is a array of most frequent domains\r
80 arsort( $referrers );\r
81 $referrer_sort = array();\r
82 foreach( $referrers as $site => $urls ) {\r
83         if( count($urls) > 1 )\r
84                 $referrer_sort[$site] = array_sum( $urls );\r
85 }\r
86 arsort($referrer_sort);\r
87 \r
88 /**\r
89 echo "<pre>";\r
90 echo "referrers: "; print_r( $referrers );\r
91 echo "referrer sort: "; print_r( $referrer_sort );\r
92 echo "dates: "; print_r( $dates );\r
93 echo "countries: "; print_r( $countries );\r
94 die();\r
95 /**/\r
96 \r
97 \r
98 yourls_html_head( 'infos' );\r
99 ?>\r
100 \r
101 <h1>\r
102         <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
103         <img src="<?php echo YOURLS_SITE; ?>/images/yourls-logo.png" alt="YOURLS" title="YOURLS" style="border: 0px;" /></a>\r
104 </h1>\r
105 <?php if ( yourls_is_private() ) { ?>\r
106 <p>Your are logged in as: <strong><?php echo YOURLS_USER; ?></strong>. <a href="?mode=logout" title="Logout">Logout</a></p>\r
107 <?php } ?>\r
108 \r
109 <h2>Informations</h2>\r
110 \r
111 <h3>Short URL: <?php yourls_html_link( YOURLS_SITE."/$keyword" ); ?></h3>\r
112 <p>Long URL: <?php yourls_html_link( yourls_get_keyword_longurl( $keyword ) ); ?></p>\r
113 <p>Number of hits since <?php echo date("F j, Y, g:i a", strtotime($timestamp)); ?>: <strong><?php echo $clicks; ?></strong> hit<?php echo ($clicks > 1 ? 's' : ''); ?></p>\r
114 \r
115 <h2>Traffic statistics</h2>\r
116 \r
117 <p><?php yourls_stats_clicks_bar( $dates ); ?></p>\r
118 \r
119 <?php $best = yourls_stats_get_best_day( $dates ); ?>\r
120 <p>Best day: <strong><?php echo $best['max'];?></strong> hit<?php echo ($best['max'] > 1 ? 's' : ''); ?> on <?php echo date("F j, Y", strtotime($best['day'])); ?>. \r
121 <a href="#" class='details' onclick="$('#details_clicks').toggle(); return false">Click for more details</a></p>\r
122 <ul id="details_clicks" style="display:none">\r
123         <?php\r
124         foreach( $dates as $year=>$months ) {\r
125                 foreach( $months as $month=>$days) {\r
126                         foreach( $days as $day=>$visits ) {\r
127                                 echo '<li>'.date("F j, Y", strtotime("$year-$month-$day"))." : $visits ".yourls_plural( 'hit', $visits )."</li>\n";\r
128                         }\r
129                 }\r
130         }\r
131         ?>\r
132 </ul>\r
133 \r
134 <h2>Traffic location</h2>\r
135 \r
136 <table border="0" cellspacing="2">\r
137 <tr>\r
138         <td valign="top">\r
139                 <p>Top 5 countries. <a href="#" class='details' onclick="$('#details_countries').toggle(); return false">Click for more details</a></p></p>\r
140                 <?php yourls_stats_countries_pie( $countries, 5 ); ?>\r
141                 <ul id="details_countries" style="display:none">\r
142                 <?php\r
143                 foreach( $countries as $code=>$count ) {\r
144                         echo "<li><img src='".yourls_geo_get_flag( $code )."' /> $code (".yourls_geo_countrycode_to_countryname( $code ).") : $count ".yourls_plural('hit', $count)."</li>\n";\r
145                 }               \r
146                 ?>\r
147                 </ul>\r
148 \r
149         </td>\r
150         <td valign="top">\r
151                 <p>Overall traffic</p>\r
152                 <?php yourls_stats_countries_map( $countries ); ?>\r
153         </td>\r
154 </tr>\r
155 </table>\r
156 \r
157 <h2>Traffic sources</h2>\r
158 \r
159 <ul>\r
160         <li>Direct: <?php echo $direct; ?></li>\r
161         <?php\r
162         $i = 0;\r
163         foreach( $referrer_sort as $site => $count ) {\r
164                 $i++;\r
165                 echo "<li>$site: $count. <a href='#' class='details' onclick='javascript:$(\"#details_url$i\").toggle(); return false;'>Details</a></li>\n";\r
166                 echo "<ul id='details_url$i' style='display:none'>";\r
167                 foreach( $referrers[$site] as $url => $count ) {\r
168                         echo "<li>"; yourls_html_link($url); echo ": $count</li>\n";\r
169                 }\r
170                 echo "</ul>\n";\r
171                 unset( $referrers[$site] );\r
172         }\r
173         echo "<li>Various: ". count( $referrers ). " <a href='#' class='details' onclick='javascript:$(\"#details_various\").toggle(); return false;'>Details</a></li>\n";\r
174         echo "<ul id='details_various' style='display:none'>";\r
175         foreach( $referrers as $url ) {\r
176                 echo "<li>"; yourls_html_link(key($url)); echo ": 1</li>\n";    \r
177         }\r
178         echo "</ul>\n"\r
179         ?>\r
180         \r
181 </ul>\r
182 \r
183 \r
184 \r
185 <?php yourls_html_footer(); ?>