diff options
Diffstat (limited to 'includes/common.inc')
-rw-r--r-- | includes/common.inc | 54 |
1 files changed, 44 insertions, 10 deletions
diff --git a/includes/common.inc b/includes/common.inc index 368aa64fe..263f01ac6 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -156,6 +156,46 @@ function drupal_get_headers() { */ /** + * Parse an array into a valid urlencoded query string. + * + * @param $query + * The array to be processed e.g. $_GET + * @param $exclude + * The array filled with keys to be excluded. Use parent[child] to exclude nested items. + * @param $urlencode + * If TRUE, the keys and values are both urlencoded. + * @param $parent + * Should not be passed, only used in recursive calls + * @return + * urlencoded string which can be appended to/as the URL query string + */ +function drupal_query_string_encode($query, $exclude = array(), $parent = '') { + $params = array(); + + foreach ($query as $key => $value) { + if ($parent) { + $key = $parent .'['. urlencode($key) .']'; + } + else { + $key = urlencode($key); + } + + if (in_array(urldecode($key), $exclude)) { + continue; + } + + if (is_array($value)) { + $params[] = drupal_query_string_encode($value, $exclude, $key); + } + else { + $params[] = $key .'='. urlencode($value); + } + } + + return implode('&', $params); +} + +/** * Prepare a destination query string for use in combination with * drupal_goto(). Used to direct the user back to the referring page * after completing a form. By default the current URL is returned. @@ -171,17 +211,11 @@ function drupal_get_destination() { } else { $path = $_GET['q']; - $params = array(); - foreach ($_GET as $key => $value) { - if ($key == 'q') { - continue; - } - $params[] = urlencode($key) .'='. urlencode($value); - } - if (count($params)) { - $path .= '?'; + $query = drupal_query_string_encode($_GET, array('q')); + if ($query != '') { + $path .= '?'. $query; } - return 'destination='. urlencode($path . implode('&', $params)); + return 'destination='. urlencode($path); } } |