Convert a URL parameter (e.g. ":id", ":id+") into a regular expression
@param  array  $m URL parameters
@return string    Regular expression for URL parameter
508736
protected function matchesCallback($m)
    {
        $this->paramNames[] = $m[1];
        if (isset($this->conditions[$m[1]])) {
            return '(?P<' . $m[1] . '>' . $this->conditions[$m[1]] . ')';
        }
        if (substr($m[0], -1) === '+') {
            $this->paramNamesPath[$m[1]] = 1;

            return '(?P<' . $m[1] . '>.+)';
        }

        return '(?P<' . $m[1] . '>[^/]+)';
    }
281321
private function formatUrl() {

		$jar 	= [];
		$query 	= $this -> url -> getQuery();

		if(null !== $query) {
			$jar[] = $query;
		}
		
		$query 	= '';

		if(count($this -> params) > 0 || count($jar) > 0) {

			if($this -> method === 'get') {
				
				foreach($this -> params as  $param) {

					$key  	= $param -> encode ? urlencode($param -> key) : $param -> key;
					$value 	= $param -> encode ? urlencode($param -> value) : $param -> value;

					$jar[] = sprintf('%s=%s', $key, $value);
				}
			}

			$query = '?' . implode('&', $jar);
		}

		return $this -> url -> generate(URLParser :: UNTIL_PATH) . $query;
	}
942263
protected function _parseIncludeCompileCallback($m) {
        $data = '<?php ';

        if (!preg_match('#^\$#', $m[1])) {
            $m[1] = '"' . $m[1] . '"';
        }

        if (isset($m[4])) //precised time cache
        {
            $data .= '$t = self::Template(' . $m[1] . ', "tplInclude_' . $m[4] . '_' . $this->_includeI . '_", "' . $m[4] . '"); ' . "\n";
        }
        else {
            $data .= '$t = self::Template(' . $m[1] . ', "tplInclude_' . $this->_includeI . '_", "0"); ' . "\n";
        }

        foreach ($this->_template->vars as $key => $value) {
            $data .= '$t->assign("' . $key . '", $' . $key . '); ' . "\n";
        }

        $data .= '$t->show(); ' . " ?>";

        return $data;
    }
827753
protected function doShortcodeTag($m)
    {
        // allow [[foo]] syntax for escaping a tag
        if ($m[1] == '[' && $m[6] == ']') {
            return substr($m[0], 1, -1);
        }

        $tag = $m[2];
        $attr = $this->parseAttributes($m[3]);

        $callback = $this->_shortcodes[$tag]['callback'];
        $config = $this->_shortcodes[$tag]['config'];

        $attr = ArrayHelper::merge($config, $attr);
        $content = isset($m[5]) ? $m[5] : null;

        $attr['content'] = $content;
        $this->addToPool($tag);
        return $m[1] . call_user_func($callback, $attr, $content, $tag) . $m[6];
    }
553968
protected function getIntegerToken(array $m)
	{
		$sign = (isset($m['sign']) && $m['sign'] == '-') ? -1 : 1;
		
		if(isset($m['oct']) && $m['oct'] != '')
		{
			return new AnnotationToken(AnnotationToken::T_INTEGER, $sign * intval($m['oct'], 8));
		}
		
		if(isset($m['bin']) && $m['bin'] != '')
		{
			return new AnnotationToken(AnnotationToken::T_INTEGER, $sign * intval($m['bin'], 2));
		}
		
		if(isset($m['hex']) && $m['hex'] != '')
		{
			return new AnnotationToken(AnnotationToken::T_INTEGER, $sign * intval($m['hex'], 16));
		}
		
		return new AnnotationToken(AnnotationToken::T_INTEGER, $sign * (int)$m['dec']);
	}
