(a little test for later merges)
[phpbb.git] / phpBB / style.php
blob0b4c3d0d416517e8fb1056b22dadf3a7b634bf68
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);
21 require($phpbb_root_path . 'config.' . $phpEx);
23 if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
25 exit;
28 if (version_compare(PHP_VERSION, '6.0.0-dev', '<'))
30 @set_magic_quotes_runtime(0);
33 // Load Extensions
34 if (!empty($load_extensions))
36 $load_extensions = explode(',', $load_extensions);
38 foreach ($load_extensions as $extension)
40 @dl(trim($extension));
45 $sid = (isset($_GET['sid']) && !is_array($_GET['sid'])) ? htmlspecialchars($_GET['sid']) : '';
46 $id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
48 if (strspn($sid, 'abcdefABCDEF0123456789') !== strlen($sid))
50 $sid = '';
53 // This is a simple script to grab and output the requested CSS data stored in the DB
54 // We include a session_id check to try and limit 3rd party linking ... unless they
55 // happen to have a current session it will output nothing. We will also cache the
56 // resulting CSS data for five minutes ... anything to reduce the load on the SQL
57 // server a little
58 if ($id)
60 // Include files
61 require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
62 require($phpbb_root_path . 'includes/cache.' . $phpEx);
63 require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
64 require($phpbb_root_path . 'includes/constants.' . $phpEx);
65 require($phpbb_root_path . 'includes/functions.' . $phpEx);
67 $db = new $sql_db();
68 $cache = new cache();
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 = ($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);
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);
110 if (!$theme)
112 exit;
115 if ($user['user_id'] == ANONYMOUS)
117 $user['user_lang'] = $config['default_lang'];
120 $user_image_lang = (file_exists($phpbb_root_path . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user['user_lang'])) ? $user['user_lang'] : $config['default_lang'];
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);
130 $img_array = array();
131 while ($row = $db->sql_fetchrow($result))
133 $img_array[$row['image_name']] = $row;
135 $db->sql_freeresult($result);
137 // gzip_compression
138 if ($config['gzip_compress'])
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'])) : '';
143 if ($browser && strpos($browser, 'msie 6.0') === false && @extension_loaded('zlib') && !headers_sent())
145 ob_start('ob_gzhandler');
149 // Expire time of seven days if not recached
150 $expire_time = 7*86400;
151 $recache = false;
153 // Re-cache stylesheet data if necessary
154 if ($recompile || empty($theme['theme_data']))
156 $recache = (empty($theme['theme_data'])) ? true : false;
157 $update_time = time();
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'))
162 $recache = true;
163 $update_time = @filemtime("{$phpbb_root_path}styles/" . $theme['theme_path'] . '/theme/stylesheet.css');
165 else if (!$recache)
167 $last_change = $theme['theme_mtime'];
168 $dir = @opendir("{$phpbb_root_path}styles/{$theme['theme_path']}/theme");
170 if ($dir)
172 while (($entry = readdir($dir)) !== false)
174 if (substr(strrchr($entry, '.'), 1) == 'css' && $last_change < @filemtime("{$phpbb_root_path}styles/{$theme['theme_path']}/theme/{$entry}"))
176 $recache = true;
177 break;
180 closedir($dir);
185 if ($recache)
187 include_once($phpbb_root_path . 'includes/acp/acp_styles.' . $phpEx);
189 $theme['theme_data'] = acp_styles::db_theme_data($theme);
190 $theme['theme_mtime'] = $update_time;
192 // Save CSS contents
193 $sql_ary = array(
194 'theme_mtime' => $theme['theme_mtime'],
195 'theme_data' => $theme['theme_data']
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);
202 $cache->destroy('sql', STYLES_THEME_TABLE);
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))
208 header('Expires: 0');
210 else
212 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time));
215 header('Content-type: text/css; charset=UTF-8');
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']
227 $theme['theme_data'] = str_replace(array_keys($replace), array_values($replace), $theme['theme_data']);
229 $matches = array();
230 preg_match_all('#\{IMG_([A-Za-z0-9_]*?)_(WIDTH|HEIGHT|SRC)\}#', $theme['theme_data'], $matches);
232 $imgs = $find = $replace = array();
233 if (isset($matches[0]) && sizeof($matches[0]))
235 foreach ($matches[1] as $i => $img)
237 $img = strtolower($img);
238 $find[] = $matches[0][$i];
240 if (!isset($img_array[$img]))
242 $replace[] = '';
243 continue;
246 if (!isset($imgs[$img]))
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'],
257 switch ($matches[2][$i])
259 case 'SRC':
260 $replace[] = $imgs[$img]['src'];
261 break;
263 case 'WIDTH':
264 $replace[] = $imgs[$img]['width'];
265 break;
267 case 'HEIGHT':
268 $replace[] = $imgs[$img]['height'];
269 break;
271 default:
272 continue;
276 if (sizeof($find))
278 $theme['theme_data'] = str_replace($find, $replace, $theme['theme_data']);
282 echo $theme['theme_data'];
284 if (!empty($cache))
286 $cache->unload();
288 $db->sql_close();
291 exit;