decir/bbcode.php
author Dan
Tue, 13 Nov 2007 22:28:30 -0500
changeset 7 37387f84fe25
parent 1 6f8b7c6fac02
permissions -rw-r--r--
Add edit functionality to forum management and implemented a sick drag-and-drop reordering system for forums
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     1
<?php
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     2
/*
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     3
 * Decir
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     4
 * Version 0.1
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     5
 * Copyright (C) 2007 Dan Fuhry
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     6
 * bbcode.php - BBcode-to-HTML renderer
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     7
 *
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    10
 *
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    13
 */
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    14
 
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    15
function render_bbcode($text, $bbcode_uid = false)
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    16
{
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    17
  // First things first, strip out all [code] sections
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    18
  $text = decir_bbcode_strip_code($text, $bbcode_uid, $_code);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    19
  
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    20
  if ( $bbcode_uid )
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    21
    $bbcode_uid = ':' . $bbcode_uid;
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    22
  
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    23
  // Bold text
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    24
  $text = preg_replace("/\[b$bbcode_uid\](.*?)\[\/b$bbcode_uid\]/is", '<b>\\1</b>', $text);
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    25
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    26
  // Italicized text
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    27
  $text = preg_replace("/\[i$bbcode_uid\](.*?)\[\/i$bbcode_uid\]/is", '<i>\\1</i>', $text);
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    28
  
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    29
  // Underlined text
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    30
  $text = preg_replace("/\[u$bbcode_uid\](.*?)\[\/u$bbcode_uid\]/is", '<u>\\1</u>', $text);                        
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    31
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    32
  // Colored text
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    33
  $text = preg_replace("/\[color$bbcode_uid=#([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9])?)\](.*?)\[\/color$bbcode_uid\]/is", '<span style="color: #\\1">\\3</span>', $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    34
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    35
  // Size
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    36
  $text = preg_replace("/\[size$bbcode_uid=([0-4]+(\.[0-9]+)?)\](.*?)\[\/size$bbcode_uid\]/is", '<span style="font-size: \\1em;">\\3</span>', $text);
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    37
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    38
  // Quotes
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    39
  $text = preg_replace("/\[quote$bbcode_uid=\"?([^]]+?)\"?\](.*?)\[\/quote$bbcode_uid\]/is", '<span class="decir-quoteheader">\\1 wrote:</span><blockquote>\\2</blockquote>', $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    40
  $text = preg_replace("/\[quote$bbcode_uid\](.*?)\[\/quote$bbcode_uid\]/is", '<blockquote class="decir-quotebody">\\1</blockquote>', $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    41
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    42
  // https?:\/\/((([a-z0-9-]+\.)*)[a-z0-9-]+)(\/[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*(\?(([a-z0-9_-]+)(=[A-z0-9_%\|~`\!@#\$\^&\*\(\):;\.,\/-\[\]]+)?((&([a-z0-9_-]+)(=[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]+)?)*))?)?)?
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    43
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    44
  // Links
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    45
  // Trial and error.
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    46
  $regexp = "/\[url$bbcode_uid(=(https?:\/\/((([a-z0-9-]+\.)*)[a-z0-9-]+)(\/[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*(\?(([a-z0-9_-]+)(=[A-z0-9_%\|~`\!@#\$\^&\*\(\):;\.,\/-\[\]]+)?((&([a-z0-9_-]+)(=[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]+)?)*))?)?)?))?\](.*?)\[\/url$bbcode_uid\]/is";
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    47
  $text = preg_replace($regexp, '<a href="\\2">\\15</a>', $text);
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    48
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    49
  // Newlines
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    50
  $text = str_replace("\n", "<br />\n", $text);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    51
  
7
37387f84fe25 Add edit functionality to forum management and implemented a sick drag-and-drop reordering system for forums
Dan
parents: 1
diff changeset
    52
  // Wikilinks
37387f84fe25 Add edit functionality to forum management and implemented a sick drag-and-drop reordering system for forums
Dan
parents: 1
diff changeset
    53
  $text = RenderMan::parse_internal_links($text);
37387f84fe25 Add edit functionality to forum management and implemented a sick drag-and-drop reordering system for forums
Dan
parents: 1
diff changeset
    54
  
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    55
  // Restore [code] blocks
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    56
  $text = decir_bbcode_restore_code($text, $bbcode_uid, $_code);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    57
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    58
  // Code
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    59
  $text = preg_replace("/\[code$bbcode_uid\](.*?)\[\/code$bbcode_uid\]/is", '<pre>\\1</pre>', $text);
0
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    60
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    61
  return $text;
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    62
}
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    63
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    64
function decir_bbcode_strip_code($text, $uid, &$code_secs)
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    65
{
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    66
  preg_match_all("/\[code:$uid\](.*?)\[\/code:$uid\]/is", $text, $matches);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    67
  foreach ( $matches[1] as $i => $m )
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    68
  {
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    69
    $text = str_replace_once($m, "{CODE_SECTION|$i:$uid}", $text);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    70
    $code_secs[$i] = $m;
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    71
  }
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    72
  return $text;
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    73
}
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    74
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    75
function decir_bbcode_restore_code($text, $uid, $code_secs)
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    76
{
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    77
  foreach ( $code_secs as $i => $code )
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    78
  {
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    79
    $text = str_replace("{CODE_SECTION|$i:$uid}", $code, $text);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    80
  }
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    81
  return $text;
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    82
}
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    83
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    84
function bbcode_strip_uid($bbcode, $uid)
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    85
{
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    86
  // BBcode tags with attributes
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    87
  $bbcode = preg_replace("/\[([a-z]+?):{$uid}=([^\]]+?)\](.*?)\[\/\\1:{$uid}\]/is", '[\\1=\\2]\\3[/\\1]', $bbcode);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    88
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    89
  // BBcode tags without attributes
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    90
  $bbcode = preg_replace("/\[([a-z]+?):{$uid}\](.*?)\[\/\\1:{$uid}\]/is", '[\\1]\\2[/\\1]', $bbcode);
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    91
  
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    92
  return $bbcode;
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    93
}
0417a5a0c7be Initial repository population
dan@fuhry
parents:
diff changeset
    94
1
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    95
function bbcode_inject_uid($text, &$uid)
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    96
{
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    97
  $seed = md5( implode('.', explode(' ', microtime)) );
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    98
  $uid = substr($seed, 0, 10);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
    99
  // Bold text
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   100
  $text = preg_replace("/\[b\](.*?)\[\/b\]/is", "[b:$uid]\\1[/b:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   101
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   102
  // Italicized text
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   103
  $text = preg_replace("/\[i\](.*?)\[\/i\]/is", "[i:$uid]\\1[/i:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   104
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   105
  // Uunderlined text
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   106
  $text = preg_replace("/\[u\](.*?)\[\/u\]/is", "[u:$uid]\\1[/u:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   107
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   108
  // Colored text
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   109
  $text = preg_replace("/\[color=\#([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9])?)\](.*?)\[\/color\]/is", "[color:$uid=#\\1]\\3[/color:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   110
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   111
  // Size
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   112
  $text = preg_replace('/\[size=([0-4]+(\.[0-9]+)?)\](.*?)\[\/size\]/is', "[size:$uid=\\1]\\3[/size:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   113
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   114
  // Quotes
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   115
  $text = preg_replace("/\[quote\](.*?)\[\/quote\]/is", "[quote:$uid]\\1[/quote:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   116
  $text = preg_replace("/\[quote=\"?([^]]+)\"?\](.*?)\[\/quote\]/is", "[quote:$uid=\\1]\\2[/quote:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   117
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   118
  // Code
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   119
  $text = preg_replace("/\[code\](.*?)\[\/code\]/is", "[code:$uid]\\1[/code:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   120
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   121
  // URLs
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   122
  $text = preg_replace('/\[url(=https?:\/\/([^ ]+))?\](.*?)\[\/url\]/is', "[url:$uid\\1]\\3[/url:$uid]", $text);
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   123
  
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   124
  return $text;
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   125
}
6f8b7c6fac02 Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents: 0
diff changeset
   126