-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.php
More file actions
2404 lines (2035 loc) · 59.3 KB
/
Copy pathstdlib.php
File metadata and controls
2404 lines (2035 loc) · 59.3 KB
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 declare(strict_types = 1);
# TODO: check argument types for __object_map() functions
use dqdp\InvalidTypeException;
use dqdp\LV;
use dqdp\MailQueue\MailQueue;
use dqdp\StdObject;
use PHPMailer\PHPMailer\PHPMailer;
require_once("qblib.php");
require_once("objflib.php");
final class dqdp {
static public $DATE_FORMAT = 'd.m.Y';
static public $TIME_FORMAT = 'H:i:s';
static public $LOCALE_INFO = null;
}
function netmasks(){
return [
"0.0.0.0", "128.0.0.0", "192.0.0.0", "224.0.0.0", "240.0.0.0", "248.0.0.0", "252.0.0.0",
"254.0.0.0", "255.0.0.0", "255.128.0.0", "255.192.0.0", "255.224.0.0", "255.240.0.0", "255.248.0.0", "255.252.0.0",
"255.254.0.0", "255.255.0.0", "255.255.128.0", "255.255.192.0", "255.255.224.0", "255.255.240.0", "255.255.248.0",
"255.255.252.0", "255.255.254.0", "255.255.255.0", "255.255.255.128", "255.255.255.192", "255.255.255.224",
"255.255.255.240", "255.255.255.248", "255.255.255.252", "255.255.255.254", "255.255.255.255"
];
};
function menesi(){
return [
'01'=>'Janvāris',
'02'=>'Februāris',
'03'=>'Marts',
'04'=>'Aprīlis',
'05'=>'Maijs',
'06'=>'Jūnijs',
'07'=>'Jūlijs',
'08'=>'Augusts',
'09'=>'Septembris',
'10'=>'Oktobris',
'11'=>'Novembris',
'12'=>'Decembris',
];
}
function country_find_iso($country){
$country = mb_strtoupper($country);
foreach(countries() as $k=>$v){
if(mb_strtoupper($v) == $country){
return $k;
}
}
return false;
}
function country_codes_eu($date = null){
if($date){
$date = strtotime($date);
} else {
$date = time();
}
$codes = [
'AT','BE','BG','CY','CZ','DK','EE','FI','FR','DE','GR','HU','HR','IE',
'IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES','SE'
];
if($date < strtotime('1.1.2020')){
$codes[] = 'GB';
}
return $codes;
}
function country_codes_eu_sql($date = null){
return "'".join("','", country_codes_eu($date))."'";
}
function countries(){
return [
"AF" => "Afghanistan",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua and Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"BY" => "Belarus",
"BE" => "Belgium",
"BZ" => "Belize",
"BJ" => "Benin",
"BM" => "Bermuda",
"BT" => "Bhutan",
"BO" => "Bolivia",
"BA" => "Bosnia and Herzegovina",
"BW" => "Botswana",
"BV" => "Bouvet Island",
"BR" => "Brazil",
"BQ" => "British Antarctic Territory",
"IO" => "British Indian Ocean Territory",
"VG" => "British Virgin Islands",
"BN" => "Brunei",
"BG" => "Bulgaria",
"BF" => "Burkina Faso",
"BI" => "Burundi",
"KH" => "Cambodia",
"CM" => "Cameroon",
"CA" => "Canada",
"CT" => "Canton and Enderbury Islands",
"CV" => "Cape Verde",
"KY" => "Cayman Islands",
"CF" => "Central African Republic",
"TD" => "Chad",
"CL" => "Chile",
"CN" => "China",
"CX" => "Christmas Island",
"CC" => "Cocos [Keeling] Islands",
"CO" => "Colombia",
"KM" => "Comoros",
"CG" => "Congo - Brazzaville",
"CD" => "Congo - Kinshasa",
"CK" => "Cook Islands",
"CR" => "Costa Rica",
"HR" => "Croatia",
"CU" => "Cuba",
"CY" => "Cyprus",
"CZ" => "Czech Republic",
"CI" => "Côte d’Ivoire",
"DK" => "Denmark",
"DJ" => "Djibouti",
"DM" => "Dominica",
"DO" => "Dominican Republic",
"NQ" => "Dronning Maud Land",
"DD" => "East Germany",
"EC" => "Ecuador",
"EG" => "Egypt",
"SV" => "El Salvador",
"GQ" => "Equatorial Guinea",
"ER" => "Eritrea",
"EE" => "Estonia",
"ET" => "Ethiopia",
"FK" => "Falkland Islands",
"FO" => "Faroe Islands",
"FJ" => "Fiji",
"FI" => "Finland",
"FR" => "France",
"GF" => "French Guiana",
"PF" => "French Polynesia",
"TF" => "French Southern Territories",
"FQ" => "French Southern and Antarctic Territories",
"GA" => "Gabon",
"GM" => "Gambia",
"GE" => "Georgia",
"DE" => "Germany",
"GH" => "Ghana",
"GI" => "Gibraltar",
"GR" => "Greece",
"GL" => "Greenland",
"GD" => "Grenada",
"GP" => "Guadeloupe",
"GU" => "Guam",
"GT" => "Guatemala",
"GG" => "Guernsey",
"GN" => "Guinea",
"GW" => "Guinea-Bissau",
"GY" => "Guyana",
"HT" => "Haiti",
"HM" => "Heard Island and McDonald Islands",
"HN" => "Honduras",
"HK" => "Hong Kong SAR China",
"HU" => "Hungary",
"IS" => "Iceland",
"IN" => "India",
"ID" => "Indonesia",
"IR" => "Iran",
"IQ" => "Iraq",
"IE" => "Ireland",
"IM" => "Isle of Man",
"IL" => "Israel",
"IT" => "Italy",
"JM" => "Jamaica",
"JP" => "Japan",
"JE" => "Jersey",
"JT" => "Johnston Island",
"JO" => "Jordan",
"KZ" => "Kazakhstan",
"KE" => "Kenya",
"KI" => "Kiribati",
"KW" => "Kuwait",
"KG" => "Kyrgyzstan",
"LA" => "Laos",
"LV" => "Latvia",
"LB" => "Lebanon",
"LS" => "Lesotho",
"LR" => "Liberia",
"LY" => "Libya",
"LI" => "Liechtenstein",
"LT" => "Lithuania",
"LU" => "Luxembourg",
"MO" => "Macau SAR China",
"MK" => "Macedonia",
"MG" => "Madagascar",
"MW" => "Malawi",
"MY" => "Malaysia",
"MV" => "Maldives",
"ML" => "Mali",
"MT" => "Malta",
"MH" => "Marshall Islands",
"MQ" => "Martinique",
"MR" => "Mauritania",
"MU" => "Mauritius",
"YT" => "Mayotte",
"FX" => "Metropolitan France",
"MX" => "Mexico",
"FM" => "Micronesia",
"MI" => "Midway Islands",
"MD" => "Moldova",
"MC" => "Monaco",
"MN" => "Mongolia",
"ME" => "Montenegro",
"MS" => "Montserrat",
"MA" => "Morocco",
"MZ" => "Mozambique",
"MM" => "Myanmar [Burma]",
"NA" => "Namibia",
"NR" => "Nauru",
"NP" => "Nepal",
"NL" => "Netherlands",
"AN" => "Netherlands Antilles",
"NT" => "Neutral Zone",
"NC" => "New Caledonia",
"NZ" => "New Zealand",
"NI" => "Nicaragua",
"NE" => "Niger",
"NG" => "Nigeria",
"NU" => "Niue",
"NF" => "Norfolk Island",
"KP" => "North Korea",
"VD" => "North Vietnam",
"MP" => "Northern Mariana Islands",
"NO" => "Norway",
"OM" => "Oman",
"PC" => "Pacific Islands Trust Territory",
"PK" => "Pakistan",
"PW" => "Palau",
"PS" => "Palestinian Territories",
"PA" => "Panama",
"PZ" => "Panama Canal Zone",
"PG" => "Papua New Guinea",
"PY" => "Paraguay",
"YD" => "People's Democratic Republic of Yemen",
"PE" => "Peru",
"PH" => "Philippines",
"PN" => "Pitcairn Islands",
"PL" => "Poland",
"PT" => "Portugal",
"PR" => "Puerto Rico",
"QA" => "Qatar",
"RO" => "Romania",
"RU" => "Russia",
"RW" => "Rwanda",
"RE" => "Réunion",
"BL" => "Saint Barthélemy",
"SH" => "Saint Helena",
"KN" => "Saint Kitts and Nevis",
"LC" => "Saint Lucia",
"MF" => "Saint Martin",
"PM" => "Saint Pierre and Miquelon",
"VC" => "Saint Vincent and the Grenadines",
"WS" => "Samoa",
"SM" => "San Marino",
"SA" => "Saudi Arabia",
"SN" => "Senegal",
"RS" => "Serbia",
"CS" => "Serbia and Montenegro",
"SC" => "Seychelles",
"SL" => "Sierra Leone",
"SG" => "Singapore",
"SK" => "Slovakia",
"SI" => "Slovenia",
"SB" => "Solomon Islands",
"SO" => "Somalia",
"ZA" => "South Africa",
"GS" => "South Georgia and the South Sandwich Islands",
"KR" => "South Korea",
"ES" => "Spain",
"LK" => "Sri Lanka",
"SD" => "Sudan",
"SR" => "Suriname",
"SJ" => "Svalbard and Jan Mayen",
"SZ" => "Swaziland",
"SE" => "Sweden",
"CH" => "Switzerland",
"SY" => "Syria",
"ST" => "São Tomé and Príncipe",
"TW" => "Taiwan",
"TJ" => "Tajikistan",
"TZ" => "Tanzania",
"TH" => "Thailand",
"TL" => "Timor-Leste",
"TG" => "Togo",
"TK" => "Tokelau",
"TO" => "Tonga",
"TT" => "Trinidad and Tobago",
"TN" => "Tunisia",
"TR" => "Turkey",
"TM" => "Turkmenistan",
"TC" => "Turks and Caicos Islands",
"TV" => "Tuvalu",
"UM" => "U.S. Minor Outlying Islands",
"PU" => "U.S. Miscellaneous Pacific Islands",
"VI" => "U.S. Virgin Islands",
"UG" => "Uganda",
"UA" => "Ukraine",
"SU" => "Union of Soviet Socialist Republics",
"AE" => "United Arab Emirates",
"GB" => "United Kingdom",
"US" => "United States",
"ZZ" => "Unknown or Invalid Region",
"UY" => "Uruguay",
"UZ" => "Uzbekistan",
"VU" => "Vanuatu",
"VA" => "Vatican City",
"VE" => "Venezuela",
"VN" => "Vietnam",
"WK" => "Wake Island",
"WF" => "Wallis and Futuna",
"EH" => "Western Sahara",
"YE" => "Yemen",
"ZM" => "Zambia",
"ZW" => "Zimbabwe",
"AX" => "Åland Islands",
];
}
function req(string $key, $default = ''){
return $_REQUEST[$key]??$default;
}
function get($key, $default = ''){
return isset($_GET[$key]) ? $_GET[$key] : $default;
}
function post($key, $default = ''){
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
function postget($key, $default = ''){
return isset($_POST[$key]) ? $_POST[$key] : get($key, $default);
}
function getpost($key, $default = ''){
return isset($_GET[$key]) ? $_GET[$key] : post($key, $default);
}
function sess($key, $default = ''){
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
}
function cookie($key, $default = ''){
return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default;
}
function server($key, $default = ''){
return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
}
function env($key, $default = ''){
return isset($_ENV[$key]) ? $_ENV[$key] : $default;
}
function upload($key, $default = ''){
return isset($_FILES[$key]) ? $_FILES[$key] : $default;
}
function redirect(string $url = null): bool {
header("Location: ".($url??php_self()));
return true;
}
function redirect_wqs(string $url = null): bool {
$qs = empty($_SERVER['QUERY_STRING']) ? "" : "?".$_SERVER['QUERY_STRING'];
header("Location: ".($url??php_self()).$qs);
return true;
}
function redirectp($url): bool {
header("Location: $url", true, 301);
return true;
}
function redirectp_wqs($url): bool {
$qs = empty($_SERVER['QUERY_STRING']) ? "" : "?".$_SERVER['QUERY_STRING'];
header("Location: $url$qs", true, 301);
return true;
}
function redirect_not_found(string $url = '/', string $msg = ''): bool {
header404($msg);
redirect($url);
return true;
}
function redirect_referer(string $default = "/"): bool {
if(empty($_SERVER['HTTP_REFERER'])){
return redirect($default);
} else {
return redirect($_SERVER["HTTP_REFERER"]);
}
}
function floatpoint(mixed $val): float {
$val = preg_replace('/[^0-9,\.\-]/', '', (string)$val);
return (float)str_replace(',', '.', (string)$val);
}
function to_float(mixed $data): mixed {
return __object_map($data, function(mixed $item): float {
return floatpoint($item);
});
}
function to_int(mixed $data): mixed {
return __object_map($data, function(mixed $item): int {
return (int)$item;
});
}
function money_conv(mixed $data): float {
return floatpoint($data);
}
function money_round(mixed $data): string {
return number_format(money_conv($data), 2, '.', '');
}
function to_money(mixed $data): mixed {
return __object_map($data, function(mixed $item): float {
return money_conv($item);
});
}
function to_range($val, $range, $default = ''){
$range_a = preg_split('//', $range);
if(!$val || !in_array($val, $range_a)){
$val = $default;
}
return $val;
}
function upload_save($id, $save_path){
$f = upload($id);
return $f && $f['tmp_name'] && move_uploaded_file($f['tmp_name'], $save_path);
}
function upload_errormsg($id){
$f = upload($id);
if(empty($f['error'])){
return "";
}
switch($f['error']){
case UPLOAD_ERR_INI_SIZE:
$errormsg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case UPLOAD_ERR_FORM_SIZE:
$errormsg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case UPLOAD_ERR_PARTIAL:
$errormsg = 'The uploaded file was only partially uploaded.';
break;
case UPLOAD_ERR_NO_FILE:
$errormsg = 'No file was uploaded.';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$errormsg = 'Missing a temporary folder.';
break;
case UPLOAD_ERR_CANT_WRITE:
$errormsg = 'Failed to write file to disk.';
break;
default:
$errormsg = 'Unknow error.';
break;
}
return $errormsg;
}
function numpad($data, int $size = 2): string {
return str_pad($data, $size, "0", STR_PAD_LEFT);
}
function md5uniqid(): string {
return md5(uniqid((string)rand(), true));
}
function browse_tree($path, callable $function){
foreach(scandir($path) as $entry){
($r = $function($path, $entry, $function)) && $ret[$entry] = $r;
}
return $ret??[];
}
function browse_flat($path, callable $function){
$ret = [];
foreach(scandir($path) as $entry){
($r = $function($path, $entry, $function)) && $ret = array_merge($ret, is_array($r) ? $r : [$r]);
}
return $ret??[];
}
function compacto($data): mixed {
return __object_filter($data, function($item){
return (bool)$item;
});
}
function utf2win(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string|false =>
mb_convert_encoding($item, 'ISO-8859-13', 'UTF-8')
));
}
function win2utf(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string|false =>
mb_convert_encoding($item, 'UTF-8', 'ISO-8859-13')
));
}
function translit(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string|false =>
iconv("utf-8","ascii//TRANSLIT", $item)
));
}
function is_empty(mixed $data): bool {
return __object_reduce($data, function(bool $carry, mixed $item): bool {
return $carry && empty($item);
}, true);
}
function non_empty(mixed $data): bool {
return !is_empty($data);
}
function ent(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string =>
htmlentities($item)
));
}
function entdecode(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string =>
html_entity_decode($item)
));
}
# https://www.gyrocode.com/articles/php-urlencode-vs-rawurlencode/
# scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
# If you are encoding *path* segment, use rawurlencode().
# If you are encoding *query* component, use urlencode().
function urlenc(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string =>
urlencode($item)
));
}
function urldec(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string =>
urldecode($item)
));
}
function rawurlenc(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string =>
rawurlencode($item)
));
}
function rawurldec(mixed $data): mixed {
return __object_map($data, stringwrap(fn(string $item): string =>
rawurldecode($item)
));
}
##
function specialchars(mixed $data){
return __object_map($data, stringwrap(fn(string $item): string =>
htmlspecialchars(string: $item, double_encode: false)
));
}
function date_in_periods($date, array $periods): bool {
if(empty($periods)){
return false;
}
$ts = empty($date) ? time() : strtotime($date);
if(($ts === false) || ($ts > time())){
return false;
}
foreach($periods as list($s, $e)){
$e = is_null($e) ? time() : strtotime($e);
$s = strtotime($s);
if(($ts >= $s) && ($ts <= $e)){
return true;
}
}
return false;
}
function date_monthstamp(){
return date('Ym');
}
function date_datestamp(){
return date('Ymd');
}
function date_timestamp(){
return date('YmdHi');
}
function get_date_format(){
return dqdp::$DATE_FORMAT;
}
function set_date_format($f){
return dqdp::$DATE_FORMAT = $f;
}
function timef($ts = null){
return date(get_time_format(), $ts ?? time());
}
function get_time_format(){
return dqdp::$TIME_FORMAT;
}
function set_time_format($f){
return dqdp::$TIME_FORMAT = $f;
}
function datef(int $ts = null): string {
return date(get_date_format(), $ts ?? time());
}
function date_today(): string {
return datef(time());
}
function date_yesterday(): string {
return datef(strtotime('yesterday'));
}
function date_daycount(int $m = null, int $y = null): int {
return (int)($m ? (date('t', mktime(0,0,0, $m, 1, ($y ? $y : (int)date('Y'))))) : date('t'));
}
function date_month_start(): string {
return datef(strtotime("first day of this month"));
}
function date_month_end(): string {
return datef(strtotime("last day of this month"));
}
function date_lastmonth_start(): string {
return datef(strtotime("first day of previous month"));
}
function date_lastmonth_end(): string {
return datef(strtotime("last day of previous month"));
}
function is_valid_date($date): bool {
return strtotime($date) !== false;
}
function ustrftime(string $format, int $timestamp = 0): string {
return win2utf(strftime($format, $timestamp));
}
# quarter month
function date_qt_month(int $C, int $m = 1): int {
return ($C - 1) * 3 + $m;
}
// function date_startend($D): array {
// $DATE = eoe($D);
// $format = get_date_format();
// $start_date = $end_date = false;
// $ceturksnis = false;
// for($i = 1; $i < 5; $i++){
// if($DATE->{"C$i"}){
// $ceturksnis = $i;
// }
// }
// if($ceturksnis){
// // $start_date = mktime(0,0,0, ($ceturksnis - 1) * 3 + 1, 1, date('Y'));
// // $days_in_end_month = date_daycount(($ceturksnis - 1) * 3 + 3);
// // $end_date = mktime(0,0,0, ($ceturksnis - 1) * 3 + 3, $days_in_end_month, date('Y'));
// $start_date = mktime(0,0,0, date_qt_month($ceturksnis, 1), 1, (int)date('Y'));
// $days_in_end_month = date_daycount(date_qt_month($ceturksnis, 3));
// $end_date = mktime(0,0,0, date_qt_month($ceturksnis, 3), $days_in_end_month, (int)date('Y'));
// } elseif($DATE->PREV_YEAR) {
// $start_date = strtotime('first day of January last year');
// $end_date = strtotime('last day of December last year');
// } elseif($DATE->THIS_YEAR){
// $start_date = strtotime('first day of January');
// $end_date = time();
// } elseif($DATE->TODAY) {
// $start_date = $end_date = strtotime('today');
// } elseif($DATE->YESTERDAY) {
// $start_date = $end_date = strtotime('yesterday');
// } elseif($DATE->THIS_WEEK) {
// $start_date = strtotime("last Monday");
// $end_date = time();
// } elseif($DATE->THIS_MONTH) {
// $start_date = strtotime("first day of");
// $end_date = time();
// } elseif($DATE->PREV_MONTH) {
// $start_date = strtotime("first day of previous month");
// $end_date = strtotime("last day of previous month");
// } elseif($DATE->PREV_30DAYS){
// $start_date = strtotime("-30 days");
// $end_date = time();
// } elseif($DATE->MONTH){
// if(empty($DATE->YEAR))$DATE->YEAR = date('Y');
// $dc = date_daycount((int)$DATE->MONTH, (int)$DATE->YEAR);
// $start_date = strtotime("$DATE->YEAR-$DATE->MONTH-01");
// $end_date = strtotime("$DATE->YEAR-$DATE->MONTH-$dc");
// } elseif($DATE->YEAR){
// $start_date = strtotime("first day of January $DATE->YEAR");
// $end_date = strtotime("last day of December $DATE->YEAR");
// } else {
// if($DATE->START)$start_date = strtotime($DATE->START);
// if($DATE->END)$end_date = strtotime($DATE->END);
// }
// if($start_date)$start_date = date($format, $start_date);
// if($end_date)$end_date = date($format, $end_date);
// return [$start_date, $end_date];
// }
function php_self(){
return $_SERVER['REQUEST_URI'] ?? '';
}
# TODO: query klasē
function queryl($format = '', $allowed = []){
return __query($_SERVER['QUERY_STRING'] ?? '', $format, '&', $allowed);
}
function query($format = '', $allowed = []){
return __query($_SERVER['QUERY_STRING'] ?? '', $format, '&', $allowed);
}
function __query($query_string = '', $format = '', $delim = '&', $allowed = []){
parse_str($query_string, $QS);
if(is_array($format)){
$FORMAT = $format;
} else {
parse_str($format, $FORMAT);
}
foreach($allowed as $k=>$v){
unset($QS[$k]);
}
foreach($FORMAT as $k=>$v){
if($k[0] == '-'){
$k2 = substr($k, 1);
if(!$v || $v == $QS[$k2]){
unset($QS[$k2]);
}
} else {
$QS[$k] = $v;
}
}
// $ret = [];
// foreach($QS as $k=>$v){
// $ret[] = "$k=$v";
// }
// $q1 = join($delim, $ret);
$q2 = http_build_query($QS, "", $delim);
// print "\n$q1\n$q2\n";
// die;
return $q2;
}
function format_debug(mixed $item): mixed {
// return __object_map($v, function($item) {
if((is_string($item) || $item instanceof Stringable) && mb_detect_encoding($item)){
return mb_substr($item, 0, 4096).(mb_strlen($item) > 4096 ? '...' : '');
} elseif(is_bool($item)){
return $item ? "true" : "false";
} elseif(is_scalar($item)){
return $item;
} elseif(is_null($item)) {
return "NULL";
} elseif(is_resource($item)) {
return "$item";
} elseif(is_array($item)) {
return "[ARRAY]";
} elseif(is_object($item) && method_exists($item , '__toString')) {
return "$item";
} elseif(is_callable($item)) {
return "[FUNC]";
} else {
return "[BLOB]";
}
// });
}
# NOTE: dep on https://highlightjs.org/
function sqlr(){
print is_climode() ? "\n" : '<pre style="background: gainsboro; color: black">';
print ($t = debug_backtrace()) ? __back_trace_fmt($t[0])."\n------------------------------------------------------------------------------\n" : '';
if(!is_climode())print '<code class="sql" style="background: gainsboro;">';
__output_wrapper(function($v){
if($v instanceof dqdp\SQL\Statement){
print (string)$v;
if(method_exists($v, 'getVars')){
print ("\n\n--[Bind vars]\n");
if($vars = $v->getVars()){
foreach($vars as $k=>$var){
printf("--[%s] = %s\n", $k, format_debug($var));
}
} else {
print "-- none --";
}
// printf("\n--Finished in: %.3f sec", $v->end_ts - $v->start_ts);
}
} else {
print_r(format_debug($v));
}
}, ...func_get_args());
print is_climode() ? "\n------------------------------------------------------------------------------\n" : "</code></pre>";
print "\n";
}
function dumpr(){
__pre_wrapper('var_dump', ...func_get_args());
}
function printr(){
__pre_wrapper('print_r', ...func_get_args());
}
function __pre_wrapper(callable $func, ...$args){
global $DQDP_DEBUG;
if(isset($DQDP_DEBUG) && !$DQDP_DEBUG)
{
return;
}
print is_climode() ? "\n" : '<pre style="background: gainsboro; color: black">';
print ($t = debug_backtrace()) ? __back_trace_fmt($t[1])."\n------------------------------------------------------------------------------\n" : '';
if(!is_climode())print '<code class="accesslog" style="background: gainsboro;">';
__output_wrapper($func, ...$args);
if(!is_climode())print "</code>";
print is_climode() ? "\n------------------------------------------------------------------------------\n" : "</pre>";
print "\n";
}
function __output_wrapper(callable $func, ...$args){
$c = count($args);
if(is_climode()){
for($i = 0; $i < $c; $i++){
if($i > 0)print "\n";
$func($args[$i]);
}
} else {
for($i = 0; $i < $c; $i++){
if($i > 0)print "\n";
ob_start();
$func($args[$i]);
print htmlspecialchars(string: ob_get_clean(), double_encode: false);
}
}
}
function __back_trace_fmt($t){
return sprintf("called '%s' in '%s' on line %d", $t['function'], $t['file'], $t['line']);
}
function printrr(){
ob_start();
call_user_func_array('printr', func_get_args());
return ob_get_clean();
}
function dumprr(){
ob_start();
call_user_func_array('dumpr', func_get_args());
return ob_get_clean();
}
function br2nl($text){
return preg_replace('/<br\\\\s*?\\/??>/i', "\\n", $text);
}
function js_bool($js_bool){
if($js_bool === 'true')
$ret = true;
elseif($js_bool === 'on')
$ret = true;
else
$ret = false;
return $ret;
}
function mb_streqi($s1, $s2){
return mb_strtoupper($s1) === mb_strtoupper($s2);
}
function net2cidr($mask){
return array_search($mask, netmasks());
}
function cidr2net($bits){
return netmasks()[$bits] ?? false;
}
function net2long($ip){
return ip2long($ip);
}
function long2net($net){
return long2ip($net);
}
# $ip = 192.168.1.2
# $network = 192.168.1.0/24
function ipInNet($pIp, $pNetwork){
list($net, $cidr) = explode('/', $pNetwork);
$ipLong = net2long($pIp);
$netLong = net2long($net);
$mask = net2long(cidr2net($cidr));
return ($ipLong & $mask) == ($netLong & $mask);
}
function array_sort_len($a){
array_multisort(array_map('strlen', $a), SORT_NUMERIC, SORT_DESC, $a);
return $a;
}
# TODO: SORT_DESC, SORT_NUMERIC parametros
function array_sort_byk($a, $k){
$ka = array_map(function($i) use ($k){
return $i[$k];
}, $a);
array_multisort($ka, SORT_DESC, SORT_NUMERIC, $a);
return $a;
}
function array_insert_after(array $a, $v1, $v2){
if(($pos = array_search($v1, $a)) === false){
return $a;
}
return array_merge(
array_slice($a, 0, $pos + 1),
[$v2],
array_slice($a, $pos + 1));
}
function array_insert_afterr(array &$a, $v1, $v2){