Skip to content
Snippets Groups Projects
functions.php 40 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    	$tpl_temp = trim(ob_get_contents());
    	$tpl_redir = str_replace('<pun_footer>', $tpl_temp, $tpl_redir);
    	ob_end_clean();
    	// END SUBST - <pun_footer>
    
    
    	// Close the db connection (and free up any result data)
    	$db->close();
    
    
    	pun_exit($tpl_redir);
    
    }
    
    
    //
    // Display a simple error message
    //
    function error($message, $file, $line, $db_error = false)
    {
    	global $pun_config;
    
    	// Set a default title if the script failed before $pun_config could be populated
    	if (empty($pun_config))
    		$pun_config['o_board_title'] = 'PunBB';
    
    	// Empty output buffer and stop buffering
    	@ob_end_clean();
    
    	// "Restart" output buffering if we are using ob_gzhandler (since the gzip header is already sent)
    	if (!empty($pun_config['o_gzip']) && extension_loaded('zlib') && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false || strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') !== false))
    		ob_start('ob_gzhandler');
    
    ?>
    
    <div id="errorbox">
    	<h2>An error was encountered</h2>
    	<div>
    <?php
    
    	if (defined('PUN_DEBUG'))
    	{
    		echo "\t\t".'<strong>File:</strong> '.$file.'<br />'."\n\t\t".'<strong>Line:</strong> '.$line.'<br /><br />'."\n\t\t".'<strong>PunBB reported</strong>: '.$message."\n";
    
    		if ($db_error)
    		{
    			echo "\t\t".'<br /><br /><strong>Database reported:</strong> '.pun_htmlspecialchars($db_error['error_msg']).(($db_error['error_no']) ? ' (Errno: '.$db_error['error_no'].')' : '')."\n";
    
    			if ($db_error['error_sql'] != '')
    				echo "\t\t".'<br /><br /><strong>Failed query:</strong> '.pun_htmlspecialchars($db_error['error_sql'])."\n";
    		}
    	}
    	else
    		echo "\t\t".'Error: <strong>'.$message.'.</strong>'."\n";
    
    ?>
    	</div>
    </div>
    
    <?php
    
    	// If a database connection was established (before this error) we close it
    	if ($db_error)
    		$GLOBALS['db']->close();
    
    
    	pun_exit();
    
    }
    
    // DEBUG FUNCTIONS BELOW
    
    //
    // Display executed queries (if enabled)
    //
    function display_saved_queries()
    {
    	global $db, $lang_common;
    
    	// Get the queries so that we can print them out
    	$saved_queries = $db->get_saved_queries();
    
    ?>
    
    <div id="debug" class="blocktable">
    	<h2><span><?php echo $lang_common['Debug table'] ?></span></h2>
    	<div class="box">
    		<div class="inbox">
    			<table cellspacing="0">
    			<thead>
    				<tr>
    					<th class="tcl" scope="col">Time (s)</th>
    					<th class="tcr" scope="col">Query</th>
    				</tr>
    			</thead>
    			<tbody>
    <?php
    
    	$query_time_total = 0.0;
    	while (list(, $cur_query) = @each($saved_queries))
    	{
    		$query_time_total += $cur_query[1];
    
    ?>
    				<tr>
    					<td class="tcl"><?php echo ($cur_query[1] != 0) ? $cur_query[1] : '&nbsp;' ?></td>
    					<td class="tcr"><?php echo pun_htmlspecialchars($cur_query[0]) ?></td>
    				</tr>
    <?php
    
    	}
    
    ?>
    				<tr>
    					<td class="tcl" colspan="2">Total query time: <?php echo $query_time_total ?> s</td>
    				</tr>
    			</tbody>
    			</table>
    		</div>
    	</div>
    </div>
    <?php
    
    }
    
    
    //
    // Unset any variables instantiated as a result of register_globals being enabled
    //
    function unregister_globals()
    {
    	$register_globals = @ini_get('register_globals');
    	if ($register_globals === "" || $register_globals === "0" || strtolower($register_globals) === "off")
    		return;
    
    	// Prevent script.php?GLOBALS[foo]=bar
    	if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS']))
    
    		pun_exit('I\'ll have a steak sandwich and... a steak sandwich.');
    
    	
    	// Variables that shouldn't be unset
    	$no_unset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
    
    	// Remove elements in $GLOBALS that are present in any of the superglobals
    	$input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
    	foreach ($input as $k => $v)
    	{
    		if (!in_array($k, $no_unset) && isset($GLOBALS[$k]))
    		{
    			unset($GLOBALS[$k]);
    			unset($GLOBALS[$k]);	// Double unset to circumvent the zend_hash_del_key_or_index hole in PHP <4.4.3 and <5.1.4
    		}
    	}
    }
    
    
    //
    // Dump contents of variable(s)
    //
    function dump()
    {
    	echo '<pre>';
    
    	$num_args = func_num_args();
    
    	for ($i = 0; $i < $num_args; ++$i)
    	{
    		print_r(func_get_arg($i));
    		echo "\n\n";
    	}
    
    	echo '</pre>';
    
    	pun_exit();