Blame Extras/phpBB/3.0.4/style.php

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