PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 1.12.0
Tracking Code Manager v1.12.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 8 years ago Ecommerce.php 7 years ago Language.php 10 years ago Logger.php 8 years ago MobileDetect.php 8 years ago Options.php 8 years ago Plugin.php 10 years ago Properties.php 10 years ago Tracking.php 8 years ago Utils.php 8 years ago
Utils.php
2785 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3
4 class TCMP_Utils {
5 const FORMAT_DATETIME='d/m/Y H:i';
6 const FORMAT_COMPACT_DATETIME='d/m H:i';
7 const FORMAT_DATE='d/m/Y';
8 const FORMAT_TIME='H:i';
9
10 const FORMAT_SQL_DATETIME='Y-m-d H:i:s';
11 const FORMAT_SQL_DATE='Y-m-d';
12 const FORMAT_SQL_TIME='H:i:s';
13
14 private $colorIndex;
15 private $defaultCurrencySymbol;
16
17 public function __construct() {
18 $this->colorIndex=0;
19 }
20
21 public function setDefaultCurrencySymbol($value) {
22 $this->defaultCurrencySymbol=$value;
23 }
24 public function getDefaultCurrencySymbol() {
25 return ($this->defaultCurrencySymbol=='' ? 'USD' : $this->defaultCurrencySymbol);
26 }
27 function format($message, $v1=NULL, $v2=NULL, $v3=NULL, $v4=NULL, $v5=NULL) {
28 if($v1 || $v2 || $v3 || $v4 || $v5) {
29 $message=sprintf($message, $v1, $v2, $v3, $v4, $v5);
30 }
31 return $message;
32 }
33 function startsWith($haystack, $needle) {
34 $length=strlen($needle);
35 return (substr($haystack, 0, $length) === $needle);
36 }
37
38 function endsWith($haystack, $needle) {
39 $length=strlen($needle);
40 $start=$length * -1; //negative
41 return (substr($haystack, $start) === $needle);
42 }
43 function substr($text, $start=0, $end=-1) {
44 if($end<0) {
45 $end=strlen($text);
46 }
47 $length=$end-$start;
48 return substr($text, $start, $length);
49 }
50
51 function shortcodeArgs($args, $defaults) {
52 $args=$this->sanitizeShortcodeKeys($args);
53 $defaults=$this->sanitizeShortcodeKeys($defaults);
54 $args=shortcode_atts($defaults, $args);
55 return $args;
56 }
57 function sanitizeShortcodeKeys($array) {
58 $result=array();
59 foreach($array as $k=>$v) {
60 if(is_string($k)) {
61 $k=strtolower($k);
62 }
63 $result[$k]=$v;
64 }
65 return $result;
66 }
67
68 //WOW! $end is passed as reference due to we can change it if we found \n character after
69 //substring to avoid having these characters after or before
70 function substrln($text, $start=0, &$end=-1) {
71 if($end<0) {
72 $end=strlen($text);
73 }
74
75 do {
76 $loop=FALSE;
77 $c=substr($text, $end, 1);
78 if($c=="\n" || $c=="\r" || $c==".") {
79 $end += 1;
80 $loop=TRUE;
81 }
82 } while($loop);
83
84 $length=$end-$start;
85 return substr($text, $start, $length);
86 }
87
88 function toCommaArray($array, $isNumeric=TRUE, $isTrim=TRUE) {
89 if(is_string($array)) {
90 if(trim($array)=='') {
91 $array=array();
92 } else {
93 $array=explode(',', $array);
94 }
95 } elseif(is_numeric($array)) {
96 $array=array($array);
97 }
98 if(!is_array($array)) {
99 $array=array();
100 }
101 for($i=0; $i<count($array); $i++) {
102 if($isTrim) {
103 $array[$i]=trim($array[$i]);
104 }
105 if($isNumeric) {
106 $array[$i]=floatval($array[$i]);
107 }
108 }
109 return $array;
110 }
111 function inAllArray($search, $where) {
112 return ($this->inArray(-1, $where) || $this->inArray($search, $where));
113 }
114 function inArray($search, $where) {
115 $result=FALSE;
116 $where=$this->toArray($where);
117 $search=$this->toArray($search);
118 if(count($where)==0 || count($search)==0) {
119 return FALSE;
120 }
121
122 foreach($where as $v) {
123 $v.='';
124 foreach($search as $c) {
125 $c.='';
126 if($v===$c) {
127 $result=TRUE;
128 break;
129 }
130 }
131 /*$v=intval($v);
132 if ($v<0) {
133 //if one element of the array have -1 value means i select "all" option
134 $result=TRUE;
135 break;
136 }*/
137
138 if($result) {
139 break;
140 }
141 }
142 return $result;
143 }
144
145 function is($name, $compare, $default='', $ignoreCase=TRUE) {
146 $what=$this->qs($name, $default);
147 $result=FALSE;
148 if(is_string($compare)) {
149 $compare=explode(',', $compare);
150 }
151 if($ignoreCase){
152 $what=strtolower($what);
153 }
154
155 foreach($compare as $v) {
156 if($ignoreCase){
157 $v=strtolower($v);
158 }
159 if($what==$v) {
160 $result=TRUE;
161 break;
162 }
163 }
164 return $result;
165 }
166
167 public function twitter($name) {
168 ?>
169 <a href="https://twitter.com/<?php echo $name?>" class="twitter-follow-button" data-show-count="false" data-dnt="true">Follow @<?php echo $name?></a>
170 <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
171 <?php
172 }
173
174 public function sort($isAssociative, $a1, $a2=NULL, $a3=NULL, $a4=NULL, $a5=NULL) {
175 $array=$this->merge($isAssociative, $a1, $a2, $a3, $a4, $a5);
176 ksort($array);
177 return $array;
178 }
179 public function merge($isAssociative, $a1, $a2=NULL, $a3=NULL, $a4=NULL, $a5=NULL) {
180 $result=array();
181 if($isAssociative) {
182 $array=array($a1, $a2, $a3, $a4, $a5);
183 foreach($array as $a) {
184 if(!is_array($a)) {
185 continue;
186 }
187
188 foreach($a as $k=>$v) {
189 if(!isset($result[$k])) {
190 $result[$k]=$v;
191 }
192 }
193 }
194 } else {
195 $result=array_merge($a1, $a2, $a3, $a4, $a5);
196 }
197 return $result;
198 }
199
200 function bget($instance, $name, $index=-1) {
201 $v=$this->get($instance, $name, FALSE, $index);
202 $v=$this->isTrue($v);
203 return $v;
204 }
205 function dget($instance, $name, $index=-1) {
206 $v=$this->get($instance, $name, FALSE, $index);
207 $v=$this->parseDateToTime($v);
208 return $v;
209 }
210 function aget($instance, $name, $index=-1) {
211 $v=$this->get($instance, $name, FALSE, $index);
212 $v=$this->toArray($v);
213 return $v;
214 }
215 function get($instance, $name, $default='', $index=-1) {
216 if($this->isEmpty($instance)) {
217 return $default;
218 }
219 $options=array();
220 //assolutamente da non fare altrimenti succede un disastro in quanto i metodi del inputComponent
221 //gli passano come name il valore...insomma un disastro!
222 //$name=$this->toArray($name);
223 //$name=implode('.', $name);
224
225 $result=$default;
226 if(is_array($instance) || is_object($instance)) {
227 if($this->propertyReflect($instance, $name, $options)) {
228 $result=$options['get'];
229 }
230 }
231 if($index>-1) {
232 $result=$this->toArray($result);
233 if(isset($result[$index])) {
234 $result=$result[$index];
235 } else {
236 $result=$default;
237 }
238 }
239 return $result;
240 }
241 function has($instance, $name) {
242 return $this->propertyReflect($instance, $name);
243 }
244 function set(&$instance, $name, $value) {
245 $options=array('set'=>$value);
246 $result=$this->propertyReflect($instance, $name, $options);
247 if(!$result) {
248 }
249 return $result;
250 }
251 function iget($array, $name, $default='') {
252 return floatval($this->get($array, $name, $default));
253 }
254
255 private function propertyReflect(&$instance, $name, &$options=array()) {
256 if(!is_object($instance) && !is_array($instance)) {
257 return FALSE;
258 }
259
260 if($options===FALSE || !is_array($options)) {
261 $options=array();
262 }
263 $options['has']=FALSE;
264 $options['get']=FALSE;
265
266 $current=$instance;
267 $names=explode('.', $name);
268 $value=FALSE;
269 $result=TRUE;
270 for($i=0; $i<count($names); $i++) {
271 $name=$names[$i];
272 if(!is_object($current) && !is_array($current)) {
273 return FALSE;
274 }
275 if(is_null($current)) {
276 return FALSE;
277 }
278
279 if(is_object($current)) {
280 if(get_class($current)=='stdClass') {
281 if(isset($current->$name)) {
282 $value=$current->$name;
283 } else {
284 $result=FALSE;
285 }
286 } else {
287 $r=new ReflectionClass($current);
288 try {
289 if($r->getProperty($name)!==FALSE) {
290 $value=$current->$name;
291 } else {
292 $result=FALSE;
293 }
294 } catch(Exception $ex) {
295 if(isset($current->$name)) {
296 $value=$current->$name;
297 } else {
298 $result=FALSE;
299 }
300 }
301 }
302 } elseif(is_array($current)) {
303 if(isset($current[$name])) {
304 $value=$current[$name];
305 } else {
306 $result=FALSE;
307 }
308 }
309
310 if(!$result) {
311 break;
312 } elseif($i<(count($names)-1)) {
313 $current=$value;
314 } else {
315 $options['get']=$value;
316 if(isset($options['set'])) {
317 if(is_object($current)) {
318 $current->$name=$options['set'];
319 } elseif(is_array($current)) {
320 $current[$name]=$options['set'];
321 }
322 }
323 }
324 }
325 return $result;
326 }
327 function isTrue($value) {
328 $result=FALSE;
329 if(is_bool($value)) {
330 $result=(bool)$value;
331 } elseif(is_numeric($value)) {
332 $result=floatval($value)>0;
333 } else {
334 $result=strtolower($value);
335 if($result=='ok' || $result=='yes' || $result=='true' || $result=='on') {
336 $result=TRUE;
337 } else {
338 $result=FALSE;
339 }
340 }
341 return $result;
342 }
343 function aqs($prefix, $removePrefix=TRUE) {
344 $result=array();
345 $array=$this->merge(TRUE, $_POST, $_GET);
346 foreach($array as $k=>$v) {
347 if($this->startsWith($k, $prefix)) {
348 if($removePrefix) {
349 $k=substr($k, strlen($prefix));
350 }
351 $result[$k]=$v;
352 }
353 }
354 return $result;
355 }
356 function iqs($name, $default=0, $min=0, $max=0) {
357 $result=floatval($this->qs($name, $default));
358 if($min!=$max) {
359 if($result<$min) {
360 $result=$min;
361 } elseif($result>$max) {
362 $result=$max;
363 }
364 }
365 return $result;
366 }
367 function dqs($name, $default=0) {
368 $result=($this->qs($name, $default));
369 $result=$this->parseDateToTime($result);
370 if($result==0) {
371 $result=$default;
372 }
373 return $result;
374 }
375 //per ottenere un campo dal $_GET oppure dal $_POST
376 function qs($name, $default='') {
377 $result=$default;
378 if(isset($_POST[$name])) {
379 $result=$_POST[$name];
380 } elseif (isset($_GET[$name])) {
381 $result=$_GET[$name];
382 }
383
384 if (is_string($result)) {
385 //The superglobals $_GET and $_REQUEST are already decoded.
386 //Using urldecode() on an element in $_GET or $_REQUEST
387 //could have unexpected and dangerous results.
388 //$result=urldecode($result);
389 $result=trim($result);
390 }
391 return $result;
392 }
393
394 var $_taxonomyType;
395 private function getTermLink($id) {
396 if(is_array($id)) {
397 foreach($id as $v) {
398 $id=$v;
399 break;
400 }
401 }
402 if(is_numeric($id)) {
403 $id=intval($id);
404 }
405 $result=get_term_link($id, $this->_taxonomyType);
406 if(is_wp_error($result)) {
407 $result=FALSE;
408 }
409 return $result;
410 }
411 function query($query, $options=NULL) {
412 global $tcmp, $wpdb;
413
414 $parent='';
415 $defaults=array(
416 'post_type' => ''
417 , 'all' => FALSE
418 , 'select' => FALSE
419 , 'taxonomy'=>''
420 );
421 $options=wp_parse_args($options, $defaults);
422
423 if(!isset($options['type'])) {
424 if($options['post_type']!='') {
425 $options['type']=$options['post_type'];
426 } elseif($options['taxonomy']!='') {
427 $options['type']=$options['taxonomy'];
428 } else {
429 $options['type']='';
430 }
431 }
432
433 if($query==TCMP_QUERY_CONVERSION_PLUGINS) {
434 $array=$tcmp->Ecommerce->getPlugins(FALSE);
435 $result=array();
436 foreach($array as $k=>$v) {
437 $result[]=$v;
438 }
439 } else {
440 $key=array('Query', $query.'_'.$options['type']);
441 $result=$tcmp->Options->getCache($key);
442 if (!is_array($result) || count($result) == 0) {
443 $q=NULL;
444 $id='ID';
445 $name='post_title';
446 $function='';
447 switch ($query) {
448 case TCMP_QUERY_POSTS_OF_TYPE:
449 //$options=array('posts_per_page'=>-1, 'post_type'=>$args['post_type']);
450 //$q=get_posts($options);
451 $sql="SELECT ID, post_title FROM ".$wpdb->prefix."posts WHERE post_status='publish' AND post_type='".$options['type']."' ORDER BY post_title";
452 $q=$wpdb->get_results($sql);
453 $function='get_permalink';
454 break;
455 case TCMP_QUERY_CATEGORIES:
456
457 break;
458 case TCMP_QUERY_TAGS:
459
460 break;
461 case TCMP_QUERY_TAXONOMIES_OF_TYPE:
462
463 break;
464 }
465
466 $result=array();
467 if ($q) {
468 if(!is_wp_error($q)) {
469 foreach ($q as $v) {
470 $item=array('id' => $v->$id, 'name' => $v->$name);
471 if($parent!='') {
472 $item['parent']=$v->$parent;
473 }
474 $result[]=$item;
475 }
476 }
477 } elseif ($query==TCMP_QUERY_POST_TYPES) {
478 global $wp_post_types;
479 $result=array();
480 foreach($wp_post_types as $k=>$v) {
481 $isPublic=$tcmp->Utils->bget($v, 'public');
482 if($isPublic && $k!='attachment') {
483 $v=$tcmp->Utils->get($v, 'labels.singular_name');
484 if($k=='post' || $k=='page') {
485 $result[$k]=$v;
486 }
487 }
488 }
489 $result=$tcmp->Utils->toFormatListArrayFromListObjects($result, FALSE, '{text} ({id})');
490 } elseif($query==TCMP_QUERY_TAXONOMY_TYPES) {
491
492 }
493
494 if($this->functionExists($function)) {
495 for($i=0; $i<count($result); $i++) {
496 $v=$result[$i];
497 $v['url']=$this->functionCall($function, array($v['id']));
498 $result[$i]=$v;
499 }
500 }
501 $tcmp->Options->setCache($key, $result);
502 }
503 }
504
505 if ($options['all']) {
506 $first=array();
507 $first[]=array('id'=>-1, 'name'=>'['.$tcmp->Lang->L('All').']', 'url'=>'');
508 $result=array_merge($first, $result);
509 }
510 if ($options['select']) {
511 $first=array();
512 $first[]=array('id'=>0, 'name'=>'['.$tcmp->Lang->L('Select').']', 'url'=>'');
513 $result=array_merge($first, $result);
514 }
515 $result=$this->sortOptions($result);
516 $this->_taxonomyType='';
517 return $result;
518 }
519
520 //wp_parse_args with null correction
521 function parseArgs($options, $defaults) {
522 if (is_null($options)) {
523 $options=array();
524 } elseif(is_object($options)) {
525 $options=(array)$options;
526 } elseif(!is_array($options)) {
527 $options=array();
528 }
529 if (is_null($defaults)) {
530 $defaults=array();
531 } elseif(is_object($defaults)) {
532 $defaults=(array)$defaults;
533 } elseif(!is_array($defaults)) {
534 $defaults=array();
535 }
536
537 foreach($defaults as $k=>$v) {
538 if(is_null($v)) {
539 unset($defaults[$k]);
540 }
541 }
542
543 foreach($options as $k=>$v) {
544 if(isset($defaults[$k])) {
545 if (is_null($v)) {
546 //so can take the default value
547 unset($options[$k]);
548 } elseif (is_string($v) && ($v==='') && isset($defaults[$k]) && is_array($defaults[$k])) {
549 //a very strange case, i have a blank string for rappresenting an empty array
550 unset($options[$k]);
551 } else {
552 unset($defaults[$k]);
553 }
554 }
555 }
556 foreach($defaults as $k=>$v) {
557 $options[$k]=$v;
558 }
559 return $options;
560 }
561
562 function redirect($location) {
563 if($location=='') {
564 return;
565 }
566 //seems that if you have installed xdebug (or some version of it) doesnt work so js added
567 if(!headers_sent()) {
568 wp_redirect($location);
569 }
570 ?>
571 <script> window.location.replace('<?php echo $location?>'); </script>
572 <?php
573 die();
574 }
575
576 //return the element inside array with the specified key
577 function getArrayValue($key, $array, $value='') {
578 $result=FALSE;
579 if (isset($array[$key])) {
580 $result=$array[$key];
581 $result['name']=$key;
582 }
583 if($result!==FALSE && $value!='') {
584 if(isset($result[$value])) {
585 $result=$result[$value];
586 }
587 }
588 return $result;
589 }
590
591 var $_sortField;
592 var $_ignoreCase;
593 function aksort(&$array, $sortField='name', $ignoreCase=TRUE) {
594 $this->_sortField=$sortField;
595 $this->_ignoreCase=$ignoreCase;
596 usort($array, array($this, "aksortCompare"));
597 }
598 //not thread-safe!
599 private function aksortCompare($a, $b) {
600 if ($a===$b || $a==$b) {
601 return 0;
602 }
603
604 $result=0;
605 $a=$a[$this->_sortField];
606 $b=$b[$this->_sortField];
607 if(is_numeric($a) && is_numeric($b)) {
608 $result=($a < $b) ? -1 : 1;
609 } else {
610 $a.='';
611 $b.='';
612 if($this->_ignoreCase) {
613 $result=strcasecmp($a, $b);
614 } else {
615 $result=strcmp($a.'', $b);
616 }
617 }
618 return $result;
619 }
620
621 function printScriptCss() {
622 global $tcmp;
623 $uri=get_bloginfo('wpurl');
624 $tcmp->Tabs->enqueueScripts();
625 //wp_enqueue_style('buttons', $uri.'/wp-includes/css/buttons.min.css');
626 //wp_enqueue_style('editor', $uri.'/wp-includes/css/editor.min.css');
627 //wp_enqueue_style('jquery-ui-dialog', $uri.'/wp-includes/css/jquery-ui-dialog.min.css');
628 $styles='dashicons,admin-bar,buttons,media-views,wp-admin,wp-auth-check,wp-color-picker';
629 $styles=explode(',', $styles);
630 foreach($styles as $v) {
631 wp_enqueue_style($v);
632 }
633
634 remove_all_actions('wp_print_scripts');
635 print_head_scripts();
636 print_admin_styles();
637 }
638
639 public function formatCustomDate($time, $format) {
640 $time=$this->parseDateToTime($time);
641 if($time>0) {
642 $time=date($format, $time);
643 } else {
644 $time='';
645 }
646 return $time;
647 }
648
649 public function formatDatetime($time='now') {
650 return $this->formatCustomDate($time, TCMP_Utils::FORMAT_DATETIME);
651 }
652 public function formatCompactDatetime($time='now') {
653 return $this->formatCustomDate($time, TCMP_Utils::FORMAT_COMPACT_DATETIME);
654 }
655 public function formatDate($time='date') {
656 return $this->formatCustomDate($time, TCMP_Utils::FORMAT_DATE);
657 }
658 public function formatSmartDatetime($time='now') {
659 $time=$this->parseDateToTime($time);
660 $result='';
661 if($time>0) {
662 $h=intval(date('H', $time));
663 $i=intval(date('i', $time));
664 $s=intval(date('s', $time));
665 if($h==0 && $i==0 && $s==0) {
666 $result=$this->formatDate($time);
667 } else {
668 $result=$this->formatDatetime($time);
669 }
670 }
671 return $result;
672 }
673 public function formatTime($time='now') {
674 return $this->formatCustomTime($time, TCMP_Utils::FORMAT_TIME);
675 }
676 public function formatSqlDatetime($time='now') {
677 return $this->formatCustomDate($time, TCMP_Utils::FORMAT_SQL_DATETIME);
678 }
679 public function formatSqlDate($time='date') {
680 return $this->formatCustomDate($time, TCMP_Utils::FORMAT_SQL_DATE);
681 }
682 public function formatSqlTime($time='now') {
683 return $this->formatCustomTime($time, TCMP_Utils::FORMAT_SQL_TIME);
684 }
685
686 private function formatCustomTime($time, $format) {
687 $time=$this->parseDateToTime($time);
688 if($time>86400) {
689 $h=date('H', $time);
690 $i=date('i', $time);
691 $s=date('s', $time);
692 $time=$h*3600+$i*60+$s;
693 }
694
695 $s=$time%60;
696 $time=($time-$s)/60;
697 $i=$time%60;
698 $h=($time-$i)/60;
699 $s=str_pad($s, 2, "0", STR_PAD_LEFT);
700 $i=str_pad($i, 2, "0", STR_PAD_LEFT);
701 $h=str_pad($h, 2, "0", STR_PAD_LEFT);
702 $format=str_replace('H', $h, $format);
703 $format=str_replace('i', $i, $format);
704 $format=str_replace('s', $s, $format);
705 return $format;
706 }
707
708 public function parseNumber($what, $default=0) {
709 $result=$default;
710 if(is_array($what)) {
711 if(count($what)>0) {
712 $result=doubleval($what[0]);
713 }
714 }
715 elseif(is_numeric($what)) {
716 $result=doubleval($what);
717 } elseif(is_string($what) || is_bool($what)) {
718 $result=($this->isTrue($what) ? 1 : 0);
719 }
720 return $result;
721 }
722 public function parseDateToArray($date) {
723 global $tcmp;
724
725 $pm=FALSE;
726 $date=strtoupper(trim($date));
727 if($tcmp->Utils->endsWith($date, 'AM')) {
728 $date=substr($date, 0, strlen($date)-2);
729 $date=trim($date);
730 } elseif($tcmp->Utils->endsWith($date, 'PM')) {
731 $date=substr($date, 0, strlen($date)-2);
732 $date=trim($date);
733 $pm=TRUE;
734 }
735
736 $date=explode(' ', $date);
737 if(count($date)==1) {
738 $result=array();
739 $date=$date[0];
740 $date=str_replace("/", "-", $date);
741 if(strpos($date, '-')!==FALSE) {
742 $date=explode('-', $date);
743 if(count($date)>=3) {
744 $d=intval($date[0]);
745 $m=intval($date[1]);
746 $y=intval($date[2]);
747 if($d>1900) {
748 $t=$d;
749 $d=$y;
750 $y=$t;
751 }
752 if($y>0 && $m>0 && $d>0) {
753 $result['y']=$y;
754 $result['m']=$m;
755 $result['d']=$d;
756 }
757 }
758 } elseif(strpos($date, ':')!==FALSE) {
759 $date=explode(':', $date);
760 if(count($date)==2) {
761 $date[]=0;
762 }
763 if(count($date)>=3) {
764 $h=intval($date[0]);
765 $i=intval($date[1]);
766 $s=intval($date[2]);
767 if($h>=0 && $i>=0 && $s>=0) {
768 $result['h']=$h;
769 $result['i']=$i;
770 $result['s']=$s;
771 }
772 }
773 }
774 } else {
775 $a1=$this->parseDateToArray($date[0]);
776 $a2=$this->parseDateToArray($date[1]);
777 $result=$tcmp->Utils->parseArgs($a1, $a2);
778 }
779
780 if($pm && isset($result['h'])) {
781 $result['h']=intval($result['h'])+12;
782 }
783 return $result;
784 }
785 public function parseDateToTime($date) {
786 global $tcmp;
787 if(is_numeric($date) || trim($date)=='') {
788 $date=intval($date);
789 return $date;
790 }
791
792 $date=strtolower($date);
793 if($date=='now') {
794 $date=time();
795 return $date;
796 } elseif($date=='date') {
797 $date=strtotime(date('Y-m-d', time()));
798 return $date;
799 } elseif($date=='time') {
800 $date=date('H:i:s', time());
801 }
802 $result=$this->parseDateToArray($date);
803 $defaults=array('y'=>0, 'm'=>0, 'd'=>0, 'h'=>0, 'i'=>0, 's'=>0);
804 $a=$tcmp->Utils->parseArgs($result, $defaults);
805 if($a['y']==0 && $a['m']==0 && $a['d']==0) {
806 $result=$a['h']*3600+$a['i']*60+$a['s'];
807 } else {
808 $result=mktime($a['h'], $a['i'], $a['s'], $a['m'], $a['d'], $a['y']);
809 }
810 if($result<0) {
811 $result=0;
812 }
813 return $result;
814 }
815 public function getIntDate($time, $separator='') {
816 $time=$this->parseDateToTime($time);
817 if($time>0) {
818 if($separator=='') {
819 $time=date('Ymd', $time);
820 $time=intval($time);
821 } else {
822 $time=date('Y', $time).$separator.date('m', $time).$separator.date('d', $time);
823 }
824 }
825
826 return $time;
827 }
828 public function getIntMinute($h, $m, $separator='') {
829 $h=intval($h);
830 $m=intval($m);
831 if($m<10) {
832 $m='0'.$m;
833 }
834 $result=$h.$separator.$m;
835 if($separator=='') {
836 $result=intval($result);
837 }
838 return $result;
839 }
840
841 //args can be a string or an associative array if you want
842 public function getTextArgs($args, $defaults=array(), $excludes=array()) {
843 $result=$args;
844 $excludes=$this->toArray($excludes);
845 if(is_array($result) && count($result)>0) {
846 $result='';
847 foreach($args as $k=>$v) {
848 if(is_array($v) || is_object($v)) {
849 continue;
850 }
851
852 if(count($excludes)==0 || !in_array($k, $excludes)) {
853 $v=trim($v);
854 $result.=' '.$k.'="'.$v.'"';
855 }
856 }
857 } elseif(!$args) {
858 $result='';
859 }
860 if(is_array($defaults) && count($defaults)>0) {
861 foreach($defaults as $k=>$v) {
862 if(count($excludes)==0 || !in_array($k, $excludes)) {
863 if(!isset($args[$k])) {
864 $v=trim($v);
865 $result.=' '.$k.'="'.$v.'"';
866 }
867 }
868 }
869 }
870 return $result;
871 }
872 public function queryString($uri, $options=array()) {
873 if(is_string($options)) {
874 $options=explode('&', $options);
875 $array=array();
876 foreach($options as $v) {
877 $v=explode('=', $v);
878 if(count($v)>1) {
879 $array[trim($v[0])]=trim($v[1]);
880 }
881 }
882 $options=$array;
883 }
884 if(!isset($options['root']) || $this->isTrue($options['root'])) {
885 $uri=TCMP_BLOG_URL.$uri;
886 }
887 unset($options['root']);
888 $uri=$this->addQueryString($options, $uri);
889 return $uri;
890 }
891 public function iuarray($ids, $positive=FALSE) {
892 $array=$this->iarray($ids, $positive);
893 $array=array_unique($array);
894 sort($array);
895 return $array;
896 }
897 public function iarray($ids, $positive=FALSE) {
898 if(is_string($ids)) {
899 $ids=explode(',', $ids);
900 } elseif(is_numeric($ids)) {
901 $ids=array($ids);
902 } elseif(!is_array($ids)) {
903 $ids=array();
904 }
905
906 $array=array();
907 foreach($ids as $v) {
908 $v=trim($v);
909 if($v!='') {
910 $v=intval($v);
911 if(!$positive || $v>0) {
912 $array[]=$v;
913 }
914 }
915 }
916 return $array;
917 }
918 public function dbarray($ids) {
919 if(is_string($ids)) {
920 $ids=explode(',', $ids);
921 } elseif(is_numeric($ids)) {
922 $ids=array($ids);
923 } elseif(!is_array($ids)) {
924 $ids=array();
925 }
926
927 $array=array();
928 foreach($ids as $v) {
929 $v=trim($v);
930 if($v!='') {
931 if(is_numeric($v)) {
932 $v=intval($v);
933 }
934 $array[]=$v;
935 }
936 }
937 return $array;
938 }
939
940 function isAssociativeArray($array) {
941 if(!is_array($array)) {
942 return FALSE;
943 }
944
945 $isArray=TRUE;
946 $i=0;
947 foreach($array as $k=>$v) {
948 if($k!==$i) {
949 $isArray=FALSE;
950 break;
951 }
952 ++$i;
953 }
954 return !$isArray;
955 }
956 function trim($value) {
957 if(is_null($value)) {
958
959 } elseif(is_string($value)) {
960 $value=trim($value);
961 } elseif(is_numeric($value)) {
962
963 } elseif($this->isAssociativeArray($value)) {
964 foreach($value as $k=>$v) {
965 $value[$k]=$this->trim($v);
966 }
967 } elseif(is_object($value)) {
968 foreach($value as $k=>$v) {
969 $value->$k=$this->trim($v);
970 }
971 } elseif(is_array($value)) {
972 for($i=0; $i<count($value); $i++) {
973 $v=$value[$i];
974 $this->trim($v);
975 $value[$i]=$v;
976 }
977 }
978 return $value;
979 }
980 function implode($open, $close, $join, $array) {
981 $result='';
982 foreach($array as $v) {
983 if($result!='') {
984 $result.=$join;
985 }
986 $result.=$open.$v.$close;
987 }
988 return $result;
989 }
990 function toArray($text, $index=-1, $default='') {
991 if(is_array($text)) {
992 if(is_string($index)) {
993 $array=array();
994 foreach($text as $v) {
995 $v=$this->get($v, $index, FALSE);
996 if($v!==FALSE) {
997 $array[]=$v;
998 }
999 }
1000 } else {
1001 $array=$text;
1002 }
1003 return $array;
1004 } elseif(is_numeric($text)) {
1005 return array($text);
1006 } elseif(is_bool($text) || $text==='') {
1007 return array();
1008 }
1009
1010 if(($this->startsWith($text, '[') && $this->endsWith($text, ']'))
1011 || ($this->startsWith($text, '{') && $this->endsWith($text, '}'))) {
1012 $text=substr($text, 1, strlen($text)-2);
1013 }
1014 $text=str_replace('|', ',', $text);
1015 $text=explode(',', $text);
1016
1017 //exclude empty string
1018 $array=array();
1019 foreach($text as $t) {
1020 if($t!=='') {
1021 $array[]=$t;
1022 }
1023 }
1024 $text=$array;
1025 if($index>-1) {
1026 $result=$default;
1027 if(isset($text[$index])) {
1028 $result=$text[$index];
1029 }
1030 $text=$result;
1031 }
1032 return $text;
1033 }
1034 function dirToFlatArray($dir, &$output) {
1035 if(!isset($output['dirs'])) {
1036 $output['dirs']=array();
1037 }
1038 if(!isset($output['files'])) {
1039 $output['files']=array();
1040 }
1041
1042 $cdir=scandir($dir);
1043 foreach ($cdir as $k=>$v) {
1044 if (!in_array($v,array(".",".."))) {
1045 if (is_dir($dir.DIRECTORY_SEPARATOR.$v)) {
1046 $i=$dir.DIRECTORY_SEPARATOR.$v;
1047 array_push($output['dirs'], $i);
1048 $this->dirToFlatArray($i, $output);
1049 } else {
1050 $i=$this->getFileInfo($dir.DIRECTORY_SEPARATOR.$v);
1051 array_push($output['files'], $i);
1052 }
1053 }
1054 }
1055 }
1056 function dirToArray($dir) {
1057 $result=array();
1058 if(!is_string($dir)) {
1059 return $result;
1060 }
1061
1062 $cdir=scandir($dir);
1063 foreach ($cdir as $k=>$v) {
1064 if (!in_array($v, array(".",".."))) {
1065 if (is_dir($dir.DIRECTORY_SEPARATOR.$v)) {
1066 $result[$v]=$this->dirToArray($dir.DIRECTORY_SEPARATOR.$v);
1067 } else {
1068 $result[]=$this->getFileInfo($dir.DIRECTORY_SEPARATOR.$v);
1069 }
1070 }
1071 }
1072 return $result;
1073 }
1074 function getFileInfo($source) {
1075 $source=$this->toDirectory($source);
1076 if(!file_exists($source)) {
1077 return FALSE;
1078 }
1079
1080 $array=explode(DIRECTORY_SEPARATOR, $source);
1081 $size=filesize($source);
1082 $source=array_pop($array);
1083 $directory=implode(DIRECTORY_SEPARATOR, $array).DIRECTORY_SEPARATOR;
1084
1085 $pos=strrpos($source, ".");
1086 $ext='';
1087 if($pos!==FALSE) {
1088 $name=substr($source, 0, $pos);
1089 $ext=strtolower(substr($source, $pos));
1090 }
1091 $array=array(
1092 'directory'=>$directory
1093 , 'name'=>$name
1094 , 'file'=>$source
1095 , 'size'=>$size
1096 , 'textSize'=>$this->getFileTextSize($size)
1097 , 'ext'=>$ext
1098 , 'textExt'=>$this->getFileTextExt($source)
1099 );
1100 return $array;
1101 }
1102 function getFileTextSize($size) {
1103 $units=array('B', 'KB', 'MB', 'GB');
1104 for ($i=0; $i<count($units); $i++) {
1105 if($size<1024) {
1106 break;
1107 } else {
1108 $size/=1024;
1109 }
1110 }
1111 return intval($size).' '.$units[$i];
1112 }
1113 function getFileTextExt($source) {
1114 $ext=strrpos($source, ".");
1115 if($ext!==FALSE) {
1116 $ext=strtolower(substr($source, $ext+1));
1117 } else {
1118 $ext=$source;
1119 }
1120 $ext=strtolower($ext);
1121 $text='text';
1122 switch ($ext) {
1123 case 'doc':
1124 case 'docx':
1125 case 'odt':
1126 $text='word';
1127 break;
1128 case 'xls':
1129 case 'xlsx':
1130 case 'ods':
1131 $text='excel';
1132 break;
1133 case 'ppt':
1134 case 'pptx':
1135 case 'odp':
1136 $text='powerpoint';
1137 break;
1138 case 'zip':
1139 case 'tar':
1140 case 'gzip':
1141 case 'rar':
1142 case '7z':
1143 $text='archive';
1144 break;
1145 case 'mp3':
1146 case 'wav':
1147 $text='audio';
1148 break;
1149 case 'mpeg':
1150 case 'mpg':
1151 case 'avi':
1152 case 'mp4':
1153 $text='video';
1154 break;
1155 case 'gif':
1156 case 'jpg':
1157 case 'jpeg':
1158 case 'png':
1159 case 'bmp':
1160 $text='image';
1161 break;
1162 case 'pdf':
1163 $text='pdf';
1164 break;
1165 }
1166 return $text;
1167 }
1168 function match($value, $array, $default='', $ignoreCase=TRUE) {
1169 $result=$default;
1170 if($ignoreCase) {
1171 $value=strtolower($value);
1172 }
1173 foreach($array as $k=>$v) {
1174 $v=$this->toArray($v);
1175 foreach($v as $c) {
1176 if($ignoreCase) {
1177 $c=strtolower($c);
1178 }
1179 if($value==$c || strpos($value, $c)!==FALSE) {
1180 $result=$k;
1181 break;
1182 }
1183 }
1184
1185 if($result!==$default) {
1186 break;
1187 }
1188 }
1189 return $result;
1190 }
1191
1192 function pickColor() {
1193 $names=explode('|', 'primary|success|warning|danger|info|alert|system|dark');
1194 $colors=explode('|', '3498db|70ca63|f6bb42|df5640|3bafda|967adc|37bc9b|666');
1195
1196 $i=($this->colorIndex%count($colors));
1197 $names=$names[$i];
1198 $colors=$colors[$i];
1199 ++$this->colorIndex;
1200 return array($names, '#'.$colors);
1201 }
1202 function upperUnderscoreCase($text) {
1203 $text=$this->arrayCase($text);
1204 $text=implode('_', $text);
1205 $text=strtoupper($text);
1206 return $text;
1207 }
1208 function lowerUnderscoreCase($text) {
1209 $text=$this->upperUnderscoreCase($text);
1210 $text=strtolower($text);
1211 return $text;
1212 }
1213 function toDirectory($file, $mkdirs=FALSE) {
1214 $file=str_replace("\\", DIRECTORY_SEPARATOR, $file);
1215 $file=str_replace("/", DIRECTORY_SEPARATOR, $file);
1216 $file=str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $file);
1217
1218 if(is_dir($file) && !file_exists($file) && $mkdirs) {
1219 mkdir($file, 0777, TRUE);
1220 }
1221 return $file;
1222 }
1223 function getUploadName($name) {
1224 if($name=='') {
1225 return '';
1226 }
1227
1228 $name=$this->toDirectory($name);
1229 $name=explode(DIRECTORY_SEPARATOR, $name);
1230 $name=$name[count($name)-1];
1231 $ext='';
1232 $pos=strpos($name, '.');
1233 if($pos!==FALSE) {
1234 $ext=substr($name, $pos);
1235 $name=substr($name, 0, $pos);
1236 }
1237
1238 $buffer='';
1239 $name=str_split(strtolower($name));
1240 for($i=0; $i<count($name); $i++) {
1241 if($name[$i]>='a' && $name[$i]<='z') {
1242 $buffer.=$name[$i];
1243 } else {
1244 $buffer.=' ';
1245 }
1246 }
1247 while(strpos($buffer, ' ')!==FALSE) {
1248 $buffer=str_replace(' ', ' ', $buffer);
1249 }
1250 $buffer=trim($buffer);
1251 $buffer=str_replace(' ', '-', $buffer);
1252 $buffer.='-'.date('Ymd-His', time()).$ext;
1253 return $buffer;
1254 }
1255 function toListArrayFromClass($array, $id=FALSE, $value=FALSE) {
1256 global $tcmp;
1257 $result=array();
1258 if($array!==FALSE && count($array)>0) {
1259 foreach($array as $k=>$v) {
1260 if($id!==FALSE) {
1261 $k=$tcmp->Utils->get($v, $id);
1262 }
1263 if($value!==FALSE) {
1264 $v=$tcmp->Utils->get($v, $value);
1265 }
1266
1267 if($k!='' && $v!='') {
1268 $result[]=array(
1269 'id'=>$k
1270 , 'text'=>$v
1271 , 'name'=>$v
1272 );
1273 }
1274 }
1275 }
1276 return $result;
1277 }
1278 function toFormatListArrayFromListObjects($array, $idField, $textFormat) {
1279 global $tcmp;
1280 $result=array();
1281 if($array!==FALSE && count($array)>0) {
1282 foreach($array as $i=>$e) {
1283 $text=$textFormat;
1284 $idExists=FALSE;
1285 if(is_array($e) || is_object($e)) {
1286 foreach($e as $k=>$v) {
1287 if($k=='id') {
1288 $idExists=TRUE;
1289 }
1290 if(is_array($v)) {
1291 $v=implode(', ', $v);
1292 }
1293 $text=str_replace("{".$k."}", $v, $text);
1294 }
1295 } else {
1296 $text=str_replace("{text}", $e, $text);
1297 }
1298
1299 $id=$i;
1300 if($idField!==FALSE && $idField!=='') {
1301 $id=$tcmp->Utils->get($e, $idField, '');
1302 }
1303
1304 if(!$idExists) {
1305 $text=str_replace("{id}", $id, $text);
1306 }
1307 if($id!='') {
1308 $result[]=array(
1309 'id'=>$id
1310 , 'text'=>$text
1311 , 'name'=>$text
1312 );
1313 }
1314 }
1315 }
1316 return $result;
1317 }
1318 function toListArrayFromListObjects($array
1319 , $idFrom=FALSE, $textFrom='name', $idTo='id', $textTo='text') {
1320
1321 $result=array();
1322 foreach($array as $v) {
1323 $sId=$v;
1324 $sText=$v;
1325 if($idFrom!==FALSE) {
1326 $sId=$this->get($v, $idFrom, FALSE);
1327 $sText=$this->get($v, $textFrom, FALSE);
1328 }
1329 if($sId!==FALSE && $sText!='') {
1330 if($sId!='') {
1331 $result[]=array(
1332 $idTo=>$sId
1333 , $textTo=>$sText
1334 );
1335 }
1336 }
1337 }
1338 return $result;
1339 }
1340 function toColorListArrayFromListObjects($array, $colors, $id='id', $text='name') {
1341 global $tcmp;
1342 $result=array();
1343 foreach($array as $instance) {
1344 $sId=$this->get($instance, $id, FALSE);
1345 $sText=$this->get($instance, $text, FALSE);
1346 foreach($colors as $color=>$when) {
1347 $success=FALSE;
1348 foreach($when['conditions'] as $conditionKey=>$conditionValue) {
1349 $conditionValue=$tcmp->Utils->toArray($conditionValue);
1350 $c=$this->get($instance, $conditionKey, FALSE);
1351 if($c!==FALSE) {
1352 $c.='';
1353 foreach($conditionValue as $v) {
1354 $v.='';
1355 if($c===$v) {
1356 $success=TRUE;
1357 break;
1358 }
1359 }
1360 }
1361 if($success) {
1362 break;
1363 }
1364 }
1365
1366 if($success) {
1367 $style='color:'.$color.'; ';
1368 if(isset($when['bold']) && $when['bold']) {
1369 $style.='font-weight:bold; ';
1370 }
1371 $sText='<span style="'.$style.'">'.$sText.'</span>';
1372 }
1373 }
1374 if($sId!='' && $sText!==FALSE) {
1375 $result[]=array(
1376 'id'=>$sId
1377 , 'text'=>$sText
1378 , 'name'=>$sText
1379 );
1380 }
1381 }
1382 return $result;
1383 }
1384 function md5() {
1385 $array=func_get_args();
1386 $buffer='';
1387 foreach($array as $v) {
1388 $buffer.=':)'.$v;
1389 }
1390 $buffer=md5($buffer);
1391 return $buffer;
1392 }
1393 function arrayCase($text) {
1394 $buffer='';
1395 $array=array();
1396 $text=str_split($text);
1397 $prevUpper=FALSE;
1398 $nextUpper=FALSE;
1399 foreach($text as $c) {
1400 if($c>='a' && $c<='z') {
1401 if($nextUpper) {
1402 if($buffer!='') {
1403 $array[]=$buffer;
1404 $buffer='';
1405 }
1406 $c=strtoupper($c);
1407 }
1408 $buffer.=$c;
1409 $nextUpper=FALSE;
1410 $prevUpper=FALSE;
1411 } elseif($c>='0' && $c<='9') {
1412 $buffer.=$c;
1413 $nextUpper=TRUE;
1414 } elseif($c>='A' && $c<='Z') {
1415 if(!$prevUpper) {
1416 if($buffer!='') {
1417 $array[]=$buffer;
1418 $buffer='';
1419 }
1420 }
1421 $buffer.=$c;
1422 $nextUpper=FALSE;
1423 $prevUpper=TRUE;
1424 } else {
1425 if($buffer!='') {
1426 $array[]=$buffer;
1427 $buffer='';
1428 }
1429 $nextUpper=TRUE;
1430 $prevUpper=FALSE;
1431 }
1432 }
1433 if($buffer!='') {
1434 $array[]=$buffer;
1435 }
1436 return $array;
1437 }
1438 function lowerCamelCase($text) {
1439 $buffer='';
1440 if(strpos($text, '_')!==FALSE || strpos($text, '-')!==FALSE) {
1441 $text=strtolower($text);
1442 }
1443
1444 $text=str_split($text);
1445 $allUpper=TRUE;
1446 $nextUpper=FALSE;
1447 foreach($text as $c) {
1448 if($c>='a' && $c<='z') {
1449 $allUpper=FALSE;
1450 if($nextUpper) {
1451 $c=strtoupper($c);
1452 }
1453 $buffer.=$c;
1454 $nextUpper=FALSE;
1455 } elseif($c>='0' && $c<='9') {
1456 $buffer.=$c;
1457 $nextUpper=TRUE;
1458 } elseif($c>='A' && $c<='Z') {
1459 $buffer.=$c;
1460 $nextUpper=FALSE;
1461 } else {
1462 $nextUpper=TRUE;
1463 }
1464 }
1465 if($allUpper) {
1466 $buffer=strtolower($buffer);
1467 } else {
1468 $buffer=lcfirst($buffer);
1469 }
1470 return $buffer;
1471 }
1472 function upperCamelCase($text) {
1473 $text=$this->lowerCamelCase($text);
1474 $text=ucfirst($text);
1475 return $text;
1476 }
1477
1478 function castStdClass($a) {
1479 $a=(array)$a;
1480 $r=new stdClass();
1481 foreach($a as $k=>$v) {
1482 $r->$k=$v;
1483 }
1484 return $r;
1485 }
1486 function castArray($a) {
1487 $r=$a;
1488 if(is_object($a)) {
1489 $r=(array)$a;
1490 }
1491
1492 if(!is_array($r)) {
1493 $r=array();
1494 }
1495 return $r;
1496 }
1497 public function copyArray($array) {
1498 $temp=array();
1499 foreach($array as $k=>$v) {
1500 $temp[$k]=$v;
1501 }
1502 return $temp;
1503 }
1504 public function isObject($v) {
1505 return ($v!==FALSE && !is_null($v) && is_object($v));
1506 }
1507 public function isArray($v) {
1508 return ($v!==FALSE && !is_null($v) && is_array($v));
1509 }
1510 public function getConstants($class, $prefix, $reverse=FALSE) {
1511 global $tcmp;
1512 if(is_object($class)) {
1513 $class=get_class($class);
1514 }
1515 $class=str_replace('Search', '', $class);
1516 $class=str_replace('Constants', '', $class);
1517 $class.='Constants';
1518 if(!class_exists($class)) {
1519 $class=TCMP_PLUGIN_PREFIX.$class;
1520 }
1521
1522 $result=array();
1523 if(class_exists($class)) {
1524 $reflection=new ReflectionClass($class);
1525 $array=$reflection->getConstants();
1526 foreach($array as $k=>$v) {
1527 $pos=0;
1528 if($prefix!='') {
1529 $pos=stripos($k, $prefix);
1530 }
1531 if($pos===0) {
1532 if($reverse) {
1533 $result[$v]=$k;
1534 } else {
1535 $result[$k]=$v;
1536 }
1537 }
1538 }
1539 }
1540 return $result;
1541 }
1542 public function getConstantValue($class, $prefix, $name, $default=FALSE) {
1543 /* @var $ec TCMP_Singleton */
1544 global $ec;
1545 $result=$default;
1546 if(is_object($class)) {
1547 $class=get_class($class);
1548 }
1549 $class=str_replace('Search', '', $class);
1550 $class=str_replace('Constants', '', $class);
1551 $class.='Constants';
1552 if(!class_exists($class)) {
1553 $class=TCMP_PLUGIN_PREFIX.$class;
1554 }
1555
1556 if(class_exists($class)) {
1557 $name=$prefix.'_'.$name;
1558 $name=$ec->Utils->upperUnderscoreCase($name);
1559 $reflection=new ReflectionClass($class);
1560 $result=$reflection->getConstant($name);
1561 }
1562 return $result;
1563 }
1564 public function getConstantName($class, $prefix, $value, $default=FALSE) {
1565 /* @var $ec TCMP_Singleton */
1566 $constants=$this->getConstants($class, $prefix, TRUE);
1567 $result=$default;
1568 if(isset($constants[$value])) {
1569 $result=$constants[$value];
1570 }
1571 return $result;
1572 }
1573 public function daysDiff($dt1, $dt2) {
1574 $dt1=$this->parseDateToTime($dt1);
1575 $dt2=$this->parseDateToTime($dt2);
1576 $result=($dt2-$dt1)/86400;
1577 $result=intval($result);
1578 return $result;
1579 }
1580 public function getFirstLastDayOfWeek($dt) {
1581 $dt=$this->parseDateToTime($dt);
1582 // Get the day of the week: Sunday=0 to Saturday=6
1583 $dotw=date('w', $dt);
1584 if($dotw>1) {
1585 $dt1=$dt-(($dotw-1)*24*60*60);
1586 $dt2=$dt+((7-$dotw)*24*60*60);
1587 }
1588 else if($dotw==1) {
1589 $dt1=$dt;
1590 $dt2=$dt+((7-$dotw)*24*60*60);
1591 } else if($dotw==0) {
1592 $dt1=$dt -(6*24*60*60);;
1593 $dt2=$dt;
1594 }
1595
1596 $result=array($dt1, $dt2);
1597 return $result;
1598 }
1599 public function toMap($array, $key=FALSE, $value=FALSE, $classes=FALSE) {
1600 $classes=$this->toArray($classes);
1601 $result=array();
1602 if(is_string($array)) {
1603 $array=$this->toArray($array);
1604 $key=FALSE;
1605 $value=FALSE;
1606 }
1607 if(!is_array($array)) {
1608 $array=array();
1609 }
1610
1611 $assoc=$this->isAssociativeArray($array);
1612 foreach($array as $k=>$v) {
1613 if(!$assoc) {
1614 $k=$v;
1615 }
1616
1617 if($classes!==FALSE && count($classes)>0 && is_object($v)) {
1618 $class=get_class($v);
1619 if(!in_array($class, $classes)) {
1620 continue;
1621 }
1622 }
1623
1624 if($key!==FALSE) {
1625 $k=$this->get($v, $key, FALSE);
1626 }
1627 if($value!==FALSE) {
1628 $v=$this->get($v, $value, FALSE);
1629 }
1630
1631 if($k!==FALSE && $v!==FALSE) {
1632 $result[$k]=$v;
1633 }
1634 }
1635 return $result;
1636 }
1637 public function getText($text, $args) {
1638 if($args===FALSE || count($args)==0) {
1639 return $text;
1640 }
1641
1642 foreach($args as $k=>$v) {
1643 $text=str_replace("{".$k."}", $v, $text);
1644 }
1645 return $text;
1646 }
1647 public function arrayExtends($options, $defaults) {
1648 global $tcmp;
1649 $options=$tcmp->Utils->parseArgs($options, $defaults);
1650 foreach ($options as $k=>$v) {
1651 if(is_bool($v)) {
1652 $v=($v ? 1 : 0);
1653 }
1654 if (isset($defaults[$k])) {
1655 if($this->isAssociativeArray($v)) {
1656 $v=$this->arrayExtends($v, $defaults[$k]);
1657 } else {
1658 $v=$tcmp->Utils->toArray($v);
1659 $old=$defaults[$k];
1660 $old=$tcmp->Utils->toArray($old);
1661 if(!$this->isAssociativeArray($old)) {
1662 $v=array_merge($v, $old);
1663 $v=array_unique($v);
1664 }
1665 }
1666 } else {
1667 $v=$tcmp->Utils->toArray($v);
1668 }
1669 $options[$k]=$v;
1670 }
1671 return $options;
1672 }
1673 //send remote request to our server to store tracking and feedback
1674 function remotePost($action, $data = '') {
1675 global $tcmp;
1676
1677 $data['secret']='WYSIWYG';
1678 $response = wp_remote_post(TCMP_INTELLYWP_ENDPOINT.'?iwpm_action='.$action, array(
1679 'method'=>'POST'
1680 , 'timeout'=>20
1681 , 'redirection'=>5
1682 , 'httpversion'=>'1.1'
1683 , 'blocking'=>TRUE
1684 , 'body'=>$data
1685 , 'user-agent'=>TCMP_PLUGIN_NAME.'/'.TCMP_PLUGIN_VERSION.'; '.get_bloginfo('url')
1686 ));
1687 $data = json_decode(wp_remote_retrieve_body($response), TRUE);
1688 if (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200
1689 || !isset($data['success']) || !$data['success']
1690 ) {
1691 $tcmp->Log->error('ERRORS SENDING REMOTE-POST ACTION=%s DUE TO REASON=%s', $action, $response);
1692 $data = FALSE;
1693 } else {
1694 $tcmp->Log->debug('SUCCESSFULLY SENT REMOTE-POST ACTION=%s RESPONSE=%s', $action, $data);
1695 }
1696 return $data;
1697 }
1698 function remoteGet($uri, $options) {
1699 global $tcmp;
1700 $result=FALSE;
1701 $uri=$this->addQueryString($options, $uri);
1702 //$uri=str_replace('https://', 'http://', $uri);
1703
1704 $args=array('timeout'=>900, 'sslverify'=>false);
1705 $tcmp->Log->debug('REMOTEGET: URI=%s', $uri);
1706 $response=FALSE;//wp_remote_get($uri, $args);
1707 if ($response!==FALSE && !is_wp_error($response)) {
1708 // decode the license data
1709 $body=wp_remote_retrieve_body($response);
1710 if($body!==FALSE && $body!='') {
1711 $tcmp->Log->debug('REMOTEGET: RETRIEVEBODY=%s', $body);
1712 $result=json_decode($body);
1713 if(!$result) {
1714 $tcmp->Log->error('REMOTEGET: UNDECODABLE BODY');
1715 if(strpos($body, 'debug')!==FALSE || strpos($body, 'fatal')!==FALSE || strpos($body, 'warning')!==FALSE || strpos($body, 'error')!==FALSE) {
1716 $tcmp->Options->pushErrorMessage('XdebugException');
1717 }
1718 }
1719 } else {
1720 $tcmp->Log->debug('REMOTEGET: RETRIEVEBODY ERROR');
1721 $result=FALSE;
1722 }
1723 } else {
1724 $tcmp->Log->debug('REMOTEGET: RESULT=ERROR');
1725 }
1726 if($result===FALSE) {
1727 $tcmp->Log->debug('file_get_contents: URI=%s', $uri);
1728 //$body=file_get_contents($uri);
1729 $ch=curl_init();
1730 $timeout=900;
1731 curl_setopt($ch, CURLOPT_URL, $uri);
1732 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1733 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
1734 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1735 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
1736 $body=curl_exec($ch);
1737 curl_close($ch);
1738
1739 $tcmp->Log->debug('file_get_contents: RETRIEVEBODY=%s', $body);
1740 if($body!==FALSE && $body!='') {
1741 $tcmp->Log->debug('REMOTEGET: RETRIEVEBODY=%s', $body);
1742 $result=json_decode($body);
1743 if(!$result) {
1744 $tcmp->Log->error('REMOTEGET: UNDECODABLE BODY');
1745 if(strpos($body, 'xdebug')!==FALSE) {
1746 $tcmp->Options->pushErrorMessage('XdebugException');
1747 }
1748 }
1749 } else {
1750 $tcmp->Log->debug('REMOTEGET: RETRIEVEBODY ERROR');
1751 $result=FALSE;
1752 }
1753 }
1754 /*if(TRUE || $result===FALSE) {
1755 $ch=curl_init();
1756 curl_setopt($ch, CURLOPT_URL, $uri);
1757 curl_setopt($ch, CURLOPT_HEADER, 0);
1758 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
1759 $result=curl_exec($ch);
1760 curl_close($ch);
1761 if($result!==FALSE && $result!='') {
1762 $result=json_decode($result);
1763 } else {
1764 $result=FALSE;
1765 }
1766 }*/
1767 if(!$result) {
1768 $result=FALSE;
1769 }
1770 return $result;
1771 }
1772
1773 function isAdminUser() {
1774 //https://wordpress.org/support/topic/how-to-check-admin-right-without-include-pluggablephp
1775 return TRUE;
1776 /*
1777 if (!function_exists('wp_get_current_user')) {
1778 require_once(ABSPATH.'wp-includes/pluggable.php');
1779 }
1780 return (is_multisite() || current_user_can('manage_options'));
1781 */
1782 }
1783 function isUserLogged() {
1784 if (!function_exists('is_user_logged_in')) {
1785 require_once(ABSPATH.'wp-includes/pluggable.php');
1786 }
1787 $result=is_user_logged_in();
1788 return $result;
1789 }
1790 function isPluginPage() {
1791 global $tcmp;
1792 $page=TCMP_SQS('page');
1793 $result=($this->startsWith($page, TCMP_PLUGIN_SLUG));
1794 return $result;
1795 }
1796 function isQsNull($v) {
1797 return (is_null($v) || $v===FALSE || $v==='');
1798 }
1799 function jsonToClass($json, $class) {
1800 global $tcmp;
1801 $instance=$tcmp->Dao->Utils->getClass($class);
1802 if($instance=='') {
1803 throw new Exception('CLASS ['.$class.'] DOES NOT EXIST');
1804 }
1805 $result=FALSE;
1806 if(is_bool($json)) {
1807 return $json;
1808 }
1809 if(is_string($json)) {
1810 $json=json_decode($json);
1811 }
1812 if($class=='stdClass') {
1813 $result=new stdClass();
1814 foreach ($json as $k=>$v) {
1815 $result->$k=$v;
1816 }
1817 return $result;
1818 }
1819
1820 if($tcmp->Utils->isArray($json) && !$tcmp->Utils->isAssociativeArray($json)) {
1821 $match=FALSE;
1822 $result=array();
1823 foreach($json as $v) {
1824 $v=$this->jsonToInstance($json, $v);
1825 if($v!==FALSE) {
1826 $result[]=$v;
1827 $match=TRUE;
1828 }
1829 }
1830 if(!$match) {
1831 $result=FALSE;
1832 }
1833 } elseif($tcmp->Utils->isAssociativeArray($json) || is_object($json)) {
1834 $result=$this->jsonToInstance($json, $class);
1835 }
1836 return $result;
1837 }
1838 private function jsonToInstance($json, $class) {
1839 global $tcmp;
1840
1841 $match=FALSE;
1842 $result=FALSE;
1843 $columns=$tcmp->Dao->Utils->getAllColumns($class);
1844 $instance=$tcmp->Dao->Utils->getClass($class);
1845 $instance=new $instance();
1846 foreach($json as $property=>$value) {
1847 if(isset($columns[$property])) {
1848 $column=$columns[$property];
1849 if(isset($column['ui-type']) && $column['ui-type']=='array') {
1850 $array=array();
1851 $rel=$column['rel'];
1852 foreach ($value as $k => $v) {
1853 $v=$this->jsonToInstance($v, $rel);
1854 $array[$k] = $v;
1855 }
1856 $value=$array;
1857 } else {
1858 $value=$tcmp->Dao->Utils->decode($class, $property, $value);
1859 }
1860 }
1861 if($tcmp->Utils->set($instance, $property, $value)) {
1862 $match=TRUE;
1863 }
1864 }
1865 if($match) {
1866 $result=$instance;
1867 }
1868 return $result;
1869 }
1870 public function classToJson($instance) {
1871 if(!is_object($instance)) {
1872 $instance=(array)$instance;
1873 }
1874 $result=wp_json_encode($instance);
1875 return $result;
1876 }
1877
1878 function dateGt($dt1, $dt2) {
1879 $dt1=$this->parseDateToTime($dt1);
1880 $dt2=$this->parseDateToTime($dt2);
1881 return ($dt1>$dt2);
1882 }
1883 function dateGtEq($dt1, $dt2) {
1884 $dt1=$this->parseDateToTime($dt1);
1885 $dt2=$this->parseDateToTime($dt2);
1886 return ($dt1>=$dt2);
1887 }
1888 function dateEq($dt1, $dt2) {
1889 $dt1=$this->parseDateToTime($dt1);
1890 $dt2=$this->parseDateToTime($dt2);
1891 return ($dt1==$dt2);
1892 }
1893 function dateLt($dt1, $dt2) {
1894 $dt1=$this->parseDateToTime($dt1);
1895 $dt2=$this->parseDateToTime($dt2);
1896 return ($dt1<$dt2);
1897 }
1898 function dateLtEq($dt1, $dt2) {
1899 $dt1=$this->parseDateToTime($dt1);
1900 $dt2=$this->parseDateToTime($dt2);
1901 return ($dt1<=$dt2);
1902 }
1903 function absDateDiff($dt1, $dt2, $unit='d') {
1904 $diff=$this->dateDiff($dt1, $dt2, $unit);
1905 $diff=abs($diff);
1906 return $diff;
1907 }
1908 function dateDiff($dt1, $dt2, $unit='d') {
1909 $dt1=$this->formatSqlDatetime($dt1);
1910 $dt2=$this->formatSqlDatetime($dt2);
1911 $dt1=new DateTime($dt1);
1912 $dt2=new DateTime($dt2);
1913 $diff=$dt1->diff($dt2);
1914
1915 $result=0;
1916 switch ($unit) {
1917 case 'Y':
1918 case 'y':
1919 $result=$diff->y;
1920 break;
1921 case 'm':
1922 $result=$diff->m;
1923 break;
1924 case 'd':
1925 $result=$diff->days;
1926 break;
1927 case 'H':
1928 case 'h':
1929 $result=$diff->h;
1930 break;
1931 case 'n':
1932 case 'i':
1933 $result=$diff->i;
1934 break;
1935 case 's':
1936 $result=$diff->s;
1937 break;
1938 }
1939 return $result;
1940 }
1941 public function arrayPush(&$array, $another) {
1942 if(!is_array($another)) {
1943 array_push($array, $another);
1944 } elseif(is_array($another)) {
1945 foreach($another as $v) {
1946 array_push($array, $v);
1947 }
1948 }
1949 return $array;
1950 }
1951 public function encodeData($data) {
1952 $dataType='';
1953 $dataClass='';
1954 $text=FALSE;
1955 if(is_object($data)) {
1956 $dataType='class';
1957 $dataClass=get_class($data);
1958 $text=$this->classToJson($data);
1959 } elseif(is_array($data)) {
1960 $dataType='array';
1961 if(count($data)>0) {
1962 //array of class??
1963 $associative=$this->isAssociativeArray($data);
1964 foreach($data as $k=>$v) {
1965 break;
1966 }
1967 if(is_object($v)) {
1968 $dataType='class';
1969 $dataClass=get_class($v);
1970 $text=$this->classToJson($data);
1971 } else {
1972 $text=json_encode($data);
1973 }
1974 } else {
1975 $text=json_encode($data);
1976 }
1977 } else {
1978 $dataType='primitive';
1979 $text=json_encode($data);
1980 }
1981
1982 $result=array(
1983 'dataType'=>$dataType
1984 , 'dataClass'=>$dataClass
1985 , 'data'=>$this->httpEncode($text)
1986 );
1987 return $result;
1988 }
1989 public function decodeData($data=array()) {
1990 $defaults=array(
1991 'dataType'=>$this->qs('dataType', '')
1992 , 'dataClass'=>$this->qs('dataClass', '')
1993 , 'data'=>$this->qs('data', '')
1994 );
1995 $data=$this->parseArgs($data, $defaults);
1996 $data['data']=$this->httpDecode($data['data']);
1997
1998 //$data['data']=str_replace("\\\"", "\"", $data['data']);
1999 //$data['data']=str_replace("\\\\", "\\", $data['data']);
2000
2001 $result=FALSE;
2002 switch (strtolower($data['dataType'])) {
2003 case 'array':
2004 $result=json_decode($data['data'], TRUE);
2005 break;
2006 case 'class':
2007 if(class_exists($data['dataClass']) && $this->startsWith($data['dataClass'], TCMP_PLUGIN_PREFIX)) {
2008 $result=$this->jsonToClass($data['data'], $data['dataClass']);
2009 } else {
2010 $result=json_decode($data['data'], TRUE);
2011 }
2012 break;
2013 default:
2014 $result=json_decode($data['data']);
2015 break;
2016 }
2017 return $result;
2018 }
2019 public function getConstantsValues($class, $prefix='', $glue=FALSE) {
2020 $array=$this->getConstants($class, $prefix);
2021 $result=array_values($array);
2022 if($glue!==FALSE) {
2023 $result=implode($glue, $result);
2024 }
2025 return $result;
2026
2027 }
2028 public function getValue($array, $index, $default=FALSE) {
2029 $result=$this->getIndex($array, $index, $default);
2030 if($result!==$default) {
2031 $result=$result['v'];
2032 }
2033 return $result;
2034 }
2035 public function getKey($array, $index, $default=FALSE) {
2036 $result=$this->getIndex($array, $index, $default);
2037 if($result!==$default) {
2038 $result=$result['k'];
2039 }
2040 return $result;
2041 }
2042 public function getIndex($array, $index, $default=FALSE) {
2043 $result=$default;
2044 if(is_array($array) && count($array)>0) {
2045 if($this->isAssociativeArray($array)) {
2046 $i=0;
2047 foreach($array as $k=>$v) {
2048 if($index==$i) {
2049 $result=array(
2050 'k'=>$k
2051 , 'v'=>$v
2052 );
2053 break;
2054 }
2055 $i++;
2056 }
2057 } else {
2058 if($index<count($array) && $index>=0) {
2059 $result=$array[$index];
2060 }
2061 }
2062 }
2063 return $result;
2064 }
2065 public function isEmpty($v) {
2066 if(!$v) {
2067 return TRUE;
2068 }
2069
2070 $result=FALSE;
2071 if(is_string($v)) {
2072 $result=($v=='');
2073 } elseif(is_array($v)) {
2074 $result=count($v)==0;
2075 } elseif(is_object($v)) {
2076 $result=TRUE;
2077 foreach($v as $k=>$w) {
2078 if(!is_null($w) && $w!=='') {
2079 $result=FALSE;
2080 break;
2081 }
2082 }
2083 }
2084 return $result;
2085 }
2086 public function httpEncode($v) {
2087 $v=gzcompress($v);
2088 $v=bin2hex($v);
2089 return $v;
2090 }
2091 public function httpDecode($v) {
2092 $v=hex2bin($v);
2093 $v=gzuncompress($v);
2094 return $v;
2095 }
2096 public function trimHttp($uri) {
2097 $uri=str_replace('http://', '', $uri);
2098 $uri=str_replace('https://', '', $uri);
2099 return $uri;
2100 }
2101 function getClientIpAddress() {
2102 $ipaddress='';
2103 if (getenv('HTTP_CLIENT_IP'))
2104 $ipaddress=getenv('HTTP_CLIENT_IP');
2105 else if(getenv('HTTP_X_FORWARDED_FOR'))
2106 $ipaddress=getenv('HTTP_X_FORWARDED_FOR');
2107 else if(getenv('HTTP_X_FORWARDED'))
2108 $ipaddress=getenv('HTTP_X_FORWARDED');
2109 else if(getenv('HTTP_FORWARDED_FOR'))
2110 $ipaddress=getenv('HTTP_FORWARDED_FOR');
2111 else if(getenv('HTTP_FORWARDED'))
2112 $ipaddress=getenv('HTTP_FORWARDED');
2113 else if(getenv('REMOTE_ADDR'))
2114 $ipaddress=getenv('REMOTE_ADDR');
2115 else
2116 $ipaddress='UNKNOWN';
2117 $ipaddress=($ipaddress == '::1') ? '192.168.0.1' : $ipaddress;
2118 return $ipaddress;
2119 }
2120 public function isMail($mail) {
2121 $at=strpos($mail, '@');
2122 $dot=strrpos($mail, '.');
2123 $result=FALSE;
2124 if($at!==FALSE && $dot!==FALSE && $at<$dot) {
2125 $result=TRUE;
2126 }
2127 return $result;
2128 }
2129 public function getNameFromListArray($array, $id, $default=FALSE) {
2130 $result=$default;
2131 foreach($array as $v) {
2132 if($v['id']==$id) {
2133 if(isset($v['text'])) {
2134 $result=$v['text'];
2135 break;
2136 } elseif(isset($v['name'])) {
2137 $result=$v['name'];
2138 break;
2139 }
2140 }
2141 }
2142 return $result;
2143 }
2144 function bqs($name, $default=FALSE) {
2145 $v=$this->qs($name, '');
2146 $result=$default;
2147 if($v!='') {
2148 if(is_numeric($v)) {
2149 $v=intval($v);
2150 $result=($v>0);
2151 } else {
2152 $result=$this->isTrue($v);
2153 }
2154 }
2155 return $result;
2156 }
2157 /**
2158 * Get either a Gravatar URL or complete image tag for a specified email address.
2159 *
2160 * @param string $email The email address
2161 * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
2162 * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
2163 * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
2164 * @param boole $img True to return a complete IMG tag False for just the URL
2165 * @param array $atts Optional, additional key/value attributes to include in the IMG tag
2166 * @return String containing either just a URL or a complete image tag
2167 * @source http://gravatar.com/site/implement/images/php/
2168 */
2169 function getGravatarImage($email, $s=80, $d='mm', $r='g', $img=TRUE, $atts=array()) {
2170 if(!is_array($atts)) {
2171 $atts=array();
2172 }
2173 if(!isset($atts['class'])) {
2174 $atts['class']='gravatar';
2175 }
2176 $url='//www.gravatar.com/avatar/';
2177 $url.=md5(strtolower(trim($email)));
2178 $url.="?s=$s&d=$d&r=$r";
2179 if ($img) {
2180 $url='<img src="'.$url.'"';
2181 foreach ($atts as $key => $val)
2182 $url .= ' '.$key.'="'.$val.'"';
2183 $url .= ' />';
2184 }
2185 return $url;
2186 }
2187 function getGravatarUri($email, $s=80, $d='mm', $r='g', $atts=array()) {
2188 $url=$this->getGravatarImage($email, $s, $d, $r, FALSE, $atts);
2189 return $url;
2190 }
2191 function getFunctionName($function) {
2192 $result=FALSE;
2193 if(is_string($function)) {
2194 $result=$function;
2195 } elseif(is_array($function)) {
2196 $result=$function[1];
2197 }
2198 return $result;
2199 }
2200 function functionExists($function) {
2201 $result=FALSE;
2202 if(is_string($function)) {
2203 $result=function_exists($function);
2204 } elseif(is_array($function)) {
2205 $result=method_exists($function[0], $function[1]);
2206 } elseif(is_callable($function)) {
2207 $result=TRUE;
2208 }
2209 return $result;
2210 }
2211 function functionCall() {
2212 $args=func_get_args();
2213 if($args===FALSE || count($args)==0) {
2214 return;
2215 }
2216
2217 $function=array_shift($args);
2218 $result=NULL;
2219 if($this->functionExists($function)) {
2220 $result=call_user_func_array($function, $args);
2221 }
2222 return $result;
2223 }
2224
2225 function passwordsEquals($p1, $p2) {
2226 if (!function_exists('wp_check_password')) {
2227 require_once(ABSPATH.'wp-includes/pluggable.php');
2228 }
2229 $result=wp_check_password($p1, $p2);
2230 return $result;
2231 }
2232 public function contains($v1, $v2, $ignoreCase=TRUE) {
2233 $result=FALSE;
2234 if($ignoreCase) {
2235 $result=stripos($v1, $v2)!==FALSE;
2236 } else {
2237 $result=strpos($v1, $v2)!==FALSE;
2238 }
2239 return $result;
2240 }
2241 public function getMailTextHtml() {
2242 return "text/html";
2243 }
2244 public function mail($to, $subject, $body, $options=array()) {
2245 $subject='[LeadsBridge] '.$subject;
2246 $defaults=array(
2247 'html'=>TRUE
2248 , 'footer'=>TRUE
2249 , 'headers'=>array()
2250 , 'noreply'=>TRUE
2251 , 'attachments'=>array()
2252 );
2253 $options=$this->parseArgs($options, $defaults);
2254 $to=$this->toArray($to);
2255 if($options['html']) {
2256 add_filter('wp_mail_content_type', array($this, 'getMailTextHtml'));
2257 }
2258 if($options['noreply']) {
2259 $options['headers'][]='From: LeadsBridge <no-reply@leadsbrige.com>';
2260 }
2261 if($options['footer'] && $options['html']) {
2262 $body .= '<br><hr><a href="'.TCMP_WORDPRESS_SITE.'"><img src="'.TCMP_SITE_IMAGES_URI.'logos/logo-mail.png" /></a>';
2263 }
2264 $result=FALSE;
2265 if(!function_exists('wp_mail')) {
2266 include_once('../../../../../../wp-includes/pluggable.php');
2267 }
2268 if(function_exists('wp_mail')) {
2269 if(count($to)>0) {
2270 $result=wp_mail($to, $subject, $body
2271 , $options['headers'], $options['attachments']);
2272 }
2273 }
2274 return $result;
2275 }
2276 /*public function formSubmit($action, $method='POST', $data=array(), $options=array()) {
2277 $defaults=array('json'=>FALSE);
2278 $options=$this->parseArgs($options, $defaults);
2279
2280 $postData=http_build_query($data);
2281 $curl=curl_init();
2282 curl_setopt($curl, CURLOPT_URL, $action);
2283 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
2284 curl_setopt($curl, CURLOPT_POST, strtolower($method)=='post');
2285 curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
2286 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
2287 $result=curl_exec($curl);
2288 curl_close($curl);
2289 if($result!==FALSE && $result!=='' && $options['json']) {
2290 $result=json_decode($result, TRUE);
2291 }
2292 return $result;
2293 }*/
2294
2295 private function getHtmlCode($value) {
2296 $value=str_replace('\"', '', $value);
2297 $value=str_replace('"', '', $value);
2298 return $value;
2299 }
2300 public function parseHtmlForm($html) {
2301 $data=array(
2302 'action'=>''
2303 , 'method'=>'GET'
2304 , 'fields'=>array()
2305 );
2306 $result=FALSE;
2307
2308 $previous=error_reporting();
2309 error_reporting($previous^E_WARNING);
2310
2311 $doc=new DOMDocument();
2312 if ($doc->loadHTML($html)) {
2313 $xpath=new DOMXPath($doc);
2314 $form=$xpath->query('//form');
2315 if ($form->length > 0) {
2316 $result=TRUE;
2317 $data['action']=$this->getHtmlCode($form->item(0)->getAttribute('action'));
2318 $data['method']=$this->getHtmlCode($form->item(0)->getAttribute('method'));
2319 if($data['method']===FALSE || $data['method']=='') {
2320 $data['method']='GET';
2321 }
2322 $data['method']=strtoupper($data['method']);
2323 $inputs=$xpath->query('//input');
2324 foreach ($inputs as $input) {
2325 $name=$this->getHtmlCode($input->getAttribute('name'));
2326 $type=$this->getHtmlCode($input->getAttribute('type'));
2327 $value=$this->getHtmlCode($input->getAttribute('value'));
2328 if($name!='') {
2329 $data['fields'][$name]=array(
2330 'name'=>$name
2331 , 'type'=>$type
2332 , 'value'=>$value
2333 );
2334 }
2335 }
2336 }
2337 }
2338 error_reporting($previous);
2339 return ($result ? $data : FALSE);
2340 }
2341 public function dequeueScripts($array) {
2342 if(!function_exists('wp_scripts') || function_exists('wp_dequeue_script')) {
2343 return;
2344 }
2345
2346 $array=$this->toArray($array);
2347 $scripts=wp_scripts();
2348 /* @var $v _WP_Dependency */
2349 foreach($scripts->registered as $k=>$v) {
2350 foreach($array as $pattern) {
2351 if($this->contains($v->src, $pattern) || $this->contains($v->handle, $pattern)) {
2352 wp_dequeue_script($v->handle);
2353 break;
2354 }
2355 }
2356 }
2357 }
2358 public function dequeueStyles($array) {
2359 if(!function_exists('wp_styles') || function_exists('wp_dequeue_style')) {
2360 return;
2361 }
2362
2363 $array=$this->toArray($array);
2364 $styles=wp_styles();
2365 /* @var $v _WP_Dependency */
2366 foreach($styles->registered as $k=>$v) {
2367 foreach($array as $pattern) {
2368 if($this->contains($v->src, $pattern) || $this->contains($v->handle, $pattern)) {
2369 wp_dequeue_style($v->handle);
2370 break;
2371 }
2372 }
2373 }
2374 }
2375 public function formatSeconds($time) {
2376 if($time==='') {
2377 return '';
2378 }
2379
2380 $time=intval($time);
2381 $seconds=($time%60);
2382 $time=(($time-$seconds)/60);
2383 $minutes=($time%60);
2384 $time=(($time-$minutes)/60);
2385 $hours=($time%24);
2386 $time=(($time-$hours)/24);
2387 $days=$time;
2388
2389 $array=array();
2390 if($seconds>0) {
2391 $array[]=$seconds.'s';
2392 }
2393 if($minutes>0) {
2394 $array[]=$minutes.'m';
2395 }
2396 if($hours>0) {
2397 $array[]=$hours.'h';
2398 }
2399 if($days>0) {
2400 $array[]=$days.'d';
2401 }
2402 $array=array_reverse($array);
2403 $text=implode(' ', $array);
2404 return $text;
2405 }
2406 function logout() {
2407 if (!function_exists('wp_logout')) {
2408 require_once(ABSPATH.'wp-includes/pluggable.php');
2409 }
2410 wp_logout();
2411 return TRUE;
2412 }
2413 function formatPercentage($value, $options=array()) {
2414 if(is_bool($options)) {
2415 $options=array('symbol'=>$options);
2416 }
2417 $defaults=array('symbol'=>TRUE);
2418 $options=$this->parseArgs($options, $defaults);
2419
2420 $value=floatval($value);
2421 $value=round($value, 3);
2422 $value=number_format($value, 3, ',', '');
2423 if($options['symbol']) {
2424 $value.=' %';
2425 }
2426 return $value;
2427 }
2428 function formatCurrencyMoney($value, $options=array()) {
2429 $defaults=array('currency'=>$this->getDefaultCurrencySymbol());
2430 $options=$this->parseArgs($options, $defaults);
2431
2432 $value=$this->formatMoney($value, $options);
2433 return $value;
2434 }
2435 function formatMoney($value, $options=array()) {
2436 if(is_string($options)) {
2437 $options=array('currency'=>$options);
2438 }
2439 $defaults=array('currency'=>FALSE);
2440 $options=$this->parseArgs($options, $defaults);
2441
2442 $value=floatval($value);
2443 $value=round($value, 3);
2444 $value=number_format($value, 3, ',', '.');
2445 if($options['currency']!='') {
2446 $symbol=$options['currency'];
2447 if(strlen($symbol)>1) {
2448 $symbol=$this->getCurrencySymbol($symbol);
2449 }
2450 $value.=' '.$symbol;
2451 }
2452 return $value;
2453 }
2454 function sortOptions(&$options) {
2455 if(!is_array($options)) {
2456 return $options;
2457 }
2458
2459 usort($options, array($this, 'sortOptions_Compare'));
2460 return $options;
2461 }
2462 public function sortOptions_Compare($o1, $o2) {
2463 global $tcmp;
2464 $v1=$tcmp->Utils->get($o1, 'text', FALSE);
2465 if($v1===FALSE) {
2466 $v1=$tcmp->Utils->get($o1, 'name', FALSE);
2467 }
2468 $v2=$tcmp->Utils->get($o2, 'text', FALSE);
2469 if($v2===FALSE) {
2470 $v2=$tcmp->Utils->get($o2, 'name', FALSE);
2471 }
2472
2473 //to order properly
2474 if($tcmp->Utils->startsWith($v1, '[')) {
2475 $v1=' '.$v1;
2476 }
2477 if($tcmp->Utils->startsWith($v2, '[')) {
2478 $v2=' '.$v2;
2479 }
2480 return strcasecmp($v1, $v2);
2481 }
2482 public function getVisitorIpAddress() {
2483 $ip='';
2484 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
2485 $ip=$_SERVER['HTTP_CLIENT_IP'];
2486 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
2487 $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
2488 } else {
2489 $ip=$_SERVER['REMOTE_ADDR'];
2490 }
2491 return $ip;
2492 }
2493 public function getVisitorUserAgent() {
2494 $result='';
2495 if(isset($_SERVER['HTTP_USER_AGENT'])) {
2496 $result=$_SERVER['HTTP_USER_AGENT'];
2497 }
2498 return $result;
2499 }
2500 public function cURL($uri, $method, $options=array()) {
2501 global $tcmp;
2502 $defaults=array(
2503 'body'=>''
2504 , 'agent'=>''
2505 , 'timeout'=>5
2506 , 'header'=>array()
2507 );
2508 $options=$this->parseArgs($options, $defaults);
2509
2510 if($this->startsWith($uri, '//')) {
2511 $uri='https:'.$uri;
2512 }
2513
2514 try {
2515 $curl=curl_init();
2516 if($method=='') {
2517 $method='GET';
2518 }
2519 $method=strtoupper($method);
2520 switch($method) {
2521 case 'GET':
2522 if(is_array($options['body'])) {
2523 $uri=$this->addQueryString($options['body'], $uri);
2524 }
2525 break;
2526 case 'POST':
2527 if(is_array($options['body'])) {
2528 curl_setopt($curl, CURLOPT_POST, TRUE);
2529 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($options['body']));
2530 }
2531 break;
2532 case 'PUT':
2533 case 'DELETE':
2534 default:
2535 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
2536 if(is_array($options['body'])) {
2537 curl_setopt($curl, CURLOPT_POST, TRUE);
2538 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($options['body']));
2539 }
2540 break;
2541 }
2542
2543 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
2544 $header=$options['header'];
2545 if(is_array($header) && count($header)>0) {
2546 $data=array();
2547 foreach($header as $k=>$v) {
2548 if(is_numeric($k)) {
2549 $data[]=$v;
2550 } else {
2551 $data[]=$k.': '.$v;
2552 }
2553 }
2554 curl_setopt($curl, CURLOPT_HEADER, TRUE);
2555 curl_setopt($curl, CURLOPT_HTTPHEADER, $data);
2556 }
2557 curl_setopt($curl, CURLOPT_URL, $uri);
2558 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
2559 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
2560 curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
2561
2562 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, intval($options['timeout']));
2563 if($options['agent']!=='' && $options['agent']!==FALSE) {
2564 curl_setopt($curl, CURLOPT_USERAGENT, $options['agent']);
2565 }
2566
2567 $response=curl_exec($curl);
2568 $info=curl_getinfo($curl);
2569
2570 $errorCode=intval(curl_errno($curl));
2571 $errorDescription=curl_error($curl);
2572 if($errorCode==0 && isset($info['http_code'])) {
2573 $info['http_code']=intval($info['http_code']);
2574 if($info['http_code']!=200 && $info['http_code']!=201) {
2575 $errorCode=$info['http_code'];
2576 if($errorDescription=='') {
2577 $errorDescription=$response;
2578 }
2579 } elseif(is_string($response) && $this->startsWith($response, 'No site configured at this address')) {
2580 $errorCode=-1;
2581 if($errorDescription=='') {
2582 $errorDescription=$response;
2583 }
2584 }
2585 }
2586 if($errorCode!=0) {
2587 $result=new WP_Error($errorCode, $errorDescription);
2588 } else {
2589 if(is_array($header) && count($header)>0) {
2590 $text=trim(substr($response, 0, $info['header_size']));
2591 $result=substr($response, $info['header_size']);
2592 } else {
2593 $result=$response;
2594 }
2595 }
2596 curl_close($curl);
2597 } catch(Exception $ex) {
2598 $result=new WP_Error($ex->getCode(), $ex->getMessage());
2599 }
2600 return $result;
2601 }
2602 public function toEmailsArray($data) {
2603 if(!is_array($data)) {
2604 $data=str_replace(',', '|', $data);
2605 $data=str_replace(';', '|', $data);
2606 $data=str_replace(' ', '|', $data);
2607 $data=$this->toArray($data);
2608 }
2609
2610 $result=array();
2611 foreach($data as $v) {
2612 $v=trim($v);
2613 if(function_exists('is_email')) {
2614 if(!is_email($v)) {
2615 $v='';
2616 }
2617 } elseif(!filter_var($v, FILTER_VALIDATE_EMAIL)) {
2618 $v='';
2619 }
2620 if($v!='') {
2621 $result[$v]=$v;
2622 }
2623 }
2624 $result=array_keys($result);
2625 return $result;
2626 }
2627 function getCustomFields($fields) {
2628 $items=$this->toArray($fields);
2629 $result=array();
2630 foreach($items as $v) {
2631 $name=str_replace('_', ' ', $v);
2632 $name=str_replace('-', ' ', $name);
2633 $name=str_replace('$', '', $name);
2634 $name=ucwords($name);
2635 $result[$v]=array('name'=>$name, 'format'=>'text');
2636 }
2637 return $result;
2638 }
2639 function getCurrencySymbol($currency) {
2640 // Create a NumberFormatter
2641 $locale='en_US';
2642 $formatter=new NumberFormatter($locale, NumberFormatter::CURRENCY);
2643
2644 // Figure out what 0.00 looks like with the currency symbol
2645 $withCurrency=$formatter->formatCurrency(0, $currency);
2646
2647 // Figure out what 0.00 looks like without the currency symbol
2648 $formatter->setPattern(str_replace('¤', '', $formatter->getPattern()));
2649 $withoutCurrency=$formatter->formatCurrency(0, $currency);
2650
2651 // Extract just the currency symbol from the first string
2652 return str_replace($withoutCurrency, '', $withCurrency);
2653 }
2654 function encodeUri($string) {
2655 $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%23', '%5B', '%5D');
2656 $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "#", "[", "]");
2657 $result=urlencode($string);
2658 //$result=str_replace($replacements, $entities, $result);
2659 return $result;
2660 }
2661 function addQueryString($args, $uri) {
2662 if(!is_array($args) || count($args)==0) {
2663 return $uri;
2664 }
2665
2666 $params=array();
2667 foreach($args as $k=>$v) {
2668 $params[]=$k.'='.$this->encodeUri($v);
2669 }
2670 $params=implode('&', $params);
2671 if($this->contains($uri, '?')) {
2672 $uri.='&'.$params;
2673 } else {
2674 $uri.='?'.$params;
2675 }
2676 return $uri;
2677 }
2678 function arrayCopy($fromArray, &$toArray, $options=array()) {
2679 $defaults=array(
2680 'matchFields'=>array()
2681 , 'otherFields'=>TRUE
2682 , 'key'=>''
2683 );
2684 $options=$this->parseArgs($options, $defaults);
2685
2686 foreach($fromArray as $i=>$item) {
2687 $new=array();
2688 foreach($options['matchFields'] as $k=>$t) {
2689 if(is_bool($k)) {
2690 $v=$i;
2691 } else {
2692 $v=$this->get($item, $k, '');
2693 unset($item[$k]);
2694 }
2695 $new[$t]=$v;
2696 }
2697
2698 if($options['otherFields']) {
2699 foreach($item as $k=>$v) {
2700 $new[$k]=$v;
2701 }
2702 }
2703
2704 $k=$options['key'];
2705 if($k=='') {
2706 $toArray[]=$new;
2707 } else {
2708 $k=$this->get($new, $k, '');
2709 if($k!='') {
2710 $toArray[$k]=$new;
2711 }
2712 }
2713
2714 }
2715 return $toArray;
2716 }
2717 public function formatTimer($time) {
2718 if(!is_int($time)) {
2719 if(is_string($time)) {
2720 $time=str_replace(' ', ':', $time);
2721 $time=str_replace('.', ':', $time);
2722 $time=str_replace('/', ':', $time);
2723 $time=explode(':', $time);
2724
2725 $length=count($time);
2726 $days=0;
2727 $hours=0;
2728 $minutes=0;
2729 $secs=intval($time[$length-1]);
2730
2731 if($length>1) {
2732 $minutes=intval($time[$length-2]);
2733 if($length>2) {
2734 $hours=intval($time[$length-3]);
2735 if($length>3) {
2736 $days=intval($time[$length-4]);
2737 }
2738 }
2739 }
2740 $time=$days*86400+$hours*3600+$minutes*60+$secs;
2741 } else {
2742 $time=0;
2743 }
2744 } else {
2745 $time=intval($time);
2746 }
2747
2748 $secs=$time%60;
2749 $time=($time-$secs)/60;
2750 $minutes=$time%60;
2751 $time=($time-$minutes)/60;
2752 $hours=$time%24;
2753 $days=($time-$hours)/24;
2754
2755 $result=array();
2756 $result[]=$days;
2757 $result[]=($hours<10 ? '0' : '').$hours;
2758 $result[]=($minutes<10 ? '0' : '').$minutes;
2759 $result[]=($secs<10 ? '0' : '').$secs;
2760 $result=implode(':', $result);
2761 return $result;
2762 }
2763 public function parseTimer($time) {
2764 $time=$this->formatTimer($time);
2765 $time=explode(':', $time);
2766 $result=intval($time[0])*86400+intval($time[1])*3600+intval($time[2])*60+intval($time[3]);
2767 return $result;
2768 }
2769 public function isTax($taxonomy='', $term='') {
2770 $result=FALSE;
2771 switch ($taxonomy) {
2772 case 'category':
2773 $result=is_category($term);
2774 break;
2775 case 'tag':
2776 $result=is_tag($term);
2777 break;
2778 default:
2779 $result=is_tax($taxonomy, $term);
2780 break;
2781 }
2782 return $result;
2783 }
2784 }
2785