summaryrefslogtreecommitdiff
path: root/public/js/cassis.js
blob: aee3b6201c655e9d9db6ec12c6161efe1c905867 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
/* <!--
   cassis.js Copyright 2008-2015 Tantek Çelik http://tantek.com 
   http://cassisproject.com conceived:2008-254; created:2009-299;
   license:http://creativecommons.org/licenses/by-sa/3.0/         -->
if you see this in the browser, you need to wrap your PHP include of cassis.js and use thereof with calls to ob_start and ob_end_clean, e.g.:
ob_start(); 
include 'cassis.js'; 
// your code that calls CASSIS functions goes here
ob_end_clean(); 
/* <!-- <?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;
}

// -------------------------------------------------------------------
// date time functions

function date_get_full_year($d = "") {
  if ($d == "") {
    $d = new DateTime();
  }
  return $d->format('Y');
} 

function date_get_timestamp($d) { 
  return $d->format('U'); // $d->getTimestamp(); // in PHP 5.3+
}

// mixed-case function names are bad for PHP vs JS. Don't do it.
//function Number($n) {
//  return $n-0;
//}


// -------------------------------------------------------------------
// 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_space(s) {
 return /\s/.test(s);
}

// -------------------------------------------------------------------
// date time functions

function date_create(s) {
  var d = new Date();
  d.parse(s);
  return d;
}

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);
}


// -------------------------------------------------------------------
// 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 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 = [["&","&amp;"],  ["<","&lt;"], [">","&gt;"], 
       ["'","&#039;"], ['"',"&quot;"]];
  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 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 


// -------------------------------------------------------------------
// 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 preg_match_all() { }
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 */
/// ?> <!--   ///
// -------------------------------------------------------------------
// javascript-only framework functions

function doevent(el, evt) {
  if (evt==="click" && el.tagName==='A') {
  // dispatch/fireEvent fails FF3.5+/IE8+ on [a href] w "click" event
    window.location = el.href; // workaround
    return true;
  }
  if (document.createEvent) {
    var eo = document.createEvent("HTMLEvents");
    eo.initEvent(evt, true, true);
    return !el.dispatchEvent(eo);
  } 
  else if (document.createEventObject) {
    return el.fireEvent("on"+evt);
  }
}

function targetelement(e) {
  var t;
  e = e ? e : window.event;
  t = e.target ? e.target : e.srcElement;
  t = (t.nodeType == 3) ? t.parentNode : t; // Safari workaround
  return t;
}

// CommonJS (node, browserify, npm) Exports
if (typeof exports !== 'undefined' &&
    typeof module !== 'undefined' && module.exports) {
  exports.auto_link = auto_link;
  exports.num_to_sxg = num_to_sxg;
  exports.sxg_to_num = sxg_to_num;
}

/// --> <?php ///


// -------------------------------------------------------------------
// character and string functions

// strcat: takes as many strings as you want to give it.
function strcat() {         /// ?> <!--   ///
  var $args, $i, $isjs, $r; /// --> <?php ///
  $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;
}

// define a few JS functions that PHP already has, using CASSIS funcs
/// ?> <!--   ///
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"));
}
/// --> <?php ///

function preg_match_1($p, $s) { /// ?> <!--   ///
  var $m;                       /// --> <?php ///
  if (js()) {
    return $s.match(new RegExp(trim_slashes($p), "i"));
  } else {
    $m = array();
    if (preg_match($p, $s, $m) !== FALSE) {
      return $m[0];
    } else {
      return null; //array();
    }
  }
}

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 preg_matches($p, $s) { /// ?> <!--   ///
  var $m;                       /// --> <?php ///
  if (js()) {
    return $s.match(new RegExp(trim_slashes($p), "gi"));
  } else {
    $m = array();
    if (preg_match_all($p, $s, $m, PREG_PATTERN_ORDER) !== FALSE) {
      return $m[0];
    } else {
      return null; //array();
    }
  }
}

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) { /// ?> <!--   ///
  var $d, $m, $p, $s;     /// --> <?php ///
  $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) { /// ?> <!--   ///
  var $c, $i, $j, $m, $n; /// --> <?php ///
  $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);
}

// -------------------------------------------------------------------
// date and time

