web/lib/Zend/View/Helper/BaseUrl.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  * @version    $Id: BaseUrl.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /** @see Zend_View_Helper_Abstract */
       
    24 require_once 'Zend/View/Helper/Abstract.php';
       
    25 
       
    26 /**
       
    27  * Helper for retrieving the BaseUrl
       
    28  *
       
    29  * @package    Zend_View
       
    30  * @subpackage Helper
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  */
       
    34 class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract
       
    35 {
       
    36     /**
       
    37      * BaseUrl
       
    38      *
       
    39      * @var string
       
    40      */
       
    41     protected $_baseUrl;
       
    42 
       
    43     /**
       
    44      * Returns site's base url, or file with base url prepended
       
    45      *
       
    46      * $file is appended to the base url for simplicity
       
    47      *
       
    48      * @param  string|null $file
       
    49      * @return string
       
    50      */
       
    51     public function baseUrl($file = null)
       
    52     {
       
    53         // Get baseUrl
       
    54         $baseUrl = $this->getBaseUrl();
       
    55 
       
    56         // Remove trailing slashes
       
    57         if (null !== $file) {
       
    58             $file = '/' . ltrim($file, '/\\');
       
    59         }
       
    60 
       
    61         return $baseUrl . $file;
       
    62     }
       
    63 
       
    64     /**
       
    65      * Set BaseUrl
       
    66      *
       
    67      * @param  string $base
       
    68      * @return Zend_View_Helper_BaseUrl
       
    69      */
       
    70     public function setBaseUrl($base)
       
    71     {
       
    72         $this->_baseUrl = rtrim($base, '/\\');
       
    73         return $this;
       
    74     }
       
    75 
       
    76     /**
       
    77      * Get BaseUrl
       
    78      *
       
    79      * @return string
       
    80      */
       
    81     public function getBaseUrl()
       
    82     {
       
    83         if ($this->_baseUrl === null) {
       
    84             /** @see Zend_Controller_Front */
       
    85             require_once 'Zend/Controller/Front.php';
       
    86             $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
       
    87 
       
    88             // Remove scriptname, eg. index.php from baseUrl
       
    89             $baseUrl = $this->_removeScriptName($baseUrl);
       
    90 
       
    91             $this->setBaseUrl($baseUrl);
       
    92         }
       
    93 
       
    94         return $this->_baseUrl;
       
    95     }
       
    96 
       
    97     /**
       
    98      * Remove Script filename from baseurl
       
    99      *
       
   100      * @param  string $url
       
   101      * @return string
       
   102      */
       
   103     protected function _removeScriptName($url)
       
   104     {
       
   105         if (!isset($_SERVER['SCRIPT_NAME'])) {
       
   106             // We can't do much now can we? (Well, we could parse out by ".")
       
   107             return $url;
       
   108         }
       
   109 
       
   110         if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
       
   111             $url = substr($url, 0, $pos);
       
   112         }
       
   113 
       
   114         return $url;
       
   115     }
       
   116 }