]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-infos.php
Misc cosmetic tweaks
[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( (array)$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                         $host = yourls_get_domain( $referrer );\r
42                         if( !array_key_exists( $host, $referrers ) )\r
43                                 $referrers[$host] = array( );\r
44                         if( !array_key_exists( $referrer, $referrers[$host] ) )\r
45                                 $referrers[$host][$referrer] = 0;\r
46                         $referrers[$host][$referrer]++;\r
47                 }\r
48         }\r
49         \r
50         if( isset( $click_time ) ) {\r
51                 preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches);\r
52                 list( $temp, $year, $month, $day ) = $matches;\r
53                 unset( $matches );\r
54                 if( !array_key_exists( $year, $dates ) )\r
55                         $dates[$year] = array();\r
56                 if( !array_key_exists( $month, $dates[$year] ) )\r
57                         $dates[$year][$month] = array();\r
58                 if( !array_key_exists( $day, $dates[$year][$month] ) )\r
59                         $dates[$year][$month][$day] = 0;\r
60                 $dates[$year][$month][$day]++;\r
61         }\r
62 }\r
63 \r
64 // Sort dates, chronologically from [2007][12][24] to [2009][02][19]\r
65 ksort( $dates );\r
66 foreach( $dates as $year=>$months ) {\r
67         ksort( $dates[$year] );\r
68         foreach( $months as $month=>$day ) {\r
69                 ksort( $dates[$year][$month] );\r
70         }\r
71 }\r
72 \r
73 // Sort countries, most frequent first\r
74 if ( $countries )\r
75         arsort( $countries );\r
76 \r
77 // Sort referrers. $referrer_sort is a array of most frequent domains\r
78 arsort( $referrers );\r
79 $referrer_sort = array();\r
80 $number_of_sites = count( array_keys( $referrers ) );\r
81 foreach( $referrers as $site => $urls ) {\r
82         if( count($urls) > 1 || $number_of_sites == 1 )\r
83                 $referrer_sort[$site] = array_sum( $urls );\r
84 }\r
85 arsort($referrer_sort);\r
86 \r
87 /**\r
88 echo "<pre>";\r
89 echo "referrers: "; print_r( $referrers );\r
90 echo "referrer sort: "; print_r( $referrer_sort );\r
91 echo "dates: "; print_r( $dates );\r
92 echo "countries: "; print_r( $countries );\r
93 die();\r
94 /**/\r
95 \r
96 \r
97 yourls_html_head( 'infos' );\r
98 ?>\r
99 \r
100 <h1>\r
101         <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
102         <img src="<?php echo YOURLS_SITE; ?>/images/yourls-logo.png" alt="YOURLS" title="YOURLS" style="border: 0px;" /></a>\r
103 </h1>\r
104 <?php if ( yourls_is_private() ) { ?>\r
105 <p>Your are logged in as: <strong><?php echo YOURLS_USER; ?></strong>. <a href="?mode=logout" title="Logout">Logout</a></p>\r
106 <?php } ?>\r
107 \r
108 <h2 id="informations">Informations</h2>\r
109 \r
110 <h3>Short URL: <?php yourls_html_link( YOURLS_SITE."/$keyword" ); ?></h3>\r
111 <h3>Long URL: <img src="<?php echo yourls_get_domain( $longurl, true );?>/favicon.ico"/><?php yourls_html_link( $longurl, '', 'longurl' ); ?></h3>\r
112 <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
113 \r
114 <div id="tabs">\r
115         <ul id="headers">\r
116                 <li class="selected"><a href="#stats"><h2>Traffic statistics</h2></a></li>\r
117                 <li><a href="#location"><h2>Traffic location</h2></a></li>\r
118                 <li><a href="#sources"><h2>Traffic sources</h2></a></li>\r
119                 <li><a href="#share"><h2>Share</h2></a></li>\r
120         </ul>\r
121 \r
122                         \r
123         <div id="stats" class="tab first_tab">\r
124                 <h2>Traffic statistics</h2>\r
125                 \r
126                 <?php if ( $dates ) { ?>\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                 \r
149                 <?php } else {\r
150                         echo "<p>No traffic yet. Get some clicks first!</p>";\r
151                 } ?>\r
152         </div>\r
153 \r
154 \r
155         <div id="location" class="tab">\r
156                 <h2>Traffic location</h2>\r
157                 \r
158                 <?php if ( $countries ) { ?>\r
159                         \r
160                         <table border="0" cellspacing="2">\r
161                         <tr>\r
162                                 <td valign="top">\r
163                                         <h3>Top 5 countries</h3>\r
164                                         <?php yourls_stats_pie( $countries, 5 ); ?>\r
165                                         <p><a href="" class='details' id="more_countries">Click for more details</a></p>\r
166                                         <ul id="details_countries" style="display:none" class="no_bullet">\r
167                                         <?php\r
168                                         foreach( $countries as $code=>$count ) {\r
169                                                 echo "<li><img src='".yourls_geo_get_flag( $code )."' /> $code (".yourls_geo_countrycode_to_countryname( $code ).") : $count ".yourls_plural('hit', $count)."</li>\n";\r
170                                         }               \r
171                                         ?>\r
172                                         </ul>\r
173 \r
174                                 </td>\r
175                                 <td valign="top">\r
176                                         <h3>Overall traffic</h3>\r
177                                         <?php yourls_stats_countries_map( $countries ); ?>\r
178                                 </td>\r
179                         </tr>\r
180                         </table>\r
181                 \r
182                 <?php } else {\r
183                         echo "<p>No countrie data. Maybe the geolocation module is not installed, or no traffic yet.</p>";\r
184                 } ?>\r
185         </div>\r
186                                 \r
187                                 \r
188         <div id="sources" class="tab">\r
189                 <h2>Traffic Sources</h2>\r
190                 \r
191                 <?php if ( $referrers ) { ?>\r
192                         \r
193                         <table border="0" cellspacing="2">\r
194                         <tr>\r
195                                 <td valign="top">\r
196                                         <h3>Referrer shares</h3>\r
197                                         <?php\r
198                                         if ( $number_of_sites > 1 )\r
199                                                 $referrer_sort['Others'] = count( $referrers );\r
200                                         yourls_stats_pie( $referrer_sort, 5, '440x220', '902020,FF6060' );\r
201                                         unset( $referrer_sort['Others'] );\r
202                                         ?>\r
203                                         <h3>Referrers</h3>\r
204                                         <ul class="no_bullet">\r
205                                                 <?php\r
206                                                 $i = 0;\r
207                                                 foreach( $referrer_sort as $site => $count ) {\r
208                                                         $i++;\r
209                                                         echo "<li class='sites_list'><img src='http://$site/favicon.ico'/> $site: <strong>$count</strong> <a href='' class='details' id='more_url$i'>(details)</a></li>\n";\r
210                                                         echo "<ul id='details_url$i' style='display:none'>";\r
211                                                         foreach( $referrers[$site] as $url => $count ) {\r
212                                                                 echo "<li>"; yourls_html_link($url); echo ": <strong>$count</strong></li>\n";\r
213                                                         }\r
214                                                         echo "</ul>\n";\r
215                                                         unset( $referrers[$site] );\r
216                                                 }\r
217                                                 // Any referrer left? Group in "various"\r
218                                                 if ( $referrers ) {\r
219                                                         echo "<li id='sites_various'>Various: <strong>". count( $referrers ). "</strong> <a href='' class='details' id='more_various'>(details)</a></li>\n";\r
220                                                         echo "<ul id='details_various' style='display:none'>";\r
221                                                         foreach( $referrers as $url ) {\r
222                                                                 echo "<li>"; yourls_html_link(key($url)); echo ": 1</li>\n";    \r
223                                                         }\r
224                                                         echo "</ul>\n";\r
225                                                 }\r
226                                                 ?>\r
227                                                 \r
228                                         </ul>\r
229                                 \r
230                                 </td>\r
231                                 \r
232                                 <td valign="top">\r
233                                         <h3>Direct vs Referrer Traffic</h3>\r
234                                         <?php\r
235                                         $ref_traffic = count($referrer_sort) + count($referrers);\r
236                                         yourls_stats_pie( array('Direct'=>$direct, 'Referrers'=> $ref_traffic), 5, '440x220', '902020,FF6060' );\r
237                                         ?>\r
238                                         <p>Direct traffic: <strong><?php echo $direct; ?></strong> <?php echo yourls_plural( 'hit', $direct ); ?> </p>\r
239                                         <p>Referrer traffic: <strong><?php echo $ref_traffic; ?></strong> <?php echo yourls_plural( 'hit', $ref_traffic ); ?> </p>\r
240 \r
241                                 </td>\r
242                         </tr>\r
243                         </table>\r
244 \r
245                 <?php } else {\r
246                         echo "<p>No referrer data. Get some clicks first!</p>";\r
247                 } ?>\r
248                         \r
249         </div>\r
250 \r
251         <div id="share" class="tab">\r
252                 <h2>Share</h2>\r
253                 \r
254                 <?php yourls_share_box( $longurl, YOURLS_SITE.'/'.$keyword, '', '', '<h3>Short link</h3>', '<h3>Quick Share</h3>'); ?>\r
255 \r
256         </div>\r
257         \r
258 </div>\r
259 \r
260 \r
261 <?php yourls_html_footer(); ?>