示例#1
0
/**
 * Wrapper for @see Router::reverse(). Additional params are _fullurl and _https, for links with https-protocol and
 * absolute urls. Define the route name with _name and everything else is used as arguments, you may also specify all
 * arguments at once using the 'arguments' param.
 *
 * @package FeM\sPof\template\smartyPlugins
 * @author dangerground
 * @since 1.0
 *
 * @api
 *
 * @throws FeM\sPof\exception\SmartyTemplateException
 *
 * @param array $params
 * @param Smarty $smarty
 *
 * @return string
 */
function smarty_function_route($params, &$smarty)
{
    // get array elements as single params
    if (isset($params['arguments']) && is_array($params['arguments'])) {
        $params = array_merge($params['arguments'], $params);
        unset($params['arguments']);
    }
    $arguments = $params;
    unset($arguments['_name']);
    $fullurl = isset($arguments['_fullurl']) ? $arguments['_fullurl'] : false;
    unset($arguments['_fullurl']);
    $https = isset($arguments['_https']) || isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'on' ? $arguments['_https'] : true;
    unset($arguments['_https']);
    try {
        $server = FeM\sPof\Config::get('server');
        $basedir = 'https://' . $_SERVER['SERVER_NAME'] . $server['path'];
        return ($fullurl ? $https ? $basedir : preg_replace('#^https#', 'http', $basedir) : '') . \FeM\sPof\Router::reverse($params['_name'], $arguments);
    } catch (\InvalidArgumentException $e) {
        throw new \FeM\sPof\exception\SmartyTemplateException(__FUNCTION__ . ': ' . $e->getMessage(), $smarty->getTemplateDir()[0] . $smarty->template_resource, $e);
    }
}
示例#2
0
/**
 * Create a new route based on the currently visited site, by disassembling the
 * url. Throwing all optional params away (these with 'name:value') and append
 * the params as new optional params.
 *
 * @package FeM\sPof\template\smartyPlugins
 * @author dangerground
 * @author pegro
 *
 * @api
 *
 * @param array $params
 * @param Smarty $smarty (reference)
 *
 * @return string
 *
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
function smarty_function_currenturl($params, &$smarty)
{
    $rawUrl = false;
    $server = FeM\sPof\Config::get('server');
    // get pattern route
    if (isset($_SERVER['REQUEST_URI'])) {
        if ($server['path'] === '/') {
            $route = $_SERVER['REQUEST_URI'];
        } else {
            $route = str_replace($server['path'], '', $_SERVER['REQUEST_URI']);
        }
        if (strpos($_SERVER['REQUEST_URI'], '?') > 0) {
            $rawUrl = true;
        }
        // drop optional params
        foreach ($params as $key => $param) {
            if ($param === null) {
                continue;
            }
            $route = preg_replace('/' . ($rawUrl ? '&' . $key . '=' : '\\/?\\/?[a-z0-9_]+:') . '[a-z0-9_]+/i', '', $route);
        }
    }
    // make default route based on basedir
    if (empty($route)) {
        $route = '/';
    }
    // get a list of valid new optional params
    $new = [$route];
    foreach ($params as $key => $param) {
        if ($param === null) {
            continue;
        }
        $new[] = $key . ($rawUrl ? '=' : ':') . $param;
    }
    // return route including new optional params
    return '//' . $_SERVER['SERVER_NAME'] . $server['path'] . ltrim(implode($rawUrl ? '&' : '/', $new), '/');
}