function date_create_ymd($s) { /// ?> <!--   ///
  var $d;                      /// --> <?php ///
  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) {
  if (js()) {
    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');
  } else {
    return date_format($d, 'c');
  }
}

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) { /// ?> <!--   ///
  var $md;                       /// --> <?php ///
  $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) {
  if (js()) {
    return ymdp_to_d($d.getFullYear(), 1+$d.getMonth(), $d.getDate());
  } else {
    return 1+date_format($d, 'z');
  }
}

function bim_from_od($d) {
  return 1+floor(($d-1)/61);
}

function date_get_bim() { /// ?> <!--   ///
  var $args;              /// --> <?php ///
  $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) { /// ?> <!--   ///
  var $a;                 /// --> <?php ///
  $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() { /// ?> <!--   ///
  var $args, $d;                   /// --> <?php ///
  $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) { /// ?> <!--   ///
  var $md, $m;                /// --> <?php ///
  $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) { /// ?> <!--   ///
  var $a, $y;             /// --> <?php ///
  $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)));
}

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 is_http_uri($uri) {
  $uri = explode(":", $uri, 2);
  return !!strncmp($uri[0], "http", 4);
}

// -------------------------------------------------------------------
// compat as of 2011-149
function webaddresstouri($wa, $addhttp) { 
  return web_address_to_uri($wa, $addhttp);
}
function uriclean($uri) { return uri_clean($uri); }


// -------------------------------------------------------------------
// hexatridecimal

function num_to_hxt($n) { /// ?> <!--   ///
  var $d, $m, $s;         /// --> <?php ///
  $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) { /// ?> <!--   ///
  var $c, $i, $j, $n;     /// --> <?php ///
  $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) { /// ?> <!--   ///
  var $d, $f, $i;            /// --> <?php ///
  $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; 
  }
}

function contains($h, $n) {
// HyperTalk syntax haystack contains needle: if ("abc" contains "b")
  return strpos($h, $n)!==false;
}

function last_character_of($s) {
  return (strlen($s) > 0) ? $s[strlen($s)-1] : '';
}

function line_1_of($s) { /// ?> <!--   ///
  var $r;                /// --> <?php ///
  $r = preg_match_1('/^[\\w\\W]*?[\\n\\r]+?/', $s);
  if ($r === null) { return $s; }
  return substr($r, 0, strlen($r)-1);
}

function delete_line_1_of($s) {
 if (preg_match_1('/^[\\w\\W]*?[\\n\\r]+?/', $s) === null) {
   return '';
 }
 return preg_replace_1('/^[\\w\\W]*?[\\n\\r]+?/', '', $s); 
}

// -------------------------------------------------------------------
// microformats

// xpath expressions to extract microformats
function xp_has_class($s) {
  return strcat("//*[contains(concat(' ',@class,' '),' ",$s," ')]");
}

function xpr_has_class($s) {
  return strcat(".//*[contains(concat(' ',@class,' '),' ",$s," ')]");
}

function xp_has_id($s) {
  return strcat("//*[@id='", $s, "']");
}

function xp_attr_starts_with($a, $s) {
  return strcat("//*[starts-with(@", $a, ",'", $s, "')]");
}

function xp_has_rel($s) {
  return strcat("//*[contains(concat(' ',@rel,' '),' ", $s, " ')]");
}

function xpr_has_rel($s) {
  return strcat(".//*[contains(concat(' ',@rel,' '),' ", $s, " ')]");
}

function xpr_attr_starts_with_has_rel($a, $s, $r) {
  return strcat(".//*[contains(concat(' ',@rel,' '),' ", $r, 
                " ') and starts-with(@", $a, ",'", $s, "')]");
}

// value class pattern readable date time from ISO8601 datetime
function vcp_dt_readable($d) { /// ?> <!--   ///
  var $r;                      /// --> <?php ///
  $d = explode("T", $d);
  $r = "";
  if (count($d)>1) { 
     $r = explode("-", $d[1]);
     if (count($d)==1) {
			 $r = explode("+", $d[1]);
     }
     if (count($d)>1) {
       $r = strcat('<time class="value" datetime="',$d[1],'">', 
                   $r[0],'</time> on ');
     }
     else {
       $r = strcat('<time class="value">',$d[1],'</time> on ');
     }
  }
  return strcat($r, '<time class="value">', $d[0], '</time>');
}


