From fa2fca9512c208e89971661548bf960f991d613d Mon Sep 17 00:00:00 2001 From: pafgoncalves Date: Wed, 30 Nov 2016 18:52:08 +0000 Subject: Add regular expressions to the exclude parameters (#10846) Allows to use regular expressions in the exclude parameters option. Doesn't break compatibility if the parameter is not a regular expression, doing a strict compare. "/.*/" can be used to exclude all parameters. * Changed the UI texts to explain that the excluding parameters list supports regular expressions. Added unit tests. --- core/UrlHelper.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'core/UrlHelper.php') diff --git a/core/UrlHelper.php b/core/UrlHelper.php index 66a0e64e25..ae4f3c622d 100644 --- a/core/UrlHelper.php +++ b/core/UrlHelper.php @@ -17,6 +17,32 @@ use Piwik\Intl\Data\Provider\RegionDataProvider; */ class UrlHelper { + + /** + * Checks if a string matches/is equal to one of the patterns/strings. + * + * @static + * @param $test String to test. + * @param $patterns Array of strings or regexs. + * + * @return true if $test matches or is equal to one of the regex/string in $patterns, false otherwise. + */ + protected static function in_array_reg($test, $patterns) + { + foreach($patterns as $val) { + if(@preg_match($val, null) === false) { + if( strcasecmp($val, $test) === 0 ) { + return true; + } + } else { + if( preg_match($val, $test) === 1 ) { + return true; + } + } + } + return false; + } + /** * Converts an array of query parameter name/value mappings into a query string. * Parameters that are in `$parametersToExclude` will not appear in the result. @@ -36,7 +62,7 @@ class UrlHelper // decode encoded square brackets $name = str_replace(array('%5B', '%5D'), array('[', ']'), $name); - if (!in_array(strtolower($name), $parametersToExclude)) { + if (!self::in_array_reg(strtolower($name), $parametersToExclude)) { if (is_array($value)) { foreach ($value as $param) { if ($param === false) { -- cgit v1.2.3