PageRenderTime 69ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/nicolas-grekas/Patchwork-sandbox
PHP | 359 lines | 279 code | 51 blank | 29 comment | 57 complexity | 771908158cfea207ca15760ad35c3420 MD5 | raw file
  1. <?php // vi: set fenc=utf-8 ts=4 sw=4 et:
  2. #patchwork __patchwork__/core/http
  3. #patchwork __patchwork__/core/superloader
  4. #patchwork __patchwork__/core/logger
  5. // Default settings
  6. $CONFIG += array(
  7. // General
  8. 'umask' => false, // Set the user file creation mode mask
  9. // Patchwork
  10. 'clientside' => true, // Enable browser-side page rendering when available
  11. 'i18n.lang_list' => '', // List of available languages ('en|fr' for example)
  12. 'maxage' => 2678400, // Max age (in seconds) for HTTP ressources caching
  13. 'P3P' => 'CUR ADM', // P3P - Platform for Privacy Preferences
  14. 'xsendfile' => false, // "X-Sendfile" enabling pattern
  15. 'document.domain' => '', // Value of document.domain for clientside cross subdomain communication
  16. 'X-UA-Compatible' => 'IE=edge,chrome=1', // X-UA-Compatible - by default use chrome frame or latest IE engine
  17. // Session
  18. 'session.save_path' => PATCHWORK_ZCACHE,
  19. 'session.cookie_path' => 'auto',
  20. 'session.cookie_domain' => 'auto',
  21. 'session.auth_vars' => array(), // Set of session vars used for authentication or authorization
  22. 'session.group_vars' => array(), // Set of session vars whose values define user groups
  23. // Translation adapter
  24. 'translator.adapter' => false,
  25. 'translator.options' => array(),
  26. );
  27. // Setup patchwork's environment
  28. Patchwork\FunctionOverride(header, Patchwork::header, $s, $replace = true, $response_code = null);
  29. empty($CONFIG['umask']) || umask($CONFIG['umask']);
  30. empty($CONFIG['xsendfile']) && isset($_SERVER['PATCHWORK_XSENDFILE']) && $CONFIG['xsendfile'] = $_SERVER['PATCHWORK_XSENDFILE'];
  31. // Prepare for I18N
  32. $a =& $CONFIG['i18n.lang_list'];
  33. $a ? (is_array($a) || $a = explode('|', $a)) : ($a = array('' => '__'));
  34. define('PATCHWORK_I18N', 2 <= count($a));
  35. $b = array();
  36. foreach ($a as $k => &$v)
  37. {
  38. if (is_int($k))
  39. {
  40. $v = (string) $v;
  41. if (!isset($a[$v]))
  42. {
  43. $a[$v] = $v;
  44. $b[] = preg_quote($v, '#');
  45. }
  46. unset($a[$k]);
  47. }
  48. else $b[] = preg_quote($v, '#');
  49. }
  50. unset($a, $v);
  51. usort($b, 'strlencmp');
  52. $b = '(' . implode('|', $b) . ')';
  53. /* patchwork's context initialization
  54. *
  55. * Setup needed environment variables if they don't exists :
  56. * $_SERVER['PATCHWORK_BASE']: application's base part of the url. Lang independant (ex. /myapp/__/)
  57. * $_SERVER['PATCHWORK_REQUEST']: request part of the url (ex. myagent/mysubagent/...)
  58. * $_SERVER['PATCHWORK_LANG']: lang (ex. en) if application is internationalized
  59. */
  60. if (false === $a = strpos($_SERVER['REQUEST_URI'], '?')) $a = $_SERVER['REQUEST_URI'];
  61. else $a = substr($_SERVER['REQUEST_URI'], 0, $a);
  62. $a = rawurldecode($a);
  63. /**/isset($_SERVER['REDIRECT_STATUS'])
  64. /**/ && false !== strpos(php_sapi_name(), 'apache')
  65. /**/ && '200' !== $_SERVER['REDIRECT_STATUS']
  66. /**/ && die('Patchwork error: Initialization forbidden (try using the shortest possible URL)');
  67. /**/
  68. /**/switch ($a = true)
  69. /**/{
  70. /**/case isset($_SERVER['REDIRECT_PATCHWORK_REQUEST']):
  71. /**/case isset($_SERVER['PATCHWORK_REQUEST']):
  72. /**/case isset($_SERVER['ORIG_PATH_INFO']):
  73. /**/case isset($_SERVER['PATH_INFO']): break;
  74. /**/
  75. /**/default:
  76. /**/ // Check if the webserver supports PATH_INFO
  77. /**/
  78. /**/ $h = patchwork_http_socket($_SERVER['SERVER_ADDR'], $_SERVER['SERVER_PORT'], isset($_SERVER['HTTPS']));
  79. /**/
  80. /**/ $a = strpos($_SERVER['REQUEST_URI'], '?');
  81. /**/ $a = false === $a ? $_SERVER['REQUEST_URI'] : substr($_SERVER['REQUEST_URI'], 0, $a);
  82. /**/ '/' === substr($a, -1) && $a .= basename(isset($_SERVER['ORIG_SCRIPT_NAME']) ? $_SERVER['ORIG_SCRIPT_NAME'] : $_SERVER['SCRIPT_NAME']);
  83. /**/
  84. /**/ $a = "GET {$a}/:?p:=exit HTTP/1.0\r\n";
  85. /**/ $a .= "Host: {$_SERVER['HTTP_HOST']}\r\n";
  86. /**/ $a .= "Connection: close\r\n\r\n";
  87. /**/
  88. /**/ fwrite($h, $a);
  89. /**/ $a = fgets($h, 14);
  90. /**/ fclose($h);
  91. /**/
  92. /**/ $a = (bool) strpos($a, ' 200');
  93. /**/}
  94. /**/
  95. /**/if ($a)
  96. /**/{
  97. switch (true)
  98. {
  99. case isset($_SERVER['REDIRECT_PATCHWORK_REQUEST']): $r = $_SERVER['REDIRECT_PATCHWORK_REQUEST']; break;
  100. case isset($_SERVER['PATCHWORK_REQUEST']) : $r = $_SERVER['PATCHWORK_REQUEST'] ; break;
  101. case isset($_SERVER['ORIG_PATH_INFO']) : $r = $_SERVER['ORIG_PATH_INFO'] ; break;
  102. case isset($_SERVER['PATH_INFO']) : $r = $_SERVER['PATH_INFO'] ; break;
  103. case '/' === substr($a, -1): $a .= basename(isset($_SERVER['ORIG_SCRIPT_NAME']) ? $_SERVER['ORIG_SCRIPT_NAME'] : $_SERVER['SCRIPT_NAME']);
  104. default: $r = '';
  105. }
  106. $a .= '/';
  107. /**/}
  108. /**/else
  109. /**/{
  110. $r = $_SERVER['QUERY_STRING'];
  111. $j = strpos($r, '?');
  112. false !== $j || $j = strpos($r, '&');
  113. if (false !== $j)
  114. {
  115. $r = substr($r, 0, $j);
  116. $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], $j+1);
  117. parse_str($_SERVER['QUERY_STRING'], $_GET);
  118. /**/ if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc())
  119. /**/ {
  120. $k = array(&$_GET);
  121. for ($i = 0, $j = 1; $i < $j; ++$i)
  122. {
  123. foreach ($k[$i] as &$v)
  124. {
  125. if (is_array($v)) $k[$j++] =& $v;
  126. else
  127. {
  128. /**/ if (ini_get_bool('magic_quotes_sybase'))
  129. $v = str_replace("''", "'", $v);
  130. /**/ else
  131. $v = stripslashes($v);
  132. }
  133. }
  134. reset($k[$i]);
  135. unset($k[$i]);
  136. }
  137. unset($k, $v);
  138. /**/ }
  139. }
  140. else if ('' !== $r)
  141. {
  142. $_SERVER['QUERY_STRING'] = '';
  143. reset($_GET);
  144. $j = key($_GET);
  145. unset($_GET[$j]);
  146. }
  147. $j = explode('/', urldecode($r));
  148. $r = array();
  149. $v = 0;
  150. foreach ($j as $j)
  151. {
  152. if ('.' === $j) continue;
  153. if ('..' === $j) $r ? array_pop($r) : ++$v;
  154. else $r[]= $j;
  155. }
  156. $r = implode('/', $r);
  157. if ($v)
  158. {
  159. '/' !== substr($a, -1) && $a .= '/';
  160. $a = preg_replace("'[^/]*/{1,{$v}}$'", '', $a);
  161. '' === $a && $a = '/';
  162. $a = str_replace('%2F', '/', rawurlencode($a . $r));
  163. '' !== $_SERVER['QUERY_STRING'] && $a .= '?' . $_SERVER['QUERY_STRING'];
  164. header('HTTP/1.1 301 Moved Permanently');
  165. header('Location: ' . $a);
  166. exit;
  167. }
  168. /**/}
  169. $r = preg_replace("'/[./]*/'", '/', '/' . $r . '/');
  170. $a = preg_replace("'/[./]*/'", '/', '/' . $a);
  171. /**/if ($a && '\\' === DIRECTORY_SEPARATOR)
  172. /**/{
  173. // Workaround for http://bugs.php.net/44001
  174. if ('/' !== $r && false !== strpos($a, './') && false === strpos($r, './'))
  175. {
  176. $r = explode('/', $r);
  177. $j = count($r) - 1;
  178. $a = explode('/', strrev($a), $j);
  179. for ($i = 0; $i < $j; ++$i) $r[$j - $i] .= str_repeat('.', strspn($a[$i], '.'));
  180. $a = strrev(implode('/', $a));
  181. $r = implode('/', $r);
  182. }
  183. /**/}
  184. $_SERVER['PATCHWORK_REQUEST'] = (string) substr($r, 1, -1);
  185. isset($_SERVER['REDIRECT_PATCHWORK_BASE']) && $_SERVER['PATCHWORK_BASE'] = $_SERVER['REDIRECT_PATCHWORK_BASE'];
  186. isset($_SERVER['REDIRECT_PATCHWORK_LANG']) && $_SERVER['PATCHWORK_LANG'] = $_SERVER['REDIRECT_PATCHWORK_LANG'];
  187. if (isset($_SERVER['PATCHWORK_BASE']))
  188. {
  189. if (0 === strncmp($_SERVER['PATCHWORK_BASE'], '/', 1)) $_SERVER['PATCHWORK_BASE'] = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['PATCHWORK_BASE'];
  190. if (!isset($_SERVER['PATCHWORK_LANG']))
  191. {
  192. $k = explode('__', $_SERVER['PATCHWORK_BASE'], 2);
  193. if (2 === count($k))
  194. {
  195. $k = '#' . preg_quote($k[0], '#') . $b . '#';
  196. preg_match($k, 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $a, $k)
  197. && $_SERVER['PATCHWORK_LANG'] = (string) array_search($k[1], $CONFIG['i18n.lang_list']);
  198. }
  199. else if (PATCHWORK_I18N) switch (substr($_SERVER['PATCHWORK_BASE'], -1))
  200. {
  201. case '/':
  202. case '?': $_SERVER['PATCHWORK_BASE'] .= '__/'; break;
  203. default:
  204. /**/ if ($a)
  205. $_SERVER['PATCHWORK_BASE'] .= '/__/';
  206. /**/ else
  207. $_SERVER['PATCHWORK_BASE'] .= '?__/';
  208. }
  209. }
  210. }
  211. else
  212. {
  213. $a = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $a;
  214. /**/if ($a)
  215. $_SERVER['PATCHWORK_BASE'] = substr($a, 0, -strlen($r)) . '/' . (PATCHWORK_I18N ? '__/' : '');
  216. /**/else
  217. $_SERVER['PATCHWORK_BASE'] = $a . '?' . (PATCHWORK_I18N ? '__/' : '');
  218. }
  219. if (isset($_SERVER['PATCHWORK_LANG']))
  220. {
  221. $a =& $CONFIG['i18n.lang_list'];
  222. $b =& $_SERVER['PATCHWORK_LANG'];
  223. isset($a[$b]) || $b = (string) array_search($b, $a);
  224. unset($a, $b);
  225. }
  226. else if ('__/' === substr($_SERVER['PATCHWORK_BASE'], -3) && preg_match("#^/{$b}/#", $r, $a))
  227. {
  228. $_SERVER['PATCHWORK_LANG'] = array_search($a[1], $CONFIG['i18n.lang_list']);
  229. $_SERVER['PATCHWORK_REQUEST'] = (string) substr($r, strlen($a[1])+2, -1);
  230. }
  231. else $_SERVER['PATCHWORK_LANG'] = '';
  232. reset($CONFIG['i18n.lang_list']);
  233. PATCHWORK_I18N || $_SERVER['PATCHWORK_LANG'] = key($CONFIG['i18n.lang_list']);
  234. $a = 'auto' === $CONFIG['session.cookie_path'];
  235. $b = 'auto' === $CONFIG['session.cookie_domain'];
  236. if ($a || $b)
  237. {
  238. if (preg_match("'^(https?://)([^/:]+)(\.?(?::[^/_]*)?)(/(?:[^?#/]*/)*)'", $_SERVER['PATCHWORK_BASE'], $k))
  239. {
  240. if ($k[0] = strrpos($k[0], '__'))
  241. {
  242. $k[0] -= strlen($k[1]);
  243. $k[1] = strlen($k[2]);
  244. }
  245. else $k[1] = 0;
  246. if ($a)
  247. {
  248. if ($k[0] >= $k[1])
  249. {
  250. $k[4] = substr($k[4], 0, $k[0] - $k[1] - strlen($k[3]));
  251. $a = strrpos($k[4], '/');
  252. $CONFIG['session.cookie_path'] = $a ? substr($k[4], 0, $a) : '/';
  253. }
  254. else $CONFIG['session.cookie_path'] = $k[4];
  255. }
  256. if ($b)
  257. {
  258. if ($k[0] < $k[1])
  259. {
  260. $k[2] = substr($k[2], $k[0]+2);
  261. $a = strpos($k[2], '.');
  262. $CONFIG['session.cookie_domain'] = false !== $a ? substr($k[2], $a) : '';
  263. }
  264. else $CONFIG['session.cookie_domain'] = '';
  265. }
  266. unset($k);
  267. }
  268. else
  269. {
  270. $a
  271. ? ($CONFIG['session.cookie_path'] = '/')
  272. : ($CONFIG['session.cookie_domain'] = '' );
  273. }
  274. }
  275. if (strtr($_SERVER['PATCHWORK_BASE'], '<>&"', '----') !== $_SERVER['PATCHWORK_BASE'])
  276. {
  277. die('Patchwork error: Base URL can not contain special HTML character (' . htmlspecialchars($_SERVER['PATCHWORK_BASE']) . ')');
  278. }
  279. // Shortcut for applications developers
  280. if ($_SERVER['PATCHWORK_LANG'])
  281. {
  282. function T($string, $lang = false)
  283. {
  284. if (!$lang) $lang = Patchwork::__LANG__();
  285. return TRANSLATOR::get($string, $lang, true);
  286. }
  287. }
  288. else
  289. {
  290. function T($string) {return $string;}
  291. }
  292. // Debug trace
  293. function E()
  294. {
  295. $a = func_get_args();
  296. foreach ($a as $a) Patchwork::log('server-dump', $a);
  297. }