'
);
}
break;
}
return $retval;
}
// 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;
}
// Parse bulletin board code in a string, as well as smileys optionally.
function parse_bbc($message, $smileys = true, $cache_id = '', $parse_tags = array())
{
global $txt, $scripturl, $context, $modSettings, $user_info, $smcFunc;
static $bbc_codes = array(), $itemcodes = array(), $no_autolink_tags = array();
static $disabled;
// Don't waste cycles
if ($message === '')
return '';
// 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';
// If we are not doing every tag then we don't cache this run.
if (!empty($parse_tags) && !empty($bbc_codes))
{
$temp_bbc = $bbc_codes;
$bbc_codes = array();
}
// Sift out the bbc for a performance improvement.
if (empty($bbc_codes) || $message === false || !empty($parse_tags))
{
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 replaced 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, '
$1
' elsewise.
disabled_before: used in place of before when disabled. Defaults
to '
' if block_level, '' if not.
disabled_after: used in place of after when disabled. Defaults
to '
' 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.
parsed_tags_allowed: an array restricting what BBC can be in the
parsed_equals parameter, if desired.
*/
$codes = array(
array(
'tag' => 'abbr',
'type' => 'unparsed_equals',
'before' => '',
'after' => '',
'quoted' => 'optional',
'disabled_after' => ' ($1)',
),
array(
'tag' => 'acronym',
'type' => 'unparsed_equals',
'before' => '',
'after' => '',
'quoted' => 'optional',
'disabled_after' => ' ($1)',
),
array(
'tag' => 'anchor',
'type' => 'unparsed_equals',
'test' => '[#]?([A-Za-z][A-Za-z0-9_\-]*)\]',
'before' => '',
'after' => '',
),
array(
'tag' => 'b',
'before' => '',
'after' => '',
),
array(
'tag' => 'bdo',
'type' => 'unparsed_equals',
'before' => '',
'after' => '',
'test' => '(rtl|ltr)\]',
'block_level' => true,
),
array(
'tag' => 'black',
'before' => '',
'after' => '',
),
array(
'tag' => 'blue',
'before' => '',
'after' => '',
),
array(
'tag' => 'br',
'type' => 'closed',
'content' => ' ',
),
array(
'tag' => 'center',
'before' => '
';
}
// Tell the [list] that it needs to close specially.
else
{
// Move the li over, because we're not sure what we'll hit.
$open_tags[count($open_tags) - 1]['after'] = '';
$open_tags[count($open_tags) - 2]['after'] = '';
}
continue;
}
// Implicitly close lists and tables if something other than what's required is in them. This is needed for itemcode.
if ($tag === null && $inside !== null && !empty($inside['require_children']))
{
array_pop($open_tags);
$message = substr($message, 0, $pos) . "\n" . $inside['after'] . "\n" . substr($message, $pos);
$pos += strlen($inside['after']) - 1 + 2;
}
// No tag? Keep looking, then. Silly people using brackets without actual tags.
if ($tag === null)
continue;
// Propagate the list to the child (so wrapping the disallowed tag won't work either.)
if (isset($inside['disallow_children']))
$tag['disallow_children'] = isset($tag['disallow_children']) ? array_unique(array_merge($tag['disallow_children'], $inside['disallow_children'])) : $inside['disallow_children'];
// Is this tag disabled?
if (isset($disabled[$tag['tag']]))
{
if (!isset($tag['disabled_before']) && !isset($tag['disabled_after']) && !isset($tag['disabled_content']))
{
$tag['before'] = !empty($tag['block_level']) ? '
' : '');
}
else
$tag['content'] = $tag['disabled_content'];
}
// The only special case is 'html', which doesn't need to close things.
if (!empty($tag['block_level']) && $tag['tag'] != 'html' && empty($inside['block_level']))
{
$n = count($open_tags) - 1;
while (empty($open_tags[$n]['block_level']) && $n >= 0)
$n--;
// Close all the non block level tags so this tag isn't surrounded by them.
for ($i = count($open_tags) - 1; $i > $n; $i--)
{
$message = substr($message, 0, $pos) . "\n" . $open_tags[$i]['after'] . "\n" . substr($message, $pos);
$pos += strlen($open_tags[$i]['after']) + 2;
$pos1 += strlen($open_tags[$i]['after']) + 2;
// Trim or eat trailing stuff... see comment at the end of the big loop.
if (!empty($open_tags[$i]['block_level']) && substr($message, $pos, 6) == ' ')
$message = substr($message, 0, $pos) . substr($message, $pos + 6);
if (!empty($open_tags[$i]['trim']) && $tag['trim'] != 'inside' && preg_match('~( | |\s)*~', substr($message, $pos), $matches) != 0)
$message = substr($message, 0, $pos) . substr($message, $pos + strlen($matches[0]));
array_pop($open_tags);
}
}
// No type means 'parsed_content'.
if (!isset($tag['type']))
{
// !!! Check for end tag first, so people can say "I like that [i] tag"?
$open_tags[] = $tag;
$message = substr($message, 0, $pos) . "\n" . $tag['before'] . "\n" . substr($message, $pos1);
$pos += strlen($tag['before']) - 1 + 2;
}
// Don't parse the content, just skip it.
elseif ($tag['type'] == 'unparsed_content')
{
$pos2 = stripos($message, '[/' . substr($message, $pos + 1, strlen($tag['tag'])) . ']', $pos1);
if ($pos2 === false)
continue;
$data = substr($message, $pos1, $pos2 - $pos1);
if (!empty($tag['block_level']) && substr($data, 0, 6) == ' ')
$data = substr($data, 6);
if (isset($tag['validate']))
$tag['validate']($tag, $data, $disabled);
$code = strtr($tag['content'], array('$1' => $data));
$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos2 + 3 + strlen($tag['tag']));
$pos += strlen($code) - 1 + 2;
$last_pos = $pos + 1;
}
// Don't parse the content, just skip it.
elseif ($tag['type'] == 'unparsed_equals_content')
{
// The value may be quoted for some tags - check.
if (isset($tag['quoted']))
{
$quoted = substr($message, $pos1, 6) == '"';
if ($tag['quoted'] != 'optional' && !$quoted)
continue;
if ($quoted)
$pos1 += 6;
}
else
$quoted = false;
$pos2 = strpos($message, $quoted == false ? ']' : '"]', $pos1);
if ($pos2 === false)
continue;
$pos3 = stripos($message, '[/' . substr($message, $pos + 1, strlen($tag['tag'])) . ']', $pos2);
if ($pos3 === false)
continue;
$data = array(
substr($message, $pos2 + ($quoted == false ? 1 : 7), $pos3 - ($pos2 + ($quoted == false ? 1 : 7))),
substr($message, $pos1, $pos2 - $pos1)
);
if (!empty($tag['block_level']) && substr($data[0], 0, 6) == ' ')
$data[0] = substr($data[0], 6);
// Validation for my parking, please!
if (isset($tag['validate']))
$tag['validate']($tag, $data, $disabled);
$code = strtr($tag['content'], array('$1' => $data[0], '$2' => $data[1]));
$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos3 + 3 + strlen($tag['tag']));
$pos += strlen($code) - 1 + 2;
}
// A closed tag, with no content or value.
elseif ($tag['type'] == 'closed')
{
$pos2 = strpos($message, ']', $pos);
$message = substr($message, 0, $pos) . "\n" . $tag['content'] . "\n" . substr($message, $pos2 + 1);
$pos += strlen($tag['content']) - 1 + 2;
}
// This one is sorta ugly... :/. Unfortunately, it's needed for flash.
elseif ($tag['type'] == 'unparsed_commas_content')
{
$pos2 = strpos($message, ']', $pos1);
if ($pos2 === false)
continue;
$pos3 = stripos($message, '[/' . substr($message, $pos + 1, strlen($tag['tag'])) . ']', $pos2);
if ($pos3 === false)
continue;
// We want $1 to be the content, and the rest to be csv.
$data = explode(',', ',' . substr($message, $pos1, $pos2 - $pos1));
$data[0] = substr($message, $pos2 + 1, $pos3 - $pos2 - 1);
if (isset($tag['validate']))
$tag['validate']($tag, $data, $disabled);
$code = $tag['content'];
foreach ($data as $k => $d)
$code = strtr($code, array('$' . ($k + 1) => trim($d)));
$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos3 + 3 + strlen($tag['tag']));
$pos += strlen($code) - 1 + 2;
}
// This has parsed content, and a csv value which is unparsed.
elseif ($tag['type'] == 'unparsed_commas')
{
$pos2 = strpos($message, ']', $pos1);
if ($pos2 === false)
continue;
$data = explode(',', substr($message, $pos1, $pos2 - $pos1));
if (isset($tag['validate']))
$tag['validate']($tag, $data, $disabled);
// Fix after, for disabled code mainly.
foreach ($data as $k => $d)
$tag['after'] = strtr($tag['after'], array('$' . ($k + 1) => trim($d)));
$open_tags[] = $tag;
// Replace them out, $1, $2, $3, $4, etc.
$code = $tag['before'];
foreach ($data as $k => $d)
$code = strtr($code, array('$' . ($k + 1) => trim($d)));
$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos2 + 1);
$pos += strlen($code) - 1 + 2;
}
// A tag set to a value, parsed or not.
elseif ($tag['type'] == 'unparsed_equals' || $tag['type'] == 'parsed_equals')
{
// The value may be quoted for some tags - check.
if (isset($tag['quoted']))
{
$quoted = substr($message, $pos1, 6) == '"';
if ($tag['quoted'] != 'optional' && !$quoted)
continue;
if ($quoted)
$pos1 += 6;
}
else
$quoted = false;
$pos2 = strpos($message, $quoted == false ? ']' : '"]', $pos1);
if ($pos2 === false)
continue;
$data = substr($message, $pos1, $pos2 - $pos1);
// Validation for my parking, please!
if (isset($tag['validate']))
$tag['validate']($tag, $data, $disabled);
// For parsed content, we must recurse to avoid security problems.
if ($tag['type'] != 'unparsed_equals')
$data = parse_bbc($data, !empty($tag['parsed_tags_allowed']) ? false : true, '', !empty($tag['parsed_tags_allowed']) ? $tag['parsed_tags_allowed'] : array());
$tag['after'] = strtr($tag['after'], array('$1' => $data));
$open_tags[] = $tag;
$code = strtr($tag['before'], array('$1' => $data));
$message = substr($message, 0, $pos) . "\n" . $code . "\n" . substr($message, $pos2 + ($quoted == false ? 1 : 7));
$pos += strlen($code) - 1 + 2;
}
// If this is block level, eat any breaks after it.
if (!empty($tag['block_level']) && substr($message, $pos + 1, 6) == ' ')
$message = substr($message, 0, $pos + 1) . substr($message, $pos + 7);
// Are we trimming outside this tag?
if (!empty($tag['trim']) && $tag['trim'] != 'outside' && preg_match('~( | |\s)*~', substr($message, $pos + 1), $matches) != 0)
$message = substr($message, 0, $pos + 1) . substr($message, $pos + 1 + strlen($matches[0]));
}
// Close any remaining tags.
while ($tag = array_pop($open_tags))
$message .= "\n" . $tag['after'] . "\n";
// Parse the smileys within the parts where it can be done safely.
if ($smileys === true)
{
$message_parts = explode("\n", $message);
for ($i = 0, $n = count($message_parts); $i < $n; $i += 2)
parsesmileys($message_parts[$i]);
$message = implode('', $message_parts);
}
// No smileys, just get rid of the markers.
else
$message = strtr($message, array("\n" => ''));
if (substr($message, 0, 1) == ' ')
$message = ' ' . substr($message, 1);
// Cleanup whitespace.
$message = strtr($message, array(' ' => ' ', "\r" => '', "\n" => ' ', ' ' => ' ', '
' => "\n"));
// Cache the output if it took some time...
if (isset($cache_key, $cache_t) && array_sum(explode(' ', microtime())) - array_sum(explode(' ', $cache_t)) > 0.05)
cache_put_data($cache_key, $message, 240);
// If this was a force parse revert if needed.
if (!empty($parse_tags))
{
if (empty($temp_bbc))
$bbc_codes = array();
else
{
$bbc_codes = $temp_bbc;
unset($temp_bbc);
}
}
return $message;
}
// Parse smileys in the passed message.
function parsesmileys(&$message)
{
global $modSettings, $txt, $user_info, $context, $smcFunc;
static $smileyPregSearch = array(), $smileyPregReplacements = array();
// No smiley set at all?!
if ($user_info['smiley_set'] == 'none')
return;
// If the smiley array hasn't been set, do it now.
if (empty($smileyPregSearch))
{
// Use the default smileys if it is disabled. (better for "portability" of smileys.)
if (empty($modSettings['smiley_enable']))
{
$smileysfrom = array('>:D', ':D', '::)', '>:(', ':))', ':)', ';)', ';D', ':(', ':o', '8)', ':P', '???', ':-[', ':-X', ':-*', ':\'(', ':-\\', '^-^', 'O0', 'C:-)', '0:)');
$smileysto = array('evil.gif', 'cheesy.gif', 'rolleyes.gif', 'angry.gif', 'laugh.gif', 'smiley.gif', 'wink.gif', 'grin.gif', 'sad.gif', 'shocked.gif', 'cool.gif', 'tongue.gif', 'huh.gif', 'embarrassed.gif', 'lipsrsealed.gif', 'kiss.gif', 'cry.gif', 'undecided.gif', 'azn.gif', 'afro.gif', 'police.gif', 'angel.gif');
$smileysdescs = array('', $txt['icon_cheesy'], $txt['icon_rolleyes'], $txt['icon_angry'], '', $txt['icon_smiley'], $txt['icon_wink'], $txt['icon_grin'], $txt['icon_sad'], $txt['icon_shocked'], $txt['icon_cool'], $txt['icon_tongue'], $txt['icon_huh'], $txt['icon_embarrassed'], $txt['icon_lips'], $txt['icon_kiss'], $txt['icon_cry'], $txt['icon_undecided'], '', '', '', '');
}
else
{
// Load the smileys in reverse order by length so they don't get parsed wrong.
if (($temp = cache_get_data('parsing_smileys', 480)) == null)
{
$result = $smcFunc['db_query']('', '
SELECT code, filename, description
FROM {db_prefix}smileys',
array(
)
);
$smileysfrom = array();
$smileysto = array();
$smileysdescs = array();
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$smileysfrom[] = $row['code'];
$smileysto[] = $row['filename'];
$smileysdescs[] = $row['description'];
}
$smcFunc['db_free_result']($result);
cache_put_data('parsing_smileys', array($smileysfrom, $smileysto, $smileysdescs), 480);
}
else
list ($smileysfrom, $smileysto, $smileysdescs) = $temp;
}
// The non-breaking-space is a complex thing...
$non_breaking_space = $context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{A0}' : "\xC2\xA0") : '\xA0';
// This smiley regex makes sure it doesn't parse smileys within code tags (so [url=mailto:David@bla.com] doesn't parse the :D smiley)
$smileyPregReplacements = array();
$searchParts = array();
for ($i = 0, $n = count($smileysfrom); $i < $n; $i++)
{
$smileyCode = '';
$smileyPregReplacements[$smileysfrom[$i]] = $smileyCode;
$smileyPregReplacements[htmlspecialchars($smileysfrom[$i], ENT_QUOTES)] = $smileyCode;
$searchParts[] = preg_quote($smileysfrom[$i], '~');
$searchParts[] = preg_quote(htmlspecialchars($smileysfrom[$i], ENT_QUOTES), '~');
}
$smileyPregSearch = '~(?<=[>:\?\.\s' . $non_breaking_space . '[\]()*\\\;]|^)(' . implode('|', $searchParts) . ')(?=[^[:alpha:]0-9]|$)~' . ($context['utf8'] ? 'u' : '');
}
// Replace away!
// TODO: When SMF supports only PHP 5.3+, we can change this to "uses" keyword and simplify this.
$context['smiley_replacements'] = $smileyPregReplacements;
$message = preg_replace_callback($smileyPregSearch, 'smileyPregReplaceCallback', $message);
}
// This allows use to do delayed argument binding and bring in the replacement variables for some preg replacements.
function pregReplaceCurry($func, $arity)
{
return create_function('', "
\$args = func_get_args();
if(count(\$args) >= $arity)
return call_user_func_array('$func', \$args);
\$args = var_export(\$args, 1);
return create_function('','
\$a = func_get_args();
\$z = ' . \$args . ';
\$a = array_merge(\$z,\$a);
return call_user_func_array(\'$func\', \$a);
');
");
}
// Our callback that does the actual smiley replacements.
function smileyPregReplaceCallback($matches)
{
global $context;
return $context['smiley_replacements'][$matches[1]];
}
// Highlight any code...
function highlight_php_code($code)
{
global $context;
// Remove special characters.
$code = un_htmlspecialchars(strtr($code, array(' ' => "\n", "\t" => 'SMF_TAB();', '[' => '[')));
$oldlevel = error_reporting(0);
// It's easier in 4.2.x+.
if (@version_compare(PHP_VERSION, '4.2.0') == -1)
{
ob_start();
@highlight_string($code);
$buffer = str_replace(array("\n", "\r"), '', ob_get_contents());
ob_end_clean();
}
else
$buffer = str_replace(array("\n", "\r"), '', @highlight_string($code, true));
error_reporting($oldlevel);
// Yes, I know this is kludging it, but this is the best way to preserve tabs from PHP :P.
$buffer = preg_replace('~SMF_TAB(?:(?:font|span)><(?:font color|span style)="[^"]*?">)?\\(\\);~', '
' . "\t" . '
', $buffer);
return strtr($buffer, array('\'' => ''', '' => '', '' => ''));
}
// Put this user in the online log.
function writeLog($force = false)
{
global $user_info, $user_settings, $context, $modSettings, $settings, $topic, $board, $smcFunc, $sourcedir;
// If we are showing who is viewing a topic, let's see if we are, and force an update if so - to make it accurate.
if (!empty($settings['display_who_viewing']) && ($topic || $board))
{
// Take the opposite approach!
$force = true;
// Don't update for every page - this isn't wholly accurate but who cares.
if ($topic)
{
if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic)
$force = false;
$_SESSION['last_topic_id'] = $topic;
}
}
// Are they a spider we should be tracking? Mode = 1 gets tracked on its spider check...
if (!empty($user_info['possibly_robot']) && !empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1)
{
require_once($sourcedir . '/ManageSearchEngines.php');
logSpider();
}
// Don't mark them as online more than every so often.
if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= (time() - 8) && !$force)
return;
if (!empty($modSettings['who_enabled']))
{
$serialized = $_GET + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']);
// In the case of a dlattach action, session_var may not be set.
if (!isset($context['session_var']))
$context['session_var'] = $_SESSION['session_var'];
unset($serialized['sesc'], $serialized[$context['session_var']]);
$serialized = serialize($serialized);
}
else
$serialized = '';
// Guests use 0, members use their session ID.
$session_id = $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id();
// Grab the last all-of-SMF-specific log_online deletion time.
$do_delete = cache_get_data('log_online-update', 30) < time() - 30;
// If the last click wasn't a long time ago, and there was a last click...
if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - $modSettings['lastActive'] * 20)
{
if ($do_delete)
{
$smcFunc['db_query']('delete_log_online_interval', '
DELETE FROM {db_prefix}log_online
WHERE log_time < {int:log_time}
AND session != {string:session}',
array(
'log_time' => time() - $modSettings['lastActive'] * 60,
'session' => $session_id,
)
);
// Cache when we did it last.
cache_put_data('log_online-update', time(), 30);
}
$smcFunc['db_query']('', '
UPDATE {db_prefix}log_online
SET log_time = {int:log_time}, ip = IFNULL(INET_ATON({string:ip}), 0), url = {string:url}
WHERE session = {string:session}',
array(
'log_time' => time(),
'ip' => $user_info['ip'],
'url' => $serialized,
'session' => $session_id,
)
);
// Guess it got deleted.
if ($smcFunc['db_affected_rows']() == 0)
$_SESSION['log_time'] = 0;
}
else
$_SESSION['log_time'] = 0;
// Otherwise, we have to delete and insert.
if (empty($_SESSION['log_time']))
{
if ($do_delete || !empty($user_info['id']))
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}log_online
WHERE ' . ($do_delete ? 'log_time < {int:log_time}' : '') . ($do_delete && !empty($user_info['id']) ? ' OR ' : '') . (empty($user_info['id']) ? '' : 'id_member = {int:current_member}'),
array(
'current_member' => $user_info['id'],
'log_time' => time() - $modSettings['lastActive'] * 60,
)
);
$smcFunc['db_insert']($do_delete ? 'ignore' : 'replace',
'{db_prefix}log_online',
array('session' => 'string', 'id_member' => 'int', 'id_spider' => 'int', 'log_time' => 'int', 'ip' => 'raw', 'url' => 'string'),
array($session_id, $user_info['id'], empty($_SESSION['id_robot']) ? 0 : $_SESSION['id_robot'], time(), 'IFNULL(INET_ATON(\'' . $user_info['ip'] . '\'), 0)', $serialized),
array('session')
);
}
// Mark your session as being logged.
$_SESSION['log_time'] = time();
// Well, they are online now.
if (empty($_SESSION['timeOnlineUpdated']))
$_SESSION['timeOnlineUpdated'] = time();
// Set their login time, if not already done within the last minute.
if (SMF != 'SSI' && !empty($user_info['last_login']) && $user_info['last_login'] < time() - 60)
{
// Don't count longer than 15 minutes.
if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15)
$_SESSION['timeOnlineUpdated'] = time();
$user_settings['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
updateMemberData($user_info['id'], array('last_login' => time(), 'member_ip' => $user_info['ip'], 'member_ip2' => $_SERVER['BAN_CHECK_IP'], 'total_time_logged_in' => $user_settings['total_time_logged_in']));
if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60);
$user_info['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
$_SESSION['timeOnlineUpdated'] = time();
}
}
// Make sure the browser doesn't come back and repost the form data. Should be used whenever anything is posted.
function redirectexit($setLocation = '', $refresh = false)
{
global $scripturl, $context, $modSettings, $db_show_debug, $db_cache;
// In case we have mail to send, better do that - as obExit doesn't always quite make it...
if (!empty($context['flush_mail']))
AddMailQueue(true);
$add = preg_match('~^(ftp|http)[s]?://~', $setLocation) == 0 && substr($setLocation, 0, 6) != 'about:';
if (WIRELESS)
{
// Add the scripturl on if needed.
if ($add)
$setLocation = $scripturl . '?' . $setLocation;
$char = strpos($setLocation, '?') === false ? '?' : ';';
if (strpos($setLocation, '#') !== false)
$setLocation = strtr($setLocation, array('#' => $char . WIRELESS_PROTOCOL . '#'));
else
$setLocation .= $char . WIRELESS_PROTOCOL;
}
elseif ($add)
$setLocation = $scripturl . ($setLocation != '' ? '?' . $setLocation : '');
// Put the session ID in.
if (defined('SID') && SID != '')
$setLocation = preg_replace('/^' . preg_quote($scripturl, '/') . '(?!\?' . preg_quote(SID, '/') . ')\\??/', $scripturl . '?' . SID . ';', $setLocation);
// Keep that debug in their for template debugging!
elseif (isset($_GET['debug']))
$setLocation = preg_replace('/^' . preg_quote($scripturl, '/') . '\\??/', $scripturl . '?debug;', $setLocation);
if (!empty($modSettings['queryless_urls']) && (empty($context['server']['is_cgi']) || @ini_get('cgi.fix_pathinfo') == 1 || @get_cfg_var('cgi.fix_pathinfo') == 1) && (!empty($context['server']['is_apache']) || !empty($context['server']['is_lighttpd'])))
{
if (defined('SID') && SID != '')
$setLocation = preg_replace_callback('~"' . preg_quote($scripturl, '/') . '\?(?:' . SID . '(?:;|&|&))((?:board|topic)=[^#]+?)(#[^"]*?)?$~', 'fix_redirect_sid__preg_callback', $setLocation);
else
$setLocation = preg_replace_callback('~"' . preg_quote($scripturl, '/') . '\?((?:board|topic)=[^#"]+?)(#[^"]*?)?$~', 'fix_redirect_path__preg_callback', $setLocation);
}
// Maybe integrations want to change where we are heading?
call_integration_hook('integrate_redirect', array(&$setLocation, &$refresh));
// We send a Refresh header only in special cases because Location looks better. (and is quicker...)
if ($refresh && !WIRELESS)
header('Refresh: 0; URL=' . strtr($setLocation, array(' ' => '%20')));
else
header('Location: ' . str_replace(' ', '%20', $setLocation));
// Debugging.
if (isset($db_show_debug) && $db_show_debug === true)
$_SESSION['debug_redirect'] = $db_cache;
obExit(false);
}
// Ends execution. Takes care of template loading and remembering the previous URL.
function obExit($header = null, $do_footer = null, $from_index = false, $from_fatal_error = false)
{
global $context, $settings, $modSettings, $txt, $smcFunc;
static $header_done = false, $footer_done = false, $level = 0, $has_fatal_error = false;
// Attempt to prevent a recursive loop.
++$level;
if ($level > 1 && !$from_fatal_error && !$has_fatal_error)
exit;
if ($from_fatal_error)
$has_fatal_error = true;
// Clear out the stat cache.
trackStats();
// If we have mail to send, send it.
if (!empty($context['flush_mail']))
AddMailQueue(true);
$do_header = $header === null ? !$header_done : $header;
if ($do_footer === null)
$do_footer = $do_header;
// Has the template/header been done yet?
if ($do_header)
{
// Was the page title set last minute? Also update the HTML safe one.
if (!empty($context['page_title']) && empty($context['page_title_html_safe']))
$context['page_title_html_safe'] = $smcFunc['htmlspecialchars'](un_htmlspecialchars($context['page_title']));
// Start up the session URL fixer.
ob_start('ob_sessrewrite');
if (!empty($settings['output_buffers']) && is_string($settings['output_buffers']))
$buffers = explode(',', $settings['output_buffers']);
elseif (!empty($settings['output_buffers']))
$buffers = $settings['output_buffers'];
else
$buffers = array();
if (isset($modSettings['integrate_buffer']))
$buffers = array_merge(explode(',', $modSettings['integrate_buffer']), $buffers);
if (!empty($buffers))
foreach ($buffers as $function)
{
$function = trim($function);
$call = strpos($function, '::') !== false ? explode('::', $function) : $function;
// Is it valid?
if (is_callable($call))
ob_start($call);
}
// Display the screen in the logical order.
template_header();
$header_done = true;
}
if ($do_footer)
{
if (WIRELESS && !isset($context['sub_template']))
fatal_lang_error('wireless_error_notyet', false);
// Just show the footer, then.
loadSubTemplate(isset($context['sub_template']) ? $context['sub_template'] : 'main');
// Anything special to put out?
if (!empty($context['insert_after_template']) && !isset($_REQUEST['xml']))
echo $context['insert_after_template'];
// Just so we don't get caught in an endless loop of errors from the footer...
if (!$footer_done)
{
$footer_done = true;
template_footer();
// (since this is just debugging... it's okay that it's after