[ Index ] |
PHP Cross Reference of phpBB 3.0 Beta 3 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * @package acp 5 * @version $Id: acp_bbcodes.php,v 1.29 2006/11/03 21:04:08 acydburn Exp $ 6 * @copyright (c) 2005 phpBB Group 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 8 * 9 */ 10 11 /** 12 * @package acp 13 */ 14 class acp_bbcodes 15 { 16 var $u_action; 17 18 function main($id, $mode) 19 { 20 global $db, $user, $auth, $template, $cache; 21 global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx; 22 23 $user->add_lang('acp/posting'); 24 25 // Set up general vars 26 $action = request_var('action', ''); 27 $bbcode_id = request_var('bbcode', 0); 28 29 $this->tpl_name = 'acp_bbcodes'; 30 $this->page_title = 'ACP_BBCODES'; 31 32 // Set up mode-specific vars 33 switch ($action) 34 { 35 case 'add': 36 $bbcode_match = $bbcode_tpl = $bbcode_helpline = ''; 37 $display_on_posting = 0; 38 break; 39 40 case 'edit': 41 $sql = 'SELECT bbcode_match, bbcode_tpl, display_on_posting, bbcode_helpline 42 FROM ' . BBCODES_TABLE . ' 43 WHERE bbcode_id = ' . $bbcode_id; 44 $result = $db->sql_query($sql); 45 $row = $db->sql_fetchrow($result); 46 $db->sql_freeresult($result); 47 48 if (!$row) 49 { 50 trigger_error($user->lang['BBCODE_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING); 51 } 52 53 $bbcode_match = $row['bbcode_match']; 54 $bbcode_tpl = htmlspecialchars($row['bbcode_tpl']); 55 $display_on_posting = $row['display_on_posting']; 56 $bbcode_helpline = $row['bbcode_helpline']; 57 break; 58 59 case 'modify': 60 $sql = 'SELECT bbcode_id, bbcode_tag 61 FROM ' . BBCODES_TABLE . ' 62 WHERE bbcode_id = ' . $bbcode_id; 63 $result = $db->sql_query($sql); 64 $row = $db->sql_fetchrow($result); 65 $db->sql_freeresult($result); 66 67 if (!$row) 68 { 69 trigger_error($user->lang['BBCODE_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING); 70 } 71 72 // No break here 73 74 case 'create': 75 $display_on_posting = request_var('display_on_posting', 0); 76 77 $bbcode_match = request_var('bbcode_match', ''); 78 $bbcode_tpl = htmlspecialchars_decode(request_var('bbcode_tpl', '')); 79 $bbcode_helpline = request_var('bbcode_helpline', ''); 80 break; 81 } 82 83 // Do major work 84 switch ($action) 85 { 86 case 'edit': 87 case 'add': 88 89 $template->assign_vars(array( 90 'S_EDIT_BBCODE' => true, 91 'U_BACK' => $this->u_action, 92 'U_ACTION' => $this->u_action . '&action=' . (($action == 'add') ? 'create' : 'modify') . (($bbcode_id) ? "&bbcode=$bbcode_id" : ''), 93 94 'L_BBCODE_USAGE_EXPLAIN'=> sprintf($user->lang['BBCODE_USAGE_EXPLAIN'], '<a href="#down">', '</a>'), 95 'BBCODE_MATCH' => $bbcode_match, 96 'BBCODE_TPL' => $bbcode_tpl, 97 'BBCODE_HELPLINE' => $bbcode_helpline, 98 'DISPLAY_ON_POSTING' => $display_on_posting) 99 ); 100 101 foreach ($user->lang['tokens'] as $token => $token_explain) 102 { 103 $template->assign_block_vars('token', array( 104 'TOKEN' => '{' . $token . '}', 105 'EXPLAIN' => $token_explain) 106 ); 107 } 108 109 return; 110 111 break; 112 113 case 'modify': 114 case 'create': 115 116 $data = $this->build_regexp($bbcode_match, $bbcode_tpl); 117 118 // Make sure the user didn't pick a "bad" name for the BBCode tag. 119 $hard_coded = array('code', 'quote', 'quote=', 'attachment', 'attachment=', 'b', 'i', 'url', 'url=', 'img', 'size', 'size=', 'color', 'color=', 'u', 'list', 'list=', 'email', 'email=', 'flash', 'flash='); 120 121 if (($action == 'modify' && $data['bbcode_tag'] !== $row['bbcode_tag']) || ($action == 'create')) 122 { 123 $sql = 'SELECT 1 as test 124 FROM ' . BBCODES_TABLE . " 125 WHERE LOWER(bbcode_tag) = '" . $db->sql_escape(strtolower($data['bbcode_tag'])) . "'"; 126 $result = $db->sql_query($sql); 127 $info = $db->sql_fetchrow($result); 128 $db->sql_freeresult($result); 129 130 // Grab the end, interrogate the last closing tag 131 preg_match('#\[/([^[]*)]$#', $bbcode_match, $regs); 132 if ($info['test'] === '1' || in_array(strtolower($data['bbcode_tag']), $hard_coded) || in_array(strtolower($regs[1]), $hard_coded)) 133 { 134 trigger_error($user->lang['BBCODE_INVALID_TAG_NAME'] . adm_back_link($this->u_action), E_USER_WARNING); 135 } 136 } 137 138 $sql_ary = array( 139 'bbcode_tag' => $data['bbcode_tag'], 140 'bbcode_match' => $bbcode_match, 141 'bbcode_tpl' => $bbcode_tpl, 142 'display_on_posting' => $display_on_posting, 143 'bbcode_helpline' => $bbcode_helpline, 144 'first_pass_match' => $data['first_pass_match'], 145 'first_pass_replace' => $data['first_pass_replace'], 146 'second_pass_match' => $data['second_pass_match'], 147 'second_pass_replace' => $data['second_pass_replace'] 148 ); 149 150 if ($action == 'create') 151 { 152 $sql = 'SELECT MAX(bbcode_id) as max_bbcode_id 153 FROM ' . BBCODES_TABLE; 154 $result = $db->sql_query($sql); 155 $row = $db->sql_fetchrow($result); 156 $db->sql_freeresult($result); 157 158 if ($row) 159 { 160 $bbcode_id = $row['max_bbcode_id'] + 1; 161 162 // Make sure it is greater than the core bbcode ids... 163 if ($bbcode_id <= NUM_CORE_BBCODES) 164 { 165 $bbcode_id = NUM_CORE_BBCODES + 1; 166 } 167 } 168 else 169 { 170 $bbcode_id = NUM_CORE_BBCODES + 1; 171 } 172 173 if ($bbcode_id > 1511) 174 { 175 trigger_error($user->lang['TOO_MANY_BBCODES'] . adm_back_link($this->u_action), E_USER_WARNING); 176 } 177 178 $sql_ary['bbcode_id'] = (int) $bbcode_id; 179 180 $db->sql_query('INSERT INTO ' . BBCODES_TABLE . $db->sql_build_array('INSERT', $sql_ary)); 181 $cache->destroy('sql', BBCODES_TABLE); 182 183 $lang = 'BBCODE_ADDED'; 184 $log_action = 'LOG_BBCODE_ADD'; 185 } 186 else 187 { 188 $sql = 'UPDATE ' . BBCODES_TABLE . ' 189 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' 190 WHERE bbcode_id = ' . $bbcode_id; 191 $db->sql_query($sql); 192 $cache->destroy('sql', BBCODES_TABLE); 193 194 $lang = 'BBCODE_EDITED'; 195 $log_action = 'LOG_BBCODE_EDIT'; 196 } 197 198 add_log('admin', $log_action, $data['bbcode_tag']); 199 200 trigger_error($user->lang[$lang] . adm_back_link($this->u_action)); 201 202 break; 203 204 case 'delete': 205 206 $sql = 'SELECT bbcode_tag 207 FROM ' . BBCODES_TABLE . " 208 WHERE bbcode_id = $bbcode_id"; 209 $result = $db->sql_query($sql); 210 $row = $db->sql_fetchrow($result); 211 $db->sql_freeresult($result); 212 213 if ($row) 214 { 215 if (confirm_box(true)) 216 { 217 $db->sql_query('DELETE FROM ' . BBCODES_TABLE . " WHERE bbcode_id = $bbcode_id"); 218 $cache->destroy('sql', BBCODES_TABLE); 219 add_log('admin', 'LOG_BBCODE_DELETE', $row['bbcode_tag']); 220 } 221 else 222 { 223 confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array( 224 'bbcode' => $bbcode_id, 225 'i' => $id, 226 'mode' => $mode, 227 'action' => $action)) 228 ); 229 } 230 } 231 232 break; 233 } 234 235 $template->assign_vars(array( 236 'U_ACTION' => $this->u_action . '&action=add') 237 ); 238 239 $sql = 'SELECT * 240 FROM ' . BBCODES_TABLE . ' 241 ORDER BY bbcode_id'; 242 $result = $db->sql_query($sql); 243 244 while ($row = $db->sql_fetchrow($result)) 245 { 246 $template->assign_block_vars('bbcodes', array( 247 'BBCODE_TAG' => $row['bbcode_tag'], 248 'U_EDIT' => $this->u_action . '&action=edit&bbcode=' . $row['bbcode_id'], 249 'U_DELETE' => $this->u_action . '&action=delete&bbcode=' . $row['bbcode_id']) 250 ); 251 } 252 $db->sql_freeresult($result); 253 } 254 255 /* 256 * Build regular expression for custom bbcode 257 */ 258 function build_regexp(&$bbcode_match, &$bbcode_tpl) 259 { 260 $bbcode_match = trim($bbcode_match); 261 $bbcode_tpl = trim($bbcode_tpl); 262 263 $fp_match = preg_quote($bbcode_match, '!'); 264 $fp_replace = preg_replace('#^\[(.*?)\]#', '[$1:$uid]', $bbcode_match); 265 $fp_replace = preg_replace('#\[/(.*?)\]$#', '[/$1:$uid]', $fp_replace); 266 267 $sp_match = preg_quote($bbcode_match, '!'); 268 $sp_match = preg_replace('#^\\\\\[(.*?)\\\\\]#', '\[$1:$uid\]', $sp_match); 269 $sp_match = preg_replace('#\\\\\[/(.*?)\\\\\]$#', '\[/$1:$uid\]', $sp_match); 270 $sp_replace = $bbcode_tpl; 271 272 // @todo Make sure to change this too if something changed in message parsing 273 $tokens = array( 274 'URL' => array( 275 '!([a-z0-9]+://)?([^?].*?[^ \t\n\r<"]*)!ie' => "(('\$1') ? '\$1\$2' : 'http://\$2')" 276 ), 277 'LOCAL_URL' => array( 278 '!([^:]+/[^ \t\n\r<"]*)!' => '$1' 279 ), 280 'EMAIL' => array( 281 '!([a-z0-9]+[a-z0-9\-\._]*@(?:(?:[0-9]{1,3}\.){3,5}[0-9]{1,3}|[a-z0-9]+[a-z0-9\-\._]*\.[a-z]+))!i' => '$1' 282 ), 283 'TEXT' => array( 284 '!(.*?)!es' => "str_replace(\"\\r\\n\",\"\\n\", str_replace('\\\"', '\"', str_replace('\\'', ''', trim('\$1'))))" 285 ), 286 'COLOR' => array( 287 '!([a-z]+|#[0-9abcdef]+)!i' => '$1' 288 ), 289 'NUMBER' => array( 290 '!([0-9]+)!' => '$1' 291 ) 292 ); 293 294 $pad = 0; 295 $modifiers = 'i'; 296 297 if (preg_match_all('/\{(' . implode('|', array_keys($tokens)) . ')[0-9]*\}/i', $bbcode_match, $m)) 298 { 299 foreach ($m[0] as $n => $token) 300 { 301 $token_type = $m[1][$n]; 302 303 reset($tokens[strtoupper($token_type)]); 304 list($match, $replace) = each($tokens[strtoupper($token_type)]); 305 306 // Pad backreference numbers from tokens 307 if (preg_match_all('/(?<!\\\\)\$([0-9]+)/', $replace, $repad)) 308 { 309 $repad = $pad + sizeof(array_unique($repad[0])); 310 $replace = preg_replace('/(?<!\\\\)\$([0-9]+)/e', "'\${' . (\$1 + \$pad) . '}'", $replace); 311 $pad = $repad; 312 } 313 314 // Obtain pattern modifiers to use and alter the regex accordingly 315 $regex = preg_replace('/!(.*)!([a-z]*)/', '$1', $match); 316 $regex_modifiers = preg_replace('/!(.*)!([a-z]*)/', '$2', $match); 317 318 for ($i = 0, $size = strlen($regex_modifiers); $i < $size; ++$i) 319 { 320 if (strpos($modifiers, $regex_modifiers[$i]) === false) 321 { 322 $modifiers .= $regex_modifiers[$i]; 323 324 if ($regex_modifiers[$i] == 'e') 325 { 326 $fp_replace = "'" . str_replace("'", "\\'", $fp_replace) . "'"; 327 } 328 } 329 330 if ($regex_modifiers[$i] == 'e') 331 { 332 $replace = "'.$replace.'"; 333 } 334 } 335 336 $fp_match = str_replace(preg_quote($token, '!'), $regex, $fp_match); 337 $fp_replace = str_replace($token, $replace, $fp_replace); 338 339 $sp_match = str_replace(preg_quote($token, '!'), '(.*?)', $sp_match); 340 $sp_replace = str_replace($token, '${' . ($n + 1) . '}', $sp_replace); 341 } 342 343 $fp_match = '!' . $fp_match . '!' . $modifiers; 344 $sp_match = '!' . $sp_match . '!s'; 345 346 if (strpos($fp_match, 'e') !== false) 347 { 348 $fp_replace = str_replace("'.'", '', $fp_replace); 349 $fp_replace = str_replace(".''.", '.', $fp_replace); 350 } 351 } 352 else 353 { 354 // No replacement is present, no need for a second-pass pattern replacement 355 // A simple str_replace will suffice 356 $fp_match = '!' . $fp_match . '!' . $modifiers; 357 $sp_match = $fp_replace; 358 $sp_replace = ''; 359 } 360 361 // Lowercase tags 362 $bbcode_tag = preg_replace('/.*?\[([a-z0-9_-]+=?).*/i', '$1', $bbcode_match); 363 $fp_match = preg_replace('#\[/?' . $bbcode_tag . '#ie', "strtolower('\$0')", $fp_match); 364 $fp_replace = preg_replace('#\[/?' . $bbcode_tag . '#ie', "strtolower('\$0')", $fp_replace); 365 $sp_match = preg_replace('#\[/?' . $bbcode_tag . '#ie', "strtolower('\$0')", $sp_match); 366 $sp_replace = preg_replace('#\[/?' . $bbcode_tag . '#ie', "strtolower('\$0')", $sp_replace); 367 368 return array( 369 'bbcode_tag' => $bbcode_tag, 370 'first_pass_match' => $fp_match, 371 'first_pass_replace' => $fp_replace, 372 'second_pass_match' => $sp_match, 373 'second_pass_replace' => $sp_replace 374 ); 375 } 376 } 377 378 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 22 00:35:05 2006 | Cross-referenced by PHPXref 0.6 |