Blame Identity/Webenv/phpBB/3.0.4/style.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package phpBB3
ef5584
* @version $Id: style.php 8780 2008-08-22 12:52:48Z acydburn $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
define('IN_PHPBB', true);
ef5584
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
ef5584
$phpEx = substr(strrchr(__FILE__, '.'), 1);
ef5584
ef5584
// Report all errors, except notices
ef5584
error_reporting(E_ALL ^ E_NOTICE);
ef5584
ef5584
require($phpbb_root_path . 'config.' . $phpEx);
ef5584
ef5584
if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
if (version_compare(PHP_VERSION, '6.0.0-dev', '<'))
ef5584
{
ef5584
	@set_magic_quotes_runtime(0);
ef5584
}
ef5584
ef5584
// Load Extensions
ef5584
if (!empty($load_extensions))
ef5584
{
ef5584
	$load_extensions = explode(',', $load_extensions);
ef5584
ef5584
	foreach ($load_extensions as $extension)
ef5584
	{
ef5584
		@dl(trim($extension));
ef5584
	}
ef5584
}
ef5584
ef5584
ef5584
$sid = (isset($_GET['sid']) && !is_array($_GET['sid'])) ? htmlspecialchars($_GET['sid']) : '';
ef5584
$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
ef5584
ef5584
if (strspn($sid, 'abcdefABCDEF0123456789') !== strlen($sid))
ef5584
{
ef5584
	$sid = '';
ef5584
}
ef5584
ef5584
// This is a simple script to grab and output the requested CSS data stored in the DB
ef5584
// We include a session_id check to try and limit 3rd party linking ... unless they
ef5584
// happen to have a current session it will output nothing. We will also cache the
ef5584
// resulting CSS data for five minutes ... anything to reduce the load on the SQL
ef5584
// server a little
ef5584
if ($id)
ef5584
{
ef5584
	// Include files
ef5584
	require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
ef5584
	require($phpbb_root_path . 'includes/cache.' . $phpEx);
ef5584
	require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
ef5584
	require($phpbb_root_path . 'includes/constants.' . $phpEx);
ef5584
	require($phpbb_root_path . 'includes/functions.' . $phpEx);
ef5584
ef5584
	$db = new $sql_db();
ef5584
	$cache = new cache();
ef5584
ef5584
	// Connect to DB
ef5584
	if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
ef5584
	{
ef5584
		exit;
ef5584
	}
ef5584
	unset($dbpasswd);
ef5584
ef5584
	$config = $cache->obtain_config();
ef5584
	$user = false;
ef5584
ef5584
	if ($sid)
ef5584
	{
ef5584
		$sql = 'SELECT u.user_id, u.user_lang
ef5584
			FROM ' . SESSIONS_TABLE . ' s, ' . USERS_TABLE . " u
ef5584
			WHERE s.session_id = '" . $db->sql_escape($sid) . "'
ef5584
				AND s.session_user_id = u.user_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$user = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
	}
ef5584
ef5584
	$recompile = $config['load_tplcompile'];
ef5584
	if (!$user)
ef5584
	{
ef5584
		$id			= $config['default_style'];
ef5584
		$recompile	= false;
ef5584
		$user		= array('user_id' => ANONYMOUS);
ef5584
	}
ef5584
ef5584
	$sql = 'SELECT s.style_id, c.theme_id, c.theme_data, c.theme_path, c.theme_name, c.theme_mtime, i.*, t.template_path
ef5584
		FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i
ef5584
		WHERE s.style_id = ' . $id . '
ef5584
			AND t.template_id = s.template_id
ef5584
			AND c.theme_id = s.theme_id
ef5584
			AND i.imageset_id = s.imageset_id';
ef5584
	$result = $db->sql_query($sql, 300);
ef5584
	$theme = $db->sql_fetchrow($result);
ef5584
	$db->sql_freeresult($result);
ef5584
ef5584
	if (!$theme)
ef5584
	{
ef5584
		exit;
ef5584
	}
ef5584
ef5584
	if ($user['user_id'] == ANONYMOUS)
ef5584
	{
ef5584
		$user['user_lang'] = $config['default_lang'];
ef5584
	}
ef5584
ef5584
	$user_image_lang = (file_exists($phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user['user_lang'])) ? $user['user_lang'] : $config['default_lang'];
ef5584
ef5584
	$sql = 'SELECT *
ef5584
		FROM ' . STYLES_IMAGESET_DATA_TABLE . '
ef5584
		WHERE imageset_id = ' . $theme['imageset_id'] . "
ef5584
		AND image_filename <> '' 
ef5584
		AND image_lang IN ('" . $db->sql_escape($user_image_lang) . "', '')";
ef5584
	$result = $db->sql_query($sql, 3600);
ef5584
ef5584
	$img_array = array();
ef5584
	while ($row = $db->sql_fetchrow($result))
ef5584
	{
ef5584
		$img_array[$row['image_name']] = $row;
ef5584
	}
ef5584
	$db->sql_freeresult($result);
ef5584
ef5584
	// gzip_compression
ef5584
	if ($config['gzip_compress'])
ef5584
	{
ef5584
		// IE6 is not able to compress the style (do not ask us why!)
ef5584
		$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? strtolower(htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT'])) : '';
ef5584
ef5584
		if ($browser && strpos($browser, 'msie 6.0') === false && @extension_loaded('zlib') && !headers_sent())
ef5584
		{
ef5584
			ob_start('ob_gzhandler');
ef5584
		}
ef5584
	}
ef5584
ef5584
	// Expire time of seven days if not recached
ef5584
	$expire_time = 7*86400;
ef5584
	$recache = false;
ef5584
ef5584
	// Re-cache stylesheet data if necessary
ef5584
	if ($recompile || empty($theme['theme_data']))
ef5584
	{
ef5584
		$recache = (empty($theme['theme_data'])) ? true : false;
ef5584
		$update_time = time();
ef5584
ef5584
		// We test for stylesheet.css because it is faster and most likely the only file changed on common themes
ef5584
		if (!$recache && $theme['theme_mtime'] < @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css'))
ef5584
		{
ef5584
			$recache = true;
ef5584
			$update_time = @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css');
ef5584
		}
ef5584
		else if (!$recache)
ef5584
		{
ef5584
			$last_change = $theme['theme_mtime'];
ef5584
			$dir = @opendir("{$phpbb_root_path}styles/{$theme['theme_path']}/theme");
ef5584
ef5584
			if ($dir)
ef5584
			{
ef5584
				while (($entry = readdir($dir)) !== false)
ef5584
				{
ef5584
					if (substr(strrchr($entry, '.'), 1) == 'css' && $last_change < @filemtime("{$phpbb_root_path}styles/{$theme['theme_path']}/theme/{$entry}"))
ef5584
					{
ef5584
						$recache = true;
ef5584
						break;
ef5584
					}
ef5584
				}
ef5584
				closedir($dir);
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
ef5584
	if ($recache)
ef5584
	{
ef5584
		include_once($phpbb_root_path . 'includes/acp/acp_styles.' . $phpEx);
ef5584
ef5584
		$theme['theme_data'] = acp_styles::db_theme_data($theme);
ef5584
		$theme['theme_mtime'] = $update_time;
ef5584
ef5584
		// Save CSS contents
ef5584
		$sql_ary = array(
ef5584
			'theme_mtime'	=> $theme['theme_mtime'],
ef5584
			'theme_data'	=> $theme['theme_data']
ef5584
		);
ef5584
ef5584
		$sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
ef5584
			WHERE theme_id = {$theme['theme_id']}";
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		$cache->destroy('sql', STYLES_THEME_TABLE);
ef5584
	}
ef5584
ef5584
	// Only set the expire time if the theme changed data is older than 30 minutes - to cope with changes from the ACP
ef5584
	if ($recache || $theme['theme_mtime'] > (time() - 1800))
ef5584
	{
ef5584
		header('Expires: 0');
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time));
ef5584
	}
ef5584
ef5584
	header('Content-type: text/css; charset=UTF-8');
ef5584
ef5584
	// Parse Theme Data
ef5584
	$replace = array(
ef5584
		'{T_THEME_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme',
ef5584
		'{T_TEMPLATE_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['template_path'] . '/template',
ef5584
		'{T_IMAGESET_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset',
ef5584
		'{T_IMAGESET_LANG_PATH}'	=> "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset/' . $user_image_lang,
ef5584
		'{T_STYLESHEET_NAME}'		=> $theme['theme_name'],
ef5584
		'{S_USER_LANG}'				=> $user['user_lang']
ef5584
	);
ef5584
ef5584
	$theme['theme_data'] = str_replace(array_keys($replace), array_values($replace), $theme['theme_data']);
ef5584
ef5584
	$matches = array();
ef5584
	preg_match_all('#\{IMG_([A-Za-z0-9_]*?)_(WIDTH|HEIGHT|SRC)\}#', $theme['theme_data'], $matches);
ef5584
ef5584
	$imgs = $find = $replace = array();
ef5584
	if (isset($matches[0]) && sizeof($matches[0]))
ef5584
	{
ef5584
		foreach ($matches[1] as $i => $img)
ef5584
		{
ef5584
			$img = strtolower($img);
ef5584
			$find[] = $matches[0][$i];
ef5584
ef5584
			if (!isset($img_array[$img]))
ef5584
			{
ef5584
				$replace[] = '';
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			if (!isset($imgs[$img]))
ef5584
			{
ef5584
				$img_data = &$img_array[$img];
ef5584
				$imgsrc = ($img_data['image_lang'] ? $img_data['image_lang'] . '/' : '') . $img_data['image_filename'];
ef5584
				$imgs[$img] = array(
ef5584
					'src'		=> $phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $imgsrc,
ef5584
					'width'		=> $img_data['image_width'],
ef5584
					'height'	=> $img_data['image_height'],
ef5584
				);
ef5584
			}
ef5584
ef5584
			switch ($matches[2][$i])
ef5584
			{
ef5584
				case 'SRC':
ef5584
					$replace[] = $imgs[$img]['src'];
ef5584
				break;
ef5584
				
ef5584
				case 'WIDTH':
ef5584
					$replace[] = $imgs[$img]['width'];
ef5584
				break;
ef5584
	
ef5584
				case 'HEIGHT':
ef5584
					$replace[] = $imgs[$img]['height'];
ef5584
				break;
ef5584
ef5584
				default:
ef5584
					continue;
ef5584
			}
ef5584
		}
ef5584
ef5584
		if (sizeof($find))
ef5584
		{
ef5584
			$theme['theme_data'] = str_replace($find, $replace, $theme['theme_data']);
ef5584
		}
ef5584
	}
ef5584
ef5584
	echo $theme['theme_data'];
ef5584
ef5584
	if (!empty($cache))
ef5584
	{
ef5584
		$cache->unload();
ef5584
	}
ef5584
	$db->sql_close();
ef5584
}
ef5584
ef5584
exit;
ef5584
ef5584
?>