PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/phpmyfaq/inc/Bootstrap.php

https://github.com/cyrke/phpMyFAQ
PHP | 265 lines | 145 code | 26 blank | 94 comment | 36 complexity | 845111b71c0dae13c8e6440ed217c163 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Bootstrap phpMyFAQ
  4. *
  5. * PHP Version 5.3
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package Configuration
  13. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  14. * @copyright 2012 phpMyFAQ Team
  15. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  16. * @link http://www.phpmyfaq.de
  17. * @since 2012-03-07
  18. */
  19. //
  20. // Debug mode:
  21. // - false debug mode disabled
  22. // - true debug mode enabled
  23. //
  24. define('DEBUG', true);
  25. if (DEBUG) {
  26. ini_set('display_errors', 1);
  27. ini_set('display_startup_errors', 1);
  28. error_reporting(E_ALL | E_STRICT);
  29. } else {
  30. error_reporting(0);
  31. }
  32. if (!defined('IS_VALID_PHPMYFAQ')) {
  33. exit();
  34. }
  35. //
  36. // Fix the PHP include path if PMF is running under a "strange" PHP configuration
  37. //
  38. $foundCurrPath = false;
  39. $includePaths = explode(PATH_SEPARATOR, ini_get('include_path'));
  40. $i = 0;
  41. while ((!$foundCurrPath) && ($i < count($includePaths))) {
  42. if ('.' == $includePaths[$i]) {
  43. $foundCurrPath = true;
  44. }
  45. $i++;
  46. }
  47. if (!$foundCurrPath) {
  48. ini_set('include_path', '.' . PATH_SEPARATOR . ini_get('include_path'));
  49. }
  50. //
  51. // Tweak some PHP configuration values
  52. // Warning: be sure the server has enough memory and stack for PHP
  53. //
  54. ini_set('pcre.backtrack_limit', 100000000);
  55. ini_set('pcre.recursion_limit', 100000000);
  56. //
  57. // Check if multisite/multisite.php exist for Multisite support
  58. //
  59. if (file_exists(__DIR__ . '/../multisite/multisite.php')) {
  60. require __DIR__ . '/../multisite/multisite.php';
  61. }
  62. //
  63. // Read configuration and constants
  64. //
  65. if (! defined('PMF_MULTI_INSTANCE_CONFIG_DIR')) {
  66. // Single instance configuration
  67. define('PMF_CONFIG_DIR', dirname(__DIR__) . '/config');
  68. } else {
  69. // Multi instance configuration
  70. define('PMF_CONFIG_DIR', PMF_MULTI_INSTANCE_CONFIG_DIR);
  71. }
  72. //
  73. // Check if config/database.php exist -> if not, redirect to installer
  74. //
  75. if (!file_exists(PMF_CONFIG_DIR . '/database.php')) {
  76. header("Location: install/setup.php");
  77. exit();
  78. }
  79. require PMF_CONFIG_DIR . '/database.php';
  80. require PMF_CONFIG_DIR . '/constants.php';
  81. //
  82. // Include Autoloader and global functions
  83. //
  84. define('PMF_INCLUDE_DIR', __DIR__);
  85. require PMF_INCLUDE_DIR . '/Autoloader.php';
  86. //
  87. // Set the error handler to our pmf_error_handler() function
  88. //
  89. set_error_handler('pmf_error_handler');
  90. //
  91. // Create a database connection
  92. //
  93. PMF_Db::setTablePrefix($DB['prefix']);
  94. $db = PMF_Db::factory($DB['type']);
  95. $db->connect($DB['server'], $DB['user'], $DB['password'], $DB['db']);
  96. //
  97. // Fetch the configuration and add the database connection
  98. //
  99. $faqConfig = new PMF_Configuration($db);
  100. $faqConfig->getAll();
  101. //
  102. // We always need a valid session!
  103. //
  104. ini_set('session.use_only_cookies', 1); // Avoid any PHP version to move sessions on URLs
  105. ini_set('session.auto_start', 0); // Prevent error to use session_start() if it's active in php.ini
  106. ini_set('session.use_trans_sid', 0);
  107. ini_set('url_rewriter.tags', '');
  108. //
  109. // Connect to LDAP server, when LDAP support is enabled
  110. //
  111. if ($faqConfig->get('security.ldapSupport') && file_exists(PMF_CONFIG_DIR . '/ldap.php')) {
  112. require PMF_CONFIG_DIR . '/constants_ldap.php';
  113. require PMF_CONFIG_DIR . '/ldap.php';
  114. $faqConfig->setLdapConfig($PMF_LDAP);
  115. } else {
  116. $ldap = null;
  117. }
  118. //
  119. // Build attachments path
  120. //
  121. $confAttachmentsPath = trim($faqConfig->get('records.attachmentsPath'));
  122. if ('/' == $confAttachmentsPath[0] || preg_match('%^[a-z]:(\\\\|/)%i', $confAttachmentsPath)) {
  123. // If we're here, some windows or unix style absolute path was detected.
  124. define('PMF_ATTACHMENTS_DIR', $confAttachmentsPath);
  125. } else {
  126. // otherwise build the absolute path
  127. $tmp = dirname(__DIR__) . DIRECTORY_SEPARATOR . $confAttachmentsPath;
  128. // Check that nobody is traversing
  129. if (0 === strpos((string)$tmp, dirname(__DIR__))) {
  130. define('PMF_ATTACHMENTS_DIR', $tmp);
  131. } else {
  132. define('PMF_ATTACHMENTS_DIR', false);
  133. }
  134. }
  135. //
  136. // Fix if phpMyFAQ is running behind a proxy server
  137. //
  138. if (! isset($_SERVER['HTTP_HOST'])) {
  139. if (isset($_SERVER['HTTP_X_FORWARDED_SERVER'])) {
  140. $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_SERVER'];
  141. } else {
  142. $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
  143. };
  144. }
  145. //
  146. // Fix undefined server variables in Windows IIS & CGI mode
  147. //
  148. if (! isset($_SERVER['SCRIPT_NAME'])) {
  149. if(isset($_SERVER['SCRIPT_FILENAME'])) {
  150. $_SERVER['SCRIPT_NAME'] = $_SERVER['SCRIPT_FILENAME'];
  151. } elseif(isset($_SERVER['PATH_TRANSLATED'])) {
  152. $_SERVER['SCRIPT_NAME'] = $_SERVER['PATH_TRANSLATED'];
  153. } elseif(isset($_SERVER['PATH_INFO'])) {
  154. $_SERVER['SCRIPT_NAME'] = $_SERVER['PATH_INFO'];
  155. } elseif(isset($_SERVER['SCRIPT_URL'])) {
  156. $_SERVER['SCRIPT_NAME'] = $_SERVER['SCRIPT_URL'];
  157. }
  158. }
  159. /**
  160. * phpMyFAQ custom error handler function, also to prevent the disclosure of
  161. * potential sensitive data.
  162. *
  163. * @access public
  164. * @param int $level The level of the error raised.
  165. * @param string $message The error message.
  166. * @param string $filename The filename that the error was raised in.
  167. * @param int $line The line number the error was raised at.
  168. * @param mixed $context It optionally contains an array of every variable
  169. * that existed in the scope the error was triggered in.
  170. *
  171. * @return bool
  172. */
  173. function pmf_error_handler($level, $message, $filename, $line, $context)
  174. {
  175. // Sanity check
  176. // Note: when DEBUG mode is true we want to track any error!
  177. if (
  178. // 1. the @ operator sets the PHP's error_reporting() value to 0
  179. (!DEBUG && (0 == error_reporting()))
  180. // 2. Honor the value of PHP's error_reporting() function
  181. || (!DEBUG && (0 == ($level & error_reporting())))
  182. ) {
  183. // Do nothing
  184. return true;
  185. }
  186. // Cleanup potential sensitive data
  187. $filename = (DEBUG ? $filename : basename($filename));
  188. $errorTypes = array(
  189. E_ERROR => 'error',
  190. E_WARNING => 'warning',
  191. E_PARSE => 'parse error',
  192. E_NOTICE => 'notice',
  193. E_CORE_ERROR => 'code error',
  194. E_CORE_WARNING => 'core warning',
  195. E_COMPILE_ERROR => 'compile error',
  196. E_COMPILE_WARNING => 'compile warning',
  197. E_USER_ERROR => 'user error',
  198. E_USER_WARNING => 'user warning',
  199. E_USER_NOTICE => 'user notice',
  200. E_STRICT => 'strict warning',
  201. E_RECOVERABLE_ERROR => 'recoverable error',
  202. E_DEPRECATED => 'deprecated warning',
  203. E_USER_DEPRECATED => 'user deprecated warning',
  204. );
  205. $errorType = 'unknown error';
  206. if (isset($errorTypes[$level])) {
  207. $errorType = $errorTypes[$level];
  208. }
  209. // Custom error message
  210. $errorMessage = <<<EOD
  211. <br />
  212. <b>phpMyFAQ $errorType</b> [$level]: $message in <b>$filename</b> on line <b>$line</b><br />
  213. EOD;
  214. if (ini_get('display_errors')) {
  215. print $errorMessage;
  216. }
  217. if (ini_get('log_errors')) {
  218. error_log(sprintf('phpMyFAQ %s: %s in %s on line %d',
  219. $errorType,
  220. $message,
  221. $filename,
  222. $line));
  223. }
  224. switch ($level) {
  225. // Blocking errors
  226. case E_ERROR:
  227. case E_PARSE:
  228. case E_CORE_ERROR:
  229. case E_COMPILE_ERROR:
  230. case E_USER_ERROR:
  231. // Prevent processing any more PHP scripts
  232. exit();
  233. break;
  234. // Not blocking errors
  235. default:
  236. break;
  237. }
  238. return true;
  239. }