1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 22:10:45 +02:00

Merge remote-tracking branch 'github-prototech/ticket/11508' into develop-ascraeus

* github-prototech/ticket/11508:
  [ticket/11508] Fix build_url test.
  [ticket/11508] Add functional test for jumpbox.
  [ticket/11508] Use $phpbb_path_helper.
  [ticket/11508] Allow equal sign in parameter value.
  [ticket/11508] Remove unnecessary implode.
  [ticket/11508] Remove unnecessary isset check.
  [ticket/11508] Fix build_url test.
  [ticket/11508] Curly brace should be on its own line...
  [ticket/11508] The question mark is expected even if there are no parameters.
  [ticket/11508] Add tests.
  [ticket/11508] Do not add the '?' unless there are parameters.
  [ticket/11508] Change separator parameter to a simple true|false $is_amp.
  [ticket/11508] Move helper functions to path_helper class.
  [ticket/11508] Move the stripping param code to separate function as well.
  [ticket/11508] Build the jumpbox hidden fields using the _form action_
  [ticket/11508] Split parts of build_url() into reusable functions.
This commit is contained in:
Nils Adermann
2014-05-02 16:40:01 +02:00
6 changed files with 393 additions and 62 deletions

View File

@@ -216,4 +216,120 @@ 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 bool $is_amp Is the parameter separator &. Defaults to true.
* @return array Returns the base and parameters in the form of array('base' => string, 'params' => array(name => value))
*/
public function get_url_parts($url, $is_amp = true)
{
$separator = ($is_amp) ? '&' : '&';
$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)
{
if (empty($argument))
{
continue;
}
list($key, $value) = explode('=', $argument, 2);
if ($key === '')
{
continue;
}
$params[$key] = $value;
}
}
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 bool $is_amp Is the parameter separator &. Defaults to true.
* @return string Returns the new URL.
*/
public function strip_url_params($url, $strip, $is_amp = true)
{
$url_parts = $this->get_url_parts($url, $is_amp);
$params = $url_parts['params'];
if (!is_array($strip))
{
$strip = array($strip);
}
if (!empty($params))
{
// Strip the parameters off
foreach ($strip as $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 $is_amp Is the parameter separator &. Defaults to true.
* @return string Returns the new URL.
*/
public function append_url_params($url, $new_params, $is_amp = true)
{
$url_parts = $this->get_url_parts($url, $is_amp);
$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'] . (($params) ? '?' . $this->glue_url_params($params) : '');
}
}