mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 14:18: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
|
// Append SID
|
||||||
$redirect = append_sid($page, false, false);
|
$redirect = append_sid($page, false, false);
|
||||||
|
$basic_parts = phpbb_get_url_parts($redirect, '&');
|
||||||
// Add delimiter if not there...
|
$params = $basic_parts['params'];
|
||||||
if (strpos($redirect, '?') === false)
|
|
||||||
{
|
|
||||||
$redirect .= '?';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strip vars...
|
// Strip vars...
|
||||||
if ($strip_vars !== false && strpos($redirect, '?') !== false)
|
if ($strip_vars !== false)
|
||||||
{
|
{
|
||||||
if (!is_array($strip_vars))
|
if (!is_array($strip_vars))
|
||||||
{
|
{
|
||||||
$strip_vars = 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);
|
return $basic_parts['base'] . (($params) ? '?' . phpbb_glue_url_params($params) : '');
|
||||||
$args = ($args) ? explode('&', $args) : array();
|
}
|
||||||
$redirect = substr($redirect, 0, strpos($redirect, '?'));
|
|
||||||
|
/**
|
||||||
|
* 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)
|
foreach ($args as $argument)
|
||||||
{
|
{
|
||||||
|
@ -2400,29 +2419,54 @@ function build_url($strip_vars = false)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query[$key] = implode('=', $arguments);
|
$params[$key] = implode('=', $arguments);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Strip the vars off
|
else
|
||||||
foreach ($strip_vars as $strip)
|
{
|
||||||
{
|
$base = $url;
|
||||||
if (isset($query[$strip]))
|
|
||||||
{
|
|
||||||
unset($query[$strip]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Glue the remaining parts together... already urlencoded
|
|
||||||
foreach ($query as $key => $value)
|
|
||||||
{
|
|
||||||
$_query[] = $key . '=' . $value;
|
|
||||||
}
|
|
||||||
$query = implode('&', $_query);
|
|
||||||
|
|
||||||
$redirect .= ($query) ? '?' . $query : '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return str_replace('&', '&', $redirect);
|
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)
|
||||||
|
{
|
||||||
|
$_params[] = $key . '=' . $value;
|
||||||
|
}
|
||||||
|
return implode('&', $_params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
// 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'] . '?' . phpbb_glue_url_params($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue