forked from samclarke/SBBCodeParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSBBCodeParser.php
1503 lines (1280 loc) · 43.7 KB
/
SBBCodeParser.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
/**
* SSBBCodeParser
*
* BBCode parser classes.
*
* @copyright (C) 2011 Sam Clarke (samclarke.com)
* @license http://www.gnu.org/licenses/lgpl.html LGPL version 3 or higher
*
* @TODO: Add inline/block to tags and forbid adding block elements to inline ones.
* Maybe split the inline elemnt and put the block element inbetween?
* @TODO: Have options for limiting nesting of tags
* @TODO: Have whitespace trimming options for tags
*/
/*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
class SBBCodeParser_Exception extends Exception
{
}
class SBBCodeParser_MissingEndTagException extends SBBCodeParser_Exception
{
}
class SBBCodeParser_InvalidNestingException extends SBBCodeParser_Exception
{
}
abstract class SBBCodeParser_Node
{
/**
* Nodes parent
* @var SBBCodeParser_ContainerNode
*/
protected $parent;
/**
* Nodes root parent
* @var SBBCodeParser_ContainerNode
*/
protected $root;
/**
* Sets the nodes parent
* @param SBBCodeParser_Node $parent
*/
public function set_parent(SBBCodeParser_ContainerNode $parent=null)
{
$this->parent = $parent;
if($parent instanceof SBBCodeParser_Document)
$this->root = $parent;
else
$this->root = $parent->root();
}
/**
* Gets the nodes parent. Returns null if there
* is no parent
* @return SBBCodeParser_Node
*/
public function parent()
{
return $this->parent;
}
/**
* @return string
*/
public function get_html()
{
return null;
}
/**
* Gets the nodes root node
* @return SBBCodeParser_Node
*/
public function root()
{
return $this->root;
}
/**
* Finds a parent node of the passed type.
* Returns null if none found.
* @param string $tag
* @return SBBCodeParser_TagNode
*/
public function find_parent_by_tag($tag)
{
$node = $this->parent();
while($this->parent() != null
&& !$node instanceof SBBCodeParser_Document)
{
if($node->tag() === $tag)
return $node;
$node = $node->parent();
}
return null;
}
}
abstract class SBBCodeParser_ContainerNode extends SBBCodeParser_Node
{
/**
* Array of child nodes
* @var array
*/
protected $children = array();
/**
* Adds a SBBCodeParser_Node as a child
* of this node.
* @param $child The child node to add
* @return void
*/
public function add_child(SBBCodeParser_Node $child)
{
$this->children[] = $child;
$child->set_parent($this);
}
/**
* Replaces a child node
* @param SBBCodeParser_Node $what
* @param mixed $with SBBCodeParser_Node or an array of SBBCodeParser_Node
* @return bool
*/
public function replace_child(SBBCodeParser_Node $what, $with)
{
$replace_key = array_search($what, $this->children);
if($replace_key === false)
return false;
if(is_array($with))
foreach($with as $child)
$child->set_parent($this);
array_splice($this->children, $replace_key, 1, $with);
return true;
}
/**
* Removes a child fromthe node
* @param SBBCodeParser_Node $child
* @return bool
*/
public function remove_child(SBBCodeParser_Node $child)
{
$key = array_search($what, $this->children);
if($key === false)
return false;
$this->children[$key]->set_parent();
unset($this->children[$key]);
return true;
}
/**
* Gets the nodes children
* @return array
*/
public function children()
{
return $this->children;
}
/**
* Gets the last child of type SBBCodeParser_TagNode.
* @return SBBCodeParser_TagNode
*/
public function last_tag_node()
{
$children_len = count($this->children);
for($i=$children_len-1; $i >= 0; $i--)
if($this->children[$i] instanceof SBBCodeParser_TagNode)
return $this->children[$i];
return null;
}
/**
* Gets a HTML representation of this node
* @return string
*/
public function get_html($nl2br=true)
{
$html = '';
foreach($this->children as $child)
$html .= $child->get_html($nl2br);
if($this instanceof SBBCodeParser_Document)
return $html;
$bbcode = $this->root()->get_bbcode($this->tag);
if(is_callable($bbcode->handler()) && ($func = $bbcode->handler()) !== false)
return $func($html, $this->attribs, $this);
//return call_user_func($bbcode->handler(), $html, $this->attribs, $this);
return str_replace('%content%', $html, $bbcode->handler());
}
/**
* Gets the raw text content of this node
* and it's children.
*
* The returned text is UNSAFE and should not
* be used without filtering!
* @return string
*/
public function get_text()
{
$text = '';
foreach($this->children as $child)
$text .= $child->get_text();
return $text;
}
}
class SBBCodeParser_TextNode extends SBBCodeParser_Node
{
protected $text;
public function __construct($text)
{
$this->text = $text;
}
public function get_html($nl2br=true)
{
if(!$nl2br)
return str_replace(" ", " ", htmlentities($this->text, ENT_QUOTES | ENT_IGNORE, "UTF-8"));
return str_replace(" ", " ", nl2br(htmlentities($this->text, ENT_QUOTES | ENT_IGNORE, "UTF-8")));
}
public function get_text()
{
return $this->text;
}
}
class SBBCodeParser_TagNode extends SBBCodeParser_ContainerNode
{
/**
* Tag name of this node
* @var string
*/
protected $tag;
/**
* Assoc array of attributes
* @var array
*/
protected $attribs;
public function __construct($tag, $attribs)
{
$this->tag = $tag;
$this->attribs = $attribs;
}
/**
* Gets the tag of this node
* @return string
*/
public function tag()
{
return $this->tag;
}
/**
* Gets the tags attributes
* @return array
*/
public function attributes()
{
return $this->attribs;
}
}
class SBBCodeParser_BBCode
{
/**
* The tag this BBCode applies to
* @var string
*/
protected $tag;
/**
* The BBCodes handler
* @var mixed string or function
*/
protected $handler;
/**
* If the tag is a self closing tag
* @var bool
*/
protected $is_self_closing;
/**
* Array of tags which will cause this tag to close
* if they are encountered before the end of it.
* Used for [*] which may not have a closing tag so
* other [*] or [/list] tags will cause it to be closed·
* @var array
*/
protected $closing_tags;
/**
* Valid child nodes for this tag. Tags like list, table,
* ect. will only accept li, tr, ect. tags and not text nodes
* @var array
*/
protected $accepted_children;
/**
* Which auto detections this BBCode should be excluded from
* @var int
*/
protected $is_inline;
const AUTO_DETECT_EXCLUDE_NONE = 0;
const AUTO_DETECT_EXCLUDE_URL = 2;
const AUTO_DETECT_EXCLUDE_EMAIL = 4;
const AUTO_DETECT_EXCLUDE_EMOTICON = 8;
const AUTO_DETECT_EXCLUDE_ALL = 15;
const BLOCK_TAG = false;
const INLINE_TAG = true;
/**
* Creates a new BBCode
* @param string $tag Tag this BBCode is for
* @param mixed $handler String or function, should return a string
* @param bool $is_inline If this tag is an inline tag or a block tag
* @param array $is_self_closing If this tag is self closing, I.E. doesn't need [/tag]
* @param array $closing_tags Tags which will close this tag
* @param int $accepted_children Tags allowed as children of this BBCode. Can also include text_node
* @param int $auto_detect_exclude Which auto detections to exclude this BBCode from
*/
public function __construct($tag,
$handler,
$is_inline=SBBCodeParser_BBCode::INLINE_TAG,
$is_self_closing=false,
$closing_tags=array(),
$accepted_children=array(),
$auto_detect_exclude=SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_NONE)
{
$this->tag = $tag;
$this->is_inline = $is_inline;
$this->handler = $handler;
$this->is_self_closing = $is_self_closing;
$this->closing_tags = $closing_tags;
$this->accepted_children = $accepted_children;
$this->auto_detect_exclude = $auto_detect_exclude;
}
/**
* Gets the tag name this BBCode is for
* @return string
*/
public function tag()
{
return $this->tag;
}
/**
* Gets if this BBCode is inline or if it's block
* @return bool
*/
public function is_inline()
{
return $this->is_inline;
}
/**
* Gets if this BBCode is self closing
* @return bool
*/
public function is_self_closing()
{
return $this->is_self_closing;
}
/**
* Gets the format string/handler for this BBCode
* @return mixed String or function
*/
public function handler()
{
return $this->handler;
}
/**
* Gets an array of tags which will cause this tag to be closed
* @return array
*/
public function closing_tags()
{
return $this->closing_tags;
}
/**
* Gets an array of tags which are allowed as children of this tag
* @return array
*/
public function accepted_children()
{
return $this->accepted_children;
}
/**
* Which auto detections this BBCode should be excluded from
* @return int
*/
public function auto_detect_exclude()
{
return $this->auto_detect_exclude;
}
}
class SBBCodeParser_Document extends SBBCodeParser_ContainerNode
{
/**
* Current Tag
* @var SBBCodeParser_ContainerNode
*/
protected $current_tag = null;
/**
* Assoc array of all the BBCodes for this document
* @var array
*/
protected $bbcodes = array();
/**
* Base URI to be used for links, images, ect.
* @var string
*/
protected $base_uri = null;
/**
* Assoc array of emoticons in smiley_code => smiley_url format
* @var array
*/
protected $emoticons = array();
/*
* If to throw errors when encountering bad BBCode or
* to silently try and fix
* @var bool
*/
protected $throw_errors = false;
public function __construct($load_defaults=true, $throw_errors=true)
{
$this->throw_errors = $throw_errors;
// Load default BBCodes
if($load_defaults)
$this->add_bbcodes($this->default_bbcodes());
}
/**
* Gets an array of the default BBCodes
* @return array
*/
public static function default_bbcodes()
{
return array(
new SBBCodeParser_BBCode('b', '<strong>%content%</strong>'),
new SBBCodeParser_BBCode('i', '<em>%content%</em>'),
new SBBCodeParser_BBCode('strong', '<strong>%content%</strong>'),
new SBBCodeParser_BBCode('em', '<em>%content%</em>'),
new SBBCodeParser_BBCode('u', '<span style="text-decoration: underline">%content%</span>'),
new SBBCodeParser_BBCode('s', '<span style="text-decoration: line-through">%content%</span>'),
new SBBCodeParser_BBCode('blink', '<span style="text-decoration: blink">%content%</span>'),
new SBBCodeParser_BBCode('sub', '<sub>%content%</sub>'),
new SBBCodeParser_BBCode('sup', '<sup>%content%</sup>'),
new SBBCodeParser_BBCode('ins', '<ins>%content%</ins>'),
new SBBCodeParser_BBCode('del', '<del>%content%</del>'),
new SBBCodeParser_BBCode('right', '<div style="text-align: right">%content%</div>', SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('left', '<div style="text-align: left">%content%</div>', SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('center', '<div style="text-align: center">%content%</div>', SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('justify', '<div style="text-align: justify">%content%</div>', SBBCodeParser_BBCode::BLOCK_TAG),
// notes only show in editing so ignore it
new SBBCodeParser_BBCode('note', ''),
new SBBCodeParser_BBCode('hidden', ''),
new SBBCodeParser_BBCode('abbr', function($content, $attribs)
{
return '<abbr title="' . $attribs['default'] . '">' . $content . '</abbr>';
}),
new SBBCodeParser_BBCode('acronym', function($content, $attribs)
{
return '<acronym title="' . $attribs['default'] . '">' . $content . '</acronym>';
}),
new SBBCodeParser_BBCode('icq', '<a href="http://www.icq.com/people/about_me.php?uin=%content%">
<img src="http://status.icq.com/online.gif?icq=%content%&img=5"> %content%</a>',
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('skype', '<a href="skype:jovisa737590?call">
<img src="http://mystatus.skype.com/bigclassic/%content%" style="border: none;" width="182"
height="44" alt="My status" /></a>',
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('bing', '<a href="http://www.bing.com/search?q=%content%">%content%</a>',
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('google', '<a href="http://www.google.com/search?q=%content%">%content%</a>',
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('wikipedia', '<a href="http://www.wikipedia.org/wiki/%content%">%content%</a>',
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('youtube', function($content, $attribs)
{
if(substr($content, 0, 23) === 'http://www.youtube.com/')
$uri = $content;
else
$uri = 'http://www.youtube.com/v/' . $content;
return '<iframe width="480" height="390" src="' . $uri . '" frameborder="0"></iframe>';
}, SBBCodeParser_BBCode::BLOCK_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('vimeo', function($content, $attribs)
{
if(substr($content, 0, 24) === 'http://player.vimeo.com/')
$uri = $content;
else if(substr($content, 0, 17) === 'http://vimeo.com/'
|| substr($content, 0, 21) === 'http://www.vimeo.com/'
&& preg_match("/http:\/\/(?:www\.)?vimeo\.com\/([0-9]{4,10})/", $content, $matches))
{
preg_match("/http:\/\/(?:www\.)?vimeo\.com\/([0-9]{4,10})/", $content, $matches);
$uri = 'http://player.vimeo.com/video/' . $matches[1] . '?title=0&byline=0&portrait=0';
}
else
$uri = 'http://player.vimeo.com/video/' . $content . '?title=0&byline=0&portrait=0';
return '<iframe src="' . $uri . '" width="400" height="225" frameborder="0"></iframe>';
}, SBBCodeParser_BBCode::BLOCK_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('flash', function($content, $attribs)
{
$width = 640;
$height = 385;
if(substr($content, 0, 4) !== 'http')
$content = $node->root()->get_base_uri() . $content;
if(isset($attribs['width']) && is_numeric($attribs['width']))
$width = $attribs['width'];
if(isset($attribs['height']) && is_numeric($attribs['height']))
$height = $attribs['height'];
// for [flash=200,100] format
if(!empty($attribs['default']))
{
list($w, $h) = explode(',', $attribs['default']);
if($w > 20 && is_numeric($w))
$width = $w;
if($h > 20 && is_numeric($h))
$height = $h;
}
return '<object width="' . $width. '" height="' . $height. '">
<param name="movie" value="' . $content . '"></param>
<embed src="' . $content . '"
type="application/x-shockwave-flash"
width="' . $width. '" height="' . $height. '">
</embed>
</object>';
}, SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('paypal', function($content, $attribs)
{
$content = urlencode($content);
return '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business='
. $content . '&lc=US&no_note=0¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest">
<img src="https://www.paypal.com/en_US/i/btn/x-click-but21.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"/></a>';
},
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('pastebin', function($content, $attribs)
{
if(!preg_match("/^[a-zA-Z0-9]$/", $content))
{
preg_match("#http://pastebin.com/([a-zA-Z0-9]+)#", $content, $matches);
$content = '';
if(isset($matches[1]))
$content = $matches[1];
}
return '<script src="http://pastebin.com/embed_js.php?i=' . $content . '"></script>';
}, SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('gist', function($content, $attribs)
{
if($content != (string)intval($content))
{
preg_match("#https://gist.github.com/([0-9]+)#", $content, $matches);
$content = '';
if(isset($matches[1]))
$content = $matches[1];
}
else
$content = intval($content);
return '<script src="http://gist.github.com/' . $content . '.js"></script>';
}, SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('twitter', '<a href="https://twitter.com/%content%"
class="twitter-follow-button" data-show-count="false">Follow @%content%</a>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>',
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('tweets', "<script src=\"http://widgets.twimg.com/j/2/widget.js\"></script>
<script>
new TWTR.Widget({
version: 2,
type: 'profile',
rpp: 3,
interval: 6000,
width: 400,
height: 150,
theme: {
shell: {
background: '#333333',
color: '#ffffff'
},
tweets: {
background: '#000000',
color: '#ffffff',
links: '#4aed05'
}
},
features: {
scrollbar: false,
loop: false,
live: false,
hashtags: true,
timestamp: true,
avatars: false,
behavior: 'all'
}
}).render().setUser('%content%').start();
</script>",
SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('googlemaps', '<iframe src="http://maps.google.com/maps?q=%content%&output=embed"
scrolling="no" width="100%" height="350" frameborder="0"></iframe>',
SBBCodeParser_BBCode::BLOCK_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('pdf', '<iframe src="http://docs.google.com/gview?url=%content%&embedded=true"
width="100%" height="500" frameborder="0"></iframe>',
SBBCodeParser_BBCode::BLOCK_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('scribd', function($content, $attribs)
{
if(!isset($attribs['id'])
|| !($attribs['id'] = intval($attribs['id'])) > 1)
return 'Invalid scribd ID.';
if(!isset($attribs['key']))
return 'Missing scribd key.';
return '<iframe src="http://www.scribd.com/embeds/' . $attribs['id'] . '/content?start_page=1&view_mode=list&access_key=' . $attribs['key'] . '"
data-auto-height="true" data-aspect-ratio="1" scrolling="no" width="100%"
height="500" frameborder="0"></iframe>
<script type="text/javascript">(function() {
var scribd = document.createElement("script");
scribd.type = "text/javascript";
scribd.async = true;
scribd.src = "http://www.scribd.com/javascripts/embed_code/inject.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(scribd, s);
})();</script>';
}, SBBCodeParser_BBCode::BLOCK_TAG, true, array(), array(), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('spoiler', '<div class="spoiler" style="margin:5px 15px 15px 15px">
<div style="margin:0 0 2px 0; font-weight:bold;">Spoiler:
<input type="button" value="Show"
onclick="if (this.value == \'Show\')
{
this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'block\';
this.value = \'Hide\';
} else {
this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'none\';
this.value = \'Show\';
}" />
</div>
<div style="margin:0; padding:6px; border:1px inset;">
<div style="display: none;">
%content%
</div>
</div>
</div>'),
new SBBCodeParser_BBCode('tt', '<span style="font-family: monospace">%content%</span>'),
new SBBCodeParser_BBCode('pre', function($content, $attribs, $node)
{
$content = '';
foreach($node->children() as $child)
$content .= $child->get_html(false);
return "<pre>{$content}</pre>";
}, SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('code', '<code>%content%</code>',
SBBCodeParser_BBCode::BLOCK_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_EMOTICON),
new SBBCodeParser_BBCode('php', function($content, $attribs, $node)
{
ob_start();
highlight_string($node->get_text());
$content = ob_get_contents();
ob_end_clean();
return "<code class=\"php\">{$content}</code>";
}, SBBCodeParser_BBCode::BLOCK_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_EMOTICON),
new SBBCodeParser_BBCode('quote', function($content, $attribs, $node)
{
$cite = '';
if(!empty($attribs['default']))
$cite = "<cite>{$attribs['default']}:</cite>";
if($node->find_parent_by_tag('quote') !== null)
return "</p><blockquote><p>{$cite}{$content}</p></blockquote><p>";
return "<blockquote><p>{$cite}{$content}</p></blockquote>";
}, SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('font', function($content, $attribs)
{
// Font can have letters, spaces, quotes, - and commas
if(!isset($attribs['default'])
|| !preg_match("/^([A-Za-z\'\",\- ]+)$/", $attribs['default']))
$attribs['default'] = 'Arial';
return '<span style="font-family: ' . $attribs['default'] . '">' . $content . '</span>';
}),
new SBBCodeParser_BBCode('size', function($content, $attribs)
{
$size = 'xx-small';
/*
Font tag sizes 1-7 should be:
1 = xx-small
2 = small
3 = medium
4 = large
5 = x-large
6 = xx-large
7 = ?? in chrome it's 48px
*/
if(!isset($attribs['default']))
$size = 'xx-small';
else if($attribs['default'] == 2)
$size = 'small';
else if($attribs['default'] == 3)
$size = 'medium';
else if($attribs['default'] == 4)
$size = 'large';
else if($attribs['default'] == 5)
$size = 'x-large';
else if($attribs['default'] == 6)
$size = 'xx-large';
else if($attribs['default'] == 7)
$size = '48px';
else if($attribs['default'][strlen($attribs['default']) - 1] === '%'
&& is_numeric(substr($attribs['default'], 0, -1)))
$size = $attribs['default'];
else
{
if(!is_numeric($attribs['default']))
$attribs['default'] = 13;
if($attribs['default'] < 6)
$attribs['default'] = 6;
if($attribs['default'] > 48)
$attribs['default'] = 48;
$size = $attribs['default'] . 'px';
}
return '<span style="font-size: ' . $size . '">' . $content . '</span>';
}),
new SBBCodeParser_BBCode('color', function($content, $attribs)
{
// colour must be either a hex #xxx/#xxxxxx or a word with no spaces red/blue/ect.
if(!isset($attribs['default'])
|| !preg_match("/^(#[a-fA-F0-9]{3,6}|[A-Za-z]+)$/", $attribs['default']))
$attribs['default'] = '#000';
return '<span style="color: ' . $attribs['default'] . '">' . $content . '</span>';
}),
new SBBCodeParser_BBCode('list', function($content, $attribs)
{
$style = 'circle';
$type = 'ul';
switch($attribs['default'])
{
case 'd':
$style = 'disc';
$type = 'ul';
break;
case 's':
$style = 'square';
$type = 'ul';
break;
case '1':
$style = 'decimal';
$type = 'ol';
break;
case 'a':
$style = 'lower-alpha';
$type = 'ol';
break;
case 'A':
$style = 'upper-alpha';
$type = 'ol';
break;
case 'i':
$style = 'lower-roman';
$type = 'ol';
break;
case 'I':
$style = 'upper-roman';
$type = 'ol';
break;
}
return "<{$type} style=\"list-style: {$style}\">{$content}</{$type}>";
}, SBBCodeParser_BBCode::BLOCK_TAG, false, array(), array('*', 'li', 'ul', 'li', 'ol', 'list')),
new SBBCodeParser_BBCode('ul', '<ul>%content%</ul>', SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('ol', '<ol>%content%</ol>', SBBCodeParser_BBCode::BLOCK_TAG),
new SBBCodeParser_BBCode('li', '<li>%content%</li>'),
new SBBCodeParser_BBCode('*', '<li>%content%</li>', SBBCodeParser_BBCode::BLOCK_TAG, false,
array('*', 'li', 'ul', 'li', 'ol', '/list')),
new SBBCodeParser_BBCode('table', '<table>%content%</table>', SBBCodeParser_BBCode::BLOCK_TAG,
false, array(), array('table', 'th', 'h', 'tr', 'row', 'r', 'td', 'col', 'c')),
new SBBCodeParser_BBCode('th', '<th>%content%</th>'),
new SBBCodeParser_BBCode('h', '<th>%content%</th>'),
new SBBCodeParser_BBCode('tr', '<tr>%content%</tr>', SBBCodeParser_BBCode::BLOCK_TAG, false,
array(), array('table', 'th', 'h', 'tr', 'row', 'r', 'td', 'col', 'c')),
new SBBCodeParser_BBCode('row', '<tr>%content%</tr>', SBBCodeParser_BBCode::BLOCK_TAG, false,
array(), array('table', 'th', 'h', 'tr', 'row', 'r', 'td', 'col', 'c')),
new SBBCodeParser_BBCode('r', '<tr>%content%</tr>', SBBCodeParser_BBCode::BLOCK_TAG, false,
array(), array('table', 'th', 'h', 'tr', 'row', 'r', 'td', 'col', 'c')),
new SBBCodeParser_BBCode('td', '<td>%content%</td>'),
new SBBCodeParser_BBCode('col', '<td>%content%</td>'),
new SBBCodeParser_BBCode('c', '<td>%content%</td>'),
new SBBCodeParser_BBCode('notag', '%content%', SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'),
SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('nobbc', '%content%', SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'),
SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('noparse', '%content%', SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'),
SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('h1', '<h1>%content%</h1>'),
new SBBCodeParser_BBCode('h2', '<h2>%content%</h2>'),
new SBBCodeParser_BBCode('h3', '<h3>%content%</h3>'),
new SBBCodeParser_BBCode('h4', '<h4>%content%</h4>'),
new SBBCodeParser_BBCode('h5', '<h5>%content%</h5>'),
new SBBCodeParser_BBCode('h6', '<h6>%content%</h6>'),
new SBBCodeParser_BBCode('h7', '<h7>%content%</h7>'),
new SBBCodeParser_BBCode('big', '<span style="font-size: large">%content%</span>'),
new SBBCodeParser_BBCode('small', '<span style="font-size: x-small">%content%</span>'),
// tables use this tag so can't be a header.
//new SBBCodeParser_BBCode('h', '<h5>%content%</h5>'),
new SBBCodeParser_BBCode('br', '<br />', SBBCodeParser_BBCode::INLINE_TAG, true),
new SBBCodeParser_BBCode('sp', ' ', SBBCodeParser_BBCode::INLINE_TAG, true),
new SBBCodeParser_BBCode('hr', '<hr />', SBBCodeParser_BBCode::INLINE_TAG, true),
new SBBCodeParser_BBCode('anchor', function($content, $attribs, $node)
{
if(empty($attribs['default']))
{
$attribs['default'] = $content;
// remove the content for [anchor]test[/anchor]
// usage as test is the anchor
$content = '';
}
$attribs['default'] = preg_replace('/[^a-zA-Z0-9_\-]+/', '', $attribs['default']);
return "<a name=\"{$attribs['default']}\">{$content}</a>";
}),
new SBBCodeParser_BBCode('goto', function($content, $attribs, $node)
{
if(empty($attribs['default']))
$attribs['default'] = $content;
$attribs['default'] = preg_replace('/[^a-zA-Z0-9_\-#]+/', '', $attribs['default']);
return "<a href=\"#{$attribs['default']}\">{$content}</a>";
}),
new SBBCodeParser_BBCode('jumpto', function($content, $attribs, $node)
{
if(empty($attribs['default']))
$attribs['default'] = $content;
$attribs['default'] = preg_replace('/[^a-zA-Z0-9_\-#]+/', '', $attribs['default']);
return "<a href=\"#{$attribs['default']}\">{$content}</a>";
}),
new SBBCodeParser_BBCode('img', function($content, $attribs, $node)
{
$attrs = '';
// for when default attrib is used for width x height
if(isset($attribs['default']) && preg_match("/[0-9]+[Xx\*][0-9]+/", $attribs['default']))
{
list($attribs['width'],$attribs['height']) = explode('x', $attribs['default']);
$attribs['default'] = '';
}
// for when width & height are specified as the default attrib
else if(isset($attribs['default']) && is_numeric($attribs['default']))
{
$attribs['width'] = $attribs['height'] = $attribs['default'];
$attribs['default'] = '';
}
// add alt tag if is one
if(isset($attribs['default']) && !empty($attribs['default']))
$attrs .= " alt=\"{$attribs['default']}\"";
else if(isset($attribs['alt']))
$attrs .= " alt=\"{$attribs['alt']}\"";
else
$attrs .= " alt=\"{$content}\"";
// width and height can only be numeric, anything else should be ignored to prevent XSS
if(isset($attribs['width']) && is_numeric($attribs['width']))
$attrs .= " width=\"{$attribs['width']}\"";
if(isset($attribs['height']) && is_numeric($attribs['height']))
$attrs .= " height=\"{$attribs['height']}\"";
// add http:// to www starting urls
if(strpos($content, 'www') === 0)
$content = 'http://' . $content;
// add the base url to any urls not starting with http or ftp as they must be relative
else if(substr($content, 0, 4) !== 'http'
&& substr($content, 0, 3) !== 'ftp')
$content = $node->root()->get_base_uri() . $content;
return "<img{$attrs} src=\"{$content}\" />";
}, SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('email', function($content, $attribs, $node)
{
if(empty($attribs['default']))
$attribs['default'] = $content;
return "<a href=\"mailto:{$attribs['default']}\">{$content}</a>";
}, SBBCodeParser_BBCode::INLINE_TAG, false, array(), array('text_node'), SBBCodeParser_BBCode::AUTO_DETECT_EXCLUDE_ALL),
new SBBCodeParser_BBCode('url', function($content, $attribs, $node)
{