forked from epochtalk/epochtalk_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.php
1823 lines (1616 loc) · 72.1 KB
/
parsing.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
require('parsing_extra.php');
setReasonableValues();
// Parse bulletin board code in a string, as well as smileys optionally.
function parse_bbc($message, $smileys = true, $cache_id = '', $local_disable = array())
{
global $txt, $scripturl, $context, $modSettings, $user_info;
static $bbc_codes = array(), $itemcodes = array(), $no_autolink_tags = array();
$disabled = array();
//theymos - die if it's taking way too long
if(isset($_SERVER["REQUEST_TIME_FLOAT"]) && microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"] > 29 && strlen($message)>500 && php_sapi_name() != 'cli') {
die();
}
// Never show smileys for wireless clients. More bytes, can't see it anyway :P.
if (WIRELESS)
$smileys = false;
elseif ($smileys !== null && ($smileys == '1' || $smileys == '0'))
$smileys = (bool) $smileys;
if (empty($modSettings['enableBBC']) && $message !== false)
{
if ($smileys === true)
parsesmileys($message);
return $message;
}
// Just in case it wasn't determined yet whether UTF-8 is enabled.
if (!isset($context['utf8']))
$context['utf8'] = (empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8';
//theymos - disable links and images on pages where we don't want to send a referer to random people
$disabledsecurity='';
if(isset($_GET['sesc'])) {
$cache_id = '';
$disabled['img']=true;
$disabled['iurl']=true;
$disabled['url']=true;
$disabled['ftp']=true;
$disabledsecurity=' (FORUM: disabled on this page for security.)';
}
if(isset($_GET['patrol'])) {
$cache_id = '';
$disabled['black']=true;
$disabled['color']=true;
}
//theymos - these tags are aways disabled
$disabled['flash'] = true;
$disabled['move'] = true;
// Sift out the bbc for a performance improvement.
if (empty($bbc_codes) || $message === false)
{
/*if (!empty($modSettings['disabledBBC']))
{
$temp = explode(',', strtolower($modSettings['disabledBBC']));
foreach ($temp as $tag)
$disabled[trim($tag)] = true;
}
if (empty($modSettings['enableEmbeddedFlash']))
$disabled['flash'] = true;*/
/* The following bbc are formatted as an array, with keys as follows:
tag: the tag's name - should be lowercase!
type: one of...
- (missing): [tag]parsed content[/tag]
- unparsed_equals: [tag=xyz]parsed content[/tag]
- parsed_equals: [tag=parsed data]parsed content[/tag]
- unparsed_content: [tag]unparsed content[/tag]
- closed: [tag], [tag/], [tag /]
- unparsed_commas: [tag=1,2,3]parsed content[/tag]
- unparsed_commas_content: [tag=1,2,3]unparsed content[/tag]
- unparsed_equals_content: [tag=...]unparsed content[/tag]
parameters: an optional array of parameters, for the form
[tag abc=123]content[/tag]. The array is an associative array
where the keys are the parameter names, and the values are an
array which may contain the following:
- match: a regular expression to validate and match the value.
- quoted: true if the value should be quoted.
- validate: callback to evaluate on the data, which is $data.
- value: a string in which to replace $1 with the data.
either it or validate may be used, not both.
- optional: true if the parameter is optional.
test: a regular expression to test immediately after the tag's
'=', ' ' or ']'. Typically, should have a \] at the end.
Optional.
content: only available for unparsed_content, closed,
unparsed_commas_content, and unparsed_equals_content.
$1 is replaced with the content of the tag. Parameters
are repalced in the form {param}. For unparsed_commas_content,
$2, $3, ..., $n are replaced.
before: only when content is not used, to go before any
content. For unparsed_equals, $1 is replaced with the value.
For unparsed_commas, $1, $2, ..., $n are replaced.
after: similar to before in every way, except that it is used
when the tag is closed.
disabled_content: used in place of content when the tag is
disabled. For closed, default is '', otherwise it is '$1' if
block_level is false, '<div>$1</div>' elsewise.
disabled_before: used in place of before when disabled. Defaults
to '<div>' if block_level, '' if not.
disabled_after: used in place of after when disabled. Defaults
to '</div>' if block_level, '' if not.
block_level: set to true the tag is a "block level" tag, similar
to HTML. Block level tags cannot be nested inside tags that are
not block level, and will not be implicitly closed as easily.
One break following a block level tag may also be removed.
trim: if set, and 'inside' whitespace after the begin tag will be
removed. If set to 'outside', whitespace after the end tag will
meet the same fate.
validate: except when type is missing or 'closed', a callback to
validate the data as $data. Depending on the tag's type, $data
may be a string or an array of strings (corresponding to the
replacement.)
quoted: when type is 'unparsed_equals' or 'parsed_equals' only,
may be not set, 'optional', or 'required' corresponding to if
the content may be quoted. This allows the parser to read
[tag="abc]def[esdf]"] properly.
require_parents: an array of tag names, or not set. If set, the
enclosing tag *must* be one of the listed tags, or parsing won't
occur.
require_children: similar to require_parents, if set children
won't be parsed if they are not in the list.
disallow_children: similar to, but very different from,
require_children, if it is set the listed tags will not be
parsed inside the tag.
*/
$codes = array(
array(
'tag' => 'abbr',
'type' => 'unparsed_equals',
'before' => '<abbr title="$1">',
'after' => '</abbr>',
'quoted' => 'optional',
'disabled_after' => ' ($1)',
),
array(
'tag' => 'acronym',
'type' => 'unparsed_equals',
'before' => '<acronym title="$1">',
'after' => '</acronym>',
'quoted' => 'optional',
'disabled_after' => ' ($1)',
),
array(
'tag' => 'anchor',
'type' => 'unparsed_equals',
'test' => '[#]?([A-Za-z][A-Za-z0-9_\-]*)\]',
'before' => '<span id="post_$1" />',
'after' => '',
),
array(
'tag' => 'b',
'before' => '<b>',
'after' => '</b>',
),
array(
'tag' => 'black',
'before' => '<span style="color: black;">',
'after' => '</span>',
),
array(
'tag' => 'blue',
'before' => '<span style="color: blue;">',
'after' => '</span>',
),
array(
'tag' => 'br',
'type' => 'closed',
'content' => '<br />',
),
array(
'tag' => 'btc',
'type' => 'closed',
'content' => '<span class="BTC">BTC</span>',
),
array(
'tag' => 'code',
'type' => 'unparsed_content',
'content' => '<div class="codeheader">' . $txt['smf238'] . ':</div><div class="code">' . ($context['browser']['is_gecko'] ? '<pre style="margin-top: 0; display: inline;">$1</pre>' : '$1') . '</div>',
// !!! Maybe this can be simplified?
'validate' => isset($disabled['code']) ? null : function(&$tag, &$data, $disabled) {
global $context;
if (!isset($disabled['code']))
{
$php_parts = preg_split('~(<\?php|\?>)~', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
{
// Do PHP code coloring?
if ($php_parts[$php_i] != '<?php')
continue;
$php_string = '';
while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != '?>')
{
$php_string .= $php_parts[$php_i];
$php_parts[$php_i++] = '';
}
$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
}
// Fix the PHP code stuff...
$data = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode('', $php_parts));
// Older browsers are annoying, aren't they?
if ($context['browser']['is_ie4'] || $context['browser']['is_ie5'] || $context['browser']['is_ie5.5'])
$data = str_replace("\t", "<pre style=\"display: inline;\">\t</pre>", $data);
elseif (!$context['browser']['is_gecko'])
$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
}},
'block_level' => true,
),
array(
'tag' => 'code',
'type' => 'unparsed_equals_content',
'content' => '<div class="codeheader">' . $txt['smf238'] . ': ($2)</div><div class="code">' . ($context['browser']['is_gecko'] ? '<pre style="margin-top: 0; display: inline;">$1</pre>' : '$1') . '</div>',
// !!! Maybe this can be simplified?
'validate' => isset($disabled['code']) ? null : function(&$tag, &$data, $disabled) {
global $context;
if (!isset($disabled['code']))
{
$php_parts = preg_split('~(<\?php|\?>)~', $data[0], -1, PREG_SPLIT_DELIM_CAPTURE);
for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
{
// Do PHP code coloring?
if ($php_parts[$php_i] != '<?php')
continue;
$php_string = '';
while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != '?>')
{
$php_string .= $php_parts[$php_i];
$php_parts[$php_i++] = '';
}
$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
}
// Fix the PHP code stuff...
$data[0] = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode('', $php_parts));
// Older browsers are annoying, aren't they?
if ($context['browser']['is_ie4'] || $context['browser']['is_ie5'] || $context['browser']['is_ie5.5'])
$data = str_replace("\t", "<pre style=\"display: inline;\">\t</pre>", $data);
elseif (!$context['browser']['is_gecko'])
$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
}},
'block_level' => true,
),
array(
'tag' => 'center',
'before' => '<div align="center">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'color',
'type' => 'unparsed_equals',
'test' => '(#[\da-fA-F]{3}|#[\da-fA-F]{6}|[A-Za-z]{1,12})\]',
'before' => '<span style="color: $1;">',
'after' => '</span>',
),
array(
'tag' => 'email',
'type' => 'unparsed_content',
'content' => '<a href="mailto:$1">$1</a>',
// !!! Should this respect guest_hideContacts?
'validate' => function(&$tag, &$data, $disabled) {$data = strtr($data, array('<br />' => ''));},
),
array(
'tag' => 'email',
'type' => 'unparsed_equals',
'before' => '<a href="mailto:$1">',
'after' => '</a>',
// !!! Should this respect guest_hideContacts?
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'ftp',
'type' => 'unparsed_content',
'content' => '<a href="$1">$1</a>',
'validate' => function(&$tag, &$data, $disabled) {
$data = strtr($data, array('<br />' => ''));
if (strpos($data, 'ftp://') !== 0 && strpos($data, 'ftps://') !== 0)
$data = 'ftp://' . $data;
},
),
array(
'tag' => 'ftp',
'type' => 'unparsed_equals',
'before' => '<a href="$1">',
'after' => '</a>',
'validate' => function(&$tag, &$data, $disabled) {
if (strpos($data, 'ftp://') !== 0 && strpos($data, 'ftps://') !== 0)
$data = 'ftp://' . $data;
},
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'font',
'type' => 'unparsed_equals',
'test' => '[A-Za-z0-9_,\-\s]+?\]',
'before' => '<span style="font-family: $1;">',
'after' => '</span>',
),
array(
'tag' => 'flash',
'type' => 'unparsed_commas_content',
'test' => '\d+,\d+\]',
'content' => ($context['browser']['is_ie'] && !$context['browser']['is_mac_ie'] ? '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="$2" height="$3"><param name="movie" value="$1" /><param name="play" value="true" /><param name="loop" value="true" /><param name="quality" value="high" /><param name="AllowScriptAccess" value="never" /><embed src="$1" width="$2" height="$3" play="true" loop="true" quality="high" AllowScriptAccess="never" /><noembed><a href="$1">$1</a></noembed></object>' : '<embed type="application/x-shockwave-flash" src="$1" width="$2" height="$3" play="true" loop="true" quality="high" AllowScriptAccess="never" /><noembed><a href="$1">$1</a></noembed>'),
'validate' => function(&$tag, &$data, $disabled) {
if (isset($disabled['url']))
$tag['content'] = '$1';
elseif (strpos($data[0], 'http://') !== 0 && strpos($data[0], 'https://') !== 0)
$data[0] = 'http://' . $data[0];
},
'disabled_content' => $disabledsecurity ? '$1': '<a href="$1">$1</a>',
),
array(
'tag' => 'green',
'before' => '<span style="color: green;">',
'after' => '</span>',
),
array(
'tag' => 'glow',
'type' => 'unparsed_commas',
'test' => '[#0-9a-zA-Z\-]{3,12},([012]\d{1,2}|\d{1,2})(,[^]]+)?\]',
'before' => $context['browser']['is_ie'] ? '<table border="0" cellpadding="0" cellspacing="0" style="display: inline; vertical-align: middle; font: inherit;"><tr><td style="filter: Glow(color=$1, strength=$2); font: inherit;">' : '<span style="background-color: $1;">',
'after' => $context['browser']['is_ie'] ? '</td></tr></table> ' : '</span>',
),
array(
'tag' => 'hr',
'type' => 'closed',
'content' => '<hr />',
'block_level' => true,
),
array(
'tag' => 'html',
'type' => 'unparsed_content',
'content' => '$1',
'block_level' => true,
'disabled_content' => '$1',
),
array(
'tag' => 'img',
'type' => 'unparsed_content',
'parameters' => array(
'alt' => array('optional' => true),
'width' => array('optional' => true, 'value' => ' width="$1"', 'match' => '(\d{1,4})'),
'height' => array('optional' => true, 'value' => ' height="$1"', 'match' => '(\d{1,4})'),
),
'content' => '<img class="userimg" src="$1" alt="{alt}"{width}{height} border="0" />',
'validate' => function(&$tag, &$data, $disabled) {
$data = strtr($data, array('<br />' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
$data = 'http://' . $data;
if(!isset($disabled['img']))
$data = proxyurl($data);
},
'disabled_content' => $disabledsecurity ? ('($1)'.$disabledsecurity) : '<a href="$1">$1</a>',
),
array(
'tag' => 'img',
'type' => 'unparsed_content',
'content' => '<img class="userimg" src="$1" alt="" border="0" />',
'validate' => function(&$tag, &$data, $disabled) {
$data = strtr($data, array('<br />' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
$data = 'http://' . $data;
if(!isset($disabled['img']))
$data = proxyurl($data);
},
'disabled_content' => $disabledsecurity ? ('($1)'.$disabledsecurity) : '<a href="$1">$1</a>',
),
array(
'tag' => 'i',
'before' => '<i>',
'after' => '</i>',
),
array(
'tag' => 'iurl',
'type' => 'unparsed_content',
'content' => '<a class="ul" href="$1">$1</a>',
'validate' => function(&$tag, &$data, $disabled) {
$data = strtr($data, array('<br />' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0 && strpos($data, 'bitcoin:') !== 0 && strpos($data, 'magnet:') !== 0)
$data = 'http://' . $data;
},
),
array(
'tag' => 'iurl',
'type' => 'unparsed_equals',
'before' => '<a class="ul" href="$1">',
'after' => '</a>',
'validate' => function(&$tag, &$data, $disabled) {
if (substr($data, 0, 1) == '#')
$data = '#post_' . substr($data, 1);
elseif (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0 && strpos($data, 'bitcoin:') !== 0 && strpos($data, 'magnet:') !== 0)
$data = 'http://' . $data;
},
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'li',
'before' => '<li>',
'after' => '</li>',
'trim' => 'outside',
'require_parents' => array('list'),
'block_level' => true,
'disabled_before' => '',
'disabled_after' => '<br />',
),
array(
'tag' => 'list',
'before' => '<ul style="margin-top: 0; margin-bottom: 0;">',
'after' => '</ul>',
'trim' => 'inside',
'require_children' => array('li'),
'block_level' => true,
),
array(
'tag' => 'list',
'parameters' => array(
'type' => array('match' => '(none|disc|circle|square|decimal|decimal-leading-zero|lower-roman|upper-roman|lower-alpha|upper-alpha|lower-greek|lower-latin|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha)'),
),
'before' => '<ul style="margin-top: 0; margin-bottom: 0; list-style-type: {type};">',
'after' => '</ul>',
'trim' => 'inside',
'require_children' => array('li'),
'block_level' => true,
),
array(
'tag' => 'left',
'before' => '<div style="text-align: left;">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'ltr',
'before' => '<div dir="ltr">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'me',
'type' => 'unparsed_equals',
'before' => '<div class="meaction">* $1 ',
'after' => '</div>',
'quoted' => 'optional',
'block_level' => true,
'disabled_before' => '/me ',
'disabled_after' => '<br />',
),
array(
'tag' => 'move',
'before' => '<marquee>',
'after' => '</marquee>',
'block_level' => true,
),
array(
'tag' => 'nbsp',
'type' => 'closed',
'content' => ' ',
),
array(
'tag' => 'nobbc',
'type' => 'unparsed_content',
'content' => '$1',
),
array(
'tag' => 'pre',
'before' => '<pre>',
'after' => '</pre>',
),
array(
'tag' => 'php',
'type' => 'unparsed_content',
'content' => '<div class="phpcode">$1</div>',
'validate' => isset($disabled['php']) ? null : function(&$tag, &$data, $disabled) {
if (!isset($disabled['php']))
{
$add_begin = substr(trim($data), 0, 5) != '<?';
$data = highlight_php_code($add_begin ? '<?php ' . $data . '?>' : $data);
if ($add_begin)
$data = preg_replace(array('~^(.+?)<\?.{0,40}?php( |\s)~', '~\?>((?:</(font|span)>)*)$~'), '$1', $data, 2);
}},
'block_level' => true,
'disabled_content' => '$1',
),
array(
'tag' => 'quote',
'before' => '<div class="quoteheader">' . $txt['smf240'] . '</div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'quote',
'parameters' => array(
'author' => array('match' => '(.{1,192}?)', 'quoted' => true, 'validate' => 'parse_bbc'),
),
'before' => '<div class="quoteheader">' . $txt['smf239'] . ': {author}</div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'quote',
'type' => 'parsed_equals',
'before' => '<div class="quoteheader">' . $txt['smf239'] . ': $1</div><div class="quote">',
'after' => '</div>',
'quoted' => 'optional',
'block_level' => true,
),
array(
'tag' => 'quote',
'parameters' => array(
'author' => array('match' => '([^<>]{1,192}?)'),
'link' => array('match' => '(?:board=\d+;)?((?:topic|threadid)=[\dmsg#\./]{1,40}(?:;start=[\dmsg#\./]{1,40})?|action=profile;u=\d+)'),
'date' => array('match' => '(\d+)', 'validate' => 'timeformat'),
),
'before' => '<div class="quoteheader"><a href="' . $scripturl . '?{link}">' . $txt['smf239'] . ': {author} ' . $txt[176] . ' {date}</a></div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'quote',
'parameters' => array(
'author' => array('match' => '(.{1,192}?)', 'validate' => 'parse_bbc'),
),
'before' => '<div class="quoteheader">' . $txt['smf239'] . ': {author}</div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'right',
'before' => '<div style="text-align: right;">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'red',
'before' => '<span style="color: red;">',
'after' => '</span>',
),
array(
'tag' => 'rtl',
'before' => '<div dir="rtl">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 's',
'before' => '<del>',
'after' => '</del>',
),
array(
'tag' => 'size',
'type' => 'unparsed_equals',
'test' => '([1-9][\d]?p[xt]|(?:x-)?small(?:er)?|(?:x-)?large[r]?)\]',
// !!! line-height
'before' => '<span style="font-size: $1 !important; line-height: 1.3em;">',
'after' => '</span>',
),
array(
'tag' => 'size',
'type' => 'unparsed_equals',
'test' => '[1-9]\]',
// !!! line-height
'before' => '<font size="$1" style="line-height: 1.3em;">',
'after' => '</font>',
),
array(
'tag' => 'sub',
'before' => '<sub>',
'after' => '</sub>',
),
array(
'tag' => 'sup',
'before' => '<sup>',
'after' => '</sup>',
),
array(
'tag' => 'shadow',
'type' => 'unparsed_commas',
'test' => '[#0-9a-zA-Z\-]{3,12},(left|right|top|bottom|[0123]\d{0,2})\]',
'before' => $context['browser']['is_ie'] ? '<span style="filter: Shadow(color=$1, direction=$2); height: 1.2em;\">' : '<span style="text-shadow: $1 $2">',
'after' => '</span>',
'validate' => $context['browser']['is_ie'] ? function(&$tag, &$data, $disabled) {
if ($data[1] == 'left')
$data[1] = 270;
elseif ($data[1] == 'right')
$data[1] = 90;
elseif ($data[1] == 'top')
$data[1] = 0;
elseif ($data[1] == 'bottom')
$data[1] = 180;
else
$data[1] = (int) $data[1];} : function(&$tag, &$data, $disabled) {
if ($data[1] == 'top' || (is_numeric($data[1]) && $data[1] < 50))
return '0 -2px';
elseif ($data[1] == 'right' || (is_numeric($data[1]) && $data[1] < 100))
return '2px 0';
elseif ($data[1] == 'bottom' || (is_numeric($data[1]) && $data[1] < 190))
return '0 2px';
elseif ($data[1] == 'left' || (is_numeric($data[1]) && $data[1] < 280))
return '-2px 0';
else
return '0 0';},
),
array(
'tag' => 'time',
'type' => 'unparsed_content',
'content' => '$1',
'validate' => function(&$tag, &$data, $disabled) {
if (is_numeric($data))
$data = timeformat($data);
else
$tag['content'] = '[time]$1[/time]';},
),
array(
'tag' => 'tt',
'before' => '<tt>',
'after' => '</tt>',
),
array(
'tag' => 'table',
'before' => '<table style="font: inherit; color: inherit;">',
'after' => '</table>',
'trim' => 'inside',
'require_children' => array('tr'),
'block_level' => true,
),
array(
'tag' => 'tr',
'before' => '<tr>',
'after' => '</tr>',
'require_parents' => array('table'),
'require_children' => array('td'),
'trim' => 'both',
'block_level' => true,
'disabled_before' => '',
'disabled_after' => '',
),
array(
'tag' => 'td',
'before' => '<td valign="top" style="font: inherit; color: inherit;">',
'after' => '</td>',
'require_parents' => array('tr'),
'trim' => 'outside',
'block_level' => true,
'disabled_before' => '',
'disabled_after' => '',
),
array(
'tag' => 'url',
'type' => 'unparsed_content',
'content' => '<a class="ul" href="$1">$1</a>',
'validate' => function(&$tag, &$data, $disabled) {
$data = strtr($data, array('<br />' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0 && strpos($data, 'bitcoin:') !== 0 && strpos($data, 'magnet:') !== 0)
$data = 'http://' . $data;
},
),
array(
'tag' => 'url',
'type' => 'unparsed_equals',
'before' => '<a class="ul" href="$1">',
'after' => '</a>',
'validate' => function(&$tag, &$data, $disabled) {
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0 && strpos($data, 'bitcoin:') !== 0 && strpos($data, 'magnet:') !== 0)
$data = 'http://' . $data;
},
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'u',
'before' => '<span style="text-decoration: underline;">',
'after' => '</span>',
),
array(
'tag' => 'white',
'before' => '<span style="color: white;">',
'after' => '</span>',
),
);
// This is mainly for the bbc manager, so it's easy to add tags above. Custom BBC should be added above this line.
if ($message === false)
return $codes;
// So the parser won't skip them.
$itemcodes = array(
'*' => '',
'@' => 'disc',
'+' => 'square',
'x' => 'square',
'#' => 'square',
'o' => 'circle',
'O' => 'circle',
'0' => 'circle',
);
if (!isset($disabled['li']) && !isset($disabled['list']))
{
foreach ($itemcodes as $c => $dummy)
$bbc_codes[$c] = array();
}
// Inside these tags autolink is not recommendable.
$no_autolink_tags = array(
'url',
'iurl',
'ftp',
'email',
);
// Shhhh!
if (!isset($disabled['color']))
{
$codes[] = array(
'tag' => 'chrissy',
'before' => '<span style="color: #CC0099;">',
'after' => ' :-*</span>',
);
$codes[] = array(
'tag' => 'kissy',
'before' => '<span style="color: #CC0099;">',
'after' => ' :-*</span>',
);
}
foreach ($codes as $c)
$bbc_codes[substr($c['tag'], 0, 1)][] = $c;
$codes = null;
}
// Shall we take the time to cache this?
if ($cache_id != '' && !empty($modSettings['cache_enable']) && (($modSettings['cache_enable'] >= 2 && strlen($message) > 1000) || strlen($message) > 2400))
{
// It's likely this will change if the message is modified.
$cache_key = 'parse:' . $cache_id . '-' . md5(md5($message) . '-' . $smileys . (empty($disabled) ? '' : implode(',', array_keys($disabled))) . safe_serialize($context['browser']) . $txt['lang_locale'] . $user_info['time_offset'] . $user_info['time_format']);
if (($temp = cache_get_data($cache_key, 600)) != null)
return $temp;
$cache_t = microtime();
}
if ($smileys === 'print')
{
// [glow], [shadow], and [move] can't really be printed.
$disabled['glow'] = true;
$disabled['shadow'] = true;
$disabled['move'] = true;
// Colors can't well be displayed... supposed to be black and white.
$disabled['color'] = true;
$disabled['black'] = true;
$disabled['blue'] = true;
$disabled['white'] = true;
$disabled['red'] = true;
$disabled['green'] = true;
$disabled['me'] = true;
// Color coding doesn't make sense.
$disabled['php'] = true;
// Links are useless on paper... just show the link.
$disabled['ftp'] = true;
$disabled['url'] = true;
$disabled['iurl'] = true;
$disabled['email'] = true;
$disabled['flash'] = true;
// !!! Change maybe?
if (!isset($_GET['images']))
$disabled['img'] = true;
// !!! Interface/setting to add more?
}
if($local_disable)
foreach($local_disable as $d)
$disabled[$d] = true;
$open_tags = array();
$message = strtr($message, array("\n" => '<br />'));
// The non-breaking-space looks a bit different each time.
$non_breaking_space = $context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{C2A0}' : chr(0xC2) . chr(0xA0)) : '\xA0';
$pos = -1;
while ($pos !== false)
{
// theymos - prevent various infinite loops
if($pos>90000) {
if(!isset($loopcount))
$loopcount=0;
$loopcount++;
if($loopcount > 500)
return 'INVALID BBCODE: loop, probably unclosed tags';
}
$last_pos = isset($last_pos) ? max($pos, $last_pos) : $pos;
$pos = strpos($message, '[', $pos + 1);
// Failsafe.
if ($pos === false || $last_pos > $pos)
$pos = strlen($message) + 1;
// Can't have a one letter smiley, URL, or email! (sorry.)
if ($last_pos < $pos - 1)
{
// We want to eat one less, and one more, character (for smileys.)
$last_pos = max($last_pos - 1, 0);
$data = substr($message, $last_pos, $pos - $last_pos + 1);
// Take care of some HTML!
if (!empty($modSettings['enablePostHTML']) && strpos($data, '<') !== false)
{
$data = preg_replace('~<a\s+href=((?:")?)((?:https?://|ftps?://|mailto:|bitcoin:)\S+?)\\1>~i', '[url=$2]', $data);
$data = preg_replace('~</a>~i', '[/url]', $data);
// <br /> should be empty.
$empty_tags = array('br', 'hr');
foreach ($empty_tags as $tag)
$data = str_replace(array('<' . $tag . '>', '<' . $tag . '/>', '<' . $tag . ' />'), '[' . $tag . ' /]', $data);
// b, u, i, s, pre... basic tags.
$closable_tags = array('b', 'u', 'i', 's', 'em', 'ins', 'del', 'pre', 'blockquote');
foreach ($closable_tags as $tag)
{
$diff = substr_count($data, '<' . $tag . '>') - substr_count($data, '</' . $tag . '>');
$data = strtr($data, array('<' . $tag . '>' => '<' . $tag . '>', '</' . $tag . '>' => '</' . $tag . '>'));
if ($diff > 0)
$data .= str_repeat('</' . $tag . '>', $diff);
}
// Do <img ... /> - with security... action= -> action-.
preg_match_all('~<img\s+src=((?:")?)((?:https?://|ftps?://)\S+?)\\1(?:\s+alt=(".*?"|\S*?))?(?:\s?/)?>~i', $data, $matches, PREG_PATTERN_ORDER);
if (!empty($matches[0]))
{
$replaces = array();
foreach ($matches[2] as $match => $imgtag)
{
$alt = empty($matches[3][$match]) ? '' : ' alt=' . preg_replace('~^"|"$~', '', $matches[3][$match]);
// Remove action= from the URL - no funny business, now.
if (preg_match('~action(=|%3d)(?!dlattach)~i', $imgtag) != 0)
$imgtag = preg_replace('~action(=|%3d)(?!dlattach)~i', 'action-', $imgtag);
// Check if the image is larger than allowed.
if (!empty($modSettings['max_image_width']) && !empty($modSettings['max_image_height']))
{
list ($width, $height) = url_image_size($imgtag);
if (!empty($modSettings['max_image_width']) && $width > $modSettings['max_image_width'])
{
$height = (int) (($modSettings['max_image_width'] * $height) / $width);
$width = $modSettings['max_image_width'];
}
if (!empty($modSettings['max_image_height']) && $height > $modSettings['max_image_height'])
{
$width = (int) (($modSettings['max_image_height'] * $width) / $height);
$height = $modSettings['max_image_height'];
}
// Set the new image tag.
$replaces[$matches[0][$match]] = '[img width=' . $width . ' height=' . $height . $alt . ']' . $imgtag . '[/img]';
}
else
$replaces[$matches[0][$match]] = '[img' . $alt . ']' . $imgtag . '[/img]';
}
$data = strtr($data, $replaces);
}
}
if (!empty($modSettings['autoLinkUrls']))
{
// Are we inside tags that should be auto linked?
$no_autolink_area = false;
if (!empty($open_tags))
{
foreach ($open_tags as $open_tag)
if (in_array($open_tag['tag'], $no_autolink_tags))
$no_autolink_area = true;
}
// Don't go backwards.
//!!! Don't think is the real solution....
$lastAutoPos = isset($lastAutoPos) ? $lastAutoPos : 0;
if ($pos < $lastAutoPos)
$no_autolink_area = true;
$lastAutoPos = $pos;
if (!$no_autolink_area)
{
// Parse any URLs.... have to get rid of the @ problems some things cause... stupid email addresses.
if (!isset($disabled['url']) && (strpos($data, '://') !== false || strpos($data, 'www.') !== false || strpos($data,'bitcoin:') !==false))
{
// Switch out quotes really quick because they can cause problems.
$data = strtr($data, array(''' => '\'', ' ' => $context['utf8'] ? "\xC2\xA0" : "\xA0", '"' => '>">', '"' => '<"<', '<' => '<lt<'));
// Can't make use of $non_breaking_space in the URL regexes (that definition won't work without the "u" modifier).
$nbsp = $context['utf8'] ? '\xc2\xa0' : '\xa0';
// Only do this if the preg survives.
if (is_string($result = preg_replace(array(
'~(?<=[\s>\.(;\'"]|' . $nbsp . '|^)((?:http|https|ftp|ftps)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i',
'~(?<=[\s>(;\'<]|' . $nbsp . '|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i',
'~bitcoin:([-A-Za-z0-9._:/?#!%@$()*+,;=]{25,})~i'
), array(
'[url]$1[/url]',
'[url=http://$1]$1[/url]',
'[url]bitcoin:$1[/url]'
), $data)))
$data = $result;
$data = strtr($data, array('\'' => ''', $context['utf8'] ? "\xC2\xA0" : "\xA0" => ' ', '>">' => '"', '<"<' => '"', '<lt<' => '<'));
}
// Next, emails...
if (!isset($disabled['email']) && strpos($data, '@') !== false)
{
$data = preg_replace('~(?<=[\?\s' . $non_breaking_space . '\[\]()*\\\;>]|^)([\w\-\.]{1,80}@[\w\-]+\.[\w\-\.]+[\w\-])(?=[?,\s' . $non_breaking_space . '\[\]()*\\\]|$|<br />| |>|<|"|'|\.(?:\.|;| |\s|$|<br />))~' . ($context['utf8'] ? 'u' : ''), '[email]$1[/email]', $data);
$data = preg_replace('~(?<=<br />)([\w\-\.]{1,80}@[\w\-]+\.[\w\-\.]+[\w\-])(?=[?\.,;\s' . $non_breaking_space . '\[\]()*\\\]|$|<br />| |>|<|"|')~' . ($context['utf8'] ? 'u' : ''), '[email]$1[/email]', $data);
// theymos - infinite loop
if($pos > 1000 && strpos(substr($message, $pos-100), '[email][email][email][email]') !== false)
return 'INVALID BBCODE: loop, probably unclosed tags (2)';
}
}
}
$data = strtr($data, array("\t" => ' '));
if (!empty($modSettings['fixLongWords']) && $modSettings['fixLongWords'] > 5)
{
// This is SADLY and INCREDIBLY browser dependent.
if ($context['browser']['is_gecko'] || $context['browser']['is_konqueror'])
$breaker = '<span style="margin: 0 -0.5ex 0 0;"> </span>';
// Opera...
elseif ($context['browser']['is_opera'])
$breaker = '<span style="margin: 0 -0.65ex 0 -1px;"> </span>';
// Internet Explorer...
else
$breaker = '<span style="width: 0; margin: 0 -0.6ex 0 -1px;"> </span>';
// PCRE will not be happy if we don't give it a short.
$modSettings['fixLongWords'] = (int) min(65535, $modSettings['fixLongWords']);
// The idea is, find words xx long, and then replace them with xx + space + more.
if (strlen($data) > $modSettings['fixLongWords'])
{
// This is done in a roundabout way because $breaker has "long words" :P.
$data = strtr($data, array($breaker => '< >', ' ' => $context['utf8'] ? "\xC2\xA0" : "\xA0"));
$data = preg_replace_callback(
'~(?<=[>;:!? ' . $non_breaking_space . '\]()]|^)([\w' . ($context['utf8'] ? '\pL' : '') . '\.]{' . $modSettings['fixLongWords'] . ',})~' . ($context['utf8'] ? 'u' : ''),
'word_break__preg_callback',
$data);
$data = strtr($data, array('< >' => $breaker, $context['utf8'] ? "\xC2\xA0" : "\xA0" => ' '));
}
}
// Do any smileys!
if ($smileys === true)
parsesmileys($data);
// If it wasn't changed, no copying or other boring stuff has to happen!