web/lib/Zend/View/Helper/ServerUrl.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_View
       
    17  * @subpackage Helper
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: ServerUrl.php 23371 2010-11-18 20:49:55Z bittarman $
       
    21  */
       
    22 
       
    23 /**
       
    24  * Helper for returning the current server URL (optionally with request URI)
       
    25  *
       
    26  * @category   Zend
       
    27  * @package    Zend_View
       
    28  * @subpackage Helper
       
    29  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    31  */
       
    32 class Zend_View_Helper_ServerUrl
       
    33 {
       
    34     /**
       
    35      * Scheme
       
    36      *
       
    37      * @var string
       
    38      */
       
    39     protected $_scheme;
       
    40 
       
    41     /**
       
    42      * Host (including port)
       
    43      *
       
    44      * @var string
       
    45      */
       
    46     protected $_host;
       
    47 
       
    48     /**
       
    49      * Constructor
       
    50      *
       
    51      * @return void
       
    52      */
       
    53     public function __construct()
       
    54     {
       
    55         switch (true) {
       
    56             case (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)):
       
    57             case (isset($_SERVER['HTTP_SCHEME']) && ($_SERVER['HTTP_SCHEME'] == 'https')):
       
    58             case (isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] == 443)):
       
    59                 $scheme = 'https';
       
    60                 break;
       
    61             default:
       
    62             $scheme = 'http';   
       
    63         }
       
    64         $this->setScheme($scheme);
       
    65 
       
    66         if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
       
    67             $this->setHost($_SERVER['HTTP_HOST']);
       
    68         } else if (isset($_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'])) {
       
    69             $name = $_SERVER['SERVER_NAME'];
       
    70             $port = $_SERVER['SERVER_PORT'];
       
    71 
       
    72             if (($scheme == 'http' && $port == 80) ||
       
    73                 ($scheme == 'https' && $port == 443)) {
       
    74                 $this->setHost($name);
       
    75             } else {
       
    76                 $this->setHost($name . ':' . $port);
       
    77             }
       
    78         }
       
    79     }
       
    80 
       
    81     /**
       
    82      * View helper entry point:
       
    83      * Returns the current host's URL like http://site.com
       
    84      *
       
    85      * @param  string|boolean $requestUri  [optional] if true, the request URI
       
    86      *                                     found in $_SERVER will be appended
       
    87      *                                     as a path. If a string is given, it
       
    88      *                                     will be appended as a path. Default
       
    89      *                                     is to not append any path.
       
    90      * @return string                      server url
       
    91      */
       
    92     public function serverUrl($requestUri = null)
       
    93     {
       
    94         if ($requestUri === true) {
       
    95             $path = $_SERVER['REQUEST_URI'];
       
    96         } else if (is_string($requestUri)) {
       
    97             $path = $requestUri;
       
    98         } else {
       
    99             $path = '';
       
   100         }
       
   101 
       
   102         return $this->getScheme() . '://' . $this->getHost() . $path;
       
   103     }
       
   104 
       
   105     /**
       
   106      * Returns host
       
   107      *
       
   108      * @return string  host
       
   109      */
       
   110     public function getHost()
       
   111     {
       
   112         return $this->_host;
       
   113     }
       
   114 
       
   115     /**
       
   116      * Sets host
       
   117      *
       
   118      * @param  string $host                new host
       
   119      * @return Zend_View_Helper_ServerUrl  fluent interface, returns self
       
   120      */
       
   121     public function setHost($host)
       
   122     {
       
   123         $this->_host = $host;
       
   124         return $this;
       
   125     }
       
   126 
       
   127     /**
       
   128      * Returns scheme (typically http or https)
       
   129      *
       
   130      * @return string  scheme (typically http or https)
       
   131      */
       
   132     public function getScheme()
       
   133     {
       
   134         return $this->_scheme;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Sets scheme (typically http or https)
       
   139      *
       
   140      * @param  string $scheme              new scheme (typically http or https)
       
   141      * @return Zend_View_Helper_ServerUrl  fluent interface, returns self
       
   142      */
       
   143     public function setScheme($scheme)
       
   144     {
       
   145         $this->_scheme = $scheme;
       
   146         return $this;
       
   147     }
       
   148 }