Exemplo n.º 1
0
/**
 * Return the absolute URL path appended the query string if necessary
 *
 * Alias `_url()`
 *
 * @param string $path Routing path such as "foo/bar"; Named route such as "fool_bar"; NULL for the current path
 * @param array $queryStr Query string as
 *
 *     array(
 *       $value1, // no key here
 *       'key1' => $value2,
 *       'key3' => $value3 or array($value3, $value4)
 *     )
 *
 * @param string $lang Languague code to be prepended to $path such as "en/foo/bar".
 *   It will be useful for site language switch redirect
 *
 * @return string
 *
 */
function route_url($path = null, $queryStr = array(), $lang = '')
{
    global $lc_cleanURL;
    global $lc_translationEnabled;
    global $lc_sites;
    global $lc_langInURI;
    $forceExcludeLangInURL = $lang === false ? true : false;
    if (stripos($path, 'http') === 0) {
        return $path;
    }
    $customRoute = Router::getPathByName($path);
    if ($customRoute !== null) {
        $path = $customRoute ? $customRoute : 'home';
        if ($queryStr && is_array($queryStr) && count($queryStr)) {
            foreach ($queryStr as $key => $value) {
                $path = str_replace('{' . $key . '}', urlencode($value), $path);
            }
        }
        $queryStr = array();
        // clean query strings to not be processed later
    }
    if (strtolower($path) == 'home') {
        if (isset($GLOBALS['lc_homeRouting']) && $GLOBALS['lc_homeRouting']) {
            $path = $GLOBALS['lc_homeRouting'];
        }
    }
    if ($path && is_string($path)) {
        $path = rtrim($path, '/');
    } else {
        $r = _isRewriteRule() ? REQUEST_URI : route_path();
        $path = route_updateQueryStr($r, $queryStr);
    }
    $q = '';
    if ($queryStr && is_array($queryStr) && count($queryStr)) {
        foreach ($queryStr as $key => $value) {
            if (is_array($value)) {
                $v = array_map('urlencode', $value);
                $value = implode('/', $v);
            } else {
                $value = urlencode($value);
            }
            if (is_numeric($key)) {
                if ($lc_cleanURL) {
                    $q .= '/' . $value;
                } else {
                    $q .= '&' . $value;
                }
            } else {
                if ($lc_cleanURL) {
                    $q .= '/-' . $key . '/' . $value;
                } else {
                    $q .= '&' . $key . '=' . $value;
                }
            }
        }
    }
    if (is_array($lc_sites) && array_key_exists(LC_NAMESPACE, $lc_sites)) {
        $regex = '/\\b^(' . $lc_sites[LC_NAMESPACE] . ') {1}\\b/i';
        $path = preg_replace($regex, LC_NAMESPACE, $path);
    }
    # If URI contains the language code, force to include it in the URI
    if (is_null($lc_langInURI)) {
        $lc_langInURI = _getLangInURI();
    }
    if (empty($lang) && $lc_langInURI) {
        $lang = $lc_langInURI;
    }
    $url = WEB_ROOT;
    if ($lang && $lc_translationEnabled && !$forceExcludeLangInURL) {
        if ($lc_cleanURL) {
            $url .= $lang . '/';
        } else {
            $q .= '&lang=' . $lang;
        }
    }
    if (strtolower($path) == 'home') {
        $path = '';
    }
    if ($lc_cleanURL) {
        $url .= $path . $q;
    } else {
        $url .= $path . '?' . ltrim($q, '&');
        $url = trim($url, '?');
    }
    $url = preg_replace('/(\\s) {1,}/', '+', $url);
    # replace the space with "+"
    $url = rtrim($url, '/');
    return $url;
}
/**
 * The more realistic function to get the current routing path on the address bar regardless of RewriteRule behind
 * For example,
 *
 * - example.com/foo/bar would return foo/bar
 * - example.com/en/foo/bar would also return foo/bar
 * - example.com/1/this-is-slug would return 1/this-is-slug
 *
 * @return string The route path starting from the site root
 */
function _rr()
{
    return _isRewriteRule() ? REQUEST_URI : _r();
}