forked from brandonwamboldt/utilphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.php
executable file
·2202 lines (1989 loc) · 87.5 KB
/
util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* util.php
*
* util.php is a library of helper functions for common tasks such as
* formatting bytes as a string or displaying a date in terms of how long ago
* it was in human readable terms (E.g. 4 minutes ago). The library is entirely
* contained within a single file and hosts no dependencies. The library is
* designed to avoid any possible conflicts.
*
* @author Brandon Wamboldt
* @link http://github.com/brandonwamboldt/utilphp/ Official Documentation
* @version 1.0.000
*/
declare(encoding='UTF-8');
if ( ! class_exists( 'util' ) ) {
class util
{
/**
* A constant representing the number of seconds in a minute, for
* making code more verbose
*
* @since 1.0.000
* @var int
*/
const SECONDS_IN_A_MINUTE = 60;
/**
* A constant representing the number of seconds in an hour, for making
* code more verbose
*
* @since 1.0.000
* @var int
*/
const SECONDS_IN_A_HOUR = 3600;
const SECONDS_IN_AN_HOUR = 3600;
/**
* A constant representing the number of seconds in a day, for making
* code more verbose
*
* @since 1.0.000
* @var int
*/
const SECONDS_IN_A_DAY = 86400;
/**
* A constant representing the number of seconds in a week, for making
* code more verbose
*
* @since 1.0.000
* @var int
*/
const SECONDS_IN_A_WEEK = 604800;
/**
* A constant representing the number of seconds in a month (30 days),
* for making code more verbose
*
* @since 1.0.000
* @var int
*/
const SECONDS_IN_A_MONTH = 2592000;
/**
* A constant representing the number of seconds in a year (365 days),
* for making code more verbose
*
* @since 1.0.000
* @var int
*/
const SECONDS_IN_A_YEAR = 31536000;
/**
* A collapse icon, using in the dump_var function to allow collapsing
* an array or object
*
* @access public
* @since 1.0.000
* @static
* @var string
*/
public static $icon_collapse = 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAMAAADXT/YiAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2RpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo3MjlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpFNzFDNDQyNEMyQzkxMUUxOTU4MEM4M0UxRDA0MUVGNSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpFNzFDNDQyM0MyQzkxMUUxOTU4MEM4M0UxRDA0MUVGNSIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IFdpbmRvd3MiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo3NDlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo3MjlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PuF4AWkAAAA2UExURU9t2DBStczM/1h16DNmzHiW7iNFrypMvrnD52yJ4ezs7Onp6ejo6P///+Tk5GSG7D9h5SRGq0Q2K74AAAA/SURBVHjaLMhZDsAgDANRY3ZISnP/y1ZWeV+jAeuRSky6cKL4ryDdSggP8UC7r6GvR1YHxjazPQDmVzI/AQYAnFQDdVSJ80EAAAAASUVORK5CYII=';
/**
* A collapse icon, using in the dump_var function to allow collapsing
* an array or object
*
* @access public
* @since 1.0.000
* @static
* @var string
*/
public static $icon_expand = 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAMAAADXT/YiAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2RpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo3MTlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpFQzZERTJDNEMyQzkxMUUxODRCQzgyRUNDMzZEQkZFQiIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpFQzZERTJDM0MyQzkxMUUxODRCQzgyRUNDMzZEQkZFQiIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IFdpbmRvd3MiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo3MzlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo3MTlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PkmDvWIAAABIUExURU9t2MzM/3iW7ubm59/f5urq85mZzOvr6////9ra38zMzObm5rfB8FZz5myJ4SNFrypMvjBStTNmzOvr+mSG7OXl8T9h5SRGq/OfqCEAAABKSURBVHjaFMlbEoAwCEPRULXF2jdW9r9T4czcyUdA4XWB0IgdNSybxU9amMzHzDlPKKu7Fd1e6+wY195jW0ARYZECxPq5Gn8BBgCr0gQmxpjKAwAAAABJRU5ErkJggg==';
/**
* Retrieve a value from the $_POST array, or return a given default if
* the index isn't set
*
* The first n parameters represent the fields to retrieve, being a
* single index or an array of indexes to access multi-level arrays.
*
* Calling the function as util::post_var( ['tags', '1412'] ) is
* identical to using $_POST['tags']['1412'].
*
* @param string $fields The name of the field to retrieve
* @param mixed $default A default value to return if the
* requested variable isn't set
* @return mixed
*
* @see array_get()
*
* @access public
* @since 1.0.000
* @static
*/
public static function post_var( $fields, $default = NULL )
{
return self::array_get( $_POST, $fields, $default );
}
/**
* Retrieve a value from the $_GET array, or return a given default if
* the index isn't set
*
* The first n parameters represent the fields to retrieve, being a
* single index or an array of indexes to access multi-level arrays.
*
* Calling the function as util::get_var( ['tags', '1412'] ) is
* identical to using $_GET['tags']['1412'].
*
* @param string $fields The name of the field to retrieve
* @param mixed $default A default value to return if the
* requested variable isn't set
* @return mixed
*
* @see array_get()
*
* @access public
* @since 1.0.000
* @static
*/
public static function get_var( $fields, $default = NULL )
{
return self::array_get( $_GET, $fields, $default );
}
/**
* Retrieve a value from the $_GET or the $_POST array, or return a
* given default if the index isn't set. You may expect this function
* to check the $_REQUEST variable, but the desired behavior is often
* to check $_GET or $_POST. To avoid screwing stuff up if $_COOKIE is
* set, and to avoid relying on the user to set the request_order
* option, we just make that assumption for them.
*
* The first n parameters represent the fields to retrieve, being a
* single index or an array of indexes to access multi-level arrays.
*
* Calling the function as util::request_var( ['tags', '1412'] ) is
* identical to using $_REQUEST['tags']['1412'].
*
* @param string $fields The name of the field to retrieve
* @param mixed $default A default value to return if the requested variable isn't set
* @return mixed
*
* @see array_get()
*
* @access public
* @since 1.0.000
* @static
*/
public static function request_var( $fields, $default = NULL )
{
if ( strstr( ini_get( 'request_order' ), 'GP' ) ) {
return self::array_get( array_merge( $_POST, $_GET ), $fields, $default );
} else {
return self::array_get( array_merge( $_GET, $_POST ), $fields, $default );
}
}
/**
* Retrieve a value from the $_SESSION array, or return a given default
* if the index isn't set
*
* The first n parameters represent the fields to retrieve, being a
* single index or an array of indexes to access multi-level arrays.
*
* Calling the function as util::session_var( ['tags', '1412'] ) is
* identical to using $_SESSION['tags']['1412'].
*
* @param string $fields The name of the field to retrieve
* @param mixed $default A default value to return if the
* requested variable isn't set
* @return mixed
*
* @see array_get()
*
* @access public
* @since 1.0.000
* @static
*/
public static function session_var( $fields, $default = NULL )
{
return self::array_get( $_SESSION, $fields, $default );
}
/**
* Retrieve a value from the $_COOKIE array, or return a given default
* if the index isn't set
*
* The first n parameters represent the fields to retrieve, being a
* single index or an array of indexes to access multi-level arrays.
*
* Calling the function as util::cookie_var( ['tags', '1412'] ) is
* identical to using $_COOKIE['tags']['1412'].
*
* @param string $fields The name of the field to retrieve
* @param mixed $default A default value to return if the
* requested variable isn't set
* @return mixed
*
* @see array_get()
*
* @access public
* @since 1.0.000
* @static
*/
public static function cookie_var( $fields, $default = NULL )
{
return self::array_get( $_COOKIE, $fields, $default );
}
/**
* Access an array index, retrieving the value stored there if it
* exists or a default if it does not. This function allows you to
* concisely access an index which may or may not exist without
* raising a warning
*
* @param array $var Array to access
* @param string $field Index to access in the array
* @param mixed $default Default value to return if the key is not
* present in the array
* @return mixed
*
* @access public
* @since 1.0.000
* @static
*/
public static function array_get( array $array, $fields, $default = NULL )
{
if ( ! is_array( $array ) ) {
return $default;
} else if ( ! is_array( $fields ) ) {
if ( isset( $array[$fields] ) ) {
return $array[$fields];
} else {
return $default;
}
} else {
foreach ( $fields as $field ) {
$found_it = FALSE;
if ( ! is_array( $array ) ) {
break;
}
foreach ( $array as $key => $value ) {
if ( $key == $field ) {
$found_it = TRUE;
$array = $value;
break;
}
}
}
if ( $found_it ) {
return $array;
} else {
return $default;
}
}
}
/**
* Display a variable's contents using nice HTML formatting and will
* properly display the value of booleans as true or false
*
* @param mixed $var The variable to dump
* @return string
*
* @see var_dump_plain()
*
* @access public
* @since 1.0.000
* @static
*/
public static function var_dump( $var, $return = FALSE )
{
$html = '<pre style="margin-bottom: 18px;' .
'background: #f7f7f9;' .
'border: 1px solid #e1e1e8;' .
'padding: 8px;' .
'border-radius: 4px;' .
'-moz-border-radius: 4px;' .
'-webkit-border radius: 4px;' .
'display: block;' .
'font-size: 12.05px;' .
'white-space: pre-wrap;' .
'word-wrap: break-word;' .
'color: #333;' .
'font-family: Menlo,Monaco,Consolas,\'Courier New\',monospace;">';
$html .= self::var_dump_plain( $var );
$html .= '</pre>';
if ( ! $return ) {
echo $html;
} else {
return $html;
}
}
/**
* Display a variable's contents using nice HTML formatting (Without
* the <pre> tag) and will properly display the values of variables
* like booleans and resources. Supports collapsable arrays and objects
* as well.
*
* @param mixed $var The variable to dump
* @return string
*
* @access public
* @since 1.0.000
* @static
*/
public static function var_dump_plain( $var )
{
$html = '';
if ( is_bool( $var ) ) {
$html .= '<span style="color:#588bff;">bool</span><span style="color:#999;">(</span><strong>' . ( ( $var ) ? 'true' : 'false' ) . '</strong><span style="color:#999;">)</span>';
} else if ( is_int( $var ) ) {
$html .= '<span style="color:#588bff;">int</span><span style="color:#999;">(</span><strong>' . $var . '</strong><span style="color:#999;">)</span>';
} else if ( is_float( $var ) ) {
$html .= '<span style="color:#588bff;">float</span><span style="color:#999;">(</span><strong>' . $var . '</strong><span style="color:#999;">)</span>';
} else if ( is_string( $var ) ) {
$html .= '<span style="color:#588bff;">string</span><span style="color:#999;">(</span>' . strlen( $var ) . '<span style="color:#999;">)</span> <strong>"' . self::htmlentities( $var ) . '"</strong>';
} else if ( is_null( $var ) ) {
$html .= '<strong>NULL</strong>';
} else if ( is_resource( $var ) ) {
$html .= '<span style="color:#588bff;">resource</span>("' . get_resource_type( $var ) . '") <strong>"' . $var . '"</strong>';
} else if ( is_array( $var ) ) {
$uuid = 'include-php-' . uniqid();
$html .= '<span style="color:#588bff;">array</span>(' . count( $var ) . ')';
if ( ! empty( $var ) ) {
$html .= ' <img id="' . $uuid . '" data-expand="data:image/png;base64,' . self::$icon_expand . '" style="position:relative;left:-5px;top:-1px;cursor:pointer;" src="data:image/png;base64,' . self::$icon_collapse . '" /><br /><span id="' . $uuid . '-collapsable">[<br />';
$indent = 4;
$longest_key = 0;
foreach( $var as $key => $value ) {
if ( is_string( $key ) ) {
$longest_key = max( $longest_key, strlen( $key ) + 2 );
} else {
$longest_key = max( $longest_key, strlen( $key ) );
}
}
foreach ( $var as $key => $value ) {
if ( is_numeric( $key ) ) {
$html .= str_repeat( ' ', $indent ) . str_pad( $key, $longest_key, ' ');
} else {
$html .= str_repeat( ' ', $indent ) . str_pad( '"' . self::htmlentities( $key ) . '"', $longest_key, ' ' );
}
$html .= ' => ';
$value = explode( '<br />', self::var_dump_plain( $value ) );
foreach ( $value as $line => $val ) {
if ( $line != 0 ) {
$value[$line] = str_repeat( ' ', $indent * 2 ) . $val;
}
}
$html .= implode( '<br />', $value ) . '<br />';
}
$html .= ']</span>';
$html .= preg_replace( '/ +/', ' ', '<script type="text/javascript">(function() {
var img = document.getElementById("' . $uuid . '");
img.onclick = function() {
if ( document.getElementById("' . $uuid . '-collapsable").style.display == "none" ) {
document.getElementById("' . $uuid . '-collapsable").style.display = "inline";
img.src = img.getAttribute("data-collapse");
var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
while ( previousSibling != null && ( previousSibling.nodeType != 1 || previousSibling.tagName.toLowerCase() != "br" ) ) {
previousSibling = previousSibling.previousSibling;
}
if ( previousSibling != null && previousSibling.tagName.toLowerCase() == "br" ) {
previousSibling.style.display = "inline";
}
} else {
document.getElementById("' . $uuid . '-collapsable").style.display = "none";
img.setAttribute( "data-collapse", img.getAttribute("src") );
img.src = img.getAttribute("data-expand");
var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
while ( previousSibling != null && ( previousSibling.nodeType != 1 || previousSibling.tagName.toLowerCase() != "br" ) ) {
previousSibling = previousSibling.previousSibling;
}
if ( previousSibling != null && previousSibling.tagName.toLowerCase() == "br" ) {
previousSibling.style.display = "none";
}
}
};
})();
</script>' );
}
} else if ( is_object( $var ) ) {
$uuid = 'include-php-' . uniqid();
$html .= '<span style="color:#588bff;">object</span>(' . get_class( $var ) . ') <img id="' . $uuid . '" data-expand="data:image/png;base64,' . self::$icon_expand . '" style="position:relative;left:-5px;top:-1px;cursor:pointer;" src="data:image/png;base64,' . self::$icon_collapse . '" /><br /><span id="' . $uuid . '-collapsable">[<br />';
$original = $var;
$var = (array) $var;
$indent = 4;
$longest_key = 0;
foreach( $var as $key => $value ) {
if ( substr( $key, 0, 2 ) == "\0*" ) {
unset( $var[$key] );
$key = 'protected:' . substr( $key, 2 );
$var[$key] = $value;
} else if ( substr( $key, 0, 1 ) == "\0" ) {
unset( $var[$key] );
$key = 'private:' . substr( $key, 1, strpos( substr( $key, 1 ), "\0" ) ) . ':' . substr( $key, strpos( substr( $key, 1 ), "\0" ) + 1 );
$var[$key] = $value;
}
if ( is_string( $key ) ) {
$longest_key = max( $longest_key, strlen( $key ) + 2 );
} else {
$longest_key = max( $longest_key, strlen( $key ) );
}
}
foreach ( $var as $key => $value ) {
if ( is_numeric( $key ) ) {
$html .= str_repeat( ' ', $indent ) . str_pad( $key, $longest_key, ' ');
} else {
$html .= str_repeat( ' ', $indent ) . str_pad( '"' . self::htmlentities( $key ) . '"', $longest_key, ' ' );
}
$html .= ' => ';
$value = explode( '<br />', self::var_dump_plain( $value ) );
foreach ( $value as $line => $val ) {
if ( $line != 0 ) {
$value[$line] = str_repeat( ' ', $indent * 2 ) . $val;
}
}
$html .= implode( '<br />', $value ) . '<br />';
}
$html .= ']</span>';
$html .= preg_replace( '/ +/', ' ', '<script type="text/javascript">(function() {
var img = document.getElementById("' . $uuid . '");
img.onclick = function() {
if ( document.getElementById("' . $uuid . '-collapsable").style.display == "none" ) {
document.getElementById("' . $uuid . '-collapsable").style.display = "inline";
img.src = img.getAttribute("data-collapse");
var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
while ( previousSibling != null && ( previousSibling.nodeType != 1 || previousSibling.tagName.toLowerCase() != "br" ) ) {
previousSibling = previousSibling.previousSibling;
}
if ( previousSibling != null && previousSibling.tagName.toLowerCase() == "br" ) {
previousSibling.style.display = "inline";
}
} else {
document.getElementById("' . $uuid . '-collapsable").style.display = "none";
img.setAttribute( "data-collapse", img.getAttribute("src") );
img.src = img.getAttribute("data-expand");
var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
while ( previousSibling != null && ( previousSibling.nodeType != 1 || previousSibling.tagName.toLowerCase() != "br" ) ) {
previousSibling = previousSibling.previousSibling;
}
if ( previousSibling != null && previousSibling.tagName.toLowerCase() == "br" ) {
previousSibling.style.display = "none";
}
}
};
})();
</script>' );
}
return $html;
}
/**
* Converts any accent characters to their equivalent normal characters
* and converts any other non-alphanumeric characters to dashes, then
* converts any sequence of two or more dashes to a single dash. This
* function generates slugs safe for use as URLs, and if you pass TRUE
* as the second parameter, it will create strings safe for use as CSS
* classes or IDs
*
* @param string $string A string to convert to a slug
* @param bool $css_mode Whether or not to generate strings safe
* for CSS classes/IDs (Default to false)
* @return string
*
* @access public
* @since 1.0.000
* @static
*/
public static function slugify( $string, $css_mode = FALSE )
{
$slug = preg_replace( '/([^A-Za-z0-9\-]+)/', '-', strtolower( self::remove_accents( $string ) ) );
$slug = preg_replace( '/(\-+)/', '-', $slug );
if ( $css_mode ) {
$digits = array( 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' );
if ( is_numeric( substr( $slug, 0, 1 ) ) ) {
$slug = $digits[substr( $slug, 0, 1 )] . substr( $slug, 1 );
}
}
return $slug;
}
/**
* Converts a string to UTF-8 without the need to specify the source
* encoding
*
* @param string $string A string that may or may not be UTF-8
* @return string
*
* @link https://github.com/facebook/libphutil/blob/master/src/utils/utf8.php
*
* @access public
* @since 1.0.000
* @static
*/
public static function str_to_utf8( $string )
{
// Don't re-encode a UTF-8 string since that will mess it up
if ( self::seems_utf8( $string ) ) {
return $string;
} else {
// There is no function to do this in iconv, mbstring or ICU to
// do this, so do it (very very slowly) in pure PHP.
$result = array();
$regex = "/([\x01-\x7F]" .
"|[\xC2-\xDF][\x80-\xBF]" .
"|[\xE0-\xEF][\x80-\xBF][\x80-\xBF]" .
"|[\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF])" .
"|(.)/";
$offset = 0;
$matches = NULL;
while ( preg_match( $regex, $string, $matches, 0, $offset ) ) {
if ( ! isset( $matches[2] ) ) {
$result[] = $matches[1];
} else {
// Unicode replacement character, U+FFFD.
$result[] = "\xEF\xBF\xBD";
}
$offset += strlen( $matches[0] );
}
return implode( '', $result );
}
}
/**
* Checks to see if a string is utf8 encoded.
*
* NOTE: This function checks for 5-Byte sequences, UTF8
* has Bytes Sequences with a maximum length of 4.
*
* @param string $string The string to be checked
* @return bool
*
* @link https://github.com/facebook/libphutil/blob/master/src/utils/utf8.php
*
* @access public
* @author bmorel@ssi.fr
* @since 1.0.000
* @static
*/
public static function seems_utf8( $string )
{
if ( function_exists( 'mb_check_encoding' ) ) {
// If mbstring is available, this is significantly faster than
// using PHP regexps.
return mb_check_encoding( $string, 'UTF-8' );
}
$regex = "/^(" .
"[\x01-\x7F]+" .
"|([\xC2-\xDF][\x80-\xBF])" .
"|([\xE0-\xEF][\x80-\xBF][\x80-\xBF])" .
"|([\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF]))*\$/";
return preg_match( $regex, $string );
}
/**
* Nice formatting for computer sizes (Bytes)
*
* @param int $bytes The number in bytes to format
* @param int $decimals The number of decimal points to include
* @return string
*
* @access public
* @since 1.0.000
* @static
*/
public static function size_format( $bytes, $decimals = 0 )
{
$bytes = floatval( $bytes );
if ( $bytes < 1024 ) {
return $bytes . ' B';
} else if ( $bytes < pow( 1024, 2 ) ) {
return number_format( $bytes / 1024, $decimals, '.', '' ) . ' KiB';
} else if ( $bytes < pow( 1024, 3 ) ) {
return number_format( $bytes / pow( 1024, 2 ), $decimals, '.', '' ) . ' MiB';
} else if ( $bytes < pow( 1024, 4 ) ) {
return number_format( $bytes / pow( 1024, 3 ), $decimals, '.', '' ) . ' GiB';
} else if ( $bytes < pow( 1024, 5 ) ) {
return number_format( $bytes / pow( 1024, 4 ), $decimals, '.', '' ) . ' TiB';
} else if ( $bytes < pow( 1024, 6 ) ) {
return number_format( $bytes / pow( 1024, 5 ), $decimals, '.', '' ) . ' PiB';
} else {
return number_format( $bytes / pow( 1024, 5 ), $decimals, '.', '' ) . ' PiB';
}
}
/**
* Serialize data, if needed.
*
* @param mixed $data Data that might need to be serialized
* @return mixed
*
* @link http://codex.wordpress.org/Function_Reference/maybe_serialize
*
* @access public
* @since 1.0.000
* @static
*/
public static function maybe_serialize( $data )
{
if ( is_array( $data ) || is_object( $data ) ) {
return serialize( $data );
}
return $data;
}
/**
* Unserialize value only if it is serialized
*
* @param string $data A variable that may or may not be serialized
* @return mixed
*
* @link http://codex.wordpress.org/Function_Reference/maybe_unserialize
*
* @access public
* @since 1.0.000
* @static
*/
public static function maybe_unserialize( $data )
{
// Don't attempt to unserialize data that isn't serialized
if ( self::is_serialized( $data ) ) {
return @unserialize( $data );
}
return $data;
}
/**
* Check value to find if it was serialized.
*
* If $data is not an string, then returned value will always be false.
* Serialized data is always a string.
*
* @param mixed $data Value to check to see if was serialized
* @return bool
*
* @link http://codex.wordpress.org/Function_Reference/is_serialized
*
* @access public
* @since 1.0.000
* @static
*/
public static function is_serialized( $data )
{
// If it isn't a string, it isn't serialized
if ( ! is_string( $data ) ) {
return FALSE;
}
$data = trim( $data );
if ( 'N;' == $data ) {
return TRUE;
}
$length = strlen( $data );
if ( $length < 4 ) {
return FALSE;
}
if ( ':' !== $data[1] ) {
return FALSE;
}
$lastc = $data[$length - 1];
if ( ';' !== $lastc && '}' !== $lastc ) {
return FALSE;
}
$token = $data[0];
switch ( $token ) {
case 's' :
if ( '"' !== $data[$length-2] ) {
return FALSE;
}
case 'a' :
case 'O' :
return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
case 'b' :
case 'i' :
case 'd' :
return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data );
}
return FALSE;
}
/**
* Checks to see if the page is being server over SSL or not
*
* @return bool
*
* @access public
* @since 1.0.000
* @static
*/
public static function is_https()
{
if ( isset( $_SERVER['HTTPS'] ) && ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) {
return TRUE;
} else if ( isset( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] == 443 ) {
return TRUE;
} else {
return FALSE;
}
}
/**
* Retrieve a modified URL query string.
*
* You can rebuild the URL and append a new query variable to the URL
* query by using this function. You can also retrieve the full URL
* with query data.
*
* Adding a single key & value or an associative array. Setting a key
* value to an empty string removes the key. Omitting oldquery_or_uri
* uses the $_SERVER value. Additional values provided are expected
* to be encoded appropriately with urlencode() or rawurlencode().
*
* @param mixed $newkey Either newkey or an associative
* array
* @param mixed $newvalue Either newvalue or oldquery or uri
* @param mixed $oldquery_or_uri Optionally the old query or uri
* @return string
*
* @link http://codex.wordpress.org/Function_Reference/add_query_arg
*
* @access public
* @since 1.0.000
* @static
*/
public static function add_query_arg()
{
$ret = '';
// Was an associative array of key => value pairs passed?
if ( is_array( func_get_arg( 0 ) ) ) {
// Was the URL passed as an argument?
if ( func_num_args() == 2 && func_get_arg( 1 ) ) {
$uri = func_get_arg( 1 );
} else if ( func_num_args() == 3 && func_get_arg( 2 ) ) {
$uri = func_get_arg( 2 );
} else {
$uri = $_SERVER['REQUEST_URI'];
}
} else {
// Was the URL passed as an argument?
if ( func_num_args() == 3 && func_get_arg( 2 ) ) {
$uri = func_get_arg( 2 );
} else {
$uri = $_SERVER['REQUEST_URI'];
}
}
// Does the URI contain a fragment section (The part after the #)
if ( $frag = strstr( $uri, '#' ) ) {
$uri = substr( $uri, 0, -strlen( $frag ) );
} else {
$frag = '';
}
// Get the URI protocol if possible
if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
$protocol = $matches[0];
$uri = substr( $uri, strlen( $protocol ) );
} else {
$protocol = '';
}
// Does the URI contain a query string?
if ( strpos( $uri, '?' ) !== FALSE ) {
$parts = explode( '?', $uri, 2 );
if ( 1 == count( $parts ) ) {
$base = '?';
$query = $parts[0];
} else {
$base = $parts[0] . '?';
$query = $parts[1];
}
} else if ( ! empty( $protocol ) || strpos( $uri, '=' ) === FALSE ) {
$base = $uri . '?';
$query = '';
} else {
$base = '';
$query = $uri;
}
// Parse the query string into an array
parse_str( $query, $qs );
// This re-URL-encodes things that were already in the query string
$qs = self::array_map_deep( $qs, 'urlencode' );
if ( is_array( func_get_arg( 0 ) ) ) {
$kayvees = func_get_arg( 0 );
$qs = array_merge( $qs, $kayvees );
} else {
$qs[func_get_arg( 0 )] = func_get_arg( 1 );
}
foreach ( (array) $qs as $k => $v ) {
if ( $v === false )
unset( $qs[$k] );
}
$ret = http_build_query( $qs );
$ret = trim( $ret, '?' );
$ret = preg_replace( '#=(&|$)#', '$1', $ret );
$ret = $protocol . $base . $ret . $frag;
$ret = rtrim( $ret, '?' );
return $ret;
}
/**
* Removes an item or list from the query string.
*
* @param string|array $keys Query key or keys to remove.
* @param bool $uri When false uses the $_SERVER value
* @return string
*
* @link http://codex.wordpress.org/Function_Reference/remove_query_arg
*
* @access public
* @since 1.0.000
* @static
*/
public static function remove_query_arg( $keys, $uri = FALSE )
{
if ( is_array( $keys ) ) {
foreach ( $keys as $key ) {
$uri = self::add_query_arg( $key, FALSE, $uri );
}
return $uri;
}
return self::add_query_arg( $keys, FALSE, $uri );
}
/**
* Converts many english words that equate to true or false to boolean
*
* Supports 'y', 'n', 'yes', 'no' and a few other variations
*
* @param string $string The string to convert to boolean
* @param bool $default The value to return if we can't match any
* yes/no words
* @return bool
*
* @access public
* @since 1.0.000
* @static
*/
public static function str_to_bool( $string, $default = FALSE )
{
$yes_words = 'affirmative|all right|aye|indubitably|most assuredly|of course|okay|sure thing|y|yes|yea|yep|sure|yeah|true|t';
$no_words = 'no|no way|nope|nah|na|noo|nooo|never|absolutely not|by no means|negative|never ever|false|f';
if ( preg_match( '/^(' . $yes_words . ')$/i', $string ) ) {
return TRUE;
} else if ( preg_match( '/^(' . $no_words . ')$/i', $string ) ) {
return FALSE;
} else {
return $default;
}
}
/**
* Return the absolute integer value of a given variable
*
* @param mixed $maybeint A variable that could be a string,
* integer or other value
* @return int
*
* @access public
* @since 1.0.000
* @static
*/
public static function absint( $maybeint )
{
return abs( intval( $maybeint ) );
}
/**
* Convert entities, while preserving already-encoded entities
*
* @param string $string The text to be converted
* @return string
*
* @link http://ca2.php.net/manual/en/function.htmlentities.php#90111
*
* @access public
* @since 1.0.000
* @static
*/
public static function htmlentities( $string, $preserve_encoded_entities = FALSE )
{
if ( $preserve_encoded_entities ) {
$translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
$translation_table[chr(38)] = '&';
return preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/', '&', strtr( $string, $translation_table ) );
} else {
return htmlentities( $string, ENT_QUOTES );
}
}
/**
* Convert >, <, ', " and & to html entities, but preserves entities
* that are already encoded
*
* @param string $string The text to be converted
* @return string
*
* @link http://ca2.php.net/manual/en/function.htmlentities.php#90111
*
* @access public
* @since 1.0.000