punbb/include/cache.php
changeset 7 98bbc533541c
equal deleted inserted replaced
6:5e1f1e916419 7:98bbc533541c
       
     1 <?php
       
     2 /***********************************************************************
       
     3 
       
     4   Copyright (C) 2002-2008  PunBB.org
       
     5 
       
     6   This file is part of PunBB.
       
     7 
       
     8   PunBB is free software; you can redistribute it and/or modify it
       
     9   under the terms of the GNU General Public License as published
       
    10   by the Free Software Foundation; either version 2 of the License,
       
    11   or (at your option) any later version.
       
    12 
       
    13   PunBB is distributed in the hope that it will be useful, but
       
    14   WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16   GNU General Public License for more details.
       
    17 
       
    18   You should have received a copy of the GNU General Public License
       
    19   along with this program; if not, write to the Free Software
       
    20   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
       
    21   MA  02111-1307  USA
       
    22 
       
    23 ************************************************************************/
       
    24 
       
    25 
       
    26 // Make sure no one attempts to run this script "directly"
       
    27 if (!defined('PUN'))
       
    28 	exit;
       
    29 
       
    30 
       
    31 //
       
    32 // Generate the config cache PHP script
       
    33 //
       
    34 function generate_config_cache()
       
    35 {
       
    36 	global $pun_db;
       
    37 
       
    38 	// Get the forum config from the DB
       
    39 	$query = array(
       
    40 		'SELECT'	=> 'c.*',
       
    41 		'FROM'		=> 'config AS c'
       
    42 	);
       
    43 
       
    44 	($hook = get_hook('ch_qr_get_config')) ? eval($hook) : null;
       
    45 	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);
       
    46 
       
    47 	$output = array();
       
    48 	while ($cur_config_item = $pun_db->fetch_row($result))
       
    49 		$output[$cur_config_item[0]] = $cur_config_item[1];
       
    50 
       
    51 	// Output config as PHP code
       
    52 	$fh = @fopen(PUN_CACHE_DIR.'cache_config.php', 'wb');
       
    53 	if (!$fh)
       
    54 		error('Unable to write configuration cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
       
    55 
       
    56 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_CONFIG_LOADED\', 1);'."\n\n".'$pun_config = '.var_export($output, true).';'."\n\n".'?>');
       
    57 
       
    58 	fclose($fh);
       
    59 }
       
    60 
       
    61 
       
    62 //
       
    63 // Generate the bans cache PHP script
       
    64 //
       
    65 function generate_bans_cache()
       
    66 {
       
    67 	global $pun_db;
       
    68 
       
    69 	// Get the ban list from the DB
       
    70 	$query = array(
       
    71 		'SELECT'	=> 'b.*, u.username AS ban_creator_username',
       
    72 		'FROM'		=> 'bans AS b',
       
    73 		'JOINS'		=> array(
       
    74 			array(
       
    75 				'LEFT JOIN'		=> 'users AS u',
       
    76 				'ON'			=> 'u.id=b.ban_creator'
       
    77 			)
       
    78 		),
       
    79 		'ORDER BY'	=> 'b.id'
       
    80 	);
       
    81 
       
    82 	($hook = get_hook('ch_qr_get_bans')) ? eval($hook) : null;
       
    83 	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);
       
    84 
       
    85 	$output = array();
       
    86 	while ($cur_ban = $pun_db->fetch_assoc($result))
       
    87 		$output[] = $cur_ban;
       
    88 
       
    89 	// Output ban list as PHP code
       
    90 	$fh = @fopen(PUN_CACHE_DIR.'cache_bans.php', 'wb');
       
    91 	if (!$fh)
       
    92 		error('Unable to write bans cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
       
    93 
       
    94 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_BANS_LOADED\', 1);'."\n\n".'$pun_bans = '.var_export($output, true).';'."\n\n".'?>');
       
    95 
       
    96 	fclose($fh);
       
    97 }
       
    98 
       
    99 
       
   100 //
       
   101 // Generate the ranks cache PHP script
       
   102 //
       
   103 function generate_ranks_cache()
       
   104 {
       
   105 	global $pun_db;
       
   106 
       
   107 	// Get the rank list from the DB
       
   108 	$query = array(
       
   109 		'SELECT'	=> 'r.*',
       
   110 		'FROM'		=> 'ranks AS r',
       
   111 		'ORDER BY'	=> 'r.min_posts'
       
   112 	);
       
   113 
       
   114 	($hook = get_hook('ch_qr_get_ranks')) ? eval($hook) : null;
       
   115 	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);
       
   116 
       
   117 	$output = array();
       
   118 	while ($cur_rank = $pun_db->fetch_assoc($result))
       
   119 		$output[] = $cur_rank;
       
   120 
       
   121 	// Output ranks list as PHP code
       
   122 	$fh = @fopen(PUN_CACHE_DIR.'cache_ranks.php', 'wb');
       
   123 	if (!$fh)
       
   124 		error('Unable to write ranks cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
       
   125 
       
   126 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_RANKS_LOADED\', 1);'."\n\n".'$pun_ranks = '.var_export($output, true).';'."\n\n".'?>');
       
   127 
       
   128 	fclose($fh);
       
   129 }
       
   130 
       
   131 
       
   132 //
       
   133 // Generate the censor cache PHP script
       
   134 //
       
   135 function generate_censors_cache()
       
   136 {
       
   137 	global $pun_db;
       
   138 
       
   139 	// Get the censor list from the DB
       
   140 	$query = array(
       
   141 		'SELECT'	=> 'c.*',
       
   142 		'FROM'		=> 'censoring AS c',
       
   143 		'ORDER BY'	=> 'c.id'
       
   144 	);
       
   145 
       
   146 	($hook = get_hook('ch_qr_get_censored_words')) ? eval($hook) : null;
       
   147 	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);
       
   148 
       
   149 	$output = array();
       
   150 	while ($cur_censor = $pun_db->fetch_assoc($result))
       
   151 		$output[] = $cur_censor;
       
   152 
       
   153 	// Output censors list as PHP code
       
   154 	$fh = @fopen(PUN_CACHE_DIR.'cache_censors.php', 'wb');
       
   155 	if (!$fh)
       
   156 		error('Unable to write censor cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
       
   157 
       
   158 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_CENSORS_LOADED\', 1);'."\n\n".'$pun_censors = '.var_export($output, true).';'."\n\n".'?>');
       
   159 
       
   160 	fclose($fh);
       
   161 }
       
   162 
       
   163 
       
   164 //
       
   165 // Generate quickjump cache PHP scripts
       
   166 //
       
   167 function generate_quickjump_cache($group_id = false)
       
   168 {
       
   169 	global $pun_db, $lang_common, $pun_url, $pun_config, $pun_user, $base_url;
       
   170 
       
   171 	// If a group_id was supplied, we generate the quickjump cache for that group only
       
   172 	if ($group_id !== false)
       
   173 		$groups[0] = $group_id;
       
   174 	else
       
   175 	{
       
   176 		// A group_id was not supplied, so we generate the quickjump cache for all groups
       
   177 		$query = array(
       
   178 			'SELECT'	=> 'g.g_id',
       
   179 			'FROM'		=> 'groups AS g'
       
   180 		);
       
   181 
       
   182 		($hook = get_hook('ch_qr_get_groups')) ? eval($hook) : null;
       
   183 		$result = $pun_db->query_build($query) or error(__FILE__, __LINE__);
       
   184 		$num_groups = $pun_db->num_rows($result);
       
   185 
       
   186 		for ($i = 0; $i < $num_groups; ++$i)
       
   187 			$groups[] = $pun_db->result($result, $i);
       
   188 	}
       
   189 
       
   190 	// Loop through the groups in $groups and output the cache for each of them
       
   191 	while (list(, $group_id) = @each($groups))
       
   192 	{
       
   193 		// Output quickjump as PHP code
       
   194 		$fh = @fopen(PUN_CACHE_DIR.'cache_quickjump_'.$group_id.'.php', 'wb');
       
   195 		if (!$fh)
       
   196 			error('Unable to write quickjump cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
       
   197 
       
   198 		$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".'?>';
       
   199 		$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";
       
   200 
       
   201 		// Get the list of categories and forums from the DB
       
   202 		$query = array(
       
   203 			'SELECT'	=> 'c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url',
       
   204 			'FROM'		=> 'categories AS c',
       
   205 			'JOINS'		=> array(
       
   206 				array(
       
   207 					'INNER JOIN'	=> 'forums AS f',
       
   208 					'ON'			=> 'c.id=f.cat_id'
       
   209 				),
       
   210 				array(
       
   211 					'LEFT JOIN'		=> 'forum_perms AS fp',
       
   212 					'ON'			=> '(fp.forum_id=f.id AND fp.group_id='.$group_id.')'
       
   213 				)
       
   214 			),
       
   215 			'WHERE'		=> 'fp.read_forum IS NULL OR fp.read_forum=1',
       
   216 			'ORDER BY'	=> 'c.disp_position, c.id, f.disp_position'
       
   217 		);
       
   218 
       
   219 		($hook = get_hook('ch_qr_get_cats_and_forums')) ? eval($hook) : null;
       
   220 		$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);
       
   221 
       
   222 		$cur_category = 0;
       
   223 		$forum_count = 0;
       
   224 		while ($cur_forum = $pun_db->fetch_assoc($result))
       
   225 		{
       
   226 			if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
       
   227 			{
       
   228 				if ($cur_category)
       
   229 					$output .= "\t\t\t".'</optgroup>'."\n";
       
   230 
       
   231 				$output .= "\t\t\t".'<optgroup label="'.htmlspecialchars($cur_forum['cat_name']).'">'."\n";
       
   232 				$cur_category = $cur_forum['cid'];
       
   233 			}
       
   234 
       
   235 			$redirect_tag = ($cur_forum['redirect_url'] != '') ? ' &gt;&gt;&gt;' : '';
       
   236 			$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";
       
   237 			$forum_count++;
       
   238 		}
       
   239 
       
   240 		$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";
       
   241 
       
   242 		if ($forum_count < 2)
       
   243 			$output = '<?php'."\n\n".'if (!defined(\'PUN\')) exit;'."\n".'define(\'PUN_QJ_LOADED\', 1);';
       
   244 
       
   245 		fwrite($fh, $output);
       
   246 
       
   247 		fclose($fh);
       
   248 	}
       
   249 }
       
   250 
       
   251 
       
   252 //
       
   253 // Generate the hooks cache PHP script
       
   254 //
       
   255 function generate_hooks_cache()
       
   256 {
       
   257 	global $pun_db, $pun_config, $base_url;
       
   258 
       
   259 	// Get the hooks from the DB
       
   260 	$query = array(
       
   261 		'SELECT'	=> 'eh.id, eh.code, eh.extension_id',
       
   262 		'FROM'		=> 'extension_hooks AS eh',
       
   263 		'JOINS'		=> array(
       
   264 			array(
       
   265 				'INNER JOIN'	=> 'extensions AS e',
       
   266 				'ON'			=> 'e.id=eh.extension_id'
       
   267 			)
       
   268 		),
       
   269 		'WHERE'		=> 'e.disabled=0',
       
   270 		'ORDER BY'	=> 'eh.installed'
       
   271 	);
       
   272 
       
   273 	($hook = get_hook('ch_qr_get_hooks')) ? eval($hook) : null;
       
   274 	$result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__);
       
   275 
       
   276 	$output = array();
       
   277 	while ($cur_hook = $pun_db->fetch_assoc($result))
       
   278 	{
       
   279 		$ext_info = '$ext_info = array('."\n".
       
   280 			'\'id\'		=> \''.$cur_hook['extension_id'].'\','."\n".
       
   281 			'\'path\'	=> PUN_ROOT.\'extensions/'.$cur_hook['extension_id'].'\','."\n".
       
   282 			'\'url\'	=> $base_url.\'/extensions/'.$cur_hook['extension_id'].'\');';
       
   283 		$output[$cur_hook['id']][] = $ext_info."\n\n".$cur_hook['code']."\n";
       
   284 	}
       
   285 
       
   286 	// Output hooks as PHP code
       
   287 	$fh = @fopen(PUN_CACHE_DIR.'cache_hooks.php', 'wb');
       
   288 	if (!$fh)
       
   289 		error('Unable to write hooks cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
       
   290 
       
   291 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_HOOKS_LOADED\', 1);'."\n\n".'$pun_hooks = '.var_export($output, true).';'."\n\n".'?>');
       
   292 
       
   293 	fclose($fh);
       
   294 }
       
   295 
       
   296 
       
   297 //
       
   298 // Generate the updates cache PHP script
       
   299 //
       
   300 function generate_updates_cache()
       
   301 {
       
   302 	global $pun_db, $pun_config;
       
   303 
       
   304 	// Get a list of installed hotfix extensions
       
   305 	$query = array(
       
   306 		'SELECT'	=> 'e.id',
       
   307 		'FROM'		=> 'extensions AS e',
       
   308 		'WHERE'		=> 'e.id LIKE \'hotfix_%\''
       
   309 	);
       
   310 
       
   311 	($hook = get_hook('ch_qr_get_hotfixes')) ? eval($hook) : null;
       
   312 	$result = $pun_db->query_build($query) or error(__FILE__, __LINE__);
       
   313 	$num_hotfixes = $pun_db->num_rows($result);
       
   314 
       
   315 	$hotfixes = array();
       
   316 	for ($i = 0; $i < $num_hotfixes; ++$i)
       
   317 		$hotfixes[] = urlencode($pun_db->result($result, $i));
       
   318 
       
   319 	// Contact the punbb.org updates service
       
   320 	$result = get_remote_file('http://punbb.org/update/?type=xml&version='.urlencode($pun_config['o_cur_version']).'&hotfixes='.implode(',', $hotfixes), 8);
       
   321 
       
   322 	// Make sure we got everything we need
       
   323 	if ($result != null && strpos($result['content'], '</updates>') !== false)
       
   324 	{
       
   325 		require PUN_ROOT.'/include/xml.php';
       
   326 
       
   327 		$output = xml_to_array($result['content']);
       
   328 		$output = current($output);
       
   329 		$output['cached'] = time();
       
   330 		$output['fail'] = false;
       
   331 	}
       
   332 	else	// If the update check failed, set the fail flag
       
   333 		$output = array('cached' => time(), 'fail' => true);
       
   334 
       
   335 	// This hook could potentially (and responsibly) be used by an extension to do its own little update check
       
   336 	($hook = get_hook('ch_generate_updates_cache_write')) ? eval($hook) : null;
       
   337 
       
   338 	// Output update status as PHP code
       
   339 	$fh = @fopen(PUN_CACHE_DIR.'cache_updates.php', 'wb');
       
   340 	if (!$fh)
       
   341 		error('Unable to write updates cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
       
   342 
       
   343 	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".'?>');
       
   344 
       
   345 	fclose($fh);
       
   346 }