Register
phpBB.com Wiki · Home Projects Help

root / trunk / phpBB / style.php

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