PageRenderTime 86ms CodeModel.GetById 33ms RepoModel.GetById 2ms app.codeStats 0ms

/includes/fix.inc.php

https://bitbucket.org/djl/flyspray-mirror
PHP | 253 lines | 137 code | 59 blank | 57 comment | 37 complexity | 9aa86cb6ba0193aa8b3aefdd5f95f18f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /*
  3. * This file is meant to add every hack that is needed to fix default PHP
  4. * behaviours, and to ensure that our PHP env will be able to run flyspray
  5. * correctly.
  6. *
  7. */
  8. ini_set('display_errors', 1);
  9. // html errors will mess the layout
  10. ini_set('html_errors', 0);
  11. error_reporting(E_ALL);
  12. // our default charset
  13. ini_set('default_charset','utf-8');
  14. // This to stop PHP being retarded and using the '&' char for session id delimiters
  15. ini_set('arg_separator.output','&amp;');
  16. // MySQLi driver is _useless_ if zend.ze1_compatibility_mode is enabled
  17. // in fact you should never use this setting,the damn thing does not work.
  18. ini_set('zend.ze1_compatibility_mode',0);
  19. //we don't want magic_quotes_runtime ..
  20. ini_set('magic_quotes_runtime',0);
  21. //this one too
  22. ini_set('magic_quotes_sybase',0);
  23. // no transparent session id improperly configured servers
  24. @ini_set('session.use_trans_sid', 0); // might cause error in setup
  25. //see http://php.net/manual/en/ref.session.php#ini.session.use-only-cookies
  26. ini_set('session.use_only_cookies',1);
  27. //no session auto start
  28. ini_set('session.auto_start',0);
  29. /*this stops most cookie attacks via XSS at the interpreter level
  30. * see http://msdn.microsoft.com/workshop/author/dhtml/httponly_cookies.asp
  31. * supported by IE 6 SP1, Safari, Konqueror, Opera, silently ignored by others
  32. * ( sadly, including firefox) available since PHP 5.2.0
  33. */
  34. ini_set('session.cookie_httponly',1);
  35. ini_set('include_path', join( PATH_SEPARATOR, array(
  36. dirname(__FILE__) ,
  37. dirname(__FILE__) . '/external' ,
  38. dirname(__FILE__) . '/external/swift-mailer',
  39. dirname(__FILE__) . '/external/compat',
  40. ini_get('include_path'))));
  41. // we live is register_globals Off world forever..
  42. //This code was written By Stefan Esser from the hardened PHP project (sesser@php.net)
  43. // it's now part of the PHP manual
  44. function unregister_GLOBALS()
  45. {
  46. if (!ini_get('register_globals')) {
  47. return;
  48. }
  49. // Might want to change this perhaps to a nicer error
  50. if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
  51. die('GLOBALS overwrite attempt detected');
  52. }
  53. // Variables that shouldn't be unset
  54. $noUnset = array('GLOBALS', '_GET',
  55. '_POST', '_COOKIE',
  56. '_REQUEST', '_SERVER',
  57. '_ENV', '_FILES');
  58. $input = array_merge($_GET, $_POST,
  59. $_COOKIE, $_SERVER,
  60. $_ENV, $_FILES,
  61. isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  62. foreach ($input as $k => $v) {
  63. if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
  64. unset($GLOBALS[$k]);
  65. /* no, this is not a bug, we use double unset() .. it is to circunvent
  66. /* this PHP critical vulnerability
  67. * http://www.hardened-php.net/hphp/zend_hash_del_key_or_index_vulnerability.html
  68. * this is intended to minimize the catastrophic effects that has on systems with
  69. * register_globals on.. users with register_globals off are still vulnerable but
  70. * afaik,there is nothing we can do for them.
  71. */
  72. unset($GLOBALS[$k]);
  73. }
  74. }
  75. }
  76. unregister_GLOBALS();
  77. /*unless we want to use this in the future, get rid of the
  78. * the PHP >= 5.2 , input filter extension, if not, it
  79. * will mess with user input if sysadmin or webmaster use a filter different
  80. * than the default.
  81. * This is based on work by Tobias Schlitt <toby@php.net> available under
  82. * the BSD license, but has been slightly modified for Flyspray.
  83. */
  84. if (PHP_VERSION >= 5.2 && extension_loaded('filter') && filter_id(ini_get('filter.default')) !== FILTER_UNSAFE_RAW) {
  85. if(count($_GET)) {
  86. foreach ($_GET as $key => $value) {
  87. $_GET[$key] = filter_input(INPUT_GET, $key, FILTER_UNSAFE_RAW);
  88. }
  89. }
  90. if(count($_POST)) {
  91. foreach ($_POST as $key => $value) {
  92. $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_UNSAFE_RAW);
  93. }
  94. }
  95. if(count($_COOKIE)) {
  96. foreach ($_COOKIE as $key => $value) {
  97. $_COOKIE[$key] = filter_input(INPUT_COOKIE, $key, FILTER_UNSAFE_RAW);
  98. }
  99. }
  100. if(isset($_SESSION) && is_array($_SESSION) && count($_SESSION)) {
  101. foreach ($_SESSION as $key => $value) {
  102. $_SESSION[$key] = filter_input(INPUT_SESSION, $key, FILTER_UNSAFE_RAW);
  103. }
  104. }
  105. }
  106. // This is for retarded Windows servers not having REQUEST_URI
  107. if (!isset($_SERVER['REQUEST_URI']))
  108. {
  109. if (isset($_SERVER['SCRIPT_NAME'])) {
  110. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
  111. }
  112. else {
  113. // this is tained now.
  114. $_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
  115. }
  116. if (isset($_SERVER['QUERY_STRING'])) {
  117. $_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
  118. }
  119. }
  120. if (!isset($_SERVER['QUERY_STRING']))
  121. {
  122. $_SERVER['QUERY_STRING'] = '';
  123. }
  124. /* we also don't want magic_quotes_gpc at all
  125. * this code was written by Ilia Alshanetsky <iilia@php.net>
  126. * is licensed under the BSD.
  127. */
  128. function undo_magic_quotes(&$var)
  129. {
  130. if (is_array($var)) {
  131. foreach ($var as $k => $v) {
  132. if (is_array($v)) {
  133. array_walk($var[$k], 'undo_magic_quotes');
  134. } else {
  135. $var[$k] = stripslashes($v);
  136. }
  137. }
  138. } else {
  139. $var = stripslashes($var);
  140. }
  141. }
  142. if (ini_get('magic_quotes_gpc')) {
  143. if (count($_REQUEST)) {
  144. array_walk($_REQUEST, 'undo_magic_quotes');
  145. }
  146. if (count($_GET)) {
  147. array_walk($_GET, 'undo_magic_quotes');
  148. }
  149. if (count($_POST)) {
  150. array_walk($_POST, 'undo_magic_quotes');
  151. }
  152. if (count($_COOKIE)) {
  153. array_walk($_COOKIE, 'undo_magic_quotes');
  154. }
  155. if (count($_FILES) && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
  156. array_walk($_FILES, 'undo_magic_quotes');
  157. }
  158. }
  159. /**
  160. * Replace glob() since this function is apparently
  161. * disabled for no apparent reason ("security") on some systems
  162. *
  163. * @see glob()
  164. */
  165. function glob_compat($pattern, $flags = 0) {
  166. if(in_array('glob', explode(',', ini_get('disable_functions'))) || !function_exists('glob')) {
  167. include 'glob.php';
  168. return php_compat_glob($pattern, $flags);
  169. }
  170. return glob($pattern, $flags);
  171. }
  172. // now for all those borked PHP installations...
  173. if (!function_exists('hash_hmac')) {
  174. function hash_hmac($algo, $data, $key, $raw_output = false) {
  175. if(function_exists('mhash') && $algo == 'md5') {
  176. return $raw_output ? mhash(MHASH_MD5, $data, $key) : bin2hex(mhash(MHASH_MD5, $data, $key));
  177. }
  178. include_once 'HMAC.php';
  179. $hashobj =& new Crypt_HMAC($key, $algo);
  180. return $raw_output ? pack('H*', $hashobj->hash($data)) : $hashobj->hash($data);
  181. }
  182. }
  183. // for reasons outside flsypray, the PHP core may throw Exceptions in PHP5
  184. // for a good example see this article
  185. // http://ilia.ws/archives/107-Another-unserialize-abuse.html
  186. function flyspray_exception_handler($exception) {
  187. die("Completely unexpected exception: " .
  188. htmlspecialchars($exception->getMessage(),ENT_QUOTES, 'utf-8') . "<br/>" .
  189. "This should <strong> never </strong> happend, please inform Flyspray Developers");
  190. }
  191. set_exception_handler('flyspray_exception_handler');
  192. // We don't need session IDs in URLs
  193. output_reset_rewrite_vars();
  194. ?>