mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 06:08:52 +00:00
[ticket/11508] Split parts of build_url() into reusable functions.
PHPBB3-11508
This commit is contained in:
parent
5b6a675399
commit
c96ade8463
1 changed files with 75 additions and 31 deletions
|
@ -2368,26 +2368,45 @@ function build_url($strip_vars = false)
|
|||
|
||||
// Append SID
|
||||
$redirect = append_sid($page, false, false);
|
||||
|
||||
// Add delimiter if not there...
|
||||
if (strpos($redirect, '?') === false)
|
||||
{
|
||||
$redirect .= '?';
|
||||
}
|
||||
$basic_parts = phpbb_get_url_parts($redirect, '&');
|
||||
$params = $basic_parts['params'];
|
||||
|
||||
// Strip vars...
|
||||
if ($strip_vars !== false && strpos($redirect, '?') !== false)
|
||||
if ($strip_vars !== false)
|
||||
{
|
||||
if (!is_array($strip_vars))
|
||||
{
|
||||
$strip_vars = array($strip_vars);
|
||||
}
|
||||
|
||||
$query = $_query = array();
|
||||
// Strip the vars off
|
||||
foreach ($strip_vars as $strip)
|
||||
{
|
||||
if (isset($params[$strip]))
|
||||
{
|
||||
unset($params[$strip]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$args = substr($redirect, strpos($redirect, '?') + 1);
|
||||
$args = ($args) ? explode('&', $args) : array();
|
||||
$redirect = substr($redirect, 0, strpos($redirect, '?'));
|
||||
return $basic_parts['base'] . (($params) ? '?' . phpbb_glue_url_params($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))
|
||||
*/
|
||||
function phpbb_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)
|
||||
{
|
||||
|
@ -2400,29 +2419,54 @@ function build_url($strip_vars = false)
|
|||
continue;
|
||||
}
|
||||
|
||||
$query[$key] = implode('=', $arguments);
|
||||
$params[$key] = implode('=', $arguments);
|
||||
}
|
||||
|
||||
// Strip the vars off
|
||||
foreach ($strip_vars as $strip)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($query[$strip]))
|
||||
$base = $url;
|
||||
}
|
||||
|
||||
return array(
|
||||
'base' => $base,
|
||||
'params' => $params,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Glue URL parameters together
|
||||
*
|
||||
* @param array $params URL parameters in the form of array(name => value)
|
||||
* @return array Returns the glued string.
|
||||
*/
|
||||
function phpbb_glue_url_params($params) {
|
||||
$_params = array();
|
||||
|
||||
foreach ($params as $key => $value)
|
||||
{
|
||||
unset($query[$strip]);
|
||||
$_params[] = $key . '=' . $value;
|
||||
}
|
||||
return implode('&', $_params);
|
||||
}
|
||||
|
||||
// Glue the remaining parts together... already urlencoded
|
||||
foreach ($query as $key => $value)
|
||||
{
|
||||
$_query[] = $key . '=' . $value;
|
||||
}
|
||||
$query = implode('&', $_query);
|
||||
/**
|
||||
* Append parameters to an already built URL.
|
||||
*
|
||||
* @param array $new_params Parameters to add in the form of array(name => value)
|
||||
* @return string Returns the new URL.
|
||||
*/
|
||||
function phpbb_append_url_params($url, $new_params) {
|
||||
$url_parts = phpbb_get_url_parts($url);
|
||||
$params = array_merge($url_parts['params'], $new_params);
|
||||
|
||||
$redirect .= ($query) ? '?' . $query : '';
|
||||
// Move the sid to the end if it's set
|
||||
if (isset($params['sid'])) {
|
||||
$sid = $params['sid'];
|
||||
unset($params['sid']);
|
||||
$params['sid'] = $sid;
|
||||
}
|
||||
|
||||
return str_replace('&', '&', $redirect);
|
||||
return $url_parts['base'] . '?' . phpbb_glue_url_params($params);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue