This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/urls.php

144 lines
3.3 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class Link
{
public ?string $page;
public ?string $query;
2023-11-11 21:49:12 +00:00
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);
}
}
/**
* Build a link to a search page for given terms,
* with all the appropriate escaping
2024-01-15 15:08:22 +00:00
*
* @param string[] $terms
*/
function search_link(array $terms = [], int $page = 1): string
{
if($terms) {
$q = url_escape(Tag::implode($terms));
return make_link("post/list/$q/$page");
} else {
return make_link("post/list/$page");
}
}
/**
* Figure out the correct way to link to a page, taking into account
* things like the nice URLs setting.
*
* eg make_link("foo/bar") becomes "/v2/index.php?q=foo/bar"
*/
2023-11-11 21:49:12 +00:00
function make_link(?string $page = null, ?string $query = null, ?string $fragment = null): string
{
global $config;
if (is_null($page)) {
2019-08-02 19:40:03 +00:00
$page = $config->get_string(SetupConfig::MAIN_PAGE);
}
2020-03-28 14:11:14 +00:00
$page = trim($page, "/");
2020-03-28 14:11:14 +00:00
$parts = [];
2020-02-01 22:26:08 +00:00
$install_dir = get_base_href();
if (SPEED_HAX || $config->get_bool('nice_urls', false)) {
2020-03-28 14:11:14 +00:00
$parts['path'] = "$install_dir/$page";
} 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";
}
2020-03-28 14:11:14 +00:00
$parts['query'] = $query; // http_build_query($query);
$parts['fragment'] = $fragment; // http_build_query($hash);
2020-03-28 14:11:14 +00:00
return unparse_url($parts);
}
/**
* Take the current URL and modify some parameters
2024-01-15 15:08:22 +00:00
*
* @param array<string, mixed> $changes
*/
function modify_current_url(array $changes): string
{
2020-03-27 23:35:07 +00:00
return modify_url($_SERVER['REQUEST_URI'], $changes);
}
2024-01-15 15:08:22 +00:00
/**
* Take a URL and modify some parameters
*
* @param array<string, mixed> $changes
*/
function modify_url(string $url, array $changes): string
{
2020-03-27 23:35:07 +00:00
$parts = parse_url($url);
$params = [];
2020-04-25 20:36:28 +00:00
if (isset($parts['query'])) {
parse_str($parts['query'], $params);
}
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);
2020-03-27 23:35:07 +00:00
return unparse_url($parts);
}
/**
* Turn a relative link into an absolute one, including hostname
*/
function make_http(string $link): string
{
if (str_contains($link, "://")) {
return $link;
}
if (strlen($link) > 0 && $link[0] != '/') {
$link = get_base_href() . '/' . $link;
}
$protocol = is_https_enabled() ? "https://" : "http://";
$link = $protocol . $_SERVER["HTTP_HOST"] . $link;
$link = str_replace("/./", "/", $link);
return $link;
}
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
2024-01-15 15:08:22 +00:00
*
* @param string[]|null $blacklist
2020-03-27 23:35:07 +00:00
*/
2023-11-11 21:49:12 +00:00
function referer_or(string $dest, ?array $blacklist = null): string
2020-03-27 19:41:34 +00:00
{
2020-04-25 20:36:28 +00:00
if (empty($_SERVER['HTTP_REFERER'])) {
return $dest;
}
if ($blacklist) {
foreach ($blacklist as $b) {
if (str_contains($_SERVER['HTTP_REFERER'], $b)) {
2020-04-25 20:36:28 +00:00
return $dest;
}
2020-03-27 20:24:26 +00:00
}
}
return $_SERVER['HTTP_REFERER'];
2020-03-27 19:41:34 +00:00
}