146478
public static function toJsonDebugResursive(&$m)
    {
        if ($m instanceof \Closure) {
            $m = '__CALLBACK__';
        } elseif (is_array($m)) {
            if (sizeof($m) === 2 && isset($m[0]) && $m[0] instanceof \PHPDaemon\WebSocket\Route) {
                if (isset($m[1]) && is_string($m[1]) && strncmp($m[1], 'remote_', 7) === 0) {
                    $m = '__CALLBACK__';
                }
            } else {
                foreach ($m as &$v) {
                    static::toJsonDebugResursive($v);
                }
            }
        } elseif (is_object($m)) {
            foreach ($m as &$v) {
                static::toJsonDebugResursive($v);
            }
        }
    }
30661
protected static function getUrlParameterNamesToExcludeFromUrl()
    {
        $paramsToExclude = Config::getInstance()->Tracker['url_query_parameter_to_exclude_from_url'];
        $paramsToExclude = explode(",", $paramsToExclude);
        $paramsToExclude = array_map('trim', $paramsToExclude);
        return $paramsToExclude;
    }
288834
private function unParseUrl(array $url): string
    {
        $scheme = isset($url['scheme']) ? $url['scheme'] . '://' : '';
        $host = isset($url['host']) ? $url['host'] : '';
        $port = isset($url['port']) ? ':' . $url['port'] : '';
        $user = isset($url['user']) ? $url['user'] : '';
        $pass = isset($url['pass']) ? ':' . $url['pass'] : '';
        $pass = ($user || $pass) ? "$pass@" : '';
        $path = isset($url['path']) ? $url['path'] : '';
        $query = isset($url['query']) ? '?' . $url['query'] : '';
        $fragment = isset($url['fragment']) ? '#' . $url['fragment'] : '';

        return "{$scheme}{$user}{$pass}{$host}{$port}{$path}{$query}{$fragment}";
    }
1370446
private static function parseUrl(string $url, array $query = [], array $allowedSchemes = ['http' => 80, 'https' => 443]): array
    {
        if (false === $parts = parse_url($url)) {
            throw new InvalidArgumentException(sprintf('Malformed URL "%s".', $url));
        }

        if ($query) {
            $parts['query'] = self::mergeQueryString($parts['query'] ?? null, $query, true);
        }

        $port = $parts['port'] ?? 0;

        if (null !== $scheme = $parts['scheme'] ?? null) {
            if (!isset($allowedSchemes[$scheme = strtolower($scheme)])) {
                throw new InvalidArgumentException(sprintf('Unsupported scheme in "%s".', $url));
            }

            $port = $allowedSchemes[$scheme] === $port ? 0 : $port;
            $scheme .= ':';
        }

        if (null !== $host = $parts['host'] ?? null) {
            if (!\defined('INTL_IDNA_VARIANT_UTS46') && preg_match('/[\x80-\xFF]/', $host)) {
                throw new InvalidArgumentException(sprintf('Unsupported IDN "%s", try enabling the "intl" PHP extension or running "composer require symfony/polyfill-intl-idn".', $host));
            }

            if (false === $host = \defined('INTL_IDNA_VARIANT_UTS46') ? idn_to_ascii($host, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46) : strtolower($host)) {
                throw new InvalidArgumentException(sprintf('Unsupported host in "%s".', $url));
            }

            $host .= $port ? ':'.$port : '';
        }

        foreach (['user', 'pass', 'path', 'query', 'fragment'] as $part) {
            if (!isset($parts[$part])) {
                continue;
            }

            if (false !== strpos($parts[$part], '%')) {
                // https://tools.ietf.org/html/rfc3986#section-2.3
                $parts[$part] = preg_replace_callback('/%(?:2[DE]|3[0-9]|[46][1-9A-F]|5F|[57][0-9A]|7E)++/i', function ($m) { return rawurldecode($m[0]); }, $parts[$part]);
            }

            // https://tools.ietf.org/html/rfc3986#section-3.3
            $parts[$part] = preg_replace_callback("#[^-A-Za-z0-9._~!$&/'()*+,;=:@%]++#", function ($m) { return rawurlencode($m[0]); }, $parts[$part]);
        }

        return [
            'scheme' => $scheme,
            'authority' => null !== $host ? '//'.(isset($parts['user']) ? $parts['user'].(isset($parts['pass']) ? ':'.$parts['pass'] : '').'@' : '').$host : null,
            'path' => isset($parts['path'][0]) ? $parts['path'] : null,
            'query' => isset($parts['query']) ? '?'.$parts['query'] : null,
            'fragment' => isset($parts['fragment']) ? '#'.$parts['fragment'] : null,
        ];
    }
