erm, revert last commit and add relevant comment. ;) This actually allows us to do...
[phpbb.git] / phpBB / style.php
blobec8c7881282d8ac3b64aed29aefbe117f244d693
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 if (!defined('PHPBB_ROOT_PATH')) define('PHPBB_ROOT_PATH', './');
16 if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
18 $id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
20 // This is a simple script to grab and output the requested CSS data stored in the DB
21 // We cache the resulting CSS data for five minutes
22 if (!$id)
24 exit;
27 include(PHPBB_ROOT_PATH . 'common.' . PHP_EXT);
29 $sql = 'SELECT s.style_id, c.theme_id, c.theme_data, c.theme_path, c.theme_name, c.theme_mtime, i.*, t.template_path
30 FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i
31 WHERE s.style_id = ' . $id . '
32 AND t.template_id = s.template_id
33 AND c.theme_id = s.theme_id
34 AND i.imageset_id = s.imageset_id';
35 $result = phpbb::$db->sql_query($sql, 300);
36 $theme = phpbb::$db->sql_fetchrow($result);
37 phpbb::$db->sql_freeresult($result);
39 if (!$theme)
41 garbage_collection();
42 exit_handler();
45 $user_lang = basename(request_var('lang', phpbb::$config['default_lang']));
46 $user_image_lang = (file_exists(PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user_lang)) ? $user_lang : phpbb::$config['default_lang'];
48 $sql = 'SELECT *
49 FROM ' . STYLES_IMAGESET_DATA_TABLE . '
50 WHERE imageset_id = ' . $theme['imageset_id'] . "
51 AND image_filename <> ''
52 AND image_lang IN ('" . phpbb::$db->sql_escape($user_image_lang) . "', '')";
53 $result = phpbb::$db->sql_query($sql, 3600);
55 $img_array = array();
56 while ($row = phpbb::$db->sql_fetchrow($result))
58 $img_array[$row['image_name']] = $row;
60 phpbb::$db->sql_freeresult($result);
62 // gzip_compression
63 if (phpbb::$config['gzip_compress'])
65 if (strpos(strtolower(phpbb::$user->system['browser']), 'msie 6.0') === false && @extension_loaded('zlib') && !headers_sent())
67 ob_start('ob_gzhandler');
71 // Expire time of seven days if not recached
72 $expire_time = 7 * 86400;
73 $recache = false;
75 // Re-cache stylesheet data if necessary
76 if (phpbb::$config['load_tplcompile'] || empty($theme['theme_data']))
78 $recache = (empty($theme['theme_data'])) ? true : false;
79 $update_time = time();
81 // We test for stylesheet.css because it is faster and most likely the only file changed on common themes
82 if (!$recache && $theme['theme_mtime'] < @filemtime(PHPBB_ROOT_PATH . 'styles/' . $theme['theme_path'] . '/theme/stylesheet.css'))
84 $recache = true;
85 $update_time = @filemtime(PHPBB_ROOT_PATH . 'styles/' . $theme['theme_path'] . '/theme/stylesheet.css');
87 else if (!$recache)
89 $last_change = $theme['theme_mtime'];
90 $dir = @opendir(PHPBB_ROOT_PATH . "styles/{$theme['theme_path']}/theme");
92 if ($dir)
94 while (($entry = readdir($dir)) !== false)
96 if (substr(strrchr($entry, '.'), 1) == 'css' && $last_change < @filemtime(PHPBB_ROOT_PATH . "styles/{$theme['theme_path']}/theme/{$entry}"))
98 $recache = true;
99 break;
102 closedir($dir);
107 // We store new data within database
108 if ($recache)
110 if (!class_exists('acp_styles'))
112 include PHPBB_ROOT_PATH . 'modules/acp/acp_styles.' . PHP_EXT;
115 $theme['theme_data'] = acp_styles::db_theme_data($theme);
116 $theme['theme_mtime'] = $update_time;
118 // Save CSS contents
119 $sql_ary = array(
120 'theme_mtime' => $theme['theme_mtime'],
121 'theme_data' => $theme['theme_data']
124 phpbb::$db->sql_handle_data('UPDATE', STYLES_THEME_TABLE, $sql_ary, 'theme_id = ' . $theme['theme_id']);
125 phpbb::$acm->destroy_sql(STYLES_THEME_TABLE);
128 // Only set the expire time if the theme changed data is older than 30 minutes - to cope with changes from the ACP
129 if ($recache || $theme['theme_mtime'] > (time() - 1800))
131 header('Expires: 0');
133 else
135 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time));
138 header('Content-type: text/css; charset=UTF-8');
140 // Parse Theme Data
141 $replace = array(
142 '{T_THEME_PATH}' => PHPBB_ROOT_PATH . 'styles/' . $theme['theme_path'] . '/theme',
143 '{T_TEMPLATE_PATH}' => PHPBB_ROOT_PATH . 'styles/' . $theme['template_path'] . '/template',
144 '{T_IMAGESET_PATH}' => PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset',
145 '{T_IMAGESET_LANG_PATH}' => PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset/' . $user_image_lang,
146 '{T_STYLESHEET_NAME}' => $theme['theme_name'],
147 '{S_USER_LANG}' => $user_lang,
150 $theme['theme_data'] = str_replace(array_keys($replace), array_values($replace), $theme['theme_data']);
152 // Replace IMG_*_WIDTH/HEIGHT/SRC tags
153 $matches = array();
154 preg_match_all('#\{IMG_([A-Za-z0-9_]*?)_(WIDTH|HEIGHT|SRC)\}#', $theme['theme_data'], $matches);
156 $imgs = $find = $replace = array();
157 if (isset($matches[0]) && sizeof($matches[0]))
159 foreach ($matches[1] as $i => $img)
161 $img = strtolower($img);
162 $find[] = $matches[0][$i];
164 if (!isset($img_array[$img]))
166 $replace[] = '';
167 continue;
170 if (!isset($imgs[$img]))
172 $img_data = &$img_array[$img];
173 $imgsrc = ($img_data['image_lang'] ? $img_data['image_lang'] . '/' : '') . $img_data['image_filename'];
175 $imgs[$img] = array(
176 'src' => PHPBB_ROOT_PATH . 'styles/' . $theme['imageset_path'] . '/imageset/' . $imgsrc,
177 'width' => $img_data['image_width'],
178 'height' => $img_data['image_height'],
182 switch ($matches[2][$i])
184 case 'SRC':
185 $replace[] = $imgs[$img]['src'];
186 break;
188 case 'WIDTH':
189 $replace[] = $imgs[$img]['width'];
190 break;
192 case 'HEIGHT':
193 $replace[] = $imgs[$img]['height'];
194 break;
196 default:
197 continue;
201 if (sizeof($find))
203 $theme['theme_data'] = str_replace($find, $replace, $theme['theme_data']);
207 echo $theme['theme_data'];
209 garbage_collection();
210 exit_handler();