Location: PHPKode > projects > The Liberated Students' Movement site. > tlsm/include/cache.php
<?php
/***********************************************************************

  Copyright (C) 2002-2005  Rickard Andersson (hide@address.com)

  This file is part of TLSM (token from 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('TLSM'))
	exit;


//
// If we are running pre PHP 4.2.0, we add our own implementation of var_export
//
if (!function_exists('var_export'))
{
	function var_export()
	{
		$args = func_get_args();
		$indent = (isset($args[2])) ? $args[2] : '';

		if (is_array($args[0]))
		{
			$output = 'array ('."\n";

			foreach ($args[0] as $k => $v)
			{
				if (is_numeric($k))
					$output .= $indent.'  '.$k.' => ';
				else
					$output .= $indent.'  \''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $k)).'\' => ';

				if (is_array($v))
					$output .= var_export($v, true, $indent.'  ');
				else
				{
					if (gettype($v) != 'string' && !empty($v))
						$output .= $v.','."\n";
					else
						$output .= '\''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $v)).'\','."\n";
				}
			}

			$output .= ($indent != '') ? $indent.'),'."\n" : ')';
		}
		else
			$output = $args[0];

		if ($args[1] == true)
			return $output;
		else
			echo $output;
	}
}


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

	// Get the forum config from the DB
	$result = $db->query('SELECT `conf_name`, `conf_value` FROM '.$db->prefix.'tlsm_config', true) or error('Unable to fetch forum config', __FILE__, __LINE__, $db->error());
	while ($cur_config_item = $db->fetch_row($result))
		$output[$cur_config_item[0]] = $cur_config_item[1];

	// Output config as PHP code
	$fh = @fopen(TLSM_ROOT.'cache/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(\'TLSM_CONFIG_LOADED\', 1);'."\n\n".'$general = ' . var_export($output, true) . "\n\n".'?>');

	fclose($fh);
}

//
// Generate calendar cache
//
function generate_calendar_cache($month, $year)
{
	global $db;
		$monthStart	= mktime(0, 0, 0, $month, 1, $year);
		$monthEnd	= mktime(0, 0, 0, $month, date("t", $monthStart), $year);

	// Get the month calendar
	$result = $db->query('SELECT `id`,`name`,`begin`,`end`,`allday`,`about`,`accepted`,`location_name`  FROM '.$db->prefix.'calendar WHERE (`begin` >= \'' . $monthStart . '\' AND `begin` <= \'' . $monthEnd . '\') OR (`begin` <= \'' . $monthStart . '\' AND `end` >= \'' . $monthStart . '\') ORDER BY \'begin\'', true) or error('Unable to fetch events', __FILE__, __LINE__, $db->error());
	while ($cur_item = $db->fetch_assoc($result)){
		$output[$cur_item['id']] = array(
			'id'	=> $cur_item['id'],
			'name'	=> $cur_item['name'],
			'begin'	=> $cur_item['begin'],
			'end'	=> $cur_item['end'],
			'allday'=> $cur_item['allday'],
			'about'	=> $cur_item['about'],
			'location_name'	=> $cur_item['location_name'],
			'accepted'=> $cur_item['accepted']
		);

	}
	// Output calendar as PHP code
	$fh = @fopen(TLSM_ROOT.'cache/cal_'.$month.'_'.$year.'.php', 'wb');
	if (!$fh)
		error('Unable to write calendar cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'', __FILE__, __LINE__);

	fwrite($fh, '<?php'."\n\n".'define(\'TLSM_CALENDAR_'. $month .'_'.$year.'\', 1);'."\n\n".'$events = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}


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

	// Get the ban list from the DB
	$result = $db->query('SELECT * FROM '.$db->prefix.'bans', true) or error('Unable to fetch ban list', __FILE__, __LINE__, $db->error());

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

	// Output ban list as PHP code
	$fh = @fopen(TLSM_ROOT.'cache/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(\'TLSM_BANS_LOADED\', 1);'."\n\n".'$tlsm_bans = '.var_export($output, true).';'."\n\n".'?>');

	fclose($fh);
}
Return current item: The Liberated Students' Movement site.