| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace OAuth\Common\Http\Uri;
- use RuntimeException;
- /**
- * Factory class for uniform resource indicators
- */
- class UriFactory implements UriFactoryInterface
- {
- /**
- * Factory method to build a URI from a super-global $_SERVER array.
- *
- * @param array $_server
- *
- * @return UriInterface
- */
- public function createFromSuperGlobalArray(array $_server)
- {
- if ($uri = $this->attemptProxyStyleParse($_server)) {
- return $uri;
- }
- $scheme = $this->detectScheme($_server);
- $host = $this->detectHost($_server);
- $port = $this->detectPort($_server);
- $path = $this->detectPath($_server);
- $query = $this->detectQuery($_server);
- return $this->createFromParts($scheme, '', $host, $port, $path, $query);
- }
- /**
- * @param string $absoluteUri
- *
- * @return UriInterface
- */
- public function createFromAbsolute($absoluteUri)
- {
- return new Uri($absoluteUri);
- }
- /**
- * Factory method to build a URI from parts
- *
- * @param string $scheme
- * @param string $userInfo
- * @param string $host
- * @param string $port
- * @param string $path
- * @param string $query
- * @param string $fragment
- *
- * @return UriInterface
- */
- public function createFromParts($scheme, $userInfo, $host, $port, $path = '', $query = '', $fragment = '')
- {
- $uri = new Uri();
- $uri->setScheme($scheme);
- $uri->setUserInfo($userInfo);
- $uri->setHost($host);
- $uri->setPort($port);
- $uri->setPath($path);
- $uri->setQuery($query);
- $uri->setFragment($fragment);
- return $uri;
- }
- /**
- * @param array $_server
- *
- * @return UriInterface|null
- */
- private function attemptProxyStyleParse($_server)
- {
- // If the raw HTTP request message arrives with a proxy-style absolute URI in the
- // initial request line, the absolute URI is stored in $_SERVER['REQUEST_URI'] and
- // we only need to parse that.
- if (isset($_server['REQUEST_URI']) && parse_url($_server['REQUEST_URI'], PHP_URL_SCHEME)) {
- return new Uri($_server['REQUEST_URI']);
- }
- return null;
- }
- /**
- * @param array $_server
- *
- * @return string
- *
- * @throws RuntimeException
- */
- private function detectPath($_server)
- {
- if (isset($_server['REQUEST_URI'])) {
- $uri = $_server['REQUEST_URI'];
- } elseif (isset($_server['REDIRECT_URL'])) {
- $uri = $_server['REDIRECT_URL'];
- } else {
- throw new RuntimeException('Could not detect URI path from superglobal');
- }
- $queryStr = strpos($uri, '?');
- if ($queryStr !== false) {
- $uri = substr($uri, 0, $queryStr);
- }
- return $uri;
- }
- /**
- * @param array $_server
- *
- * @return string
- */
- private function detectHost(array $_server)
- {
- $host = isset($_server['HTTP_HOST']) ? $_server['HTTP_HOST'] : '';
- if (strstr($host, ':')) {
- $host = parse_url($host, PHP_URL_HOST);
- }
- return $host;
- }
- /**
- * @param array $_server
- *
- * @return string
- */
- private function detectPort(array $_server)
- {
- return isset($_server['SERVER_PORT']) ? $_server['SERVER_PORT'] : 80;
- }
- /**
- * @param array $_server
- *
- * @return string
- */
- private function detectQuery(array $_server)
- {
- return isset($_server['QUERY_STRING']) ? $_server['QUERY_STRING'] : '';
- }
- /**
- * Determine URI scheme component from superglobal array
- *
- * When using ISAPI with IIS, the value will be "off" if the request was
- * not made through the HTTPS protocol. As a result, we filter the
- * value to a bool.
- *
- * @param array $_server A super-global $_SERVER array
- *
- * @return string Returns http or https depending on the URI scheme
- */
- private function detectScheme(array $_server)
- {
- if (isset($_server['HTTPS']) && filter_var($_server['HTTPS'], FILTER_VALIDATE_BOOLEAN)) {
- return 'https';
- } else {
- return 'http';
- }
- }
- }
|