Skip to content

Commit

Permalink
Move URL related methods to URL class
Browse files Browse the repository at this point in the history
  • Loading branch information
vampy committed Oct 31, 2017
1 parent 14ce82b commit dc9366c
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 130 deletions.
3 changes: 2 additions & 1 deletion bugs/add.php
Expand Up @@ -19,7 +19,8 @@
*/
require_once(dirname(__DIR__) . DIRECTORY_SEPARATOR . "config.php");

$tpl = StkTemplate::get("bugs/add.tpl")->assign("current_url", urlencode(Util::getCurrentUrl(false, false)));
$tpl = StkTemplate::get("bugs/add.tpl")
->assign("current_url", URL::encode(URL::getCurrent(false, false)));

// check permission
if (!User::hasPermission(AccessControl::PERM_ADD_BUG))
Expand Down
2 changes: 1 addition & 1 deletion bugs/view.php
Expand Up @@ -63,7 +63,7 @@

$can_edit = User::hasPermission(AccessControl::PERM_EDIT_BUGS);
$tpl->assign("bug", $tpl_data)
->assign("current_url", urlencode(Util::getCurrentUrl(false, false)))
->assign("current_url", URL::encode(URL::getCurrent(false, false)))
->assign("can_add_comment", User::hasPermission(AccessControl::PERM_ADD_BUG_COMMENT))
->assign("can_edit_bug", (User::getLoggedId() === $tpl_data["id"]) || $can_edit)
->assign("can_delete_bug", $can_edit)
Expand Down
2 changes: 1 addition & 1 deletion include/PaginationTemplate.class.php
Expand Up @@ -186,7 +186,7 @@ protected function setup()
// set default page, if not set already
if (!$this->page_url)
{
$this->page_url = Util::removeQueryArguments([static::PAGE_ARGUMENT], Util::getCurrentUrl());
$this->page_url = URL::removeQueryArguments([static::PAGE_ARGUMENT], URL::getCurrent());
}

// see if we build the ... on one direction or the other
Expand Down
1 change: 0 additions & 1 deletion include/Session.class.php
Expand Up @@ -204,7 +204,6 @@ public static function isStarted()
return session_status() === PHP_SESSION_ACTIVE;
}


