PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 2.0.13
Tracking Code Manager v2.0.13
trunk 1.11.8 1.11.9 1.12.0 1.12.1 1.12.2 1.12.3 1.4 1.5 2.0.0 2.0.1 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0
tracking-code-manager / includes / classes / utils / Utils.php
tracking-code-manager / includes / classes / utils Last commit date
Cron.php 3 years ago Ecommerce.php 3 years ago Language.php 3 years ago Logger.php 3 years ago MobileDetect.php 3 years ago Options.php 3 years ago Plugin.php 3 years ago Properties.php 3 years ago Tracking.php 3 years ago Utils.php 3 years ago
Utils.php
2062 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 class TCMP_Utils {
7 const FORMAT_DATETIME = 'd/m/Y H:i';
8 const FORMAT_COMPACT_DATETIME = 'd/m H:i';
9 const FORMAT_DATE = 'd/m/Y';
10 const FORMAT_TIME = 'H:i';
11
12 const FORMAT_SQL_DATETIME = 'Y-m-d H:i:s';
13 const FORMAT_SQL_DATE = 'Y-m-d';
14 const FORMAT_SQL_TIME = 'H:i:s';
15
16 private $color_index;
17 private $default_currency_symbol;
18
19 public function __construct() {
20 $this->color_index = 0;
21 }
22
23 public function setDefaultCurrencySymbol( $value ) {
24 $this->default_currency_symbol = $value;
25 }
26 public function getDefaultCurrencySymbol() {
27 return ( '' == $this->default_currency_symbol ? 'USD' : $this->default_currency_symbol );
28 }
29 function format( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
30 if ( $v1 || $v2 || $v3 || $v4 || $v5 ) {
31 $message = sprintf( $message, $v1, $v2, $v3, $v4, $v5 );
32 }
33 return $message;
34 }
35 function starts_with( $haystack, $needle ) {
36 $length = strlen( $needle );
37 return ( substr( $haystack, 0, $length ) === $needle );
38 }
39
40 function ends_with( $haystack, $needle ) {
41 $length = strlen( $needle );
42 $start = $length * -1; //negative
43 return ( substr( $haystack, $start ) === $needle );
44 }
45 function substr( $text, $start = 0, $end = -1 ) {
46 if ( $end < 0 ) {
47 $end = strlen( $text );
48 }
49 $length = $end - $start;
50 return substr( $text, $start, $length );
51 }
52
53 function shortcode_args( $args, $defaults ) {
54 $args = $this->sanitize_shortcode_keys( $args );
55 $defaults = $this->sanitize_shortcode_keys( $defaults );
56 $args = shortcode_atts( $defaults, $args );
57 return $args;
58 }
59 function sanitize_shortcode_keys( $array ) {
60 $result = array();
61 foreach ( $array as $k => $v ) {
62 if ( is_string( $k ) ) {
63 $k = strtolower( $k );
64 }
65 $result[ $k ] = $v;
66 }
67 return $result;
68 }
69
70 //WOW! $end is passed as reference due to we can change it if we found \n character after
71 //substring to avoid having these characters after or before
72 function substrln( $text, $start = 0, &$end = -1 ) {
73 if ( $end < 0 ) {
74 $end = strlen( $text );
75 }
76
77 do {
78 $loop = false;
79 $c = substr( $text, $end, 1 );
80 if ( "\n" == $c || "\r" == $c || '.' == $c ) {
81 $end += 1;
82 $loop = true;
83 }
84 } while ( $loop );
85
86 $length = $end - $start;
87 return substr( $text, $start, $length );
88 }
89
90 function toCommaArray( $array, $is_numeric = true, $is_trim = true ) {
91 if ( is_string( $array ) ) {
92 if ( trim( $array ) == '' ) {
93 $array = array();
94 } else {
95 $array = explode( ',', $array );
96 }
97 } elseif ( is_numeric( $array ) ) {
98 $array = array( $array );
99 }
100 if ( ! is_array( $array ) ) {
101 $array = array();
102 }
103 for ( $i = 0; $i < count( $array ); $i++ ) {
104 if ( $is_trim ) {
105 $array[ $i ] = trim( $array[ $i ] );
106 }
107 if ( $is_numeric ) {
108 $array[ $i ] = floatval( $array[ $i ] );
109 }
110 }
111 return $array;
112 }
113 function in_all_array( $search, $where ) {
114 return ( $this->inArray( -1, $where ) || $this->inArray( $search, $where ) );
115 }
116 function inArray( $search, $where ) {
117 $result = false;
118 $where = $this->to_array( $where );
119 $search = $this->to_array( $search );
120 if ( 0 == count( $where ) || 0 == count( $search ) ) {
121 return false;
122 }
123
124 foreach ( $where as $v ) {
125 $v .= '';
126 foreach ( $search as $c ) {
127 $c .= '';
128 if ( $v == $c ) {
129 $result = true;
130 break;
131 }
132 }
133
134 if ( $result ) {
135 break;
136 }
137 }
138 return $result;
139 }
140
141 function is( $name, $compare, $default = '', $ignore_case = true ) {
142 $what = $this->qs( $name, $default );
143 $result = false;
144 if ( is_string( $compare ) ) {
145 $compare = explode( ',', $compare );
146 }
147 if ( $ignore_case ) {
148 $what = strtolower( $what );
149 }
150
151 foreach ( $compare as $v ) {
152 if ( $ignore_case ) {
153 $v = strtolower( $v );
154 }
155 if ( $what == $v ) {
156 $result = true;
157 break;
158 }
159 }
160 return $result;
161 }
162
163 public function twitter( $name ) {
164 ?>
165 <a href="https://twitter.com/<?php echo esc_attr( $name ); ?>" class="twitter-follow-button" data-show-count="false" data-dnt="true">Follow @<?php echo esc_attr( $name ); ?></a>
166 <?php
167 }
168
169 public function sort( $is_associative, $a1, $a2 = null, $a3 = null, $a4 = null, $a5 = null ) {
170 $array = $this->merge( $is_associative, $a1, $a2, $a3, $a4, $a5 );
171 ksort( $array );
172 return $array;
173 }
174 public function merge( $is_associative, $a1, $a2 = null, $a3 = null, $a4 = null, $a5 = null ) {
175 $result = array();
176 if ( $is_associative ) {
177 $array = array( $a1, $a2, $a3, $a4, $a5 );
178 foreach ( $array as $a ) {
179 if ( ! is_array( $a ) ) {
180 continue;
181 }
182
183 foreach ( $a as $k => $v ) {
184 if ( ! isset( $result[ $k ] ) ) {
185 $result[ $k ] = $v;
186 }
187 }
188 }
189 } else {
190 $result = array_merge( $a1, $a2, $a3, $a4, $a5 );
191 }
192 return $result;
193 }
194
195 function bget( $instance, $name, $index = -1 ) {
196 $v = $this->get( $instance, $name, false, $index );
197 $v = $this->isTrue( $v );
198 return $v;
199 }
200 function dget( $instance, $name, $index = -1 ) {
201 $v = $this->get( $instance, $name, false, $index );
202 $v = $this->parse_date_to_time( $v );
203 return $v;
204 }
205 function aget( $instance, $name, $index = -1 ) {
206 $v = $this->get( $instance, $name, false, $index );
207 $v = $this->to_array( $v );
208 return $v;
209 }
210 function get( $instance, $name, $default = '', $index = -1 ) {
211 if ( $this->is_empty( $instance ) ) {
212 return $default;
213 }
214 $options = array();
215 //assolutamente da non fare altrimenti succede un disastro in quanto i metodi del inputComponent
216 //gli passano come name il valore...insomma un disastro!
217 //$name=$this->to_array($name);
218 //$name=implode('.', $name);
219
220 $result = $default;
221 if ( is_array( $instance ) || is_object( $instance ) ) {
222 if ( $this->propertyReflect( $instance, $name, $options ) ) {
223 $result = $options['get'];
224 }
225 }
226 if ( $index > -1 ) {
227 $result = $this->to_array( $result );
228 if ( isset( $result[ $index ] ) ) {
229 $result = $result[ $index ];
230 } else {
231 $result = $default;
232 }
233 }
234 return $result;
235 }
236 function has( $instance, $name ) {
237 return $this->propertyReflect( $instance, $name );
238 }
239 function set( &$instance, $name, $value ) {
240 $options = array( 'set' => $value );
241 $result = $this->propertyReflect( $instance, $name, $options );
242 if ( ! $result ) {
243 }
244 return $result;
245 }
246 function iget( $array, $name, $default = '' ) {
247 return intval( $this->get( $array, $name, $default ) );
248 }
249
250 private function propertyReflect( &$instance, $name, &$options = array() ) {
251 if ( ! is_object( $instance ) && ! is_array( $instance ) ) {
252 return false;
253 }
254
255 if ( false === $options || ! is_array( $options ) ) {
256 $options = array();
257 }
258 $options['has'] = false;
259 $options['get'] = false;
260
261 $current = $instance;
262 $names = explode( '.', $name );
263 $value = false;
264 $result = true;
265 for ( $i = 0; $i < count( $names ); $i++ ) {
266 $name = $names[ $i ];
267 if ( ! is_object( $current ) && ! is_array( $current ) ) {
268 return false;
269 }
270 if ( is_null( $current ) ) {
271 return false;
272 }
273
274 if ( is_object( $current ) ) {
275 if ( get_class( $current ) == 'stdClass' ) {
276 if ( isset( $current->$name ) ) {
277 $value = $current->$name;
278 } else {
279 $result = false;
280 }
281 } else {
282 $r = new ReflectionClass( $current );
283 try {
284 if ( $r->getProperty( $name ) !== false ) {
285 $value = $current->$name;
286 } else {
287 $result = false;
288 }
289 } catch ( Exception $ex ) {
290 if ( isset( $current->$name ) ) {
291 $value = $current->$name;
292 } else {
293 $result = false;
294 }
295 }
296 }
297 } elseif ( is_array( $current ) ) {
298 if ( isset( $current[ $name ] ) ) {
299 $value = $current[ $name ];
300 } else {
301 $result = false;
302 }
303 }
304
305 if ( ! $result ) {
306 break;
307 } elseif ( $i < ( count( $names ) - 1 ) ) {
308 $current = $value;
309 } else {
310 $options['get'] = $value;
311 if ( isset( $options['set'] ) ) {
312 if ( is_object( $current ) ) {
313 $current->$name = $options['set'];
314 } elseif ( is_array( $current ) ) {
315 $current[ $name ] = $options['set'];
316 }
317 }
318 }
319 }
320 return $result;
321 }
322 function isTrue( $value ) {
323 $result = false;
324 if ( is_bool( $value ) ) {
325 $result = (bool) $value;
326 } elseif ( is_numeric( $value ) ) {
327 $result = floatval( $value ) > 0;
328 } else {
329 $result = strtolower( $value );
330 if ( 'ok' == $result || 'yes' == $result || 'true' == $result || 'on' == $result ) {
331 $result = true;
332 } else {
333 $result = false;
334 }
335 }
336 return $result;
337 }
338 function aqs( $prefix, $remove_prefix = true ) {
339 $result = array();
340 $array = $this->merge( true, $_POST, $_GET );
341 foreach ( $array as $k => $v ) {
342 if ( $this->starts_with( $k, $prefix ) ) {
343 if ( $remove_prefix ) {
344 $k = substr( $k, strlen( $prefix ) );
345 }
346 $result[ $k ] = $v;
347 }
348 }
349 return $result;
350 }
351 function iqs( $name, $default = 0, $min = 0, $max = 0 ) {
352 $result = floatval( $this->qs( $name, $default ) );
353 if ( $min != $max ) {
354 if ( $result < $min ) {
355 $result = $min;
356 } elseif ( $result > $max ) {
357 $result = $max;
358 }
359 }
360 return $result;
361 }
362 function dqs( $name, $default = 0 ) {
363 $result = ( $this->qs( $name, $default ) );
364 $result = $this->parse_date_to_time( $result );
365 if ( 0 == $result ) {
366 $result = $default;
367 }
368 return $result;
369 }
370 //per ottenere un campo dal $_GET oppure dal $_POST
371 function qs( $name, $default = '' ) {
372 global $tcmp_allowed_html_tags;
373 $result = $default;
374 if ( isset( $_POST[ $name ] ) ) {
375 $result = $this->sanitize_post_or_get( $_POST[ $name ] );
376 } elseif ( isset( $_GET[ $name ] ) ) {
377 $result = $this->sanitize_post_or_get( $_GET[ $name ] );
378 }
379
380 if ( is_string( $result ) ) {
381 //The superglobals $_GET and $_REQUEST are already decoded.
382 //Using urldecode() on an element in $_GET or $_REQUEST
383 //could have unexpected and dangerous results.
384 //$result=urldecode($result);
385 $result = trim( $result );
386 }
387 return $result;
388 }
389
390 private function sanitize_post_or_get( $array ) {
391 global $tcmp_allowed_html_tags;
392 if ( is_array( $array ) ) {
393 foreach ( $array as $k => &$v ) {
394 if ( 'code' == $k ) {
395 $v = wp_kses( $v, $tcmp_allowed_html_tags );
396 } elseif ( is_string( $v ) ) {
397 $v = sanitize_text_field( $v );
398 }
399 }
400 }
401 return $array;
402 }
403
404 var $_taxonomyType;
405
406 function query( $query, $options = null ) {
407 global $tcmp, $wpdb;
408
409 $parent = '';
410 $defaults = array(
411 'post_type' => '',
412 'all' => false,
413 'select' => false,
414 'taxonomy' => '',
415 );
416 $options = wp_parse_args( $options, $defaults );
417
418 if ( ! isset( $options['type'] ) ) {
419 if ( '' != $options['post_type'] ) {
420 $options['type'] = $options['post_type'];
421 } elseif ( '' != $options['taxonomy'] ) {
422 $options['type'] = $options['taxonomy'];
423 } else {
424 $options['type'] = '';
425 }
426 }
427
428 if ( TCMP_QUERY_CONVERSION_PLUGINS == $query ) {
429 $array = $tcmp->ecommerce->getPlugins( false );
430 $result = array();
431 foreach ( $array as $k => $v ) {
432 $result[] = $v;
433 }
434 } else {
435 $key = array( 'Query', $query . '_' . $options['type'] );
436 $result = $tcmp->options->getCache( $key );
437 if ( ! is_array( $result ) || 0 == count( $result ) ) {
438 $q = null;
439 $id = 'ID';
440 $name = 'post_title';
441 $function = '';
442 switch ( $query ) {
443 case TCMP_QUERY_POSTS_OF_TYPE:
444 //$options=array('posts_per_page'=>-1, 'post_type'=>$args['post_type']);
445 //$q=get_posts($options);
446 $sql = 'SELECT ID, post_title FROM ' . $wpdb->prefix . "posts WHERE post_status='publish' AND post_type='" . $options['type'] . "' ORDER BY post_title";
447 $q = $wpdb->get_results( $sql );
448 $function = 'get_permalink';
449 break;
450 case TCMP_QUERY_CATEGORIES:
451 break;
452 case TCMP_QUERY_TAGS:
453 break;
454 case TCMP_QUERY_TAXONOMIES_OF_TYPE:
455 break;
456 }
457
458 $result = array();
459 if ( $q ) {
460 if ( ! is_wp_error( $q ) ) {
461 foreach ( $q as $v ) {
462 $item = array(
463 'id' => $v->$id,
464 'name' => $v->$name,
465 );
466 if ( '' != $parent ) {
467 $item['parent'] = $v->$parent;
468 }
469 $result[] = $item;
470 }
471 }
472 } elseif ( TCMP_QUERY_POST_TYPES == $query ) {
473 global $wp_post_types;
474 $result = array();
475 foreach ( $wp_post_types as $k => $v ) {
476 $is_public = $tcmp->utils->bget( $v, 'public' );
477 if ( $is_public && 'attachment' != $k ) {
478 $v = $tcmp->utils->get( $v, 'labels.singular_name' );
479 if ( 'post' == $k || 'page' == $k ) {
480 $result[ $k ] = $v;
481 }
482 }
483 }
484 $result = $tcmp->utils->toFormatListArrayFromListObjects( $result, false, '{text} ({id})' );
485 } elseif ( TCMP_QUERY_TAXONOMY_TYPES == $query ) {
486
487 }
488
489 if ( $this->functionExists( $function ) ) {
490 for ( $i = 0; $i < count( $result ); $i++ ) {
491 $v = $result[ $i ];
492 $v['url'] = $this->functionCall( $function, array( $v['id'] ) );
493 $result[ $i ] = $v;
494 }
495 }
496 $tcmp->options->setCache( $key, $result );
497 }
498 }
499
500 if ( $options['all'] ) {
501 $first = array();
502 $first[] = array(
503 'id' => -1,
504 'name' => '[' . $tcmp->lang->L( 'All' ) . ']',
505 'url' => '',
506 );
507 $result = array_merge( $first, $result );
508 }
509 if ( $options['select'] ) {
510 $first = array();
511 $first[] = array(
512 'id' => 0,
513 'name' => '[' . $tcmp->lang->L( 'Select' ) . ']',
514 'url' => '',
515 );
516 $result = array_merge( $first, $result );
517 }
518 $result = $this->sortOptions( $result );
519 $this->_taxonomyType = '';
520 return $result;
521 }
522
523 //wp_parse_args with null correction
524 function parseArgs( $options, $defaults ) {
525 if ( is_null( $options ) ) {
526 $options = array();
527 } elseif ( is_object( $options ) ) {
528 $options = (array) $options;
529 } elseif ( ! is_array( $options ) ) {
530 $options = array();
531 }
532 if ( is_null( $defaults ) ) {
533 $defaults = array();
534 } elseif ( is_object( $defaults ) ) {
535 $defaults = (array) $defaults;
536 } elseif ( ! is_array( $defaults ) ) {
537 $defaults = array();
538 }
539
540 foreach ( $defaults as $k => $v ) {
541 if ( is_null( $v ) ) {
542 unset( $defaults[ $k ] );
543 }
544 }
545
546 foreach ( $options as $k => $v ) {
547 if ( isset( $defaults[ $k ] ) ) {
548 if ( is_null( $v ) ) {
549 //so can take the default value
550 unset( $options[ $k ] );
551 } elseif ( is_string( $v ) && ( '' === $v ) && isset( $defaults[ $k ] ) && is_array( $defaults[ $k ] ) ) {
552 //a very strange case, i have a blank string for rappresenting an empty array
553 unset( $options[ $k ] );
554 } else {
555 unset( $defaults[ $k ] );
556 }
557 }
558 }
559 foreach ( $defaults as $k => $v ) {
560 $options[ $k ] = $v;
561 }
562 return $options;
563 }
564
565 function redirect( $location ) {
566 if ( '' == $location ) {
567 return;
568 }
569 ?>
570 <div id="tcmpRedirect" href="<?php echo esc_attr( $location ); ?>"></div>
571 <?php
572 die();
573 }
574
575 //return the element inside array with the specified key
576 function getArrayValue( $key, $array, $value = '' ) {
577 $result = false;
578 if ( isset( $array[ $key ] ) ) {
579 $result = $array[ $key ];
580 $result['name'] = $key;
581 }
582 if ( false !== $result && '' != $value ) {
583 if ( isset( $result[ $value ] ) ) {
584 $result = $result[ $value ];
585 }
586 }
587 return $result;
588 }
589
590 var $_sort_field;
591 var $_ignore_case;
592 function aksort( &$array, $sort_field = 'name', $ignore_case = true ) {
593 $this->_sort_field = $sort_field;
594 $this->_ignore_case = $ignore_case;
595 usort( $array, array( $this, 'aksortCompare' ) );
596 }
597 //not thread-safe!
598 private function aksortCompare( $a, $b ) {
599 if ( $a === $b || $a == $b ) {
600 return 0;
601 }
602
603 $result = 0;
604 $a = $a[ $this->_sort_field ];
605 $b = $b[ $this->_sort_field ];
606 if ( is_numeric( $a ) && is_numeric( $b ) ) {
607 $result = ( $a < $b ) ? -1 : 1;
608 } else {
609 $a .= '';
610 $b .= '';
611 if ( $this->_ignore_case ) {
612 $result = strcasecmp( $a, $b );
613 } else {
614 $result = strcmp( $a . '', $b );
615 }
616 }
617 return $result;
618 }
619
620 public function formatCustomDate( $time, $format ) {
621 $time = $this->parse_date_to_time( $time );
622 if ( $time > 0 ) {
623 $time = date( $format, $time );
624 } else {
625 $time = '';
626 }
627 return $time;
628 }
629
630 public function formatDatetime( $time = 'now' ) {
631 return $this->formatCustomDate( $time, TCMP_Utils::FORMAT_DATETIME );
632 }
633 public function formatCompactDatetime( $time = 'now' ) {
634 return $this->formatCustomDate( $time, TCMP_Utils::FORMAT_COMPACT_DATETIME );
635 }
636 public function formatDate( $time = 'date' ) {
637 return $this->formatCustomDate( $time, TCMP_Utils::FORMAT_DATE );
638 }
639 public function formatSmartDatetime( $time = 'now' ) {
640 $time = $this->parse_date_to_time( $time );
641 $result = '';
642 if ( $time > 0 ) {
643 $h = intval( date( 'H', $time ) );
644 $i = intval( date( 'i', $time ) );
645 $s = intval( date( 's', $time ) );
646 if ( 0 == $h && 0 == $i && 0 == $s ) {
647 $result = $this->formatDate( $time );
648 } else {
649 $result = $this->formatDatetime( $time );
650 }
651 }
652 return $result;
653 }
654 public function formatTime( $time = 'now' ) {
655 return $this->formatCustomTime( $time, TCMP_Utils::FORMAT_TIME );
656 }
657 public function formatSqlDatetime( $time = 'now' ) {
658 return $this->formatCustomDate( $time, TCMP_Utils::FORMAT_SQL_DATETIME );
659 }
660 public function formatSqlDate( $time = 'date' ) {
661 return $this->formatCustomDate( $time, TCMP_Utils::FORMAT_SQL_DATE );
662 }
663 public function formatSqlTime( $time = 'now' ) {
664 return $this->formatCustomTime( $time, TCMP_Utils::FORMAT_SQL_TIME );
665 }
666
667 private function formatCustomTime( $time, $format ) {
668 $time = $this->parse_date_to_time( $time );
669 if ( $time > 86400 ) {
670 $h = date( 'H', $time );
671 $i = date( 'i', $time );
672 $s = date( 's', $time );
673 $time = $h * 3600 + $i * 60 + $s;
674 }
675
676 $s = $time % 60;
677 $time = ( $time - $s ) / 60;
678 $i = $time % 60;
679 $h = ( $time - $i ) / 60;
680 $s = str_pad( $s, 2, '0', STR_PAD_LEFT );
681 $i = str_pad( $i, 2, '0', STR_PAD_LEFT );
682 $h = str_pad( $h, 2, '0', STR_PAD_LEFT );
683 $format = str_replace( 'H', $h, $format );
684 $format = str_replace( 'i', $i, $format );
685 $format = str_replace( 's', $s, $format );
686 return $format;
687 }
688
689 public function parseNumber( $what, $default = 0 ) {
690 $result = $default;
691 if ( is_array( $what ) ) {
692 if ( count( $what ) > 0 ) {
693 $result = doubleval( $what[0] );
694 }
695 } elseif ( is_numeric( $what ) ) {
696 $result = doubleval( $what );
697 } elseif ( is_string( $what ) || is_bool( $what ) ) {
698 $result = ( $this->isTrue( $what ) ? 1 : 0 );
699 }
700 return $result;
701 }
702 public function parseDateToArray( $date ) {
703 global $tcmp;
704
705 $pm = false;
706 $date = strtoupper( trim( $date ) );
707 if ( $tcmp->utils->ends_with( $date, 'AM' ) ) {
708 $date = substr( $date, 0, strlen( $date ) - 2 );
709 $date = trim( $date );
710 } elseif ( $tcmp->utils->ends_with( $date, 'PM' ) ) {
711 $date = substr( $date, 0, strlen( $date ) - 2 );
712 $date = trim( $date );
713 $pm = true;
714 }
715
716 $date = explode( ' ', $date );
717 if ( 1 == count( $date ) ) {
718 $result = array();
719 $date = $date[0];
720 $date = str_replace( '/', '-', $date );
721 if ( strpos( $date, '-' ) !== false ) {
722 $date = explode( '-', $date );
723 if ( count( $date ) >= 3 ) {
724 $d = intval( $date[0] );
725 $m = intval( $date[1] );
726 $y = intval( $date[2] );
727 if ( $d > 1900 ) {
728 $t = $d;
729 $d = $y;
730 $y = $t;
731 }
732 if ( $y > 0 && $m > 0 && $d > 0 ) {
733 $result['y'] = $y;
734 $result['m'] = $m;
735 $result['d'] = $d;
736 }
737 }
738 } elseif ( strpos( $date, ':' ) !== false ) {
739 $date = explode( ':', $date );
740 if ( 2 == count( $date ) ) {
741 $date[] = 0;
742 }
743 if ( count( $date ) >= 3 ) {
744 $h = intval( $date[0] );
745 $i = intval( $date[1] );
746 $s = intval( $date[2] );
747 if ( $h >= 0 && $i >= 0 && $s >= 0 ) {
748 $result['h'] = $h;
749 $result['i'] = $i;
750 $result['s'] = $s;
751 }
752 }
753 }
754 } else {
755 $a1 = $this->parseDateToArray( $date[0] );
756 $a2 = $this->parseDateToArray( $date[1] );
757 $result = $tcmp->utils->parseArgs( $a1, $a2 );
758 }
759
760 if ( $pm && isset( $result['h'] ) ) {
761 $result['h'] = intval( $result['h'] ) + 12;
762 }
763 return $result;
764 }
765 public function parse_date_to_time( $date ) {
766 global $tcmp;
767 if ( is_numeric( $date ) || trim( $date ) == '' ) {
768 $date = intval( $date );
769 return $date;
770 }
771
772 $date = strtolower( $date );
773 if ( 'now' == $date ) {
774 $date = time();
775 return $date;
776 } elseif ( 'date' == $date ) {
777 $date = strtotime( date( 'Y-m-d', time() ) );
778 return $date;
779 } elseif ( 'time' == $date ) {
780 $date = date( 'H:i:s', time() );
781 }
782 $result = $this->parseDateToArray( $date );
783 $defaults = array(
784 'y' => 0,
785 'm' => 0,
786 'd' => 0,
787 'h' => 0,
788 'i' => 0,
789 's' => 0,
790 );
791 $a = $tcmp->utils->parseArgs( $result, $defaults );
792 if ( 0 == $a['y'] && 0 == $a['m'] && 0 == $a['d'] ) {
793 $result = $a['h'] * 3600 + $a['i'] * 60 + $a['s'];
794 } else {
795 $result = mktime( $a['h'], $a['i'], $a['s'], $a['m'], $a['d'], $a['y'] );
796 }
797 if ( $result < 0 ) {
798 $result = 0;
799 }
800 return $result;
801 }
802 public function getIntDate( $time, $separator = '' ) {
803 $time = $this->parse_date_to_time( $time );
804 if ( $time > 0 ) {
805 if ( '' == $separator ) {
806 $time = date( 'Ymd', $time );
807 $time = intval( $time );
808 } else {
809 $time = date( 'Y', $time ) . $separator . date( 'm', $time ) . $separator . date( 'd', $time );
810 }
811 }
812
813 return $time;
814 }
815 public function getIntMinute( $h, $m, $separator = '' ) {
816 $h = intval( $h );
817 $m = intval( $m );
818 if ( $m < 10 ) {
819 $m = '0' . $m;
820 }
821 $result = $h . $separator . $m;
822 if ( '' == $separator ) {
823 $result = intval( $result );
824 }
825 return $result;
826 }
827
828 //args can be a string or an associative array if you want
829 public function get_text_args( $args, $defaults = array(), $excludes = array() ) {
830 $result = $args;
831 $excludes = $this->to_array( $excludes );
832 if ( is_array( $result ) && count( $result ) > 0 ) {
833 $result = '';
834 foreach ( $args as $k => $v ) {
835 if ( is_array( $v ) || is_object( $v ) ) {
836 continue;
837 }
838
839 if ( 0 == count( $excludes ) || ! in_array( $k, $excludes ) ) {
840 $v = trim( $v );
841 $result .= ' ' . $k . '="' . $v . '"';
842 }
843 }
844 } elseif ( ! $args ) {
845 $result = '';
846 }
847 if ( is_array( $defaults ) && count( $defaults ) > 0 ) {
848 foreach ( $defaults as $k => $v ) {
849 if ( 0 == count( $excludes ) || ! in_array( $k, $excludes ) ) {
850 if ( ! isset( $args[ $k ] ) ) {
851 $v = trim( $v );
852 $result .= ' ' . $k . '="' . $v . '"';
853 }
854 }
855 }
856 }
857 return $result;
858 }
859
860 public function iuarray( $ids, $positive = false ) {
861 $array = $this->iarray( $ids, $positive );
862 $array = array_unique( $array );
863 sort( $array );
864 return $array;
865 }
866 public function iarray( $ids, $positive = false ) {
867 if ( is_string( $ids ) ) {
868 $ids = explode( ',', $ids );
869 } elseif ( is_numeric( $ids ) ) {
870 $ids = array( $ids );
871 } elseif ( ! is_array( $ids ) ) {
872 $ids = array();
873 }
874
875 $array = array();
876 foreach ( $ids as $v ) {
877 $v = trim( $v );
878 if ( '' != $v ) {
879 $v = intval( $v );
880 if ( ! $positive || $v > 0 ) {
881 $array[] = $v;
882 }
883 }
884 }
885 return $array;
886 }
887 public function dbarray( $ids ) {
888 if ( is_string( $ids ) ) {
889 $ids = explode( ',', $ids );
890 } elseif ( is_numeric( $ids ) ) {
891 $ids = array( $ids );
892 } elseif ( ! is_array( $ids ) ) {
893 $ids = array();
894 }
895
896 $array = array();
897 foreach ( $ids as $v ) {
898 $v = trim( $v );
899 if ( '' != $v ) {
900 if ( is_numeric( $v ) ) {
901 $v = intval( $v );
902 }
903 $array[] = $v;
904 }
905 }
906 return $array;
907 }
908
909 function is_associativeArray( $array ) {
910 if ( ! is_array( $array ) ) {
911 return false;
912 }
913
914 $isArray = true;
915 $i = 0;
916 foreach ( $array as $k => $v ) {
917 if ( $k !== $i ) {
918 $isArray = false;
919 break;
920 }
921 ++$i;
922 }
923 return ! $isArray;
924 }
925 function trim( $value ) {
926 if ( is_null( $value ) ) {
927
928 } elseif ( is_string( $value ) ) {
929 $value = trim( $value );
930 } elseif ( is_numeric( $value ) ) {
931
932 } elseif ( $this->is_associativeArray( $value ) ) {
933 foreach ( $value as $k => $v ) {
934 $value[ $k ] = $this->trim( $v );
935 }
936 } elseif ( is_object( $value ) ) {
937 foreach ( $value as $k => $v ) {
938 $value->$k = $this->trim( $v );
939 }
940 } elseif ( is_array( $value ) ) {
941 for ( $i = 0; $i < count( $value ); $i++ ) {
942 $v = $value[ $i ];
943 $this->trim( $v );
944 $value[ $i ] = $v;
945 }
946 }
947 return $value;
948 }
949 function implode( $open, $close, $join, $array ) {
950 $result = '';
951 foreach ( $array as $v ) {
952 if ( '' != $result ) {
953 $result .= $join;
954 }
955 $result .= $open . $v . $close;
956 }
957 return $result;
958 }
959 function to_array( $text, $index = -1, $default = '' ) {
960 if ( is_array( $text ) ) {
961 if ( is_string( $index ) ) {
962 $array = array();
963 foreach ( $text as $v ) {
964 $v = $this->get( $v, $index, false );
965 if ( false !== $v ) {
966 $array[] = $v;
967 }
968 }
969 } else {
970 $array = $text;
971 }
972 return $array;
973 } elseif ( is_numeric( $text ) ) {
974 return array( $text );
975 } elseif ( is_bool( $text ) || '' === $text ) {
976 return array();
977 }
978
979 if ( ( $this->starts_with( $text, '[' ) && $this->ends_with( $text, ']' ) )
980 || ( $this->starts_with( $text, '{' ) && $this->ends_with( $text, '}' ) ) ) {
981 $text = substr( $text, 1, strlen( $text ) - 2 );
982 }
983 $text = str_replace( '|', ',', $text );
984 $text = explode( ',', $text );
985
986 //exclude empty string
987 $array = array();
988 foreach ( $text as $t ) {
989 if ( '' !== $t ) {
990 $array[] = $t;
991 }
992 }
993 $text = $array;
994 if ( $index > -1 ) {
995 $result = $default;
996 if ( isset( $text[ $index ] ) ) {
997 $result = $text[ $index ];
998 }
999 $text = $result;
1000 }
1001 return $text;
1002 }
1003 function dirToFlatArray( $dir, &$output ) {
1004 if ( ! isset( $output['dirs'] ) ) {
1005 $output['dirs'] = array();
1006 }
1007 if ( ! isset( $output['files'] ) ) {
1008 $output['files'] = array();
1009 }
1010
1011 $cdir = scandir( $dir );
1012 foreach ( $cdir as $k => $v ) {
1013 if ( ! in_array( $v, array( '.', '..' ) ) ) {
1014 if ( is_dir( $dir . DIRECTORY_SEPARATOR . $v ) ) {
1015 $i = $dir . DIRECTORY_SEPARATOR . $v;
1016 array_push( $output['dirs'], $i );
1017 $this->dirToFlatArray( $i, $output );
1018 } else {
1019 $i = $this->getFileInfo( $dir . DIRECTORY_SEPARATOR . $v );
1020 array_push( $output['files'], $i );
1021 }
1022 }
1023 }
1024 }
1025 function dirToArray( $dir ) {
1026 $result = array();
1027 if ( ! is_string( $dir ) ) {
1028 return $result;
1029 }
1030
1031 $cdir = scandir( $dir );
1032 foreach ( $cdir as $k => $v ) {
1033 if ( ! in_array( $v, array( '.', '..' ) ) ) {
1034 if ( is_dir( $dir . DIRECTORY_SEPARATOR . $v ) ) {
1035 $result[ $v ] = $this->dirToArray( $dir . DIRECTORY_SEPARATOR . $v );
1036 } else {
1037 $result[] = $this->getFileInfo( $dir . DIRECTORY_SEPARATOR . $v );
1038 }
1039 }
1040 }
1041 return $result;
1042 }
1043 function getFileInfo( $source ) {
1044 $source = $this->toDirectory( $source );
1045 if ( ! file_exists( $source ) ) {
1046 return false;
1047 }
1048
1049 $array = explode( DIRECTORY_SEPARATOR, $source );
1050 $size = filesize( $source );
1051 $source = array_pop( $array );
1052 $directory = implode( DIRECTORY_SEPARATOR, $array ) . DIRECTORY_SEPARATOR;
1053
1054 $pos = strrpos( $source, '.' );
1055 $ext = '';
1056 if ( false !== $pos ) {
1057 $name = substr( $source, 0, $pos );
1058 $ext = strtolower( substr( $source, $pos ) );
1059 }
1060 $array = array(
1061 'directory' => $directory,
1062 'name' => $name,
1063 'file' => $source,
1064 'size' => $size,
1065 'textSize' => $this->getFileTextSize( $size ),
1066 'ext' => $ext,
1067 'textExt' => $this->getFileTextExt( $source ),
1068 );
1069 return $array;
1070 }
1071 function getFileTextSize( $size ) {
1072 $units = array( 'B', 'KB', 'MB', 'GB' );
1073 for ( $i = 0; $i < count( $units ); $i++ ) {
1074 if ( $size < 1024 ) {
1075 break;
1076 } else {
1077 $size /= 1024;
1078 }
1079 }
1080 return intval( $size ) . ' ' . $units[ $i ];
1081 }
1082 function getFileTextExt( $source ) {
1083 $ext = strrpos( $source, '.' );
1084 if ( false !== $ext ) {
1085 $ext = strtolower( substr( $source, $ext + 1 ) );
1086 } else {
1087 $ext = $source;
1088 }
1089 $ext = strtolower( $ext );
1090 $text = 'text';
1091 switch ( $ext ) {
1092 case 'doc':
1093 case 'docx':
1094 case 'odt':
1095 $text = 'word';
1096 break;
1097 case 'xls':
1098 case 'xlsx':
1099 case 'ods':
1100 $text = 'excel';
1101 break;
1102 case 'ppt':
1103 case 'pptx':
1104 case 'odp':
1105 $text = 'powerpoint';
1106 break;
1107 case 'zip':
1108 case 'tar':
1109 case 'gzip':
1110 case 'rar':
1111 case '7z':
1112 $text = 'archive';
1113 break;
1114 case 'mp3':
1115 case 'wav':
1116 $text = 'audio';
1117 break;
1118 case 'mpeg':
1119 case 'mpg':
1120 case 'avi':
1121 case 'mp4':
1122 $text = 'video';
1123 break;
1124 case 'gif':
1125 case 'jpg':
1126 case 'jpeg':
1127 case 'png':
1128 case 'bmp':
1129 $text = 'image';
1130 break;
1131 case 'pdf':
1132 $text = 'pdf';
1133 break;
1134 }
1135 return $text;
1136 }
1137 function match( $value, $array, $default = '', $ignore_case = true ) {
1138 $result = $default;
1139 if ( $ignore_case ) {
1140 $value = strtolower( $value );
1141 }
1142 foreach ( $array as $k => $v ) {
1143 $v = $this->to_array( $v );
1144 foreach ( $v as $c ) {
1145 if ( $ignore_case ) {
1146 $c = strtolower( $c );
1147 }
1148 if ( $value == $c || strpos( $value, $c ) !== false ) {
1149 $result = $k;
1150 break;
1151 }
1152 }
1153
1154 if ( $result !== $default ) {
1155 break;
1156 }
1157 }
1158 return $result;
1159 }
1160
1161 function pickColor() {
1162 $names = explode( '|', 'primary|success|warning|danger|info|alert|system|dark' );
1163 $colors = explode( '|', '3498db|70ca63|f6bb42|df5640|3bafda|967adc|37bc9b|666' );
1164
1165 $i = ( $this->color_index % count( $colors ) );
1166 $names = $names[ $i ];
1167 $colors = $colors[ $i ];
1168 ++$this->color_index;
1169 return array( $names, '#' . $colors );
1170 }
1171 function upperUnderscoreCase( $text ) {
1172 $text = $this->arrayCase( $text );
1173 $text = implode( '_', $text );
1174 $text = strtoupper( $text );
1175 return $text;
1176 }
1177 function lowerUnderscoreCase( $text ) {
1178 $text = $this->upperUnderscoreCase( $text );
1179 $text = strtolower( $text );
1180 return $text;
1181 }
1182 function toDirectory( $file, $mkdirs = false ) {
1183 $file = str_replace( '\\', DIRECTORY_SEPARATOR, $file );
1184 $file = str_replace( '/', DIRECTORY_SEPARATOR, $file );
1185 $file = str_replace( DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $file );
1186
1187 if ( is_dir( $file ) && ! file_exists( $file ) && $mkdirs ) {
1188 mkdir( $file, 0777, true );
1189 }
1190 return $file;
1191 }
1192 function getUploadName( $name ) {
1193 if ( '' == $name ) {
1194 return '';
1195 }
1196
1197 $name = $this->toDirectory( $name );
1198 $name = explode( DIRECTORY_SEPARATOR, $name );
1199 $name = $name[ count( $name ) - 1 ];
1200 $ext = '';
1201 $pos = strpos( $name, '.' );
1202 if ( false !== $pos ) {
1203 $ext = substr( $name, $pos );
1204 $name = substr( $name, 0, $pos );
1205 }
1206
1207 $buffer = '';
1208 $name = str_split( strtolower( $name ) );
1209 for ( $i = 0; $i < count( $name ); $i++ ) {
1210 if ( $name[ $i ] >= 'a' && $name[ $i ] <= 'z' ) {
1211 $buffer .= $name[ $i ];
1212 } else {
1213 $buffer .= ' ';
1214 }
1215 }
1216 while ( strpos( $buffer, ' ' ) !== false ) {
1217 $buffer = str_replace( ' ', ' ', $buffer );
1218 }
1219 $buffer = trim( $buffer );
1220 $buffer = str_replace( ' ', '-', $buffer );
1221 $buffer .= '-' . date( 'Ymd-His', time() ) . $ext;
1222 return $buffer;
1223 }
1224 function toListArrayFromClass( $array, $id = false, $value = false ) {
1225 global $tcmp;
1226 $result = array();
1227 if ( false !== $array && count( $array ) > 0 ) {
1228 foreach ( $array as $k => $v ) {
1229 if ( false !== $id ) {
1230 $k = $tcmp->utils->get( $v, $id );
1231 }
1232 if ( false !== $value ) {
1233 $v = $tcmp->utils->get( $v, $value );
1234 }
1235
1236 if ( '' != $k && '' != $v ) {
1237 $result[] = array(
1238 'id' => $k,
1239 'text' => $v,
1240 'name' => $v,
1241 );
1242 }
1243 }
1244 }
1245 return $result;
1246 }
1247 function toFormatListArrayFromListObjects( $array, $id_field, $textFormat ) {
1248 global $tcmp;
1249 $result = array();
1250 if ( false !== $array && count( $array ) > 0 ) {
1251 foreach ( $array as $i => $e ) {
1252 $text = $textFormat;
1253 $id_exists = false;
1254 if ( is_array( $e ) || is_object( $e ) ) {
1255 foreach ( $e as $k => $v ) {
1256 if ( 'id' == $k ) {
1257 $id_exists = true;
1258 }
1259 if ( is_array( $v ) ) {
1260 $v = implode( ', ', $v );
1261 }
1262 $text = str_replace( '{' . $k . '}', $v, $text );
1263 }
1264 } else {
1265 $text = str_replace( '{text}', $e, $text );
1266 }
1267
1268 $id = $i;
1269 if ( false !== $id_field && '' !== $id_field ) {
1270 $id = $tcmp->utils->get( $e, $id_field, '' );
1271 }
1272
1273 if ( ! $id_exists ) {
1274 $text = str_replace( '{id}', $id, $text );
1275 }
1276 if ( '' != $id ) {
1277 $result[] = array(
1278 'id' => $id,
1279 'text' => $text,
1280 'name' => $text,
1281 );
1282 }
1283 }
1284 }
1285 return $result;
1286 }
1287 function toListArrayFromListObjects( $array, $id_from = false, $textFrom = 'name', $idTo = 'id', $textTo = 'text' ) {
1288
1289 $result = array();
1290 foreach ( $array as $v ) {
1291 $s_id = $v;
1292 $s_text = $v;
1293 if ( false !== $id_from ) {
1294 $s_id = $this->get( $v, $id_from, false );
1295 $s_text = $this->get( $v, $textFrom, false );
1296 }
1297 if ( false !== $s_id && '' != $s_text ) {
1298 if ( '' != $s_id ) {
1299 $result[] = array(
1300 $idTo => $s_id,
1301 $textTo => $s_text,
1302 );
1303 }
1304 }
1305 }
1306 return $result;
1307 }
1308 function toColorListArrayFromListObjects( $array, $colors, $id = 'id', $text = 'name' ) {
1309 global $tcmp;
1310 $result = array();
1311 foreach ( $array as $instance ) {
1312 $s_id = $this->get( $instance, $id, false );
1313 $s_text = $this->get( $instance, $text, false );
1314 foreach ( $colors as $color => $when ) {
1315 $success = false;
1316 foreach ( $when['conditions'] as $conditionKey => $condition_value ) {
1317 $condition_value = $tcmp->utils->to_array( $condition_value );
1318 $c = $this->get( $instance, $conditionKey, false );
1319 if ( false !== $c ) {
1320 $c .= '';
1321 foreach ( $condition_value as $v ) {
1322 $v .= '';
1323 if ( $c === $v ) {
1324 $success = true;
1325 break;
1326 }
1327 }
1328 }
1329 if ( $success ) {
1330 break;
1331 }
1332 }
1333
1334 if ( $success ) {
1335 $style = 'color:' . $color . '; ';
1336 if ( isset( $when['bold'] ) && $when['bold'] ) {
1337 $style .= 'font-weight:bold; ';
1338 }
1339 $s_text = '<span style="' . $style . '">' . $s_text . '</span>';
1340 }
1341 }
1342 if ( '' != $s_id && false !== $s_text ) {
1343 $result[] = array(
1344 'id' => $s_id,
1345 'text' => $s_text,
1346 'name' => $s_text,
1347 );
1348 }
1349 }
1350 return $result;
1351 }
1352 function md5() {
1353 $array = func_get_args();
1354 $buffer = '';
1355 foreach ( $array as $v ) {
1356 $buffer .= ':)' . $v;
1357 }
1358 $buffer = md5( $buffer );
1359 return $buffer;
1360 }
1361 function arrayCase( $text ) {
1362 $buffer = '';
1363 $array = array();
1364 $text = str_split( $text );
1365 $prev_upper = false;
1366 $next_upper = false;
1367 foreach ( $text as $c ) {
1368 if ( $c >= 'a' && $c <= 'z' ) {
1369 if ( $next_upper ) {
1370 if ( '' != $buffer ) {
1371 $array[] = $buffer;
1372 $buffer = '';
1373 }
1374 $c = strtoupper( $c );
1375 }
1376 $buffer .= $c;
1377 $next_upper = false;
1378 $prev_upper = false;
1379 } elseif ( $c >= '0' && $c <= '9' ) {
1380 $buffer .= $c;
1381 $next_upper = true;
1382 } elseif ( $c >= 'A' && $c <= 'Z' ) {
1383 if ( ! $prev_upper ) {
1384 if ( '' != $buffer ) {
1385 $array[] = $buffer;
1386 $buffer = '';
1387 }
1388 }
1389 $buffer .= $c;
1390 $next_upper = false;
1391 $prev_upper = true;
1392 } else {
1393 if ( '' != $buffer ) {
1394 $array[] = $buffer;
1395 $buffer = '';
1396 }
1397 $next_upper = true;
1398 $prev_upper = false;
1399 }
1400 }
1401 if ( '' != $buffer ) {
1402 $array[] = $buffer;
1403 }
1404 return $array;
1405 }
1406 function lowerCamelCase( $text ) {
1407 $buffer = '';
1408 if ( strpos( $text, '_' ) !== false || strpos( $text, '-' ) !== false ) {
1409 $text = strtolower( $text );
1410 }
1411
1412 $text = str_split( $text );
1413 $all_upper = true;
1414 $next_upper = false;
1415 foreach ( $text as $c ) {
1416 if ( $c >= 'a' && $c <= 'z' ) {
1417 $all_upper = false;
1418 if ( $next_upper ) {
1419 $c = strtoupper( $c );
1420 }
1421 $buffer .= $c;
1422 $next_upper = false;
1423 } elseif ( $c >= '0' && $c <= '9' ) {
1424 $buffer .= $c;
1425 $next_upper = true;
1426 } elseif ( $c >= 'A' && $c <= 'Z' ) {
1427 $buffer .= $c;
1428 $next_upper = false;
1429 } else {
1430 $next_upper = true;
1431 }
1432 }
1433 if ( $all_upper ) {
1434 $buffer = strtolower( $buffer );
1435 } else {
1436 $buffer = lcfirst( $buffer );
1437 }
1438 return $buffer;
1439 }
1440 function upperCamelCase( $text ) {
1441 $text = $this->lowerCamelCase( $text );
1442 $text = ucfirst( $text );
1443 return $text;
1444 }
1445
1446 function castStdClass( $a ) {
1447 $a = (array) $a;
1448 $r = new stdClass();
1449 foreach ( $a as $k => $v ) {
1450 $r->$k = $v;
1451 }
1452 return $r;
1453 }
1454 function castArray( $a ) {
1455 $r = $a;
1456 if ( is_object( $a ) ) {
1457 $r = (array) $a;
1458 }
1459
1460 if ( ! is_array( $r ) ) {
1461 $r = array();
1462 }
1463 return $r;
1464 }
1465 public function copyArray( $array ) {
1466 $temp = array();
1467 foreach ( $array as $k => $v ) {
1468 $temp[ $k ] = $v;
1469 }
1470 return $temp;
1471 }
1472 public function isObject( $v ) {
1473 return ( false !== $v && ! is_null( $v ) && is_object( $v ) );
1474 }
1475 public function isArray( $v ) {
1476 return ( false !== $v && ! is_null( $v ) && is_array( $v ) );
1477 }
1478 public function getConstants( $class, $prefix, $reverse = false ) {
1479 global $tcmp;
1480 if ( is_object( $class ) ) {
1481 $class = get_class( $class );
1482 }
1483 $class = str_replace( 'Search', '', $class );
1484 $class = str_replace( 'Constants', '', $class );
1485 $class .= 'Constants';
1486 if ( ! class_exists( $class ) ) {
1487 $class = TCMP_PLUGIN_PREFIX . $class;
1488 }
1489
1490 $result = array();
1491 if ( class_exists( $class ) ) {
1492 $reflection = new ReflectionClass( $class );
1493 $array = $reflection->getConstants();
1494 foreach ( $array as $k => $v ) {
1495 $pos = 0;
1496 if ( '' != $prefix ) {
1497 $pos = stripos( $k, $prefix );
1498 }
1499 if ( 0 === $pos ) {
1500 if ( $reverse ) {
1501 $result[ $v ] = $k;
1502 } else {
1503 $result[ $k ] = $v;
1504 }
1505 }
1506 }
1507 }
1508 return $result;
1509 }
1510 public function getConstantValue( $class, $prefix, $name, $default = false ) {
1511 /* @var $ec TCMP_Singleton */
1512 global $ec;
1513 $result = $default;
1514 if ( is_object( $class ) ) {
1515 $class = get_class( $class );
1516 }
1517 $class = str_replace( 'Search', '', $class );
1518 $class = str_replace( 'Constants', '', $class );
1519 $class .= 'Constants';
1520 if ( ! class_exists( $class ) ) {
1521 $class = TCMP_PLUGIN_PREFIX . $class;
1522 }
1523
1524 if ( class_exists( $class ) ) {
1525 $name = $prefix . '_' . $name;
1526 $name = $ec->utils->upperUnderscoreCase( $name );
1527 $reflection = new ReflectionClass( $class );
1528 $result = $reflection->getConstant( $name );
1529 }
1530 return $result;
1531 }
1532 public function getConstantName( $class, $prefix, $value, $default = false ) {
1533 /* @var $ec TCMP_Singleton */
1534 $constants = $this->getConstants( $class, $prefix, true );
1535 $result = $default;
1536 if ( isset( $constants[ $value ] ) ) {
1537 $result = $constants[ $value ];
1538 }
1539 return $result;
1540 }
1541 public function daysDiff( $dt1, $dt2 ) {
1542 $dt1 = $this->parse_date_to_time( $dt1 );
1543 $dt2 = $this->parse_date_to_time( $dt2 );
1544 $result = ( $dt2 - $dt1 ) / 86400;
1545 $result = intval( $result );
1546 return $result;
1547 }
1548
1549 public function getText( $text, $args ) {
1550 if ( false === $args || 0 == count( $args ) ) {
1551 return $text;
1552 }
1553
1554 foreach ( $args as $k => $v ) {
1555 $text = str_replace( '{' . $k . '}', $v, $text );
1556 }
1557 return $text;
1558 }
1559 public function arrayExtends( $options, $defaults ) {
1560 global $tcmp;
1561 $options = $tcmp->utils->parseArgs( $options, $defaults );
1562 foreach ( $options as $k => $v ) {
1563 if ( is_bool( $v ) ) {
1564 $v = ( $v ? 1 : 0 );
1565 }
1566 if ( isset( $defaults[ $k ] ) ) {
1567 if ( $this->is_associativeArray( $v ) ) {
1568 $v = $this->arrayExtends( $v, $defaults[ $k ] );
1569 } else {
1570 $v = $tcmp->utils->to_array( $v );
1571 $old = $defaults[ $k ];
1572 $old = $tcmp->utils->to_array( $old );
1573 if ( ! $this->is_associativeArray( $old ) ) {
1574 $v = array_merge( $v, $old );
1575 $v = array_unique( $v );
1576 }
1577 }
1578 } else {
1579 $v = $tcmp->utils->to_array( $v );
1580 }
1581 $options[ $k ] = $v;
1582 }
1583 return $options;
1584 }
1585 //send remote request to our server to store tracking and feedback
1586 function remotePost( $action, $data = '' ) {
1587 global $tcmp;
1588
1589 $data['secret'] = 'WYSIWYG';
1590 $response = wp_remote_post(
1591 TCMP_INTELLYWP_ENDPOINT . '?iwpm_action=' . $action,
1592 array(
1593 'method' => 'POST',
1594 'timeout' => 20,
1595 'redirection' => 5,
1596 'httpversion' => '1.1',
1597 'blocking' => true,
1598 'body' => $data,
1599 'user-agent' => TCMP_PLUGIN_NAME . '/' . TCMP_PLUGIN_VERSION . '; ' . get_bloginfo( 'url' ),
1600 )
1601 );
1602 $data = json_decode( wp_remote_retrieve_body( $response ), true );
1603 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200
1604 || ! isset( $data['success'] ) || ! $data['success']
1605 ) {
1606 $tcmp->log->error( 'ERRORS SENDING REMOTE-POST ACTION=%s DUE TO REASON=%s', $action, $response );
1607 $data = false;
1608 } else {
1609 $tcmp->log->debug( 'SUCCESSFULLY SENT REMOTE-POST ACTION=%s RESPONSE=%s', $action, $data );
1610 }
1611 return $data;
1612 }
1613
1614 function isAdminUser() {
1615 //https://wordpress.org/support/topic/how-to-check-admin-right-without-include-pluggablephp
1616 return true;
1617 }
1618
1619 function isPluginPage() {
1620 global $tcmp;
1621 $page = tcmp_sqs( 'page' );
1622 $result = ( $this->starts_with( $page, TCMP_PLUGIN_SLUG ) );
1623 return $result;
1624 }
1625
1626 public function arrayPush( &$array, $another ) {
1627 if ( ! is_array( $another ) ) {
1628 array_push( $array, $another );
1629 } elseif ( is_array( $another ) ) {
1630 foreach ( $another as $v ) {
1631 array_push( $array, $v );
1632 }
1633 }
1634 return $array;
1635 }
1636
1637 public function getConstantsValues( $class, $prefix = '', $glue = false ) {
1638 $array = $this->getConstants( $class, $prefix );
1639 $result = array_values( $array );
1640 if ( false !== $glue ) {
1641 $result = implode( $glue, $result );
1642 }
1643 return $result;
1644
1645 }
1646 public function getValue( $array, $index, $default = false ) {
1647 $result = $this->get_index( $array, $index, $default );
1648 if ( $result !== $default ) {
1649 $result = $result['v'];
1650 }
1651 return $result;
1652 }
1653 public function get_key( $array, $index, $default = false ) {
1654 $result = $this->get_index( $array, $index, $default );
1655 if ( $result !== $default ) {
1656 $result = $result['k'];
1657 }
1658 return $result;
1659 }
1660 public function get_index( $array, $index, $default = false ) {
1661 $result = $default;
1662 if ( is_array( $array ) && count( $array ) > 0 ) {
1663 if ( $this->is_associativeArray( $array ) ) {
1664 $i = 0;
1665 foreach ( $array as $k => $v ) {
1666 if ( $index == $i ) {
1667 $result = array(
1668 'k' => $k,
1669 'v' => $v,
1670 );
1671 break;
1672 }
1673 $i++;
1674 }
1675 } else {
1676 if ( $index < count( $array ) && $index >= 0 ) {
1677 $result = $array[ $index ];
1678 }
1679 }
1680 }
1681 return $result;
1682 }
1683 public function is_empty( $v ) {
1684 if ( ! $v ) {
1685 return true;
1686 }
1687
1688 $result = false;
1689 if ( is_string( $v ) ) {
1690 $result = ( '' == $v );
1691 } elseif ( is_array( $v ) ) {
1692 $result = 0 == count( $v );
1693 } elseif ( is_object( $v ) ) {
1694 $result = true;
1695 foreach ( $v as $k => $w ) {
1696 if ( ! is_null( $w ) && '' !== $w ) {
1697 $result = false;
1698 break;
1699 }
1700 }
1701 }
1702 return $result;
1703 }
1704 public function httpEncode( $v ) {
1705 $v = gzcompress( $v );
1706 $v = bin2hex( $v );
1707 return $v;
1708 }
1709 public function httpDecode( $v ) {
1710 $v = hex2bin( $v );
1711 $v = gzuncompress( $v );
1712 return $v;
1713 }
1714 public function trimHttp( $uri ) {
1715 $uri = str_replace( 'http://', '', $uri );
1716 $uri = str_replace( 'https://', '', $uri );
1717 return $uri;
1718 }
1719 function getClientIpAddress() {
1720 $ipaddress = '';
1721 if ( getenv( 'HTTP_CLIENT_IP' ) ) {
1722 $ipaddress = getenv( 'HTTP_CLIENT_IP' );
1723 } elseif ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
1724 $ipaddress = getenv( 'HTTP_X_FORWARDED_FOR' );
1725 } elseif ( getenv( 'HTTP_X_FORWARDED' ) ) {
1726 $ipaddress = getenv( 'HTTP_X_FORWARDED' );
1727 } elseif ( getenv( 'HTTP_FORWARDED_FOR' ) ) {
1728 $ipaddress = getenv( 'HTTP_FORWARDED_FOR' );
1729 } elseif ( getenv( 'HTTP_FORWARDED' ) ) {
1730 $ipaddress = getenv( 'HTTP_FORWARDED' );
1731 } elseif ( getenv( 'REMOTE_ADDR' ) ) {
1732 $ipaddress = getenv( 'REMOTE_ADDR' );
1733 } else {
1734 $ipaddress = 'UNKNOWN';
1735 }
1736 $ipaddress = ( '::1' == $ipaddress ) ? '192.168.0.1' : $ipaddress;
1737 return $ipaddress;
1738 }
1739 public function isMail( $mail ) {
1740 $at = strpos( $mail, '@' );
1741 $dot = strrpos( $mail, '.' );
1742 $result = false;
1743 if ( false !== $at && false !== $dot && $at < $dot ) {
1744 $result = true;
1745 }
1746 return $result;
1747 }
1748 public function getNameFromListArray( $array, $id, $default = false ) {
1749 $result = $default;
1750 foreach ( $array as $v ) {
1751 if ( $v['id'] == $id ) {
1752 if ( isset( $v['text'] ) ) {
1753 $result = $v['text'];
1754 break;
1755 } elseif ( isset( $v['name'] ) ) {
1756 $result = $v['name'];
1757 break;
1758 }
1759 }
1760 }
1761 return $result;
1762 }
1763 function bqs( $name, $default = false ) {
1764 $v = $this->qs( $name, '' );
1765 $result = $default;
1766 if ( '' != $v ) {
1767 if ( is_numeric( $v ) ) {
1768 $v = intval( $v );
1769 $result = ( $v > 0 );
1770 } else {
1771 $result = $this->isTrue( $v );
1772 }
1773 }
1774 return $result;
1775 }
1776
1777 function getFunctionName( $function ) {
1778 $result = false;
1779 if ( is_string( $function ) ) {
1780 $result = $function;
1781 } elseif ( is_array( $function ) ) {
1782 $result = $function[1];
1783 }
1784 return $result;
1785 }
1786 function functionExists( $function ) {
1787 $result = false;
1788 if ( is_string( $function ) ) {
1789 $result = function_exists( $function );
1790 } elseif ( is_array( $function ) ) {
1791 $result = method_exists( $function[0], $function[1] );
1792 } elseif ( is_callable( $function ) ) {
1793 $result = true;
1794 }
1795 return $result;
1796 }
1797 function functionCall() {
1798 $args = func_get_args();
1799 if ( false === $args || 0 == count( $args ) ) {
1800 return;
1801 }
1802
1803 $function = array_shift( $args );
1804 $result = null;
1805 if ( $this->functionExists( $function ) ) {
1806 $result = call_user_func_array( $function, $args );
1807 }
1808 return $result;
1809 }
1810
1811 public function contains( $v1, $v2, $ignore_case = true ) {
1812 $result = false;
1813 if ( $ignore_case ) {
1814 $result = stripos( $v1, $v2 ) !== false;
1815 } else {
1816 $result = strpos( $v1, $v2 ) !== false;
1817 }
1818 return $result;
1819 }
1820
1821 private function getHtmlCode( $value ) {
1822 $value = str_replace( '\"', '', $value );
1823 $value = str_replace( '"', '', $value );
1824 return $value;
1825 }
1826
1827 public function dequeueScripts( $array ) {
1828 if ( ! function_exists( 'wp_scripts' ) || function_exists( 'wp_dequeue_script' ) ) {
1829 return;
1830 }
1831
1832 $array = $this->to_array( $array );
1833 $scripts = wp_scripts();
1834 /* @var $v _WP_Dependency */
1835 foreach ( $scripts->registered as $k => $v ) {
1836 foreach ( $array as $pattern ) {
1837 if ( $this->contains( $v->src, $pattern ) || $this->contains( $v->handle, $pattern ) ) {
1838 wp_dequeue_script( $v->handle );
1839 break;
1840 }
1841 }
1842 }
1843 }
1844 public function dequeueStyles( $array ) {
1845 if ( ! function_exists( 'wp_styles' ) || function_exists( 'wp_dequeue_style' ) ) {
1846 return;
1847 }
1848
1849 $array = $this->to_array( $array );
1850 $styles = wp_styles();
1851 /* @var $v _WP_Dependency */
1852 foreach ( $styles->registered as $k => $v ) {
1853 foreach ( $array as $pattern ) {
1854 if ( $this->contains( $v->src, $pattern ) || $this->contains( $v->handle, $pattern ) ) {
1855 wp_dequeue_style( $v->handle );
1856 break;
1857 }
1858 }
1859 }
1860 }
1861 public function formatSeconds( $time ) {
1862 if ( '' === $time ) {
1863 return '';
1864 }
1865
1866 $time = intval( $time );
1867 $seconds = ( $time % 60 );
1868 $time = ( ( $time - $seconds ) / 60 );
1869 $minutes = ( $time % 60 );
1870 $time = ( ( $time - $minutes ) / 60 );
1871 $hours = ( $time % 24 );
1872 $time = ( ( $time - $hours ) / 24 );
1873 $days = $time;
1874
1875 $array = array();
1876 if ( $seconds > 0 ) {
1877 $array[] = $seconds . 's';
1878 }
1879 if ( $minutes > 0 ) {
1880 $array[] = $minutes . 'm';
1881 }
1882 if ( $hours > 0 ) {
1883 $array[] = $hours . 'h';
1884 }
1885 if ( $days > 0 ) {
1886 $array[] = $days . 'd';
1887 }
1888 $array = array_reverse( $array );
1889 $text = implode( ' ', $array );
1890 return $text;
1891 }
1892
1893 function formatPercentage( $value, $options = array() ) {
1894 if ( is_bool( $options ) ) {
1895 $options = array( 'symbol' => $options );
1896 }
1897 $defaults = array( 'symbol' => true );
1898 $options = $this->parseArgs( $options, $defaults );
1899
1900 $value = floatval( $value );
1901 $value = round( $value, 3 );
1902 $value = number_format( $value, 3, ',', '' );
1903 if ( $options['symbol'] ) {
1904 $value .= ' %';
1905 }
1906 return $value;
1907 }
1908 function formatCurrencyMoney( $value, $options = array() ) {
1909 $defaults = array( 'currency' => $this->getDefaultCurrencySymbol() );
1910 $options = $this->parseArgs( $options, $defaults );
1911
1912 $value = $this->formatMoney( $value, $options );
1913 return $value;
1914 }
1915 function formatMoney( $value, $options = array() ) {
1916 if ( is_string( $options ) ) {
1917 $options = array( 'currency' => $options );
1918 }
1919 $defaults = array( 'currency' => false );
1920 $options = $this->parseArgs( $options, $defaults );
1921
1922 $value = floatval( $value );
1923 $value = round( $value, 3 );
1924 $value = number_format( $value, 3, ',', '.' );
1925 if ( '' != $options['currency'] ) {
1926 $symbol = $options['currency'];
1927 if ( strlen( $symbol ) > 1 ) {
1928 $symbol = $this->getCurrencySymbol( $symbol );
1929 }
1930 $value .= ' ' . $symbol;
1931 }
1932 return $value;
1933 }
1934 function sortOptions( &$options ) {
1935 if ( ! is_array( $options ) ) {
1936 return $options;
1937 }
1938
1939 usort( $options, array( $this, 'sortOptions_Compare' ) );
1940 return $options;
1941 }
1942 public function sortOptions_Compare( $o1, $o2 ) {
1943 global $tcmp;
1944 $v1 = $tcmp->utils->get( $o1, 'text', false );
1945 if ( false == $v1 ) {
1946 $v1 = $tcmp->utils->get( $o1, 'name', false );
1947 }
1948 $v2 = $tcmp->utils->get( $o2, 'text', false );
1949 if ( false == $v2 ) {
1950 $v2 = $tcmp->utils->get( $o2, 'name', false );
1951 }
1952
1953 //to order properly
1954 if ( $tcmp->utils->starts_with( $v1, '[' ) ) {
1955 $v1 = ' ' . $v1;
1956 }
1957 if ( $tcmp->utils->starts_with( $v2, '[' ) ) {
1958 $v2 = ' ' . $v2;
1959 }
1960 return strcasecmp( $v1, $v2 );
1961 }
1962
1963 private function validate_ip( $ip ) {
1964 if ( filter_var( $ip, FILTER_VALIDATE_IP ) ) {
1965 return $ip;
1966 }
1967 return '';
1968 }
1969
1970 public function getVisitorIpAddress() {
1971 $ip = '';
1972 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
1973 $ip = validate_ip( $_SERVER['HTTP_CLIENT_IP'] );
1974 }
1975
1976 if ( '' == $ip && ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
1977 $ip = validate_ip( $_SERVER['HTTP_X_FORWARDED_FOR'] );
1978 }
1979
1980 if ( '' == $ip ) {
1981 $ip = validate_ip( $_SERVER['REMOTE_ADDR'] );
1982 }
1983 return $ip;
1984 }
1985
1986 function getCurrencySymbol( $currency ) {
1987 // Create a NumberFormatter
1988 $locale = 'en_US';
1989 $formatter = new NumberFormatter( $locale, NumberFormatter::CURRENCY );
1990
1991 // Figure out what 0.00 looks like with the currency symbol
1992 $withCurrency = $formatter->formatCurrency( 0, $currency );
1993
1994 // Figure out what 0.00 looks like without the currency symbol
1995 $formatter->setPattern( str_replace( '¤', '', $formatter->getPattern() ) );
1996 $without_currency = $formatter->formatCurrency( 0, $currency );
1997
1998 // Extract just the currency symbol from the first string
1999 return str_replace( $without_currency, '', $withCurrency );
2000 }
2001 function encodeUri( $string ) {
2002 $entities = array( '%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%23', '%5B', '%5D' );
2003 $replacements = array( '!', '*', "'", '(', ')', ';', ':', '@', '&', '=', '+', '$', ',', '/', '?', '#', '[', ']' );
2004 $result = urlencode( $string );
2005 //$result=str_replace($replacements, $entities, $result);
2006 return $result;
2007 }
2008
2009 public function formatTimer( $time ) {
2010 if ( ! is_int( $time ) ) {
2011 if ( is_string( $time ) ) {
2012 $time = str_replace( ' ', ':', $time );
2013 $time = str_replace( '.', ':', $time );
2014 $time = str_replace( '/', ':', $time );
2015 $time = explode( ':', $time );
2016
2017 $length = count( $time );
2018 $days = 0;
2019 $hours = 0;
2020 $minutes = 0;
2021 $secs = intval( $time[ $length - 1 ] );
2022
2023 if ( $length > 1 ) {
2024 $minutes = intval( $time[ $length - 2 ] );
2025 if ( $length > 2 ) {
2026 $hours = intval( $time[ $length - 3 ] );
2027 if ( $length > 3 ) {
2028 $days = intval( $time[ $length - 4 ] );
2029 }
2030 }
2031 }
2032 $time = $days * 86400 + $hours * 3600 + $minutes * 60 + $secs;
2033 } else {
2034 $time = 0;
2035 }
2036 } else {
2037 $time = intval( $time );
2038 }
2039
2040 $secs = $time % 60;
2041 $time = ( $time - $secs ) / 60;
2042 $minutes = $time % 60;
2043 $time = ( $time - $minutes ) / 60;
2044 $hours = $time % 24;
2045 $days = ( $time - $hours ) / 24;
2046
2047 $result = array();
2048 $result[] = $days;
2049 $result[] = ( $hours < 10 ? '0' : '' ) . $hours;
2050 $result[] = ( $minutes < 10 ? '0' : '' ) . $minutes;
2051 $result[] = ( $secs < 10 ? '0' : '' ) . $secs;
2052 $result = implode( ':', $result );
2053 return $result;
2054 }
2055 public function parseTimer( $time ) {
2056 $time = $this->formatTimer( $time );
2057 $time = explode( ':', $time );
2058 $result = intval( $time[0] ) * 86400 + intval( $time[1] ) * 3600 + intval( $time[2] ) * 60 + intval( $time[3] );
2059 return $result;
2060 }
2061 }
2062