bbcode_uid = $uid;
$this->bbcode_bitfield = $bitfield;
//$this->bbcode_cache_init();
}
function bbcode_second_pass(&$message, $bbcode_uid = '', $bbcode_bitfield = '')
{
if (empty($this->bbcode_cache))
{
$this->bbcode_cache_init();
}
if ($bbcode_uid)
{
$this->bbcode_uid = $bbcode_uid;
}
if ($bbcode_bitfield)
{
$this->bbcode_bitfield = $bbcode_bitfield;
}
$str = array('search' => array(), 'replace' => array());
$preg = array('search' => array(), 'replace' => array());
for ($bbcode_id = 0; $bbcode_id < 31; ++$bbcode_id)
{
if ($this->bbcode_bitfield & pow(2, $bbcode_id))
{
foreach ($this->bbcode_cache[$bbcode_id] as $type => $array)
{
foreach ($array as $search => $replace)
{
${$type}['search'][] = str_replace('$uid', $this->bbcode_uid, $search);
${$type}['replace'][] = $replace;
}
}
}
}
$message = str_replace($str['search'], $str['replace'], $message);
$message = preg_replace($preg['search'], $preg['replace'], $message);
return $message;
}
//
// bbcode_cache_init()
//
// requires: $this->bbcode_bitfield
// sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
//
function bbcode_cache_init()
{
$sql = '';
$bbcode_ids = array();
// TODO: only load needed bbcodes
for ($bbcode_id = 0; $bbcode_id < 10; ++$bbcode_id)
{
$bbcode_ids[] = $bbcode_id;
//
// WARNING: hardcoded values. it assumes that bbcodes with bbcode_id > 10 are user-defined bbcodes
// and it has to be specified which bbcodes need the template to be loaded
//
if ($bbcode_id > 10)
{
$sql .= $bbcode_id . ',';
}
elseif (in_array($bbcode_id, array(0, 5, 6, 8, 9)))
{
$load_template = TRUE;
}
}
if (!empty($load_template))
{
global $template, $user;
$tpl_filename = $template->make_filename('bbcode.html');
if (!$fp = @fopen($tpl_filename, 'rb'))
{
trigger_error('Could not load bbcode template');
}
$tpl = fread($fp, filesize($tpl_filename));
@fclose($fp);
// replace \ with \\ and then ' with \'.
$tpl = str_replace('\\', '\\\\', $tpl);
$tpl = str_replace("'", "\'", $tpl);
// strip newlines.
$tpl = str_replace("\n", '', $tpl);
// Turn template blocks into PHP assignment statements for the values of $bbcode_tpl..
$tpl = preg_replace('#(.*?)#', "\n" . "\$this->bbcode_tpl['\\1'] = trim('\\2');", $tpl);
$this->bbcode_tpl = array();
eval($tpl);
$this->bbcode_tpl['quote_open'] = str_replace('{L_QUOTE}', $user->lang['QUOTE'], $this->bbcode_tpl['quote_open']);
$this->bbcode_tpl['quote_username_open'] = str_replace('{L_QUOTE}', $user->lang['QUOTE'], $this->bbcode_tpl['quote_username_open']);
$this->bbcode_tpl['quote_username_open'] = str_replace('{L_WROTE}', $user->lang['WROTE'], $this->bbcode_tpl['quote_username_open']);
$this->bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $this->bbcode_tpl['quote_username_open']);
$this->bbcode_tpl['code_open'] = str_replace('{L_CODE}', $user->lang['CODE'], $this->bbcode_tpl['code_open']);
}
if ($sql)
{
global $db;
$rowset = array();
$sql = 'SELECT bbcode_id, second_pass_regexp, second_pass_replacement
FROM ' . BBCODES_TABLE . '
WHERE bbcode_id IN (' . substr($sql, 0, -1) . ')
ORDER BY bbcode_id';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$rowset[$row['bbcode_id']] = $row;
}
}
foreach ($bbcode_ids as $bbcode_id)
{
switch ($bbcode_id)
{
case 0:
$this->bbcode_cache[$bbcode_id] = array(
'str' => array(
'[quote:$uid]' => $this->bbcode_tpl['quote_open'],
'[/quote:$uid]' => $this->bbcode_tpl['quote_close']
),
'preg' => array(
'#\[quote:$uid="(.*?)"\]#' => $this->bbcode_tpl['quote_username_open']
)
);
break;
case 1:
$this->bbcode_cache[$bbcode_id] = array(
'str' => array(
'[b:$uid]' => '',
'[/b:$uid]' => ''
)
);
break;
case 2:
$this->bbcode_cache[$bbcode_id] = array(
'str' => array(
'[i:$uid]' => '',
'[/i:$uid]' => ''
)
);
break;
case 3:
$this->bbcode_cache[$bbcode_id] = array(
'preg' => array(
'#\[url:$uid\](.*?)\[/url:$uid\]#s' => '\1',
'#\[url=(.*?):$uid\](.*?)\[/url:$uid\]#s' => '\2'
)
);
break;
case 4:
$this->bbcode_cache[$bbcode_id] = array(
'preg' => array(
'#\[img:$uid\](.*?)\[/img:$uid\]#' => ''
)
);
break;
case 5:
$this->bbcode_cache[$bbcode_id] = array(
'preg' => array(
'#\[size=(.*?):$uid\](.*?)\[/size:$uid\]#' => '\2'
)
);
break;
case 6:
$this->bbcode_cache[$bbcode_id] = array(
'preg' => array(
'#\[color=(.*?):$uid\](.*?)\[/color:$uid\]#' => '\2'
)
);
break;
case 7:
$this->bbcode_cache[$bbcode_id] = array(
'str' => array(
'[u:$uid]' => '',
'[/u:$uid]' => ''
)
);
break;
case 8:
$this->bbcode_cache[$bbcode_id] = array(
'preg' => array(
'#\[code:$uid\](.*?)\[/code:$uid\]#ise' => '$this->bbcode_second_pass_code("\1")'
)
);
break;
case 9:
$this->bbcode_cache[$bbcode_id] = array(
'str' => array(
'[list:$uid]' => '