// -------------------------------------------------------------------
// compat as of 2011-149
function xphasclass($s) { return xp_has_class($s); }
function xprhasclass($s) { return xpr_has_class($s); }
function xphasid($s) { return xp_has_id($s); }
function xpattrstartswith($a, $s) { 
  return xp_attr_starts_with($a, $s); 
}
function xphasrel($s) { return xp_has_rel($s); }
function xprhasrel($s) { return xpr_has_rel($s); }
function xprattrstartswithhasrel($a, $s, $r) {
  return xpr_attr_starts_with_has_rel($a, $s, $r);
}
function vcpdtreadable($d) { return vcp_dt_readable($d); }


// -------------------------------------------------------------------
// whistle
// algorithmic URL shortener core
// YYYY/DDD/tnnn to tdddss 
// ordinal date, type, decimal #, to sexagesimal epoch days, sexagesimal #
function whistle_short_path($p) {
  return strcat(substr($p, 9, 1),
                ((substr($p, 9, 1)!=='t') ? "/" : ""),
                yd_to_sdf(substr($p, 0, 8), 3),
                num_to_sxg(substr($p, 10, 3)));
}

// -------------------------------------------------------------------
// falcon

function html_unesc_amp_only($s) {
  return str_ireplace('&amp;', '&', $s);
}

function html_esc_amper_once($s) {
  return str_ireplace('&', '&amp;', html_unesc_amp_only($s));
}

function html_esc_amp_ang($s) {
  return str_ireplace('<', '&lt;',
         str_ireplace('>', '&gt;', html_esc_amper_once($s)));
}

function ellipsize_to_word($s, $max, $e, $min) { /// ?> <!--   ///
  var $elen, $i, $slen;                          /// --> <?php ///
  if (strlen($s)<=$max) {
    return $s; // no need to ellipsize
  }

  $elen = strlen($e);
  $slen = $max-$elen;

  // if last characters before $max+1 are ': ', truncate w/o ellipsis.
  // no need to take length of ellipsis into account
  if ($e==='...') {
    for ($i=1; $i<=$elen+1; $i+=1) {
      if (substr($s, $max-$i, 2)===': ') {
        return substr($s, 0, $max-$i+1);
      }
    }
  }

  if ($min) {
    // if a non-zero minimum is provided, then
    // find previous space or word punctuation to break at.
    // do not break at %`'"&.!?^ - reasons why to be documented.
    while ($slen>$min && 
           !contains('@$ -~*()_+[]{}|;,<>', $s[$slen-1])) {
      $slen-=1;
    }
  }
  // at this point we've got a min length string, 
  // only do minimum trimming necessary to avoid a punctuation error.
  
  // trim slash after colon or slash
  if ($s[$slen-1]==='/' && $slen > 2) {
    if ($s[$slen-2]===':') {
      $slen-=1;    
    }
    if ($s[$slen-2]==='/') {
      $slen-=2;
    }
  }

  //if trimmed at a ":" in a URL, trim the whole thing
    //or trimmed at "http", trim the whole URL
  if ($s[$slen-1]===':' && $slen > 5 && 
      substr($s, $slen-5, 5)==='http:') {
    $slen -= 5;
  } else if ($s[$slen-1]==='p' && $slen > 4 &&
             substr($s, $slen-4, 4)==='http') {
    $slen -= 4;
  } else if ($s[$slen-1]==='t' && $slen > 4 &&
             (substr($s, $slen-3, 4)==='http' || 
              substr($s, $slen-3, 4)===' htt')) {
    $slen -= 3;
  } else if ($s[$slen-1]==='h' && $slen > 4 &&
             substr($s, $slen-1, 4)==='http') {
    $slen -= 1;
  }
  
  //if char immediately before ellipsis would be @$ then trim it
  if ($slen > 0 && contains('@$', $s[$slen-1])) {
    $slen-=1;
  }
 
  //if char before ellipsis would be sentence terminator, trim 2 more
  while ($slen > 1 && contains('.!?', $s[$slen-1])) {
    $slen-=2;
  }

  // trim extra whitespace before ellipsis down to one space
  if ($slen > 2 && contains("\n\r ", $s[$slen-1])) {
    while (contains("\n\r ", $s[$slen-2]) && $slen > 2) {
      --$slen;
    }
  }

  if ($slen < 1) { // somehow shortened too much
    return $e; // or ellipsis by itself exceeded max, return ellipsis.
  }

  // if last two chars are ': ', omit ellipsis. 
  if ($e==='...' && substr($s, $slen-2, 2)===': ') {
    return substr($s, 0, $slen);
  }

  return strcat(substr($s, 0, $slen), $e);
}