/**
* Update the current session id with a newly generated one
* TODO avoid lost session like the examples here? https://secure.php.net/manual/en/function.session-regenerate-id.php
Expand Down
142 changes: 142 additions & 0 deletions include/URL.class.php
Expand Up @@ -23,6 +23,148 @@
*/
class URL
{
/**
* This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to
* pass variables to the next page.
*
* @param string $str The string to be encoded.
*
* @return string the url encode string
*/
public static function encode($str)
{
return urlencode($str);
}

/**
* Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
*
* @param string $str The string to be decoded.
*
* @return string Returns the decoded string.
*/
public static function decode($str)
{
return urldecode($str);
}

/**
* Gets/Converts a string query to an hash map array
*
* @param string $query
*
* @return array
*/
public static function queryStringToArray($query)
{
// build vars
$vars = [];
parse_str($query, $vars);

return $vars;
}

/**
* Generates a URL-encoded query string from the associative (or indexed) array provided.
*
* @param array $query_data it may be a simple one-dimensional structure, or an array of arrays (which in turn
* may contain other arrays).
* @param string $numeric_prefix If numeric indices are used in the base array and this parameter is provided, it
* will be prepended to the numeric index for elements in the base array only.
*
* @return string
*/
public static function queryArrayToString(array $query_data, $numeric_prefix = null)
{
return $numeric_prefix ? http_build_query($query_data, $numeric_prefix) : http_build_query($query_data);
}

/**
* Removes an item or list from the query string.
*
* @param string|array $keys Query key or keys to remove.
* @param string $url The url to remove them from
*
* @return string
*/
public static function removeQueryArguments(array $keys, $url)
{
$parsed = parse_url($url);
$url = rtrim($url, "?&");

// the query is empty
if (empty($parsed["query"]))
{
return $url . "?";
}

$vars = static::queryStringToArray($parsed["query"]);

// remove query
foreach ($keys as $key)
{
unset($vars[$key]);
}

$query = empty($vars) ? "" : static::queryArrayToString($vars) . "&";
$new_url = $parsed["scheme"] . "://" . $parsed["host"] . $parsed["path"] . "?" . $query;

return $new_url;
}

/**
* Get the url address currently displayed
*
* @param bool $request_params retrieve the url tih the GET params
* @param bool $request_script_name retrieve the url with only the script name
*
* Possible usage: getCurrent(true, false) - the default, get the full url
* getCurrent(false, true) - get the url without the GET params only the script name
* getCurrent(false, false) - get the url's directory path only
*
* @return string
*/
public static function getCurrent($request_params = true, $request_script_name = false)
{
// begin buildup
$page_url = "http";

// add for ssl secured connections
if (Util::isHTTPS())
{
$page_url .= "s";
}
$page_url .= "://";

// find the end part of the url
if ($request_params) // full url with requests
{
$end_url = $_SERVER["REQUEST_URI"];
}
else if ($request_script_name) // full url without requests
{
$end_url = $_SERVER["SCRIPT_NAME"];
}
else // url directory path
{
$end_url = dirname($_SERVER["SCRIPT_NAME"]) . "/";
}

// add host
$page_url .= $_SERVER["SERVER_NAME"];

if ((int)$_SERVER["SERVER_PORT"] !== 80)
{
$page_url .= ":" . $_SERVER["SERVER_PORT"] . $end_url;
}
else
{
$page_url .= $end_url;
}

return $page_url;
}

/**
* Modify an the internal link using the apache_rewrites config from the database
*
Expand Down
128 changes: 4 additions & 124 deletions include/Util.class.php
Expand Up @@ -293,8 +293,10 @@ public static function validateCaptchaKeysSet()
CAPTCHA_SECRET. Reload the page after this.</p>
MSG;

exit(StkTemplate::get('error-page.tpl')
->assign('error', ['title' => 'Add your captcha keys', 'message' => $message])->toString());
exit(
StkTemplate::get('error-page.tpl')
->assign('error', ['title' => 'Add your captcha keys', 'message' => $message])->toString()
);
}
}

Expand Down Expand Up @@ -344,128 +346,6 @@ public static function redirectError($error, $permanent = false)
static::redirectTo(ROOT_LOCATION . sprintf("error.php?e=%d", (int)$error), $permanent);
}

/**
* Get url address
*
* @param bool $request_params retrieve the url tih the GET params
* @param bool $request_script_name retrieve the url with only the script name
*
* Possible usage: getCurrentUrl(true, false) - the default, get the full url
* getCurrentUrl(false, true) - get the url without the GET params only the script name
* getCurrentUrl(false, false) - get the url's directory path only
*
* @return string
*/
public static function getCurrentUrl($request_params = true, $request_script_name = false)
{
// begin buildup
$page_url = "http";

// add for ssl secured connections
if (static::isHTTPS())
{
$page_url .= "s";
}
$page_url .= "://";

// find the end part of the url
if ($request_params) // full url with requests
{
$end_url = $_SERVER["REQUEST_URI"];
}
elseif ($request_script_name) // full url without requests
{
$end_url = $_SERVER["SCRIPT_NAME"];
}
else // url directory path
{
$end_url = dirname($_SERVER["SCRIPT_NAME"]) . "/";
}

// add host
$page_url .= $_SERVER["SERVER_NAME"];

if ((int)$_SERVER["SERVER_PORT"] !== 80)
{
$page_url .= ":" . $_SERVER["SERVER_PORT"] . $end_url;
}
else
{
$page_url .= $end_url;
}

return $page_url;
}

/**
* Get an hash map of all the url vars where the key is the name
*
* @param string $query
*
* @return array
*/
public static function getQueryVars($query)
{
// build vars
$vars = [];
$hashes = explode("&", $query);
foreach ($hashes as $hash)
{
$hash = explode("=", $hash);
$len_hash = count($hash);

// At least the key exists
if ($len_hash != 0)
{
$key = $hash[0];

$value = '';
// the value exists
if ($len_hash >= 1)
$value = $hash[1];

// key => value
$vars[$key] = $value;
}
}

return $vars;
}

/**
* Removes an item or list from the query string.
*
* @param string|array $keys Query key or keys to remove.
* @param string $url
*
* @return string
*/
public static function removeQueryArguments(array $keys, $url)
{
$parsed = parse_url($url);
$url = rtrim($url, "?&");

// the query is empty
if (empty($parsed["query"]))
{
return $url . "?";
}

$vars = static::getQueryVars($parsed["query"]);

// remove query
foreach ($keys as $key)
{
unset($vars[$key]);
}

$query = empty($vars) ? "" : http_build_query($vars) . "&";

$new_url = $parsed["scheme"] . "://" . $parsed["host"] . $parsed["path"] . "?" . $query;

return $new_url;
}

/**
* Returns ip address of the client
*
Expand Down
4 changes: 2 additions & 2 deletions login.php
Expand Up @@ -33,8 +33,8 @@
}
else if (isset($_GET["return_to"]))
{
// decode the get
$return_to_url = urldecode($_GET["return_to"]);
// already decoded
$return_to_url = $_GET["return_to"];
}
// prevent foreign domain
if (!Util::str_starts_with($return_to_url, ROOT_LOCATION))
Expand Down
50 changes: 50 additions & 0 deletions tests/include/URLTest.php
@@ -0,0 +1,50 @@
<?php
/**
* Copyright 2017 SuperTuxKart-Team
*
* This file is part of stk-addons.
*
* stk-addons is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* stk-addons is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stk-addons. If not, see <http://www.gnu.org/licenses/>.
*/

class URLTest extends PHPUnit_Framework_TestCase
{
private static $valid_query_string = "second=value2&first=value&arr[0]=foo+bar&arr[1]=baz";

private static $valid_query_array = [
"second" => "value2",
"first" => "value",
"arr" => ["foo bar", "baz"]
];

public function testQueryStringToArray()
{
$valid_array = URL::queryStringToArray(static::$valid_query_string);
$this->assertInternalType("array", $valid_array);
$this->assertEquals(3, count($valid_array));
$this->assertArrayHasKey("second", $valid_array);
$this->assertArrayHasKey("arr", $valid_array);
$this->assertArrayHasKey("first", $valid_array);
$this->assertInternalType("array", $valid_array["arr"]);
$this->assertEquals(2, count($valid_array["arr"]));
$this->assertEquals($valid_array, static::$valid_query_array);
}

public function testQueryArrayToString()
{
$valid_string = URL::queryArrayToString(static::$valid_query_array);
$this->assertInternalType("string", $valid_string);
$this->assertEquals(URL::decode($valid_string), URL::decode(static::$valid_query_string));
}
}

0 comments on commit dc9366c

Please sign in to comment.