]> git.mxchange.org Git - friendica.git/blob - include/bbcode.php
removed buggy debug code ;)
[friendica.git] / include / bbcode.php
1 <?php
2 require_once("include/oembed.php");
3         // BBcode 2 HTML was written by WAY2WEB.net
4         // extended to work with Mistpark/Friendika - Mike Macgirvin
5
6 function bbcode($Text,$preserve_nl = false) {
7
8         // Replace any html brackets with HTML Entities to prevent executing HTML or script
9         // Don't use strip_tags here because it breaks [url] search by replacing & with amp
10
11         $Text = str_replace("<", "&lt;", $Text);
12         $Text = str_replace(">", "&gt;", $Text);
13
14         // Convert new line chars to html <br /> tags
15
16         $Text = nl2br($Text);
17         if($preserve_nl)
18                 $Text = str_replace(array("\n","\r"), array('',''),$Text);
19
20         // Set up the parameters for a URL search string
21         $URLSearchString = "^\[\]";
22         // Set up the parameters for a MAIL search string
23         $MAILSearchString = $URLSearchString;
24
25         // Perform URL Search
26
27
28         $Text = preg_replace("/([^\]\=]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'\%\$\!\+\,]+)/", ' <a href="$2" target="external-link">$2</a>', $Text);
29
30         $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="$1" target="external-link">$1</a>', $Text);
31         $Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="$1" target="external-link">$2</a>', $Text);
32         //$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);
33
34
35         // Perform MAIL Search
36         $Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text);
37         $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
38          
39         // Check for bold text
40         $Text = preg_replace("(\[b\](.+?)\[\/b])is",'<strong>$1</strong>',$Text);
41
42         // Check for Italics text
43         $Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<em>$1</em>',$Text);
44
45         // Check for Underline text
46         $Text = preg_replace("(\[u\](.+?)\[\/u\])is",'<u>$1</u>',$Text);
47
48         // Check for strike-through text
49         $Text = preg_replace("(\[s\](.+?)\[\/s\])is",'<strike>$1</strike>',$Text);
50
51         // Check for over-line text
52         $Text = preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text);
53
54         // Check for colored text
55         $Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1;\">$2</span>",$Text);
56
57         // Check for sized text
58         $Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1;\">$2</span>",$Text);
59
60         // Check for list text
61         $Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text);
62         $Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text);
63         $Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s",'<ul class="listlowerroman">$1</ul>' ,$Text);
64         $Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text);
65         $Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text);
66         $Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text);
67         $Text = str_replace("[*]", "<li>", $Text);
68
69         // Check for font change text
70         $Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);
71
72         // Declare the format for [code] layout
73         $CodeLayout = '<code>$1</code>';
74         // Check for [code] text
75         $Text = preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout", $Text);
76         // Declare the format for [quote] layout
77         $QuoteLayout = '<blockquote>$1</blockquote>';                     
78         // Check for [quote] text
79         $Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout", $Text);
80          
81         // Images
82         // [img]pathtoimage[/img]
83         $Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
84
85         // html5 video and audio
86
87         $Text = preg_replace("/\[video\](.+?)\[\/video\]/", '<video src="$1" controls="controls" width="425" height="350"><a href="$1">$1</a></video>', $Text);
88
89         $Text = preg_replace("/\[audio\](.+?)\[\/audio\]/", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
90
91          
92         // [img=widthxheight]image source[/img]
93         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" height="$2" width="$1">', $Text);
94
95         // Youtube extensions
96         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.+?)\[\/youtube\]/",'[youtube]$1[/youtube]',$Text); 
97         $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.+?)\[\/youtube\]/",'[youtube]$1[/youtube]',$Text); 
98
99         $Text = preg_replace("/\[youtube\](.+?)\[\/youtube\]/", '<iframe width="425" height="349" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $Text);
100
101 //      $Text = preg_replace("/\[youtube\](.+?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
102
103         // oembed tag
104         $Text = oembed_bbcode2html($Text);
105
106         call_hooks('bbcode',$Text);
107
108         return $Text;
109 }