Register
phpBB.com Wiki · Home Projects Help

root / trunk / phpBB / style.php

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