function trim_leading_urls($s) {
  // deliberately trim URLs with explicit http: / https: from start
  // keep schemeless URLs, @-names as expected user-visible text
  // if empty or just space after trimming, just return original
  $r = trim($s);
  while (substr($r, 0, 5) == 'http:' || substr($r, 0, 6) == 'https:')
  {
    $ws = offset(' ', $r);
    $rs = offset("\r", $r);
    if ($rs == 0) { $rs = offset("\n", $r); }
    if ($rs != 0 && $rs < $ws) { $ws = $rs; }
    if ($ws == 0) { return $s; }
    $r = substr($r, $ws, strlen($r)-$ws);
  }
  $r = trim($r);
  return ((strlen($r) > 0) ? $r : $s);
}

function auto_space($s) {
// replace linebreaks with <br class="auto-break"/>
//  and one leading space with &nbsp;
// replace "  " with " &nbsp;"
// replace leading spaces (on a line or before spaces) with nbsp;
  if ($s[0] === ' ') {
    $s = strcat('&#xA0;', substr($s, 1, strlen($s)-1));
  }
  return str_ireplace(array("\r\n", "\r", "\n ", "\n", "  "),
                      array("\n", "\n", '<br class="auto-break"/>&#xA0;',
                            '<br class="auto-break"/>',
                            ' &#xA0;'),
                      $s);
}

function auto_link_re() {
  return '/(?:\\@[_a-zA-Z0-9]{1,17})|(?:(?:(?:(?:http|https|irc)?:\\/\\/(?:(?:[!$&-.0-9;=?A-Z_a-z]|(?:\\%[a-fA-F0-9]{2}))+(?:\\:(?:[!$&-.0-9;=?A-Z_a-z]|(?:\\%[a-fA-F0-9]{2}))+)?\\@)?)?(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*\\.)+(?:(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])|(?:biz|b[abdefghijmnorstvwyz])|(?:cat|com|coop|c[acdfghiklmnoruvxyz])|d[ejkmoz]|(?:edu|e[cegrstu])|f[ijkmor]|(?:gov|g[abdefghilmnpqrstuwy])|h[kmnrtu]|(?:info|int|i[delmnoqrst])|j[emop]|k[eghimnrwyz]|l[abcikrstuvy]|(?:mil|museum|m[acdeghklmnopqrstuvwxyz])|(?:name|net|n[acefgilopruz])|(?:org|om)|(?:pro|p[aefghklmnrstwy])|qa|r[eouw]|(?:space|s[abcdeghijklmnortuvyz])|(?:tel|travel|t[cdfghjklmnoprtvwz])|u[agkmsyz]|v[aceginu]|(?:wtf|w[fs])|xyz|y[etu]|z[amw]))|(?:(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])))(?:\\:\\d{1,5})?)(?:\\/(?:(?:[!#&-;=?-Z_a-z~])|(?:\\%[a-fA-F0-9]{2}))*)?)(?=\\b|\\s|$)/';
  // ccTLD compressed regular expression clauses (re)created.
  // .mobi .jobs deliberately excluded to discourage layer violations.
  // see http://flic.kr/p/2kmuSL for more on the problematic new gTLDs
  // part of $re derived from Android Open Source Project, Apache 2.0
  // with a bunch of subsequent fixes/improvements (e.g. ttk.me/t44H2)
  // thus auto_link_re is also Apache 2.0 licensed
  //  http://www.apache.org/licenses/LICENSE-2.0
  // - Tantek 2010-046 (moved to auto_link_re 2012-062)
}

