1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/acp/acp_bbcodes.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,476 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package acp
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* @package acp
1.24 +*/
1.25 +class acp_bbcodes
1.26 +{
1.27 + var $u_action;
1.28 +
1.29 + function main($id, $mode)
1.30 + {
1.31 + global $db, $user, $auth, $template, $cache;
1.32 + global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
1.33 +
1.34 + $user->add_lang('acp/posting');
1.35 +
1.36 + // Set up general vars
1.37 + $action = request_var('action', '');
1.38 + $bbcode_id = request_var('bbcode', 0);
1.39 +
1.40 + $this->tpl_name = 'acp_bbcodes';
1.41 + $this->page_title = 'ACP_BBCODES';
1.42 + $form_key = 'acp_bbcodes';
1.43 +
1.44 + add_form_key($form_key);
1.45 +
1.46 + // Set up mode-specific vars
1.47 + switch ($action)
1.48 + {
1.49 + case 'add':
1.50 + $bbcode_match = $bbcode_tpl = $bbcode_helpline = '';
1.51 + $display_on_posting = 0;
1.52 + break;
1.53 +
1.54 + case 'edit':
1.55 + $sql = 'SELECT bbcode_match, bbcode_tpl, display_on_posting, bbcode_helpline
1.56 + FROM ' . BBCODES_TABLE . '
1.57 + WHERE bbcode_id = ' . $bbcode_id;
1.58 + $result = $db->sql_query($sql);
1.59 + $row = $db->sql_fetchrow($result);
1.60 + $db->sql_freeresult($result);
1.61 +
1.62 + if (!$row)
1.63 + {
1.64 + trigger_error($user->lang['BBCODE_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
1.65 + }
1.66 +
1.67 + $bbcode_match = $row['bbcode_match'];
1.68 + $bbcode_tpl = htmlspecialchars($row['bbcode_tpl']);
1.69 + $display_on_posting = $row['display_on_posting'];
1.70 + $bbcode_helpline = $row['bbcode_helpline'];
1.71 + break;
1.72 +
1.73 + case 'modify':
1.74 + $sql = 'SELECT bbcode_id, bbcode_tag
1.75 + FROM ' . BBCODES_TABLE . '
1.76 + WHERE bbcode_id = ' . $bbcode_id;
1.77 + $result = $db->sql_query($sql);
1.78 + $row = $db->sql_fetchrow($result);
1.79 + $db->sql_freeresult($result);
1.80 +
1.81 + if (!$row)
1.82 + {
1.83 + trigger_error($user->lang['BBCODE_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
1.84 + }
1.85 +
1.86 + // No break here
1.87 +
1.88 + case 'create':
1.89 + $display_on_posting = request_var('display_on_posting', 0);
1.90 +
1.91 + $bbcode_match = request_var('bbcode_match', '');
1.92 + $bbcode_tpl = htmlspecialchars_decode(utf8_normalize_nfc(request_var('bbcode_tpl', '', true)));
1.93 + $bbcode_helpline = utf8_normalize_nfc(request_var('bbcode_helpline', '', true));
1.94 + break;
1.95 + }
1.96 +
1.97 + // Do major work
1.98 + switch ($action)
1.99 + {
1.100 + case 'edit':
1.101 + case 'add':
1.102 +
1.103 + $template->assign_vars(array(
1.104 + 'S_EDIT_BBCODE' => true,
1.105 + 'U_BACK' => $this->u_action,
1.106 + 'U_ACTION' => $this->u_action . '&action=' . (($action == 'add') ? 'create' : 'modify') . (($bbcode_id) ? "&bbcode=$bbcode_id" : ''),
1.107 +
1.108 + 'L_BBCODE_USAGE_EXPLAIN'=> sprintf($user->lang['BBCODE_USAGE_EXPLAIN'], '<a href="#down">', '</a>'),
1.109 + 'BBCODE_MATCH' => $bbcode_match,
1.110 + 'BBCODE_TPL' => $bbcode_tpl,
1.111 + 'BBCODE_HELPLINE' => $bbcode_helpline,
1.112 + 'DISPLAY_ON_POSTING' => $display_on_posting)
1.113 + );
1.114 +
1.115 + foreach ($user->lang['tokens'] as $token => $token_explain)
1.116 + {
1.117 + $template->assign_block_vars('token', array(
1.118 + 'TOKEN' => '{' . $token . '}',
1.119 + 'EXPLAIN' => $token_explain)
1.120 + );
1.121 + }
1.122 +
1.123 + return;
1.124 +
1.125 + break;
1.126 +
1.127 + case 'modify':
1.128 + case 'create':
1.129 +
1.130 + $warn_text = preg_match('%<[^>]*\{text[\d]*\}[^>]*>%i', $bbcode_tpl);
1.131 + if (!$warn_text || confirm_box(true))
1.132 + {
1.133 + $data = $this->build_regexp($bbcode_match, $bbcode_tpl);
1.134 +
1.135 + // Make sure the user didn't pick a "bad" name for the BBCode tag.
1.136 + $hard_coded = array('code', 'quote', 'quote=', 'attachment', 'attachment=', 'b', 'i', 'url', 'url=', 'img', 'size', 'size=', 'color', 'color=', 'u', 'list', 'list=', 'email', 'email=', 'flash', 'flash=');
1.137 +
1.138 + if (($action == 'modify' && strtolower($data['bbcode_tag']) !== strtolower($row['bbcode_tag'])) || ($action == 'create'))
1.139 + {
1.140 + $sql = 'SELECT 1 as test
1.141 + FROM ' . BBCODES_TABLE . "
1.142 + WHERE LOWER(bbcode_tag) = '" . $db->sql_escape(strtolower($data['bbcode_tag'])) . "'";
1.143 + $result = $db->sql_query($sql);
1.144 + $info = $db->sql_fetchrow($result);
1.145 + $db->sql_freeresult($result);
1.146 +
1.147 + // Grab the end, interrogate the last closing tag
1.148 + if ($info['test'] === '1' || in_array(strtolower($data['bbcode_tag']), $hard_coded) || (preg_match('#\[/([^[]*)]$#', $bbcode_match, $regs) && in_array(strtolower($regs[1]), $hard_coded)))
1.149 + {
1.150 + trigger_error($user->lang['BBCODE_INVALID_TAG_NAME'] . adm_back_link($this->u_action), E_USER_WARNING);
1.151 + }
1.152 + }
1.153 +
1.154 + if (substr($data['bbcode_tag'], -1) === '=')
1.155 + {
1.156 + $test = substr($data['bbcode_tag'], 0, -1);
1.157 + }
1.158 + else
1.159 + {
1.160 + $test = $data['bbcode_tag'];
1.161 + }
1.162 +
1.163 + if (!preg_match('%\\[' . $test . '[^]]*].*?\\[/' . $test . ']%s', $bbcode_match))
1.164 + {
1.165 + trigger_error($user->lang['BBCODE_OPEN_ENDED_TAG'] . adm_back_link($this->u_action), E_USER_WARNING);
1.166 + }
1.167 +
1.168 + if (strlen($data['bbcode_tag']) > 16)
1.169 + {
1.170 + trigger_error($user->lang['BBCODE_TAG_TOO_LONG'] . adm_back_link($this->u_action), E_USER_WARNING);
1.171 + }
1.172 +
1.173 + if (strlen($bbcode_match) > 4000)
1.174 + {
1.175 + trigger_error($user->lang['BBCODE_TAG_DEF_TOO_LONG'] . adm_back_link($this->u_action), E_USER_WARNING);
1.176 + }
1.177 +
1.178 +
1.179 + if (strlen($bbcode_helpline) > 255)
1.180 + {
1.181 + trigger_error($user->lang['BBCODE_HELPLINE_TOO_LONG'] . adm_back_link($this->u_action), E_USER_WARNING);
1.182 + }
1.183 +
1.184 + $sql_ary = array(
1.185 + 'bbcode_tag' => $data['bbcode_tag'],
1.186 + 'bbcode_match' => $bbcode_match,
1.187 + 'bbcode_tpl' => $bbcode_tpl,
1.188 + 'display_on_posting' => $display_on_posting,
1.189 + 'bbcode_helpline' => $bbcode_helpline,
1.190 + 'first_pass_match' => $data['first_pass_match'],
1.191 + 'first_pass_replace' => $data['first_pass_replace'],
1.192 + 'second_pass_match' => $data['second_pass_match'],
1.193 + 'second_pass_replace' => $data['second_pass_replace']
1.194 + );
1.195 +
1.196 + if ($action == 'create')
1.197 + {
1.198 + $sql = 'SELECT MAX(bbcode_id) as max_bbcode_id
1.199 + FROM ' . BBCODES_TABLE;
1.200 + $result = $db->sql_query($sql);
1.201 + $row = $db->sql_fetchrow($result);
1.202 + $db->sql_freeresult($result);
1.203 +
1.204 + if ($row)
1.205 + {
1.206 + $bbcode_id = $row['max_bbcode_id'] + 1;
1.207 +
1.208 + // Make sure it is greater than the core bbcode ids...
1.209 + if ($bbcode_id <= NUM_CORE_BBCODES)
1.210 + {
1.211 + $bbcode_id = NUM_CORE_BBCODES + 1;
1.212 + }
1.213 + }
1.214 + else
1.215 + {
1.216 + $bbcode_id = NUM_CORE_BBCODES + 1;
1.217 + }
1.218 +
1.219 + if ($bbcode_id > BBCODE_LIMIT)
1.220 + {
1.221 + trigger_error($user->lang['TOO_MANY_BBCODES'] . adm_back_link($this->u_action), E_USER_WARNING);
1.222 + }
1.223 +
1.224 + $sql_ary['bbcode_id'] = (int) $bbcode_id;
1.225 +
1.226 + $db->sql_query('INSERT INTO ' . BBCODES_TABLE . $db->sql_build_array('INSERT', $sql_ary));
1.227 + $cache->destroy('sql', BBCODES_TABLE);
1.228 +
1.229 + $lang = 'BBCODE_ADDED';
1.230 + $log_action = 'LOG_BBCODE_ADD';
1.231 + }
1.232 + else
1.233 + {
1.234 + $sql = 'UPDATE ' . BBCODES_TABLE . '
1.235 + SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1.236 + WHERE bbcode_id = ' . $bbcode_id;
1.237 + $db->sql_query($sql);
1.238 + $cache->destroy('sql', BBCODES_TABLE);
1.239 +
1.240 + $lang = 'BBCODE_EDITED';
1.241 + $log_action = 'LOG_BBCODE_EDIT';
1.242 + }
1.243 +
1.244 + add_log('admin', $log_action, $data['bbcode_tag']);
1.245 +
1.246 + trigger_error($user->lang[$lang] . adm_back_link($this->u_action));
1.247 + }
1.248 + else
1.249 + {
1.250 + confirm_box(false, $user->lang['BBCODE_DANGER'], build_hidden_fields(array(
1.251 + 'action' => $action,
1.252 + 'bbcode' => $bbcode_id,
1.253 + 'bbcode_match' => $bbcode_match,
1.254 + 'bbcode_tpl' => htmlspecialchars($bbcode_tpl),
1.255 + 'bbcode_helpline' => $bbcode_helpline,
1.256 + 'display_on_posting' => $display_on_posting,
1.257 + ))
1.258 + , 'confirm_bbcode.html');
1.259 + }
1.260 +
1.261 + break;
1.262 +
1.263 + case 'delete':
1.264 +
1.265 + $sql = 'SELECT bbcode_tag
1.266 + FROM ' . BBCODES_TABLE . "
1.267 + WHERE bbcode_id = $bbcode_id";
1.268 + $result = $db->sql_query($sql);
1.269 + $row = $db->sql_fetchrow($result);
1.270 + $db->sql_freeresult($result);
1.271 +
1.272 + if ($row)
1.273 + {
1.274 + if (confirm_box(true))
1.275 + {
1.276 + $db->sql_query('DELETE FROM ' . BBCODES_TABLE . " WHERE bbcode_id = $bbcode_id");
1.277 + $cache->destroy('sql', BBCODES_TABLE);
1.278 + add_log('admin', 'LOG_BBCODE_DELETE', $row['bbcode_tag']);
1.279 + }
1.280 + else
1.281 + {
1.282 + confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
1.283 + 'bbcode' => $bbcode_id,
1.284 + 'i' => $id,
1.285 + 'mode' => $mode,
1.286 + 'action' => $action))
1.287 + );
1.288 + }
1.289 + }
1.290 +
1.291 + break;
1.292 + }
1.293 +
1.294 + $template->assign_vars(array(
1.295 + 'U_ACTION' => $this->u_action . '&action=add')
1.296 + );
1.297 +
1.298 + $sql = 'SELECT *
1.299 + FROM ' . BBCODES_TABLE . '
1.300 + ORDER BY bbcode_tag';
1.301 + $result = $db->sql_query($sql);
1.302 +
1.303 + while ($row = $db->sql_fetchrow($result))
1.304 + {
1.305 + $template->assign_block_vars('bbcodes', array(
1.306 + 'BBCODE_TAG' => $row['bbcode_tag'],
1.307 + 'U_EDIT' => $this->u_action . '&action=edit&bbcode=' . $row['bbcode_id'],
1.308 + 'U_DELETE' => $this->u_action . '&action=delete&bbcode=' . $row['bbcode_id'])
1.309 + );
1.310 + }
1.311 + $db->sql_freeresult($result);
1.312 + }
1.313 +
1.314 + /*
1.315 + * Build regular expression for custom bbcode
1.316 + */
1.317 + function build_regexp(&$bbcode_match, &$bbcode_tpl)
1.318 + {
1.319 + $bbcode_match = trim($bbcode_match);
1.320 + $bbcode_tpl = trim($bbcode_tpl);
1.321 + $utf8 = strpos($bbcode_match, 'INTTEXT') !== false;
1.322 +
1.323 + // make sure we have utf8 support
1.324 + $utf8_pcre_properties = false;
1.325 + if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>=')))
1.326 + {
1.327 + // While this is the proper range of PHP versions, PHP may not be linked with the bundled PCRE lib and instead with an older version
1.328 + if (@preg_match('/\p{L}/u', 'a') !== false)
1.329 + {
1.330 + $utf8_pcre_properties = true;
1.331 + }
1.332 + }
1.333 +
1.334 + $fp_match = preg_quote($bbcode_match, '!');
1.335 + $fp_replace = preg_replace('#^\[(.*?)\]#', '[$1:$uid]', $bbcode_match);
1.336 + $fp_replace = preg_replace('#\[/(.*?)\]$#', '[/$1:$uid]', $fp_replace);
1.337 +
1.338 + $sp_match = preg_quote($bbcode_match, '!');
1.339 + $sp_match = preg_replace('#^\\\\\[(.*?)\\\\\]#', '\[$1:$uid\]', $sp_match);
1.340 + $sp_match = preg_replace('#\\\\\[/(.*?)\\\\\]$#', '\[/$1:$uid\]', $sp_match);
1.341 + $sp_replace = $bbcode_tpl;
1.342 +
1.343 + // @todo Make sure to change this too if something changed in message parsing
1.344 + $tokens = array(
1.345 + 'URL' => array(
1.346 + '!(?:(' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('url')) . ')|(' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('www_url')) . '))!ie' => "\$this->bbcode_specialchars(('\$1') ? '\$1' : 'http://\$2')"
1.347 + ),
1.348 + 'LOCAL_URL' => array(
1.349 + '!(' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('relative_url')) . ')!e' => "\$this->bbcode_specialchars('$1')"
1.350 + ),
1.351 + 'EMAIL' => array(
1.352 + '!(' . get_preg_expression('email') . ')!ie' => "\$this->bbcode_specialchars('$1')"
1.353 + ),
1.354 + 'TEXT' => array(
1.355 + '!(.*?)!es' => "str_replace(array(\"\\r\\n\", '\\\"', '\\'', '(', ')'), array(\"\\n\", '\"', ''', '(', ')'), trim('\$1'))"
1.356 + ),
1.357 + 'SIMPLETEXT' => array(
1.358 + '!([a-zA-Z0-9-+.,_ ]+)!' => "$1"
1.359 + ),
1.360 + 'INTTEXT' => array(
1.361 + ($utf8_pcre_properties) ? '!([\p{L}\p{N}\-+,_. ]+)!u' : '!([a-zA-Z0-9\-+,_. ]+)!u' => "$1"
1.362 + ),
1.363 + 'IDENTIFIER' => array(
1.364 + '!([a-zA-Z0-9-_]+)!' => "$1"
1.365 + ),
1.366 + 'COLOR' => array(
1.367 + '!([a-z]+|#[0-9abcdef]+)!i' => '$1'
1.368 + ),
1.369 + 'NUMBER' => array(
1.370 + '!([0-9]+)!' => '$1'
1.371 + )
1.372 + );
1.373 +
1.374 + $sp_tokens = array(
1.375 + 'URL' => '(?i)((?:' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('url')) . ')|(?:' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('www_url')) . '))(?-i)',
1.376 + 'LOCAL_URL' => '(?i)(' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('relative_url')) . ')(?-i)',
1.377 + 'EMAIL' => '(' . get_preg_expression('email') . ')',
1.378 + 'TEXT' => '(.*?)',
1.379 + 'SIMPLETEXT' => '([a-zA-Z0-9-+.,_ ]+)',
1.380 + 'INTTEXT' => ($utf8_pcre_properties) ? '([\p{L}\p{N}\-+,_. ]+)' : '([a-zA-Z0-9\-+,_. ]+)',
1.381 + 'IDENTIFIER' => '([a-zA-Z0-9-_]+)',
1.382 + 'COLOR' => '([a-zA-Z]+|#[0-9abcdefABCDEF]+)',
1.383 + 'NUMBER' => '([0-9]+)',
1.384 + );
1.385 +
1.386 + $pad = 0;
1.387 + $modifiers = 'i';
1.388 + $modifiers .= ($utf8 && $utf8_pcre_properties) ? 'u' : '';
1.389 +
1.390 + if (preg_match_all('/\{(' . implode('|', array_keys($tokens)) . ')[0-9]*\}/i', $bbcode_match, $m))
1.391 + {
1.392 + foreach ($m[0] as $n => $token)
1.393 + {
1.394 + $token_type = $m[1][$n];
1.395 +
1.396 + reset($tokens[strtoupper($token_type)]);
1.397 + list($match, $replace) = each($tokens[strtoupper($token_type)]);
1.398 +
1.399 + // Pad backreference numbers from tokens
1.400 + if (preg_match_all('/(?<!\\\\)\$([0-9]+)/', $replace, $repad))
1.401 + {
1.402 + $repad = $pad + sizeof(array_unique($repad[0]));
1.403 + $replace = preg_replace('/(?<!\\\\)\$([0-9]+)/e', "'\${' . (\$1 + \$pad) . '}'", $replace);
1.404 + $pad = $repad;
1.405 + }
1.406 +
1.407 + // Obtain pattern modifiers to use and alter the regex accordingly
1.408 + $regex = preg_replace('/!(.*)!([a-z]*)/', '$1', $match);
1.409 + $regex_modifiers = preg_replace('/!(.*)!([a-z]*)/', '$2', $match);
1.410 +
1.411 + for ($i = 0, $size = strlen($regex_modifiers); $i < $size; ++$i)
1.412 + {
1.413 + if (strpos($modifiers, $regex_modifiers[$i]) === false)
1.414 + {
1.415 + $modifiers .= $regex_modifiers[$i];
1.416 +
1.417 + if ($regex_modifiers[$i] == 'e')
1.418 + {
1.419 + $fp_replace = "'" . str_replace("'", "\\'", $fp_replace) . "'";
1.420 + }
1.421 + }
1.422 +
1.423 + if ($regex_modifiers[$i] == 'e')
1.424 + {
1.425 + $replace = "'.$replace.'";
1.426 + }
1.427 + }
1.428 +
1.429 + $fp_match = str_replace(preg_quote($token, '!'), $regex, $fp_match);
1.430 + $fp_replace = str_replace($token, $replace, $fp_replace);
1.431 +
1.432 + $sp_match = str_replace(preg_quote($token, '!'), $sp_tokens[$token_type], $sp_match);
1.433 + $sp_replace = str_replace($token, '${' . ($n + 1) . '}', $sp_replace);
1.434 + }
1.435 +
1.436 + $fp_match = '!' . $fp_match . '!' . $modifiers;
1.437 + $sp_match = '!' . $sp_match . '!s' . (($utf8) ? 'u' : '');
1.438 +
1.439 + if (strpos($fp_match, 'e') !== false)
1.440 + {
1.441 + $fp_replace = str_replace("'.'", '', $fp_replace);
1.442 + $fp_replace = str_replace(".''.", '.', $fp_replace);
1.443 + }
1.444 + }
1.445 + else
1.446 + {
1.447 + // No replacement is present, no need for a second-pass pattern replacement
1.448 + // A simple str_replace will suffice
1.449 + $fp_match = '!' . $fp_match . '!' . $modifiers;
1.450 + $sp_match = $fp_replace;
1.451 + $sp_replace = '';
1.452 + }
1.453 +
1.454 + // Lowercase tags
1.455 + $bbcode_tag = preg_replace('/.*?\[([a-z0-9_-]+=?).*/i', '$1', $bbcode_match);
1.456 + $bbcode_search = preg_replace('/.*?\[([a-z0-9_-]+)=?.*/i', '$1', $bbcode_match);
1.457 +
1.458 + if (!preg_match('/^[a-zA-Z0-9_-]+=?$/', $bbcode_tag))
1.459 + {
1.460 + global $user;
1.461 + trigger_error($user->lang['BBCODE_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
1.462 + }
1.463 +
1.464 + $fp_match = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $fp_match);
1.465 + $fp_replace = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $fp_replace);
1.466 + $sp_match = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $sp_match);
1.467 + $sp_replace = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $sp_replace);
1.468 +
1.469 + return array(
1.470 + 'bbcode_tag' => $bbcode_tag,
1.471 + 'first_pass_match' => $fp_match,
1.472 + 'first_pass_replace' => $fp_replace,
1.473 + 'second_pass_match' => $sp_match,
1.474 + 'second_pass_replace' => $sp_replace
1.475 + );
1.476 + }
1.477 +}
1.478 +
1.479 +?>
1.480 \ No newline at end of file