File indexing completed on 2024-12-22 05:37:14

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_View
0017  * @subpackage Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * Helper for returning the current server URL (optionally with request URI)
0025  *
0026  * @category   Zend
0027  * @package    Zend_View
0028  * @subpackage Helper
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_View_Helper_ServerUrl
0033 {
0034     /**
0035      * Scheme
0036      *
0037      * @var string
0038      */
0039     protected $_scheme;
0040 
0041     /**
0042      * Host (including port)
0043      *
0044      * @var string
0045      */
0046     protected $_host;
0047 
0048     /**
0049      * Constructor
0050      *
0051      * @return void
0052      */
0053     public function __construct()
0054     {
0055         switch (true) {
0056             case (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)):
0057             case (isset($_SERVER['HTTP_SCHEME']) && ($_SERVER['HTTP_SCHEME'] == 'https')):
0058             case (isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] == 443)):
0059                 $scheme = 'https';
0060                 break;
0061             default:
0062             $scheme = 'http';
0063         }
0064         $this->setScheme($scheme);
0065 
0066         if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
0067             $this->setHost($_SERVER['HTTP_HOST']);
0068         } else if (isset($_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'])) {
0069             $name = $_SERVER['SERVER_NAME'];
0070             $port = $_SERVER['SERVER_PORT'];
0071 
0072             if (($scheme == 'http' && $port == 80) ||
0073                 ($scheme == 'https' && $port == 443)) {
0074                 $this->setHost($name);
0075             } else {
0076                 $this->setHost($name . ':' . $port);
0077             }
0078         }
0079     }
0080 
0081     /**
0082      * View helper entry point:
0083      * Returns the current host's URL like http://site.com
0084      *
0085      * @param  string|boolean $requestUri  [optional] if true, the request URI
0086      *                                     found in $_SERVER will be appended
0087      *                                     as a path. If a string is given, it
0088      *                                     will be appended as a path. Default
0089      *                                     is to not append any path.
0090      * @return string                      server url
0091      */
0092     public function serverUrl($requestUri = null)
0093     {
0094         if ($requestUri === true) {
0095             $path = $_SERVER['REQUEST_URI'];
0096         } else if (is_string($requestUri)) {
0097             $path = $requestUri;
0098         } else {
0099             $path = '';
0100         }
0101 
0102         return $this->getScheme() . '://' . $this->getHost() . $path;
0103     }
0104 
0105     /**
0106      * Returns host
0107      *
0108      * @return string  host
0109      */
0110     public function getHost()
0111     {
0112         return $this->_host;
0113     }
0114 
0115     /**
0116      * Sets host
0117      *
0118      * @param  string $host                new host
0119      * @return Zend_View_Helper_ServerUrl  fluent interface, returns self
0120      */
0121     public function setHost($host)
0122     {
0123         $this->_host = $host;
0124         return $this;
0125     }
0126 
0127     /**
0128      * Returns scheme (typically http or https)
0129      *
0130      * @return string  scheme (typically http or https)
0131      */
0132     public function getScheme()
0133     {
0134         return $this->_scheme;
0135     }
0136 
0137     /**
0138      * Sets scheme (typically http or https)
0139      *
0140      * @param  string $scheme              new scheme (typically http or https)
0141      * @return Zend_View_Helper_ServerUrl  fluent interface, returns self
0142      */
0143     public function setScheme($scheme)
0144     {
0145         $this->_scheme = $scheme;
0146         return $this;
0147     }
0148 }