]> git.mxchange.org Git - friendica.git/blob - include/bbcode.php
f07c1aa6c38fde13470a5ee2c647602e90e94961
[friendica.git] / include / bbcode.php
1 <?php
2
3 require_once("include/oembed.php");
4 require_once('include/event.php');
5
6 function bb_cleanstyle($st) {
7   return "<span style=\"".cleancss($st[1]).";\">".$st[2]."</span>";
8 }
9
10 function bb_cleanclass($st) {
11   return "<span class=\"".cleancss($st[1])."\">".$st[2]."</span>";
12 }
13
14 function cleancss($input) {
15
16         $cleaned = "";
17
18         $input = strtolower($input);
19
20         for ($i = 0; $i < strlen($input); $i++) {
21                 $char = substr($input, $i, 1);
22
23                 if (($char >= "a") and ($char <= "z"))
24                         $cleaned .= $char;
25
26                 if (!(strpos(" #;:0123456789", $char) === false))
27                         $cleaned .= $char;
28         }
29
30         return($cleaned);
31 }
32
33 function stripcode_br_cb($s) {
34         return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
35 }
36
37 function tryoembed($match){
38         $url = ((count($match)==2)?$match[1]:$match[2]);
39
40         // Always embed the SSL version
41         $url = str_replace("http://www.youtube.com/", "https://www.youtube.com/", $url);
42
43         //logger("tryoembed: $url");
44
45         $o = oembed_fetch_url($url);
46
47         //echo "<pre>"; var_dump($match, $url, $o); killme();
48
49         if ($o->type=="error") return $match[0];
50
51         $html = oembed_format_object($o);
52         return $html; //oembed_iframe($html,$o->width,$o->height);
53
54 }
55
56 // [noparse][i]italic[/i][/noparse] turns into
57 // [noparse][ i ]italic[ /i ][/noparse],
58 // to hide them from parser.
59
60 function bb_spacefy($st) {
61   $whole_match = $st[0];
62   $captured = $st[1];
63   $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
64   $new_str = str_replace($captured, $spacefied, $whole_match);
65   return $new_str;
66 }
67
68 // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
69 // now turns back and the [noparse] tags are trimed
70 // returning [i]italic[/i]
71
72 function bb_unspacefy_and_trim($st) {
73   $whole_match = $st[0];
74   $captured = $st[1];
75   $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
76   return $unspacefied;
77 }
78
79 function bb_find_open_close($s, $open, $close, $occurance = 1) {
80
81         if($occurance < 1)
82                 $occurance = 1;
83
84         $start_pos = -1;
85         for($i = 1; $i <= $occurance; $i++) {
86                 if( $start_pos !== false)
87                         $start_pos = strpos($s, $open, $start_pos + 1);
88         }
89
90         if( $start_pos === false)
91                 return false;
92
93         $end_pos = strpos($s, $close, $start_pos);
94
95         if( $end_pos === false)
96                 return false;
97
98         $res = array( 'start' => $start_pos, 'end' => $end_pos );
99
100         return $res;
101 }
102
103 function get_bb_tag_pos($s, $name, $occurance = 1) {
104
105         if($occurance < 1)
106                 $occurance = 1;
107
108         $start_open = -1;
109         for($i = 1; $i <= $occurance; $i++) {
110                 if( $start_open !== false)
111                         $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
112         }
113
114         if( $start_open === false)
115                 return false;
116
117         $start_equal = strpos($s, '=', $start_open);
118         $start_close = strpos($s, ']', $start_open);
119
120         if( $start_close === false)
121                 return false;
122
123         $start_close++;
124
125         $end_open = strpos($s, '[/' . $name . ']', $start_close);
126
127         if( $end_open === false)
128                 return false;
129
130         $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
131                       'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
132         if( $start_equal !== false)
133                 $res['start']['equal'] = $start_equal + 1;
134
135         return $res;
136 }
137
138 function bb_tag_preg_replace($pattern, $replace, $name, $s) {
139
140         $string = $s;
141
142         $occurance = 1;
143         $pos = get_bb_tag_pos($string, $name, $occurance);
144         while($pos !== false && $occurance < 1000) {
145
146                 $start = substr($string, 0, $pos['start']['open']);
147                 $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
148                 $end = substr($string, $pos['end']['close']);
149                 if($end === false)
150                         $end = '';
151
152                 $subject = preg_replace($pattern, $replace, $subject);
153                 $string = $start . $subject . $end;
154
155                 $occurance++;
156                 $pos = get_bb_tag_pos($string, $name, $occurance);
157         }
158
159         return $string;
160 }
161
162 if(! function_exists('bb_extract_images')) {
163 function bb_extract_images($body) {
164
165         $saved_image = array();
166         $orig_body = $body;
167         $new_body = '';
168
169         $cnt = 0;
170         $img_start = strpos($orig_body, '[img');
171         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
172         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
173         while(($img_st_close !== false) && ($img_end !== false)) {
174
175                 $img_st_close++; // make it point to AFTER the closing bracket
176                 $img_end += $img_start;
177
178                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
179                         // This is an embedded image
180
181                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
182                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
183
184                         $cnt++;
185                 }
186                 else
187                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
188
189                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
190
191                 if($orig_body === false) // in case the body ends on a closing image tag
192                         $orig_body = '';
193
194                 $img_start = strpos($orig_body, '[img');
195                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
196                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
197         }
198
199         $new_body = $new_body . $orig_body;
200
201         return array('body' => $new_body, 'images' => $saved_image);
202 }}
203
204 if(! function_exists('bb_replace_images')) {
205 function bb_replace_images($body, $images) {
206
207         $newbody = $body;
208
209         $cnt = 0;
210         foreach($images as $image) {
211                 // We're depending on the property of 'foreach' (specified on the PHP website) that
212                 // it loops over the array starting from the first element and going sequentially
213                 // to the last element
214                 $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . $image .'" alt="' . t('Image/photo') . '" />', $newbody);
215                 $cnt++;
216         }
217
218         return $newbody;
219 }}
220
221 function bb_ShareAttributes($match) {
222
223         $attributes = $match[1];
224
225         $author = "";
226         preg_match("/author='(.*?)'/ism", $attributes, $matches);
227         if ($matches[1] != "")
228                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
229
230         preg_match('/author="(.*?)"/ism', $attributes, $matches);
231         if ($matches[1] != "")
232                 $author = $matches[1];
233
234         $link = "";
235         preg_match("/link='(.*?)'/ism", $attributes, $matches);
236         if ($matches[1] != "")
237                 $link = $matches[1];
238
239         preg_match('/link="(.*?)"/ism', $attributes, $matches);
240         if ($matches[1] != "")
241                 $link = $matches[1];
242
243         $avatar = "";
244         preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
245         if ($matches[1] != "")
246                 $avatar = $matches[1];
247
248         preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
249         if ($matches[1] != "")
250                 $avatar = $matches[1];
251
252         $profile = "";
253         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
254         if ($matches[1] != "")
255                 $profile = $matches[1];
256
257         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
258         if ($matches[1] != "")
259                 $profile = $matches[1];
260
261         $posted = "";
262         preg_match("/posted='(.*?)'/ism", $attributes, $matches);
263         if ($matches[1] != "")
264                 $posted = $matches[1];
265
266         preg_match('/posted="(.*?)"/ism', $attributes, $matches);
267         if ($matches[1] != "")
268                 $posted = $matches[1];
269                 $reldate = (($posted) ? " " . relative_date($posted) : '');
270
271         $headline = '<br /><div class="shared_header">';
272
273         if ($avatar != "")
274                 $headline .= '<img src="'.$avatar.'" height="32" width="32" >';
275
276         $headline .= sprintf(t('<span><a href="%s" target="external-link">%s</a> wrote the following <a href="%s" target="external-link">post</a>'.$reldate.':</span>'), $profile, $author, $link);
277
278         $headline .= "</div>";
279
280         $text = $headline.'<blockquote class="shared_content">'.trim($match[2])."</blockquote>";
281
282         return($text);
283 }
284
285 function bb_ShareAttributesSimple($match) {
286
287         $attributes = $match[1];
288
289         $author = "";
290         preg_match("/author='(.*?)'/ism", $attributes, $matches);
291         if ($matches[1] != "")
292                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
293
294         preg_match('/author="(.*?)"/ism', $attributes, $matches);
295         if ($matches[1] != "")
296                 $author = $matches[1];
297
298         $profile = "";
299         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
300         if ($matches[1] != "")
301                 $profile = $matches[1];
302
303         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
304         if ($matches[1] != "")
305                 $profile = $matches[1];
306
307         $userid = GetProfileUsername($profile,$author);
308
309         $text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />»".$match[2]."«";
310
311         return($text);
312 }
313 function bb_ShareAttributesSimple2($match) {
314
315         $attributes = $match[1];
316
317         $author = "";
318         preg_match("/author='(.*?)'/ism", $attributes, $matches);
319         if ($matches[1] != "")
320                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
321
322         preg_match('/author="(.*?)"/ism', $attributes, $matches);
323         if ($matches[1] != "")
324                 $author = $matches[1];
325
326         $profile = "";
327         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
328         if ($matches[1] != "")
329                 $profile = $matches[1];
330
331         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
332         if ($matches[1] != "")
333                 $profile = $matches[1];
334
335         $userid = GetProfileUsername($profile,$author);
336
337         $text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />".$match[2];
338
339         return($text);
340 }
341
342 function GetProfileUsername($profile, $username) {
343         $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2@$1", $profile);
344         if ($friendica != $profile)
345                 return($friendica);
346
347         $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
348         if ($diaspora != $profile)
349                 return($diaspora);
350
351         $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
352         if ($StatusnetHost != $profile) {
353                 $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
354                 if ($StatusnetUser != $profile) {
355                         $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
356                         $user = json_decode($UserData);
357                         if ($user)
358                                 return($user->screen_name."@".$StatusnetHost);
359                 }
360         }
361
362         return($username);
363 }
364
365         // BBcode 2 HTML was written by WAY2WEB.net
366         // extended to work with Mistpark/Friendica - Mike Macgirvin
367
368 function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = false) {
369
370         $stamp1 = microtime(true);
371
372         $a = get_app();
373
374         // Hide all [noparse] contained bbtags by spacefying them
375         // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
376
377         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
378         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
379         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
380
381
382         // Move all spaces out of the tags
383         $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
384         $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
385
386         // Extract the private images which use data url's since preg has issues with
387         // large data sizes. Stash them away while we do bbcode conversion, and then put them back
388         // in after we've done all the regex matching. We cannot use any preg functions to do this.
389
390         $extracted = bb_extract_images($Text);
391         $Text = $extracted['body'];
392         $saved_image = $extracted['images'];
393
394         // If we find any event code, turn it into an event.
395         // After we're finished processing the bbcode we'll
396         // replace all of the event code with a reformatted version.
397
398         $ev = bbtoevent($Text);
399
400
401         // Replace any html brackets with HTML Entities to prevent executing HTML or script
402         // Don't use strip_tags here because it breaks [url] search by replacing & with amp
403
404         $Text = str_replace("<", "&lt;", $Text);
405         $Text = str_replace(">", "&gt;", $Text);
406
407         // remove some newlines before the general conversion
408         $Text = preg_replace("/\s?\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","[share$1]$2[/share]",$Text);
409         $Text = preg_replace("/\s?\[quote(.*?)\]\s?(.*?)\s?\[\/quote\]\s?/ism","[quote$1]$2[/quote]",$Text);
410
411         $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
412         $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $Text);
413
414         // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
415         if (!$tryoembed)
416                 $Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
417
418         // Convert new line chars to html <br /> tags
419
420         // nlbr seems to be hopelessly messed up
421         //      $Text = nl2br($Text);
422
423         // We'll emulate it.
424
425         $Text = trim($Text);
426         $Text = str_replace("\r\n","\n", $Text);
427
428         // removing multiplicated newlines
429         if (get_config("system", "remove_multiplicated_lines")) {
430                 $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n");
431                 $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]");
432                 do {
433                         $oldtext = $Text;
434                         $Text = str_replace($search, $replace, $Text);
435                 } while ($oldtext != $Text);
436         }
437
438         $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
439
440         if($preserve_nl)
441                 $Text = str_replace(array("\n","\r"), array('',''),$Text);
442
443
444
445         // Set up the parameters for a URL search string
446         $URLSearchString = "^\[\]";
447         // Set up the parameters for a MAIL search string
448         $MAILSearchString = $URLSearchString;
449
450
451         // Perform URL Search
452
453         $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
454
455         if ($tryoembed)
456                 $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
457
458         $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
459
460         if ($tryoembed)
461                 $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
462
463         $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="external-link">$1</a>', $Text);
464         $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
465         //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
466
467         // Red compatibility, though the link can't be authenticated on Friendica
468         $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
469
470
471         // we may need to restrict this further if it picks up too many strays
472         // link acct:user@host to a webfinger profile redirector
473
474         $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2" 
475                 . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
476
477         // Perform MAIL Search
478         $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
479         $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
480
481         // Check for bold text
482         $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
483
484         // Check for Italics text
485         $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
486
487         // Check for Underline text
488         $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
489
490         // Check for strike-through text
491         $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
492
493         // Check for over-line text
494         $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
495
496         // Check for colored text
497         $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
498
499         // Check for sized text
500         // [size=50] --> font-size: 50px (with the unit).
501         $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px;\">$2</span>",$Text);
502         $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1;\">$2</span>",$Text);
503
504         // Check for centered text
505         $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
506
507         // Check for list text
508         $Text = str_replace("[*]", "<li>", $Text);
509
510         // Check for style sheet commands
511         $Text = preg_replace_callback("(\[style=(.*?)\](.*?)\[\/style\])ism","bb_cleanstyle",$Text);
512
513         // Check for CSS classes
514         $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanclass",$Text);
515
516         // handle nested lists
517         $endlessloop = 0;
518
519         while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
520                ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || 
521                ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) || 
522                ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
523                 $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
524                 $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
525                 $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
526                 $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
527                 $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
528                 $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
529                 $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
530                 $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
531                 $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
532                 $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
533         }
534
535         $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
536         $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
537         $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
538         $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
539
540         $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
541         $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
542
543         $Text = str_replace('[hr]','<hr />', $Text);
544
545         // This is actually executed in prepare_body()
546
547         $Text = str_replace('[nosmile]','',$Text);
548
549         // Check for font change text
550         $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
551
552         // Declare the format for [code] layout
553
554 //      $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
555
556         $CodeLayout = '<code>$1</code>';
557         // Check for [code] text
558         $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
559
560         // Declare the format for [spoiler] layout
561         $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
562
563         // Check for [spoiler] text
564         // handle nested quotes
565         $endlessloop = 0;
566         while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
567                 $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
568
569         // Check for [spoiler=Author] text
570
571         $t_wrote = t('$1 wrote:');
572
573         // handle nested quotes
574         $endlessloop = 0;
575         while ((strpos($Text, "[/spoiler]")!== false)  and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
576                 $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
577                                      "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
578                                      $Text);
579
580         // Declare the format for [quote] layout
581         $QuoteLayout = '<blockquote>$1</blockquote>';
582
583         // Check for [quote] text
584         // handle nested quotes
585         $endlessloop = 0;
586         while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
587                 $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
588
589         // Check for [quote=Author] text
590
591         $t_wrote = t('$1 wrote:');
592
593         // handle nested quotes
594         $endlessloop = 0;
595         while ((strpos($Text, "[/quote]")!== false)  and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
596                 $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
597                                      "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
598                                      $Text);
599
600         // [img=widthxheight]image source[/img]
601         //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
602         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
603         $Text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
604
605         // Images
606         // [img]pathtoimage[/img]
607         $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
608         $Text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
609
610         // Shared content
611         if (!$simplehtml)
612                 $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributes",$Text);
613         elseif ($simplehtml == 1)
614                 $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesSimple",$Text);
615         elseif ($simplehtml == 2)
616                 $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesSimple2",$Text);
617
618         $Text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br />', $Text);
619         $Text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
620         //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
621
622
623         // Try to Oembed
624         if ($tryoembed) {
625                 $Text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4))\[\/video\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></video>', $Text);
626                 $Text = preg_replace("/\[audio\](.*?\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\[\/audio\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
627
628                 $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
629                 $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
630         } else {
631                 $Text = preg_replace("/\[video\](.*?)\[\/video\]/", '$1', $Text);
632                 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '$1', $Text);
633         }
634
635         // html5 video and audio
636
637
638         if ($tryoembed)
639                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
640         else
641                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
642
643         // Youtube extensions
644         if ($tryoembed) {
645                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
646                 $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
647                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text);
648         }
649
650         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
651         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
652         $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
653
654         if ($tryoembed)
655                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
656         else
657                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", "https://www.youtube.com/watch?v=$1", $Text);
658
659
660         if ($tryoembed) {
661                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
662                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
663         }
664
665         $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text); 
666         $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
667
668         if ($tryoembed)
669                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
670         else
671                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", "http://vimeo.com/$1", $Text);
672
673 //      $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);
674
675
676         // oembed tag
677         $Text = oembed_bbcode2html($Text);
678
679         // Avoid triple linefeeds through oembed
680         $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
681
682         // If we found an event earlier, strip out all the event code and replace with a reformatted version.
683         // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
684         // Summary (e.g. title) is required, earlier revisions only required description (in addition to 
685         // start which is always required). Allow desc with a missing summary for compatibility.
686
687         if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
688                 $sub = format_event_html($ev);
689
690                 $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
691                 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
692                 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text); 
693                 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
694                 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
695                 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
696         }
697
698         // Unhide all [noparse] contained bbtags unspacefying them 
699         // and triming the [noparse] tag.
700
701         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
702         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
703         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
704
705
706         $Text = preg_replace('/\[\&amp\;([#a-z0-9]+)\;\]/','&$1;',$Text);
707         $Text = preg_replace('/\&\#039\;/','\'',$Text);
708         $Text = preg_replace('/\&quot\;/','"',$Text);
709
710         // fix any escaped ampersands that may have been converted into links
711         $Text = preg_replace("/\<([^>]*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
712         $Text = preg_replace("/\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|cid)(.*?)\>/ism",'<$1$2="">',$Text);
713
714         if($saved_image)
715                 $Text = bb_replace_images($Text, $saved_image);
716
717         // Clean up the HTML by loading and saving the HTML with the DOM
718         // Only do it when it has to be done - for performance reasons
719         // Update: Now it is done every time - since bad structured html can break a whole page
720         //if (!$tryoembed) {
721         //      $doc = new DOMDocument();
722         //      $doc->preserveWhiteSpace = false;
723
724         //      $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
725
726         //      $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
727         //      @$doc->loadHTML($doctype."<html><body>".$Text."</body></html>");
728
729         //      $Text = $doc->saveHTML();
730         //      $Text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $Text);
731
732         //      $Text = str_replace('<br></li>','</li>', $Text);
733
734         //      $Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
735         //}
736
737         // Clean up some useless linebreaks in lists
738         //$Text = str_replace('<br /><ul','<ul ', $Text);
739         //$Text = str_replace('</ul><br />','</ul>', $Text);
740         //$Text = str_replace('</li><br />','</li>', $Text);
741         //$Text = str_replace('<br /><li>','<li>', $Text);
742         //      $Text = str_replace('<br /><ul','<ul ', $Text);
743
744         // Remove all hashtag addresses
745         if (!$tryoembed AND get_config("system", "remove_hashtags_on_export")) {
746                 $pattern = '/#<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
747                 $Text = preg_replace($pattern, '#$2', $Text);
748         }
749
750         call_hooks('bbcode',$Text);
751
752         $a->save_timestamp($stamp1, "parser");
753
754         return $Text;
755 }