// auto_link: param 1: text; 
//  optional: param 2: do embeds or not (false),
//            param 3: do auto_links or not (true)
// auto_link is idempotent, works on plain text or typical markup.
function auto_link() { 
  /// ?> <!--   ///
  var $args, $afterchar, $afterlink, $do_embed, $fe, $i, $isjs, 
      $mi, $mlen, $ms, $re, 
      $sp, $spe, $spliti, $t, $wmi, $yvid; 
  /// --> <?php ///

  $isjs = js();
  $args = $isjs ? arguments : func_get_args();
  if (count($args) === 0) {
    return '';
  }
  $t = $args[0];
  $do_embed = (count($args) > 1) && ($args[1]!==false);
  $do_link = (count($args) < 3) || ($args[2]!==false);

  $re = auto_link_re();
  $ms = preg_matches($re, $t);
  if (!$ms) {
    return $t;
  }

  $mlen = count($ms);
  $sp = preg_split($re, $t);
  $t = "";
  $sp[0] = string($sp[0]); // force undefined to ""
  for ($i=0; $i<$mlen; $i+=1) {
    $mi = $ms[$i];
    $spliti = $sp[$i];
    $t = strcat($t, $spliti);
    $sp[$i+1] = string($sp[$i+1]); // force undefined to ""
    if (substr($sp[$i+1], 0, 1)==='/') { //regex omits end/ before </a
      $sp[$i+1] = substr($sp[$i+1], 1, strlen($sp[$i+1])-1);
      $mi = strcat($mi, '/'); // include / in the match
    }
    $spe = substr($spliti, -2, 2);
    // avoid 2x-linking, don't link CSS @-rules, attr values, asciibet
    if ((!$spe || !preg_match('/(?:\\=[\\"\\\']?|t;)/', $spe)) &&
        substr(trim($sp[$i+1]), 0, 3)!=='</a' && 
        (!contains(
'@charset@font@font-face@import@media@namespace@page@ABCDEFGHIJKLMNOPQ@',
            strcat($mi, '@')))) {
      $afterlink = '';
      $afterchar = substr($mi, -1, 1);
      while (contains('.!?,;"\')]}', $afterchar) && // trim punc from
          ($afterchar!==')' || !contains($mi,'('))) { // 1 () pair
        $afterlink = strcat($afterchar, $afterlink);
        $mi = substr($mi, 0, -1);
        $afterchar = substr($mi, -1, 1);
      }
      
      $fe = 0;
      if ($do_embed) {
        $fe = strtolower(
               (substr($mi, -4, 1)==='.') ? substr($mi, -4, 4) 
                                          : substr($mi, -5, 5));
      }
      $wmi = web_address_to_uri($mi, true);
      $prot = protocol_of_uri($wmi);
      $hn = hostname_of_uri($wmi);
      $pa = path_of_uri($wmi);
      $ih = is_http_uri($wmi);

      $ahref = '<span class="figure" style="text-align:left">';
      $enda = '</span>';
			if ($do_link) {
        $ahref = strcat('<a class="auto-link figure" href="',      
                        $wmi, '">');
        $enda = '</a>';
      }

      if ($fe && 
          ($fe === '.jpeg' || $fe === '.jpg' || $fe === '.png' || 
           $fe === '.gif' || $fe === '.svg')) {
        $alt = strcat('a ',
                      (offset('photo', $mi) != 0) ? 'photo' 
                                                  : substr($fe, 1));
        $t = strcat($t, $ahref, '<img class="auto-embed" alt="', 
                    $alt, '" src="', $wmi, '"/>', $enda, $afterlink);
      } else if ($fe && 
                 ($fe === '.mp4' || $fe === '.mov' || 
                  $fe === '.ogv' || $fe === '.webm'))
      {
        $t = strcat($t, $ahref, '<video class="auto-embed" ',
                    'controls="controls" src="', $wmi, '"></video>',
                    $enda, $afterlink);
      } else if ($hn === 'vimeo.com' 
                     && ctype_digit(substr($pa, 1))) {
				if ($do_link) {
				  $t = strcat($t, '<a class="auto-link" href="',
				              'https:', relative_uri_hash($wmi),
                      '">', $mi, '</a> ');
				}

        $t = strcat($t, '<iframe class="vimeo-player auto-embed figure" width="480" height="385" style="border:0" src="', 'https://player.vimeo.com/video/', 
                    substr($pa, 1), '"></iframe>', 
                    $afterlink);
      } else if ($hn === 'youtu.be' ||
                (($hn === 'youtube.com' || $hn === 'www.youtube.com')
                 && ($yvid = offset('watch?v=', $mi)) !== 0)) {
        if ($hn === 'youtu.be') {
          $yvid = substr($pa, 1);
        } else {
          $yvid = explode('&', substr($mi, $yvid+7));
          $yvid = $yvid[0];
        }
				if ($do_link) {
  				$t = strcat($t, '<a class="auto-link" href="',
  				            'https:', relative_uri_hash($wmi),
                      '">', $mi, '</a> ');
        }
        $t = strcat($t, '<iframe class="youtube-player auto-embed figure" width="480" height="385" style="border:0"  src="', 'https://www.youtube.com/embed/', 
                    $yvid, '"></iframe>', 
                    $afterlink);
      } else if ($mi[0] === '@' && $do_link) {
        if ($sp[$i+1][0] === '.' && 
            $spliti != '' &&
            ctype_email_local(substr($spliti, -1, 1))) {
          // if email address, simply append info, no linking
          $t = strcat($t, $mi, $afterlink);
        }
        else {
          // treat it as a Twitter @-username reference and link it
          $t = strcat($t, '<a class="auto-link h-x-username" href="',
                      $wmi, '">', $mi, '</a>', 
                      $afterlink);
        }
      } else if ($do_link) {
        $t = strcat($t, '<a class="auto-link" href="',
                    $wmi, '">', $mi, '</a>', 
                    $afterlink);
      } else {
        $t = strcat($t, $mi, $afterlink);
      }
    } else {
      $t = strcat($t, $mi);
    }
  }
  return strcat($t, $sp[$mlen]);
}


