<?php
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
*/
/**
* Outputs a page directly to the browser, parsing anything which needs to be parsed.
*
* @param string $contents The contents of the page.
*/
function output_page($contents)
{
global $db, $lang, $theme, $templates, $plugins, $mybb;
global $debug, $templatecache, $templatelist, $maintimer, $globaltime, $parsetime;
$contents = $plugins->run_hooks("pre_parse_page", $contents);
$contents = parse_page($contents);
$totaltime = format_time_duration($maintimer->stop());
$contents = $plugins->run_hooks("pre_output_page", $contents);
if($mybb->usergroup['cancp'] == 1 || $mybb->dev_mode == 1)
{
if($mybb->settings['extraadmininfo'] != 0)
{
$phptime = $maintimer->totaltime - $db->query_time;
$query_time = $db->query_time;
if($maintimer->totaltime > 0)
{
$percentphp = number_format((($phptime/$maintimer->totaltime) * 100), 2);
$percentsql = number_format((($query_time/$maintimer->totaltime) * 100), 2);
}
else
{
// if we've got a super fast script... all we can do is assume something
$percentphp = 0;
$percentsql = 0;
}
$serverload = get_server_load();
if(my_strpos(getenv("REQUEST_URI"), "?"))
{
$debuglink = htmlspecialchars_uni(getenv("REQUEST_URI")) . "&debug=1";
}
else
{
$debuglink = htmlspecialchars_uni(getenv("REQUEST_URI")) . "?debug=1";
}
$memory_usage = get_memory_usage();
if($memory_usage)
{
$memory_usage = $lang->sprintf($lang->debug_memory_usage, get_friendly_size($memory_usage));
}
else
{
$memory_usage = '';
}
// MySQLi is still MySQL, so present it that way to the user
$database_server = $db->short_title;
if($database_server == 'MySQLi')
{
$database_server = 'MySQL';
}
$generated_in = $lang->sprintf($lang->debug_generated_in, $totaltime);
$debug_weight = $lang->sprintf($lang->debug_weight, $percentphp, $percentsql, $database_server);
$sql_queries = $lang->sprintf($lang->debug_sql_queries, $db->query_count);
$server_load = $lang->sprintf($lang->debug_server_load, $serverload);
eval("\$debugstuff = \"".$templates->get("debug_summary")."\";");
$contents = str_replace("<debugstuff>", $debugstuff, $contents);
}
if($mybb->debug_mode == true)
{
debug_page();
}
}
$contents = str_replace("<debugstuff>", "", $contents);
if($mybb->settings['gzipoutput'] == 1)
{
$contents = gzip_encode($contents, $mybb->settings['gziplevel']);
}
@header("Content-type: text/html; charset={$lang->settings['charset']}");
echo $contents;
$plugins->run_hooks("post_output_page");
}
/**
* Adds a function or class to the list of code to run on shutdown.
*
* @param string|array $name The name of the function.
* @param mixed $arguments Either an array of arguments for the function or one argument
* @return boolean True if function exists, otherwise false.
*/
function add_shutdown($name, $arguments=array())
{
global $shutdown_functions;
if(!is_array($shutdown_functions))
{
$shutdown_functions = array();
}
if(!is_array($arguments))
{
$arguments = array($arguments);
}
if(is_array($name) && method_exists($name[0], $name[1]))
{
$shutdown_functions[] = array('function' => $name, 'arguments' => $arguments);
return true;
}
else if(!is_array($name) && function_exists($name))
{
$shutdown_functions[] = array('function' => $name, 'arguments' => $arguments);
return true;
}
return false;
}
/**
* Runs the shutdown items after the page has been sent to the browser.
*
*/
function run_shutdown()
{
global $config, $db, $cache, $plugins, $error_handler, $shutdown_functions, $shutdown_queries, $done_shutdown, $mybb;
if($done_shutdown == true || !$config || (isset($error_handler) && $error_handler->has_errors))
{
return;
}
if(empty($shutdown_queries) && empty($shutdown_functions))
{
// Nothing to do
return;
}
// Missing the core? Build
if(!is_object($mybb))
{
require_once MYBB_ROOT."inc/class_core.php";
$mybb = new MyBB;
// Load the settings
require MYBB_ROOT."inc/settings.php";
$mybb->settings = &$settings;
}
// If our DB has been deconstructed already (bad PHP 5.2.0), reconstruct
if(!is_object($db))
{
if(!isset($config) || empty($config['database']['type']))
{
require MYBB_ROOT."inc/config.php";
}
if(isset($config))
{
// Load DB interface
require_once MYBB_ROOT."inc/db_base.php";
require_once MYBB_ROOT."inc/db_".$config['database']['type'].".php";
switch($config['database']['type'])
{
case "sqlite":
$db = new DB_SQLite;
break;
case "pgsql":
$db = new DB_PgSQL;
break;
case "mysqli":
$db = new DB_MySQLi;
break;
default:
$db = new DB_MySQL;
}
$db->connect($config['database']);
if(!defined("TABLE_PREFIX"))
{
define("TABLE_PREFIX", $config['database']['table_prefix']);
}
$db->set_table_prefix(TABLE_PREFIX);
}
}
// Cache object deconstructed? reconstruct
if(!is_object($cache))
{
require_once MYBB_ROOT."inc/class_datacache.php";
$cache = new datacache;
$cache->cache();
}
// And finally.. plugins
if(!is_object($plugins) && !defined("NO_PLUGINS") && !($mybb->settings['no_plugins'] == 1))
{
require_once MYBB_ROOT."inc/class_plugins.php";
$plugins = new pluginSystem;
$plugins->load();
}
// We have some shutdown queries needing to be run
if(is_array($shutdown_queries))
{
// Loop through and run them all
foreach($shutdown_queries as $query)
{
$db->write_query($query);
}
}
// Run any shutdown functions if we have them
if(is_array($shutdown_functions))
{
foreach($shutdown_functions as $function)
{
call_user_func_array($function['function'], $function['arguments']);
}
}
$done_shutdown = true;
}
/**
* Sends a specified amount of messages from the mail queue
*
* @param int $count The number of messages to send (Defaults to 10)
*/
function send_mail_queue($count=10)
{
global $db, $cache, $plugins;
$plugins->run_hooks("send_mail_queue_start");
// Check to see if the mail queue has messages needing to be sent
$mailcache = $cache->read("mailqueue");
if($mailcache['queue_size'] > 0 && ($mailcache['locked'] == 0 || $mailcache['locked'] < TIME_NOW-300))
{
// Lock the queue so no other messages can be sent whilst these are (for popular boards)
$cache->update_mailqueue(0, TIME_NOW);
// Fetch emails for this page view - and send them
$query = $db->simple_select("mailqueue", "*", "", array("order_by" => "mid", "order_dir" => "asc", "limit_start" => 0, "limit" => $count));
while($email = $db->fetch_array($query))
{
// Delete the message from the queue
$db->delete_query("mailqueue", "mid='{$email['mid']}'");
if($db->affected_rows() == 1)
{
my_mail($email['mailto'], $email['subject'], $email['message'], $email['mailfrom'], "", $email['headers'], true);
}
}
// Update the mailqueue cache and remove the lock
$cache->update_mailqueue(TIME_NOW, 0);
}
$plugins->run_hooks("send_mail_queue_end");
}
/**
* Parses the contents of a page before outputting it.
*
* @param string $contents The contents of the page.
* @return string The parsed page.
*/
function parse_page($contents)
{
global $lang, $theme, $mybb, $htmldoctype, $archive_url, $error_handler;
$contents = str_replace('<navigation>', build_breadcrumb(), $contents);
$contents = str_replace('<archive_url>', $archive_url, $contents);
if($htmldoctype)
{
$contents = $htmldoctype.$contents;
}
else
{
$contents = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".$contents;
}
$contents = str_replace("<html", "<html xmlns=\"http://www.w3.org/1999/xhtml\"", $contents);
if($lang->settings['rtl'] == 1)
{
$contents = str_replace("<html", "<html dir=\"rtl\"", $co