]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-infos.php
* More consistent way of getting favicons, through Google
[Github/YOURLS.git] / includes / functions-infos.php
1 <?php
2
3 // Echoes an image tag of Google Charts map from sorted array of 'country_code' => 'number of visits' (sort by DESC)
4 function yourls_stats_countries_map( $countries ) {
5         $map = array(
6                 'cht' => 't',
7                 'chs' => '440x220',
8                 'chtm'=> 'world',
9                 'chco'=> 'FFFFFF,9090AA,202040',
10                 'chld'=> join('' , array_keys( $countries ) ),
11                 'chd' => 't:'. join(',' ,  $countries ),
12                 'chf' => 'bg,s,EAF7FE'
13         );
14         $map_src = 'http://chart.apis.google.com/chart?' . http_build_query( $map );
15         echo "<img src='$map_src' width='440' height='220' border='0' />";
16 }
17
18 // Echoes an image tag of Google Charts pie from sorted array of 'data' => 'value' (sort by DESC). Optional $limit = (integer) limit list of X first countries, sorted by most visits
19 function yourls_stats_pie( $data, $limit = 10, $size = '340x220', $colors = '202040,9090AA' ) {
20         // Trim array: $limit first item + the sum of all others
21         if ( count( $data ) > $limit ) {
22                 $i= 0;
23                 $trim_data = array('Others' => 0);
24                 foreach( $data as $item=>$value ) {
25                         $i++;
26                         if( $i <= $limit ) {
27                                 $trim_data[$item] = $value;
28                         } else {
29                                 $trim_data['Others'] += $value;
30                         }
31                 }
32                 $data = $trim_data;
33         }
34         
35         // Scale items
36         $_data = yourls_scale_data( $data );
37         
38         // Hmmm, pie
39         $pie = array(
40                 'cht' => 'p',
41                 'chs' => $size,
42                 'chd' => 't:'.( join(',' ,  $_data ) ),
43                 'chco'=> $colors,
44                 'chl' => join('|' , array_keys( $data ) )
45         );
46         $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie );
47         
48         list( $size_x, $size_y ) = split( 'x', $size );
49         echo "<img src='$pie_src' width='$size_x' height='$size_y' border='0' />";
50 }
51
52 // Build a list of all daily values between d1/m1/y1 to d2/m2/y2.
53 function yourls_build_list_of_days( $dates ) {
54         /* Say we have an array like:
55         $dates = array (
56                 2009 => array (
57                         '08' => array (
58                                 29 => 15,
59                                 30 => 5,
60                                 ),
61                     '09' => array (
62                                 '02' => 3,
63                                 '03' => 5,
64                                 '04' => 2,
65                                 '05' => 99,
66                                 ),
67                         ),
68                 )
69         */
70         
71         if( !$dates )
72                 return array();
73
74         // Get first & last years from our range. In our example: 2009 & 2009
75         $first_year = key( $dates );
76         $last_year  = end( array_keys($dates) );
77         reset( $dates );
78
79         // Get first & last months from our range. In our example: 08 & 09
80         $first_month = key( $dates[$first_year] );
81         $last_month  = end( array_keys($dates[$last_year]) );
82         reset( $dates );
83         
84         // Get first & last days from our range. In our example: 29 & 05
85         $first_day = key( $dates[$first_year][$first_month] );
86         $last_day  = end( array_keys($dates[$last_year][$last_month]) );
87
88         // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
89         $list_of_years = array();
90         $list_of_months = array();
91         $list_of_days = array();
92         for ( $year = $first_year; $year <= $last_year; $year++ ) {
93                 $_year = sprintf('%04d', $year);
94                 $list_of_years[$_year] = $_year;
95                 $current_first_month = ( $year == $first_year ? $first_month : '01' );
96                 $current_last_month  = ( $year == $last_year ? $last_month : '12' );
97                 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
98                         $_month = sprintf('%02d', $month);
99                         $list_of_months[$_month] = $_month;
100                         $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
101                         $current_last_day  = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) );
102                         for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
103                                 $day = sprintf('%02d', $day);
104                                 $list_of_days["$_year-$_month-$day"] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
105                         }
106                 }
107         }
108         
109         return array(
110                 'list_of_days' => $list_of_days,
111                 'list_of_months' => $list_of_months,
112                 'list_of_years' => $list_of_years,
113         );
114 }
115
116 // Echoes an image tag of Google Charts line graph from array of values (eg 'number of clicks'). $legend1_list & legend2_list are values used for the 2 x-axis labels
117 function yourls_stats_line( $values, $legend1_list, $legend2_list ) {
118
119         // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph
120         if ( count( $values ) == 1 )
121                 array_unshift( $values, 0 );
122                 
123         $values = yourls_array_granularity( $values, 100 );
124         
125         // If x-axis labels have only 1 value, double it for a nicer graph
126         if( count( $legend1_list ) == 1 )
127                 $legend1_list[] = current( $legend1_list );
128         if( count( $legend2_list ) == 1 )
129                 $legend2_list[] = current( $legend2_list );
130
131         // Make the chart
132         $legend1 = join('|', $legend1_list );
133         $legend2 = join('|', $legend2_list );
134         $max = max( $values );
135         if ( $max >= 4 ) {
136                 $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
137         } else {
138                 $label_clicks = array();
139                 for ($i = 0; $i <= $max; $i++) {
140                         $label_clicks[] = $i;
141                 }
142                 $label_clicks = join( '|', $label_clicks );
143         }
144         $line = array(
145                 'cht' => 'lc',
146                 'chs' => '440x220',
147                 'chxt'=> 'x,x,y',
148                 'chd' => 't:'.( join(',' ,  $values ) ),
149                 'chds' => '0,'.$max,
150                 'chm' => 'B,E3F3FF,0,0,0|o,5FA3C6,0,-1,6|o,FFFFFF,0,-1,4',
151                 'chco' => '5FA3C6',
152                 'chxl'=> '0:|'. $legend1 .'|1:|'. $legend2 .'|2:|'. $label_clicks
153         );
154         $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
155         echo "<img src='$line_src' />";
156 }
157
158 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
159 function yourls_days_in_month($month, $year) {
160         // calculate number of days in a month
161         return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
162 }
163
164 // Get max value from date array of 'year-month-day' = 'hits'
165 function yourls_stats_get_best_day( $list_of_days ) {
166         $max = 0; $day = 0;
167         $max = max( $list_of_days );
168         foreach( $list_of_days as $k=>$v ) {
169                 if ( $v == $max )
170                         return array( 'day' => $k, 'max' => $max );
171         }
172 }
173
174 // Return domain of a URL
175 function yourls_get_domain( $url, $include_scheme = false ) {
176         $parse = parse_url( $url );
177
178         // Get host & scheme. Fall back to path if not found.
179         $host = isset( $parse['host'] ) ? $parse['host'] : '';
180         $scheme = isset( $parse['scheme'] ) ? $parse['scheme'] : '';
181         $path = isset( $parse['path'] ) ? $parse['path'] : '';
182         if( !$host )
183                 $host = $path;  
184                 
185         if ( $include_scheme && $scheme )
186                 $host = $scheme.'://'.$host;
187                 
188         return $host;
189 }
190
191 // Return favicon URL
192 function yourls_get_favicon_url( $url ) {
193         return 'http://www.google.com/s2/u/0/favicons?domain=' . yourls_get_domain( $url, false );
194 }
195
196 // Scale array of data from 0 to 100 max
197 function yourls_scale_data( $data ) {
198         $max = max( $data );
199         if( $max > 100 ) {
200                 foreach( $data as $k=>$v ) {
201                         $data[$k] = intval( $v / $max * 100 );
202                 }
203         }
204         return $data;
205 }
206
207 // Tweak granularity of array $array: keep only $grain values. This make less accurate but less messy graphs when too much values. See http://code.google.com/apis/chart/formats.html#granularity
208 function yourls_array_granularity( $array, $grain = 100, $preserve_max = true ) {
209         if ( count( $array ) > $grain ) {
210                 $max = max( $array );
211                 $step = intval( count( $array ) / $grain );
212                 $i = 0;
213                 $preserved = false;
214                 // Loop through each item and unset except every $step (optional preserver the max value)
215                 foreach( $array as $k=>$v ) {
216                         $i++;
217                         if ( $i % $step != 0 ) {
218                                 if ( $preserve_max == false ) {
219                                         unset( $array[$k] );
220                                 } else {
221                                         if ( $v == $max ) {
222                                                 if( $preserved == false )
223                                                         unset( $array[$k] );
224                                                 $preserved = true;
225                                         } else {
226                                                 unset( $array[$k] );
227                                         }
228                                 }
229                         }
230                 }
231         }
232         return $array;
233 }