Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Http / PhpEnvironment / Request.php
blob9b57fcad7c9da0ac5929ab630b2bf6143bae0c6c
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Http\PhpEnvironment;
12 use Zend\Http\Header\Cookie;
13 use Zend\Http\Request as HttpRequest;
14 use Zend\Stdlib\Parameters;
15 use Zend\Stdlib\ParametersInterface;
16 use Zend\Uri\Http as HttpUri;
17 use Zend\Validator\Hostname as HostnameValidator;
19 /**
20 * HTTP Request for current PHP environment
22 class Request extends HttpRequest
24 /**
25 * Base URL of the application.
27 * @var string
29 protected $baseUrl;
31 /**
32 * Base Path of the application.
34 * @var string
36 protected $basePath;
38 /**
39 * Actual request URI, independent of the platform.
41 * @var string
43 protected $requestUri;
45 /**
46 * PHP server params ($_SERVER)
48 * @var ParametersInterface
50 protected $serverParams = null;
52 /**
53 * PHP environment params ($_ENV)
55 * @var ParametersInterface
57 protected $envParams = null;
59 /**
60 * Construct
61 * Instantiates request.
63 public function __construct()
65 $this->setEnv(new Parameters($_ENV));
67 if ($_GET) {
68 $this->setQuery(new Parameters($_GET));
70 if ($_POST) {
71 $this->setPost(new Parameters($_POST));
73 if ($_COOKIE) {
74 $this->setCookies(new Parameters($_COOKIE));
76 if ($_FILES) {
77 // convert PHP $_FILES superglobal
78 $files = $this->mapPhpFiles();
79 $this->setFiles(new Parameters($files));
82 $this->setServer(new Parameters($_SERVER));
85 /**
86 * Get raw request body
88 * @return string
90 public function getContent()
92 if (empty($this->content)) {
93 $requestBody = file_get_contents('php://input');
94 if (strlen($requestBody) > 0) {
95 $this->content = $requestBody;
99 return $this->content;
103 * Set cookies
105 * Instantiate and set cookies.
107 * @param $cookie
108 * @return Request
110 public function setCookies($cookie)
112 $this->getHeaders()->addHeader(new Cookie((array) $cookie));
113 return $this;
117 * Set the request URI.
119 * @param string $requestUri
120 * @return self
122 public function setRequestUri($requestUri)
124 $this->requestUri = $requestUri;
125 return $this;
129 * Get the request URI.
131 * @return string
133 public function getRequestUri()
135 if ($this->requestUri === null) {
136 $this->requestUri = $this->detectRequestUri();
138 return $this->requestUri;
142 * Set the base URL.
144 * @param string $baseUrl
145 * @return self
147 public function setBaseUrl($baseUrl)
149 $this->baseUrl = rtrim($baseUrl, '/');
150 return $this;
154 * Get the base URL.
156 * @return string
158 public function getBaseUrl()
160 if ($this->baseUrl === null) {
161 $this->setBaseUrl($this->detectBaseUrl());
163 return $this->baseUrl;
167 * Set the base path.
169 * @param string $basePath
170 * @return self
172 public function setBasePath($basePath)
174 $this->basePath = rtrim($basePath, '/');
175 return $this;
179 * Get the base path.
181 * @return string
183 public function getBasePath()
185 if ($this->basePath === null) {
186 $this->setBasePath($this->detectBasePath());
189 return $this->basePath;
193 * Provide an alternate Parameter Container implementation for server parameters in this object,
194 * (this is NOT the primary API for value setting, for that see getServer())
196 * @param ParametersInterface $server
197 * @return Request
199 public function setServer(ParametersInterface $server)
201 $this->serverParams = $server;
203 // This seems to be the only way to get the Authorization header on Apache
204 if (function_exists('apache_request_headers')) {
205 $apacheRequestHeaders = apache_request_headers();
206 if (!isset($this->serverParams['HTTP_AUTHORIZATION'])) {
207 if (isset($apacheRequestHeaders['Authorization'])) {
208 $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['Authorization']);
209 } elseif (isset($apacheRequestHeaders['authorization'])) {
210 $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['authorization']);
215 // set headers
216 $headers = array();
218 foreach ($server as $key => $value) {
219 if ($value && strpos($key, 'HTTP_') === 0) {
220 if (strpos($key, 'HTTP_COOKIE') === 0) {
221 // Cookies are handled using the $_COOKIE superglobal
222 continue;
224 $name = strtr(substr($key, 5), '_', ' ');
225 $name = strtr(ucwords(strtolower($name)), ' ', '-');
226 } elseif ($value && strpos($key, 'CONTENT_') === 0) {
227 $name = substr($key, 8); // Content-
228 $name = 'Content-' . (($name == 'MD5') ? $name : ucfirst(strtolower($name)));
229 } else {
230 continue;
233 $headers[$name] = $value;
236 $this->getHeaders()->addHeaders($headers);
238 // set method
239 if (isset($this->serverParams['REQUEST_METHOD'])) {
240 $this->setMethod($this->serverParams['REQUEST_METHOD']);
243 // set HTTP version
244 if (isset($this->serverParams['SERVER_PROTOCOL'])
245 && strpos($this->serverParams['SERVER_PROTOCOL'], self::VERSION_10) !== false
247 $this->setVersion(self::VERSION_10);
250 // set URI
251 $uri = new HttpUri();
253 // URI scheme
254 $scheme = (!empty($this->serverParams['HTTPS'])
255 && $this->serverParams['HTTPS'] !== 'off') ? 'https' : 'http';
256 $uri->setScheme($scheme);
258 // URI host & port
259 $host = null;
260 $port = null;
262 // Set the host
263 if ($this->getHeaders()->get('host')) {
264 $host = $this->getHeaders()->get('host')->getFieldValue();
266 // works for regname, IPv4 & IPv6
267 if (preg_match('|\:(\d+)$|', $host, $matches)) {
268 $host = substr($host, 0, -1 * (strlen($matches[1]) + 1));
269 $port = (int) $matches[1];
272 // set up a validator that check if the hostname is legal (not spoofed)
273 $hostnameValidator = new HostnameValidator(array(
274 'allow' => HostnameValidator::ALLOW_ALL,
275 'useIdnCheck' => false,
276 'useTldCheck' => false,
278 // If invalid. Reset the host & port
279 if (!$hostnameValidator->isValid($host)) {
280 $host = null;
281 $port = null;
285 if (!$host && isset($this->serverParams['SERVER_NAME'])) {
286 $host = $this->serverParams['SERVER_NAME'];
287 if (isset($this->serverParams['SERVER_PORT'])) {
288 $port = (int) $this->serverParams['SERVER_PORT'];
290 // Check for missinterpreted IPv6-Address
291 // Reported at least for Safari on Windows
292 if (isset($this->serverParams['SERVER_ADDR']) && preg_match('/^\[[0-9a-fA-F\:]+\]$/', $host)) {
293 $host = '[' . $this->serverParams['SERVER_ADDR'] . ']';
294 if ($port . ']' == substr($host, strrpos($host, ':')+1)) {
295 // The last digit of the IPv6-Address has been taken as port
296 // Unset the port so the default port can be used
297 $port = null;
301 $uri->setHost($host);
302 $uri->setPort($port);
304 // URI path
305 $requestUri = $this->getRequestUri();
306 if (($qpos = strpos($requestUri, '?')) !== false) {
307 $requestUri = substr($requestUri, 0, $qpos);
310 $uri->setPath($requestUri);
312 // URI query
313 if (isset($this->serverParams['QUERY_STRING'])) {
314 $uri->setQuery($this->serverParams['QUERY_STRING']);
317 $this->setUri($uri);
319 return $this;
323 * Return the parameter container responsible for server parameters or a single parameter value.
325 * @param string|null $name Parameter name to retrieve, or null to get the whole container.
326 * @param mixed|null $default Default value to use when the parameter is missing.
327 * @see http://www.faqs.org/rfcs/rfc3875.html
328 * @return \Zend\Stdlib\ParametersInterface|mixed
330 public function getServer($name = null, $default = null)
332 if ($this->serverParams === null) {
333 $this->serverParams = new Parameters();
336 if ($name === null) {
337 return $this->serverParams;
340 return $this->serverParams->get($name, $default);
344 * Provide an alternate Parameter Container implementation for env parameters in this object,
345 * (this is NOT the primary API for value setting, for that see env())
347 * @param ParametersInterface $env
348 * @return Request
350 public function setEnv(ParametersInterface $env)
352 $this->envParams = $env;
353 return $this;
357 * Return the parameter container responsible for env parameters or a single parameter value.
359 * @param string|null $name Parameter name to retrieve, or null to get the whole container.
360 * @param mixed|null $default Default value to use when the parameter is missing. * @return \Zend\Stdlib\ParametersInterface
361 * @return \Zend\Stdlib\ParametersInterface|mixed
363 public function getEnv($name = null, $default = null)
365 if ($this->envParams === null) {
366 $this->envParams = new Parameters();
369 if ($name === null) {
370 return $this->envParams;
373 return $this->envParams->get($name, $default);
377 * Convert PHP superglobal $_FILES into more sane parameter=value structure
378 * This handles form file input with brackets (name=files[])
380 * @return array
382 protected function mapPhpFiles()
384 $files = array();
385 foreach ($_FILES as $fileName => $fileParams) {
386 $files[$fileName] = array();
387 foreach ($fileParams as $param => $data) {
388 if (!is_array($data)) {
389 $files[$fileName][$param] = $data;
390 } else {
391 foreach ($data as $i => $v) {
392 $this->mapPhpFileParam($files[$fileName], $param, $i, $v);
398 return $files;
402 * @param array $array
403 * @param string $paramName
404 * @param int|string $index
405 * @param string|array $value
407 protected function mapPhpFileParam(&$array, $paramName, $index, $value)
409 if (!is_array($value)) {
410 $array[$index][$paramName] = $value;
411 } else {
412 foreach ($value as $i => $v) {
413 $this->mapPhpFileParam($array[$index], $paramName, $i, $v);
419 * Detect the base URI for the request
421 * Looks at a variety of criteria in order to attempt to autodetect a base
422 * URI, including rewrite URIs, proxy URIs, etc.
424 * @return string
426 protected function detectRequestUri()
428 $requestUri = null;
429 $server = $this->getServer();
431 // Check this first so IIS will catch.
432 $httpXRewriteUrl = $server->get('HTTP_X_REWRITE_URL');
433 if ($httpXRewriteUrl !== null) {
434 $requestUri = $httpXRewriteUrl;
437 // Check for IIS 7.0 or later with ISAPI_Rewrite
438 $httpXOriginalUrl = $server->get('HTTP_X_ORIGINAL_URL');
439 if ($httpXOriginalUrl !== null) {
440 $requestUri = $httpXOriginalUrl;
443 // IIS7 with URL Rewrite: make sure we get the unencoded url
444 // (double slash problem).
445 $iisUrlRewritten = $server->get('IIS_WasUrlRewritten');
446 $unencodedUrl = $server->get('UNENCODED_URL', '');
447 if ('1' == $iisUrlRewritten && '' !== $unencodedUrl) {
448 return $unencodedUrl;
451 // HTTP proxy requests setup request URI with scheme and host [and port]
452 // + the URL path, only use URL path.
453 if (!$httpXRewriteUrl) {
454 $requestUri = $server->get('REQUEST_URI');
457 if ($requestUri !== null) {
458 return preg_replace('#^[^/:]+://[^/]+#', '', $requestUri);
461 // IIS 5.0, PHP as CGI.
462 $origPathInfo = $server->get('ORIG_PATH_INFO');
463 if ($origPathInfo !== null) {
464 $queryString = $server->get('QUERY_STRING', '');
465 if ($queryString !== '') {
466 $origPathInfo .= '?' . $queryString;
468 return $origPathInfo;
471 return '/';
475 * Auto-detect the base path from the request environment
477 * Uses a variety of criteria in order to detect the base URL of the request
478 * (i.e., anything additional to the document root).
481 * @return string
483 protected function detectBaseUrl()
485 $baseUrl = '';
486 $filename = $this->getServer()->get('SCRIPT_FILENAME', '');
487 $scriptName = $this->getServer()->get('SCRIPT_NAME');
488 $phpSelf = $this->getServer()->get('PHP_SELF');
489 $origScriptName = $this->getServer()->get('ORIG_SCRIPT_NAME');
491 if ($scriptName !== null && basename($scriptName) === $filename) {
492 $baseUrl = $scriptName;
493 } elseif ($phpSelf !== null && basename($phpSelf) === $filename) {
494 $baseUrl = $phpSelf;
495 } elseif ($origScriptName !== null && basename($origScriptName) === $filename) {
496 // 1and1 shared hosting compatibility.
497 $baseUrl = $origScriptName;
498 } else {
499 // Backtrack up the SCRIPT_FILENAME to find the portion
500 // matching PHP_SELF.
502 $baseUrl = '/';
503 $basename = basename($filename);
504 if ($basename) {
505 $path = ($phpSelf ? trim($phpSelf, '/') : '');
506 $baseUrl .= substr($path, 0, strpos($path, $basename)) . $basename;
510 // Does the base URL have anything in common with the request URI?
511 $requestUri = $this->getRequestUri();
513 // Full base URL matches.
514 if (0 === strpos($requestUri, $baseUrl)) {
515 return $baseUrl;
518 // Directory portion of base path matches.
519 $baseDir = str_replace('\\', '/', dirname($baseUrl));
520 if (0 === strpos($requestUri, $baseDir)) {
521 return $baseDir;
524 $truncatedRequestUri = $requestUri;
526 if (false !== ($pos = strpos($requestUri, '?'))) {
527 $truncatedRequestUri = substr($requestUri, 0, $pos);
530 $basename = basename($baseUrl);
532 // No match whatsoever
533 if (empty($basename) || false === strpos($truncatedRequestUri, $basename)) {
534 return '';
537 // If using mod_rewrite or ISAPI_Rewrite strip the script filename
538 // out of the base path. $pos !== 0 makes sure it is not matching a
539 // value from PATH_INFO or QUERY_STRING.
540 if (strlen($requestUri) >= strlen($baseUrl)
541 && (false !== ($pos = strpos($requestUri, $baseUrl)) && $pos !== 0)
543 $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
546 return $baseUrl;
550 * Autodetect the base path of the request
552 * Uses several criteria to determine the base path of the request.
554 * @return string
556 protected function detectBasePath()
558 $filename = basename($this->getServer()->get('SCRIPT_FILENAME', ''));
559 $baseUrl = $this->getBaseUrl();
561 // Empty base url detected
562 if ($baseUrl === '') {
563 return '';
566 // basename() matches the script filename; return the directory
567 if (basename($baseUrl) === $filename) {
568 return str_replace('\\', '/', dirname($baseUrl));
571 // Base path is identical to base URL
572 return $baseUrl;