style.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package phpBB3
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 define('IN_PHPBB', true);
       
    15 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
       
    16 $phpEx = substr(strrchr(__FILE__, '.'), 1);
       
    17 
       
    18 require($phpbb_root_path . 'includes/startup.' . $phpEx);
       
    19 require($phpbb_root_path . 'config.' . $phpEx);
       
    20 
       
    21 if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
       
    22 {
       
    23 	exit;
       
    24 }
       
    25 
       
    26 // Load Extensions
       
    27 if (!empty($load_extensions) && function_exists('dl'))
       
    28 {
       
    29 	$load_extensions = explode(',', $load_extensions);
       
    30 
       
    31 	foreach ($load_extensions as $extension)
       
    32 	{
       
    33 		@dl(trim($extension));
       
    34 	}
       
    35 }
       
    36 
       
    37 $id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
       
    38 
       
    39 // This is a simple script to grab and output the requested CSS data stored in the DB
       
    40 // We include a session_id check to try and limit 3rd party linking ... unless they
       
    41 // happen to have a current session it will output nothing. We will also cache the
       
    42 // resulting CSS data for five minutes ... anything to reduce the load on the SQL
       
    43 // server a little
       
    44 if ($id)
       
    45 {
       
    46 	// Include files
       
    47 	require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
       
    48 	require($phpbb_root_path . 'includes/cache.' . $phpEx);
       
    49 	require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
       
    50 	require($phpbb_root_path . 'includes/constants.' . $phpEx);
       
    51 	require($phpbb_root_path . 'includes/functions.' . $phpEx);
       
    52 
       
    53 	$db = new $sql_db();
       
    54 	$cache = new cache();
       
    55 
       
    56 	// Connect to DB
       
    57 	if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
       
    58 	{
       
    59 		exit;
       
    60 	}
       
    61 	unset($dbpasswd);
       
    62 
       
    63 	$config = $cache->obtain_config();
       
    64 	$user = false;
       
    65 
       
    66 	// try to get a session ID from REQUEST array
       
    67 	$sid = request_var('sid', '');
       
    68 
       
    69 	if (!$sid)
       
    70 	{
       
    71 		// if that failed, then look in the cookies
       
    72 		$sid = request_var($config['cookie_name'] . '_sid', '', false, true);
       
    73 	}
       
    74 
       
    75 	if (strspn($sid, 'abcdefABCDEF0123456789') !== strlen($sid))
       
    76 	{
       
    77 		$sid = '';
       
    78 	}
       
    79 
       
    80 	if ($sid)
       
    81 	{
       
    82 		$sql = 'SELECT u.user_id, u.user_lang
       
    83 			FROM ' . SESSIONS_TABLE . ' s, ' . USERS_TABLE . " u
       
    84 			WHERE s.session_id = '" . $db->sql_escape($sid) . "'
       
    85 				AND s.session_user_id = u.user_id";
       
    86 		$result = $db->sql_query($sql);
       
    87 		$user = $db->sql_fetchrow($result);
       
    88 		$db->sql_freeresult($result);
       
    89 	}
       
    90 
       
    91 	$recompile = $config['load_tplcompile'];
       
    92 	if (!$user)
       
    93 	{
       
    94 		$id			= ($id) ? $id : $config['default_style'];
       
    95 //		Commented out because calls do not always include the SID anymore
       
    96 //		$recompile	= false;
       
    97 		$user		= array('user_id' => ANONYMOUS);
       
    98 	}
       
    99 
       
   100 	$sql = 'SELECT s.style_id, c.theme_id, c.theme_data, c.theme_path, c.theme_name, c.theme_mtime, i.*, t.template_path
       
   101 		FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i
       
   102 		WHERE s.style_id = ' . $id . '
       
   103 			AND t.template_id = s.template_id
       
   104 			AND c.theme_id = s.theme_id
       
   105 			AND i.imageset_id = s.imageset_id';
       
   106 	$result = $db->sql_query($sql, 300);
       
   107 	$theme = $db->sql_fetchrow($result);
       
   108 	$db->sql_freeresult($result);
       
   109 
       
   110 	if (!$theme)
       
   111 	{
       
   112 		exit;
       
   113 	}
       
   114 
       
   115 	if ($user['user_id'] == ANONYMOUS)
       
   116 	{
       
   117 		$user['user_lang'] = $config['default_lang'];
       
   118 	}
       
   119 
       
   120 	$user_image_lang = (file_exists($phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user['user_lang'])) ? $user['user_lang'] : $config['default_lang'];
       
   121 
       
   122 	// Same query in session.php
       
   123 	$sql = 'SELECT *
       
   124 		FROM ' . STYLES_IMAGESET_DATA_TABLE . '
       
   125 		WHERE imageset_id = ' . $theme['imageset_id'] . "
       
   126 		AND image_filename <> ''
       
   127 		AND image_lang IN ('" . $db->sql_escape($user_image_lang) . "', '')";
       
   128 	$result = $db->sql_query($sql, 3600);
       
   129 
       
   130 	$img_array = array();
       
   131 	while ($row = $db->sql_fetchrow($result))
       
   132 	{
       
   133 		$img_array[$row['image_name']] = $row;
       
   134 	}
       
   135 	$db->sql_freeresult($result);
       
   136 
       
   137 	// gzip_compression
       
   138 	if ($config['gzip_compress'])
       
   139 	{
       
   140 		// IE6 is not able to compress the style (do not ask us why!)
       
   141 		$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? strtolower(htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT'])) : '';
       
   142 
       
   143 		if ($browser && strpos($browser, 'msie 6.0') === false && @extension_loaded('zlib') && !headers_sent())
       
   144 		{
       
   145 			ob_start('ob_gzhandler');
       
   146 		}
       
   147 	}
       
   148 
       
   149 	// Expire time of seven days if not recached
       
   150 	$expire_time = 7*86400;
       
   151 	$recache = false;
       
   152 
       
   153 	// Re-cache stylesheet data if necessary
       
   154 	if ($recompile || empty($theme['theme_data']))
       
   155 	{
       
   156 		$recache = (empty($theme['theme_data'])) ? true : false;
       
   157 		$update_time = time();
       
   158 
       
   159 		// We test for stylesheet.css because it is faster and most likely the only file changed on common themes
       
   160 		if (!$recache && $theme['theme_mtime'] < @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css'))
       
   161 		{
       
   162 			$recache = true;
       
   163 			$update_time = @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css');
       
   164 		}
       
   165 		else if (!$recache)
       
   166 		{
       
   167 			$last_change = $theme['theme_mtime'];
       
   168 			$dir = @opendir("{$phpbb_root_path}styles/{$theme['theme_path']}/theme");
       
   169 
       
   170 			if ($dir)
       
   171 			{
       
   172 				while (($entry = readdir($dir)) !== false)
       
   173 				{
       
   174 					if (substr(strrchr($entry, '.'), 1) == 'css' && $last_change < @filemtime("{$phpbb_root_path}styles/{$theme['theme_path']}/theme/{$entry}"))
       
   175 					{
       
   176 						$recache = true;
       
   177 						break;
       
   178 					}
       
   179 				}
       
   180 				closedir($dir);
       
   181 			}
       
   182 		}
       
   183 	}
       
   184 
       
   185 	if ($recache)
       
   186 	{
       
   187 		include_once($phpbb_root_path . 'includes/acp/acp_styles.' . $phpEx);
       
   188 
       
   189 		$theme['theme_data'] = acp_styles::db_theme_data($theme);
       
   190 		$theme['theme_mtime'] = $update_time;
       
   191 
       
   192 		// Save CSS contents
       
   193 		$sql_ary = array(
       
   194 			'theme_mtime'	=> $theme['theme_mtime'],
       
   195 			'theme_data'	=> $theme['theme_data']
       
   196 		);
       
   197 
       
   198 		$sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
       
   199 			WHERE theme_id = {$theme['theme_id']}";
       
   200 		$db->sql_query($sql);
       
   201 
       
   202 		$cache->destroy('sql', STYLES_THEME_TABLE);
       
   203 	}
       
   204 
       
   205 	// Only set the expire time if the theme changed data is older than 30 minutes - to cope with changes from the ACP
       
   206 	if ($recache || $theme['theme_mtime'] > (time() - 1800))
       
   207 	{
       
   208 		header('Expires: 0');
       
   209 	}
       
   210 	else
       
   211 	{
       
   212 		header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time));
       
   213 	}
       
   214 
       
   215 	header('Content-type: text/css; charset=UTF-8');
       
   216 
       
   217 	// Parse Theme Data
       
   218 	$replace = array(
       
   219 		'{T_THEME_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme',
       
   220 		'{T_TEMPLATE_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['template_path'] . '/template',
       
   221 		'{T_IMAGESET_PATH}'			=> "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset',
       
   222 		'{T_IMAGESET_LANG_PATH}'	=> "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset/' . $user_image_lang,
       
   223 		'{T_STYLESHEET_NAME}'		=> $theme['theme_name'],
       
   224 		'{S_USER_LANG}'				=> $user['user_lang']
       
   225 	);
       
   226 
       
   227 	$theme['theme_data'] = str_replace(array_keys($replace), array_values($replace), $theme['theme_data']);
       
   228 
       
   229 	$matches = array();
       
   230 	preg_match_all('#\{IMG_([A-Za-z0-9_]*?)_(WIDTH|HEIGHT|SRC)\}#', $theme['theme_data'], $matches);
       
   231 
       
   232 	$imgs = $find = $replace = array();
       
   233 	if (isset($matches[0]) && sizeof($matches[0]))
       
   234 	{
       
   235 		foreach ($matches[1] as $i => $img)
       
   236 		{
       
   237 			$img = strtolower($img);
       
   238 			$find[] = $matches[0][$i];
       
   239 
       
   240 			if (!isset($img_array[$img]))
       
   241 			{
       
   242 				$replace[] = '';
       
   243 				continue;
       
   244 			}
       
   245 
       
   246 			if (!isset($imgs[$img]))
       
   247 			{
       
   248 				$img_data = &$img_array[$img];
       
   249 				$imgsrc = ($img_data['image_lang'] ? $img_data['image_lang'] . '/' : '') . $img_data['image_filename'];
       
   250 				$imgs[$img] = array(
       
   251 					'src'		=> $phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $imgsrc,
       
   252 					'width'		=> $img_data['image_width'],
       
   253 					'height'	=> $img_data['image_height'],
       
   254 				);
       
   255 			}
       
   256 
       
   257 			switch ($matches[2][$i])
       
   258 			{
       
   259 				case 'SRC':
       
   260 					$replace[] = $imgs[$img]['src'];
       
   261 				break;
       
   262 
       
   263 				case 'WIDTH':
       
   264 					$replace[] = $imgs[$img]['width'];
       
   265 				break;
       
   266 
       
   267 				case 'HEIGHT':
       
   268 					$replace[] = $imgs[$img]['height'];
       
   269 				break;
       
   270 
       
   271 				default:
       
   272 					continue;
       
   273 			}
       
   274 		}
       
   275 
       
   276 		if (sizeof($find))
       
   277 		{
       
   278 			$theme['theme_data'] = str_replace($find, $replace, $theme['theme_data']);
       
   279 		}
       
   280 	}
       
   281 
       
   282 	echo $theme['theme_data'];
       
   283 
       
   284 	if (!empty($cache))
       
   285 	{
       
   286 		$cache->unload();
       
   287 	}
       
   288 	$db->sql_close();
       
   289 }
       
   290 
       
   291 exit;
       
   292 
       
   293 ?>