Source of file ServerUrl.php
Size: 7,554 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/View/Helper/ServerUrl.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\View\Helper; /** * Helper for returning the current server URL (optionally with request URI) */ class ServerUrl extends AbstractHelper { /** * Host (including port) * * @var string */ protected $host; /** * Port * * @var int */ protected $port; /** * Scheme * * @var string */ protected $scheme; /** * Whether or not to query proxy servers for address * * @var bool */ protected $useProxy = false; /** * View helper entry point: * Returns the current host's URL like http://site.com * * @param string|bool $requestUri [optional] if true, the request URI * found in $_SERVER will be appended * as a path. If a string is given, it * will be appended as a path. Default * is to not append any path. * @return string */ public function __invoke($requestUri = null) { if ($requestUri === true) { $path = $_SERVER['REQUEST_URI']; } elseif (is_string($requestUri)) { $path = $requestUri; } else { $path = ''; } return $this->getScheme() . '://' . $this->getHost() . $path; } /** * Detect the host based on headers * * @return void */ protected function detectHost() { if ($this->setHostFromProxy()) { return; } if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) { // Detect if the port is set in SERVER_PORT and included in HTTP_HOST if (isset($_SERVER['SERVER_PORT'])) { $portStr = ':' . $_SERVER['SERVER_PORT']; if (substr($_SERVER['HTTP_HOST'], 0-strlen($portStr), strlen($portStr)) == $portStr) { $this->setHost(substr($_SERVER['HTTP_HOST'], 0, 0-strlen($portStr))); return; } } $this->setHost($_SERVER['HTTP_HOST']); return; } if (!isset($_SERVER['SERVER_NAME']) || !isset($_SERVER['SERVER_PORT'])) { return; } $name = $_SERVER['SERVER_NAME']; $this->setHost($name); } /** * Detect the port * * @return null */ protected function detectPort() { if ($this->setPortFromProxy()) { return; } if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT']) { $this->setPort($_SERVER['SERVER_PORT']); return; } } /** * Detect the scheme * * @return null */ protected function detectScheme() { if ($this->setSchemeFromProxy()) { return; } switch (true) { case (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)): case (isset($_SERVER['HTTP_SCHEME']) && ($_SERVER['HTTP_SCHEME'] == 'https')): case (443 === $this->getPort()): $scheme = 'https'; break; default: $scheme = 'http'; break; } $this->setScheme($scheme); } /** * Detect if a proxy is in use, and, if so, set the host based on it * * @return bool */ protected function setHostFromProxy() { if (!$this->useProxy) { return false; } if (!isset($_SERVER['HTTP_X_FORWARDED_HOST']) || empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { return false; } $host = $_SERVER['HTTP_X_FORWARDED_HOST']; if (strpos($host, ',') !== false) { $hosts = explode(',', $host); $host = trim(array_pop($hosts)); } if (empty($host)) { return false; } $this->setHost($host); return true; } /** * Set port based on detected proxy headers * * @return bool */ protected function setPortFromProxy() { if (!$this->useProxy) { return false; } if (!isset($_SERVER['HTTP_X_FORWARDED_PORT']) || empty($_SERVER['HTTP_X_FORWARDED_PORT'])) { return false; } $port = $_SERVER['HTTP_X_FORWARDED_PORT']; $this->setPort($port); return true; } /** * Set the current scheme based on detected proxy headers * * @return bool */ protected function setSchemeFromProxy() { if (!$this->useProxy) { return false; } if (isset($_SERVER['SSL_HTTPS'])) { $sslHttps = strtolower($_SERVER['SSL_HTTPS']); if (in_array($sslHttps, array('on', 1))) { $this->setScheme('https'); return true; } } if (!isset($_SERVER['HTTP_X_FORWARDED_PROTO']) || empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) { return false; } $scheme = trim(strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])); if (empty($scheme)) { return false; } $this->setScheme($scheme); return true; } /** * Sets host * * @param string $host * @return ServerUrl */ public function setHost($host) { $port = $this->getPort(); $scheme = $this->getScheme(); if (($scheme == 'http' && (null === $port || $port == 80)) || ($scheme == 'https' && (null === $port || $port == 443)) ) { $this->host = $host; return $this; } $this->host = $host . ':' . $port; return $this; } /** * Returns host * * @return string */ public function getHost() { if (null === $this->host) { $this->detectHost(); } return $this->host; } /** * Set server port * * @param int $port * @return ServerUrl */ public function setPort($port) { $this->port = (int) $port; return $this; } /** * Retrieve the server port * * @return int|null */ public function getPort() { if (null === $this->port) { $this->detectPort(); } return $this->port; } /** * Sets scheme (typically http or https) * * @param string $scheme * @return ServerUrl */ public function setScheme($scheme) { $this->scheme = $scheme; return $this; } /** * Returns scheme (typically http or https) * * @return string */ public function getScheme() { if (null === $this->scheme) { $this->detectScheme(); } return $this->scheme; } /** * Set flag indicating whether or not to query proxy servers * * @param bool $useProxy * @return ServerUrl */ public function setUseProxy($useProxy = false) { $this->useProxy = (bool) $useProxy; return $this; } } |