web/lib/Zend/View/Helper/BaseUrl.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/Zend/View/Helper/BaseUrl.php	Fri Mar 11 15:05:35 2011 +0100
@@ -0,0 +1,116 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_View
+ * @subpackage Helper
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @version    $Id: BaseUrl.php 20096 2010-01-06 02:05:09Z bkarwin $
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/** @see Zend_View_Helper_Abstract */
+require_once 'Zend/View/Helper/Abstract.php';
+
+/**
+ * Helper for retrieving the BaseUrl
+ *
+ * @package    Zend_View
+ * @subpackage Helper
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract
+{
+    /**
+     * BaseUrl
+     *
+     * @var string
+     */
+    protected $_baseUrl;
+
+    /**
+     * Returns site's base url, or file with base url prepended
+     *
+     * $file is appended to the base url for simplicity
+     *
+     * @param  string|null $file
+     * @return string
+     */
+    public function baseUrl($file = null)
+    {
+        // Get baseUrl
+        $baseUrl = $this->getBaseUrl();
+
+        // Remove trailing slashes
+        if (null !== $file) {
+            $file = '/' . ltrim($file, '/\\');
+        }
+
+        return $baseUrl . $file;
+    }
+
+    /**
+     * Set BaseUrl
+     *
+     * @param  string $base
+     * @return Zend_View_Helper_BaseUrl
+     */
+    public function setBaseUrl($base)
+    {
+        $this->_baseUrl = rtrim($base, '/\\');
+        return $this;
+    }
+
+    /**
+     * Get BaseUrl
+     *
+     * @return string
+     */
+    public function getBaseUrl()
+    {
+        if ($this->_baseUrl === null) {
+            /** @see Zend_Controller_Front */
+            require_once 'Zend/Controller/Front.php';
+            $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
+
+            // Remove scriptname, eg. index.php from baseUrl
+            $baseUrl = $this->_removeScriptName($baseUrl);
+
+            $this->setBaseUrl($baseUrl);
+        }
+
+        return $this->_baseUrl;
+    }
+
+    /**
+     * Remove Script filename from baseurl
+     *
+     * @param  string $url
+     * @return string
+     */
+    protected function _removeScriptName($url)
+    {
+        if (!isset($_SERVER['SCRIPT_NAME'])) {
+            // We can't do much now can we? (Well, we could parse out by ".")
+            return $url;
+        }
+
+        if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
+            $url = substr($url, 0, $pos);
+        }
+
+        return $url;
+    }
+}