PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/style.php

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