merge
[phpbb.git] / phpBB / style.php
blobcd9f8bd9d716e9f2047fc6e936c5b6698736f4d5
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 define('IN_PHPBB', true);
15 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
16 $phpEx = substr(strrchr(__FILE__, '.'), 1);
18 // Report all errors, except notices
19 error_reporting(E_ALL ^ E_NOTICE);
20 date_default_timezone_set('UTC');
22 require($phpbb_root_path . 'config.' . $phpEx);
24 if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
26 exit;
29 if (version_compare(PHP_VERSION, '6.0.0-dev', '<'))
31 set_magic_quotes_runtime(0);
34 // Load Extensions
35 if (!empty($load_extensions))
37 $load_extensions = explode(',', $load_extensions);
39 foreach ($load_extensions as $extension)
41 @dl(trim($extension));
46 $sid = (isset($_GET['sid']) && !is_array($_GET['sid'])) ? htmlspecialchars($_GET['sid']) : '';
47 $id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
49 if (strspn($sid, 'abcdefABCDEF0123456789') !== strlen($sid))
51 $sid = '';
54 // This is a simple script to grab and output the requested CSS data stored in the DB
55 // We include a session_id check to try and limit 3rd party linking ... unless they
56 // happen to have a current session it will output nothing. We will also cache the
57 // resulting CSS data for five minutes ... anything to reduce the load on the SQL
58 // server a little
59 if ($id)
61 // Include files
62 require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
63 require($phpbb_root_path . 'includes/cache.' . $phpEx);
64 require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
65 require($phpbb_root_path . 'includes/constants.' . $phpEx);
67 $db = new $sql_db();
68 $cache = new acm();
70 // Connect to DB
71 if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
73 exit;
75 unset($dbpasswd);
77 $config = cache::obtain_config();
78 $user = false;
80 if ($sid)
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);
91 $recompile = $config['load_tplcompile'];
92 if (!$user)
94 $id = $config['default_style'];
95 $recompile = false;
96 $user = array('user_id' => ANONYMOUS);
99 $sql = 'SELECT s.style_id, c.theme_id, c.theme_data, c.theme_path, c.theme_name, c.theme_mtime, i.*, t.template_path
100 FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i
101 WHERE s.style_id = ' . $id . '
102 AND t.template_id = s.template_id
103 AND c.theme_id = s.theme_id
104 AND i.imageset_id = s.imageset_id';
105 $result = $db->sql_query($sql, 300);
106 $theme = $db->sql_fetchrow($result);
107 $db->sql_freeresult($result);
109 if (!$theme)
111 exit;
114 if ($user['user_id'] == ANONYMOUS)
116 $user['user_lang'] = $config['default_lang'];
119 $user_image_lang = (file_exists($phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user['user_lang'])) ? $user['user_lang'] : $config['default_lang'];
121 $sql = 'SELECT *
122 FROM ' . STYLES_IMAGESET_DATA_TABLE . '
123 WHERE imageset_id = ' . $theme['imageset_id'] . "
124 AND image_filename <> ''
125 AND image_lang IN ('" . $db->sql_escape($user_image_lang) . "', '')";
126 $result = $db->sql_query($sql, 3600);
128 $img_array = array();
129 while ($row = $db->sql_fetchrow($result))
131 $img_array[$row['image_name']] = $row;
133 $db->sql_freeresult($result);
135 // gzip_compression
136 if ($config['gzip_compress'])
138 // IE6 is not able to compress the style (do not ask us why!)
139 $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? strtolower(htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT'])) : '';
141 if ($browser && strpos($browser, 'msie 6.0') === false && @extension_loaded('zlib') && !headers_sent())
143 ob_start('ob_gzhandler');
147 // Expire time of seven days if not recached
148 $expire_time = 7 * 86400;
149 $recache = false;
151 // Re-cache stylesheet data if necessary
152 if ($recompile || empty($theme['theme_data']))
154 $recache = (empty($theme['theme_data'])) ? true : false;
155 $update_time = time();
157 // We test for stylesheet.css because it is faster and most likely the only file changed on common themes
158 if (!$recache && $theme['theme_mtime'] < @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css'))
160 $recache = true;
161 $update_time = @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css');
163 else if (!$recache)
165 $last_change = $theme['theme_mtime'];
166 $dir = @opendir("{$phpbb_root_path}styles/{$theme['theme_path']}/theme");
168 if ($dir)
170 while (($entry = readdir($dir)) !== false)
172 if (substr(strrchr($entry, '.'), 1) == 'css' && $last_change < @filemtime("{$phpbb_root_path}styles/{$theme['theme_path']}/theme/{$entry}"))
174 $recache = true;
175 break;
178 closedir($dir);
183 if ($recache)
185 if (!class_exists('acp_styles'))
187 include($phpbb_root_path . 'includes/acp/acp_styles.' . $phpEx);
190 $theme['theme_data'] = acp_styles::db_theme_data($theme);
191 $theme['theme_mtime'] = $update_time;
193 // Save CSS contents
194 $sql_ary = array(
195 'theme_mtime' => $theme['theme_mtime'],
196 'theme_data' => $theme['theme_data']
199 // @TODO: rewrite with the new param db functions
200 $sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
201 WHERE theme_id = {$theme['theme_id']}";
202 $db->sql_query($sql);
204 $cache->destroy('sql', STYLES_THEME_TABLE);
207 // Only set the expire time if the theme changed data is older than 30 minutes - to cope with changes from the ACP
208 if ($recache || $theme['theme_mtime'] > (time() - 1800))
210 header('Expires: 0');
212 else
214 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time));
217 header('Content-type: text/css; charset=UTF-8');
219 // Parse Theme Data
220 $replace = array(
221 '{T_THEME_PATH}' => "{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme',
222 '{T_TEMPLATE_PATH}' => "{$phpbb_root_path}styles/" . $theme['template_path'] . '/template',
223 '{T_IMAGESET_PATH}' => "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset',
224 '{T_IMAGESET_LANG_PATH}' => "{$phpbb_root_path}styles/" . $theme['imageset_path'] . '/imageset/' . $user_image_lang,
225 '{T_STYLESHEET_NAME}' => $theme['theme_name'],
226 '{S_USER_LANG}' => $user['user_lang']
229 $theme['theme_data'] = str_replace(array_keys($replace), array_values($replace), $theme['theme_data']);
231 $matches = array();
232 preg_match_all('#\{IMG_([A-Za-z0-9_]*?)_(WIDTH|HEIGHT|SRC)\}#', $theme['theme_data'], $matches);
234 $imgs = $find = $replace = array();
235 if (isset($matches[0]) && sizeof($matches[0]))
237 foreach ($matches[1] as $i => $img)
239 $img = strtolower($img);
240 $find[] = $matches[0][$i];
242 if (!isset($img_array[$img]))
244 $replace[] = '';
245 continue;
248 if (!isset($imgs[$img]))
250 $img_data = &$img_array[$img];
251 $imgsrc = ($img_data['image_lang'] ? $img_data['image_lang'] . '/' : '') . $img_data['image_filename'];
252 $imgs[$img] = array(
253 'src' => $phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $imgsrc,
254 'width' => $img_data['image_width'],
255 'height' => $img_data['image_height'],
259 switch ($matches[2][$i])
261 case 'SRC':
262 $replace[] = $imgs[$img]['src'];
263 break;
265 case 'WIDTH':
266 $replace[] = $imgs[$img]['width'];
267 break;
269 case 'HEIGHT':
270 $replace[] = $imgs[$img]['height'];
271 break;
273 default:
274 continue;
278 if (sizeof($find))
280 $theme['theme_data'] = str_replace($find, $replace, $theme['theme_data']);
284 echo $theme['theme_data'];
286 if (!empty($cache))
288 $cache->unload();
290 $db->sql_close();
293 exit;