File indexing completed on 2024-06-16 05:30:34

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  * @version    $Id$
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** @see Zend_View_Helper_Abstract */
0024 // require_once 'Zend/View/Helper/Abstract.php';
0025 
0026 /**
0027  * Helper for retrieving the BaseUrl
0028  *
0029  * @package    Zend_View
0030  * @subpackage Helper
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract
0035 {
0036     /**
0037      * BaseUrl
0038      *
0039      * @var string
0040      */
0041     protected $_baseUrl;
0042 
0043     /**
0044      * Returns site's base url, or file with base url prepended
0045      *
0046      * $file is appended to the base url for simplicity
0047      *
0048      * @param  string|null $file
0049      * @return string
0050      */
0051     public function baseUrl($file = null)
0052     {
0053         // Get baseUrl
0054         $baseUrl = $this->getBaseUrl();
0055 
0056         // Remove trailing slashes
0057         if (null !== $file) {
0058             $file = '/' . ltrim($file, '/\\');
0059         }
0060 
0061         return $baseUrl . $file;
0062     }
0063 
0064     /**
0065      * Set BaseUrl
0066      *
0067      * @param  string $base
0068      * @return Zend_View_Helper_BaseUrl
0069      */
0070     public function setBaseUrl($base)
0071     {
0072         $this->_baseUrl = rtrim($base, '/\\');
0073         return $this;
0074     }
0075 
0076     /**
0077      * Get BaseUrl
0078      *
0079      * @return string
0080      */
0081     public function getBaseUrl()
0082     {
0083         if ($this->_baseUrl === null) {
0084             /** @see Zend_Controller_Front */
0085             // require_once 'Zend/Controller/Front.php';
0086             $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
0087 
0088             // Remove scriptname, eg. index.php from baseUrl
0089             $baseUrl = $this->_removeScriptName($baseUrl);
0090 
0091             $this->setBaseUrl($baseUrl);
0092         }
0093 
0094         return $this->_baseUrl;
0095     }
0096 
0097     /**
0098      * Remove Script filename from baseurl
0099      *
0100      * @param  string $url
0101      * @return string
0102      */
0103     protected function _removeScriptName($url)
0104     {
0105         if (!isset($_SERVER['SCRIPT_NAME'])) {
0106             // We can't do much now can we? (Well, we could parse out by ".")
0107             return $url;
0108         }
0109 
0110         if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
0111             $url = substr($url, 0, $pos);
0112         }
0113 
0114         return $url;
0115     }
0116 }