punbb/include/cache.php
changeset 0 f9ffdbd96607
child 2 a8a21e1c7afa
equal deleted inserted replaced
-1:000000000000 0:f9ffdbd96607
       
     1 <?php
       
     2 /***********************************************************************
       
     3 
       
     4   Copyright (C) 2002-2005  Rickard Andersson (rickard@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 // If we are running pre PHP 4.2.0, we add our own implementation of var_export
       
    33 //
       
    34 if (!function_exists('var_export'))
       
    35 {
       
    36 	function var_export()
       
    37 	{
       
    38 		$args = func_get_args();
       
    39 		$indent = (isset($args[2])) ? $args[2] : '';
       
    40 
       
    41 		if (is_array($args[0]))
       
    42 		{
       
    43 			$output = 'array ('."\n";
       
    44 
       
    45 			foreach ($args[0] as $k => $v)
       
    46 			{
       
    47 				if (is_numeric($k))
       
    48 					$output .= $indent.'  '.$k.' => ';
       
    49 				else
       
    50 					$output .= $indent.'  \''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $k)).'\' => ';
       
    51 
       
    52 				if (is_array($v))
       
    53 					$output .= var_export($v, true, $indent.'  ');
       
    54 				else
       
    55 				{
       
    56 					if (gettype($v) != 'string' && !empty($v))
       
    57 						$output .= $v.','."\n";
       
    58 					else
       
    59 						$output .= '\''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $v)).'\','."\n";
       
    60 				}
       
    61 			}
       
    62 
       
    63 			$output .= ($indent != '') ? $indent.'),'."\n" : ')';
       
    64 		}
       
    65 		else
       
    66 			$output = $args[0];
       
    67 
       
    68 		if ($args[1] == true)
       
    69 			return $output;
       
    70 		else
       
    71 			echo $output;
       
    72 	}
       
    73 }
       
    74 
       
    75 
       
    76 //
       
    77 // Generate the config cache PHP script
       
    78 //
       
    79 function generate_config_cache()
       
    80 {
       
    81 	global $db;
       
    82 
       
    83 	// Get the forum config from the DB
       
    84 	$result = $db->query('SELECT * FROM '.$db->prefix.'config', true) or error('Unable to fetch forum config', __FILE__, __LINE__, $db->error());
       
    85 	while ($cur_config_item = $db->fetch_row($result))
       
    86 		$output[$cur_config_item[0]] = $cur_config_item[1];
       
    87 
       
    88 	// Output config as PHP code
       
    89 	$fh = @fopen(PUN_ROOT.'cache/cache_config.php', 'wb');
       
    90 	if (!$fh)
       
    91 		error('Unable to write configuration cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'', __FILE__, __LINE__);
       
    92 
       
    93 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_CONFIG_LOADED\', 1);'."\n\n".'$pun_config = '.var_export($output, true).';'."\n\n".'?>');
       
    94 
       
    95 	fclose($fh);
       
    96 }
       
    97 
       
    98 
       
    99 //
       
   100 // Generate the bans cache PHP script
       
   101 //
       
   102 function generate_bans_cache()
       
   103 {
       
   104 	global $db;
       
   105 
       
   106 	// Get the ban list from the DB
       
   107 	$result = $db->query('SELECT * FROM '.$db->prefix.'bans', true) or error('Unable to fetch ban list', __FILE__, __LINE__, $db->error());
       
   108 
       
   109 	$output = array();
       
   110 	while ($cur_ban = $db->fetch_assoc($result))
       
   111 		$output[] = $cur_ban;
       
   112 
       
   113 	// Output ban list as PHP code
       
   114 	$fh = @fopen(PUN_ROOT.'cache/cache_bans.php', 'wb');
       
   115 	if (!$fh)
       
   116 		error('Unable to write bans cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'', __FILE__, __LINE__);
       
   117 
       
   118 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_BANS_LOADED\', 1);'."\n\n".'$pun_bans = '.var_export($output, true).';'."\n\n".'?>');
       
   119 
       
   120 	fclose($fh);
       
   121 }
       
   122 
       
   123 
       
   124 //
       
   125 // Generate the ranks cache PHP script
       
   126 //
       
   127 function generate_ranks_cache()
       
   128 {
       
   129 	global $db;
       
   130 
       
   131 	// Get the rank list from the DB
       
   132 	$result = $db->query('SELECT * FROM '.$db->prefix.'ranks ORDER BY min_posts', true) or error('Unable to fetch rank list', __FILE__, __LINE__, $db->error());
       
   133 
       
   134 	$output = array();
       
   135 	while ($cur_rank = $db->fetch_assoc($result))
       
   136 		$output[] = $cur_rank;
       
   137 
       
   138 	// Output ranks list as PHP code
       
   139 	$fh = @fopen(PUN_ROOT.'cache/cache_ranks.php', 'wb');
       
   140 	if (!$fh)
       
   141 		error('Unable to write ranks cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'', __FILE__, __LINE__);
       
   142 
       
   143 	fwrite($fh, '<?php'."\n\n".'define(\'PUN_RANKS_LOADED\', 1);'."\n\n".'$pun_ranks = '.var_export($output, true).';'."\n\n".'?>');
       
   144 
       
   145 	fclose($fh);
       
   146 }
       
   147 
       
   148 
       
   149 //
       
   150 // Generate quickjump cache PHP scripts
       
   151 //
       
   152 function generate_quickjump_cache($group_id = false)
       
   153 {
       
   154 	global $db, $lang_common, $pun_user;
       
   155 
       
   156 	// If a group_id was supplied, we generate the quickjump cache for that group only
       
   157 	if ($group_id !== false)
       
   158 		$groups[0] = $group_id;
       
   159 	else
       
   160 	{
       
   161 		// A group_id was now supplied, so we generate the quickjump cache for all groups
       
   162 		$result = $db->query('SELECT g_id FROM '.$db->prefix.'groups') or error('Unable to fetch user group list', __FILE__, __LINE__, $db->error());
       
   163 		$num_groups = $db->num_rows($result);
       
   164 
       
   165 		for ($i = 0; $i < $num_groups; ++$i)
       
   166 			$groups[] = $db->result($result, $i);
       
   167 	}
       
   168 
       
   169 	// Loop through the groups in $groups and output the cache for each of them
       
   170 	while (list(, $group_id) = @each($groups))
       
   171 	{
       
   172 		// Output quickjump as PHP code
       
   173 		$fh = @fopen(PUN_ROOT.'cache/cache_quickjump_'.$group_id.'.php', 'wb');
       
   174 		if (!$fh)
       
   175 			error('Unable to write quickjump cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'', __FILE__, __LINE__);
       
   176 
       
   177 		$output = '<?php'."\n\n".'if (!defined(\'PUN\')) exit;'."\n".'define(\'PUN_QJ_LOADED\', 1);'."\n\n".'?>';
       
   178 		$output .= "\t\t\t\t".'<form id="qjump" method="get" action="viewforum.php">'."\n\t\t\t\t\t".'<div><label><?php echo $lang_common[\'Jump to\'] ?>'."\n\n\t\t\t\t\t".'<br /><select name="id" onchange="window.location=(\'viewforum.php?id=\'+this.options[this.selectedIndex].value)">'."\n";
       
   179 
       
   180 
       
   181 		$result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id LEFT JOIN '.$db->prefix.'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', true) or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());
       
   182 
       
   183 		$cur_category = 0;
       
   184 		while ($cur_forum = $db->fetch_assoc($result))
       
   185 		{
       
   186 			if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
       
   187 			{
       
   188 				if ($cur_category)
       
   189 					$output .= "\t\t\t\t\t\t".'</optgroup>'."\n";
       
   190 
       
   191 				$output .= "\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
       
   192 				$cur_category = $cur_forum['cid'];
       
   193 			}
       
   194 
       
   195 			$redirect_tag = ($cur_forum['redirect_url'] != '') ? ' &gt;&gt;&gt;' : '';
       
   196 			$output .= "\t\t\t\t\t\t\t".'<option value="'.$cur_forum['fid'].'"<?php echo ($forum_id == '.$cur_forum['fid'].') ? \' selected="selected"\' : \'\' ?>>'.pun_htmlspecialchars($cur_forum['forum_name']).$redirect_tag.'</option>'."\n";
       
   197 		}
       
   198 
       
   199 		$output .= "\t\t\t\t\t".'</optgroup>'."\n\t\t\t\t\t".'</select>'."\n\t\t\t\t\t".'<input type="submit" value="<?php echo $lang_common[\'Go\'] ?>" accesskey="g" />'."\n\t\t\t\t\t".'</label></div>'."\n\t\t\t\t".'</form>'."\n";
       
   200 
       
   201 		fwrite($fh, $output);
       
   202 
       
   203 		fclose($fh);
       
   204 	}
       
   205 }