From b7bd74055d47caafb2cc710b1c572dcc0e347975 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 25 Dec 2011 17:57:15 +0000 Subject: [PATCH] PHP is officially the worst web API ever --- core/util.inc.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/core/util.inc.php b/core/util.inc.php index a5bec9db..98f17866 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -222,6 +222,44 @@ function make_link($page=null, $query=null) { } } + +/** + * Take the current URL and modify some paramaters + * + * @retval string + */ +function modify_current_url($changes) { + // SHIT: PHP is officially the worst web API ever because it does not + // have a built-in function to do this. + + // SHIT: parse_str is magically retarded; not only is it a useless name, it also + // didn't return the parsed array, preferring to overwrite global variables with + // whatever data the user supplied. Thankfully, 4.0.3 added an extra option to + // give it an array to use... + $params = array(); + parse_str($_SERVER['QUERY_STRING'], $params); + + if(isset($changes['q'])) { + $base = $changes['q']; + unset($changes['q']); + } + else { + $base = $_GET['q']; + } + + if(isset($params['q'])) { + unset($params['q']); + } + + foreach($changes as $k => $v) { + if(is_null($v) and isset($params[$k])) unset($params[$k]); + $params[$k] = $v; + } + + return make_link($base, http_build_query($params)); +} + + /** * Turn a relative link into an absolute one, including hostname *