MDL-19716 moodle_url: new parameter to get URL with unescaped ampersands

moodle_url::get_query_string() and moodle_url::out() now accept new
optional parameter. Backwards compatible. This is needed so we can use returned URL
during redirect() which expects unescaped ampersands.
This commit is contained in:
mudrd8mz
2009-07-05 14:31:58 +00:00
parent 5fdc39c725
commit c7f5e16a63
+13 -4
View File
@@ -379,15 +379,20 @@ class moodle_url {
*
* @param array $overrideparams params to add to the output params, these
* override existing ones with the same name.
* @param boolean $escaped Use & as params separator instead of plain &
* @return string query string that can be added to a url.
*/
public function get_query_string($overrideparams = array()) {
public function get_query_string($overrideparams = array(), $escaped = true) {
$arr = array();
$params = $overrideparams + $this->params;
foreach ($params as $key => $val) {
$arr[] = urlencode($key)."=".urlencode($val);
}
return implode($arr, "&");
if ($escaped) {
return implode('&', $arr);
} else {
return implode('&', $arr);
}
}
/**
@@ -415,18 +420,22 @@ class moodle_url {
/**
* Output url
*
* If you use the returned URL in HTML code, you want the escaped ampersands. If you use
* the returned URL in HTTP headers, you want $escaped=false.
*
* @param boolean $omitquerystring whether to output page params as a query string in the url.
* @param array $overrideparams params to add to the output url, these override existing ones with the same name.
* @param boolean $escaped Use & as params separator instead of plain &
* @return string Resulting URL
*/
public function out($omitquerystring = false, $overrideparams = array()) {
public function out($omitquerystring = false, $overrideparams = array(), $escaped = true) {
$uri = $this->scheme ? $this->scheme.':'.((strtolower($this->scheme) == 'mailto') ? '':'//'): '';
$uri .= $this->user ? $this->user.($this->pass? ':'.$this->pass:'').'@':'';
$uri .= $this->host ? $this->host : '';
$uri .= $this->port ? ':'.$this->port : '';
$uri .= $this->path ? $this->path : '';
if (!$omitquerystring) {
$querystring = $this->get_query_string($overrideparams);
$querystring = $this->get_query_string($overrideparams, $escaped);
if ($querystring) {
$uri .= '?' . $querystring;
}