PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 2.2.0
Tracking Code Manager v2.2.0
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 2 years ago
Utils.php
2065 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_null($date) ) {
768 $date = 'now';
769 }
770 if ( is_numeric( $date ) || trim( $date ) == '' ) {
771 $date = intval( $date );
772 return $date;
773 }
774
775 $date = strtolower( $date );
776 if ( 'now' == $date ) {
777 $date = time();
778 return $date;
779 } elseif ( 'date' == $date ) {
780 $date = strtotime( date( 'Y-m-d', time() ) );
781 return $date;
782 } elseif ( 'time' == $date ) {
783 $date = date( 'H:i:s', time() );
784 }
785 $result = $this->parseDateToArray( $date );
786 $defaults = array(
787 'y' => 0,
788 'm' => 0,
789 'd' => 0,
790 'h' => 0,
791 'i' => 0,
792 's' => 0,
793 );
794 $a = $tcmp->utils->parseArgs( $result, $defaults );
795 if ( 0 == $a['y'] && 0 == $a['m'] && 0 == $a['d'] ) {
796 $result = $a['h'] * 3600 + $a['i'] * 60 + $a['s'];
797 } else {
798 $result = mktime( $a['h'], $a['i'], $a['s'], $a['m'], $a['d'], $a['y'] );
799 }
800 if ( $result < 0 ) {
801 $result = 0;
802 }
803 return $result;
804 }
805 public function getIntDate( $time, $separator = '' ) {
806 $time = $this->parse_date_to_time( $time );
807 if ( $time > 0 ) {
808 if ( '' == $separator ) {
809 $time = date( 'Ymd', $time );
810 $time = intval( $time );
811 } else {
812 $time = date( 'Y', $time ) . $separator . date( 'm', $time ) . $separator . date( 'd', $time );
813 }
814 }
815
816 return $time;
817 }
818 public function getIntMinute( $h, $m, $separator = '' ) {
819 $h = intval( $h );
820 $m = intval( $m );
821 if ( $m < 10 ) {
822 $m = '0' . $m;
823 }
824 $result = $h . $separator . $m;
825 if ( '' == $separator ) {
826 $result = intval( $result );
827 }
828 return $result;
829 }
830
831 //args can be a string or an associative array if you want
832 public function get_text_args( $args, $defaults = array(), $excludes = array() ) {
833 $result = $args;
834 $excludes = $this->to_array( $excludes );
835 if ( is_array( $result ) && count( $result ) > 0 ) {
836 $result = '';
837 foreach ( $args as $k => $v ) {
838 if ( is_array( $v ) || is_object( $v ) ) {
839 continue;
840 }
841
842 if ( 0 == count( $excludes ) || ! in_array( $k, $excludes ) ) {
843 $v = trim( $v );
844 $result .= ' ' . $k . '="' . $v . '"';
845 }
846 }
847 } elseif ( ! $args ) {
848 $result = '';
849 }
850 if ( is_array( $defaults ) && count( $defaults ) > 0 ) {
851 foreach ( $defaults as $k => $v ) {
852 if ( 0 == count( $excludes ) || ! in_array( $k, $excludes ) ) {
853 if ( ! isset( $args[ $k ] ) ) {
854 $v = trim( $v );
855 $result .= ' ' . $k . '="' . $v . '"';
856 }
857 }
858 }
859 }
860 return $result;
861 }
862
863 public function iuarray( $ids, $positive = false ) {
864 $array = $this->iarray( $ids, $positive );
865 $array = array_unique( $array );
866 sort( $array );
867 return $array;
868 }
869 public function iarray( $ids, $positive = false ) {
870 if ( is_string( $ids ) ) {
871 $ids = explode( ',', $ids );
872 } elseif ( is_numeric( $ids ) ) {
873 $ids = array( $ids );
874 } elseif ( ! is_array( $ids ) ) {
875 $ids = array();
876 }
877
878 $array = array();
879 foreach ( $ids as $v ) {
880 $v = trim( $v );
881 if ( '' != $v ) {
882 $v = intval( $v );
883 if ( ! $positive || $v > 0 ) {
884 $array[] = $v;
885 }
886 }
887 }
888 return $array;
889 }
890 public function dbarray( $ids ) {
891 if ( is_string( $ids ) ) {
892 $ids = explode( ',', $ids );
893 } elseif ( is_numeric( $ids ) ) {
894 $ids = array( $ids );
895 } elseif ( ! is_array( $ids ) ) {
896 $ids = array();
897 }
898
899 $array = array();
900 foreach ( $ids as $v ) {
901 $v = trim( $v );
902 if ( '' != $v ) {
903 if ( is_numeric( $v ) ) {
904 $v = intval( $v );
905 }
906 $array[] = $v;
907 }
908 }
909 return $array;
910 }
911
912 function is_associativeArray( $array ) {
913 if ( ! is_array( $array ) ) {
914 return false;
915 }
916
917 $isArray = true;
918 $i = 0;
919 foreach ( $array as $k => $v ) {
920 if ( $k !== $i ) {
921 $isArray = false;
922 break;
923 }
924 ++$i;
925 }
926 return ! $isArray;
927 }
928 function trim( $value ) {
929 if ( is_null( $value ) ) {
930
931 } elseif ( is_string( $value ) ) {
932 $value = trim( $value );
933 } elseif ( is_numeric( $value ) ) {
934
935 } elseif ( $this->is_associativeArray( $value ) ) {
936 foreach ( $value as $k => $v ) {
937 $value[ $k ] = $this->trim( $v );
938 }
939 } elseif ( is_object( $value ) ) {
940 foreach ( $value as $k => $v ) {
941 $value->$k = $this->trim( $v );
942 }
943 } elseif ( is_array( $value ) ) {
944 for ( $i = 0; $i < count( $value ); $i++ ) {
945 $v = $value[ $i ];
946 $this->trim( $v );
947 $value[ $i ] = $v;
948 }
949 }
950 return $value;
951 }
952 function implode( $open, $close, $join, $array ) {
953 $result = '';
954 foreach ( $array as $v ) {
955 if ( '' != $result ) {
956 $result .= $join;
957 }
958 $result .= $open . $v . $close;
959 }
960 return $result;
961 }
962 function to_array( $text, $index = -1, $default = '' ) {
963 if ( is_array( $text ) ) {
964 if ( is_string( $index ) ) {
965 $array = array();
966 foreach ( $text as $v ) {
967 $v = $this->get( $v, $index, false );
968 if ( false !== $v ) {
969 $array[] = $v;
970 }
971 }
972 } else {
973 $array = $text;
974 }
975 return $array;
976 } elseif ( is_numeric( $text ) ) {
977 return array( $text );
978 } elseif ( is_bool( $text ) || '' === $text ) {
979 return array();
980 }
981
982 if ( ( $this->starts_with( $text, '[' ) && $this->ends_with( $text, ']' ) )
983 || ( $this->starts_with( $text, '{' ) && $this->ends_with( $text, '}' ) ) ) {
984 $text = substr( $text, 1, strlen( $text ) - 2 );
985 }
986 $text = str_replace( '|', ',', $text );
987 $text = explode( ',', $text );
988
989 //exclude empty string
990 $array = array();
991 foreach ( $text as $t ) {
992 if ( '' !== $t ) {
993 $array[] = $t;
994 }
995 }
996 $text = $array;
997 if ( $index > -1 ) {
998 $result = $default;
999 if ( isset( $text[ $index ] ) ) {
1000 $result = $text[ $index ];
1001 }
1002 $text = $result;
1003 }
1004 return $text;
1005 }
1006 function dirToFlatArray( $dir, &$output ) {
1007 if ( ! isset( $output['dirs'] ) ) {
1008 $output['dirs'] = array();
1009 }
1010 if ( ! isset( $output['files'] ) ) {
1011 $output['files'] = array();
1012 }
1013
1014 $cdir = scandir( $dir );
1015 foreach ( $cdir as $k => $v ) {
1016 if ( ! in_array( $v, array( '.', '..' ) ) ) {
1017 if ( is_dir( $dir . DIRECTORY_SEPARATOR . $v ) ) {
1018 $i = $dir . DIRECTORY_SEPARATOR . $v;
1019 array_push( $output['dirs'], $i );
1020 $this->dirToFlatArray( $i, $output );
1021 } else {
1022 $i = $this->getFileInfo( $dir . DIRECTORY_SEPARATOR . $v );
1023 array_push( $output['files'], $i );
1024 }
1025 }
1026 }
1027 }
1028 function dirToArray( $dir ) {
1029 $result = array();
1030 if ( ! is_string( $dir ) ) {
1031 return $result;
1032 }
1033
1034 $cdir = scandir( $dir );
1035 foreach ( $cdir as $k => $v ) {
1036 if ( ! in_array( $v, array( '.', '..' ) ) ) {
1037 if ( is_dir( $dir . DIRECTORY_SEPARATOR . $v ) ) {
1038 $result[ $v ] = $this->dirToArray( $dir . DIRECTORY_SEPARATOR . $v );
1039 } else {
1040 $result[] = $this->getFileInfo( $dir . DIRECTORY_SEPARATOR . $v );
1041 }
1042 }
1043 }
1044 return $result;
1045 }
1046 function getFileInfo( $source ) {
1047 $source = $this->toDirectory( $source );
1048 if ( ! file_exists( $source ) ) {
1049 return false;
1050 }
1051
1052 $array = explode( DIRECTORY_SEPARATOR, $source );
1053 $size = filesize( $source );
1054 $source = array_pop( $array );
1055 $directory = implode( DIRECTORY_SEPARATOR, $array ) . DIRECTORY_SEPARATOR;
1056
1057 $pos = strrpos( $source, '.' );
1058 $ext = '';
1059 if ( false !== $pos ) {
1060 $name = substr( $source, 0, $pos );
1061 $ext = strtolower( substr( $source, $pos ) );
1062 }
1063 $array = array(
1064 'directory' => $directory,
1065 'name' => $name,
1066 'file' => $source,
1067 'size' => $size,
1068 'textSize' => $this->getFileTextSize( $size ),
1069 'ext' => $ext,
1070 'textExt' => $this->getFileTextExt( $source ),
1071 );
1072 return $array;
1073 }
1074 function getFileTextSize( $size ) {
1075 $units = array( 'B', 'KB', 'MB', 'GB' );
1076 for ( $i = 0; $i < count( $units ); $i++ ) {
1077 if ( $size < 1024 ) {
1078 break;
1079 } else {
1080 $size /= 1024;
1081 }
1082 }
1083 return intval( $size ) . ' ' . $units[ $i ];
1084 }
1085 function getFileTextExt( $source ) {
1086 $ext = strrpos( $source, '.' );
1087 if ( false !== $ext ) {
1088 $ext = strtolower( substr( $source, $ext + 1 ) );
1089 } else {
1090 $ext = $source;
1091 }
1092 $ext = strtolower( $ext );
1093 $text = 'text';
1094 switch ( $ext ) {
1095 case 'doc':
1096 case 'docx':
1097 case 'odt':
1098 $text = 'word';
1099 break;
1100 case 'xls':
1101 case 'xlsx':
1102 case 'ods':
1103 $text = 'excel';
1104 break;
1105 case 'ppt':
1106 case 'pptx':
1107 case 'odp':
1108 $text = 'powerpoint';
1109 break;
1110 case 'zip':
1111 case 'tar':
1112 case 'gzip':
1113 case 'rar':
1114 case '7z':
1115 $text = 'archive';
1116 break;
1117 case 'mp3':
1118 case 'wav':
1119 $text = 'audio';
1120 break;
1121 case 'mpeg':
1122 case 'mpg':
1123 case 'avi':
1124 case 'mp4':
1125 $text = 'video';
1126 break;
1127 case 'gif':
1128 case 'jpg':
1129 case 'jpeg':
1130 case 'png':
1131 case 'bmp':
1132 $text = 'image';
1133 break;
1134 case 'pdf':
1135 $text = 'pdf';
1136 break;
1137 }
1138 return $text;
1139 }
1140 function match( $value, $array, $default = '', $ignore_case = true ) {
1141 $result = $default;
1142 if ( $ignore_case ) {
1143 $value = strtolower( $value );
1144 }
1145 foreach ( $array as $k => $v ) {
1146 $v = $this->to_array( $v );
1147 foreach ( $v as $c ) {
1148 if ( $ignore_case ) {
1149 $c = strtolower( $c );
1150 }
1151 if ( $value == $c || strpos( $value, $c ) !== false ) {
1152 $result = $k;
1153 break;
1154 }
1155 }
1156
1157 if ( $result !== $default ) {
1158 break;
1159 }
1160 }
1161 return $result;
1162 }
1163
1164 function pickColor() {
1165 $names = explode( '|', 'primary|success|warning|danger|info|alert|system|dark' );
1166 $colors = explode( '|', '3498db|70ca63|f6bb42|df5640|3bafda|967adc|37bc9b|666' );
1167
1168 $i = ( $this->color_index % count( $colors ) );
1169 $names = $names[ $i ];
1170 $colors = $colors[ $i ];
1171 ++$this->color_index;
1172 return array( $names, '#' . $colors );
1173 }
1174 function upperUnderscoreCase( $text ) {
1175 $text = $this->arrayCase( $text );
1176 $text = implode( '_', $text );
1177 $text = strtoupper( $text );
1178 return $text;
1179 }
1180 function lowerUnderscoreCase( $text ) {
1181 $text = $this->upperUnderscoreCase( $text );
1182 $text = strtolower( $text );
1183 return $text;
1184 }
1185 function toDirectory( $file, $mkdirs = false ) {
1186 $file = str_replace( '\\', DIRECTORY_SEPARATOR, $file );
1187 $file = str_replace( '/', DIRECTORY_SEPARATOR, $file );
1188 $file = str_replace( DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $file );
1189
1190 if ( is_dir( $file ) && ! file_exists( $file ) && $mkdirs ) {
1191 mkdir( $file, 0777, true );
1192 }
1193 return $file;
1194 }
1195 function getUploadName( $name ) {
1196 if ( '' == $name ) {
1197 return '';
1198 }
1199
1200 $name = $this->toDirectory( $name );
1201 $name = explode( DIRECTORY_SEPARATOR, $name );
1202 $name = $name[ count( $name ) - 1 ];
1203 $ext = '';
1204 $pos = strpos( $name, '.' );
1205 if ( false !== $pos ) {
1206 $ext = substr( $name, $pos );
1207 $name = substr( $name, 0, $pos );
1208 }
1209
1210 $buffer = '';
1211 $name = str_split( strtolower( $name ) );
1212 for ( $i = 0; $i < count( $name ); $i++ ) {
1213 if ( $name[ $i ] >= 'a' && $name[ $i ] <= 'z' ) {
1214 $buffer .= $name[ $i ];
1215 } else {
1216 $buffer .= ' ';
1217 }
1218 }
1219 while ( strpos( $buffer, ' ' ) !== false ) {
1220 $buffer = str_replace( ' ', ' ', $buffer );
1221 }
1222 $buffer = trim( $buffer );
1223 $buffer = str_replace( ' ', '-', $buffer );
1224 $buffer .= '-' . date( 'Ymd-His', time() ) . $ext;
1225 return $buffer;
1226 }
1227 function toListArrayFromClass( $array, $id = false, $value = false ) {
1228 global $tcmp;
1229 $result = array();
1230 if ( false !== $array && count( $array ) > 0 ) {
1231 foreach ( $array as $k => $v ) {
1232 if ( false !== $id ) {
1233 $k = $tcmp->utils->get( $v, $id );
1234 }
1235 if ( false !== $value ) {
1236 $v = $tcmp->utils->get( $v, $value );
1237 }
1238
1239 if ( '' != $k && '' != $v ) {
1240 $result[] = array(
1241 'id' => $k,
1242 'text' => $v,
1243 'name' => $v,
1244 );
1245 }
1246 }
1247 }
1248 return $result;
1249 }
1250 function toFormatListArrayFromListObjects( $array, $id_field, $textFormat ) {
1251 global $tcmp;
1252 $result = array();
1253 if ( false !== $array && count( $array ) > 0 ) {
1254 foreach ( $array as $i => $e ) {
1255 $text = $textFormat;
1256 $id_exists = false;
1257 if ( is_array( $e ) || is_object( $e ) ) {
1258 foreach ( $e as $k => $v ) {
1259 if ( 'id' == $k ) {
1260 $id_exists = true;
1261 }
1262 if ( is_array( $v ) ) {
1263 $v = implode( ', ', $v );
1264 }
1265 $text = str_replace( '{' . $k . '}', $v, $text );
1266 }
1267 } else {
1268 $text = str_replace( '{text}', $e, $text );
1269 }
1270
1271 $id = $i;
1272 if ( false !== $id_field && '' !== $id_field ) {
1273 $id = $tcmp->utils->get( $e, $id_field, '' );
1274 }
1275
1276 if ( ! $id_exists ) {
1277 $text = str_replace( '{id}', $id, $text );
1278 }
1279 if ( '' != $id ) {
1280 $result[] = array(
1281 'id' => $id,
1282 'text' => $text,
1283 'name' => $text,
1284 );
1285 }
1286 }
1287 }
1288 return $result;
1289 }
1290 function toListArrayFromListObjects( $array, $id_from = false, $textFrom = 'name', $idTo = 'id', $textTo = 'text' ) {
1291
1292 $result = array();
1293 foreach ( $array as $v ) {
1294 $s_id = $v;
1295 $s_text = $v;
1296 if ( false !== $id_from ) {
1297 $s_id = $this->get( $v, $id_from, false );
1298 $s_text = $this->get( $v, $textFrom, false );
1299 }
1300 if ( false !== $s_id && '' != $s_text ) {
1301 if ( '' != $s_id ) {
1302 $result[] = array(
1303 $idTo => $s_id,
1304 $textTo => $s_text,
1305 );
1306 }
1307 }
1308 }
1309 return $result;
1310 }
1311 function toColorListArrayFromListObjects( $array, $colors, $id = 'id', $text = 'name' ) {
1312 global $tcmp;
1313 $result = array();
1314 foreach ( $array as $instance ) {
1315 $s_id = $this->get( $instance, $id, false );
1316 $s_text = $this->get( $instance, $text, false );
1317 foreach ( $colors as $color => $when ) {
1318 $success = false;
1319 foreach ( $when['conditions'] as $conditionKey => $condition_value ) {
1320 $condition_value = $tcmp->utils->to_array( $condition_value );
1321 $c = $this->get( $instance, $conditionKey, false );
1322 if ( false !== $c ) {
1323 $c .= '';
1324 foreach ( $condition_value as $v ) {
1325 $v .= '';
1326 if ( $c === $v ) {
1327 $success = true;
1328 break;
1329 }
1330 }
1331 }
1332 if ( $success ) {
1333 break;
1334 }
1335 }
1336
1337 if ( $success ) {
1338 $style = 'color:' . $color . '; ';
1339 if ( isset( $when['bold'] ) && $when['bold'] ) {
1340 $style .= 'font-weight:bold; ';
1341 }
1342 $s_text = '<span style="' . $style . '">' . $s_text . '</span>';
1343 }
1344 }
1345 if ( '' != $s_id && false !== $s_text ) {
1346 $result[] = array(
1347 'id' => $s_id,
1348 'text' => $s_text,
1349 'name' => $s_text,
1350 );
1351 }
1352 }
1353 return $result;
1354 }
1355 function md5() {
1356 $array = func_get_args();
1357 $buffer = '';
1358 foreach ( $array as $v ) {
1359 $buffer .= ':)' . $v;
1360 }
1361 $buffer = md5( $buffer );
1362 return $buffer;
1363 }
1364 function arrayCase( $text ) {
1365 $buffer = '';
1366 $array = array();
1367 $text = str_split( $text );
1368 $prev_upper = false;
1369 $next_upper = false;
1370 foreach ( $text as $c ) {
1371 if ( $c >= 'a' && $c <= 'z' ) {
1372 if ( $next_upper ) {
1373 if ( '' != $buffer ) {
1374 $array[] = $buffer;
1375 $buffer = '';
1376 }
1377 $c = strtoupper( $c );
1378 }
1379 $buffer .= $c;
1380 $next_upper = false;
1381 $prev_upper = false;
1382 } elseif ( $c >= '0' && $c <= '9' ) {
1383 $buffer .= $c;
1384 $next_upper = true;
1385 } elseif ( $c >= 'A' && $c <= 'Z' ) {
1386 if ( ! $prev_upper ) {
1387 if ( '' != $buffer ) {
1388 $array[] = $buffer;
1389 $buffer = '';
1390 }
1391 }
1392 $buffer .= $c;
1393 $next_upper = false;
1394 $prev_upper = true;
1395 } else {
1396 if ( '' != $buffer ) {
1397 $array[] = $buffer;
1398 $buffer = '';
1399 }
1400 $next_upper = true;
1401 $prev_upper = false;
1402 }
1403 }
1404 if ( '' != $buffer ) {
1405 $array[] = $buffer;
1406 }
1407 return $array;
1408 }
1409 function lowerCamelCase( $text ) {
1410 $buffer = '';
1411 if ( strpos( $text, '_' ) !== false || strpos( $text, '-' ) !== false ) {
1412 $text = strtolower( $text );
1413 }
1414
1415 $text = str_split( $text );
1416 $all_upper = true;
1417 $next_upper = false;
1418 foreach ( $text as $c ) {
1419 if ( $c >= 'a' && $c <= 'z' ) {
1420 $all_upper = false;
1421 if ( $next_upper ) {
1422 $c = strtoupper( $c );
1423 }
1424 $buffer .= $c;
1425 $next_upper = false;
1426 } elseif ( $c >= '0' && $c <= '9' ) {
1427 $buffer .= $c;
1428 $next_upper = true;
1429 } elseif ( $c >= 'A' && $c <= 'Z' ) {
1430 $buffer .= $c;
1431 $next_upper = false;
1432 } else {
1433 $next_upper = true;
1434 }
1435 }
1436 if ( $all_upper ) {
1437 $buffer = strtolower( $buffer );
1438 } else {
1439 $buffer = lcfirst( $buffer );
1440 }
1441 return $buffer;
1442 }
1443 function upperCamelCase( $text ) {
1444 $text = $this->lowerCamelCase( $text );
1445 $text = ucfirst( $text );
1446 return $text;
1447 }
1448
1449 function castStdClass( $a ) {
1450 $a = (array) $a;
1451 $r = new stdClass();
1452 foreach ( $a as $k => $v ) {
1453 $r->$k = $v;
1454 }
1455 return $r;
1456 }
1457 function castArray( $a ) {
1458 $r = $a;
1459 if ( is_object( $a ) ) {
1460 $r = (array) $a;
1461 }
1462
1463 if ( ! is_array( $r ) ) {
1464 $r = array();
1465 }
1466 return $r;
1467 }
1468 public function copyArray( $array ) {
1469 $temp = array();
1470 foreach ( $array as $k => $v ) {
1471 $temp[ $k ] = $v;
1472 }
1473 return $temp;
1474 }
1475 public function isObject( $v ) {
1476 return ( false !== $v && ! is_null( $v ) && is_object( $v ) );
1477 }
1478 public function isArray( $v ) {
1479 return ( false !== $v && ! is_null( $v ) && is_array( $v ) );
1480 }
1481 public function getConstants( $class, $prefix, $reverse = false ) {
1482 global $tcmp;
1483 if ( is_object( $class ) ) {
1484 $class = get_class( $class );
1485 }
1486 $class = str_replace( 'Search', '', $class );
1487 $class = str_replace( 'Constants', '', $class );
1488 $class .= 'Constants';
1489 if ( ! class_exists( $class ) ) {
1490 $class = TCMP_PLUGIN_PREFIX . $class;
1491 }
1492
1493 $result = array();
1494 if ( class_exists( $class ) ) {
1495 $reflection = new ReflectionClass( $class );
1496 $array = $reflection->getConstants();
1497 foreach ( $array as $k => $v ) {
1498 $pos = 0;
1499 if ( '' != $prefix ) {
1500 $pos = stripos( $k, $prefix );
1501 }
1502 if ( 0 === $pos ) {
1503 if ( $reverse ) {
1504 $result[ $v ] = $k;
1505 } else {
1506 $result[ $k ] = $v;
1507 }
1508 }
1509 }
1510 }
1511 return $result;
1512 }
1513 public function getConstantValue( $class, $prefix, $name, $default = false ) {
1514 /* @var $ec TCMP_Singleton */
1515 global $ec;
1516 $result = $default;
1517 if ( is_object( $class ) ) {
1518 $class = get_class( $class );
1519 }
1520 $class = str_replace( 'Search', '', $class );
1521 $class = str_replace( 'Constants', '', $class );
1522 $class .= 'Constants';
1523 if ( ! class_exists( $class ) ) {
1524 $class = TCMP_PLUGIN_PREFIX . $class;
1525 }
1526
1527 if ( class_exists( $class ) ) {
1528 $name = $prefix . '_' . $name;
1529 $name = $ec->utils->upperUnderscoreCase( $name );
1530 $reflection = new ReflectionClass( $class );
1531 $result = $reflection->getConstant( $name );
1532 }
1533 return $result;
1534 }
1535 public function getConstantName( $class, $prefix, $value, $default = false ) {
1536 /* @var $ec TCMP_Singleton */
1537 $constants = $this->getConstants( $class, $prefix, true );
1538 $result = $default;
1539 if ( isset( $constants[ $value ] ) ) {
1540 $result = $constants[ $value ];
1541 }
1542 return $result;
1543 }
1544 public function daysDiff( $dt1, $dt2 ) {
1545 $dt1 = $this->parse_date_to_time( $dt1 );
1546 $dt2 = $this->parse_date_to_time( $dt2 );
1547 $result = ( $dt2 - $dt1 ) / 86400;
1548 $result = intval( $result );
1549 return $result;
1550 }
1551
1552 public function getText( $text, $args ) {
1553 if ( false === $args || 0 == count( $args ) ) {
1554 return $text;
1555 }
1556
1557 foreach ( $args as $k => $v ) {
1558 $text = str_replace( '{' . $k . '}', $v, $text );
1559 }
1560 return $text;
1561 }
1562 public function arrayExtends( $options, $defaults ) {
1563 global $tcmp;
1564 $options = $tcmp->utils->parseArgs( $options, $defaults );
1565 foreach ( $options as $k => $v ) {
1566 if ( is_bool( $v ) ) {
1567 $v = ( $v ? 1 : 0 );
1568 }
1569 if ( isset( $defaults[ $k ] ) ) {
1570 if ( $this->is_associativeArray( $v ) ) {
1571 $v = $this->arrayExtends( $v, $defaults[ $k ] );
1572 } else {
1573 $v = $tcmp->utils->to_array( $v );
1574 $old = $defaults[ $k ];
1575 $old = $tcmp->utils->to_array( $old );
1576 if ( ! $this->is_associativeArray( $old ) ) {
1577 $v = array_merge( $v, $old );
1578 $v = array_unique( $v );
1579 }
1580 }
1581 } else {
1582 $v = $tcmp->utils->to_array( $v );
1583 }
1584 $options[ $k ] = $v;
1585 }
1586 return $options;
1587 }
1588 //send remote request to our server to store tracking and feedback
1589 function remotePost( $action, $data = '' ) {
1590 global $tcmp;
1591
1592 $data['secret'] = 'WYSIWYG';
1593 $response = wp_remote_post(
1594 TCMP_INTELLYWP_ENDPOINT . '?iwpm_action=' . $action,
1595 array(
1596 'method' => 'POST',
1597 'timeout' => 20,
1598 'redirection' => 5,
1599 'httpversion' => '1.1',
1600 'blocking' => true,
1601 'body' => $data,
1602 'user-agent' => TCMP_PLUGIN_NAME . '/' . TCMP_PLUGIN_VERSION . '; ' . get_bloginfo( 'url' ),
1603 )
1604 );
1605 $data = json_decode( wp_remote_retrieve_body( $response ), true );
1606 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200
1607 || ! isset( $data['success'] ) || ! $data['success']
1608 ) {
1609 $tcmp->log->error( 'ERRORS SENDING REMOTE-POST ACTION=%s DUE TO REASON=%s', $action, $response );
1610 $data = false;
1611 } else {
1612 $tcmp->log->debug( 'SUCCESSFULLY SENT REMOTE-POST ACTION=%s RESPONSE=%s', $action, $data );
1613 }
1614 return $data;
1615 }
1616
1617 function isAdminUser() {
1618 //https://wordpress.org/support/topic/how-to-check-admin-right-without-include-pluggablephp
1619 return true;
1620 }
1621
1622 function isPluginPage() {
1623 global $tcmp;
1624 $page = tcmp_sqs( 'page' );
1625 $result = ( $this->starts_with( $page, TCMP_PLUGIN_SLUG ) );
1626 return $result;
1627 }
1628
1629 public function arrayPush( &$array, $another ) {
1630 if ( ! is_array( $another ) ) {
1631 array_push( $array, $another );
1632 } elseif ( is_array( $another ) ) {
1633 foreach ( $another as $v ) {
1634 array_push( $array, $v );
1635 }
1636 }
1637 return $array;
1638 }
1639
1640 public function getConstantsValues( $class, $prefix = '', $glue = false ) {
1641 $array = $this->getConstants( $class, $prefix );
1642 $result = array_values( $array );
1643 if ( false !== $glue ) {
1644 $result = implode( $glue, $result );
1645 }
1646 return $result;
1647
1648 }
1649 public function getValue( $array, $index, $default = false ) {
1650 $result = $this->get_index( $array, $index, $default );
1651 if ( $result !== $default ) {
1652 $result = $result['v'];
1653 }
1654 return $result;
1655 }
1656 public function get_key( $array, $index, $default = false ) {
1657 $result = $this->get_index( $array, $index, $default );
1658 if ( $result !== $default ) {
1659 $result = $result['k'];
1660 }
1661 return $result;
1662 }
1663 public function get_index( $array, $index, $default = false ) {
1664 $result = $default;
1665 if ( is_array( $array ) && count( $array ) > 0 ) {
1666 if ( $this->is_associativeArray( $array ) ) {
1667 $i = 0;
1668 foreach ( $array as $k => $v ) {
1669 if ( $index == $i ) {
1670 $result = array(
1671 'k' => $k,
1672 'v' => $v,
1673 );
1674 break;
1675 }
1676 $i++;
1677 }
1678 } else {
1679 if ( $index < count( $array ) && $index >= 0 ) {
1680 $result = $array[ $index ];
1681 }
1682 }
1683 }
1684 return $result;
1685 }
1686 public function is_empty( $v ) {
1687 if ( ! $v ) {
1688 return true;
1689 }
1690
1691 $result = false;
1692 if ( is_string( $v ) ) {
1693 $result = ( '' == $v );
1694 } elseif ( is_array( $v ) ) {
1695 $result = 0 == count( $v );
1696 } elseif ( is_object( $v ) ) {
1697 $result = true;
1698 foreach ( $v as $k => $w ) {
1699 if ( ! is_null( $w ) && '' !== $w ) {
1700 $result = false;
1701 break;
1702 }
1703 }
1704 }
1705 return $result;
1706 }
1707 public function httpEncode( $v ) {
1708 $v = gzcompress( $v );
1709 $v = bin2hex( $v );
1710 return $v;
1711 }
1712 public function httpDecode( $v ) {
1713 $v = hex2bin( $v );
1714 $v = gzuncompress( $v );
1715 return $v;
1716 }
1717 public function trimHttp( $uri ) {
1718 $uri = str_replace( 'http://', '', $uri );
1719 $uri = str_replace( 'https://', '', $uri );
1720 return $uri;
1721 }
1722 function getClientIpAddress() {
1723 $ipaddress = '';
1724 if ( getenv( 'HTTP_CLIENT_IP' ) ) {
1725 $ipaddress = getenv( 'HTTP_CLIENT_IP' );
1726 } elseif ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
1727 $ipaddress = getenv( 'HTTP_X_FORWARDED_FOR' );
1728 } elseif ( getenv( 'HTTP_X_FORWARDED' ) ) {
1729 $ipaddress = getenv( 'HTTP_X_FORWARDED' );
1730 } elseif ( getenv( 'HTTP_FORWARDED_FOR' ) ) {
1731 $ipaddress = getenv( 'HTTP_FORWARDED_FOR' );
1732 } elseif ( getenv( 'HTTP_FORWARDED' ) ) {
1733 $ipaddress = getenv( 'HTTP_FORWARDED' );
1734 } elseif ( getenv( 'REMOTE_ADDR' ) ) {
1735 $ipaddress = getenv( 'REMOTE_ADDR' );
1736 } else {
1737 $ipaddress = 'UNKNOWN';
1738 }
1739 $ipaddress = ( '::1' == $ipaddress ) ? '192.168.0.1' : $ipaddress;
1740 return $ipaddress;
1741 }
1742 public function isMail( $mail ) {
1743 $at = strpos( $mail, '@' );
1744 $dot = strrpos( $mail, '.' );
1745 $result = false;
1746 if ( false !== $at && false !== $dot && $at < $dot ) {
1747 $result = true;
1748 }
1749 return $result;
1750 }
1751 public function getNameFromListArray( $array, $id, $default = false ) {
1752 $result = $default;
1753 foreach ( $array as $v ) {
1754 if ( $v['id'] == $id ) {
1755 if ( isset( $v['text'] ) ) {
1756 $result = $v['text'];
1757 break;
1758 } elseif ( isset( $v['name'] ) ) {
1759 $result = $v['name'];
1760 break;
1761 }
1762 }
1763 }
1764 return $result;
1765 }
1766 function bqs( $name, $default = false ) {
1767 $v = $this->qs( $name, '' );
1768 $result = $default;
1769 if ( '' != $v ) {
1770 if ( is_numeric( $v ) ) {
1771 $v = intval( $v );
1772 $result = ( $v > 0 );
1773 } else {
1774 $result = $this->isTrue( $v );
1775 }
1776 }
1777 return $result;
1778 }
1779
1780 function getFunctionName( $function ) {
1781 $result = false;
1782 if ( is_string( $function ) ) {
1783 $result = $function;
1784 } elseif ( is_array( $function ) ) {
1785 $result = $function[1];
1786 }
1787 return $result;
1788 }
1789 function functionExists( $function ) {
1790 $result = false;
1791 if ( is_string( $function ) ) {
1792 $result = function_exists( $function );
1793 } elseif ( is_array( $function ) ) {
1794 $result = method_exists( $function[0], $function[1] );
1795 } elseif ( is_callable( $function ) ) {
1796 $result = true;
1797 }
1798 return $result;
1799 }
1800 function functionCall() {
1801 $args = func_get_args();
1802 if ( false === $args || 0 == count( $args ) ) {
1803 return;
1804 }
1805
1806 $function = array_shift( $args );
1807 $result = null;
1808 if ( $this->functionExists( $function ) ) {
1809 $result = call_user_func_array( $function, $args );
1810 }
1811 return $result;
1812 }
1813
1814 public function contains( $v1, $v2, $ignore_case = true ) {
1815 $result = false;
1816 if ( $ignore_case ) {
1817 $result = stripos( $v1, $v2 ) !== false;
1818 } else {
1819 $result = strpos( $v1, $v2 ) !== false;
1820 }
1821 return $result;
1822 }
1823
1824 private function getHtmlCode( $value ) {
1825 $value = str_replace( '\"', '', $value );
1826 $value = str_replace( '"', '', $value );
1827 return $value;
1828 }
1829
1830 public function dequeueScripts( $array ) {
1831 if ( ! function_exists( 'wp_scripts' ) || function_exists( 'wp_dequeue_script' ) ) {
1832 return;
1833 }
1834
1835 $array = $this->to_array( $array );
1836 $scripts = wp_scripts();
1837 /* @var $v _WP_Dependency */
1838 foreach ( $scripts->registered as $k => $v ) {
1839 foreach ( $array as $pattern ) {
1840 if ( $this->contains( $v->src, $pattern ) || $this->contains( $v->handle, $pattern ) ) {
1841 wp_dequeue_script( $v->handle );
1842 break;
1843 }
1844 }
1845 }
1846 }
1847 public function dequeueStyles( $array ) {
1848 if ( ! function_exists( 'wp_styles' ) || function_exists( 'wp_dequeue_style' ) ) {
1849 return;
1850 }
1851
1852 $array = $this->to_array( $array );
1853 $styles = wp_styles();
1854 /* @var $v _WP_Dependency */
1855 foreach ( $styles->registered as $k => $v ) {
1856 foreach ( $array as $pattern ) {
1857 if ( $this->contains( $v->src, $pattern ) || $this->contains( $v->handle, $pattern ) ) {
1858 wp_dequeue_style( $v->handle );
1859 break;
1860 }
1861 }
1862 }
1863 }
1864 public function formatSeconds( $time ) {
1865 if ( '' === $time ) {
1866 return '';
1867 }
1868
1869 $time = intval( $time );
1870 $seconds = ( $time % 60 );
1871 $time = ( ( $time - $seconds ) / 60 );
1872 $minutes = ( $time % 60 );
1873 $time = ( ( $time - $minutes ) / 60 );
1874 $hours = ( $time % 24 );
1875 $time = ( ( $time - $hours ) / 24 );
1876 $days = $time;
1877
1878 $array = array();
1879 if ( $seconds > 0 ) {
1880 $array[] = $seconds . 's';
1881 }
1882 if ( $minutes > 0 ) {
1883 $array[] = $minutes . 'm';
1884 }
1885 if ( $hours > 0 ) {
1886 $array[] = $hours . 'h';
1887 }
1888 if ( $days > 0 ) {
1889 $array[] = $days . 'd';
1890 }
1891 $array = array_reverse( $array );
1892 $text = implode( ' ', $array );
1893 return $text;
1894 }
1895
1896 function formatPercentage( $value, $options = array() ) {
1897 if ( is_bool( $options ) ) {
1898 $options = array( 'symbol' => $options );
1899 }
1900 $defaults = array( 'symbol' => true );
1901 $options = $this->parseArgs( $options, $defaults );
1902
1903 $value = floatval( $value );
1904 $value = round( $value, 3 );
1905 $value = number_format( $value, 3, ',', '' );
1906 if ( $options['symbol'] ) {
1907 $value .= ' %';
1908 }
1909 return $value;
1910 }
1911 function formatCurrencyMoney( $value, $options = array() ) {
1912 $defaults = array( 'currency' => $this->getDefaultCurrencySymbol() );
1913 $options = $this->parseArgs( $options, $defaults );
1914
1915 $value = $this->formatMoney( $value, $options );
1916 return $value;
1917 }
1918 function formatMoney( $value, $options = array() ) {
1919 if ( is_string( $options ) ) {
1920 $options = array( 'currency' => $options );
1921 }
1922 $defaults = array( 'currency' => false );
1923 $options = $this->parseArgs( $options, $defaults );
1924
1925 $value = floatval( $value );
1926 $value = round( $value, 3 );
1927 $value = number_format( $value, 3, ',', '.' );
1928 if ( '' != $options['currency'] ) {
1929 $symbol = $options['currency'];
1930 if ( strlen( $symbol ) > 1 ) {
1931 $symbol = $this->getCurrencySymbol( $symbol );
1932 }
1933 $value .= ' ' . $symbol;
1934 }
1935 return $value;
1936 }
1937 function sortOptions( &$options ) {
1938 if ( ! is_array( $options ) ) {
1939 return $options;
1940 }
1941
1942 usort( $options, array( $this, 'sortOptions_Compare' ) );
1943 return $options;
1944 }
1945 public function sortOptions_Compare( $o1, $o2 ) {
1946 global $tcmp;
1947 $v1 = $tcmp->utils->get( $o1, 'text', false );
1948 if ( false == $v1 ) {
1949 $v1 = $tcmp->utils->get( $o1, 'name', false );
1950 }
1951 $v2 = $tcmp->utils->get( $o2, 'text', false );
1952 if ( false == $v2 ) {
1953 $v2 = $tcmp->utils->get( $o2, 'name', false );
1954 }
1955
1956 //to order properly
1957 if ( $tcmp->utils->starts_with( $v1, '[' ) ) {
1958 $v1 = ' ' . $v1;
1959 }
1960 if ( $tcmp->utils->starts_with( $v2, '[' ) ) {
1961 $v2 = ' ' . $v2;
1962 }
1963 return strcasecmp( $v1, $v2 );
1964 }
1965
1966 private function validate_ip( $ip ) {
1967 if ( filter_var( $ip, FILTER_VALIDATE_IP ) ) {
1968 return $ip;
1969 }
1970 return '';
1971 }
1972
1973 public function getVisitorIpAddress() {
1974 $ip = '';
1975 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
1976 $ip = validate_ip( $_SERVER['HTTP_CLIENT_IP'] );
1977 }
1978
1979 if ( '' == $ip && ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
1980 $ip = validate_ip( $_SERVER['HTTP_X_FORWARDED_FOR'] );
1981 }
1982
1983 if ( '' == $ip ) {
1984 $ip = validate_ip( $_SERVER['REMOTE_ADDR'] );
1985 }
1986 return $ip;
1987 }
1988
1989 function getCurrencySymbol( $currency ) {
1990 // Create a NumberFormatter
1991 $locale = 'en_US';
1992 $formatter = new NumberFormatter( $locale, NumberFormatter::CURRENCY );
1993
1994 // Figure out what 0.00 looks like with the currency symbol
1995 $withCurrency = $formatter->formatCurrency( 0, $currency );
1996
1997 // Figure out what 0.00 looks like without the currency symbol
1998 $formatter->setPattern( str_replace( '¤', '', $formatter->getPattern() ) );
1999 $without_currency = $formatter->formatCurrency( 0, $currency );
2000
2001 // Extract just the currency symbol from the first string
2002 return str_replace( $without_currency, '', $withCurrency );
2003 }
2004 function encodeUri( $string ) {
2005 $entities = array( '%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%23', '%5B', '%5D' );
2006 $replacements = array( '!', '*', "'", '(', ')', ';', ':', '@', '&', '=', '+', '$', ',', '/', '?', '#', '[', ']' );
2007 $result = urlencode( $string );
2008 //$result=str_replace($replacements, $entities, $result);
2009 return $result;
2010 }
2011
2012 public function formatTimer( $time ) {
2013 if ( ! is_int( $time ) ) {
2014 if ( is_string( $time ) ) {
2015 $time = str_replace( ' ', ':', $time );
2016 $time = str_replace( '.', ':', $time );
2017 $time = str_replace( '/', ':', $time );
2018 $time = explode( ':', $time );
2019
2020 $length = count( $time );
2021 $days = 0;
2022 $hours = 0;
2023 $minutes = 0;
2024 $secs = intval( $time[ $length - 1 ] );
2025
2026 if ( $length > 1 ) {
2027 $minutes = intval( $time[ $length - 2 ] );
2028 if ( $length > 2 ) {
2029 $hours = intval( $time[ $length - 3 ] );
2030 if ( $length > 3 ) {
2031 $days = intval( $time[ $length - 4 ] );
2032 }
2033 }
2034 }
2035 $time = $days * 86400 + $hours * 3600 + $minutes * 60 + $secs;
2036 } else {
2037 $time = 0;
2038 }
2039 } else {
2040 $time = intval( $time );
2041 }
2042
2043 $secs = $time % 60;
2044 $time = ( $time - $secs ) / 60;
2045 $minutes = $time % 60;
2046 $time = ( $time - $minutes ) / 60;
2047 $hours = $time % 24;
2048 $days = ( $time - $hours ) / 24;
2049
2050 $result = array();
2051 $result[] = $days;
2052 $result[] = ( $hours < 10 ? '0' : '' ) . $hours;
2053 $result[] = ( $minutes < 10 ? '0' : '' ) . $minutes;
2054 $result[] = ( $secs < 10 ? '0' : '' ) . $secs;
2055 $result = implode( ':', $result );
2056 return $result;
2057 }
2058 public function parseTimer( $time ) {
2059 $time = $this->formatTimer( $time );
2060 $time = explode( ':', $time );
2061 $result = intval( $time[0] ) * 86400 + intval( $time[1] ) * 3600 + intval( $time[2] ) * 60 + intval( $time[3] );
2062 return $result;
2063 }
2064 }
2065