1204159
protected function renderQuickCallback(array $m)
	{
		if (isset($m[3]))
		{
			return $this->renderQuickSelfClosingTag($m);
		}

		if (isset($m[2]))
		{
			// Single tag
			$id = $m[2];
		}
		else
		{
			// Tag pair
			$id = $m[1];
			$this->checkTagPairContent($id, $m[0]);
		}

		if (isset($this->static[$id]))
		{
			return $this->static[$id];
		}
		if (isset($this->dynamic[$id]))
		{
			return preg_replace($this->dynamic[$id][0], $this->dynamic[$id][1], $m[0], 1);
		}

		return $this->renderQuickTemplate($id, $m[0]);
	}
1145859
protected function getIdentifierToken(array $m)
	{
		switch(strtolower($m[0]))
		{
			case 'null':
				return new AnnotationToken(AnnotationToken::T_NULL, 'null');
			case 'true':
				return new AnnotationToken(AnnotationToken::T_TRUE, 'true');
			case 'false':
				return new AnnotationToken(AnnotationToken::T_FALSE, 'false');
			case 'class':
				return new AnnotationToken(AnnotationToken::T_CLASS, 'class');
		}
		
		return new AnnotationToken(AnnotationToken::T_IDENTIFIER, $m[0]);
	}
434180
public static function paramFilter($param) {
        if (empty($param)) {
            $param = '';
        }

        if (is_array($param)) {
            foreach ($param as $key => $value) {
                $param[$key] = String::urlSafetyFilter($value);
            }
        }

        if (is_string($param)) {
            $param = String::urlSafetyFilter($param);
        }

        return $param;
    }
360486
protected function convert(
        /*# string */ $name,
        /*# string */ $pattern
    )/*# : string */ {
        // regex
        $groupname   = "\s*([a-zA-Z][a-zA-Z0-9_]*)\s*";
        $grouptype   = ":\s*([^{}]*(?:\{(?-1)\}[^{}]*)*)";
        $placeholder = sprintf("\{%s(?:%s)?\}", $groupname, $grouptype);
        $segmenttype = "[^/]++";

        $result = preg_replace([
                '~' . $placeholder . '(*SKIP)(*FAIL) | \[~x',
                '~' . $placeholder . '(*SKIP)(*FAIL) | \]~x',
                '~\{' . $groupname . '\}~x',
                '~' . $placeholder . '~x',
            ], [
                '(?:',  // replace '['
                ')?',   // replace ']'
                '{\\1:' . $segmenttype . '}',   // add segementtype
                '(?<${1}'. $name . '>${2})'   // replace groupname
            ], strtr('/' . trim($pattern, '/'), $this->shortcuts)
        );

        return empty($name) ? $result : ("(?<$name>" . $result . ")");
    }
