[ Index ]
 

Code source de PHP NUKE 7.9

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/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,v 1.36.2.35 2005/07/19 20:01:10 acydburn 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  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      $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1&#058;", $text);
 127  
 128      // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).

 129      // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.

 130      $text = " " . $text;
 131  
 132      // First: If there isn't a "[" and a "]" in the message, don't bother.

 133      if (! (strpos($text, "[") && strpos($text, "]")) )
 134      {
 135          // Remove padding, return.

 136          $text = substr($text, 1);
 137          return $text;
 138      }
 139  
 140      // Only load the templates ONCE..

 141      if (!defined("BBCODE_TPL_READY"))
 142      {
 143          // load templates from file into array.

 144          $bbcode_tpl = load_bbcode_template();
 145  
 146          // prepare array for use in regexps.

 147          $bbcode_tpl = prepare_bbcode_template($bbcode_tpl);
 148      }
 149  
 150      // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.

 151      $text = bbencode_second_pass_code($text, $uid, $bbcode_tpl);
 152  
 153      // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.

 154      $text = str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text);
 155      $text = str_replace("[/quote:$uid]", $bbcode_tpl['quote_close'], $text);
 156  
 157      // New one liner to deal with opening quotes with usernames...

 158      // replaces the two line version that I had here before..

 159      $text = preg_replace("/\[quote:$uid=\"(.*?)\"\]/si", $bbcode_tpl['quote_username_open'], $text);
 160  
 161      // [list] and [list=x] for (un)ordered lists.

 162      // unordered lists

 163      $text = str_replace("[list:$uid]", $bbcode_tpl['ulist_open'], $text);
 164      // li tags

 165      $text = str_replace("[*:$uid]", $bbcode_tpl['listitem'], $text);
 166      // ending tags

 167      $text = str_replace("[/list:u:$uid]", $bbcode_tpl['ulist_close'], $text);
 168      $text = str_replace("[/list:o:$uid]", $bbcode_tpl['olist_close'], $text);
 169      // Ordered lists

 170      $text = preg_replace("/\[list=([a1]):$uid\]/si", $bbcode_tpl['olist_open'], $text);
 171  
 172      // colours

 173      $text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", $bbcode_tpl['color_open'], $text);
 174      $text = str_replace("[/color:$uid]", $bbcode_tpl['color_close'], $text);
 175  
 176      // size

 177      $text = preg_replace("/\[size=([1-2]?[0-9]):$uid\]/si", $bbcode_tpl['size_open'], $text);
 178      $text = str_replace("[/size:$uid]", $bbcode_tpl['size_close'], $text);
 179  
 180      // [b] and [/b] for bolding text.

 181      $text = str_replace("[b:$uid]", $bbcode_tpl['b_open'], $text);
 182      $text = str_replace("[/b:$uid]", $bbcode_tpl['b_close'], $text);
 183  
 184      // [u] and [/u] for underlining text.

 185      $text = str_replace("[u:$uid]", $bbcode_tpl['u_open'], $text);
 186      $text = str_replace("[/u:$uid]", $bbcode_tpl['u_close'], $text);
 187  
 188      // [i] and [/i] for italicizing text.

 189      $text = str_replace("[i:$uid]", $bbcode_tpl['i_open'], $text);
 190      $text = str_replace("[/i:$uid]", $bbcode_tpl['i_close'], $text);
 191  
 192      // Patterns and replacements for URL and email tags..

 193      $patterns = array();
 194      $replacements = array();
 195  
 196      // [img]image_url_here[/img] code..

 197      // This one gets first-passed..

 198      $patterns[] = "#\[img:$uid\]([^?].*?)\[/img:$uid\]#i";
 199      $replacements[] = $bbcode_tpl['img'];
 200  
 201      // matches a [url]xxxx://www.phpbb.com[/url] code..

 202      $patterns[] = "#\[url\]([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\[/url\]#is";
 203      $replacements[] = $bbcode_tpl['url1'];
 204  
 205      // [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).

 206      $patterns[] = "#\[url\]((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*?)\[/url\]#is";
 207      $replacements[] = $bbcode_tpl['url2'];
 208  
 209      // [url=xxxx://www.phpbb.com]phpBB[/url] code..

 210      $patterns[] = "#\[url=([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is";
 211      $replacements[] = $bbcode_tpl['url3'];
 212  
 213      // [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).

 214      $patterns[] = "#\[url=((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is";
 215      $replacements[] = $bbcode_tpl['url4'];
 216  
 217      // [email]user@domain.tld[/email] code..

 218      $patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
 219      $replacements[] = $bbcode_tpl['email'];
 220  
 221      $text = preg_replace($patterns, $replacements, $text);
 222  
 223      // Remove our padding from the string..

 224      $text = substr($text, 1);
 225  
 226      return $text;
 227  
 228  } // bbencode_second_pass()

 229  
 230  // Need to initialize the random numbers only ONCE

 231  mt_srand( (double) microtime() * 1000000);
 232  
 233  function make_bbcode_uid()
 234  {
 235      // Unique ID for this message..

 236  
 237      $uid = md5(mt_rand());
 238      $uid = substr($uid, 0, BBCODE_UID_LEN);
 239  
 240      return $uid;
 241  }
 242  
 243  function bbencode_first_pass($text, $uid)
 244  {
 245      // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).

 246      // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.

 247      $text = " " . $text;
 248  
 249      // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.

 250      $text = bbencode_first_pass_pda($text, $uid, '[code]', '[/code]', '', true, '');
 251  
 252      // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.

 253      $text = bbencode_first_pass_pda($text, $uid, '[quote]', '[/quote]', '', false, '');
 254      $text = bbencode_first_pass_pda($text, $uid, '/\[quote=(\\\".*?\\\")\]/is', '[/quote]', '', false, '', "[quote:$uid=\\1]");
 255  
 256      // [list] and [list=x] for (un)ordered lists.

 257      $open_tag = array();
 258      $open_tag[0] = "[list]";
 259  
 260      // unordered..

 261      $text = bbencode_first_pass_pda($text, $uid, $open_tag, "[/list]", "[/list:u]", false, 'replace_listitems');
 262  
 263      $open_tag[0] = "[list=1]";
 264      $open_tag[1] = "[list=a]";
 265  
 266      // ordered.

 267      $text = bbencode_first_pass_pda($text, $uid, $open_tag, "[/list]", "[/list:o]",  false, 'replace_listitems');
 268  
 269      // [color] and [/color] for setting text color

 270      $text = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "[color=\\1:$uid]\\2[/color:$uid]", $text);
 271  
 272      // [size] and [/size] for setting text size

 273      $text = preg_replace("#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si", "[size=\\1:$uid]\\2[/size:$uid]", $text);
 274  
 275      // [b] and [/b] for bolding text.

 276      $text = preg_replace("#\[b\](.*?)\[/b\]#si", "[b:$uid]\\1[/b:$uid]", $text);
 277  
 278      // [u] and [/u] for underlining text.

 279      $text = preg_replace("#\[u\](.*?)\[/u\]#si", "[u:$uid]\\1[/u:$uid]", $text);
 280  
 281      // [i] and [/i] for italicizing text.

 282      $text = preg_replace("#\[i\](.*?)\[/i\]#si", "[i:$uid]\\1[/i:$uid]", $text);
 283  
 284      // [img]image_url_here[/img] code..

 285      $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);
 286  
 287      // Remove our padding from the string..

 288      return substr($text, 1);;
 289  
 290  } // bbencode_first_pass()

 291  
 292  /**

 293   * $text - The text to operate on.

 294   * $uid - The UID to add to matching tags.

 295   * $open_tag - The opening tag to match. Can be an array of opening tags.

 296   * $close_tag - The closing tag to match.

 297   * $close_tag_new - The closing tag to replace with.

 298   * $mark_lowest_level - boolean - should we specially mark the tags that occur

 299   *                     at the lowest level of nesting? (useful for [code], because

 300   *                        we need to match these tags first and transform HTML tags

 301   *                        in their contents..

 302   * $func - This variable should contain a string that is the name of a function.

 303   *                That function will be called when a match is found, and passed 2

 304   *                parameters: ($text, $uid). The function should return a string.

 305   *                This is used when some transformation needs to be applied to the

 306   *                text INSIDE a pair of matching tags. If this variable is FALSE or the

 307   *                empty string, it will not be executed.

 308   * If open_tag is an array, then the pda will try to match pairs consisting of

 309   * any element of open_tag followed by close_tag. This allows us to match things

 310   * like [list=A]...[/list] and [list=1]...[/list] in one pass of the PDA.

 311   *

 312   * NOTES:    - this function assumes the first character of $text is a space.

 313   *                - every opening tag and closing tag must be of the [...] format.

 314   */
 315  function bbencode_first_pass_pda($text, $uid, $open_tag, $close_tag, $close_tag_new, $mark_lowest_level, $func, $open_regexp_replace = false)
 316  {
 317      $open_tag_count = 0;
 318  
 319      if (!$close_tag_new || (empty($close_tag_new)))
 320      {
 321          $close_tag_new = $close_tag;
 322      }
 323  
 324      $close_tag_length = strlen($close_tag);
 325      $close_tag_new_length = strlen($close_tag_new);
 326      $uid_length = strlen($uid);
 327  
 328      $use_function_pointer = ($func && (!empty($func)));
 329  
 330      $stack = array();
 331  
 332      if (is_array($open_tag))
 333      {
 334          if (0 == count($open_tag))
 335          {
 336              // No opening tags to match, so return.

 337              return $text;
 338          }
 339          $open_tag_count = count($open_tag);
 340      }
 341      else
 342      {
 343          // only one opening tag. make it into a 1-element array.

 344          $open_tag_temp = $open_tag;
 345          $open_tag = array();
 346          $open_tag[0] = $open_tag_temp;
 347          $open_tag_count = 1;
 348      }
 349  
 350      $open_is_regexp = false;
 351  
 352      if ($open_regexp_replace)
 353      {
 354          $open_is_regexp = true;
 355          if (!is_array($open_regexp_replace))
 356          {
 357              $open_regexp_temp = $open_regexp_replace;
 358              $open_regexp_replace = array();
 359              $open_regexp_replace[0] = $open_regexp_temp;
 360          }
 361      }
 362  
 363      if ($mark_lowest_level && $open_is_regexp)
 364      {
 365          message_die(GENERAL_ERROR, "Unsupported operation for bbcode_first_pass_pda().");
 366      }
 367  
 368      // Start at the 2nd char of the string, looking for opening tags.

 369      $curr_pos = 1;
 370      while ($curr_pos && ($curr_pos < strlen($text)))
 371      {
 372          $curr_pos = strpos($text, "[", $curr_pos);
 373  
 374          // If not found, $curr_pos will be 0, and the loop will end.

 375          if ($curr_pos)
 376          {
 377              // We found a [. It starts at $curr_pos.

 378              // check if it's a starting or ending tag.

 379              $found_start = false;
 380              $which_start_tag = "";
 381              $start_tag_index = -1;
 382  
 383              for ($i = 0; $i < $open_tag_count; $i++)
 384              {
 385                  // Grab everything until the first "]"...

 386                  $possible_start = substr($text, $curr_pos, strpos($text, ']', $curr_pos + 1) - $curr_pos + 1);
 387  
 388                  //

 389                  // We're going to try and catch usernames with "[' characters.

 390                  //

 391                  if( preg_match('#\[quote=\\\"#si', $possible_start, $match) && !preg_match('#\[quote=\\\"(.*?)\\\"\]#si', $possible_start) )
 392                  {
 393                      // OK we are in a quote tag that probably contains a ] bracket.

 394                      // Grab a bit more of the string to hopefully get all of it..

 395                      if ($close_pos = strpos($text, '"]', $curr_pos + 9))
 396                      {
 397                          if (strpos(substr($text, $curr_pos + 9, $close_pos - ($curr_pos + 9)), '[quote') === false)
 398                          {
 399                              $possible_start = substr($text, $curr_pos, $close_pos - $curr_pos + 2);
 400                          }
 401                      }
 402                  }
 403  
 404                  // Now compare, either using regexp or not.

 405                  if ($open_is_regexp)
 406                  {
 407                      $match_result = array();
 408                      if (preg_match($open_tag[$i], $possible_start, $match_result))
 409                      {
 410                          $found_start = true;
 411                          $which_start_tag = $match_result[0];
 412                          $start_tag_index = $i;
 413                          break;
 414                      }
 415                  }
 416                  else
 417                  {
 418                      // straightforward string comparison.

 419                      if (0 == strcasecmp($open_tag[$i], $possible_start))
 420                      {
 421                          $found_start = true;
 422                          $which_start_tag = $open_tag[$i];
 423                          $start_tag_index = $i;
 424                          break;
 425                      }
 426                  }
 427              }
 428  
 429              if ($found_start)
 430              {
 431                  // We have an opening tag.

 432                  // 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.

 433                  $match = array("pos" => $curr_pos, "tag" => $which_start_tag, "index" => $start_tag_index);
 434                  bbcode_array_push($stack, $match);
 435                  //

 436                  // Rather than just increment $curr_pos

 437                  // Set it to the ending of the tag we just found

 438                  // Keeps error in nested tag from breaking out

 439                  // of table structure..

 440                  //

 441                  $curr_pos += strlen($possible_start);
 442              }
 443              else
 444              {
 445                  // check for a closing tag..

 446                  $possible_end = substr($text, $curr_pos, $close_tag_length);
 447                  if (0 == strcasecmp($close_tag, $possible_end))
 448                  {
 449                      // We have an ending tag.

 450                      // Check if we've already found a matching starting tag.

 451                      if (sizeof($stack) > 0)
 452                      {
 453                          // There exists a starting tag.

 454                          $curr_nesting_depth = sizeof($stack);
 455                          // We need to do 2 replacements now.

 456                          $match = bbcode_array_pop($stack);
 457                          $start_index = $match['pos'];
 458                          $start_tag = $match['tag'];
 459                          $start_length = strlen($start_tag);
 460                          $start_tag_index = $match['index'];
 461  
 462                          if ($open_is_regexp)
 463                          {
 464                              $start_tag = preg_replace($open_tag[$start_tag_index], $open_regexp_replace[$start_tag_index], $start_tag);
 465                          }
 466  
 467                          // everything before the opening tag.

 468                          $before_start_tag = substr($text, 0, $start_index);
 469  
 470                          // everything after the opening tag, but before the closing tag.

 471                          $between_tags = substr($text, $start_index + $start_length, $curr_pos - $start_index - $start_length);
 472  
 473                          // Run the given function on the text between the tags..

 474                          if ($use_function_pointer)
 475                          {
 476                              $between_tags = $func($between_tags, $uid);
 477                          }
 478  
 479                          // everything after the closing tag.

 480                          $after_end_tag = substr($text, $curr_pos + $close_tag_length);
 481  
 482                          // Mark the lowest nesting level if needed.

 483                          if ($mark_lowest_level && ($curr_nesting_depth == 1))
 484                          {
 485                              if ($open_tag[0] == '[code]')
 486                              {
 487                                  $code_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#');

 488                                  $code_entities_replace = array('&lt;', '&gt;', '&quot;', '&#58;', '&#91;', '&#93;', '&#40;', '&#41;', '&#123;', '&#125;');
 489                                  $between_tags = preg_replace($code_entities_match, $code_entities_replace, $between_tags);
 490                              }
 491                              $text = $before_start_tag . substr($start_tag, 0, $start_length - 1) . ":$curr_nesting_depth:$uid]";
 492                              $text .= $between_tags . substr($close_tag_new, 0, $close_tag_new_length - 1) . ":$curr_nesting_depth:$uid]";
 493                          }
 494                          else
 495                          {
 496                              if ($open_tag[0] == '[code]')
 497                              {
 498                                  $text = $before_start_tag . '&#91;code&#93;';
 499                                  $text .= $between_tags . '&#91;/code&#93;';
 500                              }
 501                              else
 502                              {
 503                                  if ($open_is_regexp)
 504                                  {
 505                                      $text = $before_start_tag . $start_tag;
 506                                  }
 507                                  else
 508                                  {
 509                                      $text = $before_start_tag . substr($start_tag, 0, $start_length - 1) . ":$uid]";
 510                                  }
 511                                  $text .= $between_tags . substr($close_tag_new, 0, $close_tag_new_length - 1) . ":$uid]";
 512                              }
 513                          }
 514  
 515                          $text .= $after_end_tag;
 516  
 517                          // Now.. we've screwed up the indices by changing the length of the string.

 518                          // So, if there's anything in the stack, we want to resume searching just after it.

 519                          // otherwise, we go back to the start.

 520                          if (sizeof($stack) > 0)
 521                          {
 522                              $match = bbcode_array_pop($stack);
 523                              $curr_pos = $match['pos'];
 524  //                            bbcode_array_push($stack, $match);

 525  //                            ++$curr_pos;

 526                          }
 527                          else
 528                          {
 529                              $curr_pos = 1;
 530                          }
 531                      }
 532                      else
 533                      {
 534                          // No matching start tag found. Increment pos, keep going.

 535                          ++$curr_pos;
 536                      }
 537                  }
 538                  else
 539                  {
 540                      // No starting tag or ending tag.. Increment pos, keep looping.,

 541                      ++$curr_pos;
 542                  }
 543              }
 544          }
 545      } // while

 546  
 547      return $text;
 548  
 549  } // bbencode_first_pass_pda()

 550  
 551  /**

 552   * Does second-pass bbencoding of the [code] tags. This includes

 553   * running htmlspecialchars() over the text contained between

 554   * any pair of [code] tags that are at the first level of

 555   * nesting. Tags at the first level of nesting are indicated

 556   * by this format: [code:1:$uid] ... [/code:1:$uid]

 557   * Other tags are in this format: [code:$uid] ... [/code:$uid]

 558   */
 559  function bbencode_second_pass_code($text, $uid, $bbcode_tpl)
 560  {
 561      global $lang;
 562  
 563      $code_start_html = $bbcode_tpl['code_open'];
 564      $code_end_html =  $bbcode_tpl['code_close'];
 565  
 566      // First, do all the 1st-level matches. These need an htmlspecialchars() run,

 567      // so they have to be handled differently.

 568      $match_count = preg_match_all("#\[code:1:$uid\](.*?)\[/code:1:$uid\]#si", $text, $matches);
 569  
 570      for ($i = 0; $i < $match_count; $i++)
 571      {
 572          $before_replace = $matches[1][$i];
 573          $after_replace = $matches[1][$i];
 574  
 575          // Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.

 576          $after_replace = str_replace("  ", "&nbsp; ", $after_replace);
 577          // now Replace 2 spaces with " &nbsp;" to catch odd #s of spaces.

 578          $after_replace = str_replace("  ", " &nbsp;", $after_replace);
 579  
 580          // Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.

 581          $after_replace = str_replace("\t", "&nbsp; &nbsp;", $after_replace);
 582  
 583          // now Replace space occurring at the beginning of a line

 584          $after_replace = preg_replace("/^ {1}/m", '&nbsp;', $after_replace);
 585  
 586          $str_to_match = "[code:1:$uid]" . $before_replace . "[/code:1:$uid]";
 587  
 588          $replacement = $code_start_html;
 589          $replacement .= $after_replace;
 590          $replacement .= $code_end_html;
 591  
 592          $text = str_replace($str_to_match, $replacement, $text);
 593      }
 594  
 595      // Now, do all the non-first-level matches. These are simple.

 596      $text = str_replace("[code:$uid]", $code_start_html, $text);
 597      $text = str_replace("[/code:$uid]", $code_end_html, $text);
 598  
 599      return $text;
 600  
 601  } // bbencode_second_pass_code()

 602  
 603  /**

 604   * Rewritten by Nathan Codding - Feb 6, 2001.

 605   * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking

 606   *     to that URL

 607   * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking

 608   *     to http://www.xxxx.yyyy[/zzzz]

 609   * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking

 610   *        to that email address

 611   * - Only matches these 2 patterns either after a space, or at the beginning of a line

 612   *

 613   * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe

 614   * have it require something like xxxx@yyyy.zzzz or such. We'll see.

 615   */
 616  function make_clickable($text)
 617  {
 618      $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1&#058;", $text);
 619  
 620      // pad it with a space so we can match things at the start of the 1st line.

 621      $ret = ' ' . $text;
 622  
 623      // matches an "xxxx://yyyy" URL at the start of a line, or after a space.

 624      // xxxx can only be alpha characters.

 625      // yyyy is anything up to the first space, newline, comma, double quote or <

 626      $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
 627  
 628      // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing

 629      // Must contain at least 2 dots. xxxx contains either alphanum, or "-"

 630      // zzzz is optional.. will contain everything up to the first space, newline, 

 631      // comma, double quote or <.

 632      $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
 633  
 634      // matches an email@domain type address at the start of a line, or after a space.

 635      // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".

 636      $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
 637  
 638      // Remove our padding..

 639      $ret = substr($ret, 1);
 640  
 641      return($ret);
 642  }
 643  
 644  /**

 645   * Nathan Codding - Feb 6, 2001

 646   * Reverses the effects of make_clickable(), for use in editpost.

 647   * - Does not distinguish between "www.xxxx.yyyy" and "http://aaaa.bbbb" type URLs.

 648   *

 649   */
 650  function undo_make_clickable($text)
 651  {
 652      $text = preg_replace("#<!-- BBCode auto-link start --><a href=\"(.*?)\" target=\"_blank\">.*?</a><!-- BBCode auto-link end -->#i", "\\1", $text);
 653      $text = preg_replace("#<!-- BBcode auto-mailto start --><a href=\"mailto:(.*?)\">.*?</a><!-- BBCode auto-mailto end -->#i", "\\1", $text);
 654  
 655      return $text;
 656  
 657  }
 658  
 659  /**

 660   * Nathan Codding - August 24, 2000.

 661   * Takes a string, and does the reverse of the PHP standard function

 662   * htmlspecialchars().

 663   */
 664  function undo_htmlspecialchars($input)
 665  {
 666      $input = preg_replace("/&gt;/i", ">", $input);
 667      $input = preg_replace("/&lt;/i", "<", $input);
 668      $input = preg_replace("/&quot;/i", "\"", $input);
 669      $input = preg_replace("/&amp;/i", "&", $input);
 670  
 671      return $input;
 672  }
 673  
 674  /**

 675   * This is used to change a [*] tag into a [*:$uid] tag as part

 676   * of the first-pass bbencoding of [list] tags. It fits the

 677   * standard required in order to be passed as a variable

 678   * function into bbencode_first_pass_pda().

 679   */
 680  function replace_listitems($text, $uid)
 681  {
 682      $text = str_replace("[*]", "[*:$uid]", $text);
 683  
 684      return $text;
 685  }
 686  
 687  /**

 688   * Escapes the "/" character with "\/". This is useful when you need

 689   * to stick a runtime string into a PREG regexp that is being delimited

 690   * with slashes.

 691   */
 692  function escape_slashes($input)
 693  {
 694      $output = str_replace('/', '\/', $input);
 695      return $output;
 696  }
 697  
 698  /**

 699   * This function does exactly what the PHP4 function array_push() does

 700   * however, to keep phpBB compatable with PHP 3 we had to come up with our own

 701   * method of doing it.

 702   */
 703  function bbcode_array_push(&$stack, $value)
 704  {
 705     $stack[] = $value;
 706     return(sizeof($stack));
 707  }
 708  
 709  /**

 710   * This function does exactly what the PHP4 function array_pop() does

 711   * however, to keep phpBB compatable with PHP 3 we had to come up with our own

 712   * method of doing it.

 713   */
 714  function bbcode_array_pop(&$stack)
 715  {
 716     $arrSize = count($stack);
 717     $x = 1;
 718  
 719     while(list($key, $val) = each($stack))
 720     {
 721        if($x < count($stack))
 722        {
 723               $tmpArr[] = $val;
 724        }
 725        else
 726        {
 727               $return_val = $val;
 728        }
 729        $x++;
 730     }
 731     $stack = $tmpArr;
 732  
 733     return($return_val);
 734  }
 735  
 736  //

 737  // Smilies code ... would this be better tagged on to the end of bbcode.php?

 738  // Probably so and I'll move it before B2

 739  //

 740  function smilies_pass($message)
 741  {
 742      static $orig, $repl;
 743  
 744      if (!isset($orig))
 745      {
 746          global $db, $board_config;
 747          $orig = $repl = array();
 748  
 749          $sql = 'SELECT * FROM ' . SMILIES_TABLE;
 750          if( !$result = $db->sql_query($sql) )
 751          {
 752              message_die(GENERAL_ERROR, "Couldn't obtain smilies data", "", __LINE__, __FILE__, $sql);
 753          }
 754          $smilies = $db->sql_fetchrowset($result);
 755  
 756          if (count($smilies))
 757          {
 758              usort($smilies, 'smiley_sort');
 759          }
 760  
 761          for ($i = 0; $i < count($smilies); $i++)
 762          {
 763              $orig[] = "/(?<=.\W|\W.|^\W)" . phpbb_preg_quote($smilies[$i]['code'], "/") . "(?=.\W|\W.|\W$)/";
 764              $repl[] = '<img src="'. $board_config['smilies_path'] . '/' . $smilies[$i]['smile_url'] . '" alt="' . $smilies[$i]['emoticon'] . '" border="0" />';
 765          }
 766      }
 767  
 768      if (count($orig))
 769      {
 770          $message = preg_replace($orig, $repl, ' ' . $message . ' ');
 771          $message = substr($message, 1, -1);
 772      }
 773      
 774      return $message;
 775  }
 776  
 777  function smiley_sort($a, $b)
 778  {
 779      if ( strlen($a['code']) == strlen($b['code']) )
 780      {
 781          return 0;
 782      }
 783  
 784      return ( strlen($a['code']) > strlen($b['code']) ) ? -1 : 1;
 785  }
 786  
 787  ?>


Généré le : Sun Apr 1 11:11:59 2007 par Balluche grâce à PHPXref 0.7