1 rizwank 1.1 <?php
2 /***************************************************************************
3 * bbcode.php
4 * -------------------
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
8 *
9 * $Id: bbcode.php,v 1.36.2.19 2003/01/10 13:21:24 psotfx Exp $
10 *
11 ***************************************************************************/
12
13 /***************************************************************************
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 ***************************************************************************/
21
22 rizwank 1.1 if ( !defined('IN_PHPBB') )
23 {
24 die("Hacking attempt");
25 }
26
27 define("BBCODE_UID_LEN", 10);
28
29 // global that holds loaded-and-prepared bbcode templates, so we only have to do
30 // that stuff once.
31
32 $bbcode_tpl = null;
33
34 /**
35 * Loads bbcode templates from the bbcode.tpl file of the current template set.
36 * Creates an array, keys are bbcode names like "b_open" or "url", values
37 * are the associated template.
38 * Probably pukes all over the place if there's something really screwed
39 * with the bbcode.tpl file.
40 *
41 * Nathan Codding, Sept 26 2001.
42 */
43 rizwank 1.1 function load_bbcode_template()
44 {
45 global $template;
46 $tpl_filename = $template->make_filename('bbcode.tpl');
47 $tpl = fread(fopen($tpl_filename, 'r'), filesize($tpl_filename));
48
49 // replace \ with \\ and then ' with \'.
50 $tpl = str_replace('\\', '\\\\', $tpl);
51 $tpl = str_replace('\'', '\\\'', $tpl);
52
53 // strip newlines.
54 $tpl = str_replace("\n", '', $tpl);
55
56 // Turn template blocks into PHP assignment statements for the values of $bbcode_tpls..
57 $tpl = preg_replace('#<!-- BEGIN (.*?) -->(.*?)<!-- END (.*?) -->#', "\n" . '$bbcode_tpls[\'\\1\'] = \'\\2\';', $tpl);
58
59 $bbcode_tpls = array();
60
61 eval($tpl);
62
63 return $bbcode_tpls;
64 rizwank 1.1 }
65
66
67 /**
68 * Prepares the loaded bbcode templates for insertion into preg_replace()
69 * or str_replace() calls in the bbencode_second_pass functions. This
70 * means replacing template placeholders with the appropriate preg backrefs
71 * or with language vars. NOTE: If you change how the regexps work in
72 * bbencode_second_pass(), you MUST change this function.
73 *
74 * Nathan Codding, Sept 26 2001
75 *
76 */
77 function prepare_bbcode_template($bbcode_tpl)
78 {
79 global $lang;
80
81 $bbcode_tpl['olist_open'] = str_replace('{LIST_TYPE}', '\\1', $bbcode_tpl['olist_open']);
82
83 $bbcode_tpl['color_open'] = str_replace('{COLOR}', '\\1', $bbcode_tpl['color_open']);
84
85 rizwank 1.1 $bbcode_tpl['size_open'] = str_replace('{SIZE}', '\\1', $bbcode_tpl['size_open']);
86
87 $bbcode_tpl['quote_open'] = str_replace('{L_QUOTE}', $lang['Quote'], $bbcode_tpl['quote_open']);
88
89 $bbcode_tpl['quote_username_open'] = str_replace('{L_QUOTE}', $lang['Quote'], $bbcode_tpl['quote_username_open']);
90 $bbcode_tpl['quote_username_open'] = str_replace('{L_WROTE}', $lang['wrote'], $bbcode_tpl['quote_username_open']);
91 $bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $bbcode_tpl['quote_username_open']);
92
93 $bbcode_tpl['code_open'] = str_replace('{L_CODE}', $lang['Code'], $bbcode_tpl['code_open']);
94
95 $bbcode_tpl['img'] = str_replace('{URL}', '\\1', $bbcode_tpl['img']);
96
97 // We do URLs in several different ways..
98 $bbcode_tpl['url1'] = str_replace('{URL}', '\1\2', $bbcode_tpl['url']);
99 $bbcode_tpl['url1'] = str_replace('{DESCRIPTION}', '\1\2', $bbcode_tpl['url1']);
100
101 $bbcode_tpl['url2'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
102 $bbcode_tpl['url2'] = str_replace('{DESCRIPTION}', '\\1', $bbcode_tpl['url2']);
103
104 $bbcode_tpl['url3'] = str_replace('{URL}', '\\1\\2', $bbcode_tpl['url']);
105 $bbcode_tpl['url3'] = str_replace('{DESCRIPTION}', '\\6', $bbcode_tpl['url3']);
106 rizwank 1.1
107 $bbcode_tpl['url4'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
108 $bbcode_tpl['url4'] = str_replace('{DESCRIPTION}', '\\5', $bbcode_tpl['url4']);
109
110 $bbcode_tpl['email'] = str_replace('{EMAIL}', '\\1', $bbcode_tpl['email']);
111
112 define("BBCODE_TPL_READY", true);
113
114 return $bbcode_tpl;
115 }
116
117
118 /**
119 * Does second-pass bbencoding. This should be used before displaying the message in
120 * a thread. Assumes the message is already first-pass encoded, and we are given the
121 * correct UID as used in first-pass encoding.
122 */
123 function bbencode_second_pass($text, $uid)
124 {
125 global $lang, $bbcode_tpl;
126
127 rizwank 1.1 // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
128 // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
129 $text = " " . $text;
130
131 // First: If there isn't a "[" and a "]" in the message, don't bother.
132 if (! (strpos($text, "[") && strpos($text, "]")) )
133 {
134 // Remove padding, return.
135 $text = substr($text, 1);
136 return $text;
137 }
138
139 // Only load the templates ONCE..
140 if (!defined("BBCODE_TPL_READY"))
141 {
142 // load templates from file into array.
143 $bbcode_tpl = load_bbcode_template();
144
145 // prepare array for use in regexps.
146 $bbcode_tpl = prepare_bbcode_template($bbcode_tpl);
147 }
148 rizwank 1.1
149 // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
150 $text = bbencode_second_pass_code($text, $uid, $bbcode_tpl);
151
152 // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
153 $text = str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text);
154 $text = str_replace("[/quote:$uid]", $bbcode_tpl['quote_close'], $text);
155
156 // New one liner to deal with opening quotes with usernames...
157 // replaces the two line version that I had here before..
158 $text = preg_replace("/\[quote:$uid=\"(.*?)\"\]/si", $bbcode_tpl['quote_username_open'], $text);
159
160 // [list] and [list=x] for (un)ordered lists.
161 // unordered lists
162 $text = str_replace("[list:$uid]", $bbcode_tpl['ulist_open'], $text);
163 // li tags
164 $text = str_replace("[*:$uid]", $bbcode_tpl['listitem'], $text);
165 // ending tags
166 $text = str_replace("[/list:u:$uid]", $bbcode_tpl['ulist_close'], $text);
167 $text = str_replace("[/list:o:$uid]", $bbcode_tpl['olist_close'], $text);
168 // Ordered lists
169 rizwank 1.1 $text = preg_replace("/\[list=([a1]):$uid\]/si", $bbcode_tpl['olist_open'], $text);
170
171 // colours
172 $text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", $bbcode_tpl['color_open'], $text);
173 $text = str_replace("[/color:$uid]", $bbcode_tpl['color_close'], $text);
174
175 // size
176 $text = preg_replace("/\[size=([1-2]?[0-9]):$uid\]/si", $bbcode_tpl['size_open'], $text);
177 $text = str_replace("[/size:$uid]", $bbcode_tpl['size_close'], $text);
178
179 // [b] and [/b] for bolding text.
180 $text = str_replace("[b:$uid]", $bbcode_tpl['b_open'], $text);
181 $text = str_replace("[/b:$uid]", $bbcode_tpl['b_close'], $text);
182
183 // [u] and [/u] for underlining text.
184 $text = str_replace("[u:$uid]", $bbcode_tpl['u_open'], $text);
185 $text = str_replace("[/u:$uid]", $bbcode_tpl['u_close'], $text);
186
187 // [i] and [/i] for italicizing text.
188 $text = str_replace("[i:$uid]", $bbcode_tpl['i_open'], $text);
189 $text = str_replace("[/i:$uid]", $bbcode_tpl['i_close'], $text);
190 rizwank 1.1
191 // Patterns and replacements for URL and email tags..
192 $patterns = array();
193 $replacements = array();
194
195 // [img]image_url_here[/img] code..
196 // This one gets first-passed..
197 $patterns[] = "#\[img:$uid\](.*?)\[/img:$uid\]#si";
198 $replacements[] = $bbcode_tpl['img'];
199
200 // [url]xxxx://www.phpbb.com[/url] code..
201 $patterns[] = "#\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]#is";
202 $replacements[] = $bbcode_tpl['url1'];
203
204 // [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).
205 $patterns[] = "#\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]#si";
206 $replacements[] = $bbcode_tpl['url2'];
207
208 // [url=xxxx://www.phpbb.com]phpBB[/url] code..
209 $patterns[] = "#\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]#si";
210 $replacements[] = $bbcode_tpl['url3'];
211 rizwank 1.1
212 // [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).
213 $patterns[] = "#\[url=(([\w\-]+\.)*?[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\](.*?)\[/url\]#si";
214 $replacements[] = $bbcode_tpl['url4'];
215
216 // [email]user@domain.tld[/email] code..
217 $patterns[] = "#\[email\]([a-z0-9\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
218 $replacements[] = $bbcode_tpl['email'];
219
220 $text = preg_replace($patterns, $replacements, $text);
221
222 // Remove our padding from the string..
223 $text = substr($text, 1);
224
225 return $text;
226
227 } // bbencode_second_pass()
228
229 // Need to initialize the random numbers only ONCE
230 mt_srand( (double) microtime() * 1000000);
231
232 rizwank 1.1 function make_bbcode_uid()
233 {
234 // Unique ID for this message..
235
236 $uid = md5(mt_rand());
237 $uid = substr($uid, 0, BBCODE_UID_LEN);
238
239 return $uid;
240 }
241
242 function bbencode_first_pass($text, $uid)
243 {
244 // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
245 // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
246 $text = " " . $text;
247
248 // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
249 $text = bbencode_first_pass_pda($text, $uid, '[code]', '[/code]', '', true, '');
250
251 // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
252 $text = bbencode_first_pass_pda($text, $uid, '[quote]', '[/quote]', '', false, '');
253 rizwank 1.1 $text = bbencode_first_pass_pda($text, $uid, '/\[quote=(\\\".*?\\\")\]/is', '[/quote]', '', false, '', "[quote:$uid=\\1]");
254
255 // [list] and [list=x] for (un)ordered lists.
256 $open_tag = array();
257 $open_tag[0] = "[list]";
258
259 // unordered..
260 $text = bbencode_first_pass_pda($text, $uid, $open_tag, "[/list]", "[/list:u]", false, 'replace_listitems');
261
262 $open_tag[0] = "[list=1]";
263 $open_tag[1] = "[list=a]";
264
265 // ordered.
266 $text = bbencode_first_pass_pda($text, $uid, $open_tag, "[/list]", "[/list:o]", false, 'replace_listitems');
267
268 // [color] and [/color] for setting text color
269 $text = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "[color=\\1:$uid]\\2[/color:$uid]", $text);
270
271 // [size] and [/size] for setting text size
272 $text = preg_replace("#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si", "[size=\\1:$uid]\\2[/size:$uid]", $text);
273
274 rizwank 1.1 // [b] and [/b] for bolding text.
275 $text = preg_replace("#\[b\](.*?)\[/b\]#si", "[b:$uid]\\1[/b:$uid]", $text);
276
277 // [u] and [/u] for underlining text.
278 $text = preg_replace("#\[u\](.*?)\[/u\]#si", "[u:$uid]\\1[/u:$uid]", $text);
279
280 // [i] and [/i] for italicizing text.
281 $text = preg_replace("#\[i\](.*?)\[/i\]#si", "[i:$uid]\\1[/i:$uid]", $text);
282
283 // [img]image_url_here[/img] code..
284 $text = preg_replace("#\[img\]((ht|f)tp://)([^\r\n\t<\"]*?)\[/img\]#sie", "'[img:$uid]\\1' . str_replace(' ', '%20', '\\3') . '[/img:$uid]'", $text);
285
286 // Remove our padding from the string..
287 return substr($text, 1);;
288
289 } // bbencode_first_pass()
290
291 /**
292 * $text - The text to operate on.
293 * $uid - The UID to add to matching tags.
294 * $open_tag - The opening tag to match. Can be an array of opening tags.
295 rizwank 1.1 * $close_tag - The closing tag to match.
296 * $close_tag_new - The closing tag to replace with.
297 * $mark_lowest_level - boolean - should we specially mark the tags that occur
298 * at the lowest level of nesting? (useful for [code], because
299 * we need to match these tags first and transform HTML tags
300 * in their contents..
301 * $func - This variable should contain a string that is the name of a function.
302 * That function will be called when a match is found, and passed 2
303 * parameters: ($text, $uid). The function should return a string.
304 * This is used when some transformation needs to be applied to the
305 * text INSIDE a pair of matching tags. If this variable is FALSE or the
306 * empty string, it will not be executed.
307 * If open_tag is an array, then the pda will try to match pairs consisting of
308 * any element of open_tag followed by close_tag. This allows us to match things
309 * like [list=A]...[/list] and [list=1]...[/list] in one pass of the PDA.
310 *
311 * NOTES: - this function assumes the first character of $text is a space.
312 * - every opening tag and closing tag must be of the [...] format.
313 */
314 function bbencode_first_pass_pda($text, $uid, $open_tag, $close_tag, $close_tag_new, $mark_lowest_level, $func, $open_regexp_replace = false)
315 {
316 rizwank 1.1 $open_tag_count = 0;
317
318 if (!$close_tag_new || ($close_tag_new == ''))
319 {
320 $close_tag_new = $close_tag;
321 }
322
323 $close_tag_length = strlen($close_tag);
324 $close_tag_new_length = strlen($close_tag_new);
325 $uid_length = strlen($uid);
326
327 $use_function_pointer = ($func && ($func != ''));
328
329 $stack = array();
330
331 if (is_array($open_tag))
332 {
333 if (0 == count($open_tag))
334 {
335 // No opening tags to match, so return.
336 return $text;
337 rizwank 1.1 }
338 $open_tag_count = count($open_tag);
339 }
340 else
341 {
342 // only one opening tag. make it into a 1-element array.
343 $open_tag_temp = $open_tag;
344 $open_tag = array();
345 $open_tag[0] = $open_tag_temp;
346 $open_tag_count = 1;
347 }
348
349 $open_is_regexp = false;
350
351 if ($open_regexp_replace)
352 {
353 $open_is_regexp = true;
354 if (!is_array($open_regexp_replace))
355 {
356 $open_regexp_temp = $open_regexp_replace;
357 $open_regexp_replace = array();
358 rizwank 1.1 $open_regexp_replace[0] = $open_regexp_temp;
359 }
360 }
361
362 if ($mark_lowest_level && $open_is_regexp)
363 {
364 message_die(GENERAL_ERROR, "Unsupported operation for bbcode_first_pass_pda().");
365 }
366
367 // Start at the 2nd char of the string, looking for opening tags.
368 $curr_pos = 1;
369 while ($curr_pos && ($curr_pos < strlen($text)))
370 {
371 $curr_pos = strpos($text, "[", $curr_pos);
372
373 // If not found, $curr_pos will be 0, and the loop will end.
374 if ($curr_pos)
375 {
376 // We found a [. It starts at $curr_pos.
377 // check if it's a starting or ending tag.
378 $found_start = false;
379 rizwank 1.1 $which_start_tag = "";
380 $start_tag_index = -1;
381
382 for ($i = 0; $i < $open_tag_count; $i++)
383 {
384 // Grab everything until the first "]"...
385 $possible_start = substr($text, $curr_pos, strpos($text, ']', $curr_pos + 1) - $curr_pos + 1);
386
387 //
388 // We're going to try and catch usernames with "[' characters.
389 //
390 if( preg_match('#\[quote=\\\"#si', $possible_start, $match) && !preg_match('#\[quote=\\\"(.*?)\\\"\]#si', $possible_start) )
391 {
392 // OK we are in a quote tag that probably contains a ] bracket.
393 // Grab a bit more of the string to hopefully get all of it..
394 if ($close_pos = strpos($text, '"]', $curr_pos + 9))
395 {
396 $possible_start = substr($text, $curr_pos, $close_pos - $curr_pos + 2);
397 }
398 }
399
400 rizwank 1.1 // Now compare, either using regexp or not.
401 if ($open_is_regexp)
402 {
403 $match_result = array();
404 if (preg_match($open_tag[$i], $possible_start, $match_result))
405 {
406 $found_start = true;
407 $which_start_tag = $match_result[0];
408 $start_tag_index = $i;
409 break;
410 }
411 }
412 else
413 {
414 // straightforward string comparison.
415 if (0 == strcasecmp($open_tag[$i], $possible_start))
416 {
417 $found_start = true;
418 $which_start_tag = $open_tag[$i];
419 $start_tag_index = $i;
420 break;
421 rizwank 1.1 }
422 }
423 }
424
425 if ($found_start)
426 {
427 // We have an opening tag.
428 // Push its position, the text we matched, and its index in the open_tag array on to the stack, and then keep going to the right.
429 $match = array("pos" => $curr_pos, "tag" => $which_start_tag, "index" => $start_tag_index);
430 bbcode_array_push($stack, $match);
431 //
432 // Rather than just increment $curr_pos
433 // Set it to the ending of the tag we just found
434 // Keeps error in nested tag from breaking out
435 // of table structure..
436 //
437 $curr_pos += strlen($possible_start);
438 }
439 else
440 {
441 // check for a closing tag..
442 rizwank 1.1 $possible_end = substr($text, $curr_pos, $close_tag_length);
443 if (0 == strcasecmp($close_tag, $possible_end))
444 {
445 // We have an ending tag.
446 // Check if we've already found a matching starting tag.
447 if (sizeof($stack) > 0)
448 {
449 // There exists a starting tag.
450 $curr_nesting_depth = sizeof($stack);
451 // We need to do 2 replacements now.
452 $match = bbcode_array_pop($stack);
453 $start_index = $match['pos'];
454 $start_tag = $match['tag'];
455 $start_length = strlen($start_tag);
456 $start_tag_index = $match['index'];
457
458 if ($open_is_regexp)
459 {
460 $start_tag = preg_replace($open_tag[$start_tag_index], $open_regexp_replace[$start_tag_index], $start_tag);
461 }
462
463 rizwank 1.1 // everything before the opening tag.
464 $before_start_tag = substr($text, 0, $start_index);
465
466 // everything after the opening tag, but before the closing tag.
467 $between_tags = substr($text, $start_index + $start_length, $curr_pos - $start_index - $start_length);
468
469 // Run the given function on the text between the tags..
470 if ($use_function_pointer)
471 {
472 $between_tags = $func($between_tags, $uid);
473 }
474
475 // everything after the closing tag.
476 $after_end_tag = substr($text, $curr_pos + $close_tag_length);
477
478 // Mark the lowest nesting level if needed.
479 if ($mark_lowest_level && ($curr_nesting_depth == 1))
480 {
481 if ($open_tag[0] == '[code]')
482 {
483 $code_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#');
484 rizwank 1.1 $code_entities_replace = array('<', '>', '"', ':', '[', ']', '(', ')', '{', '}');
485 $between_tags = preg_replace($code_entities_match, $code_entities_replace, $between_tags);
486 }
487 $text = $before_start_tag . substr($start_tag, 0, $start_length - 1) . ":$curr_nesting_depth:$uid]";
488 $text .= $between_tags . substr($close_tag_new, 0, $close_tag_new_length - 1) . ":$curr_nesting_depth:$uid]";
489 }
490 else
491 {
492 if ($open_tag[0] == '[code]')
493 {
494 $text = $before_start_tag . '[code]';
495 $text .= $between_tags . '[/code]';
496 }
497 else
498 {
499 if ($open_is_regexp)
500 {
501 $text = $before_start_tag . $start_tag;
502 }
503 else
504 {
505 rizwank 1.1 $text = $before_start_tag . substr($start_tag, 0, $start_length - 1) . ":$uid]";
506 }
507 $text .= $between_tags . substr($close_tag_new, 0, $close_tag_new_length - 1) . ":$uid]";
508 }
509 }
510
511 $text .= $after_end_tag;
512
513 // Now.. we've screwed up the indices by changing the length of the string.
514 // So, if there's anything in the stack, we want to resume searching just after it.
515 // otherwise, we go back to the start.
516 if (sizeof($stack) > 0)
517 {
518 $match = bbcode_array_pop($stack);
519 $curr_pos = $match['pos'];
520 // bbcode_array_push($stack, $match);
521 // ++$curr_pos;
522 }
523 else
524 {
525 $curr_pos = 1;
526 rizwank 1.1 }
527 }
528 else
529 {
530 // No matching start tag found. Increment pos, keep going.
531 ++$curr_pos;
532 }
533 }
534 else
535 {
536 // No starting tag or ending tag.. Increment pos, keep looping.,
537 ++$curr_pos;
538 }
539 }
540 }
541 } // while
542
543 return $text;
544
545 } // bbencode_first_pass_pda()
546
547 rizwank 1.1 /**
548 * Does second-pass bbencoding of the [code] tags. This includes
549 * running htmlspecialchars() over the text contained between
550 * any pair of [code] tags that are at the first level of
551 * nesting. Tags at the first level of nesting are indicated
552 * by this format: [code:1:$uid] ... [/code:1:$uid]
553 * Other tags are in this format: [code:$uid] ... [/code:$uid]
554 */
555 function bbencode_second_pass_code($text, $uid, $bbcode_tpl)
556 {
557 global $lang;
558
559 $code_start_html = $bbcode_tpl['code_open'];
560 $code_end_html = $bbcode_tpl['code_close'];
561
562 // First, do all the 1st-level matches. These need an htmlspecialchars() run,
563 // so they have to be handled differently.
564 $match_count = preg_match_all("#\[code:1:$uid\](.*?)\[/code:1:$uid\]#si", $text, $matches);
565
566 for ($i = 0; $i < $match_count; $i++)
567 {
568 rizwank 1.1 $before_replace = $matches[1][$i];
569 $after_replace = $matches[1][$i];
570
571 // Replace 2 spaces with " " so non-tabbed code indents without making huge long lines.
572 $after_replace = str_replace(" ", " ", $after_replace);
573 // now Replace 2 spaces with " " to catch odd #s of spaces.
574 $after_replace = str_replace(" ", " ", $after_replace);
575
576 // Replace tabs with " " so tabbed code indents sorta right without making huge long lines.
577 $after_replace = str_replace("\t", " ", $after_replace);
578
579 $str_to_match = "[code:1:$uid]" . $before_replace . "[/code:1:$uid]";
580
581 $replacement = $code_start_html;
582 $replacement .= $after_replace;
583 $replacement .= $code_end_html;
584
585 $text = str_replace($str_to_match, $replacement, $text);
586 }
587
588 // Now, do all the non-first-level matches. These are simple.
589 rizwank 1.1 $text = str_replace("[code:$uid]", $code_start_html, $text);
590 $text = str_replace("[/code:$uid]", $code_end_html, $text);
591
592 return $text;
593
594 } // bbencode_second_pass_code()
595
596 /**
597 * Rewritten by Nathan Codding - Feb 6, 2001.
598 * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
599 * to that URL
600 * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
601 * to http://www.xxxx.yyyy[/zzzz]
602 * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
603 * to that email address
604 * - Only matches these 2 patterns either after a space, or at the beginning of a line
605 *
606 * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
607 * have it require something like xxxx@yyyy.zzzz or such. We'll see.
608 */
609 function make_clickable($text)
610 rizwank 1.1 {
611
612 // pad it with a space so we can match things at the start of the 1st line.
613 $ret = ' ' . $text;
614
615 // matches an "xxxx://yyyy" URL at the start of a line, or after a space.
616 // xxxx can only be alpha characters.
617 // yyyy is anything up to the first space, newline, comma, double quote or <
618 $ret = preg_replace("#([\t\r\n ])([a-z0-9]+?){1}://([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i", '\1<a href="\2://\3" target="_blank">\2://\3</a>', $ret);
619
620 // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
621 // Must contain at least 2 dots. xxxx contains either alphanum, or "-"
622 // zzzz is optional.. will contain everything up to the first space, newline,
623 // comma, double quote or <.
624 $ret = preg_replace("#([\t\r\n ])(www|ftp)\.(([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i", '\1<a href="http://\2.\3" target="_blank">\2.\3</a>', $ret);
625
626 // matches an email@domain type address at the start of a line, or after a space.
627 // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
628 $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
629
630 // Remove our padding..
631 rizwank 1.1 $ret = substr($ret, 1);
632
633 return($ret);
634 }
635
636 /**
637 * Nathan Codding - Feb 6, 2001
638 * Reverses the effects of make_clickable(), for use in editpost.
639 * - Does not distinguish between "www.xxxx.yyyy" and "http://aaaa.bbbb" type URLs.
640 *
641 */
642 function undo_make_clickable($text)
643 {
644 $text = preg_replace("#<!-- BBCode auto-link start --><a href=\"(.*?)\" target=\"_blank\">.*?</a><!-- BBCode auto-link end -->#i", "\\1", $text);
645 $text = preg_replace("#<!-- BBcode auto-mailto start --><a href=\"mailto:(.*?)\">.*?</a><!-- BBCode auto-mailto end -->#i", "\\1", $text);
646
647 return $text;
648
649 }
650
651 /**
652 rizwank 1.1 * Nathan Codding - August 24, 2000.
653 * Takes a string, and does the reverse of the PHP standard function
654 * htmlspecialchars().
655 */
656 function undo_htmlspecialchars($input)
657 {
658 $input = preg_replace("/>/i", ">", $input);
659 $input = preg_replace("/</i", "<", $input);
660 $input = preg_replace("/"/i", "\"", $input);
661 $input = preg_replace("/&/i", "&", $input);
662
663 return $input;
664 }
665
666 /**
667 * This is used to change a [*] tag into a [*:$uid] tag as part
668 * of the first-pass bbencoding of [list] tags. It fits the
669 * standard required in order to be passed as a variable
670 * function into bbencode_first_pass_pda().
671 */
672 function replace_listitems($text, $uid)
673 rizwank 1.1 {
674 $text = str_replace("[*]", "[*:$uid]", $text);
675
676 return $text;
677 }
678
679 /**
680 * Escapes the "/" character with "\/". This is useful when you need
681 * to stick a runtime string into a PREG regexp that is being delimited
682 * with slashes.
683 */
684 function escape_slashes($input)
685 {
686 $output = str_replace('/', '\/', $input);
687 return $output;
688 }
689
690 /**
691 * This function does exactly what the PHP4 function array_push() does
692 * however, to keep phpBB compatable with PHP 3 we had to come up with our own
693 * method of doing it.
694 rizwank 1.1 */
695 function bbcode_array_push(&$stack, $value)
696 {
697 $stack[] = $value;
698 return(sizeof($stack));
699 }
700
701 /**
702 * This function does exactly what the PHP4 function array_pop() does
703 * however, to keep phpBB compatable with PHP 3 we had to come up with our own
704 * method of doing it.
705 */
706 function bbcode_array_pop(&$stack)
707 {
708 $arrSize = count($stack);
709 $x = 1;
710
711 while(list($key, $val) = each($stack))
712 {
713 if($x < count($stack))
714 {
715 rizwank 1.1 $tmpArr[] = $val;
716 }
717 else
718 {
719 $return_val = $val;
720 }
721 $x++;
722 }
723 $stack = $tmpArr;
724
725 return($return_val);
726 }
727
728 //
729 // Smilies code ... would this be better tagged on to the end of bbcode.php?
730 // Probably so and I'll move it before B2
731 //
732 function smilies_pass($message)
733 {
734 static $orig, $repl;
735
736 rizwank 1.1 if (!isset($orig))
737 {
738 global $db, $board_config;
739 $orig = $repl = array();
740
741 $sql = 'SELECT code, smile_url FROM ' . SMILIES_TABLE;
742 if( !$result = $db->sql_query($sql) )
743 {
744 message_die(GENERAL_ERROR, "Couldn't obtain smilies data", "", __LINE__, __FILE__, $sql);
745 }
746 $smilies = $db->sql_fetchrowset($result);
747
748 usort($smilies, 'smiley_sort');
749 for($i = 0; $i < count($smilies); $i++)
750 {
751 $orig[] = "/(?<=.\W|\W.|^\W)" . phpbb_preg_quote($smilies[$i]['code'], "/") . "(?=.\W|\W.|\W$)/";
752 $repl[] = '<img src="'. $board_config['smilies_path'] . '/' . $smilies[$i]['smile_url'] . '" alt="' . $smilies[$i]['smile_url'] . '" border="0" />';
753 }
754 }
755
756 if (count($orig))
757 rizwank 1.1 {
758 $message = preg_replace($orig, $repl, ' ' . $message . ' ');
759 $message = substr($message, 1, -1);
760 }
761 return $message;
762 }
763
764 function smiley_sort($a, $b)
765 {
766 if ( strlen($a['code']) == strlen($b['code']) )
767 {
768 return 0;
769 }
770
771 return ( strlen($a['code']) > strlen($b['code']) ) ? -1 : 1;
772 }
773
774
775 ?>
|