145969
private static function format() {

		$routes = static :: getRoutes();

		foreach($routes as $identifier => $route) {

			$url = preg_quote($route -> getUrl(), '/'); 

			//Prepend prefix for url
			if($prefix = $route -> getPrefix()) {
				$url = $prefix['url'] . $url;
			}

			if(preg_match_all('#(\\\/)*\\\{([a-zA-Z0-9_.]+)(\\\\\?)?\\\}#i', $url, $matches)) {
				
				foreach($matches[0] as $index => $match) {

					$match 		= $matches[0][$index];
					$slash 		= $matches[1][$index];
					$type 		= $matches[2][$index];
					$optional 	= $matches[3][$index];
					$where 		= $route -> getWhere();
					$variable 	= '';
					$typefound 	= false;

					if('' !== $slash) {
						$variable .= '\/';
					}

					if(true === is_array($where)) {
						
						foreach($where as $key => $regex) {
							
							if($key === $type) {
								
								$variable .= $regex;
								$typefound = true;
								break;
							}
						}
					}

					if(false === $typefound) {

						if(isset(static :: $types[$type])) {
							$variable .= static :: $types[$type];
						}
						else {
							$variable .= static :: $types['string'];	
						}
					}

					if('' !== $optional) {
						$variable = '('. $variable .')?';
					}
					else {
						$variable = '('. $variable .')';
					}
					
					$url = preg_replace('#' . preg_quote($match, '/') . '#', $variable, $url, 1);
				}
			}

			$route -> setMatch($url);
		}
	}
1167673
protected function _parseGravatarCallback($m) {
        if (preg_match('#^\$#', $m[1])) {
            foreach ($this->_template->vars as $key => $val) {
                if (substr($m[1], 1, strlen($m[1])) == $key) {
                    $m[1] = preg_replace('`' . $key . '`', $val, $m[1]);
                    $m[1] = substr($m[1], 1, strlen($m[1]));
                }
            }
        }
        if (isset($m[2]) && preg_match('#^\$#', $m[2])) {
            foreach ($this->_template->vars as $key => $val) {
                if (substr($m[2], 1, strlen($m[2])) == $key) {
                    $m[2] = preg_replace('`' . $key . '`', $val, $m[2]);
                    $m[2] = substr($m[2], 1, strlen($m[2]));
                }
            }
        }
        else {
            $m[2] = 100;
        }

        if (preg_match('#\'#', $m[1])) {
            $m[1] = preg_replace('#\'#', '"', $m[1]);

            return '<?php echo \'http://secure.gravatar.com/avatar/\'.md5(' . $m[1] . ').\'?s=' . $m[2] . '&d=identicon\'; ?>';
        }
        else {
            return '<?php echo \'http://secure.gravatar.com/avatar/\'.md5("' . $m[1] . '").\'?s=' . $m[2] . '&d=identicon\'; ?>';
        }
    }
583012
public static function parse_url_brackets(&$url)
    {
        // #1487672: special handling of square brackets,
        // URL regexp allows [] characters in URL, for example:
        // "http://example.com/?a[b]=c". However we need to handle
        // properly situation when a bracket is placed at the end
        // of the link e.g. "[http://example.com]"
        // Yes, this is not perfect handles correctly only paired characters
        // but it should work for common cases

        if (preg_match('/(\\[|\\])/', $url)) {
            $in = false;
            for ($i=0, $len=strlen($url); $i<$len; $i++) {
                if ($url[$i] == '[') {
                    if ($in)
                        break;
                    $in = true;
                }
                else if ($url[$i] == ']') {
                    if (!$in)
                        break;
                    $in = false;
                }
            }

            if ($i < $len) {
                $suffix = substr($url, $i);
                $url    = substr($url, 0, $i);
            }
        }

        // Do the same for parentheses
        if (preg_match('/(\\(|\\))/', $url)) {
            $in = false;
            for ($i=0, $len=strlen($url); $i<$len; $i++) {
                if ($url[$i] == '(') {
                    if ($in)
                        break;
                    $in = true;
                }
                else if ($url[$i] == ')') {
                    if (!$in)
                        break;
                    $in = false;
                }
            }

            if ($i < $len) {
                $suffix = substr($url, $i);
                $url    = substr($url, 0, $i);
            }
        }

        return $suffix;
    }
1108854
final protected function generateUrl(string $name, array $params = []) : string
    {
        return $this->urlGenerator
            ->generate($name, $params);
    }