<?php
/**********************************************************************************
* Subs.php *
***********************************************************************************
* SMF: Simple Machines Forum *
* Open-Source Project Inspired by Zef Hemel ([email protected]) *
* =============================================================================== *
* Software Version: SMF 1.1.11 *
* Software by: Simple Machines (http://www.simplemachines.org) *
* Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
* 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
* Support, News, Updates at: http://www.simplemachines.org *
***********************************************************************************
* This program is free software; you may redistribute it and/or modify it under *
* the terms of the provided license as published by Simple Machines LLC. *
* *
* This program is distributed in the hope that it is and will be useful, but *
* WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* See the "license.txt" file for details of the Simple Machines license. *
* The latest version can always be found at http://www.simplemachines.org. *
**********************************************************************************/
if (!defined('SMF'))
die('Hacking attempt...');
/* This file has all the main functions in it that relate to, well,
everything. It provides all of the following functions:
resource db_query(string database_query, string __FILE__, int __LINE__)
- should always be used in place of mysql_query.
- executes a query string, and implements needed error checking.
- always use the magic constants __FILE__ and __LINE__.
- returns a MySQL result resource, to be freed with mysql_free_result.
int db_affected_rows()
- should always be used in place of db_insert_id.
- returns the number of affected rows by the most recently executed
query.
- handles the current connection so the forum with other connections
active at the same time.
int db_insert_id()
- should always be used in place of mysql_insert_id.
- returns the most recently generated auto_increment column.
- handles the current connection so the forum with other connections
active at the same time.
void updateStats(string statistic, string condition = '1')
- statistic can be 'member', 'message', 'topic', 'calendar', or
'postgroups'.
- parameter1 and parameter2 are optional, and are used to update only
those stats that need updating.
- the 'member' statistic updates the latest member, the total member
count, and the number of unapproved members.
- 'member' also only counts approved members when approval is on, but
is much more efficient with it off.
- updating 'message' changes the total number of messages, and the
highest message id by ID_MSG - which can be parameters 1 and 2,
respectively.
- 'topic' updates the total number of topics, or if parameter1 is true
simply increments them.
- the 'calendar' statistic updates the cache of the calendar
information for a day before and after today.
- the 'postgroups' case updates those members who match condition's
post-based membergroups in the database (restricted by parameter1).
void updateMemberData(int ID_MEMBER, array data)
- updates the columns in the members table.
- ID_MEMBER is either an int or an array of ints to be updated.
- data is an associative array of the columns to be updated and their
respective values.
- any string values updated should be quoted and slashed.
- the value of any column can be '+' or '-', which mean 'increment'
and decrement, respectively.
- if the member's post number is updated, updates their post groups.
- this function should be used whenever member data needs to be
updated in place of an UPDATE query.
void updateSettings(array changeArray, use_update = false)
- updates both the settings table and $modSettings array.
- all of changeArray's indexes and values are assumed to have escaped
apostrophes (')!
- if a variable is already set to what you want to change it to, that
variable will be skipped over; it would be unnecessary to reset.
- if use_update is true, UPDATEs will be used instead of REPALCE.
- when use_update is true, the value can be true or false to increment
or decrement it, respectively.
string constructPageIndex(string base_url, int &start, int max_value,
int num_per_page, bool compact_start = false)
- builds the page list, e.g. 1 ... 6 7 [8] 9 10 ... 15.
- compact_start caused it to use "url.page" instead of
"url;start=page".
- handles any wireless settings (adding special things to URLs.)
- very importantly, cleans up the start value passed, and forces it to
be a multiple of num_per_page.
- also checks that start is not more than max_value.
- base_url should be the URL without any start parameter on it.
- uses the compactTopicPagesEnable and compactTopicPagesContiguous
settings to decide how to display the menu.
- an example is available near the function definition.
string comma_format(float number, int override_decimal_count = false)
- formats a number to display in the style of the admins' choosing.
- uses the format of number_format to decide how to format the number.
- for example, it might display "1 234,50".
- caches the formatting data from the setting for optimization.
string timeformat(int time, bool show_today = true)
- returns a pretty formated version of time based on the user's format
in $user_info['time_format'].
- applies any necessary time offsets to the timestamp.
- if todayMod is set and show_today was not not specified or true, an
alternate format string is used to show the date with something to
show it is "today" or "yesterday".
- performs localization (more than just strftime would do alone.)
string un_htmlspecialchars(string text)
- removes the base entities (<, ", etc.) from text.
- should be used instead of html_entity_decode for PHP version
compatibility reasons.
- additionally converts and '.
- returns the string without entities.
string shorten_subject(string regular_subject, int length)
- shortens a subject so that it is either shorter than length, or that
length plus an ellipsis.
- respects internationalization characters and entities as one character.
- avoids trailing entities.
- returns the shortened string.
int forum_time(bool use_user_offset = true)
- returns the current time with offsets.
- always applies the offset in the time_offset setting.
- if use_user_offset is true, applies the user's offset as well.
- returns seconds since the unix epoch.
array permute(array input)
- calculates all the possible permutations (orders) of array.
- should not be called on huge arrays (bugger than like 10 elements.)
- returns an array containing each permutation.
string doUBBC(string message, bool enableSmileys = true)
- passes through parse_bbc(message, enableSmileys).
- available for older implementations.
string parse_bbc(string message, bool smileys = true, string cache_id = '')
- this very hefty function parses bbc in message.
- only parses bbc tags which are not disabled in disabledBBC.
- also handles basic HTML, if enablePostHTML is on.
- caches the from/to replace regular expressions so as not to reload
them every time a string is parsed.
- only parses smileys if smileys is true.
- does nothing if the enableBBC setting is off.
- applies the fixLongWords magic if the setting is set to on.
- uses the cache_id as a unique identifier to facilitate any caching
it may do.
- returns the modified message.
void parsesmileys(string &message)
- the smiley parsing function which makes pretty faces appear :).
- if custom smiley sets are turned off by smiley_enable, the default
set of smileys will be used.
- these are specifically not parsed in code tags [url=mailto:[email protected]]
- caches the smileys from the database or array in memory.
- doesn't return anything, but rather modifies message directly.
string highlight_php_code(string code)
- Uses PHP's highlight_code() to highlight PHP syntax
- does special handling to keep the tabs in the code available.
- used to parse PHP code from inside [code] and [php] tags.
- returns the code with highlighted HTML.
void writeLog(bool force = false)
// !!!
void redirectexit(string setLocation = '', bool use_refresh = false)
// !!!
void obExit(bool do_header = true, bool do_footer = do_header)
// !!!
void adminIndex($area)
// !!!
int logAction($action, $extra = array())
// !!!
void trackStats($stats = array())
- caches statistics changes, and flushes them if you pass nothing.
- if '+' is used as a value, it will be incremented.
- does not actually commit the changes until the end of the page view.
- depends on the trackStats setting.
void spamProtection(string error_type)
- attempts to protect from spammed messages and the like.
- takes a $txt index. (not an actual string.)
- depends on the spamWaitTime setting.
array url_image_size(string url)
- uses getimagesize() to determine the size of a file.
- attempts to connect to the server first so it won't time out.
- returns false on failure, otherwise the output of getimagesize().
void determineTopicClass(array &topic_context)
// !!!
void setupThemeContext()
// !!!
void template_rawdata()
// !!!
void template_header()
// !!!
void theme_copyright(bool get_it = false)
// !!!
void template_footer()
// !!!
void db_debug_junk()
// !!!
void getAttachmentFilename(string filename, int ID_ATTACH, bool new = true)
// !!!
string host_from_ip(string ip_address)
// !!!
string create_button(string filename, string alt, string label, bool custom = '')
// !!!
*/
// Do a query. Takes care of errors too.
function db_query($db_string, $file, $line)
{
global $db_cache, $db_count, $db_connection, $db_show_debug, $modSettings;
// One more query....
$db_count = !isset($db_count) ? 1 : $db_count + 1;
// Debugging.
if (isset($db_show_debug) && $db_show_debug === true)
{
// Initialize $db_cache if not already initialized.
if (!isset($db_cache))
$db_cache = array();
if (!empty($_SESSION['debug_redirect']))
{
$db_cache = array_merge($_SESSION['debug_redirect'], $db_cache);
$db_count = count($db_cache) + 1;
$_SESSION['debug_redirect'] = array();
}
$db_cache[$db_count]['q'] = $db_string;
$db_cache[$db_count]['f'] = $file;
$db_cache[$db_count]['l'] = $line;
$st = microtime();
}
// First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over.
if (empty($modSettings['disableQueryCheck']))
{
$clean = '';
$old_pos = 0;
$pos = -1;
while (true)
{
$pos = strpos($db_string, '\'', $pos + 1);
if ($pos === false)
break;
$clean .= substr($db_string, $old_pos, $pos - $old_pos);
while (true)
{
$pos1 = strpos($db_string, '\'', $pos + 1);
$pos2 = strpos($db_string, '\\', $pos + 1);
if ($pos1 === false)
break;
elseif ($pos2 == false || $pos2 > $pos1)
{
$pos = $pos1;
break;
}
$pos = $pos2 + 1;
}
$clean .= ' %s ';
$old_pos = $pos + 1;
}
$clean .= substr($db_string, $old_pos);
$clean = trim(strtolower(preg_replace(array('~\s+~s', '~/\*!40001 SQL_NO_CACHE \*/~', '~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~'), array(' ', '', ''), $clean)));
// We don't use UNION in SMF, at least so far. But it's useful for injections.
if (strpos($clean, 'union') !== false && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0)
$fail = true;
// Comments? We don't use comments in our queries, we leave 'em outside!
elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
$fail = true;
// Trying to change passwords, slow us down, or something?
elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0)
$fail = true;
elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
$fail = true;
// Sub selects? We don't use those either.
elseif (preg_match('~\([^)]*?select~s', $clean) != 0)
$fail = true;
if (!empty($fail))
{
log_error('Hacking attempt...' . "\n" . $db_string, $file, $line);
fatal_error('Hacking attempt...', false);
}
}
$ret = mysql_query($db_string, $db_connection);
if ($ret === false && $file !== false)
$ret = db_error($db_string, $file, $line);
// Debugging.
if (isset($db_show_debug) && $db_show_debug === true)
$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
return $ret;
}
function db_affected_rows()
{
global $db_connection;
return mysql_affected_rows($db_connection);
}
function db_insert_id()
{
global $db_connection;
return mysql_insert_id($db_connection);
}
// Update some basic statistics...
function updateStats($type, $parameter1 = null, $parameter2 = null)
{
global $db_prefix, $sourcedir, $modSettings;
switch ($type)
{
case 'member':
$changes = array(
'memberlist_updated' => time(),
);
// Are we using registration approval?
if (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 2)
{
// Update the latest activated member (highest ID_MEMBER) and count.
$result = db_query("
SELECT COUNT(*), MAX(ID_MEMBER)
FROM {$db_prefix}members
WHERE is_activated = 1", __FILE__, __LINE__);
list ($changes['totalMembers'], $changes['latestMember']) = mysql_fetch_row($result);
mysql_free_result($result);
// Get the latest activated member's display name.
$result = db_query("
SELECT realName
FROM {$db_prefix}members
WHERE ID_MEMBER = " . (int) $changes['latestMember'] . "
LIMIT 1", __FILE__, __LINE__);
list ($changes['latestRealName']) = mysql_fetch_row($result);
mysql_free_result($result);
// Update the amount of members awaiting approval - ignoring COPPA accounts, as you can't approve them until you get permission.
$result = db_query("
SELECT COUNT(*)
FROM {$db_prefix}members
WHERE is_activated IN (3, 4)", __FILE__, __LINE__);
list ($changes['unapprovedMembers']) = mysql_fetch_row($result);
mysql_free_result($result);
}
// If $parameter1 is a number, it's the new ID_MEMBER and #2 is the real name for a new registration.
elseif ($parameter1 !== null && $parameter1 !== false)
{
$changes['latestMember'] = $parameter1;
$changes['latestRealName'] = $parameter2;
updateSettings(array('totalMembers' => true), true);
}
// If $parameter1 is false, and approval is off, we need change nothing.
elseif ($parameter1 !== false)
{
// Update the latest member (highest ID_MEMBER) and count.
$result = db_query("
SELECT COUNT(*), MAX(ID_MEMBER)
FROM {$db_prefix}members", __FILE__, __LINE__);
list ($changes['totalMembers'], $changes['latestMember']) = mysql_fetch_row($result);
mysql_free_result($result);
// Get the latest member's display name.
$result = db_query("
SELECT realName
FROM {$db_prefix}members
WHERE ID_MEMBER = " . (int) $changes['latestMember'] . "
LIMIT 1", __FILE__, __LINE__);
list ($changes['latestRealName']) = mysql_fetch_row($result);
mysql_free_result($result);
}
updateSettings($changes);
break;
case 'message':
if ($parameter1 === true && $parameter2 !== null)
updateSettings(array('totalMessages' => true, 'maxMsgID' => $parameter2), true);
else
{
// SUM and MAX on a smaller table is better for InnoDB tables.
$result = db_query("
SELECT SUM(numPosts) AS totalMessages, MAX(ID_LAST_MSG) AS maxMsgID
FROM {$db_prefix}boards", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
updateSettings(array(
'totalMessages' => $row['totalMessages'],
'maxMsgID' => $row['maxMsgID'] === null ? 0 : $row['maxMsgID']
));
}
break;
case 'subject':
// Remove the previous subject (if any).
db_query("
DELETE FROM {$db_prefix}log_search_subjects
WHERE ID_TOPIC = " . (int) $parameter1, __FILE__, __LINE__);
// Insert the new subject.
if ($parameter2 !== null)
{
$parameter1 = (int) $parameter1;
$parameter2 = text2words($parameter2);
$inserts = array();
foreach ($parameter2 as $word)
$inserts[] = "'$word', $parameter1";
if (!empty($inserts))
db_query("
INSERT IGNORE INTO {$db_prefix}log_search_subjects
(word, ID_TOPIC)
VALUES (" . implode('),
(', array_unique($inserts)) . ")", __FILE__, __LINE__);
}
break;
case 'topic':
if ($parameter1 === true)
updateSettings(array('totalTopics' => true), true);
else
{
// Get the number of topics - a SUM is better for InnoDB tables.
// We also ignore the recycle bin here because there will probably be a bunch of one-post topics there.
$result = db_query("
SELECT SUM(numTopics) AS totalTopics
FROM {$db_prefix}boards" . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? "
WHERE ID_BOARD != $modSettings[recycle_board]" : ''), __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
updateSettings(array('totalTopics' => $row['totalTopics']));
}
break;
case 'calendar':
require_once($sourcedir . '/Calendar.php');
// Calculate the YYYY-MM-DD of the lowest and highest days.
$low_date = strftime('%Y-%m-%d', forum_time(false) - 24 * 3600);
$high_date = strftime('%Y-%m-%d', forum_time(false) + $modSettings['cal_days_for_index'] * 24 * 3600);
$holidays = calendarHolidayArray($low_date, $high_date);
$bday = calendarBirthdayArray($low_date, $high_date);
$events = calendarEventArray($low_date, $high_date, false);
// Cache the results in the settings.
updateSettings(array(
'cal_today_updated' => strftime('%Y%m%d', forum_time(false)),
'cal_today_holiday' => addslashes(serialize($holidays)),
'cal_today_birthday' => addslashes(serialize($bday)),
'cal_today_event' => addslashes(serialize($events))
));
break;
case 'postgroups':
// Parameter two is the updated columns: we should check to see if we base groups off any of these.
if ($parameter2 !== null && !in_array('posts', $parameter2))
return;
if (($postgroups = cache_get_data('updateStats:postgroups', 360)) == null)
{
// Fetch the postgroups!
$request = db_query("
SELECT ID_GROUP, minPosts
FROM {$db_prefix}membergroups
WHERE minPosts != -1", __FILE__, __LINE__);
$postgroups = array();
while ($row = mysql_fetch_assoc($request))
$postgroups[$row['ID_GROUP']] = $row['minPosts'];
mysql_free_result($request);
// Sort them this way because if it's done with MySQL it causes a filesort :(.
arsort($postgroups);
cache_put_data('updateStats:postgroups', $postgroups, 360);
}
// Oh great, they've screwed their post groups.
if (empty($postgroups))
return;
// Set all membergroups from most posts to least posts.
$conditions = '';
foreach ($postgroups as $id => $minPosts)
{
$conditions .= '
WHEN posts >= ' . $minPosts . (!empty($lastMin) ? ' AND posts <= ' . $lastMin : '') . ' THEN ' . $id;
$lastMin = $minPosts;
}
// A big fat CASE WHEN... END is faster than a zillion UPDATE's ;).
db_query("
UPDATE {$db_prefix}members
SET ID_POST_GROUP = CASE$conditions
ELSE 0
END" . ($parameter1 != null ? "
WHERE $parameter1" : ''), __FILE__, __LINE__);
break;
default:
trigger_error('updateStats(): Invalid statistic type \'' . $type . '\'', E_USER_NOTICE);
}
}
// Assumes the data has been slashed.
function updateMemberData($members, $data)
{
global $db_prefix, $modSettings, $ID_MEMBER, $user_info;
if (is_array($members))
$condition = 'ID_MEMBER IN (' . implode(', ', $members) . ')
LIMIT ' . count($members);
elseif ($members === null)
$condition = '1';
else
$condition = 'ID_MEMBER = ' . $members . '
LIMIT 1';
if (isset($modSettings['integrate_change_member_data']) && function_exists($modSettings['integrate_change_member_data']))
{
// Only a few member variables are really interesting for integration.
$integration_vars = array(
'memberName',
'realName',
'emailAddress',
'ID_GROUP',
'gender',
'birthdate',
'websiteTitle',
'websiteUrl',
'location',
'hideEmail',
'timeFormat',
'timeOffset',
'avatar',
'lngfile',
);
$vars_to_integrate = array_intersect($integration_vars, array_keys($data));
// Only proceed if there are any variables left to call the integration function.
if (count($vars_to_integrate) != 0)
{
// Fetch a list of memberNames if necessary
if ((!is_array($members) && $members === $ID_MEMBER) || (is_array($members) && count($members) == 1 && in_array($ID_MEMBER, $members)))
$memberNames = array($user_info['username']);
else
{
$memberNames = array();
$request = db_query("
SELECT memberName
FROM {$db_prefix}members
WHERE $condition", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
$memberNames[] = $row['memberName'];
mysql_free_result($request);
}
if (!empty($memberNames))
foreach ($vars_to_integrate as $var)
call_user_func($modSettings['integrate_change_member_data'], $memberNames, $var, stripslashes($data[$var]));
}
}
foreach ($data as $var => $val)
{
if ($val === '+')
$data[$var] = $var . ' + 1';
elseif ($val === '-')
$data[$var] = $var . ' - 1';
}
// Ensure posts, instantMessages, and unreadMessages never go below 0.
foreach(array('posts', 'instantMessages', 'unreadMessages') as $type)
if (isset($data[$type]) && preg_match('~^' . $type . ' - ([\d]+)~', $data[$type], $match) === 1)
$data[$type] = 'CASE WHEN ' . $type . ' <= ' . $match[1] . ' THEN 0 ELSE ' . $data[$type] . ' END';
$setString = '';
foreach ($data as $var => $val)
{
$setString .= "
$var = $val,";
}
db_query("
UPDATE {$db_prefix}members
SET" . substr($setString, 0, -1) . '
WHERE ' . $condition, __FILE__, __LINE__);
updateStats('postgroups', $condition, array_keys($data));
// Clear any caching?
if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2 && !empty($members))
{
if (!is_array($members))
$members = array($members);
foreach ($members as $member)
{
if ($modSettings['cache_enable'] == 3)
{
cache_put_data('member_data-profile-' . $member, null, 120);
cache_put_data('member_data-normal-' . $member, null, 120);
cache_put_data('member_data-minimal-' . $member, null, 120);
}
cache_put_data('user_settings-' . $member, null, 60);
}
}
}
// Updates the settings table as well as $modSettings... only does one at a time if $update is true.
// All input variables and values are assumed to have escaped apostrophes(')!
function updateSettings($changeArray, $update = false)
{
global $db_prefix, $modSettings;
if (empty($changeArray) || !is_array($changeArray))
return;
// In some cases, this may be better and faster, but for large sets we don't want so many UPDATEs.
if ($update)
{
foreach ($changeArray as $variable => $value)
{
db_query("
UPDATE {$db_prefix}settings
SET value = " . ($value === true ? 'value + 1' : ($value === false ? 'value - 1' : "'$value'")) . "
WHERE variable = '$variable'
LIMIT 1", __FILE__, __LINE__);
$modSettings[$variable] = $value === true ? $modSettings[$variable] + 1 : ($value === false ? $modSettings[$variable] - 1 : stripslashes($value));
}
// Clean out the cache and make sure the cobwebs are gone too.
cache_put_data('modSettings', null, 90);
return;
}
$replaceArray = array();
foreach ($changeArray as $variable => $value)
{
// Don't bother if it's already like that ;).
if (isset($modSettings[$variable]) && $modSettings[$variable] == stripslashes($value))
continue;
// If the variable isn't set, but would only be set to nothing'ness, then don't bother setting it.
elseif (!isset($modSettings[$variable]) && empty($value))
continue;
$replaceArray[] = "(SUBSTRING('$variable', 1, 255), SUBSTRING('$value', 1, 65534))";
$modSettings[$variable] = stripslashes($value);
}
if (empty($replaceArray))
return;
db_query("
REPLACE INTO {$db_prefix}settings
(variable, value)
VALUES " . implode(',
', $replaceArray), __FILE__, __LINE__);
// Kill the cache - it needs redoing now, but we won't bother ourselves with that here.
cache_put_data('modSettings', null, 90);
}
// Constructs a page list.
// $pageindex = constructPageIndex($scripturl . '?board=' . $board, $_REQUEST['start'], $num_messages, $maxindex, true);
function constructPageIndex($base_url, &$start, $max_value, $num_per_page, $flexible_start = false)
{
global $modSettings;
// Save whether $start was less than 0 or not.
$start_invalid = $start < 0;
// Make sure $start is a proper variable - not less than 0.
if ($start_invalid)
$start = 0;
// Not greater than the upper bound.
elseif ($start >= $max_value)
$start = max(0, (int) $max_value - (((int) $max_value % (int) $num_per_page) == 0 ? $num_per_page : ((int) $max_value % (int) $num_per_page)));
// And it has to be a multiple of $num_per_page!
else
$start = max(0, (int) $start - ((int) $start % (int) $num_per_page));
// Wireless will need the protocol on the URL somewhere.
if (WIRELESS)
$base_url .= ';' . WIRELESS_PROTOCOL;
$base_link = '<a class="navPages" href="' . ($flexible_start ? $base_url : strtr($base_url, array('%' => '%%')) . ';start=%d') . '">%s</a> ';
// Compact pages is off or on?
if (empty($modSettings['compactTopicPagesEnable']))
{
// Show the left arrow.
$pageindex = $start == 0 ? ' ' : sprintf($base_link, $start - $num_per_page, '«');
// Show all the pages.
$display_page = 1;
for ($counter = 0; $counter < $max_value; $counter += $num_per_page)
$pageindex .= $start == $counter && !$start_invalid ? '<b>' . $display_page++ . '</b> ' : sprintf($base_link, $counter, $display_page++);
// Show the right arrow.
$display_page = ($start + $num_per_page) > $max_value ? $max_value : ($start + $num_per_page);
if ($start != $counter - $max_value && !$start_invalid)
$pageindex .= $display_page > $counter - $num_per_page ? ' ' : sprintf($base_link, $display_page, '»');
}
else
{
// If they didn't enter an odd value, pretend they did.
$PageContiguous = (int) ($modSettings['compactTopicPagesContiguous'] - ($modSettings['compactTopicPagesContiguous'] % 2)) / 2;
// Show the first page. (>1< ... 6 7 [8] 9 10 ... 15)
if ($start > $num_per_page * $PageContiguous)
$pageindex = sprintf($base_link, 0, '1');
else
$pageindex = '';
// Show the ... after the first page. (1 >...< 6 7 [8] 9 10 ... 15)
if ($start > $num_per_page * ($PageContiguous + 1))
$pageindex .= '<b> ... </b>';
// Show the pages before the current one. (1 ... >6 7< [8] 9 10 ... 15)
for ($nCont = $PageContiguous; $nCont >= 1; $nCont--)
if ($start >= $num_per_page * $nCont)
{
$tmpStart = $start - $num_per_page * $nCont;
$pageindex.= sprintf($base_link, $tmpStart, $tmpStart / $num_per_page + 1);
}
// Show the current page. (1 ... 6 7 >[8]< 9 10 ... 15)
if (!$start_invalid)
$pageindex .= '[<b>' . ($start / $num_per_page + 1) . '</b>] ';
else
$pageindex .= sprintf($base_link, $start, $start / $num_per_page + 1);
// Show the pages after the current one... (1 ... 6 7 [8] >9 10< ... 15)
$tmpMaxPages = (int) (($max_value - 1) / $num_per_page) * $num_per_page;
for ($nCont = 1; $nCont <= $PageContiguous; $nCont++)
if ($start + $num_per_page * $nCont <= $tmpMaxPages)
{
$tmpStart = $start + $num_per_page * $nCont;
$pageindex .= sprintf($base_link, $tmpStart, $tmpStart / $num_per_page + 1);
}
// Show the '...' part near the end. (1 ... 6 7 [8] 9 10 >...< 15)
if ($start + $num_per_page * ($PageContiguous + 1) < $tmpMaxPages)
$pageindex .= '<b> ... </b>';
// Show the last number in the list. (1 ... 6 7 [8] 9 10 ... >15<)
if ($start + $num_per_page * $PageContiguous < $tmpMaxPages)
$pageindex .= sprintf($base_link, $tmpMaxPages, $tmpMaxPages / $num_per_page + 1);
}
return $pageindex;
}
// Formats a number to display in the style of the admin's choosing.
function comma_format($number, $override_decimal_count = false)
{
global $modSettings;
static $thousands_separator = null, $decimal_separator = null, $decimal_count = null;
// !!! Should, perhaps, this just be handled in the language files, and not a mod setting?
// (French uses 1 234,00 for example... what about a multilingual forum?)
// Cache these values...
if ($decimal_separator === null)
{
// Not set for whatever reason?
if (empty($modSettings['number_format']) || preg_match('~^1([^\d]*)?234([^\d]*)(0*?)$~', $modSettings['number_format'], $matches) != 1)
return $number;
// Cache these each load...
$thousands_separator = $matches[1];
$decimal_separator = $matches[2];
$decimal_count = strlen($matches[3]);
}
// Format the string with our friend, number_format.
return number_format($number, is_float($number) ? ($override_decimal_count === false ? $decimal_count : $override_decimal_count) : 0, $decimal_separator, $thousands_separator);
}
// Format a time to make it look purdy.
function timeformat($logTime, $show_today = true)
{
global $user_info, $txt, $db_prefix, $modSettings, $func;
// Offset the time.
$time = $logTime + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600;
// We can't have a negative date (on Windows, at least.)
if ($time < 0)
$time = 0;
// Today and Yesterday?
if ($modSettings['todayMod'] >= 1 && $show_today === true)
{
// Get the current time.
$nowtime = forum_time();
$then = @getdate($time);
$now = @getdate($nowtime);
// Try to make something of a time format string...
$s = strpos($user_info['time_format'], '%S') === false ? '' : ':%S';
if (strpos($user_info['time_format'], '%H') === false && strpos($user_info['time_format'], '%T') === false)
$today_fmt = '%I:%M' . $s . ' %p';
else
$today_fmt = '%H:%M' . $s;
// Same day of the year, same year.... Today!
if ($then['yday'] == $now['yday'] && $then['year'] == $now['year'])
return $txt['smf10'] . timeformat($logTime, $today_fmt);
// Day-of-year is one less and same year, or it's the first of the year and that's the last of the year...
if ($modSettings['todayMod'] == '2' && (($then['yday'] == $now['yday'] - 1 && $then['year'] == $now['year']) || ($now['yday'] == 0 && $then['year'] == $now['year'] - 1) && $then['mon'] == 12 && $then['mday'] == 31))
return $txt['smf10b'] . timeformat($logTime, $today_fmt);
}
$str = !is_bool($show_today) ? $show_today : $user_info['time_format'];
if (setlocale(LC_TIME, $txt['lang_locale']))
{
foreach (array('%a', '%A', '%b', '%B') as $token)
if (strpos($str, $token) !== false)
$str = str_replace($token, $func['ucwords'](strftime($token, $time)), $str);
}
else
{
// Do-it-yourself time localization. Fun.
foreach (array('%a' => 'days_short', '%A' => 'days', '%b' => 'months_short', '%B' => 'months') as $token => $text_label)
if (strpos($str, $token) !== false)
$str = str_replace($token, $txt[$text_label][(int) strftime($token === '%a' || $token === '%A' ? '%w' : '%m', $time)], $str);
if (strpos($str, '%p'))
$str = str_replace('%p', (strftime('%H', $time) < 12 ? 'am' : 'pm'), $str);
}
// Format any other characters..
return strftime($str, $time);
}
// Removes special entities from strings. Compatibility...
function un_htmlspecialchars($string)
{
return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES)) + array(''' => '\'', ' ' => ' '));
}
if (!function_exists('stripos'))
{
function stripos($haystack, $needle, $offset = 0)
{
return strpos(strtolower($haystack), strtolower($needle), $offset);
}
}
// Shorten a subject + internationalization concerns.
function shorten_subject($subject, $len)
{
global $func;
// It was already short enough!
if ($func['strlen']($subject) <= $len)
return $subject;
// Shorten it by the length it was too long, and strip off junk from the end.
return $func['substr']($subject, 0, $len) . '...';
}
// The current time with offset.
function forum_time($use_user_offset = true, $timestamp = null)
{
global $user_info, $modSettings;
if ($timestamp === null)
$timestamp = time();
elseif ($timestamp == 0)
return 0;
return $timestamp + ($modSettings['time_offset'] + ($use_user_offset ? $user_info['time_offset'] : 0)) * 3600;
}
// This gets all possible permutations of an array.
function permute($array)
{
$orders = array($array);
$n = count($array);
$p = range(0, $n);
for ($i = 1; $i < $n; null)
{
$p[$i]--;
$j = $i % 2 != 0 ? $p[$i] : 0;
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
for ($i = 1; $p[$i] == 0; $i++)
$p[$i] = 1;
$orders[] = $array;
}
return $orders;
}
// For old stuff still using doUBBC()...
function doUBBC($message, $enableSmileys = true)
{
return parse_bbc($message, $enableSmileys);
}
// Parse bulletin board code in a string, as well as smileys optionally.
function parse_bbc($message, $smileys = true, $cache_id = '')
{
global $txt, $scripturl, $context, $modSettings, $user_info;
static $bbc_codes = array(), $itemcodes = array(), $no_autolink_tags = array();
static $disabled;
// Never show smileys for wireless clients. More bytes, can't see it anyway :P.
if (WIRELESS)
$smileys = false;
elseif ($smileys !== null && ($smileys == '1' || $smileys == '0'))
$smileys = (bool) $smileys;
if (empty($modSettings['enableBBC']) && $message !== false)
{
if ($smileys === true)
parsesmileys($message);
return $message;
}
// Just in case it wasn't determined yet whether UTF-8 is enabled.
if (!isset($context['utf8']))
$context['utf8'] = (empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8';
// Sift out the bbc for a performance improvement.
if (empty($bbc_codes) || $message === false)
{
if (!empty($modSettings['disabledBBC']))
{
$temp = explode(',', strtolower($modSettings['disabledBBC']));
foreach ($temp as $tag)
$disabled[trim($tag)] = true;
}
if (empty($modSettings['enableEmbeddedFlash']))
$disabled['flash'] = true;
/* The following bbc are formatted as an array, with keys as follows:
tag: the tag's name - should be lowercase!
type: one of...
- (missing): [tag]parsed content[/tag]
- unparsed_equals: [tag=xyz]parsed content[/tag]
- parsed_equals: [tag=parsed data]parsed content[/tag]
- unparsed_content: [tag]unparsed content[/tag]
- closed: [tag], [tag/], [tag /]
- unparsed_commas: [tag=1,2,3]parsed content[/tag]
- unparsed_commas_content: [tag=1,2,3]unparsed content[/tag]
- unparsed_equals_content: [tag=...]unparsed content[/tag]
parameters: an optional array of parameters, for the form
[tag abc=123]content[/tag]. The array is an associative array
where the keys are the parameter names, and the values are an
array which may contain the following:
- match: a regular expression to validate and match the value.
- quoted: true if the value should be quoted.
- validate: callback to evaluate on the data, which is $data.
- value: a string in which to replace $1 with the data.
either it or validate may be used, not both.
- optional: true if the parameter is optional.
test: a regular expression to test immediately after the tag's
'=', ' ' or ']'. Typically, should have a \] at the end.
Optional.
content: only available for unparsed_content, closed,
unparsed_commas_content, and unparsed_equals_content.
$1 is replaced with the content of the tag. Parameters
are repalced in the form {param}. For unparsed_commas_content,
$2, $3, ..., $n are replaced.
before: only when content is not used, to go before any
content. For unparsed_equals, $1 is replaced with the value.
For unparsed_commas, $1, $2, ..., $n are replaced.
after: similar to before in every way, except that it is used
when the tag is closed.
disabled_content: used in place of content when the tag is
disabled. For closed, default is '', otherwise it is '$1' if
block_level is false, '<div>$1</div>' elsewise.
disabled_before: used in place of before when disabled. Defaults
to '<div>' if block_level, '' if not.
disabled_after: used in place of after when disabled. Defaults
to '</div>' if block_level, '' if not.
block_level: set to true the tag is a "block level" tag, similar
to HTML. Block level tags cannot be nested inside tags that are
not block level, and will not be implicitly closed as easily.
One break following a block level tag may also be removed.
trim: if set, and 'inside' whitespace after the begin tag will be
removed. If set to 'outside', whitespace after the end tag will
meet the same fate.
validate: except when type is missing or 'closed', a callback to
validate the data as $data. Depending on the tag's type, $data
may be a string or an array of strings (corresponding to the
replacement.)
quoted: when type is 'unparsed_equals' or 'parsed_equals' only,
may be not set, 'optional', or 'required' corresponding to if
the content may be quoted. This allows the parser to read
[tag="abc]def[esdf]"] properly.
require_parents: an array of tag names, or not set. If set, the
enclosing tag *must* be one of the listed tags, or parsing won't
occur.
require_children: similar to require_parents, if set children
won't be parsed if they are not in the list.
disallow_children: similar to, but very different from,
require_children, if it is set the listed tags will not be
parsed inside the tag.
*/
$codes = array(
array(
'tag' => 'abbr',
'type' => 'unparsed_equals',
'before' => '<abbr title="$1">',
'after' => '</abbr>',
'quoted' => 'optional',
'disabled_after' => ' ($1)',
),
array(
'tag' => 'acronym',
'type' => 'unparsed_equals',
'before' => '<acronym title="$1">',
'after' => '</acronym>',
'quoted' => 'optional',
'disabled_after' => ' ($1)',
),
array(
'tag' => 'anchor',
'type' => 'unparsed_equals',
'test' => '[#]?([A-Za-z][A-Za-z0-9_\-]*)\]',
'before' => '<span id="post_$1" />',
'after' => '',
),
array(
'tag' => 'b',
'before' => '<b>',
'after' => '</b>',
),
array(
'tag' => 'black',
'before' => '<span style="color: black;">',
'after' => '</span>',
),
array(
'tag' => 'blue',
'before' => '<span style="color: blue;">',
'after' => '</span>',
),
array(
'tag' => 'br',
'type' => 'closed',
'content' => '<br />',
),
array(
'tag' => 'code',
'type' => 'unparsed_content',
'content' => '<div class="codeheader">' . $txt['smf238'] . ':</div><div class="code">' . ($context['browser']['is_gecko'] ? '<pre style="margin-top: 0; display: inline;">$1</pre>' : '$1') . '</div>',
// !!! Maybe this can be simplified?
'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
global $context;
if (!isset($disabled[\'code\']))
{
$php_parts = preg_split(\'~(<\?php|\?>)~\', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
{
// Do PHP code coloring?
if ($php_parts[$php_i] != \'<?php\')
continue;
$php_string = \'\';
while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != \'?>\')
{
$php_string .= $php_parts[$php_i];
$php_parts[$php_i++] = \'\';
}
$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
}
// Fix the PHP code stuff...
$data = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode(\'\', $php_parts));
// Older browsers are annoying, aren\'t they?
if ($context[\'browser\'][\'is_ie4\'] || $context[\'browser\'][\'is_ie5\'] || $context[\'browser\'][\'is_ie5.5\'])
$data = str_replace("\t", "<pre style=\"display: inline;\">\t</pre>", $data);
elseif (!$context[\'browser\'][\'is_gecko\'])
$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
}'),
'block_level' => true,
),
array(
'tag' => 'code',
'type' => 'unparsed_equals_content',
'content' => '<div class="codeheader">' . $txt['smf238'] . ': ($2)</div><div class="code">' . ($context['browser']['is_gecko'] ? '<pre style="margin-top: 0; display: inline;">$1</pre>' : '$1') . '</div>',
// !!! Maybe this can be simplified?
'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
global $context;
if (!isset($disabled[\'code\']))
{
$php_parts = preg_split(\'~(<\?php|\?>)~\', $data[0], -1, PREG_SPLIT_DELIM_CAPTURE);
for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
{
// Do PHP code coloring?
if ($php_parts[$php_i] != \'<?php\')
continue;
$php_string = \'\';
while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != \'?>\')
{
$php_string .= $php_parts[$php_i];
$php_parts[$php_i++] = \'\';
}
$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
}
// Fix the PHP code stuff...
$data[0] = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode(\'\', $php_parts));
// Older browsers are annoying, aren\'t they?
if ($context[\'browser\'][\'is_ie4\'] || $context[\'browser\'][\'is_ie5\'] || $context[\'browser\'][\'is_ie5.5\'])
$data = str_replace("\t", "<pre style=\"display: inline;\">\t</pre>", $data);
elseif (!$context[\'browser\'][\'is_gecko\'])
$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
}'),
'block_level' => true,
),
array(
'tag' => 'center',
'before' => '<div align="center">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'color',
'type' => 'unparsed_equals',
'test' => '(#[\da-fA-F]{3}|#[\da-fA-F]{6}|[A-Za-z]{1,12})\]',
'before' => '<span style="color: $1;">',
'after' => '</span>',
),
array(
'tag' => 'email',
'type' => 'unparsed_content',
'content' => '<a href="mailto:$1">$1</a>',
// !!! Should this respect guest_hideContacts?
'validate' => create_function('&$tag, &$data, $disabled', '$data = strtr($data, array(\'<br />\' => \'\'));'),
),
array(
'tag' => 'email',
'type' => 'unparsed_equals',
'before' => '<a href="mailto:$1">',
'after' => '</a>',
// !!! Should this respect guest_hideContacts?
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'ftp',
'type' => 'unparsed_content',
'content' => '<a href="$1" target="_blank">$1</a>',
'validate' => create_function('&$tag, &$data, $disabled', '
$data = strtr($data, array(\'<br />\' => \'\'));
if (strpos($data, \'ftp://\') !== 0 && strpos($data, \'ftps://\') !== 0)
$data = \'ftp://\' . $data;
'),
),
array(
'tag' => 'ftp',
'type' => 'unparsed_equals',
'before' => '<a href="$1" target="_blank">',
'after' => '</a>',
'validate' => create_function('&$tag, &$data, $disabled', '
if (strpos($data, \'ftp://\') !== 0 && strpos($data, \'ftps://\') !== 0)
$data = \'ftp://\' . $data;
'),
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'font',
'type' => 'unparsed_equals',
'test' => '[A-Za-z0-9_,\-\s]+?\]',
'before' => '<span style="font-family: $1;">',
'after' => '</span>',
),
array(
'tag' => 'flash',
'type' => 'unparsed_commas_content',
'test' => '\d+,\d+\]',
'content' => ($context['browser']['is_ie'] && !$context['browser']['is_mac_ie'] ? '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="$2" height="$3"><param name="movie" value="$1" /><param name="play" value="true" /><param name="loop" value="true" /><param name="quality" value="high" /><param name="AllowScriptAccess" value="never" /><embed src="$1" width="$2" height="$3" play="true" loop="true" quality="high" AllowScriptAccess="never" /><noembed><a href="$1" target="_blank">$1</a></noembed></object>' : '<embed type="application/x-shockwave-flash" src="$1" width="$2" height="$3" play="true" loop="true" quality="high" AllowScriptAccess="never" /><noembed><a href="$1" target="_blank">$1</a></noembed>'),
'validate' => create_function('&$tag, &$data, $disabled', '
if (isset($disabled[\'url\']))
$tag[\'content\'] = \'$1\';
elseif (strpos($data[0], \'http://\') !== 0 && strpos($data[0], \'https://\') !== 0)
$data[0] = \'http://\' . $data[0];
'),
'disabled_content' => '<a href="$1" target="_blank">$1</a>',
),
array(
'tag' => 'green',
'before' => '<span style="color: green;">',
'after' => '</span>',
),
array(
'tag' => 'glow',
'type' => 'unparsed_commas',
'test' => '[#0-9a-zA-Z\-]{3,12},([012]\d{1,2}|\d{1,2})(,[^]]+)?\]',
'before' => $context['browser']['is_ie'] ? '<table border="0" cellpadding="0" cellspacing="0" style="display: inline; vertical-align: middle; font: inherit;"><tr><td style="filter: Glow(color=$1, strength=$2); font: inherit;">' : '<span style="background-color: $1;">',
'after' => $context['browser']['is_ie'] ? '</td></tr></table> ' : '</span>',
),
array(
'tag' => 'hr',
'type' => 'closed',
'content' => '<hr style="clear: both;" />',
'block_level' => true,
),
array(
'tag' => 'youtube',
'type' => 'unparsed_commas_content',
'test' => '\d+,\d+\]',
'content' => '<object width="$2" height="$3"><param name="movie" value="http://www.youtube.com/embed/$1" ></param><embed src="http://www.youtube.com/embed/$1" type="application/x-shockwave-flash" width="$2" height="$3"></embed></object>',
'validate' => create_function('&$tag, &$data, $disabled', '
if (isset($disabled[\'url\']))
$tag[\'content\'] = \'$1\';'),
'disabled_content' => '<a href="$1" target="_blank">$1</a>',
),
array(
'tag' => 'html',
'type' => 'unparsed_content',
'content' => '$1',
'block_level' => true,
'disabled_content' => '$1',
),
array(
'tag' => 'img',
'type' => 'unparsed_content',
'parameters' => array(
'alt' => array('optional' => true),
'width' => array('optional' => true, 'value' => ' width="$1"', 'match' => '(\d+)'),
'height' => array('optional' => true, 'value' => ' height="$1"', 'match' => '(\d+)'),
),
'content' => '<img src="$1" alt="{alt}"{width}{height} border="0" />',
'validate' => create_function('&$tag, &$data, $disabled', '
$data = strtr($data, array(\'<br />\' => \'\'));
if (strpos($data, \'http://\') !== 0 && strpos($data, \'https://\') !== 0)
$data = \'http://\' . $data;
'),
'disabled_content' => '($1)',
),
array(
'tag' => 'img',
'type' => 'unparsed_content',
'content' => '<img src="$1" alt="" border="0" />',
'validate' => create_function('&$tag, &$data, $disabled', '
$data = strtr($data, array(\'<br />\' => \'\'));
if (strpos($data, \'http://\') !== 0 && strpos($data, \'https://\') !== 0)
$data = \'http://\' . $data;
'),
'disabled_content' => '($1)',
),
array(
'tag' => 'i',
'before' => '<i>',
'after' => '</i>',
),
array(
'tag' => 'iurl',
'type' => 'unparsed_content',
'content' => '<a href="$1">$1</a>',
'validate' => create_function('&$tag, &$data, $disabled', '
$data = strtr($data, array(\'<br />\' => \'\'));
if (strpos($data, \'http://\') !== 0 && strpos($data, \'https://\') !== 0)
$data = \'http://\' . $data;
'),
),
array(
'tag' => 'iurl',
'type' => 'unparsed_equals',
'before' => '<a href="$1">',
'after' => '</a>',
'validate' => create_function('&$tag, &$data, $disabled', '
if (substr($data, 0, 1) == \'#\')
$data = \'#post_\' . substr($data, 1);
elseif (strpos($data, \'http://\') !== 0 && strpos($data, \'https://\') !== 0)
$data = \'http://\' . $data;
'),
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'li',
'before' => '<li>',
'after' => '</li>',
'trim' => 'outside',
'require_parents' => array('list'),
'block_level' => true,
'disabled_before' => '',
'disabled_after' => '<br />',
),
array(
'tag' => 'list',
'before' => '<ul style="margin-top: 0; margin-bottom: 0;">',
'after' => '</ul>',
'trim' => 'inside',
'require_children' => array('li'),
'block_level' => true,
),
array(
'tag' => 'list',
'parameters' => array(
'type' => array('match' => '(none|disc|circle|square|decimal|decimal-leading-zero|lower-roman|upper-roman|lower-alpha|upper-alpha|lower-greek|lower-latin|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha)'),
),
'before' => '<ul style="margin-top: 0; margin-bottom: 0; list-style-type: {type};">',
'after' => '</ul>',
'trim' => 'inside',
'require_children' => array('li'),
'block_level' => true,
),
array(
'tag' => 'left',
'before' => '<div style="text-align: left;">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'ltr',
'before' => '<div dir="ltr">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'me',
'type' => 'unparsed_equals',
'before' => '<div class="meaction">* $1 ',
'after' => '</div>',
'quoted' => 'optional',
'block_level' => true,
'disabled_before' => '/me ',
'disabled_after' => '<br />',
),
array(
'tag' => 'move',
'before' => '<marquee>',
'after' => '</marquee>',
'block_level' => true,
),
array(
'tag' => 'nobbc',
'type' => 'unparsed_content',
'content' => '$1',
),
array(
'tag' => 'pre',
'before' => '<pre>',
'after' => '</pre>',
),
array(
'tag' => 'php',
'type' => 'unparsed_content',
'content' => '<div class="phpcode">$1</div>',
'validate' => isset($disabled['php']) ? null : create_function('&$tag, &$data, $disabled', '
if (!isset($disabled[\'php\']))
{
$add_begin = substr(trim($data), 0, 5) != \'<?\';
$data = highlight_php_code($add_begin ? \'<?php \' . $data . \'?>\' : $data);
if ($add_begin)
$data = preg_replace(array(\'~^(.+?)<\?.{0,40}?php( |\s)~\', \'~\?>((?:</(font|span)>)*)$~\'), \'$1\', $data, 2);
}'),
'block_level' => true,
'disabled_content' => '$1',
),
array(
'tag' => 'quote',
'before' => '<div class="quoteheader">' . $txt['smf240'] . '</div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'quote',
'parameters' => array(
'author' => array('match' => '(.{1,192}?)', 'quoted' => true, 'validate' => 'parse_bbc'),
),
'before' => '<div class="quoteheader">' . $txt['smf239'] . ': {author}</div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'quote',
'type' => 'parsed_equals',
'before' => '<div class="quoteheader">' . $txt['smf239'] . ': $1</div><div class="quote">',
'after' => '</div>',
'quoted' => 'optional',
'block_level' => true,
),
array(
'tag' => 'quote',
'parameters' => array(
'author' => array('match' => '([^<>]{1,192}?)'),
'link' => array('match' => '(?:board=\d+;)?((?:topic|threadid)=[\dmsg#\./]{1,40}(?:;start=[\dmsg#\./]{1,40})?|action=profile;u=\d+)'),
'date' => array('match' => '(\d+)', 'validate' => 'timeformat'),
),
'before' => '<div class="quoteheader"><a href="' . $scripturl . '?{link}">' . $txt['smf239'] . ': {author} ' . $txt[176] . ' {date}</a></div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'quote',
'parameters' => array(
'author' => array('match' => '(.{1,192}?)', 'validate' => 'parse_bbc'),
),
'before' => '<div class="quoteheader">' . $txt['smf239'] . ': {author}</div><div class="quote">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'right',
'before' => '<div style="text-align: right;">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 'red',
'before' => '<span style="color: red;">',
'after' => '</span>',
),
array(
'tag' => 'rtl',
'before' => '<div dir="rtl">',
'after' => '</div>',
'block_level' => true,
),
array(
'tag' => 's',
'before' => '<del>',
'after' => '</del>',
),
array(
'tag' => 'size',
'type' => 'unparsed_equals',
'test' => '([1-9][\d]?p[xt]|(?:x-)?small(?:er)?|(?:x-)?large[r]?)\]',
// !!! line-height
'before' => '<span style="font-size: $1; line-height: 1.3em;">',
'after' => '</span>',
),
array(
'tag' => 'size',
'type' => 'unparsed_equals',
'test' => '[1-9]\]',
// !!! line-height
'before' => '<font size="$1" style="line-height: 1.3em;">',
'after' => '</font>',
),
array(
'tag' => 'sub',
'before' => '<sub>',
'after' => '</sub>',
),
array(
'tag' => 'sup',
'before' => '<sup>',
'after' => '</sup>',
),
array(
'tag' => 'shadow',
'type' => 'unparsed_commas',
'test' => '[#0-9a-zA-Z\-]{3,12},(left|right|top|bottom|[0123]\d{0,2})\]',
'before' => $context['browser']['is_ie'] ? '<span style="filter: Shadow(color=$1, direction=$2); height: 1.2em;\">' : '<span style="text-shadow: $1 $2">',
'after' => '</span>',
'validate' => $context['browser']['is_ie'] ? create_function('&$tag, &$data, $disabled', '
if ($data[1] == \'left\')
$data[1] = 270;
elseif ($data[1] == \'right\')
$data[1] = 90;
elseif ($data[1] == \'top\')
$data[1] = 0;
elseif ($data[1] == \'bottom\')
$data[1] = 180;
else
$data[1] = (int) $data[1];') : create_function('&$tag, &$data, $disabled', '
if ($data[1] == \'top\' || (is_numeric($data[1]) && $data[1] < 50))
return \'0 -2px\';
elseif ($data[1] == \'right\' || (is_numeric($data[1]) && $data[1] < 100))
return \'2px 0\';
elseif ($data[1] == \'bottom\' || (is_numeric($data[1]) && $data[1] < 190))
return \'0 2px\';
elseif ($data[1] == \'left\' || (is_numeric($data[1]) && $data[1] < 280))
return \'-2px 0\';
else
return \'0 0\';'),
),
array(
'tag' => 'time',
'type' => 'unparsed_content',
'content' => '$1',
'validate' => create_function('&$tag, &$data, $disabled', '
if (is_numeric($data))
$data = timeformat($data);
else
$tag[\'content\'] = \'[time]$1[/time]\';'),
),
array(
'tag' => 'tt',
'before' => '<tt>',
'after' => '</tt>',
),
array(
'tag' => 'table',
'before' => '<table style="font: inherit; color: inherit;">',
'after' => '</table>',
'trim' => 'inside',
'require_children' => array('tr'),
'block_level' => true,
),
array(
'tag' => 'tr',
'before' => '<tr>',
'after' => '</tr>',
'require_parents' => array('table'),
'require_children' => array('td'),
'trim' => 'both',
'block_level' => true,
'disabled_before' => '',
'disabled_after' => '',
),
array(
'tag' => 'td',
'before' => '<td valign="top" style="font: inherit; color: inherit;">',
'after' => '</td>',
'require_parents' => array('tr'),
'trim' => 'outside',
'block_level' => true,
'disabled_before' => '',
'disabled_after' => '',
),
array(
'tag' => 'url',
'type' => 'unparsed_content',
'content' => '<a href="$1" target="_blank">$1</a>',
'validate' => create_function('&$tag, &$data, $disabled', '
$data = strtr($data, array(\'<br />\' => \'\'));
if (strpos($data, \'http://\') !== 0 && strpos($data, \'https://\') !== 0)
$data = \'http://\' . $data;
'),
),
array(
'tag' => 'url',
'type' => 'unparsed_equals',
'before' => '<a href="$1" target="_blank">',
'after' => '</a>',
'validate' => create_function('&$tag, &$data, $disabled', '
if (strpos($data, \'http://\') !== 0 && strpos($data, \'https://\') !== 0)
$data = \'http://\' . $data;
'),
'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
'disabled_after' => ' ($1)',
),
array(
'tag' => 'u',
'before' => '<span style="text-decoration: underline;">',
'after' => '</span>',
),
array(
'tag' => 'white',
'before' => '<span style="color: white;">',
'after' => '</span>',
),
);
// This is mainly for the bbc manager, so it's easy to add tags above. Custom BBC should be added above this line.
if ($message === false)
return $codes;
// So the parser won't skip them.
$itemcodes = array(
'*' => '',
'@' => 'disc',
'+' => 'square',
'x' => 'square',
'#' => 'square',
'o' => 'circle',
'O' => 'circle',
'0' => 'circle',
);
if (!isset($disabled['li']) && !isset($disabled['list']))
{
foreach ($itemcodes as $c => $dummy)
$bbc_codes[$c] = array();
}
// Inside these tags autolink is not recommendable.
$no_autolink_tags = array(
'url',
'iurl',
'ftp',
'email',
);
// Shhhh!
if (!isset($disabled['color']))
{
$codes[] = array(
'tag' => 'chrissy',
'before' => '<span style="color: #CC0099;">',
'after' => ' :-*</span>',
);
$codes[] = array(
'tag' => 'kissy',
'before' => '<span style="color: #CC0099;">',
'after' => ' :-*</span>',
);
}
foreach ($codes as $c)
$bbc_codes[substr($c['tag'], 0, 1)][] = $c;
$codes = null;
}
// Shall we take the time to cache this?
if ($cache_id != '' && !empty($modSettings['cache_enable']) && (($modSettings['cache_enable'] >= 2 && strlen($message) > 1000) || strlen($message) > 2400))
{
// It's likely this will change if the message is modified.
$cache_key = 'parse:' . $cache_id . '-' . md5(md5($message) . '-' . $smileys . (empty($disabled) ? '' : implode(',', array_keys($disabled))) . serialize($context['browser']) . $txt['lang_locale'] . $user_info['time_offset'] . $user_info['time_format']);
if (($temp = cache_get_data($cache_key, 240)) != null)
return $temp;
$cache_t = microtime();
}
if ($smileys === 'print')
{
// [glow], [shadow], and [move] can't really be printed.
$disabled['glow'] = true;
$disabled['shadow'] = true;
$disabled['move'] = true;
// Colors can't well be displayed... supposed to be black and white.
$disabled['color'] = true;
$disabled['black'] = true;
$disabled['blue'] = true;
$disabled['white'] = true;
$disabled['red'] = true;
$disabled['green'] = true;
$disabled['me'] = true;
// Color coding doesn't make sense.
$disabled['php'] = true;
// Links are useless on paper... just show the link.
$disabled['ftp'] = true;
$disabled['url'] = true;
$disabled['iurl'] = true;
$disabled['email'] = true;
$disabled['flash'] = true;
// !!! Change maybe?
if (!isset($_GET['images']))
$disabled['img'] = true;
// !!! Interface/setting to add more?
}
$open_tags = array();
$message = strtr($message, array("\n" => '<br />'));
// The non-breaking-space looks a bit different each time.
$non_breaking_space = $context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{C2A0}' : chr(0xC2) . chr(0xA0)) : '\xA0';
$pos = -1;
while ($pos !== false)
{
$last_pos = isset($last_pos) ? max($pos, $last_pos) : $pos;
$pos = strpos($message, '[', $pos + 1);
// Failsafe.
if ($pos === false || $last_pos > $pos)
$pos = strlen($message) + 1;
// Can't have a one letter smiley, URL, or email! (sorry.)
if ($last_pos < $pos - 1)
{
// We want to eat one less, and one more, character (for smileys.)
$last_pos = max($last_pos - 1, 0);
$data = substr($message, $last_pos, $pos - $last_pos + 1);
// Take care of some HTML!
if (!empty($modSettings['enablePostHTML']) && strpos($data, '<') !== false)
{
$data = preg_replace('~<a\s+href=((?:")?)((?:https?://|ftps?://|mailto:)\S+?)\\1>~i', '', $data);
$data = preg_replace('~</a>~i', '', $data);
// <br /> should be empty.
$empty_tags = array('br', 'hr');
foreach ($empty_tags as $tag)
$data = str_replace(array('<' . $tag . '>', '<' . $tag . '/>', '<' . $tag . ' />'), '[' . $tag . ' /]', $data);
// b, u, i, s, pre... basic tags.
$closable_tags = array('b', 'u', 'i', 's', 'em', 'ins', 'del', 'pre', 'blockquote');
foreach ($closable_tags as $tag)
{
$diff = substr_count($data, '<' . $tag . '>') - substr_count($data, '</' . $tag . '>');
$data = strtr($data, array('<' . $tag . '>' => '<' . $tag . '>', '</' . $tag . '>' => '</' . $tag . '>'));
if ($diff > 0)
$data .= str_repeat('</' . $tag . '>', $diff);
}
// Do <img ... /> - with security... action= -> action-.
preg_match_all('~<img\s+src=((?:")?)((?:https?://|ftps?://)\S+?)\\1(?:\s+alt=(".*?"|\S*?))?(?:\s?/)?>~i', $data, $matches, PREG_PATTERN_ORDER);
if (!empty($matches[0]))
{
$replaces = array();
foreach ($matches[2] as $match => $imgtag)
{
$alt = empty($matches[3][$match]) ? '' : ' alt=' . preg_replace('~^"|"$~', '', $matches[3][$match]);
// Remove action= from the URL - no funny business, now.
if (preg_match('~action(=|%3d)(?!dlattach)~i', $imgtag) != 0)
$imgtag = preg_replace('~action(=|%3d)(?!dlattach)~i', 'action-', $imgtag);
// Check if the image is larger than allowed.
if (!empty($modSettings['max_image_width']) && !empty($modSettings['max_image_height']))
{
list ($width, $height) = url_image_size($imgtag);
if (!empty($modSettings['max_image_width']) && $width > $modSettings['max_image_width'])
{
$height = (int) (($modSettings['max_image_width'] * $height) / $width);
$width = $modSettings['max_image_width'];
}
if (!empty($modSettings['max_image_height']) && $height > $modSettings['max_image_height'])
{
$width = (int) (($modSettings['max_image_height'] * $width) / $height);
$height = $modSettings['max_image_height'];
}
// Set the new image tag.
$replaces[$matches[0][$match]] = '[img width=' . $width . ' height=' . $height . $alt . ']' . $imgtag . '[/img]';
}
else
$replaces[$matches[0][$match]] = '[img' . $alt . ']' . $imgtag . '[/img]';
}
$data = strtr($data, $replaces);
}
}
if (!empty($modSettings['autoLinkUrls']))
{
// Are we inside tags that should be auto linked?
$no_autolink_area = false;
if (!empty($open_tags))
{
foreach ($open_tags as $open_tag)
if (in_array($open_tag['tag'], $no_autolink_tags))
$no_autolink_area = true;
}
// Don't go backwards.
//!!! Don't think is the real solution....
$lastAutoPos = isset($lastAutoPos) ? $lastAutoPos : 0;
if ($pos < $lastAutoPos)
$no_autolink_area = true;
$lastAutoPos = $pos;
if (!$no_autolink_area)
{
// Parse any URLs.... have to get rid of the @ problems some things cause... stupid email addresses.
if (!isset($disabled['url']) && (strpos($data, '://') !== false || strpos($data, 'www.') !== false))
{
// Switch out quotes really quick because they can cause problems.
$data = strtr($data, array(''' => '\'', ' ' => $context['utf8'] ? "\xC2\xA0" : "\xA0", '"' => '>">', '"' => '<"<', '<' => '<lt<'));
// Only do this if the preg survives.
if (is_string($result = preg_replace(array(
'~(?<=[\s>\.(;\'"]|^)((?:http|https|ftp|ftps)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i',
'~(?<=[\s>(\'<]|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i'
), array(
'http://$1',
'$1'
), $data)))
$data = $result;
$data = strtr($data, array('\'' => ''', $context['utf8'] ? "\xC2\xA0" : "\xA0" => ' ', '>">' => '"', '<"<' => '"', '<lt<' => '<'));
}
// Next, emails...
if (!isset($disabled['email']) && strpos($data, '@') !== false)
{
$data = preg_replace('~(?<=[\?\s' . $non_breaking_space . '\[\]()*\\\;>]|^)([\w\-\.]{1,80}@[\w\-]+\.[\w\-\.]+[\w\-])(?=[?,\s' . $non_breaking_space . '\[\]()*\\\]|$|<br />| |>|<|"|'|\.(?:\.|;| |\s|$|<br />))~' . ($context['utf8'] ? 'u' : ''), '$1', $data);
$data = preg_replace('~(?<=<br />)([\w\-\.]{1,80}@[\w\-]+\.[\w\-\.]+[\w\-])(?=[?\.,;\s' . $non_breaking_space . '\[\]()*\\\]|$|<br />| |>|<|"|')~' . ($context['utf8'] ? 'u' : ''), '$1', $data);
}
}
}
$data = strtr($data, array("\t" => ' '));
if (!empty($modSettings['fixLongWords']) && $modSettings['fixLongWords'] > 5)
{
// This is SADLY and INCREDIBLY browser dependent.
if ($context['browser']['is_gecko'] || $context['browser']['is_konqueror'])
$breaker = '<span style="margin: 0 -0.5ex 0 0;"> </span>';
// Opera...
elseif ($context['browser']['is_opera'])
$breaker = '<span style="margin: 0 -0.65ex 0 -1px;"> </span>';
// Internet Explorer...
else
$breaker = '<span style="width: 0; margin: 0 -0.6ex 0 -1px;"> </span>';
// PCRE will not be happy if we don't give it a short.
$modSettings['fixLongWords'] = (int) min(65535, $modSettings['fixLongWords']);
// The idea is, find words xx long, and then replace them with xx + space + more.
if (strlen($data) > $modSettings['fixLongWords'])
{
// This is done in a roundabout way because $breaker has "long words" :P.
$data = strtr($data, array($breaker => '< >', ' ' => $context['utf8'] ? "\xC2\xA0" : "\xA0"));
$data = preg_replace(
'~(?<=[>;:!? ' . $non_breaking_space . '\]()]|^)([\w\.]{' . $modSettings['fixLongWords'] . ',})~e' . ($context['utf8'] ? 'u' : ''),
"preg_replace('/(.{" . ($modSettings['fixLongWords'] - 1) . '})/' . ($context['utf8'] ? 'u' : '') . "', '\\\$1< >', '\$1')",
$data);
$data = strtr($data, array('< >' => $breaker, $context['utf8'] ? "\xC2\xA0" : "\xA0" => ' '));
}
}
// Do any smileys!
if ($smileys === true)
parsesmileys($data);
// If it wasn't changed, no copying or other boring stuff has to happen!
if ($data != substr($message, $last_pos, $pos - $last_pos + 1))
{
$message = substr($message, 0, $last_pos) . $data . substr($message, $pos + 1);
// Since we changed it, look again incase we added or removed a tag. But we don't want to skip any.
$old_pos = strlen($data) + $last_pos - 1;
$pos = strpos($message, '[', $last_pos);
$pos = $pos === false ? $old_pos : min($pos, $old_pos);
}
}
// Are we there yet? Are we there yet?
if ($pos >= strlen($message) - 1)
break;
$tags = strtolower(substr($message, $pos + 1, 1));
if ($tags == '/' && !empty($open_tags))
{
$pos2 = strpos($message, ']', $pos + 1);
if ($pos2 == $pos + 2)
continue;
$look_for = strtolower(substr($message, $pos + 2, $pos2 - $pos - 2));
$to_close = array();
$block_level = null;
do
{
$tag = array_pop($open_tags);
if (!$tag)
break;
if (!empty($tag['block_level']))
{
// Only find out if we need to.
if ($block_level === false)
{
array_push($open_tags, $tag);
break;
}
// The idea is, if we are LOOKING for a block level tag, we can close them on the way.
if (strlen($look_for) > 0 && isset($bbc_codes[$look_for{0}]))
{
foreach ($bbc_codes[$look_for{0}] as $temp)
if ($temp['tag'] == $look_for)
{
$block_level = !empty($temp['block_level']);
break;
}
}
if ($block_level !== true)
{
$block_level = false;
array_push($open_tags, $tag);
break;
}
}
$to_close[] = $tag;
}
while ($tag['tag'] != $look_for);
// Did we just eat through everything and not find it?
if ((empty($open_tags) && (empty($tag) || $tag['tag'] != $look_for)))
{
$open_tags = $to_close;
continue;
}
elseif (!empty($to_close) && $tag['tag'] != $look_for)
{
if ($block_level === null && isset($look_for{0}, $bbc_codes[$look_for{0}]))
{
foreach ($bbc_codes[$look_for{0}] as $temp)
if ($temp['tag'] == $look_for)
{
$block_level = !empty($temp['block_level']);
break;
}
}
// We're not looking for a block level tag (or maybe even a tag that exists...)
if (!$block_level)
{
foreach ($to_close as $tag)
array_push($open_tags, $tag);
continue;
}
}
foreach ($to_close as $tag)
{
$message = substr($message, 0, $pos) . $tag['after'] . substr($message, $pos2 + 1);
$pos += strlen($tag['after']);
$pos2 = $pos - 1;
// See the comment at the end of the big loop - just eating whitespace ;).
if (!empty($tag['block_level']) && substr($message, $pos, 6) == '<br />')
$message = substr($message, 0, $pos) . substr($message, $pos + 6);
if (!empty($tag['trim']) && $tag['trim'] != 'inside' && preg_match('~(<br />| |\s)*~', substr($message, $pos), $matches) != 0)
$message = substr($message, 0, $pos) . substr($message, $pos + strlen($matches[0]));
}
if (!empty($to_close))
{
$to_close = array();
$pos--;
}
continue;
}
// No tags for this character, so just keep going (fastest possible course.)
if (!isset($bbc_codes[$tags]))
continue;
$inside = empty($open_tags) ? null : $open_tags[count($open_tags) - 1];
$tag = null;
foreach ($bbc_codes[$tags] as $possible)
{
// Not a match?
if (strtolower(substr($message, $pos + 1, strlen($possible['tag']))) != $possible['tag'])
continue;
$next_c = substr($message, $pos + 1 + strlen($possible['tag']), 1);
// A test validation?
if (isset($possible['test']) && preg_match('~^' . $possible['test'] . '~', substr($message, $pos + 1 + strlen($possible['tag']) + 1)) == 0)
continue;
// Do we want parameters?
elseif (!empty($possible['parameters']))
{
if ($next_c != ' ')
continue;
}
elseif (isset($possible['type']))
{
// Do we need an equal sign?
if (in_array($possible['type'], array('unparsed_equals', 'unparsed_commas', 'unparsed_commas_content', 'unparsed_equals_content', 'parsed_equals')) && $next_c != '=')
continue;
// Maybe we just want a /...
if ($possible['type'] == 'closed' && $next_c != ']' && substr($message, $pos + 1 + strlen($possible['tag']), 2) != '/]' && substr($message, $pos + 1 + strlen($possible['tag']), 3) != ' /]')
continue;
// An immediate ]?
if ($possible['type'] == 'unparsed_content' && $next_c != ']')
continue;
}
// No type means 'parsed_content', which demands an immediate ] without parameters!
elseif ($next_c != ']')
continue;
// Check allowed tree?
if (isset($possible['require_parents']) && ($inside === null || !in_array($inside['tag'], $possible['require_parents'])))
continue;
elseif (isset($inside['require_children']) && !in_array($possible['tag'], $inside['require_children']))
continue;
// If this is in the list of disallowed child tags, don't parse it.
elseif (isset($inside['disallow_children']) && in_array($possible['tag'], $inside['disallow_children']))
continue;
$pos1 = $pos + 1 + strlen($possible['tag']) + 1;
// This is long, but it makes things much easier and cleaner.
if (!empty($possible['parameters']))
{
$preg = array();
foreach ($possible['parameters'] as $p => $info)
$preg[] = '(\s+' . $p . '=' . (empty($info['quoted']) ? '' : '"') . (isset($info['match']) ? $info['match'] : '(.+?)') . (empty($info['quoted']) ? '' : '"') . ')' . (empty($info['optional']) ? '' : '?');
// Okay, this may look ugly and it is, but it's not going to happen much and it is the best way of allowing any order of parameters but still parsing them right.
$match = false;
$orders = permute($preg);
foreach ($orders as $p)
if (preg_match('~^' . implode('', $p) . '\]~i', substr($message, $pos1 - 1), $matches) != 0)
{
$match = true;
break;
}
// Didn't match our parameter list, try the next possible.
if (!$match)
continue;
$params = array();
for ($i = 1, $n = count($matches); $i < $n; $i += 2)
{
$key = strtok(ltrim($matches[$i]), '=');
if (isset($possible['parameters'][$key]['value']))
$params['{' . $key . '}'] = strtr($possible['parameters'][$key]['value'], array('$1' => $matches[$i + 1]));
elseif (isset($possible['parameters'][$key]['validate']))
$params['{' . $key . '}'] = $possible['parameters'][$key]['validate']($matches[$i + 1]);
else
$params['{' . $key . '}'] = $matches[$i + 1];
// Just to make sure: replace any $ or { so they can't interpolate wrongly.
$params['{' . $key . '}'] = strtr($params['{' . $key . '}'], array('$' => '$', '{' => '{'));
}
foreach ($possible['parameters'] as $p => $info)
{
if (!isset($params['{' . $p . '}']))
$params['{' . $p . '}'] = '';
}
$tag = $possible;
// Put the parameters into the string.
if (isset($tag['before']))
$tag['before'] = strtr($tag['before'], $params);
if (isset($tag['after']))
$tag['after'] = strtr($tag['after'], $params);
if (isset($tag['content']))
$tag['content'] = strtr($tag['content'], $params);
$pos1 += strlen($matches[0]) - 1;
}
else
$tag = $possible;
break;
}
// Item codes are complicated buggers... they are implicit
s and can make
s!
if ($smileys !== false && $tag === null && isset($itemcodes[substr($message, $pos + 1, 1)]) && substr($message, $pos + 2, 1) == ']' && !isset($disabled['list']) && !isset($disabled['li']))
{
if (substr($message, $pos + 1, 1) == '0' && !in_array(substr($message, $pos - 1, 1), array(';', ' ', "\t", '>')))
continue;
$tag = $itemcodes[substr($message, $pos + 1, 1)];
// First let's set up the tree: it needs to be in a list, or after an li.
if ($inside === null || ($inside['tag'] != 'list' && $inside['tag'] != 'li'))
{
$open_tags[] = array(
'tag' => 'list',
'after' => '</ul>',
'block_level' => true,
'require_children' => array('li'),
'disallow_children' => isset($inside['disallow_children']) ? $inside['disallow_children'] : null,
);
$code = '<ul style="margin-top: 0; margin-bottom: 0;">';
}
// We're in a list item already: another itemcode? Close it first.
elseif ($inside['tag'] == 'li')
{
array_pop($open_tags);
$code = '</li>';
}
else
$code = '';
// Now we open a new tag.
$open_tags[] = array(
'tag' => 'li',
'after' => '</li>',
'trim' => 'outside',
'block_level' => true,
'disallow_children' => isset($inside['disallow_children']) ? $inside['disallow_children'] : null,
);
// First, open the tag...
$code .= '<li' . ($tag == '' ? '' : ' type="' . $tag . '"') . '>';
$message = substr($message, 0, $pos) . $code . substr($message, $pos + 3);
$pos += strlen($code) - 1;
// Next, find the next break (if any.) If there's more itemcode after it, keep it going - otherwise close!
$pos2 = strpos($message, '<br />', $pos);
$pos3 = strpos($message, '[/', $pos);
if ($pos2 !== false && ($pos2 <= $pos3 || $pos3 === false))
{
preg_match('~^(<br />| |\s|\[)+~', substr($message, $pos2 + 6), $matches);
$message = substr($message, 0, $pos2) . (!empty($matches[0]) && substr($matches[0], -1) == '[' ? '