PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/patchwork/pieces/agent-controller/bootup.patchwork.php

https://github.com/nicolas-grekas/Patchwork-sandbox
PHP | 154 lines | 96 code | 41 blank | 17 comment | 15 complexity | e4f7aed3579df21a0655e01ee8ed1c6f MD5 | raw file
  1. <?php // vi: set fenc=utf-8 ts=4 sw=4 et:
  2. /*
  3. * Copyright (C) 2012 Nicolas Grekas - p@tchwork.com
  4. *
  5. * This library is free software; you can redistribute it and/or modify it
  6. * under the terms of the (at your option):
  7. * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
  8. * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
  9. */
  10. // Salt for cache invalidation control
  11. /**/$a = 0;
  12. /**/for ($b = 0; $b <= PATCHWORK_PATH_LEVEL; ++$b) $a += filemtime($patchwork_path[$b] . 'config.patchwork.php');
  13. $patchwork_appId = (int) /*<*/sprintf('%020d', $a)/*>*/;
  14. // Timezone settings
  15. /**/if (!ini_get('date.timezone'))
  16. @date_default_timezone_set(date_default_timezone_get());
  17. // Configure PCRE
  18. /**/if (ini_get('pcre.backtrack_limit') < 5000000)
  19. ini_set('pcre.backtrack_limit', 5000000);
  20. /**/if (ini_get('pcre.recursion_limit') < 10000)
  21. ini_set('pcre.recursion_limit', 10000);
  22. // Helper functions
  23. function strlencmp($a, $b) {return strlen($b) - strlen($a);}
  24. function patchwork_http_socket($host, $port, $ssl, $timeout = 30)
  25. {
  26. if ($port <= 0) $port = $ssl ? '443' : '80';
  27. $ssl = $ssl ? 'ssl' : 'tcp';
  28. false !== strpos($host, ':') && $host = '[' . $host . ']';
  29. strspn(substr($host, -1), '0123456789]:.') || $host .= '.';
  30. $h = fsockopen("{$ssl}://{$host}", $port, $errno, $errstr, $timeout);
  31. if (!$h) throw new Exception("Socket error n°{$errno}: {$errstr}");
  32. return $h;
  33. }
  34. // Check HTTP validator
  35. $patchwork_private = false;
  36. /**/unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
  37. $a = isset($_SERVER['HTTP_IF_NONE_MATCH'])
  38. ? $_SERVER['HTTP_IF_NONE_MATCH']
  39. : isset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  40. if ($a)
  41. {
  42. if (true === $a)
  43. {
  44. // Patch an IE<=6 bug when using ETag + compression
  45. $a = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'], 2);
  46. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = $a = strtotime($a[0]);
  47. $_SERVER['HTTP_IF_NONE_MATCH'] = '"' . dechex($a) . '"';
  48. $patchwork_private = true;
  49. }
  50. else if (27 === strlen($a) && 25 === strspn($a, '0123456789abcdef') && '""' === $a[0] . $a[26])
  51. {
  52. $b = PATCHWORK_ZCACHE . $a[1] .'/'. $a[2] .'/'. substr($a, 3, 6) .'.v.txt';
  53. if (file_exists($b) && substr(file_get_contents($b), 0, 8) === substr($a, 9, 8))
  54. {
  55. $private = substr($a, 17, 1);
  56. $maxage = hexdec(substr($a, 18, 8));
  57. header('HTTP/1.1 304 Not Modified');
  58. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + ($private || !$maxage ? 0 : $maxage)));
  59. header('Cache-Control: max-age=' . $maxage . ($private ? ',private,must' : ',public,proxy') . '-revalidate');
  60. exit;
  61. }
  62. }
  63. }
  64. // Disables mod_deflate who overwrites any custom Vary: header and appends a body to 304 responses.
  65. // Replaced with our own output compression.
  66. /**/if (function_exists('apache_setenv'))
  67. apache_setenv('no-gzip','1');
  68. /**/if (ini_get_bool('zlib.output_compression'))
  69. ini_set('zlib.output_compression', false);
  70. // PHP session mechanism overloading
  71. class Patchwork_SessionHandler implements ArrayAccess
  72. {
  73. function offsetGet($k) {$_SESSION = SESSION::getAll(); return $_SESSION[$k];}
  74. function offsetSet($k, $v) {$_SESSION = SESSION::getAll(); $_SESSION[$k] =& $v;}
  75. function offsetExists($k) {$_SESSION = SESSION::getAll(); return isset($_SESSION[$k]);}
  76. function offsetUnset($k) {$_SESSION = SESSION::getAll(); unset($_SESSION[$k]);}
  77. static $id;
  78. static function close() {return true;}
  79. static function gc($life) {return true;}
  80. static function open($path, $name)
  81. {
  82. session_cache_limiter('');
  83. ini_set('session.use_only_cookies', true);
  84. ini_set('session.use_cookies', false);
  85. ini_set('session.use_trans_sid', false);
  86. return true;
  87. }
  88. static function read($id)
  89. {
  90. $_SESSION = SESSION::getAll();
  91. self::$id = $id;
  92. return '';
  93. }
  94. static function write($id, $data)
  95. {
  96. if (self::$id != $id) SESSION::regenerateId();
  97. return true;
  98. }
  99. static function destroy($id)
  100. {
  101. SESSION::regenerateId(true);
  102. return true;
  103. }
  104. }
  105. session_set_save_handler(
  106. array($k = 'Patchwork_SessionHandler', 'open'),
  107. array($k, 'close'),
  108. array($k, 'read'),
  109. array($k, 'write'),
  110. array($k, 'destroy'),
  111. array($k, 'gc')
  112. );
  113. $_SESSION = new Patchwork_SessionHandler;