2022-02-24 02:30:15 -08:00
|
|
|
<?php
|
2023-08-23 02:55:18 -07:00
|
|
|
function get_base_url($url) {
|
2022-02-24 02:30:15 -08:00
|
|
|
$split_url = explode("/", $url);
|
|
|
|
$base_url = $split_url[0] . "//" . $split_url[2] . "/";
|
|
|
|
return $base_url;
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:55:18 -07:00
|
|
|
function get_root_domain($url) {
|
2023-08-29 11:07:50 -07:00
|
|
|
return parse_url($url, PHP_URL_HOST);
|
2023-01-27 09:17:57 -08:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:38:59 -07:00
|
|
|
function try_replace_with_frontend($url, $frontend, $original, $opts) {
|
2023-08-22 15:55:11 -07:00
|
|
|
$frontends = $opts->frontends;
|
2022-04-21 02:02:32 -07:00
|
|
|
|
2023-08-23 02:55:18 -07:00
|
|
|
if (array_key_exists($frontend, $opts->frontends)) {
|
|
|
|
$frontend = $frontends[$frontend]["instance_url"];
|
2022-03-02 04:12:09 -08:00
|
|
|
|
2022-09-16 01:23:57 -07:00
|
|
|
if (empty(trim($frontend)))
|
|
|
|
return $url;
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
if (strpos($url, "wikipedia.org") !== false) {
|
2022-12-14 00:58:03 -08:00
|
|
|
$wiki_split = explode(".", $url);
|
2023-08-22 15:55:11 -07:00
|
|
|
if (count($wiki_split) > 1) {
|
2022-12-14 00:58:03 -08:00
|
|
|
$lang = explode("://", $wiki_split[0])[1];
|
2023-03-14 04:12:47 -07:00
|
|
|
$url = $frontend . explode($original, $url)[1] . (strpos($url, "?") !== false ? "&" : "?") . "lang=" . $lang;
|
2022-12-14 00:58:03 -08:00
|
|
|
}
|
2023-08-22 15:55:11 -07:00
|
|
|
} else if (strpos($url, "fandom.com") !== false) {
|
2022-12-28 02:45:09 -08:00
|
|
|
$fandom_split = explode(".", $url);
|
2023-08-22 15:55:11 -07:00
|
|
|
if (count($fandom_split) > 1) {
|
2022-12-28 02:45:09 -08:00
|
|
|
$wiki_name = explode("://", $fandom_split[0])[1];
|
|
|
|
$url = $frontend . "/" . $wiki_name . explode($original, $url)[1];
|
|
|
|
}
|
2023-08-22 15:55:11 -07:00
|
|
|
} else if (strpos($url, "gist.github.com") !== false) {
|
2023-04-22 14:07:25 -07:00
|
|
|
$gist_path = explode("gist.github.com", $url)[1];
|
|
|
|
$url = $frontend . "/gist" . $gist_path;
|
2023-08-22 15:55:11 -07:00
|
|
|
} else if (strpos($url, "stackexchange.com") !== false) {
|
2023-05-28 14:47:35 -07: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 15:55:11 -07:00
|
|
|
} else {
|
2022-12-14 00:58:03 -08:00
|
|
|
$url = $frontend . explode($original, $url)[1];
|
|
|
|
}
|
2022-03-24 03:28:12 -07:00
|
|
|
|
2022-12-28 02:45:09 -08:00
|
|
|
|
2022-03-24 03:28:12 -07:00
|
|
|
return $url;
|
2022-03-02 04:12:09 -08:00
|
|
|
}
|
2022-03-24 03:28:12 -07:00
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
function check_for_privacy_frontend($url, $opts) {
|
|
|
|
if ($opts->disable_frontends)
|
2022-09-16 01:23:57 -07:00
|
|
|
return $url;
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
foreach($opts->frontends as $frontend => $data) {
|
2023-02-17 00:44:03 -08:00
|
|
|
$original = $data["original_url"];
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
if (strpos($url, $original)) {
|
2023-08-22 16:38:59 -07:00
|
|
|
$url = try_replace_with_frontend($url, $frontend, $original, $opts);
|
2022-07-08 12:52:26 -07:00
|
|
|
break;
|
2023-08-22 15:55:11 -07:00
|
|
|
} else if (strpos($url, "stackexchange.com")) {
|
2023-08-22 16:38:59 -07:00
|
|
|
$url = try_replace_with_frontend($url, "anonymousoverflow", "stackexchange.com", $opts);
|
2023-05-28 14:47:35 -07:00
|
|
|
break;
|
|
|
|
}
|
2022-07-08 12:52:26 -07:00
|
|
|
}
|
2022-03-02 04:12:09 -08:00
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
function get_xpath($response) {
|
2023-08-22 16:38:59 -07:00
|
|
|
if (!$response)
|
|
|
|
return null;
|
|
|
|
|
2022-02-25 01:42:32 -08:00
|
|
|
$htmlDom = new DOMDocument;
|
|
|
|
@$htmlDom->loadHTML($response);
|
|
|
|
$xpath = new DOMXPath($htmlDom);
|
2022-02-24 02:30:15 -08:00
|
|
|
|
2022-02-25 01:42:32 -08:00
|
|
|
return $xpath;
|
2022-02-24 02:30:15 -08:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
function request($url) {
|
|
|
|
$config ??= require "config.php";
|
2022-02-24 13:29:10 -08:00
|
|
|
|
2022-02-25 01:42:32 -08:00
|
|
|
$ch = curl_init($url);
|
2022-03-11 01:04:36 -08:00
|
|
|
curl_setopt_array($ch, $config->curl_settings);
|
2022-02-25 01:42:32 -08:00
|
|
|
$response = curl_exec($ch);
|
2022-02-24 13:29:10 -08:00
|
|
|
|
2022-02-25 01:42:32 -08:00
|
|
|
return $response;
|
2022-02-24 02:30:15 -08:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
function human_filesize($bytes, $dec = 2) {
|
2022-02-25 11:26:15 -08: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 15:55:11 -07:00
|
|
|
function remove_special($string) {
|
2022-11-20 10:00:45 -08:00
|
|
|
$string = preg_replace("/[\r\n]+/", "\n", $string);
|
|
|
|
return trim(preg_replace("/\s+/", ' ', $string));
|
2022-03-04 01:36:31 -08:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
function print_elapsed_time($start_time) {
|
2022-03-04 01:36:31 -08: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 15:55:11 -07:00
|
|
|
function print_next_page_button($text, $page, $query, $type) {
|
2022-07-15 06:50:23 -07:00
|
|
|
echo "<form class=\"page\" action=\"search.php\" target=\"_top\" method=\"get\" autocomplete=\"off\">";
|
2022-02-24 02:30:15 -08:00
|
|
|
echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
|
2022-02-25 01:42:32 -08:00
|
|
|
echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
|
2022-10-25 04:02:01 -07:00
|
|
|
echo "<input type=\"hidden\" name=\"t\" value=\"$type\" />";
|
2022-02-25 01:42:32 -08:00
|
|
|
echo "<button type=\"submit\">$text</button>";
|
2022-03-15 03:37:21 -07:00
|
|
|
echo "</form>";
|
2022-02-24 02:30:15 -08:00
|
|
|
}
|
2023-08-07 09:01:12 -07:00
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
function copy_cookies($curl) {
|
2023-08-07 10:27:54 -07:00
|
|
|
if (array_key_exists("HTTP_COOKIE", $_SERVER))
|
|
|
|
curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] );
|
2023-08-07 09:01:12 -07:00
|
|
|
}
|
|
|
|
|
2023-08-16 14:26:31 -07:00
|
|
|
|
2023-08-22 15:55:11 -07:00
|
|
|
function get_country_emote($code) {
|
2023-08-16 14:26:31 -07: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 03:37:21 -07:00
|
|
|
?>
|