-
Notifications
You must be signed in to change notification settings - Fork 10
/
cassis.php
1608 lines (1387 loc) · 43.9 KB
/
cassis.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 // CASSIS v0.1 start -->
// ===================================================================
// PHP-only block. Processed only by PHP. Use only // comments here.
// -------------------------------------------------------------------
function js() {
return false;
}
// global configuration
if (php_min_version("5.1.0")) {
date_default_timezone_set("UTC");
}
function php_min_version($s) {
$s = explode(".", $s);
$phpv = explode(".", phpversion());
for ($i=0; $i < count($s); $i+=1) {
if ($s[$i] > $phpv[$i]) {
return false;
}
}
return true;
}
// -------------------------------------------------------------------
// string functions requiring separate js/php definitions
function preg_matches($p, $s) {
$m = array();
if (preg_match_all($p, $s, $m, PREG_PATTERN_ORDER) !== FALSE) {
return $m[0];
}
else {
return array();
}
}
function preg_match_1($p, $s) {
$m = array();
if (preg_match($p, $s, $m) !== FALSE) {
return $m[0];
} else {
return null; //array();
}
}
// -------------------------------------------------------------------
// date time functions
function date_get_full_year($d = "") {
if ($d == "") {
$d = new DateTime();
}
return $d->format('Y');
}
function date_get_timestamp($d = "") {
if ($d == "") {
$d = new DateTime();
}
return $d->format('U'); // $d->getTimestamp(); // in PHP 5.3+
}
function date_get_ordinal_days($d) {
return 1 + $d->format('z');
}
// mixed-case function names are bad for PHP vs JS. Don't do it.
//function Number($n) {
// return $n-0;
//}
function date_get_rfc3339($d) {
return $d->format('c');
}
// -------------------------------------------------------------------
// old wrappers. transition code away from these
// ** do not use these in new code. **
function getFullYear($d = "") {
// 2010-020 obsoleted. Use date_get_full_year instead
return date_get_full_year($d);
}
// ===================================================================
/*/ // This comment inverter switches from PHP only to JS only.
// JS-only block. Processed only by JS. Use only // comments here.
// -------------------------------------------------------------------
function js() {
return true;
}
// array functions
function array() { // makes an array from arbitrary parameter list.
return Array.prototype.slice.call(arguments);
}
function is_array(a) {
return (typeof(a) === "object") && (a instanceof Array);
}
function count(a) {
return a.length;
}
function array_slice(a, b, e) { // slice an array, begin, optional end
if (a === undefined) { return array(); }
if (b === undefined) { return a; }
if (e === undefined) { return a.slice(b); }
return a.slice(b, e);
}
// -------------------------------------------------------------------
// math and numerical functions
function floor(n) {
return Math.floor(n);
}
function intval(n) {
return parseInt(n, 10);
}
Array.min = function(a) {
// from http://ejohn.org/blog/fast-javascript-maxmin/
return Math.min.apply(Math, a);
};
function min() {
var m = arguments;
if (m.length < 1) {
return false;
}
if (m.length === 1) {
m = m[0];
if (!is_array(m)) {
return m;
}
}
return Array.min(m);
}
function ctype_digit(s) {
return (/^[0-9]+$/).test(s);
}
function ctype_lower(s) {
return (/^[a-z]+$/).test(s);
}
function ctype_space(s) {
return /\s/.test(s);
}
// -------------------------------------------------------------------
// date time functions
function date_create(s) {
if (s) return new Date(s);
else return new Date();
}
function date_get_full_year(d) {
if (arguments.length < 1) {
d = new Date();
}
return d.getFullYear();
}
function date_get_timestamp(d) {
return floor(d.getTime() / 1000);
}
function date_get_rfc3339($d) {
return strcat($d.getFullYear(),'-',
str_pad_left(1 + $d.getUTCMonth(), 2, "0"), '-',
str_pad_left($d.getDate(), 2, "0"), 'T',
str_pad_left($d.getUTCHours(), 2, "0"), ':',
str_pad_left($d.getUTCMinutes(), 2, "0"), ':',
str_pad_left($d.getUTCSeconds(), 2, "0"), 'Z');
}
// newcal
function date_get_ordinal_days($d) {
return ymdp_to_d($d.getFullYear(), 1 + $d.getMonth(), $d.getDate());
}
// -------------------------------------------------------------------
// character and string functions
function ord(s) {
return s.charCodeAt(0);
}
function strlen(s) {
return s.length;
}
function substr(s, o, n) {
var m = strlen(s);
if ((o < 0 ? -1-o : o) >= m) { return false; }
if (o < 0) { o = m + o; }
if (n < 0) { n = m - o + n; }
if (n === undefined) { n = m - o; }
return s.substring(o, o + n);
}
function substr_count(s, n) {
return s.split(n).length - 1;
}
function strpos(h, n, o) {
// clients must triple-equal test return for === false for no match!
// or use offset(n, h) instead (0 = not found, else 1-based index)
if (arguments.length === 2) {
o = 0;
}
o = h.indexOf(n, o);
if (o === -1) { return false; }
else { return o; }
}
function stripos(h, n, o) {
// clients must triple-equal test return for === false for no match!
if (arguments.length === 2) {
o = 0;
}
o = h.toLowerCase().indexOf(n.toLowerCase(), o);
if (o === -1) { return false; }
else { return o; }
}
function strncmp(s1, s2, n) {
s1 = substr(String(s1), 0, n);
s2 = substr(String(s2), 0, n);
return (s1 === s2) ? 0 :
((s1 < s2) ? -1 : 1);
}
function explode(d, s, n) {
if (arguments.length === 2) {
return s.split(d);
}
return s.split(d, n);
}
function implode(d, a) {
return a.join(d);
}
function rawurlencode(s) {
return encodeURIComponent(s);
}
function htmlspecialchars(s) {
var c, i;
c = [["&","&"], ["<","<"], [">",">"],
["'","'"], ['"',"""]];
for (i = 0; i < c.length; i+=1) {
s = s.replace(new RegExp(c[i][0], "g"), c[i][1]);
}
return s;
}
function str_ireplace(a, b, s) {
var i;
if (!is_array(a)) {
return s.replace(new RegExp(a, "gi"), is_array(b) ? b[0] : b);
}
else {
for (i=0; i<a.length; i++) {
s = s.replace(new RegExp(a[i], "gi"), is_array(b) ? b[i] : b);
}
return s;
}
}
function preg_match(p, s) {
return (s.match(trim_slashes(p)) ? 1 : 0);
}
function preg_split(p, s) {
return s.split(new RegExp(trim_slashes(p),"gi"));
}
function trim() {
var c, i, j, m, s;
m = arguments;
s = m[0];
c = count(m) > 1 ? m[1] : " \t\n\r\f\x00\x0b\xa0";
i = 0;
j = strlen(s);
while (strpos(c,s[i])!==false && i<j) {
i+=1;
}
j-=1;
while (j>i && strpos(c,s[j])!==false) {
j-=1;
}
j+=1;
if (j>i) {
return substr(s,i,j-i);
}
else {
return '';
}
}
function rtrim() {
var c,j,m,s;
m = arguments;
s = m[0];
c = count(m)>1 ? m[1] : " \t\n\r\f\x00\x0b\xa0";
j = strlen(s)-1;
while (j>=0 && strpos(c,s[j])!==false) {
j-=1;
}
if (j>=0) {
return substr(s,0,j+1);
}
else {
return '';
}
}
function strtolower(s) {
return s.toLowerCase();
}
function ucfirst(s) {
return s.charAt(0).toUpperCase() + substr(s, 1);
}
// -------------------------------------------------------------------
// more javascript-only php-equivalent functions here
// -------------------------------------------------------------------
// string functions requiring separate js/php definitions
function preg_matches(p, s) {
return s.match(new RegExp(trim_slashes(p), "gi")); // match is a keyword in PHP 8.0
}
function preg_match_1(p, s) {
return s.match(new RegExp(trim_slashes(p), "i")); // match is a keyword in PHP 8.0
}
// -------------------------------------------------------------------
// pacify jslint/jshint
// -- define functions and variables only used in PHP flow.
function func_get_args() { }
var FALSE = false;
var PREG_PATTERN_ORDER;
var STR_PAD_LEFT;
// -- may eventually define these for JS.
function date_format() { }
function str_pad() { }
function DateTime() { }
// ===================================================================
/**/ // unconditional comment closer exits PHP comment block.
// JS+PHP block. Processed by both JS and PHP. /*...*/ comments ok.
// -------------------------------------------------------------------
/* original js/php test - doesn't pass jslint/jshint.
function js() {
return "00"==false;
}
*/
/*global document: false, window: false */
/// ///
// -------------------------------------------------------------------
// character and string functions
// strcat: takes as many strings as you want to give it.
function strcat() { /// ///
$r = "";
$isjs = js();
$args = $isjs ? arguments : func_get_args();
for ($i=count($args)-1; $i>=0; $i-=1) {
$r = $isjs ? $args[$i] + $r : $args[$i] . $r;
}
return $r;
}
function number($s) {
return $s - 0;
}
function string($n) {
if (js()) {
if (typeof($n)==="number") {
return Number($n).toString();
} else if (typeof($n)==="undefined") {
return "";
} else {
return $n.toString();
}
}
else {
return "" . $n;
}
}
function str_pad_left($s1, $n, $s2) {
$s1 = string($s1);
$s2 = string($s2);
if (js()) {
$n -= strlen($s1);
while ($n >= strlen($s2)) {
$s1 = strcat($s2, $s1);
$n -= strlen($s2);
}
if ($n > 0) {
$s1 = strcat(substr($s2, 0, $n), $s1);
}
return $s1;
} else {
return str_pad($s1, $n, $s2, STR_PAD_LEFT);
}
}
function trim_slashes($s) {
if ($s[0]==="/") { // strip unnecessary / delim PHP regex funcs want
return substr($s, 1, strlen($s)-2);
}
return $s;
}
function preg_replace_1($p, $r, $s) {
if (js()) {
return $s.replace(new RegExp(trim_slashes($p), "i"), $r);
}
else {
$r = preg_replace($p, $r, $s, 1);
if ($r !== null) { return $r; }
else { return $s; }
}
}
function ctype_email_local($s) {
// close enough. no '.' because this is used for last char of.
return (preg_match("/^[a-zA-Z0-9_%+-]+$/", $s));
}
function ctype_uri_scheme($s) {
return (preg_match("/^[a-zA-Z][a-zA-Z0-9+.-]*$/", $s));
}
// -------------------------------------------------------------------
// newbase60
function num_to_sxg($n) { /// ///
$m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
$p = "";
$s = "";
if ($n==="" || $n===0) { return "0"; }
if ($n<0) {
$n = -$n;
$p = "-";
}
while ($n>0) {
if(!js() && function_exists('bcmod')) {
$d = bcmod($n, 60);
$s = $m[$d] . $s;
$n = bcdiv(bcsub($n, $d), 60);
} else {
$d = $n % 60;
$s = strcat($m[$d], $s);
$n = ($n-$d)/60;
}
}
return strcat($p, $s);
}
function num_to_sxgf($n, $f) {
if (!$f) { $f=1; }
return str_pad_left(num_to_sxg($n), $f, "0");
}
function sxg_to_num($s) { /// ///
$j = strlen($s);
$m = 1;
$n = 0;
if ($s[0]==="-") {
$m = -1;
$j-=1;
$s = substr($s, 1, $j);
}
for ($i=0; $i<$j; $i+=1) { // iterate from first to last char of $s
$c = ord($s[$i]); // put current ASCII of char into $c
if ($c>=48 && $c<=57) { $c=$c-48; }
else if ($c>=65 && $c<=72) { $c-=55; }
else if ($c===73 || $c===108) { $c=1; } // typo cap I lower l to 1
else if ($c>=74 && $c<=78) { $c-=56; }
else if ($c===79) { $c=0; } // error correct typo capital O to 0
else if ($c>=80 && $c<=90) { $c-=57; }
else if ($c===95 || $c===45) { $c=34; } // _ and dash - to _
else if ($c>=97 && $c<=107) { $c-=62; }
else if ($c>=109 && $c<=122) { $c-=63; }
else { break; } // treat all other noise as end of number
if(!js() && function_exists('bcadd')) {
$n = bcadd(bcmul(60, $n), $c);
} else {
$n = 60*$n + $c;
}
}
return $n*$m;
}
function sxg_to_numf($s, $f) {
if ($f===undefined) { $f=1; }
return str_pad_left(sxg_to_num($s), $f, "0");
}
// -------------------------------------------------------------------
// == newbase60 compat functions only == (before 2011-149)
function numtosxg($n) {
return num_to_sxg($n);
}
function numtosxgf($n, $f) {
return num_to_sxgf($n, $f);
}
function sxgtonum($s) {
return sxg_to_num($s);
}
function sxgtonumf($s, $f) {
return sxg_to_numf($s, $f);
}
/* == end compat functions == */
// -------------------------------------------------------------------
// date and time
function date_create_ymd($s) { /// ///
if (!$s) {
return (js() ? new Date() : new DateTime());
}
if (js()) {
if (substr($s,4,1)==='-') {
$s=strcat(substr($s,0,4), substr($s,5,2), substr($s,8,2));
}
$d = new Date(substr($s,0,4),substr($s,4,2)-1,substr($s,6,2));
$d.setHours(0); // was setUTCHours, avoiding bc JS no default tz
return $d;
} else {
return date_create(strcat($s, " 00:00:00"));
}
}
function date_create_timestamp($s) {
if (js()) {
return new Date(1000*$s);
} else {
return new DateTime(strcat("@", string($s)));
}
}
// function date_get_timestamp($d) // in PHP/JS specific code above.
// function date_get_rfc3339($d) // in PHP/JS specific code above.
function dt_to_time($dt) {
$dt = explode("T", $dt);
if (count($dt)==1) {
$dt = explode(" ", $dt);
}
return (count($dt)>1) ? $dt[1] : "0:00";
}
function dt_to_date($dt) {
$dt = explode("T", $dt);
if (count($dt)==1) {
$dt = explode(" ", $dt);
}
return $dt[0];
}
function dt_to_ordinal_date($dt) {
return ymd_to_yd(dt_to_date($dt));
}
// -------------------------------------------------------------------
// newcal
function isleap($y) {
return ($y % 4 === 0 && ($y % 100 !== 0 || $y % 400 === 0));
}
function ymdp_to_d($y, $m, $d) { /// ///
$md = array(
array(0,31,59,90,120,151,181,212,243,273,304,334),
array(0,31,60,91,121,152,182,213,244,274,305,335));
return $md[number(isleap($y))][$m-1] + number($d);
}
function ymd_to_d($d) {
if (substr($d, 4, 1)==='-') {
return ymdp_to_d(substr($d,0,4),substr($d,5,2),substr($d,8,2));
} else {
return ymdp_to_d(substr($d,0,4),substr($d,4,2),substr($d,6,2));
}
}
function ymdp_to_yd($y, $m, $d) {
return strcat(str_pad_left($y, 4, "0"), '-',
str_pad_left(ymdp_to_d($y, $m, $d), 3, "0"));
}
function ymd_to_yd($d) {
if (substr($d, 4, 1)==='-') {
return ymdp_to_yd(substr($d,0,4),substr($d,5,2),substr($d,8,2));
} else {
return ymdp_to_yd(substr($d,0,4),substr($d,4,2),substr($d,6,2));
}
}
// function date_get_ordinal_days($d) // in PHP/JS specific code above
function bim_from_od($d) {
return 1+floor(($d-1)/61);
}
function date_get_bim() { /// ///
$args = js() ? arguments : func_get_args();
return bim_from_od(
date_get_ordinal_days(
date_create_ymd((count($args) > 0) ? $args[0] : 0)));
}
function get_nm_str($m) { /// ///
$a = array("New January", "New February", "New March", "New April", "New May", "New June", "New July", "New August", "New September", "New October", "New November", "New December");
return $a[($m-1)];
}
function nm_from_od($d) {
return ((($d-1) % 61) > 29) ? 2+2*(bim_from_od($d)-1) : 1+2*(bim_from_od($d)-1);
}
// date_get_ordinal_date: optional date argument
function date_get_ordinal_date() { /// ///
$args = js() ? arguments : func_get_args();
$d = date_create_ymd((count($args) > 0) ? $args[0] : 0);
return strcat(date_get_full_year($d), '-',
str_pad_left(date_get_ordinal_days($d), 3, "0"));
}
// -------------------------------------------------------------------
// begin epochdays
function y_to_days($y) {
// convert y-01-01 to epoch days
return floor(
(date_get_timestamp(date_create_ymd(strcat($y, "-01-01"))) -
date_get_timestamp(date_create_ymd("1970-01-01")))/86400);
}
// convert ymd to epoch days and sexagesimal epoch days (sd)
function ymd_to_days($d) {
return yd_to_days(ymd_to_yd($d));
}
/* old:
function ymd_to_days($d) {
// fails in JS, "2013-03-10" and "2013-03-11" both return 15774
return floor(
(date_get_timestamp(date_create_ymd($d)) -
date_get_timestamp(date_create_ymd("1970-01-01")))/86400);
}
*/
function ymd_to_sd($d) {
return num_to_sxg(ymd_to_days($d));
}
function ymd_to_sdf($d, $f) {
return num_to_sxgf(ymd_to_days($d), $f);
}
// ordinal date (YYYY-DDD) to ymd, epoch days, sexagesimal epoch days
function ydp_to_ymd($y, $d) { /// ///
$md = array(
array(0,31,59,90,120,151,181,212,243,273,304,334,365),
array(0,31,60,91,121,152,182,213,244,274,305,335,366));
$d -= 1;
$m = trunc($d / 29);
if ($md[isleap($y) - 0][$m] > $d) $m -= 1;
$d = $d - $md[isleap($y)-0][$m] + 1;
$m += 1;
return strcat($y, '-', str_pad_left($m, 2, '0'),
'-', str_pad_left($d, 2, '0'));
}
function yd_to_ymd($d) {
return ydp_to_ymd(substr($d, 0, 4), substr($d, 5, 3));
}
function yd_to_days($d) {
return y_to_days(substr($d, 0, 4)) - 1 + number(substr($d, 5, 3));
}
function yd_to_sd($d) {
return num_to_sxg(yd_to_days($d));
}
function yd_to_sdf($d, $f) {
return num_to_sxgf(yd_to_days($d), $f);
}
// convert epoch days or sexagesimal epoch days (sd) to ordinal date
function days_to_yd($d) { /// ///
$d = date_create_timestamp(
date_get_timestamp(
date_create_ymd("1970-01-01")) + $d*86400);
$y = date_get_full_year($d);
$a = date_create_ymd(strcat($y, "-01-01"));
return strcat($y, "-",
str_pad_left(
1 + floor((
date_get_timestamp($d) - date_get_timestamp($a))/86400), 3, "0"));
}
function sd_to_yd($d) {
return days_to_yd(sxg_to_num($d));
}
// -------------------------------------------------------------------
// compat as of 2011-143
function ymdptod($y,$m,$d) { return ymdp_to_d($y,$m,$d); }
function ymdptoyd($y,$m,$d) { return ymdp_to_yd($y,$m,$d); }
function ymdtoyd($d) { return ymd_to_yd($d); }
function bimfromod($d) { return bim_from_od($d); }
function getnmstr($m) { return get_nm_str($m); }
function nmfromod($d) { return nm_from_od($d); }
function ymdtodays($d) { return ymd_to_days($d); }
function ymdtosd($d) { return ymd_to_sd($d); }
function ymdtosdf($d,$f) { return ymd_to_sdf($d, $f); }
function ydtodays($d) { return yd_to_days($d); }
function ydtosd($d) { return yd_to_sd($d); }
function ydtosdf($d,$f) { return yd_to_sdf($d, $f); }
function daystoyd($d) { return days_to_yd($d); }
function sdtoyd($d) { return sd_to_yd($d); }
// -------------------------------------------------------------------
// HTTP
function is_http_header($s) {
return (preg_match_1('/^[a-zA-Z][-\\w]*:/',$s)!==null);
}
// -------------------------------------------------------------------
// webaddress
function web_address_to_uri($wa, $addhttp) {
if (!$wa ||
(substr($wa, 0, 7) === 'http://') ||
(substr($wa, 0, 8) === 'https://') ||
(substr($wa, 0, 6) === 'irc://')) {
return $wa;
}
if ((substr($wa, 0, 7) === 'Http://') ||
(substr($wa, 0, 8) === 'Https://')) {
// handle iOS4 overcapitalization of input entries
return strcat('h', substr($wa, 1, strlen($wa)));
}
// TBI: may want to handle typos as well like:
// missing/extra : or / http:/ http///
// missing letter in protocol: ttps htps htts, ttp htp htt, ir ic rc
// use strtolower(substr($wa, 0, 6)); // handle capitals in URLs
if (substr($wa,0,1) === '@') {
return strcat('https://twitter.com/', substr($wa, 1, strlen($wa)));
}
if ($addhttp) {
$wa = strcat('http://', $wa);
}
return $wa;
}
function uri_clean($uri) {
$uri = web_address_to_uri($uri, false);
// prune the optional http:// for a neater param
if (substr($uri, 0, 7) === 'http://') {
$uri = explode('://', $uri);
$uri = array_slice($uri, 1);
$uri = implode('://', $uri);
}
// URL encode
return str_ireplace("%3A", ":",
str_ireplace("%2F", "/", rawurlencode($uri)));
}
// returns e.g. http:
function protocol_of_uri($uri) {
if (offset(':', $uri) === 0) { return ""; }
$uri = explode(':', $uri, 2);
if (!ctype_uri_scheme($uri[0])) { return ""; }
return strcat($uri[0], ':');
}
// returns e.g. //ttk.me/b/4DY1?seriously=yes#ud
function relative_uri_hash($uri) {
if (offset(':', $uri) === 0) { return ""; }
$uri = explode(':', $uri, 2);
if (!ctype_uri_scheme($uri[0])) { return ""; }
return $uri[1];
}
// returns e.g. ttk.me
function hostname_of_uri($uri) {
$uri = explode('/', $uri, 4);
if (count($uri) > 2) {
$uri = $uri[2];
if (offset(':', $uri) !== 0) {
$uri = explode(':', $uri, 2);
$uri = $uri[0];
}
return $uri;
}
return '';
}
function sld_of_uri($uri) {
$uri = hostname_of_uri($uri);
$uri = explode('.', $uri);
if (count($uri) > 1) {
return $uri[count($uri) - 2];
}
return "";
}
function path_of_uri($uri) {
$uri = explode('/', $uri, 4);
if (count($uri) > 3) {
$uri = array_slice($uri, 3);
$uri = strcat('/', implode('/', $uri));
if (offset('?', $uri) !== 0) {
$uri = explode('?', $uri, 2);
$uri = $uri[0];
}
if (offset('#', $uri) !== 0) {
$uri = explode('#', $uri, 2);
$uri = $uri[0];
}
return $uri;
}
return '/';
}
function prepath_of_uri($uri) {
$uri = explode('/', $uri);
$uri = array_slice($uri, 0, 3);
return implode('/', $uri);
}
function segment_of_uri($n, $u) {
/* nth starting at 1 */
$u = path_of_uri($u);
$u = explode('/', $u);
if ($n>=0 && $n<count($u))
return $u[$n];
else return "";
}
function fragment_of_uri($u) {
if (offset('#', $u) !== 0) {
$u = explode('#', $u, 2);
return $u[1];
}
return "";
}
function is_http_uri($uri) {
$uri = explode(':', $uri, 2);
return !!strncmp($uri[0], 'http', 4);
}
function get_absolute_uri($uri, $base) {
if (protocol_of_uri($uri) != "") { return $uri; }
if (substr($uri, 0, 2) === '//') {
return strcat(protocol_of_uri($base), $uri);
}
if (substr($uri, 0, 1) === '/') {
return strcat(prepath_of_uri($base), $uri);
}
// TBI # relative
return strcat(prepath_of_uri($base), path_of_uri($base), $uri);
}
// -------------------------------------------------------------------
// compat as of 2011-149
function webaddresstouri($wa, $addhttp) {
return web_address_to_uri($wa, $addhttp);
}
function uriclean($uri) { return uri_clean($uri); }
// -------------------------------------------------------------------
// HTTP related
function is_html_type($ct) {
$ct = explode(';', $ct, 2);
$ct = $ct[0];
return ($ct === 'text/html' || $ct === 'application/xhtml+xml');
}
// -------------------------------------------------------------------
// hexatridecimal
function num_to_hxt($n) { /// ///
$m = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$s = "";
if ($n === undefined || $n === 0) { return "0"; }
while ($n>0) {
$d = $n % 36;
$s = strcat($m[$d], $s);
$n = ($n-$d)/36;
}
return $s;
}
function num_to_hxtf($n, $f) {
if ($f === undefined) { $f=1; }
return str_pad_left(num_to_hxt($n), $f, "0");
}
function hxt_to_num($h) { /// ///
$j = strlen($h);
$n = 0;
for ($i=0; $i<$j; $i+=1) { // iterate from first to last char of $h
$c = ord($h[$i]); // put current ASCII of char into $c
if ($c>=48 && $c<=57) { $c=$c-48; } // 0-9
else if ($c>=65 && $c<=90) { $c-=55; } // A-Z
else if ($c>=97 && $c<=122) { $c-=87; } // a-z treat as A-Z
else { $c = 0; } // treat all other noise as 0
$n = 36*$n + $c;
}
return $n;
}
// -------------------------------------------------------------------
// compat as of 2011-149
function numtohxt($n) { return num_to_hxt($n); }
function numtohxtf($n,$f) { return num_to_hxtf($n, $f); }
function hxttonum($h) { return hxt_to_num($h); }
// -------------------------------------------------------------------
// ISBN-10
function num_to_isbn10($n) { /// ///
$n = string($n);
$d = 0;
$f = 2;
for ($i=strlen($n)-1; $i>=0; $i-=1) {
$d += $n[$i]*$f;
$f += 1;
}
$d = 11-($d % 11);
if ($d===10) {$d="X";}
else if ($d===11) {$d="0";}
else {$d=string($d);}
return strcat(str_pad_left($n, 9, "0"), $d);
}
// -------------------------------------------------------------------
// compat as of 2011-149
function numtoisbn10($n) { return num_to_isbn10($n); }
// -------------------------------------------------------------------
// HyperTalk
function trunc($n) { // just an alias from BASIC days
return floor($n);
}
function offset($n, $h) {
$n = strpos($h, $n);
if ($n === false) {
return 0;
} else {
return $n+1;
}