root / trunk / phpBB / includes / bbcode.php
History | View | Annotate | Download (16.1 kB)
| 1 | 182 | psotfx | <?php
|
|---|---|---|---|
| 2 | 8146 | acydburn | /**
|
| 3 | 5114 | acydburn | * |
| 4 | 5114 | acydburn | * @package phpBB3 |
| 5 | 8146 | acydburn | * @copyright (c) 2005 phpBB Group |
| 6 | 11653 | git-gate | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
| 7 | 5114 | acydburn | * |
| 8 | 5114 | acydburn | */ |
| 9 | 182 | psotfx | |
| 10 | 5114 | acydburn | /**
|
| 11 | 8146 | acydburn | * @ignore |
| 12 | 8146 | acydburn | */ |
| 13 | 8146 | acydburn | if (!defined('IN_PHPBB')) |
| 14 | 8146 | acydburn | {
|
| 15 | 8146 | acydburn | exit;
|
| 16 | 8146 | acydburn | } |
| 17 | 8146 | acydburn | |
| 18 | 8146 | acydburn | /**
|
| 19 | 6058 | acydburn | * BBCode class |
| 20 | 5114 | acydburn | * @package phpBB3 |
| 21 | 5114 | acydburn | */ |
| 22 | 3812 | ludovic_arnaud | class bbcode |
| 23 | 2304 | psotfx | {
|
| 24 | 3812 | ludovic_arnaud | var $bbcode_uid = ''; |
| 25 | 6209 | davidmj | var $bbcode_bitfield = ''; |
| 26 | 3812 | ludovic_arnaud | var $bbcode_cache = array(); |
| 27 | 4170 | psotfx | var $bbcode_template = array(); |
| 28 | 2304 | psotfx | |
| 29 | 4978 | acydburn | var $bbcodes = array(); |
| 30 | 4978 | acydburn | |
| 31 | 6777 | davidmj | var $template_bitfield; |
| 32 | 4363 | ludovic_arnaud | |
| 33 | 6048 | acydburn | /**
|
| 34 | 6048 | acydburn | * Constructor |
| 35 | 6048 | acydburn | * Init bbcode cache entries if bitfield is specified |
| 36 | 6048 | acydburn | */ |
| 37 | 6263 | davidmj | function bbcode($bitfield = '') |
| 38 | 182 | psotfx | {
|
| 39 | 3863 | ludovic_arnaud | if ($bitfield) |
| 40 | 3863 | ludovic_arnaud | {
|
| 41 | 3863 | ludovic_arnaud | $this->bbcode_bitfield = $bitfield; |
| 42 | 3863 | ludovic_arnaud | $this->bbcode_cache_init();
|
| 43 | 3863 | ludovic_arnaud | } |
| 44 | 182 | psotfx | } |
| 45 | 2983 | psotfx | |
| 46 | 6048 | acydburn | /**
|
| 47 | 6048 | acydburn | * Second pass bbcodes |
| 48 | 6048 | acydburn | */ |
| 49 | 4978 | acydburn | function bbcode_second_pass(&$message, $bbcode_uid = '', $bbcode_bitfield = false) |
| 50 | 1095 | natec | {
|
| 51 | 3812 | ludovic_arnaud | if ($bbcode_uid) |
| 52 | 1243 | natec | {
|
| 53 | 3812 | ludovic_arnaud | $this->bbcode_uid = $bbcode_uid; |
| 54 | 1243 | natec | } |
| 55 | 3934 | ludovic_arnaud | |
| 56 | 4978 | acydburn | if ($bbcode_bitfield !== false) |
| 57 | 3812 | ludovic_arnaud | {
|
| 58 | 3812 | ludovic_arnaud | $this->bbcode_bitfield = $bbcode_bitfield; |
| 59 | 5035 | acydburn | |
| 60 | 4903 | acydburn | // Init those added with a new bbcode_bitfield (already stored codes will not get parsed again)
|
| 61 | 4903 | acydburn | $this->bbcode_cache_init();
|
| 62 | 3812 | ludovic_arnaud | } |
| 63 | 4903 | acydburn | |
| 64 | 4190 | ludovic_arnaud | if (!$this->bbcode_bitfield) |
| 65 | 3934 | ludovic_arnaud | {
|
| 66 | 5967 | acydburn | // Remove the uid from tags that have not been transformed into HTML
|
| 67 | 5967 | acydburn | if ($this->bbcode_uid) |
| 68 | 5967 | acydburn | {
|
| 69 | 5967 | acydburn | $message = str_replace(':' . $this->bbcode_uid, '', $message); |
| 70 | 5967 | acydburn | } |
| 71 | 5967 | acydburn | |
| 72 | 5967 | acydburn | return;
|
| 73 | 3934 | ludovic_arnaud | } |
| 74 | 2983 | psotfx | |
| 75 | 3812 | ludovic_arnaud | $str = array('search' => array(), 'replace' => array()); |
| 76 | 3812 | ludovic_arnaud | $preg = array('search' => array(), 'replace' => array()); |
| 77 | 182 | psotfx | |
| 78 | 6209 | davidmj | $bitfield = new bitfield($this->bbcode_bitfield); |
| 79 | 6209 | davidmj | $bbcodes_set = $bitfield->get_all_set(); |
| 80 | 6209 | davidmj | |
| 81 | 8153 | naderman | $undid_bbcode_specialchars = false; |
| 82 | 6209 | davidmj | foreach ($bbcodes_set as $bbcode_id) |
| 83 | 182 | psotfx | {
|
| 84 | 6209 | davidmj | if (!empty($this->bbcode_cache[$bbcode_id])) |
| 85 | 182 | psotfx | {
|
| 86 | 6209 | davidmj | foreach ($this->bbcode_cache[$bbcode_id] as $type => $array) |
| 87 | 1818 | the_systech | {
|
| 88 | 6209 | davidmj | foreach ($array as $search => $replace) |
| 89 | 1243 | natec | {
|
| 90 | 6209 | davidmj | ${$type}['search'][] = str_replace('$uid', $this->bbcode_uid, $search);
|
| 91 | 6209 | davidmj | ${$type}['replace'][] = $replace;
|
| 92 | 6209 | davidmj | } |
| 93 | 5023 | acydburn | |
| 94 | 6209 | davidmj | if (sizeof($str['search'])) |
| 95 | 6209 | davidmj | {
|
| 96 | 6209 | davidmj | $message = str_replace($str['search'], $str['replace'], $message); |
| 97 | 6209 | davidmj | $str = array('search' => array(), 'replace' => array()); |
| 98 | 6209 | davidmj | } |
| 99 | 5023 | acydburn | |
| 100 | 6209 | davidmj | if (sizeof($preg['search'])) |
| 101 | 6209 | davidmj | {
|
| 102 | 8153 | naderman | // we need to turn the entities back into their original form to allow the
|
| 103 | 8153 | naderman | // search patterns to work properly
|
| 104 | 8153 | naderman | if (!$undid_bbcode_specialchars) |
| 105 | 8153 | naderman | {
|
| 106 | 8153 | naderman | $message = str_replace(array(':', '.'), array(':', '.'), $message); |
| 107 | 8153 | naderman | $undid_bbcode_specialchars = true; |
| 108 | 8153 | naderman | } |
| 109 | 8153 | naderman | |
| 110 | 6209 | davidmj | $message = preg_replace($preg['search'], $preg['replace'], $message); |
| 111 | 6209 | davidmj | $preg = array('search' => array(), 'replace' => array()); |
| 112 | 1243 | natec | } |
| 113 | 182 | psotfx | } |
| 114 | 182 | psotfx | } |
| 115 | 3812 | ludovic_arnaud | } |
| 116 | 792 | thefinn | |
| 117 | 4453 | ludovic_arnaud | // Remove the uid from tags that have not been transformed into HTML
|
| 118 | 4453 | ludovic_arnaud | $message = str_replace(':' . $this->bbcode_uid, '', $message); |
| 119 | 3812 | ludovic_arnaud | } |
| 120 | 6048 | acydburn | |
| 121 | 6048 | acydburn | /**
|
| 122 | 6048 | acydburn | * Init bbcode cache |
| 123 | 6048 | acydburn | * |
| 124 | 6048 | acydburn | * requires: $this->bbcode_bitfield |
| 125 | 6048 | acydburn | * sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield |
| 126 | 6048 | acydburn | */ |
| 127 | 3812 | ludovic_arnaud | function bbcode_cache_init() |
| 128 | 3812 | ludovic_arnaud | {
|
| 129 | 11557 | git-gate | global $phpbb_root_path, $phpEx, $config, $user, $phpbb_extension_manager; |
| 130 | 4453 | ludovic_arnaud | |
| 131 | 4363 | ludovic_arnaud | if (empty($this->template_filename)) |
| 132 | 4363 | ludovic_arnaud | {
|
| 133 | 6777 | davidmj | $this->template_bitfield = new bitfield($user->theme['bbcode_bitfield']); |
| 134 | 9461 | acydburn | |
| 135 | 11358 | git-gate | $template_locator = new phpbb_template_locator(); |
| 136 | 11557 | git-gate | $template_path_provider = new phpbb_template_extension_path_provider($phpbb_extension_manager, new phpbb_template_path_provider()); |
| 137 | 11557 | git-gate | $template = new phpbb_template($phpbb_root_path, $phpEx, $config, $user, $template_locator, $template_path_provider); |
| 138 | 11358 | git-gate | $template->set_template();
|
| 139 | 11358 | git-gate | $template_locator->set_filenames(array('bbcode.html' => 'bbcode.html')); |
| 140 | 11358 | git-gate | $this->template_filename = $template_locator->get_source_file_for_handle('bbcode.html'); |
| 141 | 4363 | ludovic_arnaud | } |
| 142 | 4363 | ludovic_arnaud | |
| 143 | 6271 | acydburn | $bbcode_ids = $rowset = $sql = array(); |
| 144 | 3863 | ludovic_arnaud | |
| 145 | 6209 | davidmj | $bitfield = new bitfield($this->bbcode_bitfield); |
| 146 | 6209 | davidmj | $bbcodes_set = $bitfield->get_all_set(); |
| 147 | 6209 | davidmj | |
| 148 | 6209 | davidmj | foreach ($bbcodes_set as $bbcode_id) |
| 149 | 3812 | ludovic_arnaud | {
|
| 150 | 6209 | davidmj | if (isset($this->bbcode_cache[$bbcode_id])) |
| 151 | 3863 | ludovic_arnaud | {
|
| 152 | 4086 | ludovic_arnaud | // do not try to re-cache it if it's already in
|
| 153 | 3863 | ludovic_arnaud | continue;
|
| 154 | 3863 | ludovic_arnaud | } |
| 155 | 4670 | ludovic_arnaud | $bbcode_ids[] = $bbcode_id; |
| 156 | 3812 | ludovic_arnaud | |
| 157 | 4819 | acydburn | if ($bbcode_id > NUM_CORE_BBCODES) |
| 158 | 182 | psotfx | {
|
| 159 | 6271 | acydburn | $sql[] = $bbcode_id; |
| 160 | 182 | psotfx | } |
| 161 | 3812 | ludovic_arnaud | } |
| 162 | 4453 | ludovic_arnaud | |
| 163 | 6271 | acydburn | if (sizeof($sql)) |
| 164 | 3812 | ludovic_arnaud | {
|
| 165 | 3812 | ludovic_arnaud | global $db; |
| 166 | 792 | thefinn | |
| 167 | 4453 | ludovic_arnaud | $sql = 'SELECT * |
| 168 | 6271 | acydburn | FROM ' . BBCODES_TABLE . ' |
| 169 | 6271 | acydburn | WHERE ' . $db->sql_in_set('bbcode_id', $sql); |
| 170 | 6271 | acydburn | $result = $db->sql_query($sql, 3600); |
| 171 | 792 | thefinn | |
| 172 | 3812 | ludovic_arnaud | while ($row = $db->sql_fetchrow($result)) |
| 173 | 3812 | ludovic_arnaud | {
|
| 174 | 6391 | acydburn | // To circumvent replacing newlines with <br /> for the generated html,
|
| 175 | 8050 | naderman | // we use carriage returns here. They are later changed back to newlines
|
| 176 | 8050 | naderman | $row['bbcode_tpl'] = str_replace("\n", "\r", $row['bbcode_tpl']); |
| 177 | 8050 | naderman | $row['second_pass_replace'] = str_replace("\n", "\r", $row['second_pass_replace']); |
| 178 | 6391 | acydburn | |
| 179 | 3812 | ludovic_arnaud | $rowset[$row['bbcode_id']] = $row; |
| 180 | 3812 | ludovic_arnaud | } |
| 181 | 3863 | ludovic_arnaud | $db->sql_freeresult($result); |
| 182 | 3812 | ludovic_arnaud | } |
| 183 | 4043 | ludovic_arnaud | |
| 184 | 3812 | ludovic_arnaud | foreach ($bbcode_ids as $bbcode_id) |
| 185 | 3812 | ludovic_arnaud | {
|
| 186 | 3812 | ludovic_arnaud | switch ($bbcode_id) |
| 187 | 3812 | ludovic_arnaud | {
|
| 188 | 3812 | ludovic_arnaud | case 0: |
| 189 | 3812 | ludovic_arnaud | $this->bbcode_cache[$bbcode_id] = array( |
| 190 | 3812 | ludovic_arnaud | 'str' => array( |
| 191 | 6048 | acydburn | '[/quote:$uid]' => $this->bbcode_tpl('quote_close', $bbcode_id) |
| 192 | 3812 | ludovic_arnaud | ), |
| 193 | 3812 | ludovic_arnaud | 'preg' => array( |
| 194 | 7110 | davidmj | '#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#ise' => "\$this->bbcode_second_pass_quote('\$1', '\$2')" |
| 195 | 3812 | ludovic_arnaud | ) |
| 196 | 3812 | ludovic_arnaud | ); |
| 197 | 3812 | ludovic_arnaud | break;
|
| 198 | 6048 | acydburn | |
| 199 | 3812 | ludovic_arnaud | case 1: |
| 200 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 201 | 6048 | acydburn | 'str' => array( |
| 202 | 6048 | acydburn | '[b:$uid]' => $this->bbcode_tpl('b_open', $bbcode_id), |
| 203 | 6048 | acydburn | '[/b:$uid]' => $this->bbcode_tpl('b_close', $bbcode_id), |
| 204 | 6048 | acydburn | ) |
| 205 | 6048 | acydburn | ); |
| 206 | 3812 | ludovic_arnaud | break;
|
| 207 | 6048 | acydburn | |
| 208 | 3812 | ludovic_arnaud | case 2: |
| 209 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 210 | 6048 | acydburn | 'str' => array( |
| 211 | 6048 | acydburn | '[i:$uid]' => $this->bbcode_tpl('i_open', $bbcode_id), |
| 212 | 6048 | acydburn | '[/i:$uid]' => $this->bbcode_tpl('i_close', $bbcode_id), |
| 213 | 6048 | acydburn | ) |
| 214 | 6048 | acydburn | ); |
| 215 | 3812 | ludovic_arnaud | break;
|
| 216 | 6048 | acydburn | |
| 217 | 3812 | ludovic_arnaud | case 3: |
| 218 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 219 | 6048 | acydburn | 'preg' => array( |
| 220 | 6048 | acydburn | '#\[url:$uid\]((.*?))\[/url:$uid\]#s' => $this->bbcode_tpl('url', $bbcode_id), |
| 221 | 6048 | acydburn | '#\[url=([^\[]+?):$uid\](.*?)\[/url:$uid\]#s' => $this->bbcode_tpl('url', $bbcode_id), |
| 222 | 6048 | acydburn | ) |
| 223 | 6048 | acydburn | ); |
| 224 | 3812 | ludovic_arnaud | break;
|
| 225 | 6048 | acydburn | |
| 226 | 3812 | ludovic_arnaud | case 4: |
| 227 | 4578 | psotfx | if ($user->optionget('viewimg')) |
| 228 | 4028 | psotfx | {
|
| 229 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 230 | 6048 | acydburn | 'preg' => array( |
| 231 | 6048 | acydburn | '#\[img:$uid\](.*?)\[/img:$uid\]#s' => $this->bbcode_tpl('img', $bbcode_id), |
| 232 | 6048 | acydburn | ) |
| 233 | 6048 | acydburn | ); |
| 234 | 4028 | psotfx | } |
| 235 | 4028 | psotfx | else
|
| 236 | 4028 | psotfx | {
|
| 237 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 238 | 6048 | acydburn | 'preg' => array( |
| 239 | 6650 | acydburn | '#\[img:$uid\](.*?)\[/img:$uid\]#s' => str_replace('$2', '[ img ]', $this->bbcode_tpl('url', $bbcode_id, true)), |
| 240 | 6048 | acydburn | ) |
| 241 | 6048 | acydburn | ); |
| 242 | 4028 | psotfx | } |
| 243 | 4043 | ludovic_arnaud | break;
|
| 244 | 6048 | acydburn | |
| 245 | 3812 | ludovic_arnaud | case 5: |
| 246 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 247 | 6048 | acydburn | 'preg' => array( |
| 248 | 7147 | davidmj | '#\[size=([\-\+]?\d+):$uid\](.*?)\[/size:$uid\]#s' => $this->bbcode_tpl('size', $bbcode_id), |
| 249 | 6048 | acydburn | ) |
| 250 | 6048 | acydburn | ); |
| 251 | 3812 | ludovic_arnaud | break;
|
| 252 | 6048 | acydburn | |
| 253 | 3812 | ludovic_arnaud | case 6: |
| 254 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 255 | 6048 | acydburn | 'preg' => array( |
| 256 | 9722 | acydburn | '!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+):$uid\](.*?)\[/color:$uid\]!is' => $this->bbcode_tpl('color', $bbcode_id), |
| 257 | 6048 | acydburn | ) |
| 258 | 6048 | acydburn | ); |
| 259 | 3812 | ludovic_arnaud | break;
|
| 260 | 6048 | acydburn | |
| 261 | 3812 | ludovic_arnaud | case 7: |
| 262 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 263 | 6048 | acydburn | 'str' => array( |
| 264 | 6048 | acydburn | '[u:$uid]' => $this->bbcode_tpl('u_open', $bbcode_id), |
| 265 | 6048 | acydburn | '[/u:$uid]' => $this->bbcode_tpl('u_close', $bbcode_id), |
| 266 | 6048 | acydburn | ) |
| 267 | 6048 | acydburn | ); |
| 268 | 3812 | ludovic_arnaud | break;
|
| 269 | 6048 | acydburn | |
| 270 | 3812 | ludovic_arnaud | case 8: |
| 271 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 272 | 6048 | acydburn | 'preg' => array( |
| 273 | 6048 | acydburn | '#\[code(?:=([a-z]+))?:$uid\](.*?)\[/code:$uid\]#ise' => "\$this->bbcode_second_pass_code('\$1', '\$2')", |
| 274 | 6048 | acydburn | ) |
| 275 | 6048 | acydburn | ); |
| 276 | 3812 | ludovic_arnaud | break;
|
| 277 | 6048 | acydburn | |
| 278 | 3812 | ludovic_arnaud | case 9: |
| 279 | 3812 | ludovic_arnaud | $this->bbcode_cache[$bbcode_id] = array( |
| 280 | 5023 | acydburn | 'preg' => array( |
| 281 | 5023 | acydburn | '#(\[\/?(list|\*):[mou]?:?$uid\])[\n]{1}#' => "\$1", |
| 282 | 6048 | acydburn | '#(\[list=([^\[]+):$uid\])[\n]{1}#' => "\$1", |
| 283 | 6048 | acydburn | '#\[list=([^\[]+):$uid\]#e' => "\$this->bbcode_list('\$1')", |
| 284 | 5023 | acydburn | ), |
| 285 | 3812 | ludovic_arnaud | 'str' => array( |
| 286 | 6048 | acydburn | '[list:$uid]' => $this->bbcode_tpl('ulist_open_default', $bbcode_id), |
| 287 | 6048 | acydburn | '[/list:u:$uid]' => $this->bbcode_tpl('ulist_close', $bbcode_id), |
| 288 | 6048 | acydburn | '[/list:o:$uid]' => $this->bbcode_tpl('olist_close', $bbcode_id), |
| 289 | 6048 | acydburn | '[*:$uid]' => $this->bbcode_tpl('listitem', $bbcode_id), |
| 290 | 6048 | acydburn | '[/*:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id), |
| 291 | 6048 | acydburn | '[/*:m:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id) |
| 292 | 3812 | ludovic_arnaud | ), |
| 293 | 3812 | ludovic_arnaud | ); |
| 294 | 3812 | ludovic_arnaud | break;
|
| 295 | 6048 | acydburn | |
| 296 | 3812 | ludovic_arnaud | case 10: |
| 297 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 298 | 6048 | acydburn | 'preg' => array( |
| 299 | 6048 | acydburn | '#\[email:$uid\]((.*?))\[/email:$uid\]#is' => $this->bbcode_tpl('email', $bbcode_id), |
| 300 | 6048 | acydburn | '#\[email=([^\[]+):$uid\](.*?)\[/email:$uid\]#is' => $this->bbcode_tpl('email', $bbcode_id) |
| 301 | 6048 | acydburn | ) |
| 302 | 6048 | acydburn | ); |
| 303 | 3812 | ludovic_arnaud | break;
|
| 304 | 6048 | acydburn | |
| 305 | 3863 | ludovic_arnaud | case 11: |
| 306 | 4578 | psotfx | if ($user->optionget('viewflash')) |
| 307 | 4028 | psotfx | {
|
| 308 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 309 | 6048 | acydburn | 'preg' => array( |
| 310 | 6048 | acydburn | '#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#' => $this->bbcode_tpl('flash', $bbcode_id), |
| 311 | 6048 | acydburn | ) |
| 312 | 6048 | acydburn | ); |
| 313 | 4028 | psotfx | } |
| 314 | 4028 | psotfx | else
|
| 315 | 4028 | psotfx | {
|
| 316 | 6048 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 317 | 6048 | acydburn | 'preg' => array( |
| 318 | 6650 | acydburn | '#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#' => str_replace('$1', '$3', str_replace('$2', '[ flash ]', $this->bbcode_tpl('url', $bbcode_id, true))) |
| 319 | 6048 | acydburn | ) |
| 320 | 6048 | acydburn | ); |
| 321 | 4028 | psotfx | } |
| 322 | 4043 | ludovic_arnaud | break;
|
| 323 | 6048 | acydburn | |
| 324 | 4819 | acydburn | case 12: |
| 325 | 4984 | acydburn | $this->bbcode_cache[$bbcode_id] = array( |
| 326 | 4984 | acydburn | 'str' => array( |
| 327 | 6048 | acydburn | '[/attachment:$uid]' => $this->bbcode_tpl('inline_attachment_close', $bbcode_id) |
| 328 | 6048 | acydburn | ), |
| 329 | 4984 | acydburn | 'preg' => array( |
| 330 | 6048 | acydburn | '#\[attachment=([0-9]+):$uid\]#' => $this->bbcode_tpl('inline_attachment_open', $bbcode_id) |
| 331 | 6048 | acydburn | ) |
| 332 | 4984 | acydburn | ); |
| 333 | 6048 | acydburn | break;
|
| 334 | 6048 | acydburn | |
| 335 | 3812 | ludovic_arnaud | default:
|
| 336 | 3812 | ludovic_arnaud | if (isset($rowset[$bbcode_id])) |
| 337 | 3812 | ludovic_arnaud | {
|
| 338 | 6777 | davidmj | if ($this->template_bitfield->get($bbcode_id)) |
| 339 | 4453 | ludovic_arnaud | {
|
| 340 | 4453 | ludovic_arnaud | // The bbcode requires a custom template to be loaded
|
| 341 | 4453 | ludovic_arnaud | if (!$bbcode_tpl = $this->bbcode_tpl($rowset[$bbcode_id]['bbcode_tag'], $bbcode_id)) |
| 342 | 4453 | ludovic_arnaud | {
|
| 343 | 6048 | acydburn | // For some reason, the required template seems not to be available, use the default template
|
| 344 | 4453 | ludovic_arnaud | $bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl']; |
| 345 | 4453 | ludovic_arnaud | } |
| 346 | 4453 | ludovic_arnaud | else
|
| 347 | 4453 | ludovic_arnaud | {
|
| 348 | 4453 | ludovic_arnaud | // In order to use templates with custom bbcodes we need
|
| 349 | 4453 | ludovic_arnaud | // to replace all {VARS} to corresponding backreferences
|
| 350 | 4453 | ludovic_arnaud | // Note that backreferences are numbered from bbcode_match
|
| 351 | 10558 | git-gate | if (preg_match_all('/\{(URL|LOCAL_URL|EMAIL|TEXT|SIMPLETEXT|INTTEXT|IDENTIFIER|COLOR|NUMBER)[0-9]*\}/', $rowset[$bbcode_id]['bbcode_match'], $m)) |
| 352 | 4453 | ludovic_arnaud | {
|
| 353 | 4453 | ludovic_arnaud | foreach ($m[0] as $i => $tok) |
| 354 | 4453 | ludovic_arnaud | {
|
| 355 | 4453 | ludovic_arnaud | $bbcode_tpl = str_replace($tok, '$' . ($i + 1), $bbcode_tpl); |
| 356 | 4453 | ludovic_arnaud | } |
| 357 | 4453 | ludovic_arnaud | } |
| 358 | 4453 | ludovic_arnaud | } |
| 359 | 4453 | ludovic_arnaud | } |
| 360 | 4453 | ludovic_arnaud | else
|
| 361 | 4453 | ludovic_arnaud | {
|
| 362 | 4453 | ludovic_arnaud | // Default template
|
| 363 | 4453 | ludovic_arnaud | $bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl']; |
| 364 | 4453 | ludovic_arnaud | } |
| 365 | 4453 | ludovic_arnaud | |
| 366 | 4453 | ludovic_arnaud | // Replace {L_*} lang strings
|
| 367 | 5737 | acydburn | $bbcode_tpl = preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $bbcode_tpl); |
| 368 | 4453 | ludovic_arnaud | |
| 369 | 4453 | ludovic_arnaud | if (!empty($rowset[$bbcode_id]['second_pass_replace'])) |
| 370 | 4453 | ludovic_arnaud | {
|
| 371 | 4453 | ludovic_arnaud | // The custom BBCode requires second-pass pattern replacements
|
| 372 | 4453 | ludovic_arnaud | $this->bbcode_cache[$bbcode_id] = array( |
| 373 | 4453 | ludovic_arnaud | 'preg' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl) |
| 374 | 4453 | ludovic_arnaud | ); |
| 375 | 4453 | ludovic_arnaud | } |
| 376 | 4453 | ludovic_arnaud | else
|
| 377 | 4453 | ludovic_arnaud | {
|
| 378 | 4453 | ludovic_arnaud | $this->bbcode_cache[$bbcode_id] = array( |
| 379 | 4453 | ludovic_arnaud | 'str' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl) |
| 380 | 4453 | ludovic_arnaud | ); |
| 381 | 4453 | ludovic_arnaud | } |
| 382 | 182 | psotfx | } |
| 383 | 182 | psotfx | else
|
| 384 | 182 | psotfx | {
|
| 385 | 5315 | acydburn | $this->bbcode_cache[$bbcode_id] = false; |
| 386 | 182 | psotfx | } |
| 387 | 6048 | acydburn | break;
|
| 388 | 182 | psotfx | } |
| 389 | 182 | psotfx | } |
| 390 | 3812 | ludovic_arnaud | } |
| 391 | 3934 | ludovic_arnaud | |
| 392 | 6048 | acydburn | /**
|
| 393 | 6048 | acydburn | * Return bbcode template |
| 394 | 6048 | acydburn | */ |
| 395 | 6650 | acydburn | function bbcode_tpl($tpl_name, $bbcode_id = -1, $skip_bitfield_check = false) |
| 396 | 3934 | ludovic_arnaud | {
|
| 397 | 6777 | davidmj | static $bbcode_hardtpl = array(); |
| 398 | 4170 | psotfx | if (empty($bbcode_hardtpl)) |
| 399 | 4170 | psotfx | {
|
| 400 | 6073 | acydburn | global $user; |
| 401 | 9461 | acydburn | |
| 402 | 6073 | acydburn | $bbcode_hardtpl = array( |
| 403 | 6048 | acydburn | 'b_open' => '<span style="font-weight: bold">', |
| 404 | 6048 | acydburn | 'b_close' => '</span>', |
| 405 | 6048 | acydburn | 'i_open' => '<span style="font-style: italic">', |
| 406 | 6048 | acydburn | 'i_close' => '</span>', |
| 407 | 6048 | acydburn | 'u_open' => '<span style="text-decoration: underline">', |
| 408 | 6048 | acydburn | 'u_close' => '</span>', |
| 409 | 6073 | acydburn | 'img' => '<img src="$1" alt="' . $user->lang['IMAGE'] . '" />', |
| 410 | 7157 | dhn2 | 'size' => '<span style="font-size: $1%; line-height: normal">$2</span>', |
| 411 | 6048 | acydburn | 'color' => '<span style="color: $1">$2</span>', |
| 412 | 6048 | acydburn | 'email' => '<a href="mailto:$1">$2</a>' |
| 413 | 4170 | psotfx | ); |
| 414 | 4170 | psotfx | } |
| 415 | 4170 | psotfx | |
| 416 | 6777 | davidmj | if ($bbcode_id != -1 && !$skip_bitfield_check && !$this->template_bitfield->get($bbcode_id)) |
| 417 | 4043 | ludovic_arnaud | {
|
| 418 | 5315 | acydburn | return (isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false; |
| 419 | 4043 | ludovic_arnaud | } |
| 420 | 4043 | ludovic_arnaud | |
| 421 | 4170 | psotfx | if (empty($this->bbcode_template)) |
| 422 | 3934 | ludovic_arnaud | {
|
| 423 | 5422 | davidmj | if (($tpl = file_get_contents($this->template_filename)) === false) |
| 424 | 3934 | ludovic_arnaud | {
|
| 425 | 6048 | acydburn | trigger_error('Could not load bbcode template', E_USER_ERROR); |
| 426 | 3934 | ludovic_arnaud | } |
| 427 | 3934 | ludovic_arnaud | |
| 428 | 3934 | ludovic_arnaud | // replace \ with \\ and then ' with \'.
|
| 429 | 3934 | ludovic_arnaud | $tpl = str_replace('\\', '\\\\', $tpl); |
| 430 | 3934 | ludovic_arnaud | $tpl = str_replace("'", "\'", $tpl); |
| 431 | 6048 | acydburn | |
| 432 | 4043 | ludovic_arnaud | // strip newlines and indent
|
| 433 | 4043 | ludovic_arnaud | $tpl = preg_replace("/\n[\n\r\s\t]*/", '', $tpl); |
| 434 | 6048 | acydburn | |
| 435 | 3934 | ludovic_arnaud | // Turn template blocks into PHP assignment statements for the values of $bbcode_tpl..
|
| 436 | 5422 | davidmj | $this->bbcode_template = array(); |
| 437 | 3934 | ludovic_arnaud | |
| 438 | 5422 | davidmj | $matches = preg_match_all('#<!-- BEGIN (.*?) -->(.*?)<!-- END (?:.*?) -->#', $tpl, $match); |
| 439 | 5422 | davidmj | |
| 440 | 5977 | acydburn | for ($i = 0; $i < $matches; $i++) |
| 441 | 5422 | davidmj | {
|
| 442 | 5977 | acydburn | if (empty($match[1][$i])) |
| 443 | 5977 | acydburn | {
|
| 444 | 5977 | acydburn | continue;
|
| 445 | 5977 | acydburn | } |
| 446 | 5977 | acydburn | |
| 447 | 5422 | davidmj | $this->bbcode_template[$match[1][$i]] = $this->bbcode_tpl_replace($match[1][$i], $match[2][$i]); |
| 448 | 5422 | davidmj | } |
| 449 | 4043 | ludovic_arnaud | } |
| 450 | 3934 | ludovic_arnaud | |
| 451 | 5315 | acydburn | return (isset($this->bbcode_template[$tpl_name])) ? $this->bbcode_template[$tpl_name] : ((isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false); |
| 452 | 4043 | ludovic_arnaud | } |
| 453 | 6048 | acydburn | |
| 454 | 6048 | acydburn | /**
|
| 455 | 6048 | acydburn | * Return bbcode template replacement |
| 456 | 6048 | acydburn | */ |
| 457 | 4043 | ludovic_arnaud | function bbcode_tpl_replace($tpl_name, $tpl) |
| 458 | 4043 | ludovic_arnaud | {
|
| 459 | 4978 | acydburn | global $user; |
| 460 | 6048 | acydburn | |
| 461 | 4043 | ludovic_arnaud | static $replacements = array( |
| 462 | 6048 | acydburn | 'quote_username_open' => array('{USERNAME}' => '$1'), |
| 463 | 6048 | acydburn | 'color' => array('{COLOR}' => '$1', '{TEXT}' => '$2'), |
| 464 | 6048 | acydburn | 'size' => array('{SIZE}' => '$1', '{TEXT}' => '$2'), |
| 465 | 6048 | acydburn | 'img' => array('{URL}' => '$1'), |
| 466 | 6048 | acydburn | 'flash' => array('{WIDTH}' => '$1', '{HEIGHT}' => '$2', '{URL}' => '$3'), |
| 467 | 6048 | acydburn | 'url' => array('{URL}' => '$1', '{DESCRIPTION}' => '$2'), |
| 468 | 6048 | acydburn | 'email' => array('{EMAIL}' => '$1', '{DESCRIPTION}' => '$2') |
| 469 | 4043 | ludovic_arnaud | ); |
| 470 | 4014 | ludovic_arnaud | |
| 471 | 4453 | ludovic_arnaud | $tpl = preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $tpl); |
| 472 | 4043 | ludovic_arnaud | |
| 473 | 4043 | ludovic_arnaud | if (!empty($replacements[$tpl_name])) |
| 474 | 4043 | ludovic_arnaud | {
|
| 475 | 4043 | ludovic_arnaud | $tpl = strtr($tpl, $replacements[$tpl_name]); |
| 476 | 3934 | ludovic_arnaud | } |
| 477 | 3934 | ludovic_arnaud | |
| 478 | 4043 | ludovic_arnaud | return trim($tpl); |
| 479 | 3934 | ludovic_arnaud | } |
| 480 | 6048 | acydburn | |
| 481 | 6048 | acydburn | /**
|
| 482 | 6048 | acydburn | * Second parse list bbcode |
| 483 | 6048 | acydburn | */ |
| 484 | 4086 | ludovic_arnaud | function bbcode_list($type) |
| 485 | 182 | psotfx | {
|
| 486 | 6067 | acydburn | if ($type == '') |
| 487 | 3812 | ludovic_arnaud | {
|
| 488 | 6067 | acydburn | $tpl = 'ulist_open_default'; |
| 489 | 6067 | acydburn | $type = 'default'; |
| 490 | 4086 | ludovic_arnaud | } |
| 491 | 6067 | acydburn | else if ($type == 'i') |
| 492 | 6067 | acydburn | {
|
| 493 | 6067 | acydburn | $tpl = 'olist_open'; |
| 494 | 6067 | acydburn | $type = 'lower-roman'; |
| 495 | 6067 | acydburn | } |
| 496 | 6067 | acydburn | else if ($type == 'I') |
| 497 | 6067 | acydburn | {
|
| 498 | 6067 | acydburn | $tpl = 'olist_open'; |
| 499 | 6067 | acydburn | $type = 'upper-roman'; |
| 500 | 6067 | acydburn | } |
| 501 | 6067 | acydburn | else if (preg_match('#^(disc|circle|square)$#i', $type)) |
| 502 | 6067 | acydburn | {
|
| 503 | 6067 | acydburn | $tpl = 'ulist_open'; |
| 504 | 6067 | acydburn | $type = strtolower($type); |
| 505 | 6067 | acydburn | } |
| 506 | 6067 | acydburn | else if (preg_match('#^[a-z]$#', $type)) |
| 507 | 6067 | acydburn | {
|
| 508 | 6067 | acydburn | $tpl = 'olist_open'; |
| 509 | 6067 | acydburn | $type = 'lower-alpha'; |
| 510 | 6067 | acydburn | } |
| 511 | 6067 | acydburn | else if (preg_match('#[A-Z]#', $type)) |
| 512 | 6067 | acydburn | {
|
| 513 | 6067 | acydburn | $tpl = 'olist_open'; |
| 514 | 6067 | acydburn | $type = 'upper-alpha'; |
| 515 | 6067 | acydburn | } |
| 516 | 6067 | acydburn | else if (is_numeric($type)) |
| 517 | 6067 | acydburn | {
|
| 518 | 6067 | acydburn | $tpl = 'olist_open'; |
| 519 | 9461 | acydburn | $type = 'decimal'; |
| 520 | 6067 | acydburn | } |
| 521 | 6067 | acydburn | else
|
| 522 | 6067 | acydburn | {
|
| 523 | 6067 | acydburn | $tpl = 'olist_open'; |
| 524 | 9461 | acydburn | $type = 'decimal'; |
| 525 | 6067 | acydburn | } |
| 526 | 4086 | ludovic_arnaud | |
| 527 | 4086 | ludovic_arnaud | return str_replace('{LIST_TYPE}', $type, $this->bbcode_tpl($tpl)); |
| 528 | 182 | psotfx | } |
| 529 | 792 | thefinn | |
| 530 | 6048 | acydburn | /**
|
| 531 | 6048 | acydburn | * Second parse quote tag |
| 532 | 6048 | acydburn | */ |
| 533 | 5027 | acydburn | function bbcode_second_pass_quote($username, $quote) |
| 534 | 5027 | acydburn | {
|
| 535 | 5027 | acydburn | // when using the /e modifier, preg_replace slashes double-quotes but does not
|
| 536 | 5027 | acydburn | // seem to slash anything else
|
| 537 | 5027 | acydburn | $quote = str_replace('\"', '"', $quote); |
| 538 | 6042 | davidmj | $username = str_replace('\"', '"', $username); |
| 539 | 5027 | acydburn | |
| 540 | 5027 | acydburn | // remove newline at the beginning
|
| 541 | 7107 | davidmj | if ($quote == "\n") |
| 542 | 5027 | acydburn | {
|
| 543 | 7107 | davidmj | $quote = ''; |
| 544 | 5027 | acydburn | } |
| 545 | 5027 | acydburn | |
| 546 | 5027 | acydburn | $quote = (($username) ? str_replace('$1', $username, $this->bbcode_tpl('quote_username_open')) : $this->bbcode_tpl('quote_open')) . $quote; |
| 547 | 5027 | acydburn | |
| 548 | 5027 | acydburn | return $quote; |
| 549 | 5027 | acydburn | } |
| 550 | 5027 | acydburn | |
| 551 | 6048 | acydburn | /**
|
| 552 | 6048 | acydburn | * Second parse code tag |
| 553 | 6048 | acydburn | */ |
| 554 | 3863 | ludovic_arnaud | function bbcode_second_pass_code($type, $code) |
| 555 | 3812 | ludovic_arnaud | {
|
| 556 | 4453 | ludovic_arnaud | // when using the /e modifier, preg_replace slashes double-quotes but does not
|
| 557 | 4453 | ludovic_arnaud | // seem to slash anything else
|
| 558 | 4453 | ludovic_arnaud | $code = str_replace('\"', '"', $code); |
| 559 | 3863 | ludovic_arnaud | |
| 560 | 3863 | ludovic_arnaud | switch ($type) |
| 561 | 3863 | ludovic_arnaud | {
|
| 562 | 3863 | ludovic_arnaud | case 'php': |
| 563 | 4984 | acydburn | // Not the english way, but valid because of hardcoded syntax highlighting
|
| 564 | 4984 | acydburn | if (strpos($code, '<span class="syntaxdefault"><br /></span>') === 0) |
| 565 | 4984 | acydburn | {
|
| 566 | 4984 | acydburn | $code = substr($code, 41); |
| 567 | 4984 | acydburn | } |
| 568 | 4984 | acydburn | |
| 569 | 6048 | acydburn | // no break;
|
| 570 | 6048 | acydburn | |
| 571 | 3863 | ludovic_arnaud | default:
|
| 572 | 8953 | acydburn | $code = str_replace("\t", ' ', $code); |
| 573 | 3863 | ludovic_arnaud | $code = str_replace(' ', ' ', $code); |
| 574 | 3863 | ludovic_arnaud | $code = str_replace(' ', ' ', $code); |
| 575 | 11347 | git-gate | $code = str_replace("\n ", "\n ", $code); |
| 576 | 8953 | acydburn | |
| 577 | 11347 | git-gate | // keep space at the beginning
|
| 578 | 11347 | git-gate | if (!empty($code) && $code[0] == ' ') |
| 579 | 11347 | git-gate | {
|
| 580 | 11347 | git-gate | $code = ' ' . substr($code, 1); |
| 581 | 11347 | git-gate | } |
| 582 | 11347 | git-gate | |
| 583 | 4984 | acydburn | // remove newline at the beginning
|
| 584 | 6459 | acydburn | if (!empty($code) && $code[0] == "\n") |
| 585 | 4984 | acydburn | {
|
| 586 | 4984 | acydburn | $code = substr($code, 1); |
| 587 | 4984 | acydburn | } |
| 588 | 6048 | acydburn | break;
|
| 589 | 3863 | ludovic_arnaud | } |
| 590 | 3863 | ludovic_arnaud | |
| 591 | 3934 | ludovic_arnaud | $code = $this->bbcode_tpl('code_open') . $code . $this->bbcode_tpl('code_close'); |
| 592 | 182 | psotfx | |
| 593 | 3812 | ludovic_arnaud | return $code; |
| 594 | 3812 | ludovic_arnaud | } |
| 595 | 328 | thefinn | } |

