aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/style.php
blob: 4a26d8ed00a16e5517205d58d4c3242c643b9044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
if (!defined('PHPBB_ROOT_PATH')) define('PHPBB_ROOT_PATH', './');
if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));

$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;

// This is a simple script to grab and output the requested CSS data stored in the DB
// We cache the resulting CSS data for five minutes
if (!$id)
{
	exit;
}

include(PHPBB_ROOT_PATH . 'common.' . PHP_EXT);

$sql = 'SELECT s.style_id, c.theme_id, c.theme_data, c.theme_path, c.theme_name, c.theme_mtime, i.*, t.template_path
	FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i
	WHERE s.style_id = ' . $id . '
		AND t.template_id = s.template_id
		AND c.theme_id = s.theme_id
		AND i.imageset_id = s.imageset_id';
$result = phpbb::$db->sql_query($sql, 300);
$theme = phpbb::$db->sql_fetchrow($result);
phpbb::$db->sql_freeresult($result);

if (!$theme)
{
	garbage_collection();
	exit_handler();
}

$user_lang = basename(request_var('lang', phpbb::$config['default_lang']));
$user_image_lang = (file_exists(PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user_lang)) ? $user_lang : phpbb::$config['default_lang'];

$sql = 'SELECT *
	FROM ' . STYLES_IMAGESET_DATA_TABLE . '
	WHERE imageset_id = ' . $theme['imageset_id'] . "
	AND image_filename <> ''
	AND image_lang IN ('" . phpbb::$db->sql_escape($user_image_lang) . "', '')";
$result = phpbb::$db->sql_query($sql, 3600);

$img_array = array();
while ($row = phpbb::$db->sql_fetchrow($result))
{
	$img_array[$row['image_name']] = $row;
}
phpbb::$db->sql_freeresult($result);

// gzip_compression
if (phpbb::$config['gzip_compress'])
{
	if (strpos(strtolower(phpbb::$user->system['browser']), 'msie 6.0') === false && @extension_loaded('zlib') && !headers_sent())
	{
		ob_start('ob_gzhandler');
	}
}

// Expire time of seven days if not recached
$expire_time = 7 * 86400;
$recache = false;

// Re-cache stylesheet data if necessary
if (phpbb::$config['load_tplcompile'] || empty($theme['theme_data']))
{
	$recache = (empty($theme['theme_data'])) ? true : false;
	$update_time = time();

	// We test for stylesheet.css because it is faster and most likely the only file changed on common themes
	if (!$recache && $theme['theme_mtime'] < @filemtime(PHPBB_ROOT_PATH . 'styles/' . $theme['theme_path'] . '/theme/stylesheet.css'))
	{
		$recache = true;
		$update_time = @filemtime(PHPBB_ROOT_PATH . 'styles/' . $theme['theme_path'] . '/theme/stylesheet.css');
	}
	else if (!$recache)
	{
		$last_change = $theme['theme_mtime'];
		$dir = @opendir(PHPBB_ROOT_PATH . "styles/{$theme['theme_path']}/theme");

		if ($dir)
		{
			while (($entry = readdir($dir)) !== false)
			{
				if (substr(strrchr($entry, '.'), 1) == 'css' && $last_change < @filemtime(PHPBB_ROOT_PATH . "styles/{$theme['theme_path']}/theme/{$entry}"))
				{
					$recache = true;
					break;
				}
			}
			closedir($dir);
		}
	}
}

// We store new data within database
if ($recache)
{
	if (!class_exists('acp_styles'))
	{
		include PHPBB_ROOT_PATH . 'modules/acp/acp_styles.' . PHP_EXT;
	}

	$theme['theme_data'] = acp_styles::db_theme_data($theme);
	$theme['theme_mtime'] = $update_time;

	// Save CSS contents
	$sql_ary = array(
		'theme_mtime'	=> $theme['theme_mtime'],
		'theme_data'	=> $theme['theme_data']
	);

	phpbb::$db->sql_query('UPDATE ' . STYLES_THEME_TABLE . ' SET ' . phpbb::$db->sql_build_array('UPDATE', $sql_ary) . ' WHERE theme_id = ' . $theme['theme_id']);
	phpbb::$acm->destroy_sql(STYLES_THEME_TABLE);
}

// Only set the expire time if the theme changed data is older than 30 minutes - to cope with changes from the ACP
if ($recache || $theme['theme_mtime'] > (time() - 1800))
{
	header('Expires: 0');
}
else
{
	header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time));
}

header('Content-type: text/css; charset=UTF-8');

// Parse Theme Data
$replace = array(
	'{T_THEME_PATH}'			=> PHPBB_ROOT_PATH . 'styles/' . $theme['theme_path'] . '/theme',
	'{T_TEMPLATE_PATH}'			=> PHPBB_ROOT_PATH . 'styles/' . $theme['template_path'] . '/template',
	'{T_IMAGESET_PATH}'			=> PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset',
	'{T_IMAGESET_LANG_PATH}'	=> PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user_image_lang,
	'{T_STYLESHEET_NAME}'		=> $theme['theme_name'],
	'{S_USER_LANG}'				=> $user_lang,
);

$theme['theme_data'] = str_replace(array_keys($replace), array_values($replace), $theme['theme_data']);

// Replace IMG_*_WIDTH/HEIGHT/SRC tags
$matches = array();
preg_match_all('#\{IMG_([A-Za-z0-9_]*?)_(WIDTH|HEIGHT|SRC)\}#', $theme['theme_data'], $matches);

$imgs = $find = $replace = array();
if (isset($matches[0]) && sizeof($matches[0]))
{
	foreach ($matches[1] as $i => $img)
	{
		$img = strtolower($img);
		$find[] = $matches[0][$i];

		if (!isset($img_array[$img]))
		{
			$replace[] = '';
			continue;
		}

		if (!isset($imgs[$img]))
		{
			$img_data = &$img_array[$img];
			$imgsrc = ($img_data['image_lang'] ? $img_data['image_lang'] . '/' : '') . $img_data['image_filename'];

			$imgs[$img] = array(
				'src'		=> PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset/' . $imgsrc,
				'width'		=> $img_data['image_width'],
				'height'	=> $img_data['image_height'],
			);
		}

		switch ($matches[2][$i])
		{
			case 'SRC':
				$replace[] = $imgs[$img]['src'];
			break;

			case 'WIDTH':
				$replace[] = $imgs[$img]['width'];
			break;

			case 'HEIGHT':
				$replace[] = $imgs[$img]['height'];
			break;

			default:
				continue;
		}
	}

	if (sizeof($find))
	{
		$theme['theme_data'] = str_replace($find, $replace, $theme['theme_data']);
	}
}

echo $theme['theme_data'];

garbage_collection();
exit_handler();

?>