punbb/include/cache.php
author Dan
Sun, 06 Apr 2008 00:28:50 -0400
changeset 7 98bbc533541c
permissions -rw-r--r--
Finishing re-add, addremove didn't work last time. Integrated with Enano's template engine properly.

<?php
/***********************************************************************

  Copyright (C) 2002-2008  PunBB.org

  This file is part of PunBB.

  PunBB is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PunBB is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

************************************************************************/


// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
	exit;


//
// Generate the config cache PHP script
//
function generate_config_cache()
{
	global $pun_db;

	// Get the forum config from the DB
	$query = array(
		'SELECT'	=> 'c.*',
		'FROM'		=> 'config AS c'
	);

	($hook = get_hook('ch_qr_get_config')) ? eval($hook) : null;
	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);

	$output = array();
	while ($cur_config_item = $pun_db->fetch_row($result))
		$output[$cur_config_item[0]] = $cur_config_item[1];

	// Output config as PHP code
	$fh = @fopen(PUN_CACHE_DIR.'cache_config.php', 'wb');
	if (!$fh)
		error('Unable to write configuration cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);

	fwrite($fh, '<?php'."\n\n".'define(\'PUN_CONFIG_LOADED\', 1);'."\n\n".'$pun_config = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}


//
// Generate the bans cache PHP script
//
function generate_bans_cache()
{
	global $pun_db;

	// Get the ban list from the DB
	$query = array(
		'SELECT'	=> 'b.*, u.username AS ban_creator_username',
		'FROM'		=> 'bans AS b',
		'JOINS'		=> array(
			array(
				'LEFT JOIN'		=> 'users AS u',
				'ON'			=> 'u.id=b.ban_creator'
			)
		),
		'ORDER BY'	=> 'b.id'
	);

	($hook = get_hook('ch_qr_get_bans')) ? eval($hook) : null;
	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);

	$output = array();
	while ($cur_ban = $pun_db->fetch_assoc($result))
		$output[] = $cur_ban;

	// Output ban list as PHP code
	$fh = @fopen(PUN_CACHE_DIR.'cache_bans.php', 'wb');
	if (!$fh)
		error('Unable to write bans cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);

	fwrite($fh, '<?php'."\n\n".'define(\'PUN_BANS_LOADED\', 1);'."\n\n".'$pun_bans = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}


//
// Generate the ranks cache PHP script
//
function generate_ranks_cache()
{
	global $pun_db;

	// Get the rank list from the DB
	$query = array(
		'SELECT'	=> 'r.*',
		'FROM'		=> 'ranks AS r',
		'ORDER BY'	=> 'r.min_posts'
	);

	($hook = get_hook('ch_qr_get_ranks')) ? eval($hook) : null;
	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);

	$output = array();
	while ($cur_rank = $pun_db->fetch_assoc($result))
		$output[] = $cur_rank;

	// Output ranks list as PHP code
	$fh = @fopen(PUN_CACHE_DIR.'cache_ranks.php', 'wb');
	if (!$fh)
		error('Unable to write ranks cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);

	fwrite($fh, '<?php'."\n\n".'define(\'PUN_RANKS_LOADED\', 1);'."\n\n".'$pun_ranks = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}


//
// Generate the censor cache PHP script
//
function generate_censors_cache()
{
	global $pun_db;

	// Get the censor list from the DB
	$query = array(
		'SELECT'	=> 'c.*',
		'FROM'		=> 'censoring AS c',
		'ORDER BY'	=> 'c.id'
	);

	($hook = get_hook('ch_qr_get_censored_words')) ? eval($hook) : null;
	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);

	$output = array();
	while ($cur_censor = $pun_db->fetch_assoc($result))
		$output[] = $cur_censor;

	// Output censors list as PHP code
	$fh = @fopen(PUN_CACHE_DIR.'cache_censors.php', 'wb');
	if (!$fh)
		error('Unable to write censor cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);

	fwrite($fh, '<?php'."\n\n".'define(\'PUN_CENSORS_LOADED\', 1);'."\n\n".'$pun_censors = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}


//
// Generate quickjump cache PHP scripts
//
function generate_quickjump_cache($group_id = false)
{
	global $pun_db, $lang_common, $pun_url, $pun_config, $pun_user, $base_url;

	// If a group_id was supplied, we generate the quickjump cache for that group only
	if ($group_id !== false)
		$groups[0] = $group_id;
	else
	{
		// A group_id was not supplied, so we generate the quickjump cache for all groups
		$query = array(
			'SELECT'	=> 'g.g_id',
			'FROM'		=> 'groups AS g'
		);

		($hook = get_hook('ch_qr_get_groups')) ? eval($hook) : null;
		$result = $pun_db->query_build($query) or error(__FILE__, __LINE__);
		$num_groups = $pun_db->num_rows($result);

		for ($i = 0; $i < $num_groups; ++$i)
			$groups[] = $pun_db->result($result, $i);
	}

	// Loop through the groups in $groups and output the cache for each of them
	while (list(, $group_id) = @each($groups))
	{
		// Output quickjump as PHP code
		$fh = @fopen(PUN_CACHE_DIR.'cache_quickjump_'.$group_id.'.php', 'wb');
		if (!$fh)
			error('Unable to write quickjump cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);

		$output = '<?php'."\n\n".'if (!defined(\'PUN\')) exit;'."\n".'define(\'PUN_QJ_LOADED\', 1);'."\n".'$forum_id = isset($forum_id) ? $forum_id : 0;'."\n\n".'?>';
		$output .= "\t".'<form id="qjump" method="get" accept-charset="utf-8" action="'.$base_url.'/viewforum.php">'."\n\t\t".'<fieldset>'."\n\t\t\t".'<legend>'.$lang_common['Quick jump legend'].'</legend>'."\n\t\t\t".'<label for="qjump-select"><?php echo $lang_common[\'Jump to\'] ?>'.'</label><br />'."\n\t\t\t".'<span><select id="qjump-select" name="id">'."\n";

		// Get the list of categories and forums from the DB
		$query = array(
			'SELECT'	=> 'c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url',
			'FROM'		=> 'categories AS c',
			'JOINS'		=> array(
				array(
					'INNER JOIN'	=> 'forums AS f',
					'ON'			=> 'c.id=f.cat_id'
				),
				array(
					'LEFT JOIN'		=> 'forum_perms AS fp',
					'ON'			=> '(fp.forum_id=f.id AND fp.group_id='.$group_id.')'
				)
			),
			'WHERE'		=> 'fp.read_forum IS NULL OR fp.read_forum=1',
			'ORDER BY'	=> 'c.disp_position, c.id, f.disp_position'
		);

		($hook = get_hook('ch_qr_get_cats_and_forums')) ? eval($hook) : null;
		$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);

		$cur_category = 0;
		$forum_count = 0;
		while ($cur_forum = $pun_db->fetch_assoc($result))
		{
			if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
			{
				if ($cur_category)
					$output .= "\t\t\t".'</optgroup>'."\n";

				$output .= "\t\t\t".'<optgroup label="'.htmlspecialchars($cur_forum['cat_name']).'">'."\n";
				$cur_category = $cur_forum['cid'];
			}

			$redirect_tag = ($cur_forum['redirect_url'] != '') ? ' &gt;&gt;&gt;' : '';
			$output .= "\t\t\t\t".'<option value="'.$cur_forum['fid'].'"<?php echo ($forum_id == '.$cur_forum['fid'].') ? \' selected="selected"\' : \'\' ?>>'.htmlspecialchars($cur_forum['forum_name']).$redirect_tag.'</option>'."\n";
			$forum_count++;
		}

		$output .= "\t\t\t".'</optgroup>'."\n\t\t\t".'</select>'."\n\t\t\t".'<input type="submit" value="<?php echo $lang_common[\'Go\'] ?>" /></span>'."\n\t\t".'</fieldset>'."\n\t".'</form>'."\n";

		if ($forum_count < 2)
			$output = '<?php'."\n\n".'if (!defined(\'PUN\')) exit;'."\n".'define(\'PUN_QJ_LOADED\', 1);';

		fwrite($fh, $output);

		fclose($fh);
	}
}


//
// Generate the hooks cache PHP script
//
function generate_hooks_cache()
{
	global $pun_db, $pun_config, $base_url;

	// Get the hooks from the DB
	$query = array(
		'SELECT'	=> 'eh.id, eh.code, eh.extension_id',
		'FROM'		=> 'extension_hooks AS eh',
		'JOINS'		=> array(
			array(
				'INNER JOIN'	=> 'extensions AS e',
				'ON'			=> 'e.id=eh.extension_id'
			)
		),
		'WHERE'		=> 'e.disabled=0',
		'ORDER BY'	=> 'eh.installed'
	);

	($hook = get_hook('ch_qr_get_hooks')) ? eval($hook) : null;
	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);

	$output = array();
	while ($cur_hook = $pun_db->fetch_assoc($result))
	{
		$ext_info = '$ext_info = array('."\n".
			'\'id\'		=> \''.$cur_hook['extension_id'].'\','."\n".
			'\'path\'	=> PUN_ROOT.\'extensions/'.$cur_hook['extension_id'].'\','."\n".
			'\'url\'	=> $base_url.\'/extensions/'.$cur_hook['extension_id'].'\');';
		$output[$cur_hook['id']][] = $ext_info."\n\n".$cur_hook['code']."\n";
	}

	// Output hooks as PHP code
	$fh = @fopen(PUN_CACHE_DIR.'cache_hooks.php', 'wb');
	if (!$fh)
		error('Unable to write hooks cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);

	fwrite($fh, '<?php'."\n\n".'define(\'PUN_HOOKS_LOADED\', 1);'."\n\n".'$pun_hooks = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}


//
// Generate the updates cache PHP script
//
function generate_updates_cache()
{
	global $pun_db, $pun_config;

	// Get a list of installed hotfix extensions
	$query = array(
		'SELECT'	=> 'e.id',
		'FROM'		=> 'extensions AS e',
		'WHERE'		=> 'e.id LIKE \'hotfix_%\''
	);

	($hook = get_hook('ch_qr_get_hotfixes')) ? eval($hook) : null;
	$result = $pun_db->query_build($query) or error(__FILE__, __LINE__);
	$num_hotfixes = $pun_db->num_rows($result);

	$hotfixes = array();
	for ($i = 0; $i < $num_hotfixes; ++$i)
		$hotfixes[] = urlencode($pun_db->result($result, $i));

	// Contact the punbb.org updates service
	$result = get_remote_file('http://punbb.org/update/?type=xml&version='.urlencode($pun_config['o_cur_version']).'&hotfixes='.implode(',', $hotfixes), 8);

	// Make sure we got everything we need
	if ($result != null && strpos($result['content'], '</updates>') !== false)
	{
		require PUN_ROOT.'/include/xml.php';

		$output = xml_to_array($result['content']);
		$output = current($output);
		$output['cached'] = time();
		$output['fail'] = false;
	}
	else	// If the update check failed, set the fail flag
		$output = array('cached' => time(), 'fail' => true);

	// This hook could potentially (and responsibly) be used by an extension to do its own little update check
	($hook = get_hook('ch_generate_updates_cache_write')) ? eval($hook) : null;

	// Output update status as PHP code
	$fh = @fopen(PUN_CACHE_DIR.'cache_updates.php', 'wb');
	if (!$fh)
		error('Unable to write updates cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);

	fwrite($fh, '<?php'."\n\n".'if (!defined(\'PUN_UPDATES_LOADED\')) define(\'PUN_UPDATES_LOADED\', 1);'."\n\n".'$pun_updates = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}