// returns array of URLs after literal "in-reply-to:" in text
function get_in_reply_to_urls($s) {
  /// ?> <!--   ///
  var $irtn, $r, $re, $i, $j,
      $ms, $sp, $afterlink, $ac; 
  /// --> <?php ///
  
  $s = explode('in-reply-to: ', $s);
  $irtn = count($s);
  if ($irtn < 2) { return array(); }
  $r = array();
  $re = auto_link_re();
  for ($i=1; $i<$irtn; $i++) {
    // iterate through all strings after an 'in-reply-to: ' for URLs
    $ms = preg_matches($re, $s[$i]);
    $msn = count($ms);
    if ($ms) {
      $sp = preg_split($re, $s[$i]);
      $j = 0;
      $afterlink = '';
      while ($j<$msn && 
             $afterlink == '' &&
             ($sp[$j] == '' || ctype_space($sp[$j]))) {
        // iterate through space separated URLs and add them to $r
        $m = $ms[$j];
        if ($m[0] != '@') { // skip @-references
          $ac = substr($m, -1, 1);
          while (contains('.!?,;"\')]}', $ac) && // trim punc @ end
              ($ac != ')' || !contains($m, '('))) { 
              // allow one paren pair
              // *** not sure twitter is this smart
              $afterlink = strcat($ac, $afterlink);
              $m = substr($m, 0, -1);
              $ac = substr($m, -1, 1);
          }
          if (substr($m, 0, 6) === 'irc://') { 
            // skip it. no known use of in-reply-to an IRC URL
          } else {
            $r[count($r)] = webaddresstouri($m, true);
          }
        }
        $j++;
      }
    }
  } 
  return $r;
}


// Twitter POSSE support

