2022-02-24 05:30:15 -05:00
|
|
|
<?php
|
|
|
|
function get_base_url($url)
|
|
|
|
{
|
|
|
|
$split_url = explode("/", $url);
|
|
|
|
$base_url = $split_url[0] . "//" . $split_url[2] . "/";
|
|
|
|
return $base_url;
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:17:57 -05:00
|
|
|
function get_root_domain($url)
|
|
|
|
{
|
|
|
|
$split_url = explode("/", $url);
|
|
|
|
$base_url = $split_url[2];
|
|
|
|
|
|
|
|
$base_url_main_split = explode(".", strrev($base_url));
|
|
|
|
$root_domain = strrev($base_url_main_split[1]) . "." . strrev($base_url_main_split[0]);
|
|
|
|
|
|
|
|
return $root_domain;
|
|
|
|
}
|
|
|
|
|
2022-07-08 15:52:26 -04:00
|
|
|
function try_replace_with_frontend($url, $frontend, $original)
|
2022-03-02 07:12:09 -05:00
|
|
|
{
|
2023-02-17 03:44:03 -05:00
|
|
|
global $config;
|
2023-08-22 18:55:11 -04:00
|
|
|
$frontends = $opts->frontends;
|
|
|
|
$frontend = $opts->$frontend["instance_url"];
|
2022-04-21 05:02:32 -04:00
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
if ($frontend) {
|
2023-02-17 03:44:03 -05:00
|
|
|
|
2022-03-02 07:12:09 -05:00
|
|
|
|
2022-09-16 04:23:57 -04:00
|
|
|
if (empty(trim($frontend)))
|
|
|
|
return $url;
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
if (strpos($url, "wikipedia.org") !== false) {
|
2022-12-14 03:58:03 -05:00
|
|
|
$wiki_split = explode(".", $url);
|
2023-08-22 18:55:11 -04:00
|
|
|
if (count($wiki_split) > 1) {
|
2022-12-14 03:58:03 -05:00
|
|
|
$lang = explode("://", $wiki_split[0])[1];
|
2023-03-14 07:12:47 -04:00
|
|
|
$url = $frontend . explode($original, $url)[1] . (strpos($url, "?") !== false ? "&" : "?") . "lang=" . $lang;
|
2022-12-14 03:58:03 -05:00
|
|
|
}
|
2023-08-22 18:55:11 -04:00
|
|
|
} else if (strpos($url, "fandom.com") !== false) {
|
2022-12-28 05:45:09 -05:00
|
|
|
$fandom_split = explode(".", $url);
|
2023-08-22 18:55:11 -04:00
|
|
|
if (count($fandom_split) > 1) {
|
2022-12-28 05:45:09 -05:00
|
|
|
$wiki_name = explode("://", $fandom_split[0])[1];
|
|
|
|
$url = $frontend . "/" . $wiki_name . explode($original, $url)[1];
|
|
|
|
}
|
2023-08-22 18:55:11 -04:00
|
|
|
} else if (strpos($url, "gist.github.com") !== false) {
|
2023-04-22 17:07:25 -04:00
|
|
|
$gist_path = explode("gist.github.com", $url)[1];
|
|
|
|
$url = $frontend . "/gist" . $gist_path;
|
2023-08-22 18:55:11 -04:00
|
|
|
} else if (strpos($url, "stackexchange.com") !== false) {
|
2023-05-28 17:47:35 -04:00
|
|
|
$se_domain = explode(".", explode("://", $url)[1])[0];
|
|
|
|
$se_path = explode("stackexchange.com", $url)[1];
|
|
|
|
$url = $frontend . "/exchange" . "/" . $se_domain . $se_path;
|
2023-08-22 18:55:11 -04:00
|
|
|
} else {
|
2022-12-14 03:58:03 -05:00
|
|
|
$url = $frontend . explode($original, $url)[1];
|
|
|
|
}
|
2022-03-24 06:28:12 -04:00
|
|
|
|
2022-12-28 05:45:09 -05:00
|
|
|
|
2022-03-24 06:28:12 -04:00
|
|
|
return $url;
|
2022-03-02 07:12:09 -05:00
|
|
|
}
|
2022-03-24 06:28:12 -04:00
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function check_for_privacy_frontend($url, $opts) {
|
|
|
|
if ($opts->disable_frontends)
|
2022-09-16 04:23:57 -04:00
|
|
|
return $url;
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
foreach($opts->frontends as $frontend => $data) {
|
2023-02-17 03:44:03 -05:00
|
|
|
$original = $data["original_url"];
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
if (strpos($url, $original)) {
|
2022-07-08 15:52:26 -04:00
|
|
|
$url = try_replace_with_frontend($url, $frontend, $original);
|
|
|
|
break;
|
2023-08-22 18:55:11 -04:00
|
|
|
} else if (strpos($url, "stackexchange.com")) {
|
2023-05-28 17:47:35 -04:00
|
|
|
$url = try_replace_with_frontend($url, "anonymousoverflow", "stackexchange.com");
|
|
|
|
break;
|
|
|
|
}
|
2022-07-08 15:52:26 -04:00
|
|
|
}
|
2022-03-02 07:12:09 -05:00
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function get_xpath($response) {
|
2022-02-25 04:42:32 -05:00
|
|
|
$htmlDom = new DOMDocument;
|
|
|
|
@$htmlDom->loadHTML($response);
|
|
|
|
$xpath = new DOMXPath($htmlDom);
|
2022-02-24 05:30:15 -05:00
|
|
|
|
2022-02-25 04:42:32 -05:00
|
|
|
return $xpath;
|
2022-02-24 05:30:15 -05:00
|
|
|
}
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function request($url) {
|
|
|
|
$config ??= require "config.php";
|
2022-02-24 16:29:10 -05:00
|
|
|
|
2022-02-25 04:42:32 -05:00
|
|
|
$ch = curl_init($url);
|
2022-03-11 04:04:36 -05:00
|
|
|
curl_setopt_array($ch, $config->curl_settings);
|
2022-02-25 04:42:32 -05:00
|
|
|
$response = curl_exec($ch);
|
2022-02-24 16:29:10 -05:00
|
|
|
|
2022-02-25 04:42:32 -05:00
|
|
|
return $response;
|
2022-02-24 05:30:15 -05:00
|
|
|
}
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function human_filesize($bytes, $dec = 2) {
|
2022-02-25 14:26:15 -05:00
|
|
|
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
|
|
|
|
$factor = floor((strlen($bytes) - 1) / 3);
|
|
|
|
|
|
|
|
return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
|
|
|
|
}
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function remove_special($string) {
|
2022-11-20 13:00:45 -05:00
|
|
|
$string = preg_replace("/[\r\n]+/", "\n", $string);
|
|
|
|
return trim(preg_replace("/\s+/", ' ', $string));
|
2022-03-04 04:36:31 -05:00
|
|
|
}
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function print_elapsed_time($start_time) {
|
2022-03-04 04:36:31 -05:00
|
|
|
$end_time = number_format(microtime(true) - $start_time, 2, '.', '');
|
|
|
|
echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
|
|
|
|
}
|
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function print_next_page_button($text, $page, $query, $type) {
|
2022-07-15 09:50:23 -04:00
|
|
|
echo "<form class=\"page\" action=\"search.php\" target=\"_top\" method=\"get\" autocomplete=\"off\">";
|
2022-02-24 05:30:15 -05:00
|
|
|
echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
|
2022-02-25 04:42:32 -05:00
|
|
|
echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
|
2022-10-25 07:02:01 -04:00
|
|
|
echo "<input type=\"hidden\" name=\"t\" value=\"$type\" />";
|
2022-02-25 04:42:32 -05:00
|
|
|
echo "<button type=\"submit\">$text</button>";
|
2022-03-15 06:37:21 -04:00
|
|
|
echo "</form>";
|
2022-02-24 05:30:15 -05:00
|
|
|
}
|
2023-08-07 12:01:12 -04:00
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function copy_cookies($curl) {
|
2023-08-07 13:27:54 -04:00
|
|
|
if (array_key_exists("HTTP_COOKIE", $_SERVER))
|
|
|
|
curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] );
|
2023-08-07 12:01:12 -04:00
|
|
|
}
|
|
|
|
|
2023-08-16 17:26:31 -04:00
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function get_country_emote($code) {
|
2023-08-16 17:26:31 -04:00
|
|
|
$emoji = [];
|
|
|
|
foreach(str_split($code) as $c) {
|
|
|
|
if(($o = ord($c)) > 64 && $o % 32 < 27) {
|
|
|
|
$emoji[] = hex2bin("f09f87" . dechex($o % 32 + 165));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$emoji[] = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return join($emoji);
|
|
|
|
}
|
|
|
|
|
2022-03-15 06:37:21 -04:00
|
|
|
?>
|