includes/acp/acp_bbcodes.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 02:49:04 +0400
changeset 19 6d3ca1857104
parent 0 ceef6d64c21d
permissions -rw-r--r--
RU translate fix
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package acp
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* @package acp
indvdum@0
    21
*/
indvdum@0
    22
class acp_bbcodes
indvdum@0
    23
{
indvdum@0
    24
	var $u_action;
indvdum@0
    25
indvdum@0
    26
	function main($id, $mode)
indvdum@0
    27
	{
indvdum@0
    28
		global $db, $user, $auth, $template, $cache;
indvdum@0
    29
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
indvdum@0
    30
indvdum@0
    31
		$user->add_lang('acp/posting');
indvdum@0
    32
indvdum@0
    33
		// Set up general vars
indvdum@0
    34
		$action	= request_var('action', '');
indvdum@0
    35
		$bbcode_id = request_var('bbcode', 0);
indvdum@0
    36
indvdum@0
    37
		$this->tpl_name = 'acp_bbcodes';
indvdum@0
    38
		$this->page_title = 'ACP_BBCODES';
indvdum@0
    39
		$form_key = 'acp_bbcodes';
indvdum@0
    40
indvdum@0
    41
		add_form_key($form_key);
indvdum@0
    42
indvdum@0
    43
		// Set up mode-specific vars
indvdum@0
    44
		switch ($action)
indvdum@0
    45
		{
indvdum@0
    46
			case 'add':
indvdum@0
    47
				$bbcode_match = $bbcode_tpl = $bbcode_helpline = '';
indvdum@0
    48
				$display_on_posting = 0;
indvdum@0
    49
			break;
indvdum@0
    50
indvdum@0
    51
			case 'edit':
indvdum@0
    52
				$sql = 'SELECT bbcode_match, bbcode_tpl, display_on_posting, bbcode_helpline
indvdum@0
    53
					FROM ' . BBCODES_TABLE . '
indvdum@0
    54
					WHERE bbcode_id = ' . $bbcode_id;
indvdum@0
    55
				$result = $db->sql_query($sql);
indvdum@0
    56
				$row = $db->sql_fetchrow($result);
indvdum@0
    57
				$db->sql_freeresult($result);
indvdum@0
    58
indvdum@0
    59
				if (!$row)
indvdum@0
    60
				{
indvdum@0
    61
					trigger_error($user->lang['BBCODE_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
    62
				}
indvdum@0
    63
indvdum@0
    64
				$bbcode_match = $row['bbcode_match'];
indvdum@0
    65
				$bbcode_tpl = htmlspecialchars($row['bbcode_tpl']);
indvdum@0
    66
				$display_on_posting = $row['display_on_posting'];
indvdum@0
    67
				$bbcode_helpline = $row['bbcode_helpline'];
indvdum@0
    68
			break;
indvdum@0
    69
indvdum@0
    70
			case 'modify':
indvdum@0
    71
				$sql = 'SELECT bbcode_id, bbcode_tag
indvdum@0
    72
					FROM ' . BBCODES_TABLE . '
indvdum@0
    73
					WHERE bbcode_id = ' . $bbcode_id;
indvdum@0
    74
				$result = $db->sql_query($sql);
indvdum@0
    75
				$row = $db->sql_fetchrow($result);
indvdum@0
    76
				$db->sql_freeresult($result);
indvdum@0
    77
indvdum@0
    78
				if (!$row)
indvdum@0
    79
				{
indvdum@0
    80
					trigger_error($user->lang['BBCODE_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
    81
				}
indvdum@0
    82
indvdum@0
    83
			// No break here
indvdum@0
    84
indvdum@0
    85
			case 'create':
indvdum@0
    86
				$display_on_posting = request_var('display_on_posting', 0);
indvdum@0
    87
indvdum@0
    88
				$bbcode_match = request_var('bbcode_match', '');
indvdum@0
    89
				$bbcode_tpl = htmlspecialchars_decode(utf8_normalize_nfc(request_var('bbcode_tpl', '', true)));
indvdum@0
    90
				$bbcode_helpline = utf8_normalize_nfc(request_var('bbcode_helpline', '', true));
indvdum@0
    91
			break;
indvdum@0
    92
		}
indvdum@0
    93
indvdum@0
    94
		// Do major work
indvdum@0
    95
		switch ($action)
indvdum@0
    96
		{
indvdum@0
    97
			case 'edit':
indvdum@0
    98
			case 'add':
indvdum@0
    99
indvdum@0
   100
				$template->assign_vars(array(
indvdum@0
   101
					'S_EDIT_BBCODE'		=> true,
indvdum@0
   102
					'U_BACK'			=> $this->u_action,
indvdum@0
   103
					'U_ACTION'			=> $this->u_action . '&amp;action=' . (($action == 'add') ? 'create' : 'modify') . (($bbcode_id) ? "&amp;bbcode=$bbcode_id" : ''),
indvdum@0
   104
indvdum@0
   105
					'L_BBCODE_USAGE_EXPLAIN'=> sprintf($user->lang['BBCODE_USAGE_EXPLAIN'], '<a href="#down">', '</a>'),
indvdum@0
   106
					'BBCODE_MATCH'			=> $bbcode_match,
indvdum@0
   107
					'BBCODE_TPL'			=> $bbcode_tpl,
indvdum@0
   108
					'BBCODE_HELPLINE'		=> $bbcode_helpline,
indvdum@0
   109
					'DISPLAY_ON_POSTING'	=> $display_on_posting)
indvdum@0
   110
				);
indvdum@0
   111
indvdum@0
   112
				foreach ($user->lang['tokens'] as $token => $token_explain)
indvdum@0
   113
				{
indvdum@0
   114
					$template->assign_block_vars('token', array(
indvdum@0
   115
						'TOKEN'		=> '{' . $token . '}',
indvdum@0
   116
						'EXPLAIN'	=> $token_explain)
indvdum@0
   117
					);
indvdum@0
   118
				}
indvdum@0
   119
indvdum@0
   120
				return;
indvdum@0
   121
indvdum@0
   122
			break;
indvdum@0
   123
indvdum@0
   124
			case 'modify':
indvdum@0
   125
			case 'create':
indvdum@0
   126
indvdum@0
   127
				$warn_text = preg_match('%<[^>]*\{text[\d]*\}[^>]*>%i', $bbcode_tpl);
indvdum@0
   128
				if (!$warn_text || confirm_box(true))
indvdum@0
   129
				{
indvdum@0
   130
					$data = $this->build_regexp($bbcode_match, $bbcode_tpl);
indvdum@0
   131
indvdum@0
   132
					// Make sure the user didn't pick a "bad" name for the BBCode tag.
indvdum@0
   133
					$hard_coded = array('code', 'quote', 'quote=', 'attachment', 'attachment=', 'b', 'i', 'url', 'url=', 'img', 'size', 'size=', 'color', 'color=', 'u', 'list', 'list=', 'email', 'email=', 'flash', 'flash=');
indvdum@0
   134
indvdum@0
   135
					if (($action == 'modify' && strtolower($data['bbcode_tag']) !== strtolower($row['bbcode_tag'])) || ($action == 'create'))
indvdum@0
   136
					{
indvdum@0
   137
						$sql = 'SELECT 1 as test
indvdum@0
   138
							FROM ' . BBCODES_TABLE . "
indvdum@0
   139
							WHERE LOWER(bbcode_tag) = '" . $db->sql_escape(strtolower($data['bbcode_tag'])) . "'";
indvdum@0
   140
						$result = $db->sql_query($sql);
indvdum@0
   141
						$info = $db->sql_fetchrow($result);
indvdum@0
   142
						$db->sql_freeresult($result);
indvdum@0
   143
indvdum@0
   144
						// Grab the end, interrogate the last closing tag
indvdum@0
   145
						if ($info['test'] === '1' || in_array(strtolower($data['bbcode_tag']), $hard_coded) || (preg_match('#\[/([^[]*)]$#', $bbcode_match, $regs) && in_array(strtolower($regs[1]), $hard_coded)))
indvdum@0
   146
						{
indvdum@0
   147
							trigger_error($user->lang['BBCODE_INVALID_TAG_NAME'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   148
						}
indvdum@0
   149
					}
indvdum@0
   150
indvdum@0
   151
					if (substr($data['bbcode_tag'], -1) === '=')
indvdum@0
   152
					{
indvdum@0
   153
						$test = substr($data['bbcode_tag'], 0, -1);
indvdum@0
   154
					}
indvdum@0
   155
					else
indvdum@0
   156
					{
indvdum@0
   157
						$test = $data['bbcode_tag'];
indvdum@0
   158
					}
indvdum@0
   159
indvdum@0
   160
					if (!preg_match('%\\[' . $test . '[^]]*].*?\\[/' . $test . ']%s', $bbcode_match))
indvdum@0
   161
					{
indvdum@0
   162
						trigger_error($user->lang['BBCODE_OPEN_ENDED_TAG'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   163
					}
indvdum@0
   164
indvdum@0
   165
					if (strlen($data['bbcode_tag']) > 16)
indvdum@0
   166
					{
indvdum@0
   167
						trigger_error($user->lang['BBCODE_TAG_TOO_LONG'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   168
					}
indvdum@0
   169
indvdum@0
   170
					if (strlen($bbcode_match) > 4000)
indvdum@0
   171
					{
indvdum@0
   172
						trigger_error($user->lang['BBCODE_TAG_DEF_TOO_LONG'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   173
					}
indvdum@0
   174
indvdum@0
   175
indvdum@0
   176
					if (strlen($bbcode_helpline) > 255)
indvdum@0
   177
					{
indvdum@0
   178
						trigger_error($user->lang['BBCODE_HELPLINE_TOO_LONG'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   179
					}
indvdum@0
   180
indvdum@0
   181
					$sql_ary = array(
indvdum@0
   182
						'bbcode_tag'				=> $data['bbcode_tag'],
indvdum@0
   183
						'bbcode_match'				=> $bbcode_match,
indvdum@0
   184
						'bbcode_tpl'				=> $bbcode_tpl,
indvdum@0
   185
						'display_on_posting'		=> $display_on_posting,
indvdum@0
   186
						'bbcode_helpline'			=> $bbcode_helpline,
indvdum@0
   187
						'first_pass_match'			=> $data['first_pass_match'],
indvdum@0
   188
						'first_pass_replace'		=> $data['first_pass_replace'],
indvdum@0
   189
						'second_pass_match'			=> $data['second_pass_match'],
indvdum@0
   190
						'second_pass_replace'		=> $data['second_pass_replace']
indvdum@0
   191
					);
indvdum@0
   192
indvdum@0
   193
					if ($action == 'create')
indvdum@0
   194
					{
indvdum@0
   195
						$sql = 'SELECT MAX(bbcode_id) as max_bbcode_id
indvdum@0
   196
							FROM ' . BBCODES_TABLE;
indvdum@0
   197
						$result = $db->sql_query($sql);
indvdum@0
   198
						$row = $db->sql_fetchrow($result);
indvdum@0
   199
						$db->sql_freeresult($result);
indvdum@0
   200
indvdum@0
   201
						if ($row)
indvdum@0
   202
						{
indvdum@0
   203
							$bbcode_id = $row['max_bbcode_id'] + 1;
indvdum@0
   204
indvdum@0
   205
							// Make sure it is greater than the core bbcode ids...
indvdum@0
   206
							if ($bbcode_id <= NUM_CORE_BBCODES)
indvdum@0
   207
							{
indvdum@0
   208
								$bbcode_id = NUM_CORE_BBCODES + 1;
indvdum@0
   209
							}
indvdum@0
   210
						}
indvdum@0
   211
						else
indvdum@0
   212
						{
indvdum@0
   213
							$bbcode_id = NUM_CORE_BBCODES + 1;
indvdum@0
   214
						}
indvdum@0
   215
indvdum@0
   216
						if ($bbcode_id > BBCODE_LIMIT)
indvdum@0
   217
						{
indvdum@0
   218
							trigger_error($user->lang['TOO_MANY_BBCODES'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   219
						}
indvdum@0
   220
indvdum@0
   221
						$sql_ary['bbcode_id'] = (int) $bbcode_id;
indvdum@0
   222
indvdum@0
   223
						$db->sql_query('INSERT INTO ' . BBCODES_TABLE . $db->sql_build_array('INSERT', $sql_ary));
indvdum@0
   224
						$cache->destroy('sql', BBCODES_TABLE);
indvdum@0
   225
indvdum@0
   226
						$lang = 'BBCODE_ADDED';
indvdum@0
   227
						$log_action = 'LOG_BBCODE_ADD';
indvdum@0
   228
					}
indvdum@0
   229
					else
indvdum@0
   230
					{
indvdum@0
   231
						$sql = 'UPDATE ' . BBCODES_TABLE . '
indvdum@0
   232
							SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
indvdum@0
   233
							WHERE bbcode_id = ' . $bbcode_id;
indvdum@0
   234
						$db->sql_query($sql);
indvdum@0
   235
						$cache->destroy('sql', BBCODES_TABLE);
indvdum@0
   236
indvdum@0
   237
						$lang = 'BBCODE_EDITED';
indvdum@0
   238
						$log_action = 'LOG_BBCODE_EDIT';
indvdum@0
   239
					}
indvdum@0
   240
indvdum@0
   241
					add_log('admin', $log_action, $data['bbcode_tag']);
indvdum@0
   242
indvdum@0
   243
					trigger_error($user->lang[$lang] . adm_back_link($this->u_action));
indvdum@0
   244
				}
indvdum@0
   245
				else
indvdum@0
   246
				{
indvdum@0
   247
					confirm_box(false, $user->lang['BBCODE_DANGER'], build_hidden_fields(array(
indvdum@0
   248
						'action'				=> $action,
indvdum@0
   249
						'bbcode'				=> $bbcode_id,
indvdum@0
   250
						'bbcode_match'			=> $bbcode_match,
indvdum@0
   251
						'bbcode_tpl'			=> htmlspecialchars($bbcode_tpl),
indvdum@0
   252
						'bbcode_helpline'		=> $bbcode_helpline,
indvdum@0
   253
						'display_on_posting'	=> $display_on_posting,
indvdum@0
   254
						))
indvdum@0
   255
					, 'confirm_bbcode.html');
indvdum@0
   256
				}
indvdum@0
   257
indvdum@0
   258
			break;
indvdum@0
   259
indvdum@0
   260
			case 'delete':
indvdum@0
   261
indvdum@0
   262
				$sql = 'SELECT bbcode_tag
indvdum@0
   263
					FROM ' . BBCODES_TABLE . "
indvdum@0
   264
					WHERE bbcode_id = $bbcode_id";
indvdum@0
   265
				$result = $db->sql_query($sql);
indvdum@0
   266
				$row = $db->sql_fetchrow($result);
indvdum@0
   267
				$db->sql_freeresult($result);
indvdum@0
   268
indvdum@0
   269
				if ($row)
indvdum@0
   270
				{
indvdum@0
   271
					if (confirm_box(true))
indvdum@0
   272
					{
indvdum@0
   273
						$db->sql_query('DELETE FROM ' . BBCODES_TABLE . " WHERE bbcode_id = $bbcode_id");
indvdum@0
   274
						$cache->destroy('sql', BBCODES_TABLE);
indvdum@0
   275
						add_log('admin', 'LOG_BBCODE_DELETE', $row['bbcode_tag']);
indvdum@0
   276
					}
indvdum@0
   277
					else
indvdum@0
   278
					{
indvdum@0
   279
						confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
indvdum@0
   280
							'bbcode'	=> $bbcode_id,
indvdum@0
   281
							'i'			=> $id,
indvdum@0
   282
							'mode'		=> $mode,
indvdum@0
   283
							'action'	=> $action))
indvdum@0
   284
						);
indvdum@0
   285
					}
indvdum@0
   286
				}
indvdum@0
   287
indvdum@0
   288
			break;
indvdum@0
   289
		}
indvdum@0
   290
indvdum@0
   291
		$template->assign_vars(array(
indvdum@0
   292
			'U_ACTION'		=> $this->u_action . '&amp;action=add')
indvdum@0
   293
		);
indvdum@0
   294
indvdum@0
   295
		$sql = 'SELECT *
indvdum@0
   296
			FROM ' . BBCODES_TABLE . '
indvdum@0
   297
			ORDER BY bbcode_tag';
indvdum@0
   298
		$result = $db->sql_query($sql);
indvdum@0
   299
indvdum@0
   300
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   301
		{
indvdum@0
   302
			$template->assign_block_vars('bbcodes', array(
indvdum@0
   303
				'BBCODE_TAG'		=> $row['bbcode_tag'],
indvdum@0
   304
				'U_EDIT'			=> $this->u_action . '&amp;action=edit&amp;bbcode=' . $row['bbcode_id'],
indvdum@0
   305
				'U_DELETE'			=> $this->u_action . '&amp;action=delete&amp;bbcode=' . $row['bbcode_id'])
indvdum@0
   306
			);
indvdum@0
   307
		}
indvdum@0
   308
		$db->sql_freeresult($result);
indvdum@0
   309
	}
indvdum@0
   310
indvdum@0
   311
	/*
indvdum@0
   312
	* Build regular expression for custom bbcode
indvdum@0
   313
	*/
indvdum@0
   314
	function build_regexp(&$bbcode_match, &$bbcode_tpl)
indvdum@0
   315
	{
indvdum@0
   316
		$bbcode_match = trim($bbcode_match);
indvdum@0
   317
		$bbcode_tpl = trim($bbcode_tpl);
indvdum@0
   318
		$utf8 = strpos($bbcode_match, 'INTTEXT') !== false;
indvdum@0
   319
indvdum@0
   320
		// make sure we have utf8 support
indvdum@0
   321
		$utf8_pcre_properties = false;
indvdum@0
   322
		if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>=')))
indvdum@0
   323
		{
indvdum@0
   324
			// 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
indvdum@0
   325
			if (@preg_match('/\p{L}/u', 'a') !== false)
indvdum@0
   326
			{
indvdum@0
   327
				$utf8_pcre_properties = true;
indvdum@0
   328
			}
indvdum@0
   329
		}
indvdum@0
   330
indvdum@0
   331
		$fp_match = preg_quote($bbcode_match, '!');
indvdum@0
   332
		$fp_replace = preg_replace('#^\[(.*?)\]#', '[$1:$uid]', $bbcode_match);
indvdum@0
   333
		$fp_replace = preg_replace('#\[/(.*?)\]$#', '[/$1:$uid]', $fp_replace);
indvdum@0
   334
indvdum@0
   335
		$sp_match = preg_quote($bbcode_match, '!');
indvdum@0
   336
		$sp_match = preg_replace('#^\\\\\[(.*?)\\\\\]#', '\[$1:$uid\]', $sp_match);
indvdum@0
   337
		$sp_match = preg_replace('#\\\\\[/(.*?)\\\\\]$#', '\[/$1:$uid\]', $sp_match);
indvdum@0
   338
		$sp_replace = $bbcode_tpl;
indvdum@0
   339
indvdum@0
   340
		// @todo Make sure to change this too if something changed in message parsing
indvdum@0
   341
		$tokens = array(
indvdum@0
   342
			'URL'	 => array(
indvdum@0
   343
				'!(?:(' . 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')"
indvdum@0
   344
			),
indvdum@0
   345
			'LOCAL_URL'	 => array(
indvdum@0
   346
				'!(' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('relative_url')) . ')!e'	=>	"\$this->bbcode_specialchars('$1')"
indvdum@0
   347
			),
indvdum@0
   348
			'EMAIL' => array(
indvdum@0
   349
				'!(' . get_preg_expression('email') . ')!ie'	=>	"\$this->bbcode_specialchars('$1')"
indvdum@0
   350
			),
indvdum@0
   351
			'TEXT' => array(
indvdum@0
   352
				'!(.*?)!es'	 =>	"str_replace(array(\"\\r\\n\", '\\\"', '\\'', '(', ')'), array(\"\\n\", '\"', '&#39;', '&#40;', '&#41;'), trim('\$1'))"
indvdum@0
   353
			),
indvdum@0
   354
			'SIMPLETEXT' => array(
indvdum@0
   355
				'!([a-zA-Z0-9-+.,_ ]+)!'	 =>	"$1"
indvdum@0
   356
			),
indvdum@0
   357
			'INTTEXT' => array(
indvdum@0
   358
				($utf8_pcre_properties) ? '!([\p{L}\p{N}\-+,_. ]+)!u' : '!([a-zA-Z0-9\-+,_. ]+)!u'	 =>	"$1"
indvdum@0
   359
			),
indvdum@0
   360
			'IDENTIFIER' => array(
indvdum@0
   361
				'!([a-zA-Z0-9-_]+)!'	 =>	"$1"
indvdum@0
   362
			),
indvdum@0
   363
			'COLOR' => array(
indvdum@0
   364
				'!([a-z]+|#[0-9abcdef]+)!i'	=>	'$1'
indvdum@0
   365
			),
indvdum@0
   366
			'NUMBER' => array(
indvdum@0
   367
				'!([0-9]+)!'	=>	'$1'
indvdum@0
   368
			)
indvdum@0
   369
		);
indvdum@0
   370
indvdum@0
   371
		$sp_tokens = array(
indvdum@0
   372
			'URL'	 => '(?i)((?:' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('url')) . ')|(?:' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('www_url')) . '))(?-i)',
indvdum@0
   373
			'LOCAL_URL'	 => '(?i)(' . str_replace(array('!', '\#'), array('\!', '#'), get_preg_expression('relative_url')) . ')(?-i)',
indvdum@0
   374
			'EMAIL' => '(' . get_preg_expression('email') . ')',
indvdum@0
   375
			'TEXT' => '(.*?)',
indvdum@0
   376
			'SIMPLETEXT' => '([a-zA-Z0-9-+.,_ ]+)',
indvdum@0
   377
			'INTTEXT' => ($utf8_pcre_properties) ? '([\p{L}\p{N}\-+,_. ]+)' : '([a-zA-Z0-9\-+,_. ]+)',
indvdum@0
   378
			'IDENTIFIER' => '([a-zA-Z0-9-_]+)',
indvdum@0
   379
			'COLOR' => '([a-zA-Z]+|#[0-9abcdefABCDEF]+)',
indvdum@0
   380
			'NUMBER' => '([0-9]+)',
indvdum@0
   381
		);
indvdum@0
   382
indvdum@0
   383
		$pad = 0;
indvdum@0
   384
		$modifiers = 'i';
indvdum@0
   385
		$modifiers .= ($utf8 && $utf8_pcre_properties) ? 'u' : '';
indvdum@0
   386
indvdum@0
   387
		if (preg_match_all('/\{(' . implode('|', array_keys($tokens)) . ')[0-9]*\}/i', $bbcode_match, $m))
indvdum@0
   388
		{
indvdum@0
   389
			foreach ($m[0] as $n => $token)
indvdum@0
   390
			{
indvdum@0
   391
				$token_type = $m[1][$n];
indvdum@0
   392
indvdum@0
   393
				reset($tokens[strtoupper($token_type)]);
indvdum@0
   394
				list($match, $replace) = each($tokens[strtoupper($token_type)]);
indvdum@0
   395
indvdum@0
   396
				// Pad backreference numbers from tokens
indvdum@0
   397
				if (preg_match_all('/(?<!\\\\)\$([0-9]+)/', $replace, $repad))
indvdum@0
   398
				{
indvdum@0
   399
					$repad = $pad + sizeof(array_unique($repad[0]));
indvdum@0
   400
					$replace = preg_replace('/(?<!\\\\)\$([0-9]+)/e', "'\${' . (\$1 + \$pad) . '}'", $replace);
indvdum@0
   401
					$pad = $repad;
indvdum@0
   402
				}
indvdum@0
   403
indvdum@0
   404
				// Obtain pattern modifiers to use and alter the regex accordingly
indvdum@0
   405
				$regex = preg_replace('/!(.*)!([a-z]*)/', '$1', $match);
indvdum@0
   406
				$regex_modifiers = preg_replace('/!(.*)!([a-z]*)/', '$2', $match);
indvdum@0
   407
indvdum@0
   408
				for ($i = 0, $size = strlen($regex_modifiers); $i < $size; ++$i)
indvdum@0
   409
				{
indvdum@0
   410
					if (strpos($modifiers, $regex_modifiers[$i]) === false)
indvdum@0
   411
					{
indvdum@0
   412
						$modifiers .= $regex_modifiers[$i];
indvdum@0
   413
indvdum@0
   414
						if ($regex_modifiers[$i] == 'e')
indvdum@0
   415
						{
indvdum@0
   416
							$fp_replace = "'" . str_replace("'", "\\'", $fp_replace) . "'";
indvdum@0
   417
						}
indvdum@0
   418
					}
indvdum@0
   419
indvdum@0
   420
					if ($regex_modifiers[$i] == 'e')
indvdum@0
   421
					{
indvdum@0
   422
						$replace = "'.$replace.'";
indvdum@0
   423
					}
indvdum@0
   424
				}
indvdum@0
   425
indvdum@0
   426
				$fp_match = str_replace(preg_quote($token, '!'), $regex, $fp_match);
indvdum@0
   427
				$fp_replace = str_replace($token, $replace, $fp_replace);
indvdum@0
   428
indvdum@0
   429
				$sp_match = str_replace(preg_quote($token, '!'), $sp_tokens[$token_type], $sp_match);
indvdum@0
   430
				$sp_replace = str_replace($token, '${' . ($n + 1) . '}', $sp_replace);
indvdum@0
   431
			}
indvdum@0
   432
indvdum@0
   433
			$fp_match = '!' . $fp_match . '!' . $modifiers;
indvdum@0
   434
			$sp_match = '!' . $sp_match . '!s' . (($utf8) ? 'u' : '');
indvdum@0
   435
indvdum@0
   436
			if (strpos($fp_match, 'e') !== false)
indvdum@0
   437
			{
indvdum@0
   438
				$fp_replace = str_replace("'.'", '', $fp_replace);
indvdum@0
   439
				$fp_replace = str_replace(".''.", '.', $fp_replace);
indvdum@0
   440
			}
indvdum@0
   441
		}
indvdum@0
   442
		else
indvdum@0
   443
		{
indvdum@0
   444
			// No replacement is present, no need for a second-pass pattern replacement
indvdum@0
   445
			// A simple str_replace will suffice
indvdum@0
   446
			$fp_match = '!' . $fp_match . '!' . $modifiers;
indvdum@0
   447
			$sp_match = $fp_replace;
indvdum@0
   448
			$sp_replace = '';
indvdum@0
   449
		}
indvdum@0
   450
indvdum@0
   451
		// Lowercase tags
indvdum@0
   452
		$bbcode_tag = preg_replace('/.*?\[([a-z0-9_-]+=?).*/i', '$1', $bbcode_match);
indvdum@0
   453
		$bbcode_search = preg_replace('/.*?\[([a-z0-9_-]+)=?.*/i', '$1', $bbcode_match);
indvdum@0
   454
indvdum@0
   455
		if (!preg_match('/^[a-zA-Z0-9_-]+=?$/', $bbcode_tag))
indvdum@0
   456
		{
indvdum@0
   457
			global $user;
indvdum@0
   458
			trigger_error($user->lang['BBCODE_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   459
		}
indvdum@0
   460
indvdum@0
   461
		$fp_match = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $fp_match);
indvdum@0
   462
		$fp_replace = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $fp_replace);
indvdum@0
   463
		$sp_match = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $sp_match);
indvdum@0
   464
		$sp_replace = preg_replace('#\[/?' . $bbcode_search . '#ie', "strtolower('\$0')", $sp_replace);
indvdum@0
   465
indvdum@0
   466
		return array(
indvdum@0
   467
			'bbcode_tag'				=> $bbcode_tag,
indvdum@0
   468
			'first_pass_match'			=> $fp_match,
indvdum@0
   469
			'first_pass_replace'		=> $fp_replace,
indvdum@0
   470
			'second_pass_match'			=> $sp_match,
indvdum@0
   471
			'second_pass_replace'		=> $sp_replace
indvdum@0
   472
		);
indvdum@0
   473
	}
indvdum@0
   474
}
indvdum@0
   475
indvdum@0
   476
?>