// replace URLs with https://j.mp/0011235813 to mimic Twitter's t.co
function tw_text_proxy() {
  /// ?> <!--   ///
  var $args, $afterchar, $afterlink, $i, $isjs,
      $mlen, $ms, $re, 
      $sp, $spe, $spliti, $prot, $proxy_url; 
  /// --> <?php ///
  
  $isjs = js();
  $args = $isjs ? arguments : func_get_args();
  if (count($args) === 0) {
    return '';
  }
  $t = $args[0];

  $re = auto_link_re();
  $ms = preg_matches($re, $t);
  if (!$ms) {
    return $t;
  }

  $mlen = count($ms);
  $sp = preg_split($re, $t);
  $t = "";
  $sp[0] = string($sp[0]); // force undefined to ""
  for ($i=0; $i<$mlen; $i++) {
    $mi = $ms[$i];
    $spliti = $sp[$i];
    $t = strcat($t, $spliti);
    $sp[$i+1] = string($sp[$i+1]); // force undefined to ""
    if (substr($sp[$i+1], 0, 1)=='/') { // regex omits '/' before </a
      $sp[$i+1] = substr($sp[$i+1], 1, strlen($sp[$i+1])-1);
      $mi = strcat($mi, '/'); // explicitly include in match
    }
    $spe = substr($spliti, -2, 2);
    // don't proxy @-names, plain ccTLDs
    if ($mi[0]!='@' &&
        (substr($mi, -3, 1) !== '.' || substr_count($mi, '.') > 1)) {
      $afterlink = '';
      $afterchar = substr($mi, -1, 1);
      while (contains('.!?,;"\')]}', $afterchar) && // trim punc @ end
          ($afterchar !== ')' || !contains($mi, '('))) { 
          // allow one paren pair
          // *** not sure twitter is this smart
          $afterlink = strcat($afterchar, $afterlink);
          $mi = substr($mi, 0, -1);
          $afterchar = substr($mi, -1, 1);
      }
      
      $prot = protocol_of_uri($mi);
      $proxy_url = '';
      if ($prot === 'irc:') {
        $proxy_url = $mi; // Twitter doesn't tco irc: URLs
      } else { /* 'https:', 'http:' or presumed for schemeless URLs */ 
        $proxy_url = 'https://j.mp/0011235813';
      }
      $t = strcat($t, $proxy_url, $afterlink);
    }
    else {
      $t = strcat($t, $mi);
    }
  }
  return strcat($t, $sp[$mlen]);
}


// note_length_check:
// checks if $note fits in $maxlen characters.
// if $username is non-empty, checks if RT'd $note fits in $maxlen
// 0 - bad params or other precondition failure error
// 200 - exactly fits max characters with RT if username provided
// 206 - less than max chars with RT if username provided
// 207 - more than RT safe length, but less than tweet max
// 208 - tweet max length but with RT would be over
// 413 - (entity too large) over max tweet length
// strlen('RT @: ') === 6.
function note_length_check($note, $maxlen, $username) {
  /// ?> <!--   ///
  var $note_size_chk_u, $note_size_chk_n;
  /// --> <?php ///

  if ($maxlen < 1) { return 0; }
  
  $note_size_chk_u = $username ? 6 + strlen(string($username)) : 0;
  $note_size_chk_n = strlen(string($note)) + $note_size_chk_u;
  
  if ($note_size_chk_n === $maxlen)                    { return 200; }
  if ($note_size_chk_n < $maxlen)                      { return 206; }
  if ($note_size_chk_n - $note_size_chk_u < $maxlen)   { return 207; }
  if ($note_size_chk_n - $note_size_chk_u === $maxlen) { return 208; }
  return 413;
}

function tw_length_check($t, $maxlen, $username) {
  return note_length_check(tw_text_proxy($t), 
                           $maxlen, $username);
}

function tw_url_to_status_id($u) {
// $u - tweet permalink url
// returns tweet status id string; 0 if not a tweet permalink.
  if (!$u) return 0;
  $u = explode("/", string($u)); // https:,,twitter.com,t,status,nnn
  if ($u[2] != "twitter.com" || 
      $u[4] != "status"      ||
      !ctype_digit($u[5])) {
    return 0;
  }
  return $u[5];
}

function tw_url_to_username($u) {
// $u - tweet permalink url
// returns twitter username; 0 if not a tweet permalink.
  if (!$u) return 0;
  $u = explode("/", string($u)); // https:,,twitter.com,t,status,nnn
  if ($u[2] != "twitter.com" || 
      $u[4] != "status"      ||
      !ctype_digit($u[5])) {
    return 0;
  }
  return $u[3];
}

function fb_url_to_event_id($u) {
// $u - fb event permalink url
// returns fb event id string; 0 if not a fb event permalink.
  if (!$u) return 0;
  $u = explode("/", string($u)); // https:,,fb.com,events,nnn
  if (($u[2] != "fb.com" && $u[2] != "facebook.com" && 
       $u[2] != "www.facebook.com") || 
      $u[3] != "events"      ||
      !ctype_digit($u[4])) {
    return 0;
  }
  return $u[4];
}

// ===================================================================
// end CASSIS v0.1, cassis.js
// ?> -->