PluginProbe
Clicky Analytics / trunk
Clicky Analytics vtrunk
trunk 1.1 1.1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5 1.5.1 1.5.2 1.6 1.6.1 All 40 releases
clicky-analytics / tools / tools.php

tools.php in Clicky Analytics trunk, at tools/tools.php

398 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Author: Alin Marcu
4 * Author URI: https://deconf.com
5 * Copyright 2013 Alin Marcu
6 * License: GPLv2 or later
7 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
8 */
9 // Exit if accessed directly
10 if ( ! defined( 'ABSPATH' ) )
11 exit();
12 if ( ! class_exists( 'CAWP_Tools' ) ) {
13
14 class CAWP_Tools {
15
16 /**
17 * Loads ISO 3166 country codes
18 * @return array
19 */
20 public static function get_countrycodes() {
21 include 'iso3166.php';
22 return $country_codes;
23 }
24
25 /**
26 * Extract the root domain from a URL
27 * @return string
28 */
29 public static function get_root_domain() {
30 $url = site_url();
31 $root = explode( '/', $url );
32 preg_match( '/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', str_ireplace( 'www', '', isset( $root[2] ) ? $root[2] : $url ), $root );
33 if ( isset( $root['domain'] ) ) {
34 return $root['domain'];
35 } else {
36 return '';
37 }
38 }
39
40 /**
41 * Simple function to remove the protocol
42 * @param string $domain
43 * @return string
44 */
45 public static function strip_protocol( $domain ) {
46 return str_replace( array( "https://", "http://", " " ), "", $domain );
47 }
48
49 /**
50 * Generates a color variation of a base color
51 * @param string $colour
52 * @param int $per
53 * @return string
54 */
55 public static function colourVariator( $colour, $per ) {
56 $colour = substr( $colour, 1 );
57 $rgb = '';
58 $per = $per / 100 * 255;
59 if ( $per < 0 ) {
60 // Darker
61 $per = abs( $per );
62 for ( $x = 0; $x < 3; $x++ ) {
63 $c = hexdec( substr( $colour, ( 2 * $x ), 2 ) ) - $per;
64 $c = ( $c < 0 ) ? 0 : dechex( (int) $c );
65 $rgb .= ( strlen( $c ) < 2 ) ? '0' . $c : $c;
66 }
67 } else {
68 // Lighter
69 for ( $x = 0; $x < 3; $x++ ) {
70 $c = hexdec( substr( $colour, ( 2 * $x ), 2 ) ) + $per;
71 $c = ( $c > 255 ) ? 'ff' : dechex( (int) $c );
72 $rgb .= ( strlen( $c ) < 2 ) ? '0' . $c : $c;
73 }
74 }
75 return '#' . $rgb;
76 }
77
78 /**
79 * Generates multiple color variations from a base color
80 * @param string $base
81 * @return array
82 */
83 public static function variations( $base ) {
84 $variations[] = $base;
85 $variations[] = self::colourVariator( $base, - 10 );
86 $variations[] = self::colourVariator( $base, + 10 );
87 $variations[] = self::colourVariator( $base, + 20 );
88 $variations[] = self::colourVariator( $base, - 20 );
89 $variations[] = self::colourVariator( $base, + 30 );
90 $variations[] = self::colourVariator( $base, - 30 );
91 return $variations;
92 }
93
94 /**
95 * Determines if a user has access to a specific feature
96 * @param string $access_level
97 * @param boolean $flag
98 * @return boolean
99 */
100 public static function check_roles( $access_level, $flag = false ) {
101 if ( is_user_logged_in() && isset( $access_level ) ) {
102 $current_user = wp_get_current_user();
103 $roles = (array) $current_user->roles;
104 if ( ( current_user_can( 'manage_options' ) ) && ! $flag ) {
105 return true;
106 }
107 if ( count( array_intersect( $roles, $access_level ) ) > 0 ) {
108 return true;
109 } else {
110 return false;
111 }
112 }
113 }
114
115 /**
116 * Cookie cleanup on uninstall
117 * @param string $name
118 */
119 public static function unset_cookie( $name ) {
120 $name = 'cawp_wg_' . $name;
121 setcookie( $name, '', time() - 3600, '/' );
122 $name = 'cawp_ir_' . $name;
123 setcookie( $name, '', time() - 3600, '/' );
124 }
125
126 /**
127 * Cache Helper function. I don't use transients because cleanup plugins can break their functionality
128 * @param string $name
129 * @param mixed $value
130 * @param number $expiration
131 */
132 public static function set_cache( $name, $value, $expiration = 0 ) {
133 update_option( '_cawp_cache_' . $name, $value, 'no' );
134 update_option( '_cawp_cache_timeout_' . $name, time() + (int) $expiration, 'no' );
135 }
136
137 /**
138 * Cache Helper function. I don't use transients because cleanup plugins can break their functionality
139 * @param string $name
140 * @param mixed $value
141 * @param number $expiration
142 */
143 public static function delete_cache( $name ) {
144 delete_option( '_cawp_cache_' . $name );
145 delete_option( '_cawp_cache_timeout_' . $name );
146 }
147
148 /**
149 * Cache Helper function. I don't use transients because cleanup plugins can break their functionality
150 * @param string $name
151 * @param mixed $value
152 * @param number $expiration
153 */
154 public static function get_cache( $name ) {
155 $value = get_option( '_cawp_cache_' . $name );
156 $expires = get_option( '_cawp_cache_timeout_' . $name );
157 if ( false === $value || ! isset( $value ) || ! isset( $expires ) ) {
158 return false;
159 }
160 if ( $expires < time() ) {
161 delete_option( '_cawp_cache_' . $name );
162 delete_option( '_cawp_cache_timeout_' . $name );
163 return false;
164 } else {
165 return $value;
166 }
167 }
168
169 /**
170 * Cache Helper function. I don't use transients because cleanup plugins can break their functionality
171 */
172 public static function clear_cache() {
173 global $wpdb;
174 $sqlquery = $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '%%cawp_cache_%%'" );
175 }
176
177 public static function delete_expired_cache() {
178 global $wpdb;
179 if ( wp_using_ext_object_cache() ) {
180 return;
181 }
182 $wpdb->query( $wpdb->prepare( "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
183 WHERE a.option_name LIKE %s
184 AND a.option_name NOT LIKE %s
185 AND b.option_name = CONCAT( '_cawp_cache_timeout_', SUBSTRING( a.option_name, 13 ) )
186 AND b.option_value < %d", $wpdb->esc_like( '_cawp_cache_' ) . '%', $wpdb->esc_like( '_cawp_cache_timeout_' ) . '%', time() ) );
187 if ( ! is_multisite() ) {
188 // Single site stores site transients in the options table.
189 $wpdb->query( $wpdb->prepare( "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
190 WHERE a.option_name LIKE %s
191 AND a.option_name NOT LIKE %s
192 AND b.option_name = CONCAT( '_site_cawp_cache_timeout_', SUBSTRING( a.option_name, 18 ) )
193 AND b.option_value < %d", $wpdb->esc_like( '_site_cawp_cache_' ) . '%', $wpdb->esc_like( '_site_cawp_cache_timeout_' ) . '%', time() ) );
194 } elseif ( is_multisite() && is_main_site() && is_main_network() ) {
195 // Multisite stores site transients in the sitemeta table.
196 $wpdb->query( $wpdb->prepare( "DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
197 WHERE a.meta_key LIKE %s
198 AND a.meta_key NOT LIKE %s
199 AND b.meta_key = CONCAT( '_site_cawp_cache_timeout_', SUBSTRING( a.meta_key, 18 ) )
200 AND b.meta_value < %d", $wpdb->esc_like( '_site_cawp_cache_' ) . '%', $wpdb->esc_like( '_site_cawp_cache_timeout_' ) . '%', time() ) );
201 }
202 }
203
204 /**
205 * Loads a view file
206 *
207 * $data parameter will be available in the template file as $data['value']
208 *
209 * @param string $template - Template file to load
210 * @param array $data - data to pass along to the template
211 * @return boolean - If template file was found
212 **/
213 public static function load_view( $path, $data = array() ) {
214 if ( file_exists( CAWP_DIR . $path ) ) {
215 require_once ( CAWP_DIR . $path );
216 return true;
217 }
218 return false;
219 }
220
221 /**
222 * Doing it wrong function
223 */
224 public static function doing_it_wrong( $function, $message, $version ) {
225 if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
226 if ( is_null( $version ) ) {
227 $version = '';
228 } else {
229 /* translators: %s: version number */
230 $version = sprintf( __( 'This message was added in version %s.', 'clicky-analytics' ), $version );
231 }
232 /* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Version information message */
233 trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', 'clicky-analytics' ), $function, $message, $version ) );
234 }
235 }
236
237 /**
238 * Error management system
239 * @param object $e
240 * @param int $timeout
241 */
242 public static function set_error( $e, $timeout ) {
243 if ( is_object( $e ) ) {
244 if ( method_exists( $e, 'getCode' ) && method_exists( $e, 'getErrors' ) ) {
245 $error_code = $e->getCode();
246 if ( 500 == $error_code || 503 == $error_code ) {
247 $timeout = 60;
248 }
249 self::set_cache( 'capi_errors', array( $e->getCode(), (array) $e->getErrors(), esc_html( print_r( $e, true ) ) ), $timeout );
250 } else {
251 self::set_cache( 'capi_errors', array( 600, array(), esc_html( print_r( $e, true ) ) ), $timeout );
252 }
253 } else if ( is_array( $e ) ) {
254 self::set_cache( 'capi_errors', array( 600, array(), esc_html( print_r( $e, true ) ) ), $timeout );
255 } else {
256 self::set_cache( 'capi_errors', array( 600, array(), esc_html( print_r( $e, true ) ) ), $timeout );
257 }
258 // Count Errors until midnight
259 $midnight = strtotime( "tomorrow 00:00:00" ); // UTC midnight
260 $midnight = $midnight + 8 * 3600; // UTC 8 AM
261 $tomidnight = $midnight - time();
262 $errors_count = self::get_cache( 'errors_count' );
263 $errors_count = (int) $errors_count + 1;
264 self::set_cache( 'errors_count', $errors_count, $tomidnight );
265 }
266
267 /**
268 * Anonymize sensitive data before displaying or reporting
269 * @param array $options
270 * @return string
271 */
272 public static function anonymize_options( $options ) {
273 global $wp_version;
274 if ( defined( 'SCRIPT_DEBUG' ) and SCRIPT_DEBUG ) {
275 return $options; // don't hide credentials when DEBUG is enabled
276 }
277 $options['wp_version'] = $wp_version;
278 $options['cawp_version'] = CAWP_CURRENT_VERSION;
279 if ( $options['sitekey'] ) {
280 $options['sitekey'] = 'HIDDEN';
281 }
282 if ( $options['siteid'] ) {
283 $options['siteid'] = 'HIDDEN';
284 }
285 return $options;
286 }
287
288 /**
289 * System details for the Debug screen
290 * @return string
291 */
292 public static function system_info() {
293 $info = '';
294 // Server Software
295 $server_soft = "-";
296 if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
297 $server_soft = $_SERVER['SERVER_SOFTWARE'];
298 }
299 $info .= 'Server Info: ' . $server_soft . "\n";
300 // PHP version
301 if ( defined( 'PHP_VERSION' ) ) {
302 $info .= 'PHP Version: ' . PHP_VERSION . "\n";
303 } else if ( defined( 'HHVM_VERSION' ) ) {
304 $info .= 'HHVM Version: ' . HHVM_VERSION . "\n";
305 } else {
306 $info .= 'Other Version: ' . '-' . "\n";
307 }
308 // cURL Info
309 if ( function_exists( 'curl_version' ) && function_exists( 'curl_exec' ) ) {
310 $curl_version = curl_version();
311 if ( ! empty( $curl_version ) ) {
312 $curl_ver = $curl_version['version'] . " " . $curl_version['ssl_version'];
313 } else {
314 $curl_ver = '-';
315 }
316 } else {
317 $curl_ver = '-';
318 }
319 $info .= 'cURL Info: ' . $curl_ver . "\n";
320 // Gzip
321 if ( is_callable( 'gzopen' ) ) {
322 $gzip = true;
323 } else {
324 $gzip = false;
325 }
326 $gzip_status = ( $gzip ) ? 'Yes' : 'No';
327 $info .= 'Gzip: ' . $gzip_status . "\n";
328 return $info;
329 }
330
331 /**
332 * Follows the SCRIPT_DEBUG settings
333 * @param string $script
334 * @return string
335 */
336 public static function script_debug_suffix() {
337 if ( defined( 'SCRIPT_DEBUG' ) and SCRIPT_DEBUG ) {
338 return '';
339 } else {
340 return '.min';
341 }
342 }
343
344 public static function number_to_kmb( $number ) {
345 $number_format = '';
346 if ( $number < 1000 ) {
347 // Anything less than a thousand
348 $number_format = number_format_i18n( $number );
349 } else if ( $number < 1000000 ) {
350 // Anything less than a milion
351 $number_format = number_format_i18n( $number / 1000, 2 ) . 'K';
352 } else if ( $number < 1000000000 ) {
353 // Anything less than a billion
354 $number_format = number_format_i18n( $number / 1000000, 2 ) . 'M';
355 } else {
356 // At least a billion
357 $number_format = number_format_i18n( $number / 1000000000, 2 ) . 'B';
358 }
359 return $number_format;
360 }
361
362 public static function secondstohms( $value ) {
363 $value = (float) $value;
364 $hours = floor( $value / 3600 );
365 $hours = $hours < 10 ? '0' . $hours : (string) $hours;
366 $minutes = floor( (int) ( $value / 60 ) % 60 );
367 $minutes = $minutes < 10 ? '0' . $minutes : (string) $minutes;
368 $seconds = floor( (int)$value % 60 );
369 $seconds = $seconds < 10 ? '0' . $seconds : (string) $seconds;
370 return $hours . ':' . $minutes . ':' . $seconds;
371 }
372
373 /** Keeps compatibility with WP < 5.3.0
374 *
375 * @return string
376 */
377 public static function timezone_string() {
378 $timezone_string = get_option( 'timezone_string' );
379
380 if ( $timezone_string ) {
381 return $timezone_string;
382 }
383
384 $offset = (float) get_option( 'gmt_offset' );
385 $hours = (int) $offset;
386 $minutes = ( $offset - $hours );
387
388 $sign = ( $offset < 0 ) ? '-' : '+';
389 $abs_hour = abs( $hours );
390 $abs_mins = abs( $minutes * 60 );
391 $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
392
393 return $tz_offset;
394 }
395
396 }
397 }
398