From d6a164c4008395e323ef8888a3c8867188f40d6b Mon Sep 17 00:00:00 2001 From: Gerhard Killesreiter Date: Thu, 13 Apr 2006 08:25:27 +0000 Subject: #5371, drupal_get_destination, pager and tablesort array handling, patch by Steven --- includes/common.inc | 54 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-) (limited to 'includes/common.inc') diff --git a/includes/common.inc b/includes/common.inc index 368aa64fe..263f01ac6 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -155,6 +155,46 @@ function drupal_get_headers() { * Functions to properly handle HTTP responses. */ +/** + * 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 @@ -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); } } -- cgit v1.2.3