Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new parameters in campaign builder #16494

Merged
merged 2 commits into from Sep 29, 2020
Merged

Use new parameters in campaign builder #16494

merged 2 commits into from Sep 29, 2020

Conversation

sgiehl
Copy link
Member

@sgiehl sgiehl commented Sep 29, 2020

fixes #16433

@sgiehl sgiehl added not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. Needs Review PRs that need a code review labels Sep 29, 2020
@sgiehl sgiehl added this to the 4.0.0-RC milestone Sep 29, 2020
@sanchezzzhak
Copy link

encodeURIComponent not escape special chars !'()*
helper method safe escape value

function buildQueryEscape(param) {
  return encodeURIComponent(param).replace(/[!'()*]/g, x => `%${x.charCodeAt(0).toString(16).toUpperCase()}`);
}

To create a key and value string, I use the equivalent of http_query_builder, which is found in PHP

function buildQuery(queryData, numericPrefix = null, argSeparator = '&', tempKey = null) {
  if (queryData === null) {
	return '';
  }
  
  let query = Object.keys(queryData).map(k => {
	let res;
	let key = k;
	if (tempKey) {
	  key = tempKey + '[' + key + ']';
	}
	if (typeof queryData[k] === 'object') {
	  res = buildQuery(queryData[k], null, argSeparator, key);
	} else {
	  if (numericPrefix) {
	    let isNum = !isNaN(parseFloat(key)) && isFinite(key);
		key = isNum ? numericPrefix + Number(key) : key;
	  }
	  if (queryData[k] !== '' && queryData[k]!== undefined && queryData[k]!== null) {
		let val = queryData[k];
                // convert boolean value to int
		if (val === true) {
		  val = '1';
		} else if (val === false) {
		  val = '0';
		}
		res = buildQueryEscape(key) + '=' + buildQueryEscape(val);
	  }
	}
	return res;
  });
  
  query = query.filter((w) => {
	return w !== undefined /* w !== null */;
  });
  return query.join(argSeparator).replace(/[!'()*]/g, '');
}

test

https://jsfiddle.net/sanchezzzhak/bs3fq7jg/9/

image

@tsteur
Copy link
Member

tsteur commented Sep 29, 2020

@sgiehl looks good

@sanchezzzhak thanks for this. Didn't even know. Do these characters need to be escaped in the URL? I was reading https://stackoverflow.com/questions/18251399/why-doesnt-encodeuricomponent-encode-single-quotes-apostrophes/18251730 and it may not be needed?

Note that angular will also escape it further to prevent eg any XSS.

@sanchezzzhak
Copy link

@tsteur I had problems parsing get string data in nodejs on special characters
then I wondered how php does it
https://3v4l.org/cvjla

@tsteur
Copy link
Member

tsteur commented Sep 29, 2020

Thanks @sanchezzzhak For now we only rename the parameters to fix the particular issue then we could in a separate PR apply the other fix if you're maybe keen on creating one?

@tsteur tsteur merged commit 071d352 into 4.x-dev Sep 29, 2020
@tsteur tsteur deleted the campaignbuilder branch September 29, 2020 21:05
@sanchezzzhak
Copy link

@tsteur I'm currently busy adding device definitions. Maybe when I have nothing to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review PRs that need a code review not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Campaign builder should use new campaign parameters
3 participants