]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-infos.php
- More functions for stat page
[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 \r
116 <div id="tabs">\r
117         <ul id="headers">\r
118                 <li class="selected"><a href="#stats"><h2>Traffic statistics</h2></a></li>\r
119                 <li><a href="#location"><h2>Traffic location</h2></a></li>\r
120                 <li><a href="#sources"><h2>Traffic sources</h2></a></li>\r
121                 <li><a href="#share"><h2>Share</h2></a></li>\r
122         </ul>\r
123 \r
124                         \r
125         <div id="stats" class="tab first_tab">\r
126                 <h2>Traffic statistics</h2>\r
127                 \r
128                 <h3>Number of hits per day</h3>\r
129                 \r
130                 <p><?php yourls_stats_clicks_line( $dates ); ?></p>\r
131 \r
132                 <?php $best = yourls_stats_get_best_day( $dates ); ?>\r
133                 \r
134                 <h3>Best day</h3>\r
135                 <p><strong><?php echo $best['max'];?></strong> hit<?php echo ($best['max'] > 1 ? 's' : ''); ?> on <?php echo date("F j, Y", strtotime($best['day'])); ?>. \r
136                 <a href="" class='details' id="more_clicks">Click for more details</a></p>\r
137                 <ul id="details_clicks" style="display:none">\r
138                         <?php\r
139                         foreach( $dates as $year=>$months ) {\r
140                                 foreach( $months as $month=>$days) {\r
141                                         foreach( $days as $day=>$visits ) {\r
142                                                 echo '<li>'.date("F j, Y", strtotime("$year-$month-$day"))." : $visits ".yourls_plural( 'hit', $visits )."</li>\n";\r
143                                         }\r
144                                 }\r
145                         }\r
146                         ?>\r
147                 </ul>\r
148         </div>\r
149 \r
150 \r
151         <div id="location" class="tab">\r
152                 <h2>Traffic location</h2>\r
153                 \r
154                 <table border="0" cellspacing="2">\r
155                 <tr>\r
156                         <td valign="top">\r
157                                 <h3>Top 5 countries</h3>\r
158                                 <?php yourls_stats_pie( $countries, 5 ); ?>\r
159                                 <p><a href="" class='details' id="more_countries">Click for more details</a></p>\r
160                                 <ul id="details_countries" style="display:none" class="no_bullet">\r
161                                 <?php\r
162                                 foreach( $countries as $code=>$count ) {\r
163                                         echo "<li><img src='".yourls_geo_get_flag( $code )."' /> $code (".yourls_geo_countrycode_to_countryname( $code ).") : $count ".yourls_plural('hit', $count)."</li>\n";\r
164                                 }               \r
165                                 ?>\r
166                                 </ul>\r
167 \r
168                         </td>\r
169                         <td valign="top">\r
170                                 <h3>Overall traffic</h3>\r
171                                 <?php yourls_stats_countries_map( $countries ); ?>\r
172                         </td>\r
173                 </tr>\r
174                 </table>\r
175         </div>\r
176                                 \r
177                                 \r
178         <div id="sources" class="tab">\r
179                 <h2>Traffic Sources</h2>\r
180                 \r
181                 <table border="0" cellspacing="2">\r
182                 <tr>\r
183                         <td valign="top">\r
184                                 <h3>Referrer shares</h3>\r
185                                 <?php\r
186                                 $referrer_sort['Others'] = count( $referrers );\r
187                                 yourls_stats_pie( $referrer_sort, 5, '440x220' );\r
188                                 unset( $referrer_sort['Others'] );\r
189                                 ?>\r
190                                 <h3>Referrers</h3>\r
191                                 <ul class="no_bullet">\r
192                                         <?php\r
193                                         $i = 0;\r
194                                         foreach( $referrer_sort as $site => $count ) {\r
195                                                 $i++;\r
196                                                 echo "<li class='sites_list'><img src='http://$site/favicon.ico'/ width='16'> $site: <strong>$count</strong> <a href='' class='details' id='more_url$i'>(details)</a></li>\n";\r
197                                                 echo "<ul id='details_url$i' style='display:none'>";\r
198                                                 foreach( $referrers[$site] as $url => $count ) {\r
199                                                         echo "<li>"; yourls_html_link($url); echo ": <strong>$count</strong></li>\n";\r
200                                                 }\r
201                                                 echo "</ul>\n";\r
202                                                 unset( $referrers[$site] );\r
203                                         }\r
204                                         echo "<li id='sites_various'>Various: <strong>". count( $referrers ). "</strong> <a href='' class='details' id='more_various'>(details)</a></li>\n";\r
205                                         echo "<ul id='details_various' style='display:none'>";\r
206                                         foreach( $referrers as $url ) {\r
207                                                 echo "<li>"; yourls_html_link(key($url)); echo ": 1</li>\n";    \r
208                                         }\r
209                                         echo "</ul>\n"\r
210                                         ?>\r
211                                         \r
212                                 </ul>\r
213                         \r
214                         </td>\r
215                         \r
216                         <td valign="top">\r
217                                 <h3>Direct vs Referrer Traffic</h3>\r
218                                 <?php\r
219                                 $ref_traffic = count($referrer_sort) + count($referrers);\r
220                                 yourls_stats_pie( array('Direct'=>$direct, 'Referrers'=> $ref_traffic), 5, '440x220' );\r
221                                 ?>\r
222                                 <p>Direct traffic: <strong><?php echo $direct; ?></strong> <?php echo yourls_plural( 'hit', $direct ); ?> </p>\r
223                                 <p>Referrer traffic: <strong><?php echo $ref_traffic; ?></strong> <?php echo yourls_plural( 'hit', $ref_traffic ); ?> </p>\r
224 \r
225                         </td>\r
226                 </tr>\r
227                 </table>\r
228                         \r
229         </div>\r
230 \r
231         <div id="share" class="tab">\r
232                 <h2>Share</h2>\r
233                 \r
234                 <?php yourls_share_box( $longurl, YOURLS_SITE.'/'.$keyword, '', '', '<h3>Short link</h3>', '<h3>Quick Share</h3>'); ?>\r
235 \r
236         </div>\r
237         \r
238 </div>\r
239 \r
240 \r
241 <?php yourls_html_footer(); ?>