/
class_parser.php
2072 lines (1804 loc) · 53.9 KB
/
class_parser.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
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
*/
/*
options = array(
allow_html
allow_smilies
allow_mycode
allow_auto_url
nl2br
filter_badwords
me_username
shorten_urls
highlight
filter_cdata
)
*/
class postParser
{
/**
* Internal cache of MyCode.
*
* @access public
* @var mixed
*/
public $mycode_cache = 0;
/**
* Internal cache of smilies
*
* @access public
* @var mixed
*/
public $smilies_cache = 0;
/**
* Internal cache of badwords filters
*
* @access public
* @var mixed
*/
public $badwords_cache = 0;
/**
* Base URL for smilies
*
* @access public
* @var string
*/
public $base_url;
/**
* Parsed Highlights cache
*
* @access public
* @var array
*/
public $highlight_cache = array();
/**
* Options for this parsed message
*
* @access public
* @var array
*/
public $options;
/**
* Internal cache for nested lists
*
* @access public
* @var array
*/
public $list_elements;
/**
* Internal counter for nested lists
*
* @access public
* @var int
*/
public $list_count;
/**
* Whether or not should a <br /> with clear: both be added at the end of the parsed message
*
* @access public
* @var boolean
*/
public $clear_needed = false;
/**
* Don't validate parser output
*/
const VALIDATION_DISABLE = 0;
/**
* Validate parser output and log errors
*/
const VALIDATION_REPORT_ONLY = 1;
/**
* Validate parser output, log errors, and block output on failure
*/
const VALIDATION_REQUIRE = 2;
/**
* Whether to validate the parser's HTML output when `allow_html` is disabled.
* Validation errors will be logged/sent/displayed according to board settings.
*
* @access public
* @var self::VALIDATION_*
*/
public $output_validation_policy = self::VALIDATION_REQUIRE;
/**
* Parses a message with the specified options.
*
* @param string $message The message to be parsed.
* @param array $options Array of yes/no options
* @return string The parsed message.
*/
function parse_message($message, $options=array())
{
global $plugins, $mybb;
$original_message = $message;
$this->clear_needed = false;
// Set base URL for parsing smilies
$this->base_url = $mybb->settings['bburl'];
if($this->base_url != "")
{
if(my_substr($this->base_url, my_strlen($this->base_url) -1) != "/")
{
$this->base_url = $this->base_url."/";
}
}
// Set the options
$this->options = $options;
$message = $plugins->run_hooks("parse_message_start", $message);
// Get rid of carriage returns for they are the workings of the devil
$message = str_replace("\r", "", $message);
// Filter bad words if requested.
if(!empty($this->options['filter_badwords']))
{
$message = $this->parse_badwords($message);
}
// Filter CDATA tags if requested (syndication.php).
if(!empty($this->options['filter_cdata']))
{
$message = $this->parse_cdata($message);
}
// If MyCode needs to be replaced, first filter out [code] and [php] tags.
$code_matches = array();
if(!empty($this->options['allow_mycode']) && $mybb->settings['allowcodemycode'] == 1)
{
// This code is reserved and could break codes
$message = str_replace("<mybb-code>\n", "<mybb_code>\n", $message);
preg_match_all("#\[(code|php)\](.*?)(\[/\\1\])+(\r\n?|\n?)#si", $message, $code_matches, PREG_SET_ORDER);
foreach($code_matches as $point => $part)
{
if(isset($part[3]))
{
$part[1] = "[".$part[1]."]";
$code_matches[$point][2] = substr_replace($part[0], "", strrpos($part[0], $part[3]), strlen($part[3]));
$code_matches[$point][2] = substr_replace($code_matches[$point][2], "", strpos($code_matches[$point][2], $part[1]), strlen($part[1]));
}
}
$message = preg_replace("#\[(code|php)\](.*?)(\[/\\1\])+(\r\n?|\n?)#si", "<mybb-code>\n", $message);
}
if(empty($this->options['allow_html']))
{
$message = $this->parse_html($message);
$message = str_replace("<mybb-code>\n", "<mybb-code>\n", $message);
}
else
{
// Replace base, meta,script and style tags in our post - these are > dangerous <
$message = preg_replace('#<(/?)(base|meta|script|style)([^>]*)>#i', '<$1$2$3>', $message);
$message = $this->fix_javascript($message);
$find = array("<br />\n", "<br>\n");
$replace = array("\n", "\n");
$message = str_replace($find, $replace, $message);
}
$message = $plugins->run_hooks("parse_message_htmlsanitized", $message);
// Replace "me" code and slaps if we have a username
if(!empty($this->options['me_username']) && $mybb->settings['allowmemycode'] == 1)
{
global $lang;
$message = preg_replace('#(>|^|\r|\n)/me ([^\r\n<]*)#i', "\\1<span style=\"color: red;\" class=\"mycode_me\">* {$this->options['me_username']} \\2</span>", $message);
$message = preg_replace('#(>|^|\r|\n)/slap ([^\r\n<]*)#i', "\\1<span style=\"color: red;\" class=\"mycode_slap\">* {$this->options['me_username']} {$lang->slaps} \\2 {$lang->with_trout}</span>", $message);
}
$message = $plugins->run_hooks("parse_message_me_mycode", $message);
// If we can, parse smilies
if(!empty($this->options['allow_smilies']))
{
$message = $this->parse_smilies($message, $this->options['allow_html']);
}
// Replace MyCode if requested.
if(!empty($this->options['allow_mycode']))
{
$message = $this->parse_mycode($message);
}
// Filter url codes, if disabled.
if($mybb->settings['allowlinkmycode'] != 1)
{
$message = preg_replace("#\[(\/)?url{1}(.*?)\]#i", "", $message);
}
// Parse Highlights
if(!empty($this->options['highlight']))
{
$message = $this->highlight_message($message, $this->options['highlight']);
}
// Run plugin hooks
$message = $plugins->run_hooks("parse_message", $message);
if(!empty($this->options['allow_mycode']))
{
// Now that we're done, if we split up any code tags, parse them and glue it all back together
if(count($code_matches) > 0)
{
foreach($code_matches as $text)
{
if(my_strtolower($text[1]) == "code")
{
// Fix up HTML inside the code tags so it is clean
$text[2] = $this->parse_html($text[2]);
$code = $this->mycode_parse_code($text[2]);
}
elseif(my_strtolower($text[1]) == "php")
{
$code = $this->mycode_parse_php($text[2]);
}
$message = preg_replace("#\<mybb-code>\n?#", $code, $message, 1);
}
}
}
if(!isset($this->options['nl2br']) || $this->options['nl2br'] != 0)
{
$message = nl2br($message);
// Fix up new lines and block level elements
$message = preg_replace("#(</?(?:html|head|body|div|p|form|table|thead|tbody|tfoot|tr|td|th|ul|ol|li|div|p|blockquote|cite|hr)[^>]*>)\s*<br />#i", "$1", $message);
$message = preg_replace("#( )+(</?(?:html|head|body|div|p|form|table|thead|tbody|tfoot|tr|td|th|ul|ol|li|div|p|blockquote|cite|hr)[^>]*>)#i", "$2", $message);
}
if($this->clear_needed)
{
$message .= '<br class="clear" />';
}
$message = $plugins->run_hooks("parse_message_end", $message);
if ($this->output_allowed($original_message, $message) === true)
{
return $message;
}
else
{
return '';
}
}
/**
* Converts HTML in a message to their specific entities whilst allowing unicode characters.
*
* @param string $message The message to be parsed.
* @return string The formatted message.
*/
function parse_html($message)
{
$message = preg_replace("#&(?!\#[0-9]+;)#si", "&", $message); // fix & but allow unicode
$message = str_replace("<","<",$message);
$message = str_replace(">",">",$message);
return $message;
}
/**
* Generates a cache of MyCode, both standard and custom.
*
* @access private
*/
function cache_mycode()
{
global $cache, $lang, $mybb;
$this->mycode_cache = array();
$standard_mycode = $callback_mycode = $nestable_mycode = $nestable_callback_mycode = array();
$standard_count = $callback_count = $nestable_count = $nestable_callback_count = 0;
if($mybb->settings['allowbasicmycode'] == 1)
{
$standard_mycode['b']['regex'] = "#\[b\](.*?)\[/b\]#si";
$standard_mycode['b']['replacement'] = "<span style=\"font-weight: bold;\" class=\"mycode_b\">$1</span>";
$standard_mycode['u']['regex'] = "#\[u\](.*?)\[/u\]#si";
$standard_mycode['u']['replacement'] = "<span style=\"text-decoration: underline;\" class=\"mycode_u\">$1</span>";
$standard_mycode['i']['regex'] = "#\[i\](.*?)\[/i\]#si";
$standard_mycode['i']['replacement'] = "<span style=\"font-style: italic;\" class=\"mycode_i\">$1</span>";
$standard_mycode['s']['regex'] = "#\[s\](.*?)\[/s\]#si";
$standard_mycode['s']['replacement'] = "<span style=\"text-decoration: line-through;\" class=\"mycode_s\">$1</span>";
$standard_mycode['hr']['regex'] = "#\[hr\]#si";
$standard_mycode['hr']['replacement'] = "<hr class=\"mycode_hr\" />";
++$standard_count;
}
if($mybb->settings['allowsymbolmycode'] == 1)
{
$standard_mycode['copy']['regex'] = "#\(c\)#i";
$standard_mycode['copy']['replacement'] = "©";
$standard_mycode['tm']['regex'] = "#\(tm\)#i";
$standard_mycode['tm']['replacement'] = "™";
$standard_mycode['reg']['regex'] = "#\(r\)#i";
$standard_mycode['reg']['replacement'] = "®";
++$standard_count;
}
if($mybb->settings['allowlinkmycode'] == 1)
{
$callback_mycode['url_simple']['regex'] = "#\[url\]((?!javascript)[a-z]+?://)([^\r\n\"<]+?)\[/url\]#si";
$callback_mycode['url_simple']['replacement'] = array($this, 'mycode_parse_url_callback1');
$callback_mycode['url_simple2']['regex'] = "#\[url\]((?!javascript:)[^\r\n\"<]+?)\[/url\]#i";
$callback_mycode['url_simple2']['replacement'] = array($this, 'mycode_parse_url_callback2');
$callback_mycode['url_complex']['regex'] = "#\[url=((?!javascript)[a-z]+?://)([^\r\n\"<]+?)\](.+?)\[/url\]#si";
$callback_mycode['url_complex']['replacement'] = array($this, 'mycode_parse_url_callback1');
$callback_mycode['url_complex2']['regex'] = "#\[url=((?!javascript:)[^\r\n\"<]+?)\](.+?)\[/url\]#si";
$callback_mycode['url_complex2']['replacement'] = array($this, 'mycode_parse_url_callback2');
++$callback_count;
}
if($mybb->settings['allowemailmycode'] == 1)
{
$callback_mycode['email_simple']['regex'] = "#\[email\]((?:[a-zA-Z0-9-_\+\.]+?)@[a-zA-Z0-9-]+\.[a-zA-Z0-9\.-]+(?:\?.*?)?)\[/email\]#i";
$callback_mycode['email_simple']['replacement'] = array($this, 'mycode_parse_email_callback');
$callback_mycode['email_complex']['regex'] = "#\[email=((?:[a-zA-Z0-9-_\+\.]+?)@[a-zA-Z0-9-]+\.[a-zA-Z0-9\.-]+(?:\?.*?)?)\](.*?)\[/email\]#i";
$callback_mycode['email_complex']['replacement'] = array($this, 'mycode_parse_email_callback');
++$callback_count;
}
if($mybb->settings['allowcolormycode'] == 1)
{
$nestable_mycode['color']['regex'] = "#\[color=([a-zA-Z]*|\#?[\da-fA-F]{3}|\#?[\da-fA-F]{6})](.*?)\[/color\]#si";
$nestable_mycode['color']['replacement'] = "<span style=\"color: $1;\" class=\"mycode_color\">$2</span>";
++$nestable_count;
}
if($mybb->settings['allowsizemycode'] == 1)
{
$nestable_mycode['size']['regex'] = "#\[size=(xx-small|x-small|small|medium|large|x-large|xx-large)\](.*?)\[/size\]#si";
$nestable_mycode['size']['replacement'] = "<span style=\"font-size: $1;\" class=\"mycode_size\">$2</span>";
$callback_mycode['size_int']['regex'] = "#\[size=([0-9\+\-]+?)\](.*?)\[/size\]#si";
$callback_mycode['size_int']['replacement'] = array($this, 'mycode_handle_size_callback');
++$nestable_count;
++$callback_count;
}
if($mybb->settings['allowalignmycode'] == 1)
{
$nestable_mycode['align']['regex'] = "#\[align=(left|center|right|justify)\](.*?)\[/align\]#si";
$nestable_mycode['align']['replacement'] = "<div style=\"text-align: $1;\" class=\"mycode_align\">$2</div>";
++$nestable_count;
}
if($mybb->settings['allowfontmycode'] == 1)
{
$nestable_callback_mycode['font']['regex'] = "#\[font=\\s*(\"?)([a-z0-9 ,\-_'\"]+)\\1\\s*\](.*?)\[/font\]#si";
$nestable_callback_mycode['font']['replacement'] = array($this, 'mycode_parse_font_callback');
++$nestable_callback_count;
}
$custom_mycode = $cache->read("mycode");
// If there is custom MyCode, load it.
if(is_array($custom_mycode))
{
foreach($custom_mycode as $key => $mycode)
{
$mycode['regex'] = str_replace("\x0", "", $mycode['regex']);
$custom_mycode[$key]['regex'] = "#".$mycode['regex']."#si";
++$standard_count;
}
$mycode = array_merge($standard_mycode, $custom_mycode);
}
else
{
$mycode = $standard_mycode;
}
// Assign the MyCode to the cache.
foreach($mycode as $code)
{
$this->mycode_cache['standard']['find'][] = $code['regex'];
$this->mycode_cache['standard']['replacement'][] = $code['replacement'];
}
// Assign the nestable MyCode to the cache.
foreach($nestable_mycode as $code)
{
$this->mycode_cache['nestable'][] = array('find' => $code['regex'], 'replacement' => $code['replacement']);
}
// Assign the callback MyCode to the cache.
foreach($callback_mycode as $code)
{
$this->mycode_cache['callback'][] = array('find' => $code['regex'], 'replacement' => $code['replacement']);
}
// Assign the nestable callback MyCode to the cache.
foreach($nestable_callback_mycode as $code)
{
$this->mycode_cache['nestable_callback'][] = array('find' => $code['regex'], 'replacement' => $code['replacement']);
}
$this->mycode_cache['standard_count'] = $standard_count;
$this->mycode_cache['callback_count'] = $callback_count;
$this->mycode_cache['nestable_count'] = $nestable_count;
$this->mycode_cache['nestable_callback_count'] = $nestable_callback_count;
}
/**
* Parses MyCode tags in a specific message with the specified options.
*
* @param string $message The message to be parsed.
* @param array $options Array of options in yes/no format. Options are allow_imgcode.
* @return string The parsed message.
*/
function parse_mycode($message, $options=array())
{
global $lang, $mybb;
if(empty($this->options))
{
$this->options = $options;
}
// Cache the MyCode globally if needed.
if($this->mycode_cache == 0)
{
$this->cache_mycode();
}
// Parse quotes first
$message = $this->mycode_parse_quotes($message);
// Convert images when allowed.
if(!empty($this->options['allow_imgcode']))
{
$message = preg_replace_callback("#\[img\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback1'), $message);
$message = preg_replace_callback("#\[img=([1-9][0-9]*)x([1-9][0-9]*)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback2'), $message);
$message = preg_replace_callback("#\[img align=(left|right)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback3'), $message);
$message = preg_replace_callback("#\[img=([1-9][0-9]*)x([1-9][0-9]*) align=(left|right)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback4'), $message);
}
else
{
$message = preg_replace_callback("#\[img\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_disabled_callback1'), $message);
$message = preg_replace_callback("#\[img=([1-9][0-9]*)x([1-9][0-9]*)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_disabled_callback2'), $message);
$message = preg_replace_callback("#\[img align=(left|right)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_disabled_callback3'), $message);
$message = preg_replace_callback("#\[img=([1-9][0-9]*)x([1-9][0-9]*) align=(left|right)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_disabled_callback4'), $message);
}
// Convert videos when allow.
if(!empty($this->options['allow_videocode']))
{
$message = preg_replace_callback("#\[video=(.*?)\](.*?)\[/video\]#i", array($this, 'mycode_parse_video_callback'), $message);
}
else
{
$message = preg_replace_callback("#\[video=(.*?)\](.*?)\[/video\]#i", array($this, 'mycode_parse_video_disabled_callback'), $message);
}
$message = str_replace('$', '$', $message);
// Replace the rest
if($this->mycode_cache['standard_count'] > 0)
{
$message = preg_replace($this->mycode_cache['standard']['find'], $this->mycode_cache['standard']['replacement'], $message);
}
if($this->mycode_cache['callback_count'] > 0)
{
foreach($this->mycode_cache['callback'] as $replace)
{
$message = preg_replace_callback($replace['find'], $replace['replacement'], $message);
}
}
// Replace the nestable mycode's
if($this->mycode_cache['nestable_count'] > 0)
{
foreach($this->mycode_cache['nestable'] as $mycode)
{
while(preg_match($mycode['find'], $message))
{
$message = preg_replace($mycode['find'], $mycode['replacement'], $message);
}
}
}
// Replace the nestable callback mycodes
if($this->mycode_cache['nestable_callback_count'] > 0)
{
foreach($this->mycode_cache['nestable_callback'] as $replace)
{
while(preg_match($replace['find'], $message))
{
$message_org = $message;
$message = preg_replace_callback($replace['find'], $replace['replacement'], $message);
if ($message_org == $message)
{
break;
}
}
}
}
// Reset list cache
if($mybb->settings['allowlistmycode'] == 1)
{
$this->list_elements = array();
$this->list_count = 0;
// Find all lists
$message = preg_replace_callback("#(\[list(=(a|A|i|I|1))?\]|\[/list\])#si", array($this, 'mycode_prepare_list'), $message);
// Replace all lists
for($i = $this->list_count; $i > 0; $i--)
{
// Ignores missing end tags
$message = preg_replace_callback("#\s?\[list(=(a|A|i|I|1))?&{$i}\](.*?)(\[/list&{$i}\]|$)(\r\n?|\n?)#si", array($this, 'mycode_parse_list_callback'), $message, 1);
}
}
if(
(!isset($this->options['allow_auto_url']) || $this->options['allow_auto_url'] == 1) &&
$mybb->settings['allowautourl'] == 1
)
{
$message = $this->mycode_auto_url($message);
}
return $message;
}
/**
* Generates a cache of smilies
*
* @access private
*/
function cache_smilies()
{
global $cache, $mybb, $theme, $templates;
$this->smilies_cache = array();
$smilies = $cache->read("smilies");
if(is_array($smilies))
{
$extra_class = $onclick = '';
foreach($smilies as $sid => $smilie)
{
if(isset($theme['imgdir']))
{
$imgdir = $theme['imgdir'];
}
else
{
$imgdir = '';
}
$smilie['find'] = explode("\n", $smilie['find']);
$smilie['image'] = str_replace("{theme}", $imgdir, $smilie['image']);
$smilie['image'] = htmlspecialchars_uni($mybb->get_asset_url($smilie['image']));
$smilie['name'] = htmlspecialchars_uni($smilie['name']);
foreach($smilie['find'] as $s)
{
$s = $this->parse_html($s);
eval("\$smilie_template = \"".$templates->get("smilie", 1, 0)."\";");
$this->smilies_cache[$s] = $smilie_template;
// workaround for smilies starting with ;
if($s[0] == ";")
{
$this->smilies_cache += array(
"&$s" => "&$s",
"<$s" => "<$s",
">$s" => ">$s",
);
}
}
}
}
}
/**
* Parses smilie code in the specified message.
*
* @param string $message $message The message being parsed.
* @param int $allow_html not used
* @return string The parsed message.
*/
function parse_smilies($message, $allow_html=0)
{
if($this->smilies_cache == 0)
{
$this->cache_smilies();
}
// No smilies?
if(!count($this->smilies_cache))
{
return $message;
}
// First we take out any of the tags we don't want parsed between (url= etc)
preg_match_all("#\[(url(=[^\]]*)?\]|quote=([^\]]*)?\])|(http|ftp)(s|)://[^\s]*#i", $message, $bad_matches, PREG_PATTERN_ORDER);
if(count($bad_matches[0]) > 0)
{
$message = preg_replace("#\[(url(=[^\]]*)?\]|quote=([^\]]*)?\])|(http|ftp)(s|)://[^\s]*#si", "<mybb-bad-sm>", $message);
}
$message = strtr($message, $this->smilies_cache);
// If we matched any tags previously, swap them back in
if(count($bad_matches[0]) > 0)
{
$message = explode("<mybb-bad-sm>", $message);
$i = 0;
foreach($bad_matches[0] as $match)
{
$message[$i] .= $match;
$i++;
}
$message = implode("", $message);
}
return $message;
}
/**
* Generates a cache of badwords filters.
*
* @access private
*/
function cache_badwords()
{
global $cache;
$this->badwords_cache = array();
$this->badwords_cache = $cache->read("badwords");
}
/**
* Parses a list of filtered/badwords in the specified message.
*
* @param string $message The message to be parsed.
* @param array $options Array of parser options in yes/no format.
* @return string The parsed message.
*/
function parse_badwords($message, $options=array())
{
if(empty($this->options))
{
$this->options = $options;
}
if($this->badwords_cache == 0)
{
$this->cache_badwords();
}
if(is_array($this->badwords_cache))
{
reset($this->badwords_cache);
foreach($this->badwords_cache as $bid => $badword)
{
if(!$badword['replacement'])
{
$badword['replacement'] = "*****";
}
if(!$badword['regex'])
{
$badword['badword'] = $this->generate_regex($badword['badword']);
}
$message = preg_replace('#'.$badword['badword'].'#is', $badword['replacement'], $message);
}
}
if(!empty($this->options['strip_tags']))
{
$message = strip_tags($message);
}
return $message;
}
/**
* Generates REGEX patterns based on user defined badword string.
*
* @param string $badword The word defined to replace.
* @return string The regex pattern to match the word or null on error.
*/
function generate_regex($bad_word = "")
{
if($bad_word == "")
{
return;
}
// Neutralize escape character, regex operators, multiple adjacent wildcards and generate pattern
$ptrn = array('/\\\\/', '/([\[\^\$\.\|\?\(\)\{\}]{1})/', '/\*\++/', '/\++\*/', '/\*+/');
$rplc = array('\\\\\\\\','\\\\${1}', '*', '*', '[^\s\n]*');
$bad_word = preg_replace($ptrn, $rplc, $bad_word);
// Count + and generate pattern
$bad_word = explode('+', $bad_word);
$trap = "";
$plus = 0;
foreach($bad_word as $bad_piece)
{
if($bad_piece)
{
$trap .= $plus ? '[^\s\n]{'.$plus.'}'.$bad_piece : $bad_piece;
$plus = 1;
}
else
{
$plus++;
}
}
// Handle trailing +
if($plus > 1)
{
$trap .= '[^\s\n]{'.($plus-1).'}';
}
return '\b'.$trap.'\b';
}
/**
* Resolves nested CDATA tags in the specified message.
*
* @param string $message The message to be parsed.
* @return string The parsed message.
*/
function parse_cdata($message)
{
$message = str_replace(']]>', ']]]]><![CDATA[>', $message);
return $message;
}
/**
* Attempts to move any javascript references in the specified message.
*
* @param string The message to be parsed.
* @return string The parsed message.
*/
function fix_javascript($message)
{
$js_array = array(
"#(&\#(0*)106;?|&\#(0*)74;?|&\#x(0*)4a;?|&\#x(0*)6a;?|j)((&\#(0*)97;?|&\#(0*)65;?|a)(&\#(0*)118;?|&\#(0*)86;?|v)(&\#(0*)97;?|&\#(0*)65;?|a)(\s)?(&\#(0*)115;?|&\#(0*)83;?|s)(&\#(0*)99;?|&\#(0*)67;?|c)(&\#(0*)114;?|&\#(0*)82;?|r)(&\#(0*)105;?|&\#(0*)73;?|i)(&\#112;?|&\#(0*)80;?|p)(&\#(0*)116;?|&\#(0*)84;?|t)(&\#(0*)58;?|\:))#i",
"#([\s\"']on)([a-z]+\s*=)#i",
);
// Add invisible white space
$message = preg_replace($js_array, "$1\xE2\x80\x8C$2$6", $message);
return $message;
}
/**
* Handles fontsize.
*
* @param int $size The original size.
* @param string $text The text within a size tag.
* @return string The parsed text.
*/
function mycode_handle_size($size, $text)
{
global $templates;
$size = (int)$size;
if($size < 1)
{
$size = 1;
}
if($size > 50)
{
$size = 50;
}
$text = str_replace("\'", "'", $text);
eval("\$mycode_size = \"".$templates->get("mycode_size_int", 1, 0)."\";");
return $mycode_size;
}
/**
* Handles fontsize.
*
* @param array $matches Matches.
* @return string The parsed text.
*/
function mycode_handle_size_callback($matches)
{
return $this->mycode_handle_size($matches[1], $matches[2]);
}
/**
* Parses quote MyCode.
*
* @param string $message The message to be parsed
* @param boolean $text_only Are we formatting as text?
* @return string The parsed message.
*/
function mycode_parse_quotes($message, $text_only=false)
{
global $lang, $templates, $theme, $mybb;
// Assign pattern and replace values.
$pattern = "#\[quote\](.*?)\[\/quote\](\r\n?|\n?)#si";
$pattern_callback = "#\[quote=([\"']|"|)(.*?)(?:\\1)(.*?)(?:[\"']|")?\](.*?)\[/quote\](\r\n?|\n?)#si";
if($text_only == false)
{
$replace = "<blockquote class=\"mycode_quote\"><cite>$lang->quote</cite>$1</blockquote>\n";
$replace_callback = array($this, 'mycode_parse_post_quotes_callback1');
}
else
{
$replace = empty($this->options['signature_parse']) ? "\n{$lang->quote}\n--\n$1\n--\n" : "$1";
$replace_callback = array($this, 'mycode_parse_post_quotes_callback2');
}
do
{
// preg_replace has erased the message? Restore it...
$previous_message = $message;
$message = preg_replace($pattern, $replace, $message, -1, $count);
$message = preg_replace_callback($pattern_callback, $replace_callback, $message, -1, $count_callback);
if(!$message)
{
$message = $previous_message;
break;
}
} while($count || $count_callback);
if($text_only == false)
{
$find = array(
"#(\r\n*|\n*)<\/cite>(\r\n*|\n*)#",
"#(\r\n*|\n*)<\/blockquote>#"
);
$replace = array(
"</cite><br />",
"</blockquote>"
);
$message = preg_replace($find, $replace, $message);
}
return $message;
}
/**
* Parses quotes with post id and/or dateline.
*
* @param string $message The message to be parsed
* @param string $username The username to be parsed
* @param boolean $text_only Are we formatting as text?
* @return string The parsed message.
*/
function mycode_parse_post_quotes($message, $username, $text_only=false)
{
global $lang, $templates, $theme, $mybb;
$linkback = $date = "";
$message = trim($message);
$message = preg_replace("#(^<br(\s?)(\/?)>|<br(\s?)(\/?)>$)#i", "", $message);
if(!$message)
{
return '';
}
$username .= "'";
$delete_quote = true;
preg_match("#pid=(?:"|\"|')?([0-9]+)[\"']?(?:"|\"|')?#i", $username, $match);
if(isset($match[1]) && (int)$match[1])
{
$pid = (int)$match[1];
$url = $mybb->settings['bburl']."/".get_post_link($pid)."#pid$pid";
if(defined("IN_ARCHIVE"))
{
$linkback = " <a href=\"{$url}\">[ -> ]</a>";
}
else
{
eval("\$linkback = \" ".$templates->get("postbit_gotopost", 1, 0)."\";");
}
$username = preg_replace("#(?:"|\"|')? pid=(?:"|\"|')?[0-9]+[\"']?(?:"|\"|')?#i", '', $username);
$delete_quote = false;
}
unset($match);
preg_match("#dateline=(?:"|\"|')?([0-9]+)(?:"|\"|')?#i", $username, $match);
if(isset($match[1]) && (int)$match[1])
{
if($match[1] < TIME_NOW)
{
if($text_only)
{
$postdate = my_date('normal', (int)$match[1]);
}
else
{
$postdate = my_date('relative', (int)$match[1]);
}
$date = " ({$postdate})";
}
$username = preg_replace("#(?:"|\"|')? dateline=(?:"|\"|')?[0-9]+(?:"|\"|')?#i", '', $username);
$delete_quote = false;
}
if($delete_quote)
{
$username = my_substr($username, 0, my_strlen($username)-1, true);
}
if(!empty($this->options['allow_html']))
{
$username = htmlspecialchars_uni($username);
}
if($text_only)
{
return "\n{$username} {$lang->wrote}{$date}\n--\n{$message}\n--\n";
}
else
{
$span = "";
if(!$delete_quote)
{
$span = "<span>{$date}</span>";
}
eval("\$mycode_quote = \"".$templates->get("mycode_quote_post", 1, 0)."\";");
return $mycode_quote;
}