bootstrap.php.cache 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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 Symfony 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 Symfony 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(implode(', ', $this->headers[$key]));
  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. if (array_keys($keys) === $keys) {
  334. $file = array_filter($file);
  335. }
  336. }
  337. }
  338. return $file;
  339. }
  340. protected function fixPhpFilesArray($data)
  341. {
  342. if (!\is_array($data)) {
  343. return $data;
  344. }
  345. $keys = array_keys($data);
  346. sort($keys);
  347. if (self::$fileKeys != $keys || !isset($data['name']) || !\is_array($data['name'])) {
  348. return $data;
  349. }
  350. $files = $data;
  351. foreach (self::$fileKeys as $k) {
  352. unset($files[$k]);
  353. }
  354. foreach ($data['name'] as $key => $name) {
  355. $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],
  356. ));
  357. }
  358. return $files;
  359. }
  360. }
  361. }
  362. namespace Symfony\Component\HttpFoundation
  363. {
  364. class ServerBag extends ParameterBag
  365. {
  366. public function getHeaders()
  367. {
  368. $headers = array();
  369. $contentHeaders = array('CONTENT_LENGTH'=> true,'CONTENT_MD5'=> true,'CONTENT_TYPE'=> true);
  370. foreach ($this->parameters as $key => $value) {
  371. if (0 === strpos($key,'HTTP_')) {
  372. $headers[substr($key, 5)] = $value;
  373. }
  374. elseif (isset($contentHeaders[$key])) {
  375. $headers[$key] = $value;
  376. }
  377. }
  378. if (isset($this->parameters['PHP_AUTH_USER'])) {
  379. $headers['PHP_AUTH_USER'] = $this->parameters['PHP_AUTH_USER'];
  380. $headers['PHP_AUTH_PW'] = isset($this->parameters['PHP_AUTH_PW']) ? $this->parameters['PHP_AUTH_PW'] :'';
  381. } else {
  382. $authorizationHeader = null;
  383. if (isset($this->parameters['HTTP_AUTHORIZATION'])) {
  384. $authorizationHeader = $this->parameters['HTTP_AUTHORIZATION'];
  385. } elseif (isset($this->parameters['REDIRECT_HTTP_AUTHORIZATION'])) {
  386. $authorizationHeader = $this->parameters['REDIRECT_HTTP_AUTHORIZATION'];
  387. }
  388. if (null !== $authorizationHeader) {
  389. if (0 === stripos($authorizationHeader,'basic ')) {
  390. $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2);
  391. if (2 == \count($exploded)) {
  392. list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
  393. }
  394. } elseif (empty($this->parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader,'digest '))) {
  395. $headers['PHP_AUTH_DIGEST'] = $authorizationHeader;
  396. $this->parameters['PHP_AUTH_DIGEST'] = $authorizationHeader;
  397. } elseif (0 === stripos($authorizationHeader,'bearer ')) {
  398. $headers['AUTHORIZATION'] = $authorizationHeader;
  399. }
  400. }
  401. }
  402. if (isset($headers['AUTHORIZATION'])) {
  403. return $headers;
  404. }
  405. if (isset($headers['PHP_AUTH_USER'])) {
  406. $headers['AUTHORIZATION'] ='Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
  407. } elseif (isset($headers['PHP_AUTH_DIGEST'])) {
  408. $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST'];
  409. }
  410. return $headers;
  411. }
  412. }
  413. }
  414. namespace Symfony\Component\HttpFoundation
  415. {
  416. use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
  417. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  418. class Request
  419. {
  420. const HEADER_FORWARDED ='forwarded';
  421. const HEADER_CLIENT_IP ='client_ip';
  422. const HEADER_CLIENT_HOST ='client_host';
  423. const HEADER_CLIENT_PROTO ='client_proto';
  424. const HEADER_CLIENT_PORT ='client_port';
  425. const METHOD_HEAD ='HEAD';
  426. const METHOD_GET ='GET';
  427. const METHOD_POST ='POST';
  428. const METHOD_PUT ='PUT';
  429. const METHOD_PATCH ='PATCH';
  430. const METHOD_DELETE ='DELETE';
  431. const METHOD_PURGE ='PURGE';
  432. const METHOD_OPTIONS ='OPTIONS';
  433. const METHOD_TRACE ='TRACE';
  434. const METHOD_CONNECT ='CONNECT';
  435. protected static $trustedProxies = array();
  436. protected static $trustedHostPatterns = array();
  437. protected static $trustedHosts = array();
  438. protected static $trustedHeaders = array(
  439. self::HEADER_FORWARDED =>'FORWARDED',
  440. self::HEADER_CLIENT_IP =>'X_FORWARDED_FOR',
  441. self::HEADER_CLIENT_HOST =>'X_FORWARDED_HOST',
  442. self::HEADER_CLIENT_PROTO =>'X_FORWARDED_PROTO',
  443. self::HEADER_CLIENT_PORT =>'X_FORWARDED_PORT',
  444. );
  445. protected static $httpMethodParameterOverride = false;
  446. public $attributes;
  447. public $request;
  448. public $query;
  449. public $server;
  450. public $files;
  451. public $cookies;
  452. public $headers;
  453. protected $content;
  454. protected $languages;
  455. protected $charsets;
  456. protected $encodings;
  457. protected $acceptableContentTypes;
  458. protected $pathInfo;
  459. protected $requestUri;
  460. protected $baseUrl;
  461. protected $basePath;
  462. protected $method;
  463. protected $format;
  464. protected $session;
  465. protected $locale;
  466. protected $defaultLocale ='en';
  467. protected static $formats;
  468. protected static $requestFactory;
  469. private $isForwardedValid = true;
  470. private static $forwardedParams = array(
  471. self::HEADER_CLIENT_IP =>'for',
  472. self::HEADER_CLIENT_HOST =>'host',
  473. self::HEADER_CLIENT_PROTO =>'proto',
  474. self::HEADER_CLIENT_PORT =>'host',
  475. );
  476. public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  477. {
  478. $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
  479. }
  480. public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  481. {
  482. $this->request = new ParameterBag($request);
  483. $this->query = new ParameterBag($query);
  484. $this->attributes = new ParameterBag($attributes);
  485. $this->cookies = new ParameterBag($cookies);
  486. $this->files = new FileBag($files);
  487. $this->server = new ServerBag($server);
  488. $this->headers = new HeaderBag($this->server->getHeaders());
  489. $this->content = $content;
  490. $this->languages = null;
  491. $this->charsets = null;
  492. $this->encodings = null;
  493. $this->acceptableContentTypes = null;
  494. $this->pathInfo = null;
  495. $this->requestUri = null;
  496. $this->baseUrl = null;
  497. $this->basePath = null;
  498. $this->method = null;
  499. $this->format = null;
  500. }
  501. public static function createFromGlobals()
  502. {
  503. $server = $_SERVER;
  504. if ('cli-server'=== \PHP_SAPI) {
  505. if (array_key_exists('HTTP_CONTENT_LENGTH', $_SERVER)) {
  506. $server['CONTENT_LENGTH'] = $_SERVER['HTTP_CONTENT_LENGTH'];
  507. }
  508. if (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER)) {
  509. $server['CONTENT_TYPE'] = $_SERVER['HTTP_CONTENT_TYPE'];
  510. }
  511. }
  512. $request = self::createRequestFromFactory($_GET, $_POST, array(), $_COOKIE, $_FILES, $server);
  513. if (0 === strpos($request->headers->get('CONTENT_TYPE'),'application/x-www-form-urlencoded')
  514. && \in_array(strtoupper($request->server->get('REQUEST_METHOD','GET')), array('PUT','DELETE','PATCH'))
  515. ) {
  516. parse_str($request->getContent(), $data);
  517. $request->request = new ParameterBag($data);
  518. }
  519. return $request;
  520. }
  521. public static function create($uri, $method ='GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
  522. {
  523. $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(),
  524. ), $server);
  525. $server['PATH_INFO'] ='';
  526. $server['REQUEST_METHOD'] = strtoupper($method);
  527. $components = parse_url($uri);
  528. if (isset($components['host'])) {
  529. $server['SERVER_NAME'] = $components['host'];
  530. $server['HTTP_HOST'] = $components['host'];
  531. }
  532. if (isset($components['scheme'])) {
  533. if ('https'=== $components['scheme']) {
  534. $server['HTTPS'] ='on';
  535. $server['SERVER_PORT'] = 443;
  536. } else {
  537. unset($server['HTTPS']);
  538. $server['SERVER_PORT'] = 80;
  539. }
  540. }
  541. if (isset($components['port'])) {
  542. $server['SERVER_PORT'] = $components['port'];
  543. $server['HTTP_HOST'] .=':'.$components['port'];
  544. }
  545. if (isset($components['user'])) {
  546. $server['PHP_AUTH_USER'] = $components['user'];
  547. }
  548. if (isset($components['pass'])) {
  549. $server['PHP_AUTH_PW'] = $components['pass'];
  550. }
  551. if (!isset($components['path'])) {
  552. $components['path'] ='/';
  553. }
  554. switch (strtoupper($method)) {
  555. case'POST':
  556. case'PUT':
  557. case'DELETE':
  558. if (!isset($server['CONTENT_TYPE'])) {
  559. $server['CONTENT_TYPE'] ='application/x-www-form-urlencoded';
  560. }
  561. case'PATCH':
  562. $request = $parameters;
  563. $query = array();
  564. break;
  565. default:
  566. $request = array();
  567. $query = $parameters;
  568. break;
  569. }
  570. $queryString ='';
  571. if (isset($components['query'])) {
  572. parse_str(html_entity_decode($components['query']), $qs);
  573. if ($query) {
  574. $query = array_replace($qs, $query);
  575. $queryString = http_build_query($query,'','&');
  576. } else {
  577. $query = $qs;
  578. $queryString = $components['query'];
  579. }
  580. } elseif ($query) {
  581. $queryString = http_build_query($query,'','&');
  582. }
  583. $server['REQUEST_URI'] = $components['path'].(''!== $queryString ?'?'.$queryString :'');
  584. $server['QUERY_STRING'] = $queryString;
  585. return self::createRequestFromFactory($query, $request, array(), $cookies, $files, $server, $content);
  586. }
  587. public static function setFactory($callable)
  588. {
  589. self::$requestFactory = $callable;
  590. }
  591. public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
  592. {
  593. $dup = clone $this;
  594. if (null !== $query) {
  595. $dup->query = new ParameterBag($query);
  596. }
  597. if (null !== $request) {
  598. $dup->request = new ParameterBag($request);
  599. }
  600. if (null !== $attributes) {
  601. $dup->attributes = new ParameterBag($attributes);
  602. }
  603. if (null !== $cookies) {
  604. $dup->cookies = new ParameterBag($cookies);
  605. }
  606. if (null !== $files) {
  607. $dup->files = new FileBag($files);
  608. }
  609. if (null !== $server) {
  610. $dup->server = new ServerBag($server);
  611. $dup->headers = new HeaderBag($dup->server->getHeaders());
  612. }
  613. $dup->languages = null;
  614. $dup->charsets = null;
  615. $dup->encodings = null;
  616. $dup->acceptableContentTypes = null;
  617. $dup->pathInfo = null;
  618. $dup->requestUri = null;
  619. $dup->baseUrl = null;
  620. $dup->basePath = null;
  621. $dup->method = null;
  622. $dup->format = null;
  623. if (!$dup->get('_format') && $this->get('_format')) {
  624. $dup->attributes->set('_format', $this->get('_format'));
  625. }
  626. if (!$dup->getRequestFormat(null)) {
  627. $dup->setRequestFormat($this->getRequestFormat(null));
  628. }
  629. return $dup;
  630. }
  631. public function __clone()
  632. {
  633. $this->query = clone $this->query;
  634. $this->request = clone $this->request;
  635. $this->attributes = clone $this->attributes;
  636. $this->cookies = clone $this->cookies;
  637. $this->files = clone $this->files;
  638. $this->server = clone $this->server;
  639. $this->headers = clone $this->headers;
  640. }
  641. public function __toString()
  642. {
  643. try {
  644. $content = $this->getContent();
  645. } catch (\LogicException $e) {
  646. return trigger_error($e, E_USER_ERROR);
  647. }
  648. $cookieHeader ='';
  649. $cookies = array();
  650. foreach ($this->cookies as $k => $v) {
  651. $cookies[] = $k.'='.$v;
  652. }
  653. if (!empty($cookies)) {
  654. $cookieHeader ='Cookie: '.implode('; ', $cookies)."\r\n";
  655. }
  656. return
  657. sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
  658. $this->headers.
  659. $cookieHeader."\r\n".
  660. $content;
  661. }
  662. public function overrideGlobals()
  663. {
  664. $this->server->set('QUERY_STRING', static::normalizeQueryString(http_build_query($this->query->all(),'','&')));
  665. $_GET = $this->query->all();
  666. $_POST = $this->request->all();
  667. $_SERVER = $this->server->all();
  668. $_COOKIE = $this->cookies->all();
  669. foreach ($this->headers->all() as $key => $value) {
  670. $key = strtoupper(str_replace('-','_', $key));
  671. if (\in_array($key, array('CONTENT_TYPE','CONTENT_LENGTH'))) {
  672. $_SERVER[$key] = implode(', ', $value);
  673. } else {
  674. $_SERVER['HTTP_'.$key] = implode(', ', $value);
  675. }
  676. }
  677. $request = array('g'=> $_GET,'p'=> $_POST,'c'=> $_COOKIE);
  678. $requestOrder = ini_get('request_order') ?: ini_get('variables_order');
  679. $requestOrder = preg_replace('#[^cgp]#','', strtolower($requestOrder)) ?:'gp';
  680. $_REQUEST = array();
  681. foreach (str_split($requestOrder) as $order) {
  682. $_REQUEST = array_merge($_REQUEST, $request[$order]);
  683. }
  684. }
  685. public static function setTrustedProxies(array $proxies)
  686. {
  687. self::$trustedProxies = $proxies;
  688. }
  689. public static function getTrustedProxies()
  690. {
  691. return self::$trustedProxies;
  692. }
  693. public static function setTrustedHosts(array $hostPatterns)
  694. {
  695. self::$trustedHostPatterns = array_map(function ($hostPattern) {
  696. return sprintf('{%s}i', $hostPattern);
  697. }, $hostPatterns);
  698. self::$trustedHosts = array();
  699. }
  700. public static function getTrustedHosts()
  701. {
  702. return self::$trustedHostPatterns;
  703. }
  704. public static function setTrustedHeaderName($key, $value)
  705. {
  706. if (!array_key_exists($key, self::$trustedHeaders)) {
  707. throw new \InvalidArgumentException(sprintf('Unable to set the trusted header name for key "%s".', $key));
  708. }
  709. self::$trustedHeaders[$key] = $value;
  710. }
  711. public static function getTrustedHeaderName($key)
  712. {
  713. if (!array_key_exists($key, self::$trustedHeaders)) {
  714. throw new \InvalidArgumentException(sprintf('Unable to get the trusted header name for key "%s".', $key));
  715. }
  716. return self::$trustedHeaders[$key];
  717. }
  718. public static function normalizeQueryString($qs)
  719. {
  720. if (''== $qs) {
  721. return'';
  722. }
  723. $parts = array();
  724. $order = array();
  725. foreach (explode('&', $qs) as $param) {
  726. if (''=== $param ||'='=== $param[0]) {
  727. continue;
  728. }
  729. $keyValuePair = explode('=', $param, 2);
  730. $parts[] = isset($keyValuePair[1]) ?
  731. rawurlencode(urldecode($keyValuePair[0])).'='.rawurlencode(urldecode($keyValuePair[1])) :
  732. rawurlencode(urldecode($keyValuePair[0]));
  733. $order[] = urldecode($keyValuePair[0]);
  734. }
  735. array_multisort($order, SORT_ASC, $parts);
  736. return implode('&', $parts);
  737. }
  738. public static function enableHttpMethodParameterOverride()
  739. {
  740. self::$httpMethodParameterOverride = true;
  741. }
  742. public static function getHttpMethodParameterOverride()
  743. {
  744. return self::$httpMethodParameterOverride;
  745. }
  746. public function get($key, $default = null, $deep = false)
  747. {
  748. if ($deep) {
  749. @trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since Symfony 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
  750. }
  751. if ($this !== $result = $this->query->get($key, $this, $deep)) {
  752. return $result;
  753. }
  754. if ($this !== $result = $this->attributes->get($key, $this, $deep)) {
  755. return $result;
  756. }
  757. if ($this !== $result = $this->request->get($key, $this, $deep)) {
  758. return $result;
  759. }
  760. return $default;
  761. }
  762. public function getSession()
  763. {
  764. return $this->session;
  765. }
  766. public function hasPreviousSession()
  767. {
  768. return $this->hasSession() && $this->cookies->has($this->session->getName());
  769. }
  770. public function hasSession()
  771. {
  772. return null !== $this->session;
  773. }
  774. public function setSession(SessionInterface $session)
  775. {
  776. $this->session = $session;
  777. }
  778. public function getClientIps()
  779. {
  780. $ip = $this->server->get('REMOTE_ADDR');
  781. if (!$this->isFromTrustedProxy()) {
  782. return array($ip);
  783. }
  784. return $this->getTrustedValues(self::HEADER_CLIENT_IP, $ip) ?: array($ip);
  785. }
  786. public function getClientIp()
  787. {
  788. $ipAddresses = $this->getClientIps();
  789. return $ipAddresses[0];
  790. }
  791. public function getScriptName()
  792. {
  793. return $this->server->get('SCRIPT_NAME', $this->server->get('ORIG_SCRIPT_NAME',''));
  794. }
  795. public function getPathInfo()
  796. {
  797. if (null === $this->pathInfo) {
  798. $this->pathInfo = $this->preparePathInfo();
  799. }
  800. return $this->pathInfo;
  801. }
  802. public function getBasePath()
  803. {
  804. if (null === $this->basePath) {
  805. $this->basePath = $this->prepareBasePath();
  806. }
  807. return $this->basePath;
  808. }
  809. public function getBaseUrl()
  810. {
  811. if (null === $this->baseUrl) {
  812. $this->baseUrl = $this->prepareBaseUrl();
  813. }
  814. return $this->baseUrl;
  815. }
  816. public function getScheme()
  817. {
  818. return $this->isSecure() ?'https':'http';
  819. }
  820. public function getPort()
  821. {
  822. if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_CLIENT_PORT)) {
  823. $host = $host[0];
  824. } elseif ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_CLIENT_HOST)) {
  825. $host = $host[0];
  826. } elseif (!$host = $this->headers->get('HOST')) {
  827. return $this->server->get('SERVER_PORT');
  828. }
  829. if ('['=== $host[0]) {
  830. $pos = strpos($host,':', strrpos($host,']'));
  831. } else {
  832. $pos = strrpos($host,':');
  833. }
  834. if (false !== $pos) {
  835. return (int) substr($host, $pos + 1);
  836. }
  837. return'https'=== $this->getScheme() ? 443 : 80;
  838. }
  839. public function getUser()
  840. {
  841. return $this->headers->get('PHP_AUTH_USER');
  842. }
  843. public function getPassword()
  844. {
  845. return $this->headers->get('PHP_AUTH_PW');
  846. }
  847. public function getUserInfo()
  848. {
  849. $userinfo = $this->getUser();
  850. $pass = $this->getPassword();
  851. if (''!= $pass) {
  852. $userinfo .= ":$pass";
  853. }
  854. return $userinfo;
  855. }
  856. public function getHttpHost()
  857. {
  858. $scheme = $this->getScheme();
  859. $port = $this->getPort();
  860. if (('http'== $scheme && 80 == $port) || ('https'== $scheme && 443 == $port)) {
  861. return $this->getHost();
  862. }
  863. return $this->getHost().':'.$port;
  864. }
  865. public function getRequestUri()
  866. {
  867. if (null === $this->requestUri) {
  868. $this->requestUri = $this->prepareRequestUri();
  869. }
  870. return $this->requestUri;
  871. }
  872. public function getSchemeAndHttpHost()
  873. {
  874. return $this->getScheme().'://'.$this->getHttpHost();
  875. }
  876. public function getUri()
  877. {
  878. if (null !== $qs = $this->getQueryString()) {
  879. $qs ='?'.$qs;
  880. }
  881. return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
  882. }
  883. public function getUriForPath($path)
  884. {
  885. return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
  886. }
  887. public function getRelativeUriForPath($path)
  888. {
  889. if (!isset($path[0]) ||'/'!== $path[0]) {
  890. return $path;
  891. }
  892. if ($path === $basePath = $this->getPathInfo()) {
  893. return'';
  894. }
  895. $sourceDirs = explode('/', isset($basePath[0]) &&'/'=== $basePath[0] ? substr($basePath, 1) : $basePath);
  896. $targetDirs = explode('/', substr($path, 1));
  897. array_pop($sourceDirs);
  898. $targetFile = array_pop($targetDirs);
  899. foreach ($sourceDirs as $i => $dir) {
  900. if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
  901. unset($sourceDirs[$i], $targetDirs[$i]);
  902. } else {
  903. break;
  904. }
  905. }
  906. $targetDirs[] = $targetFile;
  907. $path = str_repeat('../', \count($sourceDirs)).implode('/', $targetDirs);
  908. return !isset($path[0]) ||'/'=== $path[0]
  909. || false !== ($colonPos = strpos($path,':')) && ($colonPos < ($slashPos = strpos($path,'/')) || false === $slashPos)
  910. ? "./$path" : $path;
  911. }
  912. public function getQueryString()
  913. {
  914. $qs = static::normalizeQueryString($this->server->get('QUERY_STRING'));
  915. return''=== $qs ? null : $qs;
  916. }
  917. public function isSecure()
  918. {
  919. if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_CLIENT_PROTO)) {
  920. return \in_array(strtolower($proto[0]), array('https','on','ssl','1'), true);
  921. }
  922. $https = $this->server->get('HTTPS');
  923. return !empty($https) &&'off'!== strtolower($https);
  924. }
  925. public function getHost()
  926. {
  927. if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_CLIENT_HOST)) {
  928. $host = $host[0];
  929. } elseif (!$host = $this->headers->get('HOST')) {
  930. if (!$host = $this->server->get('SERVER_NAME')) {
  931. $host = $this->server->get('SERVER_ADDR','');
  932. }
  933. }
  934. $host = strtolower(preg_replace('/:\d+$/','', trim($host)));
  935. if ($host &&''!== preg_replace('/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/','', $host)) {
  936. throw new \UnexpectedValueException(sprintf('Invalid Host "%s"', $host));
  937. }
  938. if (\count(self::$trustedHostPatterns) > 0) {
  939. if (\in_array($host, self::$trustedHosts)) {
  940. return $host;
  941. }
  942. foreach (self::$trustedHostPatterns as $pattern) {
  943. if (preg_match($pattern, $host)) {
  944. self::$trustedHosts[] = $host;
  945. return $host;
  946. }
  947. }
  948. throw new \UnexpectedValueException(sprintf('Untrusted Host "%s"', $host));
  949. }
  950. return $host;
  951. }
  952. public function setMethod($method)
  953. {
  954. $this->method = null;
  955. $this->server->set('REQUEST_METHOD', $method);
  956. }
  957. public function getMethod()
  958. {
  959. if (null === $this->method) {
  960. $this->method = strtoupper($this->server->get('REQUEST_METHOD','GET'));
  961. if ('POST'=== $this->method) {
  962. if ($method = $this->headers->get('X-HTTP-METHOD-OVERRIDE')) {
  963. $this->method = strtoupper($method);
  964. } elseif (self::$httpMethodParameterOverride) {
  965. $method = $this->request->get('_method', $this->query->get('_method','POST'));
  966. if (\is_string($method)) {
  967. $this->method = strtoupper($method);
  968. }
  969. }
  970. }
  971. }
  972. return $this->method;
  973. }
  974. public function getRealMethod()
  975. {
  976. return strtoupper($this->server->get('REQUEST_METHOD','GET'));
  977. }
  978. public function getMimeType($format)
  979. {
  980. if (null === static::$formats) {
  981. static::initializeFormats();
  982. }
  983. return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
  984. }
  985. public function getFormat($mimeType)
  986. {
  987. $canonicalMimeType = null;
  988. if (false !== $pos = strpos($mimeType,';')) {
  989. $canonicalMimeType = trim(substr($mimeType, 0, $pos));
  990. }
  991. if (null === static::$formats) {
  992. static::initializeFormats();
  993. }
  994. foreach (static::$formats as $format => $mimeTypes) {
  995. if (\in_array($mimeType, (array) $mimeTypes)) {
  996. return $format;
  997. }
  998. if (null !== $canonicalMimeType && \in_array($canonicalMimeType, (array) $mimeTypes)) {
  999. return $format;
  1000. }
  1001. }
  1002. }
  1003. public function setFormat($format, $mimeTypes)
  1004. {
  1005. if (null === static::$formats) {
  1006. static::initializeFormats();
  1007. }
  1008. static::$formats[$format] = \is_array($mimeTypes) ? $mimeTypes : array($mimeTypes);
  1009. }
  1010. public function getRequestFormat($default ='html')
  1011. {
  1012. if (null === $this->format) {
  1013. $this->format = $this->get('_format');
  1014. }
  1015. return null === $this->format ? $default : $this->format;
  1016. }
  1017. public function setRequestFormat($format)
  1018. {
  1019. $this->format = $format;
  1020. }
  1021. public function getContentType()
  1022. {
  1023. return $this->getFormat($this->headers->get('CONTENT_TYPE'));
  1024. }
  1025. public function setDefaultLocale($locale)
  1026. {
  1027. $this->defaultLocale = $locale;
  1028. if (null === $this->locale) {
  1029. $this->setPhpDefaultLocale($locale);
  1030. }
  1031. }
  1032. public function getDefaultLocale()
  1033. {
  1034. return $this->defaultLocale;
  1035. }
  1036. public function setLocale($locale)
  1037. {
  1038. $this->setPhpDefaultLocale($this->locale = $locale);
  1039. }
  1040. public function getLocale()
  1041. {
  1042. return null === $this->locale ? $this->defaultLocale : $this->locale;
  1043. }
  1044. public function isMethod($method)
  1045. {
  1046. return $this->getMethod() === strtoupper($method);
  1047. }
  1048. public function isMethodSafe()
  1049. {
  1050. return \in_array($this->getMethod(), 0 < \func_num_args() && !func_get_arg(0) ? array('GET','HEAD','OPTIONS','TRACE') : array('GET','HEAD'));
  1051. }
  1052. public function isMethodCacheable()
  1053. {
  1054. return \in_array($this->getMethod(), array('GET','HEAD'));
  1055. }
  1056. public function getContent($asResource = false)
  1057. {
  1058. $currentContentIsResource = \is_resource($this->content);
  1059. if (\PHP_VERSION_ID < 50600 && false === $this->content) {
  1060. throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
  1061. }
  1062. if (true === $asResource) {
  1063. if ($currentContentIsResource) {
  1064. rewind($this->content);
  1065. return $this->content;
  1066. }
  1067. if (\is_string($this->content)) {
  1068. $resource = fopen('php://temp','r+');
  1069. fwrite($resource, $this->content);
  1070. rewind($resource);
  1071. return $resource;
  1072. }
  1073. $this->content = false;
  1074. return fopen('php://input','rb');
  1075. }
  1076. if ($currentContentIsResource) {
  1077. rewind($this->content);
  1078. return stream_get_contents($this->content);
  1079. }
  1080. if (null === $this->content || false === $this->content) {
  1081. $this->content = file_get_contents('php://input');
  1082. }
  1083. return $this->content;
  1084. }
  1085. public function getETags()
  1086. {
  1087. return preg_split('/\s*,\s*/', $this->headers->get('if_none_match'), null, PREG_SPLIT_NO_EMPTY);
  1088. }
  1089. public function isNoCache()
  1090. {
  1091. return $this->headers->hasCacheControlDirective('no-cache') ||'no-cache'== $this->headers->get('Pragma');
  1092. }
  1093. public function getPreferredLanguage(array $locales = null)
  1094. {
  1095. $preferredLanguages = $this->getLanguages();
  1096. if (empty($locales)) {
  1097. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null;
  1098. }
  1099. if (!$preferredLanguages) {
  1100. return $locales[0];
  1101. }
  1102. $extendedPreferredLanguages = array();
  1103. foreach ($preferredLanguages as $language) {
  1104. $extendedPreferredLanguages[] = $language;
  1105. if (false !== $position = strpos($language,'_')) {
  1106. $superLanguage = substr($language, 0, $position);
  1107. if (!\in_array($superLanguage, $preferredLanguages)) {
  1108. $extendedPreferredLanguages[] = $superLanguage;
  1109. }
  1110. }
  1111. }
  1112. $preferredLanguages = array_values(array_intersect($extendedPreferredLanguages, $locales));
  1113. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : $locales[0];
  1114. }
  1115. public function getLanguages()
  1116. {
  1117. if (null !== $this->languages) {
  1118. return $this->languages;
  1119. }
  1120. $languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
  1121. $this->languages = array();
  1122. foreach ($languages as $lang => $acceptHeaderItem) {
  1123. if (false !== strpos($lang,'-')) {
  1124. $codes = explode('-', $lang);
  1125. if ('i'=== $codes[0]) {
  1126. if (\count($codes) > 1) {
  1127. $lang = $codes[1];
  1128. }
  1129. } else {
  1130. for ($i = 0, $max = \count($codes); $i < $max; ++$i) {
  1131. if (0 === $i) {
  1132. $lang = strtolower($codes[0]);
  1133. } else {
  1134. $lang .='_'.strtoupper($codes[$i]);
  1135. }
  1136. }
  1137. }
  1138. }
  1139. $this->languages[] = $lang;
  1140. }
  1141. return $this->languages;
  1142. }
  1143. public function getCharsets()
  1144. {
  1145. if (null !== $this->charsets) {
  1146. return $this->charsets;
  1147. }
  1148. return $this->charsets = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Charset'))->all());
  1149. }
  1150. public function getEncodings()
  1151. {
  1152. if (null !== $this->encodings) {
  1153. return $this->encodings;
  1154. }
  1155. return $this->encodings = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Encoding'))->all());
  1156. }
  1157. public function getAcceptableContentTypes()
  1158. {
  1159. if (null !== $this->acceptableContentTypes) {
  1160. return $this->acceptableContentTypes;
  1161. }
  1162. return $this->acceptableContentTypes = array_keys(AcceptHeader::fromString($this->headers->get('Accept'))->all());
  1163. }
  1164. public function isXmlHttpRequest()
  1165. {
  1166. return'XMLHttpRequest'== $this->headers->get('X-Requested-With');
  1167. }
  1168. protected function prepareRequestUri()
  1169. {
  1170. $requestUri ='';
  1171. if ('1'== $this->server->get('IIS_WasUrlRewritten') &&''!= $this->server->get('UNENCODED_URL')) {
  1172. $requestUri = $this->server->get('UNENCODED_URL');
  1173. $this->server->remove('UNENCODED_URL');
  1174. $this->server->remove('IIS_WasUrlRewritten');
  1175. } elseif ($this->server->has('REQUEST_URI')) {
  1176. $requestUri = $this->server->get('REQUEST_URI');
  1177. $schemeAndHttpHost = $this->getSchemeAndHttpHost();
  1178. if (0 === strpos($requestUri, $schemeAndHttpHost)) {
  1179. $requestUri = substr($requestUri, \strlen($schemeAndHttpHost));
  1180. }
  1181. } elseif ($this->server->has('ORIG_PATH_INFO')) {
  1182. $requestUri = $this->server->get('ORIG_PATH_INFO');
  1183. if (''!= $this->server->get('QUERY_STRING')) {
  1184. $requestUri .='?'.$this->server->get('QUERY_STRING');
  1185. }
  1186. $this->server->remove('ORIG_PATH_INFO');
  1187. }
  1188. $this->server->set('REQUEST_URI', $requestUri);
  1189. return $requestUri;
  1190. }
  1191. protected function prepareBaseUrl()
  1192. {
  1193. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  1194. if (basename($this->server->get('SCRIPT_NAME')) === $filename) {
  1195. $baseUrl = $this->server->get('SCRIPT_NAME');
  1196. } elseif (basename($this->server->get('PHP_SELF')) === $filename) {
  1197. $baseUrl = $this->server->get('PHP_SELF');
  1198. } elseif (basename($this->server->get('ORIG_SCRIPT_NAME')) === $filename) {
  1199. $baseUrl = $this->server->get('ORIG_SCRIPT_NAME'); } else {
  1200. $path = $this->server->get('PHP_SELF','');
  1201. $file = $this->server->get('SCRIPT_FILENAME','');
  1202. $segs = explode('/', trim($file,'/'));
  1203. $segs = array_reverse($segs);
  1204. $index = 0;
  1205. $last = \count($segs);
  1206. $baseUrl ='';
  1207. do {
  1208. $seg = $segs[$index];
  1209. $baseUrl ='/'.$seg.$baseUrl;
  1210. ++$index;
  1211. } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos);
  1212. }
  1213. $requestUri = $this->getRequestUri();
  1214. if (''!== $requestUri &&'/'!== $requestUri[0]) {
  1215. $requestUri ='/'.$requestUri;
  1216. }
  1217. if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
  1218. return $prefix;
  1219. }
  1220. if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, rtrim(\dirname($baseUrl),'/'.\DIRECTORY_SEPARATOR).'/')) {
  1221. return rtrim($prefix,'/'.\DIRECTORY_SEPARATOR);
  1222. }
  1223. $truncatedRequestUri = $requestUri;
  1224. if (false !== $pos = strpos($requestUri,'?')) {
  1225. $truncatedRequestUri = substr($requestUri, 0, $pos);
  1226. }
  1227. $basename = basename($baseUrl);
  1228. if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
  1229. return'';
  1230. }
  1231. if (\strlen($requestUri) >= \strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) {
  1232. $baseUrl = substr($requestUri, 0, $pos + \strlen($baseUrl));
  1233. }
  1234. return rtrim($baseUrl,'/'.\DIRECTORY_SEPARATOR);
  1235. }
  1236. protected function prepareBasePath()
  1237. {
  1238. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  1239. $baseUrl = $this->getBaseUrl();
  1240. if (empty($baseUrl)) {
  1241. return'';
  1242. }
  1243. if (basename($baseUrl) === $filename) {
  1244. $basePath = \dirname($baseUrl);
  1245. } else {
  1246. $basePath = $baseUrl;
  1247. }
  1248. if ('\\'=== \DIRECTORY_SEPARATOR) {
  1249. $basePath = str_replace('\\','/', $basePath);
  1250. }
  1251. return rtrim($basePath,'/');
  1252. }
  1253. protected function preparePathInfo()
  1254. {
  1255. $baseUrl = $this->getBaseUrl();
  1256. if (null === ($requestUri = $this->getRequestUri())) {
  1257. return'/';
  1258. }
  1259. if (false !== $pos = strpos($requestUri,'?')) {
  1260. $requestUri = substr($requestUri, 0, $pos);
  1261. }
  1262. if (''!== $requestUri &&'/'!== $requestUri[0]) {
  1263. $requestUri ='/'.$requestUri;
  1264. }
  1265. $pathInfo = substr($requestUri, \strlen($baseUrl));
  1266. if (null !== $baseUrl && (false === $pathInfo ||''=== $pathInfo)) {
  1267. return'/';
  1268. } elseif (null === $baseUrl) {
  1269. return $requestUri;
  1270. }
  1271. return (string) $pathInfo;
  1272. }
  1273. protected static function initializeFormats()
  1274. {
  1275. 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'),'jsonld'=> array('application/ld+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'),
  1276. );
  1277. }
  1278. private function setPhpDefaultLocale($locale)
  1279. {
  1280. try {
  1281. if (class_exists('Locale', false)) {
  1282. \Locale::setDefault($locale);
  1283. }
  1284. } catch (\Exception $e) {
  1285. }
  1286. }
  1287. private function getUrlencodedPrefix($string, $prefix)
  1288. {
  1289. if (0 !== strpos(rawurldecode($string), $prefix)) {
  1290. return false;
  1291. }
  1292. $len = \strlen($prefix);
  1293. if (preg_match(sprintf('#^(%%[[:xdigit:]]{2}|.){%d}#', $len), $string, $match)) {
  1294. return $match[0];
  1295. }
  1296. return false;
  1297. }
  1298. private static function createRequestFromFactory(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  1299. {
  1300. if (self::$requestFactory) {
  1301. $request = \call_user_func(self::$requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);
  1302. if (!$request instanceof self) {
  1303. throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
  1304. }
  1305. return $request;
  1306. }
  1307. return new static($query, $request, $attributes, $cookies, $files, $server, $content);
  1308. }
  1309. private function isFromTrustedProxy()
  1310. {
  1311. return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR'), self::$trustedProxies);
  1312. }
  1313. private function getTrustedValues($type, $ip = null)
  1314. {
  1315. $clientValues = array();
  1316. $forwardedValues = array();
  1317. if (self::$trustedHeaders[$type] && $this->headers->has(self::$trustedHeaders[$type])) {
  1318. foreach (explode(',', $this->headers->get(self::$trustedHeaders[$type])) as $v) {
  1319. $clientValues[] = (self::HEADER_CLIENT_PORT === $type ?'0.0.0.0:':'').trim($v);
  1320. }
  1321. }
  1322. if (self::$trustedHeaders[self::HEADER_FORWARDED] && $this->headers->has(self::$trustedHeaders[self::HEADER_FORWARDED])) {
  1323. $forwardedValues = $this->headers->get(self::$trustedHeaders[self::HEADER_FORWARDED]);
  1324. $forwardedValues = preg_match_all(sprintf('{(?:%s)="?([a-zA-Z0-9\.:_\-/\[\]]*+)}', self::$forwardedParams[$type]), $forwardedValues, $matches) ? $matches[1] : array();
  1325. if (self::HEADER_CLIENT_PORT === $type) {
  1326. foreach ($forwardedValues as $k => $v) {
  1327. if (']'=== substr($v, -1) || false === $v = strrchr($v,':')) {
  1328. $v = $this->isSecure() ?':443':':80';
  1329. }
  1330. $forwardedValues[$k] ='0.0.0.0'.$v;
  1331. }
  1332. }
  1333. }
  1334. if (null !== $ip) {
  1335. $clientValues = $this->normalizeAndFilterClientIps($clientValues, $ip);
  1336. $forwardedValues = $this->normalizeAndFilterClientIps($forwardedValues, $ip);
  1337. }
  1338. if ($forwardedValues === $clientValues || !$clientValues) {
  1339. return $forwardedValues;
  1340. }
  1341. if (!$forwardedValues) {
  1342. return $clientValues;
  1343. }
  1344. if (!$this->isForwardedValid) {
  1345. return null !== $ip ? array('0.0.0.0', $ip) : array();
  1346. }
  1347. $this->isForwardedValid = false;
  1348. 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]));
  1349. }
  1350. private function normalizeAndFilterClientIps(array $clientIps, $ip)
  1351. {
  1352. if (!$clientIps) {
  1353. return array();
  1354. }
  1355. $clientIps[] = $ip; $firstTrustedIp = null;
  1356. foreach ($clientIps as $key => $clientIp) {
  1357. if (strpos($clientIp,'.')) {
  1358. $i = strpos($clientIp,':');
  1359. if ($i) {
  1360. $clientIps[$key] = $clientIp = substr($clientIp, 0, $i);
  1361. }
  1362. } elseif (0 === strpos($clientIp,'[')) {
  1363. $i = strpos($clientIp,']', 1);
  1364. $clientIps[$key] = $clientIp = substr($clientIp, 1, $i - 1);
  1365. }
  1366. if (!filter_var($clientIp, FILTER_VALIDATE_IP)) {
  1367. unset($clientIps[$key]);
  1368. continue;
  1369. }
  1370. if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {
  1371. unset($clientIps[$key]);
  1372. if (null === $firstTrustedIp) {
  1373. $firstTrustedIp = $clientIp;
  1374. }
  1375. }
  1376. }
  1377. return $clientIps ? array_reverse($clientIps) : array($firstTrustedIp);
  1378. }
  1379. }
  1380. }