Happy 2014
[squirrelmail.git] / functions / html.php
index 2df57f2e7aa0e9550a2e1639a06e7070ae1e5d48..796f7b492d970e0ae68b1262d405e72d6d4b56cc 100644 (file)
@@ -7,7 +7,7 @@
  * the right to left implementation by "functionize" some
  * html outputs.
  *
- * @copyright © 1999-2007 The SquirrelMail Project Team
+ * @copyright 1999-2014 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
@@ -237,7 +237,7 @@ function create_body($onload='', $class='', $aAttribs=array()) {
 
 /**
  * Generates html tags
-//FIXME: This should not be used anywhere in the core, or we should convert this to use templates.  We sould not be assuming HTML output.
+//FIXME: This should not be used anywhere in the core, or we should convert this to use templates.  We should not be assuming HTML output.
  *
  * @param string $tag Tag to output
  * @param string $val Value between tags
@@ -313,6 +313,43 @@ function html_tag( $tag,                // Tag to output
 }
 
 
+/**
+ * This function is used to add, modify or delete more than
+ * one GET variable at a time in a URL.  This simply takes
+ * an array of variables (key/value pairs) and passes them
+ * one at a time to {@link set_url_var}.
+ * 
+ * Note that the value for any one of the variables may be
+ * an array, and it will be handled properly.
+ *
+ * As with set_url_var, any of the variable values may be
+ * set to NULL to remove it from the URI.
+ *
+ * Also, to ensure compatibility with older versions, use
+ * $val='0' to set $var to 0. 
+ *
+ * @param string  $uri      URI that must be modified
+ * @param array   $values   List of GET variable names and their values
+ * @param boolean $sanitize Controls sanitizing of ampersand in URIs
+ *
+ * @return string The modified URI
+ *
+ * @since 1.5.2
+ *
+ */
+function set_uri_vars($uri, $values, $sanitize=TRUE) {
+    foreach ($values as $key => $value)
+        if (is_array($value)) {
+          $i = 0;
+          foreach ($value as $val)
+             $uri = set_url_var($uri, $key . '[' . $i++ . ']', $val, $sanitize);
+        }
+        else
+          $uri = set_url_var($uri, $key, $value, $sanitize);
+    return $uri;
+}
+
+
 /**
  * This function is used to add, modify or delete GET variables in a URL. 
  * It is especially useful when $url = $PHP_SELF
@@ -322,15 +359,20 @@ function html_tag( $tag,                // Tag to output
  *
  * @param string $url url that must be modified
  * @param string $var GET variable name
- * @param string $val variable value
+ * @param string $val variable value (CANNOT be an array)
  * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
+ * @param boolean $treat_as_array When TRUE, if $var is an array (it occurs one
+ *                                or more times with square brackets after it,
+ *                                e.g. "var[1]"), the whole array will be removed
+ *                                (when $val is NULL) or the given value will be
+ *                                added to the next array slot (@since 1.4.23/1.5.2)
  *
  * @return string $url modified url
  *
  * @since 1.3.0
  *
  */
-function set_url_var($url, $var, $val=null, $link=true) {
+function set_url_var($url, $var, $val=null, $link=true, $treat_as_array=false) {
     $url = str_replace('&','&',$url);
 
     if (strpos($url, '?') === false) {
@@ -341,18 +383,35 @@ function set_url_var($url, $var, $val=null, $link=true) {
         
     $newpar = array(); 
     $params = explode('&', $params);
+    $array_names = array();
    
     foreach ($params as $p) {
         if (trim($p)) {
             $p = explode('=', $p);
             $newpar[$p[0]] = (isset($p[1]) ? $p[1] : '');
+            if ($treat_as_array && preg_match('/(.*)\[(\d+)]$/', $p[0], $matches)) {
+               if (!isset($array_names[$matches[1]])) $array_names[$matches[1]] = array();
+               $array_names[$matches[1]][$matches[2]] = $p[1];
+            }
         }
     }
 
     if (is_null($val)) {
-        unset($newpar[$var]);
+        if ($treat_as_array && !empty($array_names[$var])) {
+            foreach ($array_names[$var] as $key => $ignore)
+                unset($newpar[$var . '[' . $key . ']']);
+        } else {
+            unset($newpar[$var]);
+        }
     } else {
-        $newpar[$var] = $val;
+        if ($treat_as_array && !empty($array_names[$var])) {
+            $max_key = 0;
+            foreach ($array_names[$var] as $key => $ignore)
+                if ($key >= $max_key) $max_key = $key + 1;
+            $newpar[$var . '[' . $max_key . ']'] = $val;
+        } else {
+            $newpar[$var] = $val;
+        }
     }
 
     if (!count($newpar)) {