-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.php
1612 lines (1444 loc) · 52.9 KB
/
Tools.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
namespace futuretek\shared;
use DateTime;
use RuntimeException;
/**
* Class Tools
*
* @package futuretek\shared
* @author Lukas Cerny <[email protected]>, Petr Compel <[email protected]>
* @license Apache-2.0
* @link http://www.futuretek.cz
*/
class Tools
{
/** Format phone number in international format for displaying */
const PHONE_NUMBER_FORMAT_INTERNATIONAL_NICE = 1;
/** Format phone number in international format as number */
const PHONE_NUMBER_FORMAT_INTERNATIONAL = 2;
/** Format phone number for displaying */
const PHONE_NUMBER_FORMAT_NICE = 3;
/** Format phone number as pure number (if the phone number is international, + sign may occur) */
const PHONE_NUMBER_FORMAT_NUMBER = 4;
/** Format phone number for use with Smstools3 */
const PHONE_NUMBER_FORMAT_SMSTOOLS = 5;
/** Long date format - d. mmmm. yyyy */
const DATE_LONG = 1;
/** Short date format - d. m. yyyy */
const DATE_SHORT = 2;
/** Long date format - d. mmmm. yyyy h:m:s */
const DATETIME_LONG = 3;
/** Short date format - d. m. yyyy h:m:s */
const DATETIME_SHORT = 4;
/** Algorithm for the hashing function */
const HASH_ALGORITHM = 'sha256';
/** Monday */
const DOW_MONDAY = 0;
/** Tuesday */
const DOW_TUESDAY = 1;
/** Wednesday */
const DOW_WEDNESDAY = 2;
/** Thursday */
const DOW_THURSDAY = 3;
/** Friday */
const DOW_FRIDAY = 4;
/** Saturday */
const DOW_SATURDAY = 5;
/** Sunday */
const DOW_SUNDAY = 6;
private static $_countryCodes = [
'AND' => 'AD', 'ARE' => 'AE', 'AFG' => 'AF', 'ATG' => 'AG', 'AIA' => 'AI', 'ALB' => 'AL', 'ARM' => 'AM', 'ANT' => 'AN', 'AGO' => 'AO', 'ARG' => 'AR',
'ASM' => 'AS', 'AUT' => 'AT', 'AUS' => 'AU', 'ABW' => 'AW', 'AZE' => 'AZ', 'BIH' => 'BA', 'BRB' => 'BB', 'BGD' => 'BD', 'BEL' => 'BE', 'BFA' => 'BF',
'BGR' => 'BG', 'BHR' => 'BH', 'BDI' => 'BI', 'BEN' => 'BJ', 'BMU' => 'BM', 'BRN' => 'BN', 'BOL' => 'BO', 'BRA' => 'BR', 'BHS' => 'BS', 'BTN' => 'BT',
'BWA' => 'BW', 'BLR' => 'BY', 'BLZ' => 'BZ', 'CAN' => 'CA', 'COD' => 'CD', 'CAF' => 'CF', 'COG' => 'CG', 'CIV' => 'CI', 'COK' => 'CK', 'CHL' => 'CL',
'CMR' => 'CM', 'CHN' => 'CN', 'COL' => 'CO', 'CRI' => 'CR', 'CUB' => 'CU', 'CPV' => 'CV', 'CYP' => 'CY', 'CZE' => 'CZ', 'DEU' => 'DE', 'DJI' => 'DJ',
'DNK' => 'DK', 'DMA' => 'DM', 'DOM' => 'DO', 'DZA' => 'DZ', 'ECU' => 'EC', 'EST' => 'EE', 'EGY' => 'EG', 'ESH' => 'EH', 'ERI' => 'ER', 'ESP' => 'ES',
'ETH' => 'ET', 'FIN' => 'FI', 'FJI' => 'FJ', 'FLK' => 'FK', 'FSM' => 'FM', 'FRO' => 'FO', 'FRA' => 'FR', 'GAB' => 'GA', 'GBR' => 'GB', 'GRD' => 'GD',
'GEO' => 'GE', 'GUF' => 'GF', 'GHA' => 'GH', 'GIB' => 'GI', 'GRL' => 'GL', 'GMB' => 'GM', 'GIN' => 'GN', 'GLP' => 'GP', 'GNQ' => 'GQ', 'GRC' => 'GR',
'GTM' => 'GT', 'GUM' => 'GU', 'GNB' => 'GW', 'GUY' => 'GY', 'HKG' => 'HK', 'HND' => 'HN', 'HRV' => 'HR', 'HTI' => 'HT', 'HUN' => 'HU', 'CHE' => 'CH',
'IDN' => 'ID', 'IRL' => 'IE', 'ISR' => 'IL', 'IND' => 'IN', 'IRQ' => 'IQ', 'IRN' => 'IR', 'ISL' => 'IS', 'ITA' => 'IT', 'JAM' => 'JM', 'JOR' => 'JO',
'JPN' => 'JP', 'KEN' => 'KE', 'KGZ' => 'KG', 'KHM' => 'KH', 'KIR' => 'KI', 'COM' => 'KM', 'KNA' => 'KN', 'PRK' => 'KP', 'KOR' => 'KR', 'KWT' => 'KW',
'CYM' => 'KY', 'KAZ' => 'KZ', 'LAO' => 'LA', 'LBN' => 'LB', 'LCA' => 'LC', 'LIE' => 'LI', 'LKA' => 'LK', 'LBR' => 'LR', 'LSO' => 'LS', 'LTU' => 'LT',
'LUX' => 'LU', 'LVA' => 'LV', 'LBY' => 'LY', 'MAR' => 'MA', 'MCO' => 'MC', 'MDA' => 'MD', 'MDG' => 'MG', 'MHL' => 'MH', 'MKD' => 'MK', 'MLI' => 'ML',
'MMR' => 'MM', 'MNG' => 'MN', 'MAC' => 'MO', 'MNP' => 'MP', 'MTQ' => 'MQ', 'MRT' => 'MR', 'MSR' => 'MS', 'MLT' => 'MT', 'MUS' => 'MU', 'MDV' => 'MV',
'MWI' => 'MW', 'MEX' => 'MX', 'MYS' => 'MY', 'MOZ' => 'MZ', 'NAM' => 'NA', 'NCL' => 'NC', 'NER' => 'NE', 'NFK' => 'NF', 'NGA' => 'NG', 'NIC' => 'NI',
'NLD' => 'NL', 'NOR' => 'NO', 'NPL' => 'NP', 'NRU' => 'NR', 'NIU' => 'NU', 'NZL' => 'NZ', 'OMN' => 'OM', 'PAN' => 'PA', 'PER' => 'PE', 'PYF' => 'PF',
'PNG' => 'PG', 'PHL' => 'PH', 'PAK' => 'PK', 'POL' => 'PL', 'SPM' => 'PM', 'PCN' => 'PN', 'PRI' => 'PR', 'PRT' => 'PT', 'PLW' => 'PW', 'PRY' => 'PY',
'QAT' => 'QA', 'REU' => 'RE', 'ROM' => 'RO', 'RUS' => 'RU', 'RWA' => 'RW', 'SAU' => 'SA', 'SLB' => 'SB', 'SYC' => 'SC', 'SDN' => 'SD', 'SWE' => 'SE',
'SGP' => 'SG', 'SHN' => 'SH', 'SVN' => 'SI', 'SJM' => 'SJ', 'SVK' => 'SK', 'SLE' => 'SL', 'SMR' => 'SM', 'SEN' => 'SN', 'SOM' => 'SO', 'SUR' => 'SR',
'STP' => 'ST', 'SLV' => 'SV', 'SYR' => 'SY', 'SWZ' => 'SZ', 'TCA' => 'TC', 'TCD' => 'TD', 'TGO' => 'TG', 'THA' => 'TH', 'TJK' => 'TJ', 'TKL' => 'TK',
'TKM' => 'TM', 'TUN' => 'TN', 'TON' => 'TO', 'TUR' => 'TR', 'TTO' => 'TT', 'TUV' => 'TV', 'TWN' => 'TW', 'TZA' => 'TZ', 'UKR' => 'UA', 'UGA' => 'UG',
'USA' => 'US', 'URY' => 'UY', 'UZB' => 'UZ', 'VAT' => 'VA', 'VCT' => 'VC', 'VEN' => 'VE', 'VGB' => 'VG', 'VIR' => 'VI', 'VNM' => 'VN', 'VUT' => 'VU',
'WLF' => 'WF', 'WSM' => 'WS', 'YEM' => 'YE', 'ZAF' => 'ZA', 'ZMB' => 'ZM', 'ZWE' => 'ZW',
];
/**
* Remove all space characters in string
*
* @param string $str Input string
*
* @return string String with removed spaces
* @static
*/
public static function removeSpace($str)
{
return strtr($str, [' ' => '']);
}
/**
* Format phone number
*
* @param string $phoneNumber Phone number
* @param int $formatType Phone number format:
* <ul>
* <li>Tools::PHONE_NUMBER_FORMAT_INTERNATIONAL_NICE</li>
* <li>Tools::PHONE_NUMBER_FORMAT_INTERNATIONAL</li>
* <li>Tools::PHONE_NUMBER_FORMAT_NICE</li>
* <li>Tools::PHONE_NUMBER_FORMAT_NUMBER</li>
* <li>Tools::PHONE_NUMBER_FORMAT_SMSTOOLS</li>
* </ul>
*
* @return string|bool Formatted phone number or false when the phone number is invalid
* @static
*/
public static function formatPhoneNumber($phoneNumber, $formatType = Tools::PHONE_NUMBER_FORMAT_NUMBER)
{
$formatType = (int)$formatType;
if ($formatType !== self::PHONE_NUMBER_FORMAT_INTERNATIONAL && $formatType !== self::PHONE_NUMBER_FORMAT_INTERNATIONAL_NICE &&
$formatType !== self::PHONE_NUMBER_FORMAT_NUMBER && $formatType !== self::PHONE_NUMBER_FORMAT_NICE &&
$formatType !== self::PHONE_NUMBER_FORMAT_SMSTOOLS
) {
return false;
}
if (!Validate::isPhoneNumber($phoneNumber)) {
return false;
}
$phoneNumber = self::removeSpace($phoneNumber);
$phoneLen = \strlen($phoneNumber);
if ($phoneLen > 9 && 0 !== strpos($phoneNumber, '+')) {
$phoneNumber = '+' . $phoneNumber;
$phoneLen++;
}
if ($phoneLen !== 9 && !($phoneLen >= 11 && $phoneLen <= 13 && 0 === strpos($phoneNumber, '+'))) {
return false;
}
$international = ($phoneLen !== 9);
switch ($formatType) {
case self::PHONE_NUMBER_FORMAT_INTERNATIONAL_NICE:
$formattedPhone = preg_replace(
'/^(\+\d{1,3})(\d{3})(\d{3})(\d{3})$/',
'$1 $2 $3 $4',
$international ? $phoneNumber : '+420' . $phoneNumber
);
break;
case self::PHONE_NUMBER_FORMAT_INTERNATIONAL:
$formattedPhone = $international ? $phoneNumber : '+420' . $phoneNumber;
break;
case self::PHONE_NUMBER_FORMAT_NICE:
$formattedPhone = preg_replace(
'/^(\+\d{1,3})(\d{3})(\d{3})(\d{3})$/',
'$2 $3 $4',
$international ? $phoneNumber : '+420' . $phoneNumber
);
break;
case self::PHONE_NUMBER_FORMAT_NUMBER:
$formattedPhone = $international ? substr($phoneNumber, -9) : $phoneNumber;
break;
case self::PHONE_NUMBER_FORMAT_SMSTOOLS:
$formattedPhone = $international ? trim($phoneNumber, '+') : '420' . $phoneNumber;
break;
default:
$formattedPhone = false;
}
return $formattedPhone;
}
/**
* Format date/datetime
*
* @param string|integer $value Timestamp or date accepted by strtotime()
* @param integer $format Date format
*
* @return bool|string Formatted date
* @static
*/
public static function formatDate($value, $format)
{
if ($value === null) {
return false;
}
//Make sure we have timestamp
if (!\is_int($value)) {
$value = strtotime($value);
}
switch ($format) {
case self::DATE_LONG:
$inflection = new Inflection();
$date = date('j', $value) . '. ' . $inflection->inflect(self::getMonthName(date('n', $value)))[2] . ' ' . date('Y', $value);
break;
case self::DATE_SHORT:
$date = date('j. n. Y', $value);
break;
case self::DATETIME_LONG:
$inflection = new Inflection();
$date =
date('j', $value) . '. ' . $inflection->inflect(self::getMonthName(date('n', $value)))[2] . ' ' .
date('Y G:i:s', $value);
break;
case self::DATETIME_SHORT:
$date = date('j. n. Y G:i:s', $value);
break;
default:
return false;
}
return $date;
}
/**
* Get month name in current language
*
* @param int $month Month number
*
* @return string Localised month name
* @static
*/
public static function getMonthName($month)
{
if ($month < 1 || $month > 12) {
return '';
}
$monthNames = [
1 => self::poorManTranslate('fts-shared', 'January'),
2 => self::poorManTranslate('fts-shared', 'February'),
3 => self::poorManTranslate('fts-shared', 'March'),
4 => self::poorManTranslate('fts-shared', 'April'),
5 => self::poorManTranslate('fts-shared', 'May'),
6 => self::poorManTranslate('fts-shared', 'June'),
7 => self::poorManTranslate('fts-shared', 'July'),
8 => self::poorManTranslate('fts-shared', 'August'),
9 => self::poorManTranslate('fts-shared', 'September'),
10 => self::poorManTranslate('fts-shared', 'October'),
11 => self::poorManTranslate('fts-shared', 'November'),
12 => self::poorManTranslate('fts-shared', 'December'),
];
return $monthNames[$month];
}
/**
* Get day name in current language
*
* @param int $day Day of week number according to Tools::dow()
*
* @return string Localised day name
* @static
*/
public static function getDayName($day)
{
if ($day < self::DOW_MONDAY || $day > self::DOW_SUNDAY) {
return '';
}
$dayNames = [
self::DOW_MONDAY => self::poorManTranslate('fts-shared', 'Monday'),
self::DOW_TUESDAY => self::poorManTranslate('fts-shared', 'Tuesday'),
self::DOW_WEDNESDAY => self::poorManTranslate('fts-shared', 'Wednesday'),
self::DOW_THURSDAY => self::poorManTranslate('fts-shared', 'Thursday'),
self::DOW_FRIDAY => self::poorManTranslate('fts-shared', 'Friday'),
self::DOW_SATURDAY => self::poorManTranslate('fts-shared', 'Saturday'),
self::DOW_SUNDAY => self::poorManTranslate('fts-shared', 'Sunday'),
];
return $dayNames[$day];
}
/**
* Get textual representation of time difference in style 'some time ago'
*
* @param int|string $value Time in any format accepted by strtotime()
*
* @return string Textual representation of time difference
* @static
*/
public static function getRelativeTime($value)
{
//Make sure we have timestamp
if (!\is_int($value)) {
$value = strtotime($value);
}
$diff = abs($value - time());
if ($value < time()) {
if ($diff < 60) {
//Less than a minute
return self::poorManTranslate('fts-shared', 'a few seconds ago');
}
if ($diff < 3600) {
//Less than a hour
return self::poorManTranslate('fts-shared', 'a {n, plural, =1{minute} other{# minutes}} ago', ['n' => floor($diff / 60)]);
}
if ($diff < 86400) {
//Less than a day
return self::poorManTranslate('fts-shared', 'a {n, plural, =1{hour} other{# hours}} ago', ['n' => floor($diff / 3600)]);
}
if ($diff < 172800) {
//Less than two days
return self::poorManTranslate('fts-shared', 'yesterday');
}
if ($diff < 604800) {
//Less than a week
return self::poorManTranslate('fts-shared', 'a {n} days ago', ['n' => floor($diff / 86400)]);
}
if ($diff < 2628000) {
//Less than a month
return self::poorManTranslate('fts-shared', 'a {n, plural, =1{week} other{# weeks}} ago', ['n' => floor($diff / 604800)]);
}
if ($diff < 31556926) {
//Less than a year
return self::poorManTranslate('fts-shared', 'a {n, plural, =1{month} other{# months}} ago', ['n' => floor($diff / 2628000)]);
}
//Less than eternity :-)
return self::poorManTranslate('fts-shared', 'a {n, plural, =1{year} other{# years}} ago', ['n' => floor($diff / 31556926)]);
}
if ($diff < 60) {
//Less than a minute
return self::poorManTranslate('fts-shared', 'after a few seconds');
}
if ($diff < 3600) {
//Less than a hour
return self::poorManTranslate('fts-shared', 'after a {n, plural, =1{minute} other{# minutes}}', ['n' => floor($diff / 60)]);
}
if ($diff < 86400) {
//Less than a day
return self::poorManTranslate('fts-shared', 'after a {n, plural, =1{hour} other{# hours}}', ['n' => floor($diff / 3600)]);
}
if ($diff < 172800) {
//Less than two days
return self::poorManTranslate('fts-shared', 'tomorrow');
}
if ($diff < 604800) {
//Less than a week
return self::poorManTranslate('fts-shared', 'after a {n, plural, =1{day} other{# days}}', ['n' => floor($diff / 86400)]);
}
if ($diff < 2628000) {
//Less than a month
return self::poorManTranslate('fts-shared', 'after a {n, plural, =1{week} other{# weeks}}', ['n' => floor($diff / 604800)]);
}
if ($diff < 31556926) {
//Less than a year
return self::poorManTranslate('fts-shared', 'after a {n, plural, =1{month} other{# months}}', ['n' => floor($diff / 2628000)]);
}
//Less than eternity :-)
return self::poorManTranslate('fts-shared', 'after a {n, plural, =1{year} other{# years}}', ['n' => floor($diff / 31556926)]);
}
/**
* Truncate string to a maximal specific length with smart truncate on given character
*
* @param string $string Input string
* @param integer $limit Maximal output string length
* @param string $break Character on which the output string should be truncated.
* @param string $pad String that will be appended at the end of truncated string
*
* @return string Truncated string
* @static
*/
public static function truncate($string, $limit, $break = ' ', $pad = '...')
{
if (\strlen($string) <= $limit) {
return $string;
}
if (false !== ($breakpoint = strrpos(substr($string, 0, $limit), $break))) {
$string = substr($string, 0, $breakpoint) . $pad;
} else {
$string = substr($string, 0, $limit) . $pad;
}
return $string;
}
/**
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*
* @return string GUIDv4
* @static
*/
public static function GUIDv4()
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
/**
* Generate v5 UUID
*
* Version 5 UUIDs are named based. They require a namespace (another
* valid UUID) and a value (the name). Given the same namespace and
* name, the output is always the same.
*
* @param string $namespace Valid namespace (GUID)
* @param string $name Valid name (value)
*
* @return string|bool GUIDv5 or false when the namespace is not a valid GUID
* @static
*/
public static function GUIDv5($namespace, $name)
{
if (!Validate::isGuid($namespace)) {
return false;
}
$nHex = str_replace(['-', '{', '}'], '', $namespace);
$nStr = '';
$nHexLen = \strlen($nHex);
for ($i = 0; $i < $nHexLen; $i += 2) {
$nStr .= \chr(\intval($nHex[$i] . $nHex[$i + 1], 16));
}
$hash = sha1($nStr . $name);
return sprintf(
'%08s-%04s-%04x-%04x-%12s',
substr($hash, 0, 8),
substr($hash, 8, 4),
(\intval(substr($hash, 12, 4), 16) & 0x0fff) | 0x5000,
(\intval(substr($hash, 16, 4), 16) & 0x3fff) | 0x8000,
substr($hash, 20, 12)
);
}
/**
* Translates a string with underscores into camel case (e.g. first_name -> firstName)
*
* @param $str
* @param bool $catapitalise_first_char
*
* @return mixed
*/
public static function toCamelCase($str, $catapitalise_first_char = false)
{
$str = strtolower($str);
if ($catapitalise_first_char) {
$str = ucfirst($str);
}
return preg_replace_callback('/_+([a-z])/', function ($c) {
return strtoupper($c[1]);
}, $str);
}
/**
* Transform a CamelCase string to underscore_case string
*
* @param string $string
*
* @return string
*/
public static function toUnderscoreCase($string)
{
return strtolower(trim(preg_replace('/([A-Z][a-z])/', '_$1', $string), '_'));
}
/**
* Transform a CamelCase string to comma-case string
*
* @param string $string
*
* @return string
*/
public static function toCommaCase($string)
{
return strtolower(trim(preg_replace('/([A-Z][a-z])/', '-$1', $string), '-'));
}
/**
* Random password generator
*
* @param integer $length Desired length (optional)
* @param string $flag Output type (NUMERIC, ALPHANUMERIC, ALPHA, ALPHA_LOWER)
*
* @return string Password
*/
public static function passwdGen($length = 8, $flag = 'ALPHANUMERIC')
{
switch ($flag) {
case 'NUMERIC':
$str = '0123456789';
break;
case 'ALPHA':
$str = 'abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'ALPHA_LOWER':
$str = 'abcdefghijkmnopqrstuvwxyz';
break;
default:
$str = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
}
for ($i = 0, $passwd = ''; $i < $length; $i++) {
$passwd .= substr($str, mt_rand(0, \strlen($str) - 1), 1);
}
return $passwd;
}
/**
* Get the server variable REMOTE_ADDR, or the first ip of HTTP_X_FORWARDED_FOR (when using proxy)
*
* @return string $remote_addr ip of client
*/
public static function getRemoteAddr()
{
// This condition is necessary when using CDN, don't remove it.
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && $_SERVER['HTTP_X_FORWARDED_FOR'] &&
(!array_key_exists('REMOTE_ADDR', $_SERVER) || preg_match('/^127\./', trim($_SERVER['REMOTE_ADDR'])) ||
preg_match('/^172\.16\./', trim($_SERVER['REMOTE_ADDR'])) ||
preg_match('/^192\.168\./', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^10\./', trim($_SERVER['REMOTE_ADDR'])))
) {
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
return $ips[0];
}
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $_SERVER['REMOTE_ADDR'];
}
/**
* Delete directory and subdirectories
*
* @param string $dirName Directory name
* @param bool $deleteSelf Delete also specified directory
*
* @return bool
*/
public static function deleteDirectory($dirName, $deleteSelf = true)
{
$dirName = rtrim($dirName, '/') . '/';
if (file_exists($dirName) && $files = scandir($dirName, SCANDIR_SORT_NONE)) {
foreach ($files as $file) {
if ($file !== '.' && $file !== '..' && $file !== '.svn') {
if (is_dir($dirName . $file)) {
self::deleteDirectory($dirName . $file, true);
} elseif (file_exists($dirName . $file)) {
@chmod($dirName . $file, 0777); // NT ?
unlink($dirName . $file);
}
}
}
if ($deleteSelf && !rmdir($dirName)) {
@chmod($dirName, 0777); // NT ?
return false;
}
return true;
}
return false;
}
/**
* Delete file
*
* @param string $file File path
* @param array $excludeFiles Excluded files
*/
public static function deleteFile($file, array $excludeFiles = [])
{
if (!\is_array($excludeFiles)) {
$excludeFiles = [$excludeFiles];
}
if (file_exists($file) && is_file($file) && \in_array(basename($file), $excludeFiles, true) === false) {
@chmod($file, 0777); // NT ?
unlink($file);
}
}
/**
* Replace all accented chars by their equivalent non accented chars.
*
* @param string $str
*
* @return string
*/
public static function replaceAccentedChars($str)
{
/* One source among others:
http://www.tachyonsoft.com/uc0000.htm
http://www.tachyonsoft.com/uc0001.htm
http://www.tachyonsoft.com/uc0004.htm
*/
$patterns = [
/* Lowercase */
/* a */
'/[\x{00E0}\x{00E1}\x{00E2}\x{00E3}\x{00E4}\x{00E5}\x{0101}\x{0103}\x{0105}\x{0430}]/u',
/* b */
'/[\x{0431}]/u',
/* c */
'/[\x{00E7}\x{0107}\x{0109}\x{010D}\x{0446}]/u',
/* d */
'/[\x{010F}\x{0111}\x{0434}]/u',
/* e */
'/[\x{00E8}\x{00E9}\x{00EA}\x{00EB}\x{0113}\x{0115}\x{0117}\x{0119}\x{011B}\x{0435}\x{044D}]/u',
/* f */
'/[\x{0444}]/u',
/* g */
'/[\x{011F}\x{0121}\x{0123}\x{0433}\x{0491}]/u',
/* h */
'/[\x{0125}\x{0127}]/u',
/* i */
'/[\x{00EC}\x{00ED}\x{00EE}\x{00EF}\x{0129}\x{012B}\x{012D}\x{012F}\x{0131}\x{0438}\x{0456}]/u',
/* j */
'/[\x{0135}\x{0439}]/u',
/* k */
'/[\x{0137}\x{0138}\x{043A}]/u',
/* l */
'/[\x{013A}\x{013C}\x{013E}\x{0140}\x{0142}\x{043B}]/u',
/* m */
'/[\x{043C}]/u',
/* n */
'/[\x{00F1}\x{0144}\x{0146}\x{0148}\x{0149}\x{014B}\x{043D}]/u',
/* o */
'/[\x{00F2}\x{00F3}\x{00F4}\x{00F5}\x{00F6}\x{00F8}\x{014D}\x{014F}\x{0151}\x{043E}]/u',
/* p */
'/[\x{043F}]/u',
/* r */
'/[\x{0155}\x{0157}\x{0159}\x{0440}]/u',
/* s */
'/[\x{015B}\x{015D}\x{015F}\x{0161}\x{0441}]/u',
/* ss */
'/[\x{00DF}]/u',
/* t */
'/[\x{0163}\x{0165}\x{0167}\x{0442}]/u',
/* u */
'/[\x{00F9}\x{00FA}\x{00FB}\x{00FC}\x{0169}\x{016B}\x{016D}\x{016F}\x{0171}\x{0173}\x{0443}]/u',
/* v */
'/[\x{0432}]/u',
/* w */
'/[\x{0175}]/u',
/* y */
'/[\x{00FF}\x{0177}\x{00FD}\x{044B}]/u',
/* z */
'/[\x{017A}\x{017C}\x{017E}\x{0437}]/u',
/* ae */
'/[\x{00E6}]/u',
/* ch */
'/[\x{0447}]/u',
/* kh */
'/[\x{0445}]/u',
/* oe */
'/[\x{0153}]/u',
/* sh */
'/[\x{0448}]/u',
/* shh*/
'/[\x{0449}]/u',
/* ya */
'/[\x{044F}]/u',
/* ye */
'/[\x{0454}]/u',
/* yi */
'/[\x{0457}]/u',
/* yo */
'/[\x{0451}]/u',
/* yu */
'/[\x{044E}]/u',
/* zh */
'/[\x{0436}]/u',
/* Uppercase */
/* A */
'/[\x{0100}\x{0102}\x{0104}\x{00C0}\x{00C1}\x{00C2}\x{00C3}\x{00C4}\x{00C5}\x{0410}]/u',
/* B */
'/[\x{0411}]]/u',
/* C */
'/[\x{00C7}\x{0106}\x{0108}\x{010A}\x{010C}\x{0426}]/u',
/* D */
'/[\x{010E}\x{0110}\x{0414}]/u',
/* E */
'/[\x{00C8}\x{00C9}\x{00CA}\x{00CB}\x{0112}\x{0114}\x{0116}\x{0118}\x{011A}\x{0415}\x{042D}]/u',
/* F */
'/[\x{0424}]/u',
/* G */
'/[\x{011C}\x{011E}\x{0120}\x{0122}\x{0413}\x{0490}]/u',
/* H */
'/[\x{0124}\x{0126}]/u',
/* I */
'/[\x{0128}\x{012A}\x{012C}\x{012E}\x{0130}\x{0418}\x{0406}]/u',
/* J */
'/[\x{0134}\x{0419}]/u',
/* K */
'/[\x{0136}\x{041A}]/u',
/* L */
'/[\x{0139}\x{013B}\x{013D}\x{0139}\x{0141}\x{041B}]/u',
/* M */
'/[\x{041C}]/u',
/* N */
'/[\x{00D1}\x{0143}\x{0145}\x{0147}\x{014A}\x{041D}]/u',
/* O */
'/[\x{00D3}\x{014C}\x{014E}\x{0150}\x{041E}]/u',
/* P */
'/[\x{041F}]/u',
/* R */
'/[\x{0154}\x{0156}\x{0158}\x{0420}]/u',
/* S */
'/[\x{015A}\x{015C}\x{015E}\x{0160}\x{0421}]/u',
/* T */
'/[\x{0162}\x{0164}\x{0166}\x{0422}]/u',
/* U */
'/[\x{00D9}\x{00DA}\x{00DB}\x{00DC}\x{0168}\x{016A}\x{016C}\x{016E}\x{0170}\x{0172}\x{0423}]/u',
/* V */
'/[\x{0412}]/u',
/* W */
'/[\x{0174}]/u',
/* Y */
'/[\x{0176}\x{042B}]/u',
/* Z */
'/[\x{0179}\x{017B}\x{017D}\x{0417}]/u',
/* AE */
'/[\x{00C6}]/u',
/* CH */
'/[\x{0427}]/u',
/* KH */
'/[\x{0425}]/u',
/* OE */
'/[\x{0152}]/u',
/* SH */
'/[\x{0428}]/u',
/* SHH*/
'/[\x{0429}]/u',
/* YA */
'/[\x{042F}]/u',
/* YE */
'/[\x{0404}]/u',
/* YI */
'/[\x{0407}]/u',
/* YO */
'/[\x{0401}]/u',
/* YU */
'/[\x{042E}]/u',
/* ZH */
'/[\x{0416}]/u',
];
// ö to oe
// å to aa
// ä to ae
$replacements = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'r',
's',
'ss',
't',
'u',
'v',
'w',
'y',
'z',
'ae',
'ch',
'kh',
'oe',
'sh',
'shh',
'ya',
'ye',
'yi',
'yo',
'yu',
'zh',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'R',
'S',
'T',
'U',
'V',
'W',
'Y',
'Z',
'AE',
'CH',
'KH',
'OE',
'SH',
'SHH',
'YA',
'YE',
'YI',
'YO',
'YU',
'ZH',
];
return preg_replace($patterns, $replacements, $str);
}
/**
* Format a number into a human readable format
* e.g. 24962496 => 23.81M
*
* @param $size
* @param int $precision
*
* @return string
*/
public static function formatBytes($size, $precision = 2)
{
if (!$size) {
return '0';
}
$base = log($size) / log(1024);
$suffixes = ['', 'k', 'M', 'G', 'T'];
return round(1024 ** ($base - floor($base)), $precision) . $suffixes[(int)floor($base)];
}
/**
* Convert a shorthand byte value from a PHP configuration directive to an integer value
*
* @param string $value value to convert
*
* @return int
*/
public static function unformatBytes($value)
{
if (is_numeric($value)) {
return $value;
}
$value_length = strlen($value);
$qty = (int)substr($value, 0, $value_length - 1);
$unit = strtolower(substr($value, $value_length - 1));
switch ($unit) {
case 'k':
$qty *= 1024;
break;
case 'm':
$qty *= 1048576;
break;
case 'g':
$qty *= 1073741824;
break;
}
return $qty;
}
/**
* getOctet allow to gets the value of a configuration option in octet
*
* @param string $option
*
* @return int the value of a configuration option in octet
*/
public static function getOctets($option)
{
if (preg_match('/\d+k/i', $option)) {
return 1024 * (int)$option;
}
if (preg_match('/\d+m/i', $option)) {
return 1024 * 1024 * (int)$option;
}
if (preg_match('/\d+g/i', $option)) {
return 1024 * 1024 * 1024 * (int)$option;
}
return $option;
}
/**
* Get user OS
*
* @return string User OS
* @static
*/
public static function getUserPlatform()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$user_platform = 'unknown';
if (false !== stripos($user_agent, 'linux')) {
$user_platform = 'Linux';
} elseif (preg_match('/macintosh|mac os x/i', $user_agent)) {
$user_platform = 'Mac';
} elseif (preg_match('/windows|win32/i', $user_agent)) {
$user_platform = 'Windows';
}
return $user_platform;
}
/**
* Get user browser
*
* @return string User browser
* @static
*/
public static function getUserBrowser()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$user_browser = 'unknown';
if (false !== stripos($user_agent, 'MSIE') && false === stripos($user_agent, 'Opera')) {
$user_browser = 'Internet Explorer';
} elseif (false !== stripos($user_agent, 'Firefox')) {
$user_browser = 'Mozilla Firefox';
} elseif (false !== stripos($user_agent, 'Chrome')) {
$user_browser = 'Google Chrome';
} elseif (false !== stripos($user_agent, 'Safari')) {
$user_browser = 'Apple Safari';
} elseif (false !== stripos($user_agent, 'Opera')) {
$user_browser = 'Opera';
} elseif (false !== stripos($user_agent, 'Netscape')) {
$user_browser = 'Netscape';
}
return $user_browser;
}
/**
* Convert wildcard IP address range notation (172.16.*.*) to CIDR netmask notation (172.16.0.0/16)
*
* @param string $wildcardIp Wildcard IP address range notation
*