style.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/style.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,293 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +define('IN_PHPBB', true);
    1.18 +$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    1.19 +$phpEx = substr(strrchr(__FILE__, '.'), 1);
    1.20 +
    1.21 +require($phpbb_root_path . 'includes/startup.' . $phpEx);
    1.22 +require($phpbb_root_path . 'config.' . $phpEx);
    1.23 +
    1.24 +if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
    1.25 +{
    1.26 +	exit;
    1.27 +}
    1.28 +
    1.29 +// Load Extensions
    1.30 +if (!empty($load_extensions) && function_exists('dl'))
    1.31 +{
    1.32 +	$load_extensions = explode(',', $load_extensions);
    1.33 +
    1.34 +	foreach ($load_extensions as $extension)
    1.35 +	{
    1.36 +		@dl(trim($extension));
    1.37 +	}
    1.38 +}
    1.39 +
    1.40 +$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
    1.41 +
    1.42 +// This is a simple script to grab and output the requested CSS data stored in the DB
    1.43 +// We include a session_id check to try and limit 3rd party linking ... unless they
    1.44 +// happen to have a current session it will output nothing. We will also cache the
    1.45 +// resulting CSS data for five minutes ... anything to reduce the load on the SQL
    1.46 +// server a little
    1.47 +if ($id)
    1.48 +{
    1.49 +	// Include files
    1.50 +	require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
    1.51 +	require($phpbb_root_path . 'includes/cache.' . $phpEx);
    1.52 +	require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
    1.53 +	require($phpbb_root_path . 'includes/constants.' . $phpEx);
    1.54 +	require($phpbb_root_path . 'includes/functions.' . $phpEx);
    1.55 +
    1.56 +	$db = new $sql_db();
    1.57 +	$cache = new cache();
    1.58 +
    1.59 +	// Connect to DB
    1.60 +	if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
    1.61 +	{
    1.62 +		exit;
    1.63 +	}
    1.64 +	unset($dbpasswd);
    1.65 +
    1.66 +	$config = $cache->obtain_config();
    1.67 +	$user = false;
    1.68 +
    1.69 +	// try to get a session ID from REQUEST array
    1.70 +	$sid = request_var('sid', '');
    1.71 +
    1.72 +	if (!$sid)
    1.73 +	{
    1.74 +		// if that failed, then look in the cookies
    1.75 +		$sid = request_var($config['cookie_name'] . '_sid', '', false, true);
    1.76 +	}
    1.77 +
    1.78 +	if (strspn($sid, 'abcdefABCDEF0123456789') !== strlen($sid))
    1.79 +	{
    1.80 +		$sid = '';
    1.81 +	}
    1.82 +
    1.83 +	if ($sid)
    1.84 +	{
    1.85 +		$sql = 'SELECT u.user_id, u.user_lang
    1.86 +			FROM ' . SESSIONS_TABLE . ' s, ' . USERS_TABLE . " u
    1.87 +			WHERE s.session_id = '" . $db->sql_escape($sid) . "'
    1.88 +				AND s.session_user_id = u.user_id";
    1.89 +		$result = $db->sql_query($sql);
    1.90 +		$user = $db->sql_fetchrow($result);
    1.91 +		$db->sql_freeresult($result);
    1.92 +	}
    1.93 +
    1.94 +	$recompile = $config['load_tplcompile'];
    1.95 +	if (!$user)
    1.96 +	{
    1.97 +		$id			= ($id) ? $id : $config['default_style'];
    1.98 +//		Commented out because calls do not always include the SID anymore
    1.99 +//		$recompile	= false;
   1.100 +		$user		= array('user_id' => ANONYMOUS);
   1.101 +	}
   1.102 +
   1.103 +	$sql = 'SELECT s.style_id, c.theme_id, c.theme_data, c.theme_path, c.theme_name, c.theme_mtime, i.*, t.template_path
   1.104 +		FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i
   1.105 +		WHERE s.style_id = ' . $id . '
   1.106 +			AND t.template_id = s.template_id
   1.107 +			AND c.theme_id = s.theme_id
   1.108 +			AND i.imageset_id = s.imageset_id';
   1.109 +	$result = $db->sql_query($sql, 300);
   1.110 +	$theme = $db->sql_fetchrow($result);
   1.111 +	$db->sql_freeresult($result);
   1.112 +
   1.113 +	if (!$theme)
   1.114 +	{
   1.115 +		exit;
   1.116 +	}
   1.117 +
   1.118 +	if ($user['user_id'] == ANONYMOUS)
   1.119 +	{
   1.120 +		$user['user_lang'] = $config['default_lang'];
   1.121 +	}
   1.122 +
   1.123 +	$user_image_lang = (file_exists($phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user['user_lang'])) ? $user['user_lang'] : $config['default_lang'];
   1.124 +
   1.125 +	// Same query in session.php
   1.126 +	$sql = 'SELECT *
   1.127 +		FROM ' . STYLES_IMAGESET_DATA_TABLE . '
   1.128 +		WHERE imageset_id = ' . $theme['imageset_id'] . "
   1.129 +		AND image_filename <> ''
   1.130 +		AND image_lang IN ('" . $db->sql_escape($user_image_lang) . "', '')";
   1.131 +	$result = $db->sql_query($sql, 3600);
   1.132 +
   1.133 +	$img_array = array();
   1.134 +	while ($row = $db->sql_fetchrow($result))
   1.135 +	{
   1.136 +		$img_array[$row['image_name']] = $row;
   1.137 +	}
   1.138 +	$db->sql_freeresult($result);
   1.139 +
   1.140 +	// gzip_compression
   1.141 +	if ($config['gzip_compress'])
   1.142 +	{
   1.143 +		// IE6 is not able to compress the style (do not ask us why!)
   1.144 +		$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? strtolower(htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT'])) : '';
   1.145 +
   1.146 +		if ($browser && strpos($browser, 'msie 6.0') === false && @extension_loaded('zlib') && !headers_sent())
   1.147 +		{
   1.148 +			ob_start('ob_gzhandler');
   1.149 +		}
   1.150 +	}
   1.151 +
   1.152 +	// Expire time of seven days if not recached
   1.153 +	$expire_time = 7*86400;
   1.154 +	$recache = false;
   1.155 +
   1.156 +	// Re-cache stylesheet data if necessary
   1.157 +	if ($recompile || empty($theme['theme_data']))
   1.158 +	{
   1.159 +		$recache = (empty($theme['theme_data'])) ? true : false;
   1.160 +		$update_time = time();
   1.161 +
   1.162 +		// We test for stylesheet.css because it is faster and most likely the only file changed on common themes
   1.163 +		if (!$recache && $theme['theme_mtime'] < @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css'))
   1.164 +		{
   1.165 +			$recache = true;
   1.166 +			$update_time = @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css');
   1.167 +		}
   1.168 +		else if (!$recache)
   1.169 +		{
   1.170 +			$last_change = $theme['theme_mtime'];
   1.171 +			$dir = @opendir("{$phpbb_root_path}styles/{$theme['theme_path']}/theme");
   1.172 +
   1.173 +			if ($dir)
   1.174 +			{
   1.175 +				while (($entry = readdir($dir)) !== false)
   1.176 +				{
   1.177 +					if (substr(strrchr($entry, '.'), 1) == 'css' && $last_change < @filemtime("{$phpbb_root_path}styles/{$theme['theme_path']}/theme/{$entry}"))
   1.178 +					{
   1.179 +						$recache = true;
   1.180 +						break;
   1.181 +					}
   1.182 +				}
   1.183 +				closedir($dir);
   1.184 +			}
   1.185 +		}
   1.186 +	}
   1.187 +
   1.188 +	if ($recache)
   1.189 +	{
   1.190 +		include_once($phpbb_root_path . 'includes/acp/acp_styles.' . $phpEx);
   1.191 +
   1.192 +		$theme['theme_data'] = acp_styles::db_theme_data($theme);
   1.193 +		$theme['theme_mtime'] = $update_time;
   1.194 +
   1.195 +		// Save CSS contents
   1.196 +		$sql_ary = array(
   1.197 +			'theme_mtime'	=> $theme['theme_mtime'],
   1.198 +			'theme_data'	=> $theme['theme_data']
   1.199 +		);
   1.200 +
   1.201 +		$sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
   1.202 +			WHERE theme_id = {$theme['theme_id']}";
   1.203 +		$db->sql_query($sql);
   1.204 +
   1.205 +		$cache->destroy('sql', STYLES_THEME_TABLE);
   1.206 +	}
   1.207 +
   1.208 +	// Only set the expire time if the theme changed data is older than 30 minutes - to cope with changes from the ACP
   1.209 +	if ($recache || $theme['theme_mtime'] > (time() - 1800))
   1.210 +	{
   1.211 +		header('Expires: 0');
   1.212 +	}
   1.213 +	else
   1.214 +	{
   1.215 +		header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time));
   1.216 +	}
   1.217 +
   1.218 +	header('Content-type: text/css; charset=UTF-8');
   1.219 +
   1.220 +	// Parse Theme Data
   1.221 +	$replace = array(
   1.222 +		'{T_THEME_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme',
   1.223 +		'{T_TEMPLATE_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['template_path'] . '/template',
   1.224 +		'{T_IMAGESET_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset',
   1.225 +		'{T_IMAGESET_LANG_PATH}'	=> "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset/' . $user_image_lang,
   1.226 +		'{T_STYLESHEET_NAME}'		=> $theme['theme_name'],
   1.227 +		'{S_USER_LANG}'				=> $user['user_lang']
   1.228 +	);
   1.229 +
   1.230 +	$theme['theme_data'] = str_replace(array_keys($replace), array_values($replace), $theme['theme_data']);
   1.231 +
   1.232 +	$matches = array();
   1.233 +	preg_match_all('#\{IMG_([A-Za-z0-9_]*?)_(WIDTH|HEIGHT|SRC)\}#', $theme['theme_data'], $matches);
   1.234 +
   1.235 +	$imgs = $find = $replace = array();
   1.236 +	if (isset($matches[0]) && sizeof($matches[0]))
   1.237 +	{
   1.238 +		foreach ($matches[1] as $i => $img)
   1.239 +		{
   1.240 +			$img = strtolower($img);
   1.241 +			$find[] = $matches[0][$i];
   1.242 +
   1.243 +			if (!isset($img_array[$img]))
   1.244 +			{
   1.245 +				$replace[] = '';
   1.246 +				continue;
   1.247 +			}
   1.248 +
   1.249 +			if (!isset($imgs[$img]))
   1.250 +			{
   1.251 +				$img_data = &$img_array[$img];
   1.252 +				$imgsrc = ($img_data['image_lang'] ? $img_data['image_lang'] . '/' : '') . $img_data['image_filename'];
   1.253 +				$imgs[$img] = array(
   1.254 +					'src'		=> $phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $imgsrc,
   1.255 +					'width'		=> $img_data['image_width'],
   1.256 +					'height'	=> $img_data['image_height'],
   1.257 +				);
   1.258 +			}
   1.259 +
   1.260 +			switch ($matches[2][$i])
   1.261 +			{
   1.262 +				case 'SRC':
   1.263 +					$replace[] = $imgs[$img]['src'];
   1.264 +				break;
   1.265 +
   1.266 +				case 'WIDTH':
   1.267 +					$replace[] = $imgs[$img]['width'];
   1.268 +				break;
   1.269 +
   1.270 +				case 'HEIGHT':
   1.271 +					$replace[] = $imgs[$img]['height'];
   1.272 +				break;
   1.273 +
   1.274 +				default:
   1.275 +					continue;
   1.276 +			}
   1.277 +		}
   1.278 +
   1.279 +		if (sizeof($find))
   1.280 +		{
   1.281 +			$theme['theme_data'] = str_replace($find, $replace, $theme['theme_data']);
   1.282 +		}
   1.283 +	}
   1.284 +
   1.285 +	echo $theme['theme_data'];
   1.286 +
   1.287 +	if (!empty($cache))
   1.288 +	{
   1.289 +		$cache->unload();
   1.290 +	}
   1.291 +	$db->sql_close();
   1.292 +}
   1.293 +
   1.294 +exit;
   1.295 +
   1.296 +?>
   1.297 \ No newline at end of file