diff options
author | Cesar G <prototech91@gmail.com> | 2013-12-06 13:38:54 -0800 |
---|---|---|
committer | Cesar G <prototech91@gmail.com> | 2014-04-22 15:21:02 -0700 |
commit | 3163388f63a6d0bac77ada86e2f1561f1d7f9714 (patch) | |
tree | cdd5acc22c8a83bf47b5048e678b2489dea9ce20 /phpBB/phpbb/path_helper.php | |
parent | [ticket/11508] Move the stripping param code to separate function as well. (diff) | |
download | phpbb-3163388f63a6d0bac77ada86e2f1561f1d7f9714.tar.gz phpbb-3163388f63a6d0bac77ada86e2f1561f1d7f9714.tar.bz2 phpbb-3163388f63a6d0bac77ada86e2f1561f1d7f9714.zip |
[ticket/11508] Move helper functions to path_helper class.
PHPBB3-11508
Diffstat (limited to 'phpBB/phpbb/path_helper.php')
-rw-r--r-- | phpBB/phpbb/path_helper.php | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/phpBB/phpbb/path_helper.php b/phpBB/phpbb/path_helper.php index fefef39c51..ff36ce5fc5 100644 --- a/phpBB/phpbb/path_helper.php +++ b/phpBB/phpbb/path_helper.php @@ -216,4 +216,119 @@ class path_helper return $scheme . $this->filesystem->clean_path($path); } + + /** + * Glue URL parameters together + * + * @param array $params URL parameters in the form of array(name => value) + * @return string Returns the glued string, e.g. name1=value1&name2=value2 + */ + public function glue_url_params($params) + { + $_params = array(); + + foreach ($params as $key => $value) + { + $_params[] = $key . '=' . $value; + } + return implode('&', $_params); + } + + /** + * Get the base and parameters of a URL + * + * @param string $url URL to break apart + * @param string $separator Parameter separator. Defaults to & + * @return array Returns the base and parameters in the form of array('base' => string, 'params' => array(name => value)) + */ + public function get_url_parts($url, $separator = '&') + { + $params = array(); + + if (strpos($url, '?') !== false) + { + $base = substr($url, 0, strpos($url, '?')); + $args = substr($url, strlen($base) + 1); + $args = ($args) ? explode($separator, $args) : array(); + + foreach ($args as $argument) + { + $arguments = explode('=', $argument); + $key = $arguments[0]; + unset($arguments[0]); + + if ($key === '') + { + continue; + } + + $params[$key] = implode('=', $arguments); + } + } + else + { + $base = $url; + } + + return array( + 'base' => $base, + 'params' => $params, + ); + } + + /** + * Strip parameters from an already built URL. + * + * @param string $url URL to strip parameters from + * @param array|string $strip Parameters to strip. + * @param string $separator Parameter separator. Defaults to & + * @return string Returns the new URL. + */ + public function strip_url_params($url, $strip, $separator = '&') + { + $url_parts = $this->get_url_parts($url, $separator); + $params = $url_parts['params']; + + if (!is_array($strip)) + { + $strip = array($strip); + } + + if (!empty($params)) + { + // Strip the parameters off + foreach ($strip as $param) + { + if (isset($params[$param])) + { + unset($params[$param]); + } + } + } + + return $url_parts['base'] . (($params) ? '?' . $this->glue_url_params($params) : ''); + } + + /** + * Append parameters to an already built URL. + * + * @param string $url URL to append parameters to + * @param array $new_params Parameters to add in the form of array(name => value) + * @param string $separator Parameter separator. Defaults to & + * @return string Returns the new URL. + */ + public function append_url_params($url, $new_params, $separator = '&') + { + $url_parts = $this->get_url_parts($url, $separator); + $params = array_merge($url_parts['params'], $new_params); + + // Move the sid to the end if it's set + if (isset($params['sid'])) { + $sid = $params['sid']; + unset($params['sid']); + $params['sid'] = $sid; + } + + return $url_parts['base'] . '?' . $this->glue_url_params($params); + } } |