bootstrap.php.cache 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. <?php
  2. namespace Symfony\Component\HttpFoundation
  3. {
  4. class ParameterBag implements \IteratorAggregate, \Countable
  5. {
  6. protected $parameters;
  7. public function __construct(array $parameters = array())
  8. {
  9. $this->parameters = $parameters;
  10. }
  11. public function all()
  12. {
  13. return $this->parameters;
  14. }
  15. public function keys()
  16. {
  17. return array_keys($this->parameters);
  18. }
  19. public function replace(array $parameters = array())
  20. {
  21. $this->parameters = $parameters;
  22. }
  23. public function add(array $parameters = array())
  24. {
  25. $this->parameters = array_replace($this->parameters, $parameters);
  26. }
  27. public function get($key, $default = null, $deep = false)
  28. {
  29. if ($deep) {
  30. @trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
  31. }
  32. if (!$deep || false === $pos = strpos($key,'[')) {
  33. return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
  34. }
  35. $root = substr($key, 0, $pos);
  36. if (!array_key_exists($root, $this->parameters)) {
  37. return $default;
  38. }
  39. $value = $this->parameters[$root];
  40. $currentKey = null;
  41. for ($i = $pos, $c = strlen($key); $i < $c; ++$i) {
  42. $char = $key[$i];
  43. if ('['=== $char) {
  44. if (null !== $currentKey) {
  45. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
  46. }
  47. $currentKey ='';
  48. } elseif (']'=== $char) {
  49. if (null === $currentKey) {
  50. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
  51. }
  52. if (!is_array($value) || !array_key_exists($currentKey, $value)) {
  53. return $default;
  54. }
  55. $value = $value[$currentKey];
  56. $currentKey = null;
  57. } else {
  58. if (null === $currentKey) {
  59. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
  60. }
  61. $currentKey .= $char;
  62. }
  63. }
  64. if (null !== $currentKey) {
  65. throw new \InvalidArgumentException('Malformed path. Path must end with "]".');
  66. }
  67. return $value;
  68. }
  69. public function set($key, $value)
  70. {
  71. $this->parameters[$key] = $value;
  72. }
  73. public function has($key)
  74. {
  75. return array_key_exists($key, $this->parameters);
  76. }
  77. public function remove($key)
  78. {
  79. unset($this->parameters[$key]);
  80. }
  81. public function getAlpha($key, $default ='', $deep = false)
  82. {
  83. return preg_replace('/[^[:alpha:]]/','', $this->get($key, $default, $deep));
  84. }
  85. public function getAlnum($key, $default ='', $deep = false)
  86. {
  87. return preg_replace('/[^[:alnum:]]/','', $this->get($key, $default, $deep));
  88. }
  89. public function getDigits($key, $default ='', $deep = false)
  90. {
  91. return str_replace(array('-','+'),'', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT, array(), $deep));
  92. }
  93. public function getInt($key, $default = 0, $deep = false)
  94. {
  95. return (int) $this->get($key, $default, $deep);
  96. }
  97. public function getBoolean($key, $default = false, $deep = false)
  98. {
  99. return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN, array(), $deep);
  100. }
  101. public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array(), $deep = false)
  102. {
  103. static $filters = null;
  104. if (null === $filters) {
  105. foreach (filter_list() as $tmp) {
  106. $filters[filter_id($tmp)] = 1;
  107. }
  108. }
  109. if (is_bool($filter) || !isset($filters[$filter]) || is_array($deep)) {
  110. @trigger_error('Passing the $deep boolean as 3rd argument to the '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Remove it altogether as the $deep argument will be removed in 3.0.', E_USER_DEPRECATED);
  111. $tmp = $deep;
  112. $deep = $filter;
  113. $filter = $options;
  114. $options = $tmp;
  115. }
  116. $value = $this->get($key, $default, $deep);
  117. if (!is_array($options) && $options) {
  118. $options = array('flags'=> $options);
  119. }
  120. if (is_array($value) && !isset($options['flags'])) {
  121. $options['flags'] = FILTER_REQUIRE_ARRAY;
  122. }
  123. return filter_var($value, $filter, $options);
  124. }
  125. public function getIterator()
  126. {
  127. return new \ArrayIterator($this->parameters);
  128. }
  129. public function count()
  130. {
  131. return count($this->parameters);
  132. }
  133. }
  134. }
  135. namespace Symfony\Component\HttpFoundation
  136. {
  137. class HeaderBag implements \IteratorAggregate, \Countable
  138. {
  139. protected $headers = array();
  140. protected $cacheControl = array();
  141. public function __construct(array $headers = array())
  142. {
  143. foreach ($headers as $key => $values) {
  144. $this->set($key, $values);
  145. }
  146. }
  147. public function __toString()
  148. {
  149. if (!$this->headers) {
  150. return'';
  151. }
  152. $max = max(array_map('strlen', array_keys($this->headers))) + 1;
  153. $content ='';
  154. ksort($this->headers);
  155. foreach ($this->headers as $name => $values) {
  156. $name = implode('-', array_map('ucfirst', explode('-', $name)));
  157. foreach ($values as $value) {
  158. $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
  159. }
  160. }
  161. return $content;
  162. }
  163. public function all()
  164. {
  165. return $this->headers;
  166. }
  167. public function keys()
  168. {
  169. return array_keys($this->headers);
  170. }
  171. public function replace(array $headers = array())
  172. {
  173. $this->headers = array();
  174. $this->add($headers);
  175. }
  176. public function add(array $headers)
  177. {
  178. foreach ($headers as $key => $values) {
  179. $this->set($key, $values);
  180. }
  181. }
  182. public function get($key, $default = null, $first = true)
  183. {
  184. $key = str_replace('_','-', strtolower($key));
  185. if (!array_key_exists($key, $this->headers)) {
  186. if (null === $default) {
  187. return $first ? null : array();
  188. }
  189. return $first ? $default : array($default);
  190. }
  191. if ($first) {
  192. return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
  193. }
  194. return $this->headers[$key];
  195. }
  196. public function set($key, $values, $replace = true)
  197. {
  198. $key = str_replace('_','-', strtolower($key));
  199. $values = array_values((array) $values);
  200. if (true === $replace || !isset($this->headers[$key])) {
  201. $this->headers[$key] = $values;
  202. } else {
  203. $this->headers[$key] = array_merge($this->headers[$key], $values);
  204. }
  205. if ('cache-control'=== $key) {
  206. $this->cacheControl = $this->parseCacheControl($values[0]);
  207. }
  208. }
  209. public function has($key)
  210. {
  211. return array_key_exists(str_replace('_','-', strtolower($key)), $this->headers);
  212. }
  213. public function contains($key, $value)
  214. {
  215. return in_array($value, $this->get($key, null, false));
  216. }
  217. public function remove($key)
  218. {
  219. $key = str_replace('_','-', strtolower($key));
  220. unset($this->headers[$key]);
  221. if ('cache-control'=== $key) {
  222. $this->cacheControl = array();
  223. }
  224. }
  225. public function getDate($key, \DateTime $default = null)
  226. {
  227. if (null === $value = $this->get($key)) {
  228. return $default;
  229. }
  230. if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
  231. throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
  232. }
  233. return $date;
  234. }
  235. public function addCacheControlDirective($key, $value = true)
  236. {
  237. $this->cacheControl[$key] = $value;
  238. $this->set('Cache-Control', $this->getCacheControlHeader());
  239. }
  240. public function hasCacheControlDirective($key)
  241. {
  242. return array_key_exists($key, $this->cacheControl);
  243. }
  244. public function getCacheControlDirective($key)
  245. {
  246. return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
  247. }
  248. public function removeCacheControlDirective($key)
  249. {
  250. unset($this->cacheControl[$key]);
  251. $this->set('Cache-Control', $this->getCacheControlHeader());
  252. }
  253. public function getIterator()
  254. {
  255. return new \ArrayIterator($this->headers);
  256. }
  257. public function count()
  258. {
  259. return count($this->headers);
  260. }
  261. protected function getCacheControlHeader()
  262. {
  263. $parts = array();
  264. ksort($this->cacheControl);
  265. foreach ($this->cacheControl as $key => $value) {
  266. if (true === $value) {
  267. $parts[] = $key;
  268. } else {
  269. if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
  270. $value ='"'.$value.'"';
  271. }
  272. $parts[] = "$key=$value";
  273. }
  274. }
  275. return implode(', ', $parts);
  276. }
  277. protected function parseCacheControl($header)
  278. {
  279. $cacheControl = array();
  280. preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
  281. foreach ($matches as $match) {
  282. $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
  283. }
  284. return $cacheControl;
  285. }
  286. }
  287. }
  288. namespace Symfony\Component\HttpFoundation
  289. {
  290. use Symfony\Component\HttpFoundation\File\UploadedFile;
  291. class FileBag extends ParameterBag
  292. {
  293. private static $fileKeys = array('error','name','size','tmp_name','type');
  294. public function __construct(array $parameters = array())
  295. {
  296. $this->replace($parameters);
  297. }
  298. public function replace(array $files = array())
  299. {
  300. $this->parameters = array();
  301. $this->add($files);
  302. }
  303. public function set($key, $value)
  304. {
  305. if (!is_array($value) && !$value instanceof UploadedFile) {
  306. throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
  307. }
  308. parent::set($key, $this->convertFileInformation($value));
  309. }
  310. public function add(array $files = array())
  311. {
  312. foreach ($files as $key => $file) {
  313. $this->set($key, $file);
  314. }
  315. }
  316. protected function convertFileInformation($file)
  317. {
  318. if ($file instanceof UploadedFile) {
  319. return $file;
  320. }
  321. $file = $this->fixPhpFilesArray($file);
  322. if (is_array($file)) {
  323. $keys = array_keys($file);
  324. sort($keys);
  325. if ($keys == self::$fileKeys) {
  326. if (UPLOAD_ERR_NO_FILE == $file['error']) {
  327. $file = null;
  328. } else {
  329. $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
  330. }
  331. } else {
  332. $file = array_map(array($this,'convertFileInformation'), $file);
  333. }
  334. }
  335. return $file;
  336. }
  337. protected function fixPhpFilesArray($data)
  338. {
  339. if (!is_array($data)) {
  340. return $data;
  341. }
  342. $keys = array_keys($data);
  343. sort($keys);
  344. if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
  345. return $data;
  346. }
  347. $files = $data;
  348. foreach (self::$fileKeys as $k) {
  349. unset($files[$k]);
  350. }
  351. foreach ($data['name'] as $key => $name) {
  352. $files[$key] = $this->fixPhpFilesArray(array('error'=> $data['error'][$key],'name'=> $name,'type'=> $data['type'][$key],'tmp_name'=> $data['tmp_name'][$key],'size'=> $data['size'][$key],
  353. ));
  354. }
  355. return $files;
  356. }
  357. }
  358. }
  359. namespace Symfony\Component\HttpFoundation
  360. {
  361. class ServerBag extends ParameterBag
  362. {
  363. public function getHeaders()
  364. {
  365. $headers = array();
  366. $contentHeaders = array('CONTENT_LENGTH'=> true,'CONTENT_MD5'=> true,'CONTENT_TYPE'=> true);
  367. foreach ($this->parameters as $key => $value) {
  368. if (0 === strpos($key,'HTTP_')) {
  369. $headers[substr($key, 5)] = $value;
  370. }
  371. elseif (isset($contentHeaders[$key])) {
  372. $headers[$key] = $value;
  373. }
  374. }
  375. if (isset($this->parameters['PHP_AUTH_USER'])) {
  376. $headers['PHP_AUTH_USER'] = $this->parameters['PHP_AUTH_USER'];
  377. $headers['PHP_AUTH_PW'] = isset($this->parameters['PHP_AUTH_PW']) ? $this->parameters['PHP_AUTH_PW'] :'';
  378. } else {
  379. $authorizationHeader = null;
  380. if (isset($this->parameters['HTTP_AUTHORIZATION'])) {
  381. $authorizationHeader = $this->parameters['HTTP_AUTHORIZATION'];
  382. } elseif (isset($this->parameters['REDIRECT_HTTP_AUTHORIZATION'])) {
  383. $authorizationHeader = $this->parameters['REDIRECT_HTTP_AUTHORIZATION'];
  384. }
  385. if (null !== $authorizationHeader) {
  386. if (0 === stripos($authorizationHeader,'basic ')) {
  387. $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2);
  388. if (count($exploded) == 2) {
  389. list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
  390. }
  391. } elseif (empty($this->parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader,'digest '))) {
  392. $headers['PHP_AUTH_DIGEST'] = $authorizationHeader;
  393. $this->parameters['PHP_AUTH_DIGEST'] = $authorizationHeader;
  394. } elseif (0 === stripos($authorizationHeader,'bearer ')) {
  395. $headers['AUTHORIZATION'] = $authorizationHeader;
  396. }
  397. }
  398. }
  399. if (isset($headers['AUTHORIZATION'])) {
  400. return $headers;
  401. }
  402. if (isset($headers['PHP_AUTH_USER'])) {
  403. $headers['AUTHORIZATION'] ='Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
  404. } elseif (isset($headers['PHP_AUTH_DIGEST'])) {
  405. $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST'];
  406. }
  407. return $headers;
  408. }
  409. }
  410. }
  411. namespace Symfony\Component\HttpFoundation
  412. {
  413. use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
  414. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  415. class Request
  416. {
  417. const HEADER_FORWARDED ='forwarded';
  418. const HEADER_CLIENT_IP ='client_ip';
  419. const HEADER_CLIENT_HOST ='client_host';
  420. const HEADER_CLIENT_PROTO ='client_proto';
  421. const HEADER_CLIENT_PORT ='client_port';
  422. const METHOD_HEAD ='HEAD';
  423. const METHOD_GET ='GET';
  424. const METHOD_POST ='POST';
  425. const METHOD_PUT ='PUT';
  426. const METHOD_PATCH ='PATCH';
  427. const METHOD_DELETE ='DELETE';
  428. const METHOD_PURGE ='PURGE';
  429. const METHOD_OPTIONS ='OPTIONS';
  430. const METHOD_TRACE ='TRACE';
  431. const METHOD_CONNECT ='CONNECT';
  432. protected static $trustedProxies = array();
  433. protected static $trustedHostPatterns = array();
  434. protected static $trustedHosts = array();
  435. protected static $trustedHeaders = array(
  436. self::HEADER_FORWARDED =>'FORWARDED',
  437. self::HEADER_CLIENT_IP =>'X_FORWARDED_FOR',
  438. self::HEADER_CLIENT_HOST =>'X_FORWARDED_HOST',
  439. self::HEADER_CLIENT_PROTO =>'X_FORWARDED_PROTO',
  440. self::HEADER_CLIENT_PORT =>'X_FORWARDED_PORT',
  441. );
  442. protected static $httpMethodParameterOverride = false;
  443. public $attributes;
  444. public $request;
  445. public $query;
  446. public $server;
  447. public $files;
  448. public $cookies;
  449. public $headers;
  450. protected $content;
  451. protected $languages;
  452. protected $charsets;
  453. protected $encodings;
  454. protected $acceptableContentTypes;
  455. protected $pathInfo;
  456. protected $requestUri;
  457. protected $baseUrl;
  458. protected $basePath;
  459. protected $method;
  460. protected $format;
  461. protected $session;
  462. protected $locale;
  463. protected $defaultLocale ='en';
  464. protected static $formats;
  465. protected static $requestFactory;
  466. private $isForwardedValid = true;
  467. private static $forwardedParams = array(
  468. self::HEADER_CLIENT_IP =>'for',
  469. self::HEADER_CLIENT_HOST =>'host',
  470. self::HEADER_CLIENT_PROTO =>'proto',
  471. self::HEADER_CLIENT_PORT =>'host',
  472. );
  473. public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  474. {
  475. $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
  476. }
  477. public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  478. {
  479. $this->request = new ParameterBag($request);
  480. $this->query = new ParameterBag($query);
  481. $this->attributes = new ParameterBag($attributes);
  482. $this->cookies = new ParameterBag($cookies);
  483. $this->files = new FileBag($files);
  484. $this->server = new ServerBag($server);
  485. $this->headers = new HeaderBag($this->server->getHeaders());
  486. $this->content = $content;
  487. $this->languages = null;
  488. $this->charsets = null;
  489. $this->encodings = null;
  490. $this->acceptableContentTypes = null;
  491. $this->pathInfo = null;
  492. $this->requestUri = null;
  493. $this->baseUrl = null;
  494. $this->basePath = null;
  495. $this->method = null;
  496. $this->format = null;
  497. }
  498. public static function createFromGlobals()
  499. {
  500. $server = $_SERVER;
  501. if ('cli-server'=== PHP_SAPI) {
  502. if (array_key_exists('HTTP_CONTENT_LENGTH', $_SERVER)) {
  503. $server['CONTENT_LENGTH'] = $_SERVER['HTTP_CONTENT_LENGTH'];
  504. }
  505. if (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER)) {
  506. $server['CONTENT_TYPE'] = $_SERVER['HTTP_CONTENT_TYPE'];
  507. }
  508. }
  509. $request = self::createRequestFromFactory($_GET, $_POST, array(), $_COOKIE, $_FILES, $server);
  510. if (0 === strpos($request->headers->get('CONTENT_TYPE'),'application/x-www-form-urlencoded')
  511. && in_array(strtoupper($request->server->get('REQUEST_METHOD','GET')), array('PUT','DELETE','PATCH'))
  512. ) {
  513. parse_str($request->getContent(), $data);
  514. $request->request = new ParameterBag($data);
  515. }
  516. return $request;
  517. }
  518. public static function create($uri, $method ='GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
  519. {
  520. $server = array_replace(array('SERVER_NAME'=>'localhost','SERVER_PORT'=> 80,'HTTP_HOST'=>'localhost','HTTP_USER_AGENT'=>'Symfony/2.X','HTTP_ACCEPT'=>'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','HTTP_ACCEPT_LANGUAGE'=>'en-us,en;q=0.5','HTTP_ACCEPT_CHARSET'=>'ISO-8859-1,utf-8;q=0.7,*;q=0.7','REMOTE_ADDR'=>'127.0.0.1','SCRIPT_NAME'=>'','SCRIPT_FILENAME'=>'','SERVER_PROTOCOL'=>'HTTP/1.1','REQUEST_TIME'=> time(),
  521. ), $server);
  522. $server['PATH_INFO'] ='';
  523. $server['REQUEST_METHOD'] = strtoupper($method);
  524. $components = parse_url($uri);
  525. if (isset($components['host'])) {
  526. $server['SERVER_NAME'] = $components['host'];
  527. $server['HTTP_HOST'] = $components['host'];
  528. }
  529. if (isset($components['scheme'])) {
  530. if ('https'=== $components['scheme']) {
  531. $server['HTTPS'] ='on';
  532. $server['SERVER_PORT'] = 443;
  533. } else {
  534. unset($server['HTTPS']);
  535. $server['SERVER_PORT'] = 80;
  536. }
  537. }
  538. if (isset($components['port'])) {
  539. $server['SERVER_PORT'] = $components['port'];
  540. $server['HTTP_HOST'] = $server['HTTP_HOST'].':'.$components['port'];
  541. }
  542. if (isset($components['user'])) {
  543. $server['PHP_AUTH_USER'] = $components['user'];
  544. }
  545. if (isset($components['pass'])) {
  546. $server['PHP_AUTH_PW'] = $components['pass'];
  547. }
  548. if (!isset($components['path'])) {
  549. $components['path'] ='/';
  550. }
  551. switch (strtoupper($method)) {
  552. case'POST':
  553. case'PUT':
  554. case'DELETE':
  555. if (!isset($server['CONTENT_TYPE'])) {
  556. $server['CONTENT_TYPE'] ='application/x-www-form-urlencoded';
  557. }
  558. case'PATCH':
  559. $request = $parameters;
  560. $query = array();
  561. break;
  562. default:
  563. $request = array();
  564. $query = $parameters;
  565. break;
  566. }
  567. $queryString ='';
  568. if (isset($components['query'])) {
  569. parse_str(html_entity_decode($components['query']), $qs);
  570. if ($query) {
  571. $query = array_replace($qs, $query);
  572. $queryString = http_build_query($query,'','&');
  573. } else {
  574. $query = $qs;
  575. $queryString = $components['query'];
  576. }
  577. } elseif ($query) {
  578. $queryString = http_build_query($query,'','&');
  579. }
  580. $server['REQUEST_URI'] = $components['path'].(''!== $queryString ?'?'.$queryString :'');
  581. $server['QUERY_STRING'] = $queryString;
  582. return self::createRequestFromFactory($query, $request, array(), $cookies, $files, $server, $content);
  583. }
  584. public static function setFactory($callable)
  585. {
  586. self::$requestFactory = $callable;
  587. }
  588. public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
  589. {
  590. $dup = clone $this;
  591. if ($query !== null) {
  592. $dup->query = new ParameterBag($query);
  593. }
  594. if ($request !== null) {
  595. $dup->request = new ParameterBag($request);
  596. }
  597. if ($attributes !== null) {
  598. $dup->attributes = new ParameterBag($attributes);
  599. }
  600. if ($cookies !== null) {
  601. $dup->cookies = new ParameterBag($cookies);
  602. }
  603. if ($files !== null) {
  604. $dup->files = new FileBag($files);
  605. }
  606. if ($server !== null) {
  607. $dup->server = new ServerBag($server);
  608. $dup->headers = new HeaderBag($dup->server->getHeaders());
  609. }
  610. $dup->languages = null;
  611. $dup->charsets = null;
  612. $dup->encodings = null;
  613. $dup->acceptableContentTypes = null;
  614. $dup->pathInfo = null;
  615. $dup->requestUri = null;
  616. $dup->baseUrl = null;
  617. $dup->basePath = null;
  618. $dup->method = null;
  619. $dup->format = null;
  620. if (!$dup->get('_format') && $this->get('_format')) {
  621. $dup->attributes->set('_format', $this->get('_format'));
  622. }
  623. if (!$dup->getRequestFormat(null)) {
  624. $dup->setRequestFormat($this->getRequestFormat(null));
  625. }
  626. return $dup;
  627. }
  628. public function __clone()
  629. {
  630. $this->query = clone $this->query;
  631. $this->request = clone $this->request;
  632. $this->attributes = clone $this->attributes;
  633. $this->cookies = clone $this->cookies;
  634. $this->files = clone $this->files;
  635. $this->server = clone $this->server;
  636. $this->headers = clone $this->headers;
  637. }
  638. public function __toString()
  639. {
  640. try {
  641. $content = $this->getContent();
  642. } catch (\LogicException $e) {
  643. return trigger_error($e, E_USER_ERROR);
  644. }
  645. return
  646. sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
  647. $this->headers."\r\n".
  648. $content;
  649. }
  650. public function overrideGlobals()
  651. {
  652. $this->server->set('QUERY_STRING', static::normalizeQueryString(http_build_query($this->query->all(), null,'&')));
  653. $_GET = $this->query->all();
  654. $_POST = $this->request->all();
  655. $_SERVER = $this->server->all();
  656. $_COOKIE = $this->cookies->all();
  657. foreach ($this->headers->all() as $key => $value) {
  658. $key = strtoupper(str_replace('-','_', $key));
  659. if (in_array($key, array('CONTENT_TYPE','CONTENT_LENGTH'))) {
  660. $_SERVER[$key] = implode(', ', $value);
  661. } else {
  662. $_SERVER['HTTP_'.$key] = implode(', ', $value);
  663. }
  664. }
  665. $request = array('g'=> $_GET,'p'=> $_POST,'c'=> $_COOKIE);
  666. $requestOrder = ini_get('request_order') ?: ini_get('variables_order');
  667. $requestOrder = preg_replace('#[^cgp]#','', strtolower($requestOrder)) ?:'gp';
  668. $_REQUEST = array();
  669. foreach (str_split($requestOrder) as $order) {
  670. $_REQUEST = array_merge($_REQUEST, $request[$order]);
  671. }
  672. }
  673. public static function setTrustedProxies(array $proxies)
  674. {
  675. self::$trustedProxies = $proxies;
  676. }
  677. public static function getTrustedProxies()
  678. {
  679. return self::$trustedProxies;
  680. }
  681. public static function setTrustedHosts(array $hostPatterns)
  682. {
  683. self::$trustedHostPatterns = array_map(function ($hostPattern) {
  684. return sprintf('#%s#i', $hostPattern);
  685. }, $hostPatterns);
  686. self::$trustedHosts = array();
  687. }
  688. public static function getTrustedHosts()
  689. {
  690. return self::$trustedHostPatterns;
  691. }
  692. public static function setTrustedHeaderName($key, $value)
  693. {
  694. if (!array_key_exists($key, self::$trustedHeaders)) {
  695. throw new \InvalidArgumentException(sprintf('Unable to set the trusted header name for key "%s".', $key));
  696. }
  697. self::$trustedHeaders[$key] = $value;
  698. }
  699. public static function getTrustedHeaderName($key)
  700. {
  701. if (!array_key_exists($key, self::$trustedHeaders)) {
  702. throw new \InvalidArgumentException(sprintf('Unable to get the trusted header name for key "%s".', $key));
  703. }
  704. return self::$trustedHeaders[$key];
  705. }
  706. public static function normalizeQueryString($qs)
  707. {
  708. if (''== $qs) {
  709. return'';
  710. }
  711. $parts = array();
  712. $order = array();
  713. foreach (explode('&', $qs) as $param) {
  714. if (''=== $param ||'='=== $param[0]) {
  715. continue;
  716. }
  717. $keyValuePair = explode('=', $param, 2);
  718. $parts[] = isset($keyValuePair[1]) ?
  719. rawurlencode(urldecode($keyValuePair[0])).'='.rawurlencode(urldecode($keyValuePair[1])) :
  720. rawurlencode(urldecode($keyValuePair[0]));
  721. $order[] = urldecode($keyValuePair[0]);
  722. }
  723. array_multisort($order, SORT_ASC, $parts);
  724. return implode('&', $parts);
  725. }
  726. public static function enableHttpMethodParameterOverride()
  727. {
  728. self::$httpMethodParameterOverride = true;
  729. }
  730. public static function getHttpMethodParameterOverride()
  731. {
  732. return self::$httpMethodParameterOverride;
  733. }
  734. public function get($key, $default = null, $deep = false)
  735. {
  736. if ($deep) {
  737. @trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
  738. }
  739. if ($this !== $result = $this->query->get($key, $this, $deep)) {
  740. return $result;
  741. }
  742. if ($this !== $result = $this->attributes->get($key, $this, $deep)) {
  743. return $result;
  744. }
  745. if ($this !== $result = $this->request->get($key, $this, $deep)) {
  746. return $result;
  747. }
  748. return $default;
  749. }
  750. public function getSession()
  751. {
  752. return $this->session;
  753. }
  754. public function hasPreviousSession()
  755. {
  756. return $this->hasSession() && $this->cookies->has($this->session->getName());
  757. }
  758. public function hasSession()
  759. {
  760. return null !== $this->session;
  761. }
  762. public function setSession(SessionInterface $session)
  763. {
  764. $this->session = $session;
  765. }
  766. public function getClientIps()
  767. {
  768. $ip = $this->server->get('REMOTE_ADDR');
  769. if (!$this->isFromTrustedProxy()) {
  770. return array($ip);
  771. }
  772. return $this->getTrustedValues(self::HEADER_CLIENT_IP, $ip) ?: array($ip);
  773. }
  774. public function getClientIp()
  775. {
  776. $ipAddresses = $this->getClientIps();
  777. return $ipAddresses[0];
  778. }
  779. public function getScriptName()
  780. {
  781. return $this->server->get('SCRIPT_NAME', $this->server->get('ORIG_SCRIPT_NAME',''));
  782. }
  783. public function getPathInfo()
  784. {
  785. if (null === $this->pathInfo) {
  786. $this->pathInfo = $this->preparePathInfo();
  787. }
  788. return $this->pathInfo;
  789. }
  790. public function getBasePath()
  791. {
  792. if (null === $this->basePath) {
  793. $this->basePath = $this->prepareBasePath();
  794. }
  795. return $this->basePath;
  796. }
  797. public function getBaseUrl()
  798. {
  799. if (null === $this->baseUrl) {
  800. $this->baseUrl = $this->prepareBaseUrl();
  801. }
  802. return $this->baseUrl;
  803. }
  804. public function getScheme()
  805. {
  806. return $this->isSecure() ?'https':'http';
  807. }
  808. public function getPort()
  809. {
  810. if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_CLIENT_PORT)) {
  811. $host = $host[0];
  812. } elseif ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_CLIENT_HOST)) {
  813. $host = $host[0];
  814. } elseif (!$host = $this->headers->get('HOST')) {
  815. return $this->server->get('SERVER_PORT');
  816. }
  817. if ($host[0] ==='[') {
  818. $pos = strpos($host,':', strrpos($host,']'));
  819. } else {
  820. $pos = strrpos($host,':');
  821. }
  822. if (false !== $pos) {
  823. return (int) substr($host, $pos + 1);
  824. }
  825. return'https'=== $this->getScheme() ? 443 : 80;
  826. }
  827. public function getUser()
  828. {
  829. return $this->headers->get('PHP_AUTH_USER');
  830. }
  831. public function getPassword()
  832. {
  833. return $this->headers->get('PHP_AUTH_PW');
  834. }
  835. public function getUserInfo()
  836. {
  837. $userinfo = $this->getUser();
  838. $pass = $this->getPassword();
  839. if (''!= $pass) {
  840. $userinfo .= ":$pass";
  841. }
  842. return $userinfo;
  843. }
  844. public function getHttpHost()
  845. {
  846. $scheme = $this->getScheme();
  847. $port = $this->getPort();
  848. if (('http'== $scheme && $port == 80) || ('https'== $scheme && $port == 443)) {
  849. return $this->getHost();
  850. }
  851. return $this->getHost().':'.$port;
  852. }
  853. public function getRequestUri()
  854. {
  855. if (null === $this->requestUri) {
  856. $this->requestUri = $this->prepareRequestUri();
  857. }
  858. return $this->requestUri;
  859. }
  860. public function getSchemeAndHttpHost()
  861. {
  862. return $this->getScheme().'://'.$this->getHttpHost();
  863. }
  864. public function getUri()
  865. {
  866. if (null !== $qs = $this->getQueryString()) {
  867. $qs ='?'.$qs;
  868. }
  869. return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
  870. }
  871. public function getUriForPath($path)
  872. {
  873. return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
  874. }
  875. public function getRelativeUriForPath($path)
  876. {
  877. if (!isset($path[0]) ||'/'!== $path[0]) {
  878. return $path;
  879. }
  880. if ($path === $basePath = $this->getPathInfo()) {
  881. return'';
  882. }
  883. $sourceDirs = explode('/', isset($basePath[0]) &&'/'=== $basePath[0] ? substr($basePath, 1) : $basePath);
  884. $targetDirs = explode('/', isset($path[0]) &&'/'=== $path[0] ? substr($path, 1) : $path);
  885. array_pop($sourceDirs);
  886. $targetFile = array_pop($targetDirs);
  887. foreach ($sourceDirs as $i => $dir) {
  888. if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
  889. unset($sourceDirs[$i], $targetDirs[$i]);
  890. } else {
  891. break;
  892. }
  893. }
  894. $targetDirs[] = $targetFile;
  895. $path = str_repeat('../', count($sourceDirs)).implode('/', $targetDirs);
  896. return !isset($path[0]) ||'/'=== $path[0]
  897. || false !== ($colonPos = strpos($path,':')) && ($colonPos < ($slashPos = strpos($path,'/')) || false === $slashPos)
  898. ? "./$path" : $path;
  899. }
  900. public function getQueryString()
  901. {
  902. $qs = static::normalizeQueryString($this->server->get('QUERY_STRING'));
  903. return''=== $qs ? null : $qs;
  904. }
  905. public function isSecure()
  906. {
  907. if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_CLIENT_PROTO)) {
  908. return in_array(strtolower($proto[0]), array('https','on','ssl','1'), true);
  909. }
  910. $https = $this->server->get('HTTPS');
  911. return !empty($https) &&'off'!== strtolower($https);
  912. }
  913. public function getHost()
  914. {
  915. if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_CLIENT_HOST)) {
  916. $host = $host[0];
  917. } elseif (!$host = $this->headers->get('HOST')) {
  918. if (!$host = $this->server->get('SERVER_NAME')) {
  919. $host = $this->server->get('SERVER_ADDR','');
  920. }
  921. }
  922. $host = strtolower(preg_replace('/:\d+$/','', trim($host)));
  923. if ($host &&''!== preg_replace('/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/','', $host)) {
  924. throw new \UnexpectedValueException(sprintf('Invalid Host "%s"', $host));
  925. }
  926. if (count(self::$trustedHostPatterns) > 0) {
  927. if (in_array($host, self::$trustedHosts)) {
  928. return $host;
  929. }
  930. foreach (self::$trustedHostPatterns as $pattern) {
  931. if (preg_match($pattern, $host)) {
  932. self::$trustedHosts[] = $host;
  933. return $host;
  934. }
  935. }
  936. throw new \UnexpectedValueException(sprintf('Untrusted Host "%s"', $host));
  937. }
  938. return $host;
  939. }
  940. public function setMethod($method)
  941. {
  942. $this->method = null;
  943. $this->server->set('REQUEST_METHOD', $method);
  944. }
  945. public function getMethod()
  946. {
  947. if (null === $this->method) {
  948. $this->method = strtoupper($this->server->get('REQUEST_METHOD','GET'));
  949. if ('POST'=== $this->method) {
  950. if ($method = $this->headers->get('X-HTTP-METHOD-OVERRIDE')) {
  951. $this->method = strtoupper($method);
  952. } elseif (self::$httpMethodParameterOverride) {
  953. $this->method = strtoupper($this->request->get('_method', $this->query->get('_method','POST')));
  954. }
  955. }
  956. }
  957. return $this->method;
  958. }
  959. public function getRealMethod()
  960. {
  961. return strtoupper($this->server->get('REQUEST_METHOD','GET'));
  962. }
  963. public function getMimeType($format)
  964. {
  965. if (null === static::$formats) {
  966. static::initializeFormats();
  967. }
  968. return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
  969. }
  970. public function getFormat($mimeType)
  971. {
  972. $canonicalMimeType = null;
  973. if (false !== $pos = strpos($mimeType,';')) {
  974. $canonicalMimeType = substr($mimeType, 0, $pos);
  975. }
  976. if (null === static::$formats) {
  977. static::initializeFormats();
  978. }
  979. foreach (static::$formats as $format => $mimeTypes) {
  980. if (in_array($mimeType, (array) $mimeTypes)) {
  981. return $format;
  982. }
  983. if (null !== $canonicalMimeType && in_array($canonicalMimeType, (array) $mimeTypes)) {
  984. return $format;
  985. }
  986. }
  987. }
  988. public function setFormat($format, $mimeTypes)
  989. {
  990. if (null === static::$formats) {
  991. static::initializeFormats();
  992. }
  993. static::$formats[$format] = is_array($mimeTypes) ? $mimeTypes : array($mimeTypes);
  994. }
  995. public function getRequestFormat($default ='html')
  996. {
  997. if (null === $this->format) {
  998. $this->format = $this->get('_format');
  999. }
  1000. return null === $this->format ? $default : $this->format;
  1001. }
  1002. public function setRequestFormat($format)
  1003. {
  1004. $this->format = $format;
  1005. }
  1006. public function getContentType()
  1007. {
  1008. return $this->getFormat($this->headers->get('CONTENT_TYPE'));
  1009. }
  1010. public function setDefaultLocale($locale)
  1011. {
  1012. $this->defaultLocale = $locale;
  1013. if (null === $this->locale) {
  1014. $this->setPhpDefaultLocale($locale);
  1015. }
  1016. }
  1017. public function getDefaultLocale()
  1018. {
  1019. return $this->defaultLocale;
  1020. }
  1021. public function setLocale($locale)
  1022. {
  1023. $this->setPhpDefaultLocale($this->locale = $locale);
  1024. }
  1025. public function getLocale()
  1026. {
  1027. return null === $this->locale ? $this->defaultLocale : $this->locale;
  1028. }
  1029. public function isMethod($method)
  1030. {
  1031. return $this->getMethod() === strtoupper($method);
  1032. }
  1033. public function isMethodSafe()
  1034. {
  1035. return in_array($this->getMethod(), 0 < func_num_args() && !func_get_arg(0) ? array('GET','HEAD','OPTIONS','TRACE') : array('GET','HEAD'));
  1036. }
  1037. public function isMethodCacheable()
  1038. {
  1039. return in_array($this->getMethod(), array('GET','HEAD'));
  1040. }
  1041. public function getContent($asResource = false)
  1042. {
  1043. $currentContentIsResource = is_resource($this->content);
  1044. if (PHP_VERSION_ID < 50600 && false === $this->content) {
  1045. throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
  1046. }
  1047. if (true === $asResource) {
  1048. if ($currentContentIsResource) {
  1049. rewind($this->content);
  1050. return $this->content;
  1051. }
  1052. if (is_string($this->content)) {
  1053. $resource = fopen('php://temp','r+');
  1054. fwrite($resource, $this->content);
  1055. rewind($resource);
  1056. return $resource;
  1057. }
  1058. $this->content = false;
  1059. return fopen('php://input','rb');
  1060. }
  1061. if ($currentContentIsResource) {
  1062. rewind($this->content);
  1063. return stream_get_contents($this->content);
  1064. }
  1065. if (null === $this->content || false === $this->content) {
  1066. $this->content = file_get_contents('php://input');
  1067. }
  1068. return $this->content;
  1069. }
  1070. public function getETags()
  1071. {
  1072. return preg_split('/\s*,\s*/', $this->headers->get('if_none_match'), null, PREG_SPLIT_NO_EMPTY);
  1073. }
  1074. public function isNoCache()
  1075. {
  1076. return $this->headers->hasCacheControlDirective('no-cache') ||'no-cache'== $this->headers->get('Pragma');
  1077. }
  1078. public function getPreferredLanguage(array $locales = null)
  1079. {
  1080. $preferredLanguages = $this->getLanguages();
  1081. if (empty($locales)) {
  1082. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null;
  1083. }
  1084. if (!$preferredLanguages) {
  1085. return $locales[0];
  1086. }
  1087. $extendedPreferredLanguages = array();
  1088. foreach ($preferredLanguages as $language) {
  1089. $extendedPreferredLanguages[] = $language;
  1090. if (false !== $position = strpos($language,'_')) {
  1091. $superLanguage = substr($language, 0, $position);
  1092. if (!in_array($superLanguage, $preferredLanguages)) {
  1093. $extendedPreferredLanguages[] = $superLanguage;
  1094. }
  1095. }
  1096. }
  1097. $preferredLanguages = array_values(array_intersect($extendedPreferredLanguages, $locales));
  1098. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : $locales[0];
  1099. }
  1100. public function getLanguages()
  1101. {
  1102. if (null !== $this->languages) {
  1103. return $this->languages;
  1104. }
  1105. $languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
  1106. $this->languages = array();
  1107. foreach ($languages as $lang => $acceptHeaderItem) {
  1108. if (false !== strpos($lang,'-')) {
  1109. $codes = explode('-', $lang);
  1110. if ('i'=== $codes[0]) {
  1111. if (count($codes) > 1) {
  1112. $lang = $codes[1];
  1113. }
  1114. } else {
  1115. for ($i = 0, $max = count($codes); $i < $max; ++$i) {
  1116. if ($i === 0) {
  1117. $lang = strtolower($codes[0]);
  1118. } else {
  1119. $lang .='_'.strtoupper($codes[$i]);
  1120. }
  1121. }
  1122. }
  1123. }
  1124. $this->languages[] = $lang;
  1125. }
  1126. return $this->languages;
  1127. }
  1128. public function getCharsets()
  1129. {
  1130. if (null !== $this->charsets) {
  1131. return $this->charsets;
  1132. }
  1133. return $this->charsets = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Charset'))->all());
  1134. }
  1135. public function getEncodings()
  1136. {
  1137. if (null !== $this->encodings) {
  1138. return $this->encodings;
  1139. }
  1140. return $this->encodings = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Encoding'))->all());
  1141. }
  1142. public function getAcceptableContentTypes()
  1143. {
  1144. if (null !== $this->acceptableContentTypes) {
  1145. return $this->acceptableContentTypes;
  1146. }
  1147. return $this->acceptableContentTypes = array_keys(AcceptHeader::fromString($this->headers->get('Accept'))->all());
  1148. }
  1149. public function isXmlHttpRequest()
  1150. {
  1151. return'XMLHttpRequest'== $this->headers->get('X-Requested-With');
  1152. }
  1153. protected function prepareRequestUri()
  1154. {
  1155. $requestUri ='';
  1156. if ($this->headers->has('X_ORIGINAL_URL')) {
  1157. $requestUri = $this->headers->get('X_ORIGINAL_URL');
  1158. $this->headers->remove('X_ORIGINAL_URL');
  1159. $this->server->remove('HTTP_X_ORIGINAL_URL');
  1160. $this->server->remove('UNENCODED_URL');
  1161. $this->server->remove('IIS_WasUrlRewritten');
  1162. } elseif ($this->headers->has('X_REWRITE_URL')) {
  1163. $requestUri = $this->headers->get('X_REWRITE_URL');
  1164. $this->headers->remove('X_REWRITE_URL');
  1165. } elseif ($this->server->get('IIS_WasUrlRewritten') =='1'&& $this->server->get('UNENCODED_URL') !='') {
  1166. $requestUri = $this->server->get('UNENCODED_URL');
  1167. $this->server->remove('UNENCODED_URL');
  1168. $this->server->remove('IIS_WasUrlRewritten');
  1169. } elseif ($this->server->has('REQUEST_URI')) {
  1170. $requestUri = $this->server->get('REQUEST_URI');
  1171. $schemeAndHttpHost = $this->getSchemeAndHttpHost();
  1172. if (strpos($requestUri, $schemeAndHttpHost) === 0) {
  1173. $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
  1174. }
  1175. } elseif ($this->server->has('ORIG_PATH_INFO')) {
  1176. $requestUri = $this->server->get('ORIG_PATH_INFO');
  1177. if (''!= $this->server->get('QUERY_STRING')) {
  1178. $requestUri .='?'.$this->server->get('QUERY_STRING');
  1179. }
  1180. $this->server->remove('ORIG_PATH_INFO');
  1181. }
  1182. $this->server->set('REQUEST_URI', $requestUri);
  1183. return $requestUri;
  1184. }
  1185. protected function prepareBaseUrl()
  1186. {
  1187. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  1188. if (basename($this->server->get('SCRIPT_NAME')) === $filename) {
  1189. $baseUrl = $this->server->get('SCRIPT_NAME');
  1190. } elseif (basename($this->server->get('PHP_SELF')) === $filename) {
  1191. $baseUrl = $this->server->get('PHP_SELF');
  1192. } elseif (basename($this->server->get('ORIG_SCRIPT_NAME')) === $filename) {
  1193. $baseUrl = $this->server->get('ORIG_SCRIPT_NAME'); } else {
  1194. $path = $this->server->get('PHP_SELF','');
  1195. $file = $this->server->get('SCRIPT_FILENAME','');
  1196. $segs = explode('/', trim($file,'/'));
  1197. $segs = array_reverse($segs);
  1198. $index = 0;
  1199. $last = count($segs);
  1200. $baseUrl ='';
  1201. do {
  1202. $seg = $segs[$index];
  1203. $baseUrl ='/'.$seg.$baseUrl;
  1204. ++$index;
  1205. } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos);
  1206. }
  1207. $requestUri = $this->getRequestUri();
  1208. if ($requestUri !==''&& $requestUri[0] !=='/') {
  1209. $requestUri ='/'.$requestUri;
  1210. }
  1211. if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
  1212. return $prefix;
  1213. }
  1214. if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, rtrim(dirname($baseUrl),'/'.DIRECTORY_SEPARATOR).'/')) {
  1215. return rtrim($prefix,'/'.DIRECTORY_SEPARATOR);
  1216. }
  1217. $truncatedRequestUri = $requestUri;
  1218. if (false !== $pos = strpos($requestUri,'?')) {
  1219. $truncatedRequestUri = substr($requestUri, 0, $pos);
  1220. }
  1221. $basename = basename($baseUrl);
  1222. if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
  1223. return'';
  1224. }
  1225. if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && $pos !== 0) {
  1226. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  1227. }
  1228. return rtrim($baseUrl,'/'.DIRECTORY_SEPARATOR);
  1229. }
  1230. protected function prepareBasePath()
  1231. {
  1232. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  1233. $baseUrl = $this->getBaseUrl();
  1234. if (empty($baseUrl)) {
  1235. return'';
  1236. }
  1237. if (basename($baseUrl) === $filename) {
  1238. $basePath = dirname($baseUrl);
  1239. } else {
  1240. $basePath = $baseUrl;
  1241. }
  1242. if ('\\'=== DIRECTORY_SEPARATOR) {
  1243. $basePath = str_replace('\\','/', $basePath);
  1244. }
  1245. return rtrim($basePath,'/');
  1246. }
  1247. protected function preparePathInfo()
  1248. {
  1249. $baseUrl = $this->getBaseUrl();
  1250. if (null === ($requestUri = $this->getRequestUri())) {
  1251. return'/';
  1252. }
  1253. if (false !== $pos = strpos($requestUri,'?')) {
  1254. $requestUri = substr($requestUri, 0, $pos);
  1255. }
  1256. if ($requestUri !==''&& $requestUri[0] !=='/') {
  1257. $requestUri ='/'.$requestUri;
  1258. }
  1259. $pathInfo = substr($requestUri, strlen($baseUrl));
  1260. if (null !== $baseUrl && (false === $pathInfo ||''=== $pathInfo)) {
  1261. return'/';
  1262. } elseif (null === $baseUrl) {
  1263. return $requestUri;
  1264. }
  1265. return (string) $pathInfo;
  1266. }
  1267. protected static function initializeFormats()
  1268. {
  1269. static::$formats = array('html'=> array('text/html','application/xhtml+xml'),'txt'=> array('text/plain'),'js'=> array('application/javascript','application/x-javascript','text/javascript'),'css'=> array('text/css'),'json'=> array('application/json','application/x-json'),'xml'=> array('text/xml','application/xml','application/x-xml'),'rdf'=> array('application/rdf+xml'),'atom'=> array('application/atom+xml'),'rss'=> array('application/rss+xml'),'form'=> array('application/x-www-form-urlencoded'),
  1270. );
  1271. }
  1272. private function setPhpDefaultLocale($locale)
  1273. {
  1274. try {
  1275. if (class_exists('Locale', false)) {
  1276. \Locale::setDefault($locale);
  1277. }
  1278. } catch (\Exception $e) {
  1279. }
  1280. }
  1281. private function getUrlencodedPrefix($string, $prefix)
  1282. {
  1283. if (0 !== strpos(rawurldecode($string), $prefix)) {
  1284. return false;
  1285. }
  1286. $len = strlen($prefix);
  1287. if (preg_match(sprintf('#^(%%[[:xdigit:]]{2}|.){%d}#', $len), $string, $match)) {
  1288. return $match[0];
  1289. }
  1290. return false;
  1291. }
  1292. private static function createRequestFromFactory(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  1293. {
  1294. if (self::$requestFactory) {
  1295. $request = call_user_func(self::$requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);
  1296. if (!$request instanceof self) {
  1297. throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
  1298. }
  1299. return $request;
  1300. }
  1301. return new static($query, $request, $attributes, $cookies, $files, $server, $content);
  1302. }
  1303. private function isFromTrustedProxy()
  1304. {
  1305. return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR'), self::$trustedProxies);
  1306. }
  1307. private function getTrustedValues($type, $ip = null)
  1308. {
  1309. $clientValues = array();
  1310. $forwardedValues = array();
  1311. if (self::$trustedHeaders[$type] && $this->headers->has(self::$trustedHeaders[$type])) {
  1312. foreach (explode(',', $this->headers->get(self::$trustedHeaders[$type])) as $v) {
  1313. $clientValues[] = (self::HEADER_CLIENT_PORT === $type ?'0.0.0.0:':'').trim($v);
  1314. }
  1315. }
  1316. if (self::$trustedHeaders[self::HEADER_FORWARDED] && $this->headers->has(self::$trustedHeaders[self::HEADER_FORWARDED])) {
  1317. $forwardedValues = $this->headers->get(self::$trustedHeaders[self::HEADER_FORWARDED]);
  1318. $forwardedValues = preg_match_all(sprintf('{(?:%s)=(?:"?\[?)([a-zA-Z0-9\.:_\-/]*+)}', self::$forwardedParams[$type]), $forwardedValues, $matches) ? $matches[1] : array();
  1319. }
  1320. if (null !== $ip) {
  1321. $clientValues = $this->normalizeAndFilterClientIps($clientValues, $ip);
  1322. $forwardedValues = $this->normalizeAndFilterClientIps($forwardedValues, $ip);
  1323. }
  1324. if ($forwardedValues === $clientValues || !$clientValues) {
  1325. return $forwardedValues;
  1326. }
  1327. if (!$forwardedValues) {
  1328. return $clientValues;
  1329. }
  1330. if (!$this->isForwardedValid) {
  1331. return null !== $ip ? array('0.0.0.0', $ip) : array();
  1332. }
  1333. $this->isForwardedValid = false;
  1334. throw new ConflictingHeadersException(sprintf('The request has both a trusted "%s" header and a trusted "%s" header, conflicting with each other. You should either configure your proxy to remove one of them, or configure your project to distrust the offending one.', self::$trustedHeaders[self::HEADER_FORWARDED], self::$trustedHeaders[$type]));
  1335. }
  1336. private function normalizeAndFilterClientIps(array $clientIps, $ip)
  1337. {
  1338. if (!$clientIps) {
  1339. return array();
  1340. }
  1341. $clientIps[] = $ip; $firstTrustedIp = null;
  1342. foreach ($clientIps as $key => $clientIp) {
  1343. if (preg_match('{((?:\d+\.){3}\d+)\:\d+}', $clientIp, $match)) {
  1344. $clientIps[$key] = $clientIp = $match[1];
  1345. }
  1346. if (!filter_var($clientIp, FILTER_VALIDATE_IP)) {
  1347. unset($clientIps[$key]);
  1348. continue;
  1349. }
  1350. if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {
  1351. unset($clientIps[$key]);
  1352. if (null === $firstTrustedIp) {
  1353. $firstTrustedIp = $clientIp;
  1354. }
  1355. }
  1356. }
  1357. return $clientIps ? array_reverse($clientIps) : array($firstTrustedIp);
  1358. }
  1359. }
  1360. }