PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/include/common.php

https://github.com/gencer/fluxbb
PHP | 205 lines | 111 code | 45 blank | 49 comment | 23 complexity | 9d646cc9c92e4e3f3a8f35114c96d78b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2012 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. if (!defined('PUN_ROOT'))
  8. exit('The constant PUN_ROOT must be defined and point to a valid FluxBB installation root directory.');
  9. // Define the version and database revision that this code was written for
  10. define('FORUM_VERSION', '1.4.8');
  11. define('FORUM_DB_REVISION', 15);
  12. define('FORUM_SI_REVISION', 2);
  13. define('FORUM_PARSER_REVISION', 2);
  14. // Block prefetch requests
  15. if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
  16. {
  17. header('HTTP/1.1 403 Prefetching Forbidden');
  18. // Send no-cache headers
  19. header('Expires: Thu, 21 Jul 1977 07:30:00 GMT'); // When yours truly first set eyes on this world! :)
  20. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  21. header('Cache-Control: post-check=0, pre-check=0', false);
  22. header('Pragma: no-cache'); // For HTTP/1.0 compatibility
  23. exit;
  24. }
  25. // Attempt to load the configuration file config.php
  26. $flux_config = file_exists(PUN_ROOT.'config.php') ? require PUN_ROOT.'config.php' : array();
  27. // Load the functions script
  28. require PUN_ROOT.'include/functions.php';
  29. // Load UTF-8 functions
  30. require PUN_ROOT.'modules/utf8/php-utf8.php';
  31. require PUN_ROOT.'modules/utf8/functions/trim.php';
  32. require_once PUN_ROOT.'modules/utf8/utils/patterns.php'; // might be already loaded by the php-utf8.php file when using mbstring extension
  33. require_once PUN_ROOT.'modules/utf8/utils/bad.php'; // might be already loaded by the php-utf8.php file when using mbstring extension
  34. // Strip out "bad" UTF-8 characters
  35. forum_remove_bad_characters();
  36. // Reverse the effect of register_globals
  37. forum_unregister_globals();
  38. // If PUN isn't defined, config.php is missing or corrupt
  39. if (empty($flux_config))
  40. {
  41. header('Location: install.php');
  42. exit;
  43. }
  44. // Record the start time (will be used to calculate the generation time for the page)
  45. $pun_start = get_microtime();
  46. // Make sure PHP reports all errors when in debug mode
  47. if (defined('PUN_DEBUG'))
  48. error_reporting(E_ALL);
  49. // Force POSIX locale (to prevent functions such as strtolower() from messing up UTF-8 strings)
  50. setlocale(LC_CTYPE, 'C');
  51. // Turn off magic_quotes_runtime
  52. if (get_magic_quotes_runtime())
  53. set_magic_quotes_runtime(0);
  54. // Strip slashes from GET/POST/COOKIE/REQUEST/FILES (if magic_quotes_gpc is enabled)
  55. if (!defined('FORUM_DISABLE_STRIPSLASHES') && get_magic_quotes_gpc())
  56. {
  57. function stripslashes_array($array)
  58. {
  59. return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
  60. }
  61. $_GET = stripslashes_array($_GET);
  62. $_POST = stripslashes_array($_POST);
  63. $_COOKIE = stripslashes_array($_COOKIE);
  64. $_REQUEST = stripslashes_array($_REQUEST);
  65. if (is_array($_FILES))
  66. {
  67. // Don't strip valid slashes from tmp_name path on Windows
  68. foreach ($_FILES AS $key => $value)
  69. $_FILES[$key]['tmp_name'] = str_replace('\\', '\\\\', $value['tmp_name']);
  70. $_FILES = stripslashes_array($_FILES);
  71. }
  72. }
  73. // If a cookie name is not specified in config.php, we use the default (pun_cookie)
  74. if (empty($cookie_name))
  75. $cookie_name = 'pun_cookie';
  76. // Load the cache module
  77. require PUN_ROOT.'modules/cache/src/cache.php';
  78. $cache = \fluxbb\cache\Cache::load($flux_config['cache']['type'], $flux_config['cache'], $flux_config['cache']['serializer']['type'], $flux_config['cache']['serializer']);
  79. // Define a few commonly used constants
  80. define('PUN_UNVERIFIED', 0);
  81. define('PUN_ADMIN', 1);
  82. define('PUN_MOD', 2);
  83. define('PUN_GUEST', 3);
  84. define('PUN_MEMBER', 4);
  85. // Load the DB module
  86. require PUN_ROOT.'modules/database/src/Database/Adapter.php';
  87. $db_options = array_merge($flux_config['db'], array('debug' => defined('PUN_DEBUG')));
  88. $db = \fluxbb\database\Adapter::factory($flux_config['db']['type'], $db_options);
  89. // Start a transaction
  90. $db->startTransaction();
  91. // Load cached config
  92. $pun_config = $cache->remember('config', function() use ($db) {
  93. $cfg = array();
  94. // Get the forum config from the DB
  95. $query = $db->select(array('conf_name' => 'c.conf_name', 'conf_value' => 'c.conf_value'), 'config AS c');
  96. $params = array();
  97. $result = $query->run($params);
  98. foreach ($result as $cur_config_item)
  99. $cfg[$cur_config_item['conf_name']] = $cur_config_item['conf_value'];
  100. unset ($query, $params, $result);
  101. return $cfg;
  102. });
  103. // Verify that we are running the proper database schema revision
  104. /*if (!isset($pun_config['o_database_revision']) || $pun_config['o_database_revision'] < FORUM_DB_REVISION ||
  105. !isset($pun_config['o_searchindex_revision']) || $pun_config['o_searchindex_revision'] < FORUM_SI_REVISION ||
  106. !isset($pun_config['o_parser_revision']) || $pun_config['o_parser_revision'] < FORUM_PARSER_REVISION ||
  107. version_compare($pun_config['o_cur_version'], FORUM_VERSION, '<'))
  108. {
  109. header('Location: db_update.php');
  110. exit;
  111. }*/
  112. // Enable output buffering
  113. if (!defined('PUN_DISABLE_BUFFERING'))
  114. {
  115. // Should we use gzip output compression?
  116. if ($pun_config['o_gzip'] && extension_loaded('zlib'))
  117. ob_start('ob_gzhandler');
  118. else
  119. ob_start();
  120. }
  121. // Define standard date/time formats
  122. $forum_time_formats = array($pun_config['o_time_format'], 'H:i:s', 'H:i', 'g:i:s a', 'g:i a');
  123. $forum_date_formats = array($pun_config['o_date_format'], 'Y-m-d', 'Y-d-m', 'd-m-Y', 'm-d-Y', 'M j Y', 'jS M Y');
  124. // Check/update/set cookie and fetch user info
  125. $pun_user = array();
  126. check_cookie($pun_user);
  127. // Load the language system
  128. require PUN_ROOT.'include/classes/lang.php';
  129. Flux_Lang::setDefaultLanguage('en');
  130. $lang = new Flux_Lang($pun_user['language']);
  131. // Load the common language file
  132. $lang->load('common');
  133. // Check if we are to display a maintenance message
  134. if ($pun_config['o_maintenance'] && $pun_user['g_id'] > PUN_ADMIN && !defined('PUN_TURN_OFF_MAINT'))
  135. maintenance_message();
  136. // Load cached bans
  137. $pun_bans = $cache->remember('bans', function() use ($db) {
  138. // Get the ban list from the DB
  139. $query = $db->select(array('id' => 'b.id', 'username' => 'b.username', 'ip' => 'b.ip', 'email' => 'b.email', 'message' => 'b.message', 'expire' => 'b.expire', 'ban_creator' => 'b.ban_creator'), 'bans AS b');
  140. $params = array();
  141. $bans = $query->run($params);
  142. unset ($query, $params);
  143. return $bans;
  144. });
  145. // Check if current user is banned
  146. check_bans();
  147. // Update online list
  148. update_users_online();
  149. // Check to see if we logged in without a cookie being set
  150. if ($pun_user['is_guest'] && isset($_GET['login']))
  151. message($lang->t('No cookie'));
  152. // The maximum size of a post, in bytes, since the field is now MEDIUMTEXT this allows ~16MB but lets cap at 1MB...
  153. if (!defined('PUN_MAX_POSTSIZE'))
  154. define('PUN_MAX_POSTSIZE', 1048576);
  155. if (!defined('PUN_SEARCH_MIN_WORD'))
  156. define('PUN_SEARCH_MIN_WORD', 3);
  157. if (!defined('PUN_SEARCH_MAX_WORD'))
  158. define('PUN_SEARCH_MAX_WORD', 20);
  159. if (!defined('FORUM_MAX_COOKIE_SIZE'))
  160. define('FORUM_MAX_COOKIE_SIZE', 4048);