PluginProbe
Clicky Analytics / 2.0
Clicky Analytics v2.0
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 2.0, at tools/tools.php

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