2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-08-02 19:54:48 +00:00
|
|
|
class Link
|
|
|
|
{
|
|
|
|
public $page;
|
|
|
|
public $query;
|
|
|
|
|
|
|
|
public function __construct(?string $page=null, ?string $query=null)
|
|
|
|
{
|
|
|
|
$this->page = $page;
|
|
|
|
$this->query = $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function make_link(): string
|
|
|
|
{
|
|
|
|
return make_link($this->page, $this->query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 22:30:18 +00:00
|
|
|
/**
|
|
|
|
* Figure out the correct way to link to a page, taking into account
|
|
|
|
* things like the nice URLs setting.
|
|
|
|
*
|
|
|
|
* eg make_link("post/list") becomes "/v2/index.php?q=post/list"
|
|
|
|
*/
|
2020-03-28 14:11:14 +00:00
|
|
|
function make_link(?string $page=null, ?string $query=null, ?string $fragment=null): string
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if (is_null($page)) {
|
2019-08-02 19:40:03 +00:00
|
|
|
$page = $config->get_string(SetupConfig::MAIN_PAGE);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-03-28 14:11:14 +00:00
|
|
|
$page = trim($page, "/");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-03-28 14:11:14 +00:00
|
|
|
$parts = [];
|
2020-02-01 22:26:08 +00:00
|
|
|
$install_dir = get_base_href();
|
2020-02-09 16:26:57 +00:00
|
|
|
if (SPEED_HAX || $config->get_bool('nice_urls', false)) {
|
2020-03-28 14:11:14 +00:00
|
|
|
$parts['path'] = "$install_dir/$page";
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2020-03-28 14:11:14 +00:00
|
|
|
$parts['path'] = "$install_dir/index.php";
|
2020-04-02 21:38:50 +00:00
|
|
|
$query = empty($query) ? "q=$page" : "q=$page&$query";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-03-28 14:11:14 +00:00
|
|
|
$parts['query'] = $query; // http_build_query($query);
|
|
|
|
$parts['fragment'] = $fragment; // http_build_query($hash);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-03-28 14:11:14 +00:00
|
|
|
return unparse_url($parts);
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take the current URL and modify some parameters
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
function modify_current_url(array $changes): string
|
|
|
|
{
|
2020-03-27 23:35:07 +00:00
|
|
|
return modify_url($_SERVER['REQUEST_URI'], $changes);
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
function modify_url(string $url, array $changes): string
|
|
|
|
{
|
2020-03-27 23:35:07 +00:00
|
|
|
$parts = parse_url($url);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$params = [];
|
2020-03-27 23:35:07 +00:00
|
|
|
if(isset($parts['query'])) parse_str($parts['query'], $params);
|
2019-05-28 16:59:38 +00:00
|
|
|
foreach ($changes as $k => $v) {
|
|
|
|
if (is_null($v) and isset($params[$k])) {
|
|
|
|
unset($params[$k]);
|
|
|
|
}
|
|
|
|
$params[$k] = $v;
|
|
|
|
}
|
2020-03-27 23:35:07 +00:00
|
|
|
$parts['query'] = http_build_query($params);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-03-27 23:35:07 +00:00
|
|
|
return unparse_url($parts);
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a relative link into an absolute one, including hostname
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
function make_http(string $link): string
|
|
|
|
{
|
|
|
|
if (strpos($link, "://") > 0) {
|
|
|
|
return $link;
|
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (strlen($link) > 0 && $link[0] != '/') {
|
|
|
|
$link = get_base_href() . '/' . $link;
|
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$protocol = is_https_enabled() ? "https://" : "http://";
|
|
|
|
$link = $protocol . $_SERVER["HTTP_HOST"] . $link;
|
|
|
|
$link = str_replace("/./", "/", $link);
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
return $link;
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|
2020-03-27 19:41:34 +00:00
|
|
|
|
2020-03-27 23:35:07 +00:00
|
|
|
/**
|
|
|
|
* If HTTP_REFERER is set, and not blacklisted, then return it
|
|
|
|
* Else return a default $dest
|
|
|
|
*/
|
2020-03-27 20:24:26 +00:00
|
|
|
function referer_or(string $dest, ?array $blacklist=null): string
|
2020-03-27 19:41:34 +00:00
|
|
|
{
|
2020-03-27 20:24:26 +00:00
|
|
|
if(empty($_SERVER['HTTP_REFERER'])) return $dest;
|
|
|
|
if($blacklist) {
|
|
|
|
foreach($blacklist as $b) {
|
|
|
|
if(strstr($_SERVER['HTTP_REFERER'], $b)) return $dest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $_SERVER['HTTP_REFERER'];
|
2020-03-27 19:41:34 +00:00
|
|
|
}
|