[ Index ]

PHP Cross Reference of phpBB 2.0.23

title

Body

[close]

/includes/ -> bbcode.php (source)

   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 5589 2006-02-26 17:35:17Z grahamje $
  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  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  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  }
  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      $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', $bbcode_tpl['url']);
  99      $bbcode_tpl['url1'] = str_replace('{DESCRIPTION}', '\\1', $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', $bbcode_tpl['url']);
 105      $bbcode_tpl['url3'] = str_replace('{DESCRIPTION}', '\\2', $bbcode_tpl['url3']);
 106  
 107      $bbcode_tpl['url4'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
 108      $bbcode_tpl['url4'] = str_replace('{DESCRIPTION}', '\\3', $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      $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1&#058;", $text);
 128  
 129      // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
 130      // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
 131      $text = " " . $text;
 132  
 133      // First: If there isn't a "[" and a "]" in the message, don't bother.
 134      if (! (strpos($text, "[") && strpos($text, "]")) )
 135      {
 136          // Remove padding, return.
 137          $text = substr($text, 1);
 138          return $text;
 139      }
 140  
 141      // Only load the templates ONCE..
 142      if (!defined("BBCODE_TPL_READY"))
 143      {
 144          // load templates from file into array.
 145          $bbcode_tpl = load_bbcode_template();
 146  
 147          // prepare array for use in regexps.
 148          $bbcode_tpl = prepare_bbcode_template($bbcode_tpl);
 149      }
 150  
 151      // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
 152      $text = bbencode_second_pass_code($text, $uid, $bbcode_tpl);
 153  
 154      // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
 155      $text = str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text);
 156      $text = str_replace("[/quote:$uid]", $bbcode_tpl['quote_close'], $text);
 157  
 158      // New one liner to deal with opening quotes with usernames...
 159      // replaces the two line version that I had here before..
 160      $text = preg_replace("/\[quote:$uid=\"(.*?)\"\]/si", $bbcode_tpl['quote_username_open'], $text);
 161  
 162      // [list] and [list=x] for (un)ordered lists.
 163      // unordered lists
 164      $text = str_replace("[list:$uid]", $bbcode_tpl['ulist_open'], $text);
 165      // li tags
 166      $text = str_replace("[*:$uid]", $bbcode_tpl['listitem'], $text);
 167      // ending tags
 168      $text = str_replace("[/list:u:$uid]", $bbcode_tpl['ulist_close'], $text);
 169      $text = str_replace("[/list:o:$uid]", $bbcode_tpl['olist_close'], $text);
 170      // Ordered lists
 171      $text = preg_replace("/\[list=([a1]):$uid\]/si", $bbcode_tpl['olist_open'], $text);
 172  
 173      // colours
 174      $text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", $bbcode_tpl['color_open'], $text);
 175      $text = str_replace("[/color:$uid]", $bbcode_tpl['color_close'], $text);
 176  
 177      // size
 178      $text = preg_replace("/\[size=([1-2]?[0-9]):$uid\]/si", $bbcode_tpl['size_open'], $text);
 179      $text = str_replace("[/size:$uid]", $bbcode_tpl['size_close'], $text);
 180  
 181      // [b] and [/b] for bolding text.
 182      $text = str_replace("[b:$uid]", $bbcode_tpl['b_open'], $text);
 183      $text = str_replace("[/b:$uid]", $bbcode_tpl['b_close'], $text);
 184  
 185      // [u] and [/u] for underlining text.
 186      $text = str_replace("[u:$uid]", $bbcode_tpl['u_open'], $text);
 187      $text = str_replace("[/u:$uid]", $bbcode_tpl['u_close'], $text);
 188  
 189      // [i] and [/i] for italicizing text.
 190      $text = str_replace("[i:$uid]", $bbcode_tpl['i_open'], $text);
 191      $text = str_replace("[/i:$uid]", $bbcode_tpl['i_close'], $text);
 192  
 193      // Patterns and replacements for URL and email tags..
 194      $patterns = array();
 195      $replacements = array();
 196  
 197      // [img]image_url_here[/img] code..
 198      // This one gets first-passed..
 199      $patterns[] = "#\[img:$uid\]([^?](?:[^\[]+|\[(?!url))*?)\[/img:$uid\]#i";
 200      $replacements[] = $bbcode_tpl['img'];
 201  
 202      // matches a [url]xxxx://www.phpbb.com[/url] code..
 203      $patterns[] = "#\[url\]([\w]+?://([\w\#$%&~/.\-;:=,?@\]+]+|\[(?!url=))*?)\[/url\]#is";
 204      $replacements[] = $bbcode_tpl['url1'];
 205  
 206      // [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).
 207      $patterns[] = "#\[url\]((www|ftp)\.([\w\#$%&~/.\-;:=,?@\]+]+|\[(?!url=))*?)\[/url\]#is";
 208      $replacements[] = $bbcode_tpl['url2'];
 209  
 210      // [url=xxxx://www.phpbb.com]phpBB[/url] code..
 211      $patterns[] = "#\[url=([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is";
 212      $replacements[] = $bbcode_tpl['url3'];
 213  
 214      // [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).
 215      $patterns[] = "#\[url=((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is";
 216      $replacements[] = $bbcode_tpl['url4'];
 217  
 218      // [email]user@domain.tld[/email] code..
 219      $patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
 220      $replacements[] = $bbcode_tpl['email'];
 221  
 222      $text = preg_replace($patterns, $replacements, $text);
 223  
 224      // Remove our padding from the string..
 225      $text = substr($text, 1);
 226  
 227      return $text;
 228  
 229  } // bbencode_second_pass()
 230  
 231  // Need to initialize the random numbers only ONCE
 232  mt_srand( (double) microtime() * 1000000);
 233  
 234  function make_bbcode_uid()
 235  {
 236      // Unique ID for this message..
 237  
 238      $uid = dss_rand();
 239      $uid = substr($uid, 0, BBCODE_UID_LEN);
 240  
 241      return $uid;
 242  }
 243  
 244  function bbencode_first_pass($text, $uid)
 245  {
 246      // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
 247      // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
 248      $text = " " . $text;
 249  
 250      // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
 251      $text = bbencode_first_pass_pda($text, $uid, '[code]', '[/code]', '', true, '');
 252  
 253      // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
 254      $text = bbencode_first_pass_pda($text, $uid, '[quote]', '[/quote]', '', false, '');
 255      $text = bbencode_first_pass_pda($text, $uid, '/\[quote=\\\\&quot;(.*?)\\\\&quot;\]/is', '[/quote]', '', false, '', "[quote:$uid=\\\"\\1\\\"]");
 256  
 257      // [list] and [list=x] for (un)ordered lists.
 258      $open_tag = array();
 259      $open_tag[0] = "[list]";
 260  
 261      // unordered..
 262      $text = bbencode_first_pass_pda($text, $uid, $open_tag, "[/list]", "[/list:u]", false, 'replace_listitems');
 263  
 264      $open_tag[0] = "[list=1]";
 265      $open_tag[1] = "[list=a]";
 266  
 267      // ordered.
 268      $text = bbencode_first_pass_pda($text, $uid, $open_tag, "[/list]", "[/list:o]",  false, 'replace_listitems');
 269  
 270      // [color] and [/color] for setting text color
 271      $text = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "[color=\\1:$uid]\\2[/color:$uid]", $text);
 272  
 273      // [size] and [/size] for setting text size
 274      $text = preg_replace("#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si", "[size=\\1:$uid]\\2[/size:$uid]", $text);
 275  
 276      // [b] and [/b] for bolding text.
 277      $text = preg_replace("#\[b\](.*?)\[/b\]#si", "[b:$uid]\\1[/b:$uid]", $text);
 278  
 279      // [u] and [/u] for underlining text.
 280      $text = preg_replace("#\[u\](.*?)\[/u\]#si", "[u:$uid]\\1[/u:$uid]", $text);
 281  
 282      // [i] and [/i] for italicizing text.
 283      $text = preg_replace("#\[i\](.*?)\[/i\]#si", "[i:$uid]\\1[/i:$uid]", $text);
 284  
 285      // [img]image_url_here[/img] code..
 286      $text = preg_replace("#\[img\]((http|ftp|https|ftps)://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png)))\[/img\]#sie", "'[img:$uid]\\1' . str_replace(' ', '%20', '\\3') . '[/img:$uid]'", $text);
 287  
 288      // Remove our padding from the string..
 289      return substr($text, 1);;
 290  
 291  } // bbencode_first_pass()
 292  
 293  /**
 294   * $text - The text to operate on.
 295   * $uid - The UID to add to matching tags.
 296   * $open_tag - The opening tag to match. Can be an array of opening tags.
 297   * $close_tag - The closing tag to match.
 298   * $close_tag_new - The closing tag to replace with.
 299   * $mark_lowest_level - boolean - should we specially mark the tags that occur
 300   *                     at the lowest level of nesting? (useful for [code], because
 301   *                        we need to match these tags first and transform HTML tags
 302   *                        in their contents..
 303   * $func - This variable should contain a string that is the name of a function.
 304   *                That function will be called when a match is found, and passed 2
 305   *                parameters: ($text, $uid). The function should return a string.
 306   *                This is used when some transformation needs to be applied to the
 307   *                text INSIDE a pair of matching tags. If this variable is FALSE or the
 308   *                empty string, it will not be executed.
 309   * If open_tag is an array, then the pda will try to match pairs consisting of
 310   * any element of open_tag followed by close_tag. This allows us to match things
 311   * like [list=A]...[/list] and [list=1]...[/list] in one pass of the PDA.
 312   *
 313   * NOTES:    - this function assumes the first character of $text is a space.
 314   *                - every opening tag and closing tag must be of the [...] format.
 315   */
 316  function bbencode_first_pass_pda($text, $uid, $open_tag, $close_tag, $close_tag_new, $mark_lowest_level, $func, $open_regexp_replace = false)
 317  {
 318      $open_tag_count = 0;
 319  
 320      if (!$close_tag_new || ($close_tag_new == ''))
 321      {
 322          $close_tag_new = $close_tag;
 323      }
 324  
 325      $close_tag_length = strlen($close_tag);
 326      $close_tag_new_length = strlen($close_tag_new);
 327      $uid_length = strlen($uid);
 328  
 329      $use_function_pointer = ($func && ($func != ''));
 330  
 331      $stack = array();
 332  
 333      if (is_array($open_tag))
 334      {
 335          if (0 == count($open_tag))
 336          {
 337              // No opening tags to match, so return.
 338              return $text;
 339          }
 340          $open_tag_count = count($open_tag);
 341      }
 342      else
 343      {
 344          // only one opening tag. make it into a 1-element array.
 345          $open_tag_temp = $open_tag;
 346          $open_tag = array();
 347          $open_tag[0] = $open_tag_temp;
 348          $open_tag_count = 1;
 349      }
 350  
 351      $open_is_regexp = false;
 352  
 353      if ($open_regexp_replace)
 354      {
 355          $open_is_regexp = true;
 356          if (!is_array($open_regexp_replace))
 357          {
 358              $open_regexp_temp = $open_regexp_replace;
 359              $open_regexp_replace = array();
 360              $open_regexp_replace[0] = $open_regexp_temp;
 361          }
 362      }
 363  
 364      if ($mark_lowest_level && $open_is_regexp)
 365      {
 366          message_die(GENERAL_ERROR, "Unsupported operation for bbcode_first_pass_pda().");
 367      }
 368  
 369      // Start at the 2nd char of the string, looking for opening tags.
 370      $curr_pos = 1;
 371      while ($curr_pos && ($curr_pos < strlen($text)))
 372      {
 373          $curr_pos = strpos($text, "[", $curr_pos);
 374  
 375          // If not found, $curr_pos will be 0, and the loop will end.
 376          if ($curr_pos)
 377          {
 378              // We found a [. It starts at $curr_pos.
 379              // check if it's a starting or ending tag.
 380              $found_start = false;
 381              $which_start_tag = "";
 382              $start_tag_index = -1;
 383  
 384              for ($i = 0; $i < $open_tag_count; $i++)
 385              {
 386                  // Grab everything until the first "]"...
 387                  $possible_start = substr($text, $curr_pos, strpos($text, ']', $curr_pos + 1) - $curr_pos + 1);
 388  
 389                  //
 390                  // We're going to try and catch usernames with "[' characters.
 391                  //
 392                  if( preg_match('#\[quote=\\\&quot;#si', $possible_start, $match) && !preg_match('#\[quote=\\\&quot;(.*?)\\\&quot;\]#si', $possible_start) )
 393                  {
 394                      // OK we are in a quote tag that probably contains a ] bracket.
 395                      // Grab a bit more of the string to hopefully get all of it..
 396                      if ($close_pos = strpos($text, '&quot;]', $curr_pos + 14))
 397                      {
 398                          if (strpos(substr($text, $curr_pos + 14, $close_pos - ($curr_pos + 14)), '[quote') === false)
 399                          {
 400                              $possible_start = substr($text, $curr_pos, $close_pos - $curr_pos + 7);
 401                          }
 402                      }
 403                  }
 404  
 405                  // Now compare, either using regexp or not.
 406                  if ($open_is_regexp)
 407                  {
 408                      $match_result = array();
 409                      if (preg_match($open_tag[$i], $possible_start, $match_result))
 410                      {
 411                          $found_start = true;
 412                          $which_start_tag = $match_result[0];
 413                          $start_tag_index = $i;
 414                          break;
 415                      }
 416                  }
 417                  else
 418                  {
 419                      // straightforward string comparison.
 420                      if (0 == strcasecmp($open_tag[$i], $possible_start))
 421                      {
 422                          $found_start = true;
 423                          $which_start_tag = $open_tag[$i];
 424                          $start_tag_index = $i;
 425                          break;
 426                      }
 427                  }
 428              }
 429  
 430              if ($found_start)
 431              {
 432                  // We have an opening tag.
 433                  // 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.
 434                  $match = array("pos" => $curr_pos, "tag" => $which_start_tag, "index" => $start_tag_index);
 435                  array_push($stack, $match);
 436                  //
 437                  // Rather than just increment $curr_pos
 438                  // Set it to the ending of the tag we just found
 439                  // Keeps error in nested tag from breaking out
 440                  // of table structure..
 441                  //
 442                  $curr_pos += strlen($possible_start);
 443              }
 444              else
 445              {
 446                  // check for a closing tag..
 447                  $possible_end = substr($text, $curr_pos, $close_tag_length);
 448                  if (0 == strcasecmp($close_tag, $possible_end))
 449                  {
 450                      // We have an ending tag.
 451                      // Check if we've already found a matching starting tag.
 452                      if (sizeof($stack) > 0)
 453                      {
 454                          // There exists a starting tag.
 455                          $curr_nesting_depth = sizeof($stack);
 456                          // We need to do 2 replacements now.
 457                          $match = array_pop($stack);
 458                          $start_index = $match['pos'];
 459                          $start_tag = $match['tag'];
 460                          $start_length = strlen($start_tag);
 461                          $start_tag_index = $match['index'];
 462  
 463                          if ($open_is_regexp)
 464                          {
 465                              $start_tag = preg_replace($open_tag[$start_tag_index], $open_regexp_replace[$start_tag_index], $start_tag);
 466                          }
 467  
 468                          // everything before the opening tag.
 469                          $before_start_tag = substr($text, 0, $start_index);
 470  
 471                          // everything after the opening tag, but before the closing tag.
 472                          $between_tags = substr($text, $start_index + $start_length, $curr_pos - $start_index - $start_length);
 473  
 474                          // Run the given function on the text between the tags..
 475                          if ($use_function_pointer)
 476                          {
 477                              $between_tags = $func($between_tags, $uid);
 478                          }
 479  
 480                          // everything after the closing tag.
 481                          $after_end_tag = substr($text, $curr_pos + $close_tag_length);
 482  
 483                          // Mark the lowest nesting level if needed.
 484                          if ($mark_lowest_level && ($curr_nesting_depth == 1))
 485                          {
 486                              if ($open_tag[0] == '[code]')
 487                              {
 488                                  $code_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#');
 489                                  $code_entities_replace = array('&lt;', '&gt;', '&quot;', '&#58;', '&#91;', '&#93;', '&#40;', '&#41;', '&#123;', '&#125;');
 490                                  $between_tags = preg_replace($code_entities_match, $code_entities_replace, $between_tags);
 491                              }
 492                              $text = $before_start_tag . substr($start_tag, 0, $start_length - 1) . ":$curr_nesting_depth:$uid]";
 493