<?php
/**
 * Base url getter that return the base url for a whole application
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Class that returns the base url for the current request.
 *
 * @package koubel MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class MKLib_AppBaseUrl
{
    /**
     * simple cache for the base url
     * @var string
     */
    private static $_baseUrl;

    /**
     * A host URI part for the application.
     * e.g. www.example.com
     * @var string
     */
    private static $_hostPart;

    /**
     * Sets host part e.g. www.example.com
     * @param string $hostName
     */
    public static function setHost($hostName)
    {
        self::$_hostPart = $hostName;
    }

    /**
     * Gets URI - http://{@link $_hostPart}
     * @throws Exception when $_hostPart is empty.
     */
    public static function getUri()
    {
        if (!self::$_hostPart) throw new Exception("host URI part isn't set");
        return "http://" . self::$_hostPart;
    }

    /**
     * Gets domain for a cookie, e.g. ".example.com".
     * @param int $domainPartCount count of parts in the returned domain. If we have
     * the host string "www.test.domain.com" and count is 2, only ".domain.com"
     * will be returned
     * @return string
     */
    public static function getCookieDomain($domainPartCount = 2)
    {
        $parts = explode(".", self::$_hostPart);
        if (count($parts) < $domainPartCount) {
            $finalDomain = "." . self::$_hostPart;
        } else {
            $maxIndex = count($parts);
            $minIndex = $maxIndex - $domainPartCount;
            $domainPartStr = "";
            for ($i = $minIndex;$i < $maxIndex;$i++) $domainPartStr.= "." . $parts[$i];
            $finalDomain = $domainPartStr;
        }

        //fix see - http://cz2.php.net/manual/en/function.setcookie.php#74231
        //domain may not be localhost
        if (strpos($finalDomain, '.') == false) $finalDomain = false;
        return $finalDomain;
    }

    /**
     * Get the base url for the current request.
     * @param $withHostPart if is true, return whole URI, e.g.
     * http://www.example.com/baseUrl, if is false, return base URL only e.g. /baseUrl
     * @return string
     * @throws Exception when $_hostPart is empty and $withHostPart is true, which bubbles
     * from {@link $_getUri()}.
     */
    public static function getBaseUrl($withHostPart = false)
    {
        if (is_null(self::$_baseUrl)) {
            self::$_baseUrl = Zend_Controller_Front::getInstance()->getRequest()->getBaseUrl();
        }

        if ($withHostPart) {
            if (empty(self::$_hostPart)) throw new Exception("host URI part isn't set");
            return self::getUri() . self::$_baseUrl;
        } else {
            return self::$_baseUrl;
        }
    }
}
