]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19716 moodle_url: new parameter to get URL with unescaped ampersands
authormudrd8mz <mudrd8mz>
Sun, 5 Jul 2009 14:31:58 +0000 (14:31 +0000)
committermudrd8mz <mudrd8mz>
Sun, 5 Jul 2009 14:31:58 +0000 (14:31 +0000)
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.

lib/weblib.php

index 6e40a59594f102a726536251fbc135695a34fa59..932d90439102adddac94d2def07bce2ee6954201 100644 (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 &amp; 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, "&amp;");
+        if ($escaped) {
+            return implode('&amp;', $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 &amp; 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;
             }