summaryrefslogtreecommitdiff
path: root/vendor/OAuth/Common/Http/Uri/UriFactory.php
diff options
context:
space:
mode:
authorFrédéric Guillot <fguillot@users.noreply.github.com>2014-05-03 22:24:03 -0400
committerFrédéric Guillot <fguillot@users.noreply.github.com>2014-05-03 22:24:03 -0400
commit560a12f0bd6347a335f8ed5201d6d9562d03d4bc (patch)
tree00510d25c1cf5e747573543fa88d44ef003b1c9a /vendor/OAuth/Common/Http/Uri/UriFactory.php
parent9531e439cd99fb7dbcfb039f422f1d1ba414ec30 (diff)
Add Google authentication
Diffstat (limited to 'vendor/OAuth/Common/Http/Uri/UriFactory.php')
-rwxr-xr-xvendor/OAuth/Common/Http/Uri/UriFactory.php168
1 files changed, 168 insertions, 0 deletions
diff --git a/vendor/OAuth/Common/Http/Uri/UriFactory.php b/vendor/OAuth/Common/Http/Uri/UriFactory.php
new file mode 100755
index 00000000..127aa203
--- /dev/null
+++ b/vendor/OAuth/Common/Http/Uri/UriFactory.php
@@ -0,0 +1,168 @@
+<?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';
+ }
+ }
+}