Back to Uri class

Method base

public static string
base
(mixed $pathonly = false)
Returns the base URI for the request.
Parameters
  • bool $pathonly If false, prepend the scheme, host and port information. Default is false.
Returns
  • string The base URI string
Since
  • 1.7.0
Class: Uri
Project: Joomla

Method base - Source code

/**
 * Returns the base URI for the request.
 *
 * @param   boolean  $pathonly  If false, prepend the scheme, host and port information. Default is false.
 *
 * @return  string  The base URI string
 *
 * @since   1.7.0
 */
public static function base($pathonly = false)
{
    // Get the base request path.
    if (empty(static::$base)) {
        $config = Factory::getContainer()->get('config');
        $uri = static::getInstance();
        $live_site = $uri->isSsl() ? str_replace('http://', 'https://', $config->get('live_site', '')) : $config->get('live_site', '');
        if (trim($live_site) != '') {
            $uri = static::getInstance($live_site);
            static::$base['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
            static::$base['path'] = rtrim($uri->toString(array('path')), '/\\');
            if (\defined('JPATH_BASE') && \defined('JPATH_ADMINISTRATOR') && JPATH_BASE == JPATH_ADMINISTRATOR) {
                static::$base['path'] .= '/administrator';
            }
            if (\defined('JPATH_BASE') && \defined('JPATH_API') && JPATH_BASE == JPATH_API) {
                static::$base['path'] .= '/api';
            }
        } else {
            static::$base['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
            if (strpos(PHP_SAPI, 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($_SERVER['REQUEST_URI'])) {
                // PHP-CGI on Apache with "cgi.fix_pathinfo = 0"
                // We shouldn't have user-supplied PATH_INFO in PHP_SELF in this case
                // because PHP will not work with PATH_INFO at all.
                $script_name = $_SERVER['PHP_SELF'];
            } else {
                // Others
                $script_name = $_SERVER['SCRIPT_NAME'];
            }
            // Extra cleanup to remove invalid chars in the URL to prevent injections through broken server implementation
            $script_name = str_replace(array("'", '"', '<', '>'), array('%27', '%22', '%3C', '%3E'), $script_name);
            static::$base['path'] = rtrim(\dirname($script_name), '/\\');
        }
    }
    return $pathonly === false ? static::$base['prefix'] . static::$base['path'] . '/' : static::$base['path'];
}