From 95c45074eb94b98ff41c5c3ebfa1f0e0e07e648e Mon Sep 17 00:00:00 2001 From: Sergii Kauk Date: Wed, 15 Jul 2015 10:56:25 +0200 Subject: [PATCH] Escape reserved characters without escaping logic operators When using multiple filters, they are separated by logical operators AND, OR (; and , respectively in GA query syntax). To avoid screening them with str_replace we can first check valid operators and add 'ga:' prefix where missing and then screen only those reserved characters which are not followed by 'ga:' prefix. See: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#combiningFilters --- gapi.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gapi.class.php b/gapi.class.php index c704301..5d168ac 100644 --- a/gapi.class.php +++ b/gapi.class.php @@ -237,8 +237,8 @@ protected function processFilter($filter) { $valid_operators = '(!~|=~|==|!=|>|<|>=|<=|=@|!@)'; $filter = preg_replace('/\s\s+/', ' ', trim($filter)); //Clean duplicate whitespace - $filter = str_replace(array(',', ';'), array('\,', '\;'), $filter); //Escape Google Analytics reserved characters $filter = preg_replace('/(&&\s*|\|\|\s*|^)([a-z0-9]+)(\s*' . $valid_operators . ')/i','$1ga:$2$3',$filter); //Prefix ga: to metrics and dimensions + $filter = preg_replace('/([,;]){1}(?!ga\:)/', '\\\\$1', $filter); //Escape Google Analytics reserved characters $filter = preg_replace('/[\'\"]/i', '', $filter); //Clear invalid quote characters $filter = preg_replace(array('/\s*&&\s*/','/\s*\|\|\s*/','/\s*' . $valid_operators . '\s*/'), array(';', ',', '$1'), $filter); //Clean up operators @@ -850,4 +850,4 @@ private function fopenRequest($get_variables=null, $post_variables=null, $header return array('body'=>$response!==false?$response:'Request failed, consider using php5-curl module for more information.', 'code'=>$response!==false?'200':'400'); } -} \ No newline at end of file +}