| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- <?php
- class frontend{
-
- public function load($template, $replacements = []){
-
- $replacements["server_name"] = htmlspecialchars(config::SERVER_NAME);
- $replacements["version"] = config::VERSION;
-
- if(isset($_COOKIE["theme"])){
-
- $theme = str_replace(["/". "."], "", $_COOKIE["theme"]);
-
- if(
- $theme != "Dark" &&
- !is_file("static/themes/" . $theme . ".css")
- ){
-
- $theme = config::DEFAULT_THEME;
- }
- }else{
-
- $theme = config::DEFAULT_THEME;
- }
-
- if($theme != "Dark"){
-
- $replacements["style"] = '<link rel="stylesheet" href="/static/themes/' . rawurlencode($theme) . '.css?v' . config::VERSION . '">';
- }else{
-
- $replacements["style"] = "";
- }
-
- if(isset($_COOKIE["scraper_ac"])){
-
- $replacements["ac"] = '?ac=' . htmlspecialchars($_COOKIE["scraper_ac"]);
- }else{
-
- $replacements["ac"] = '';
- }
-
- $handle = fopen("template/{$template}", "r");
- $data = fread($handle, filesize("template/{$template}"));
- fclose($handle);
-
- $data = explode("\n", $data);
- $html = "";
-
- for($i=0; $i<count($data); $i++){
-
- $html .= trim($data[$i]);
- }
-
- foreach($replacements as $key => $value){
-
- $html =
- str_replace(
- "{%{$key}%}",
- $value,
- $html
- );
- }
-
- return trim($html);
- }
-
- public function loadheader(array $get, array $filters, string $page){
-
- echo
- $this->load("header.html", [
- "title" => trim($get["s"] . " ({$page})"),
- "description" => ucfirst($page) . ' search results for "' . htmlspecialchars($get["s"]) . '"',
- "index" => "no",
- "search" => htmlspecialchars($get["s"]),
- "tabs" => $this->generatehtmltabs($page, $get["s"]),
- "filters" => $this->generatehtmlfilters($filters, $get)
- ]);
-
- if(
- preg_match(
- '/bot|wget|curl|python-requests|scrapy|feedfetcher|go-http-client|ruby|universalfeedparser|yahoo\! slurp|spider|rss/i',
- $_SERVER["HTTP_USER_AGENT"]
- )
- ){
-
- // bot detected !!
- apcu_inc("captcha_gen");
-
- $this->drawerror(
- "Tshh, blocked!",
- 'You were blocked from viewing this page. If you wish to scrape data from 4get, please consider running <a href="https://git.lolcat.ca/lolcat/4get" rel="noreferrer nofollow">your own 4get instance</a> or using <a href="/api.txt">the API</a>.',
- );
- die();
- }
- }
-
- public function drawerror($title, $error){
-
- echo
- $this->load("search.html", [
- "class" => "",
- "right-left" => "",
- "right-right" => "",
- "left" =>
- '<div class="infobox">' .
- '<h1>' . htmlspecialchars($title) . '</h1>' .
- $error .
- '</div>'
- ]);
- die();
- }
-
- public function drawscrapererror($error, $get, $target){
-
- $this->drawerror(
- "Shit",
- 'This scraper returned an error:' .
- '<div class="code">' . htmlspecialchars($error) . '</div>' .
- 'Things you can try:' .
- '<ul>' .
- '<li>Use a different scraper</li>' .
- '<li>Remove keywords that could cause errors</li>' .
- '<li><a href="/instances?target=' . $target . "&" . $this->buildquery($get, false) . '">Try your search on another 4get instance</a></li>' .
- '</ul><br>' .
- 'If the error persists, please <a href="/about">contact the administrator</a>.'
- );
- }
-
- public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
-
- $payload =
- '<div class="text-result">';
-
- // add favicon, link and archive links
- $payload .= $this->drawlink($site["url"]);
-
- /*
- Draw title + description + filetype
- */
- $payload .=
- '<a href="' . htmlspecialchars($site["url"]) . '" class="hover" rel="noreferrer nofollow"';
-
- if($tabindex === false){
-
- $payload .= ' tabindex="-1"';
- }
-
- $payload .= '>';
-
- if($site["thumb"]["url"] !== null){
-
- $payload .=
- '<div class="thumb-wrap';
-
- switch($site["thumb"]["ratio"]){
-
- case "16:9":
- $size = "landscape";
- break;
-
- case "9:16":
- $payload .= " portrait";
- $size = "portrait";
- break;
-
- case "1:1":
- $payload .= " square";
- $size = "square";
- break;
- }
-
- $payload .=
- '">' .
- '<img class="thumb" src="' . $this->htmlimage($site["thumb"]["url"], $size) . '" alt="thumb">';
-
- if($duration !== null){
-
- $payload .=
- '<div class="duration">' .
- htmlspecialchars($duration) .
- '</div>';
- }
-
- $payload .=
- '</div>';
- }
-
- $payload .=
- '<div class="title">';
-
- if(
- isset($site["type"]) &&
- $site["type"] != "web"
- ){
-
- $payload .= '<div class="type">' . strtoupper($site["type"]) . '</div>';
- }
-
- $payload .=
- $this->highlighttext($keywords, $site["title"]) .
- '</div>';
-
- if($greentext !== null){
-
- $payload .=
- '<div class="greentext">' .
- htmlspecialchars($greentext) .
- '</div>';
- }
-
- if($site["description"] !== null){
-
- $payload .=
- '<div class="description">' .
- $this->highlighttext($keywords, $site["description"]) .
- '</div>';
- }
-
- $payload .= $customhtml;
-
- $payload .= '</a>';
-
- /*
- Sublinks
- */
- if(
- isset($site["sublink"]) &&
- !empty($site["sublink"])
- ){
-
- usort($site["sublink"], function($a, $b){
-
- return strlen($a["description"]) > strlen($b["description"]);
- });
-
- $payload .=
- '<div class="sublinks">' .
- '<table>';
-
- $opentr = false;
- for($i=0; $i<count($site["sublink"]); $i++){
-
- if(($i % 2) === 0){
-
- $opentr = true;
- $payload .= '<tr>';
- }else{
-
- $opentr = false;
- }
-
- $payload .=
- '<td>' .
- '<a href="' . htmlspecialchars($site["sublink"][$i]["url"]) . '" rel="noreferrer nofollow">' .
- '<div class="title">' .
- htmlspecialchars($site["sublink"][$i]["title"]) .
- '</div>';
-
- if(!empty($site["sublink"][$i]["date"])){
-
- $payload .=
- '<div class="greentext">' .
- date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
- '</div>';
- }
-
- if(!empty($site["sublink"][$i]["description"])){
-
- $payload .=
- '<div class="description">' .
- $this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
- '</div>';
- }
-
- $payload .= '</a></td>';
-
- if($opentr === false){
-
- $payload .= '</tr>';
- }
- }
-
- if($opentr === true){
-
- $payload .= '<td></td></tr>';
- }
-
- $payload .= '</table></div>';
- }
-
- if(
- isset($site["table"]) &&
- !empty($site["table"])
- ){
-
- $payload .= '<table class="info-table">';
-
- foreach($site["table"] as $title => $value){
-
- $payload .=
- '<tr>' .
- '<td>' . htmlspecialchars($title) . '</td>' .
- '<td>' . htmlspecialchars($value) . '</td>' .
- '</tr>';
- }
-
- $payload .= '</table>';
- }
-
- return $payload . '</div>';
- }
-
- public function highlighttext($keywords, $text){
-
- $text = htmlspecialchars($text);
-
- $keywords = explode(" ", $keywords);
- $regex = [];
-
- foreach($keywords as $word){
-
- $regex[] = "\b" . preg_quote($word, "/") . "\b";
- }
-
- $regex = "/" . implode("|", $regex) . "/i";
-
- return
- preg_replace(
- $regex,
- '<b>${0}</b>',
- $text
- );
- }
-
- function highlightcode($text){
-
- // https://www.php.net/highlight_string
- ini_set("highlight.comment", "c-comment");
- ini_set("highlight.default", "c-default");
- ini_set("highlight.html", "c-default");
- ini_set("highlight.keyword", "c-keyword");
- ini_set("highlight.string", "c-string");
-
- $text =
- trim(
- preg_replace(
- '/<\/span>$/',
- "", // remove stray ending span because of the <?php stuff
- str_replace(
- [
- '<br />',
- ' '
- ],
- [
- "\n", // replace <br> with newlines
- " " // replace html entity to space
- ],
- str_replace(
- [
- // leading <?php garbage
- "<span style=\"color: c-default\">\n<?php ",
- "<code>",
- "</code>"
- ],
- "",
- highlight_string("<?php " . $text, true)
- )
- )
- )
- );
-
- // replace colors
- $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
-
- foreach($classes as $class){
-
- $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
- }
-
- return $text;
- }
-
- public function drawlink($link){
-
- /*
- Add favicon
- */
- $host = parse_url($link);
- $esc =
- explode(
- ".",
- $host["host"],
- 2
- );
-
- if(
- count($esc) === 2 &&
- $esc[0] == "www"
- ){
-
- $esc = $esc[1];
- }else{
-
- $esc = $esc[0];
- }
-
- $esc = substr($esc, 0, 2);
-
- $urlencode = urlencode($link);
-
- $payload =
- '<div class="url">' .
- '<button class="favicon" tabindex="-1">' .
- '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
- //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
- '</button>' .
- '<div class="favicon-dropdown">';
-
- /*
- Add archive links
- */
- if(
- $host["host"] == "boards.4chan.org" ||
- $host["host"] == "boards.4channel.org"
- ){
-
- $archives = [];
- $path = explode("/", $host["path"]);
- $count = count($path);
- // /pol/thread/417568063/post-shitty-memes-if-you-want-to
-
- if($count !== 0){
-
- $isboard = true;
-
- switch($path[1]){
-
- case "con":
- break;
-
- case "q":
- $archives[] = "desuarchive.org";
- break;
-
- case "qa":
- $archives[] = "desuarchive.org";
- break;
-
- case "qb":
- $archives[] = "arch.b4k.co";
- break;
-
- case "trash":
- $archives[] = "desuarchive.org";
- break;
-
- case "a":
- $archives[] = "desuarchive.org";
- break;
-
- case "c":
- $archives[] = "desuarchive.org";
- break;
-
- case "w":
- break;
-
- case "m":
- $archives[] = "desuarchive.org";
- break;
-
- case "cgl":
- $archives[] = "desuarchive.org";
- $archives[] = "warosu.org";
- break;
-
- case "cm":
- $archives[] = "boards.fireden.net";
- break;
-
- case "f":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "n":
- break;
-
- case "jp":
- $archives[] = "warosu.org";
- break;
-
- case "vt":
- $archives[] = "warosu.org";
- break;
-
- case "v":
- $archives[] = "boards.fireden.net";
- $archives[] = "arch.b4k.co";
- break;
-
- case "vg":
- $archives[] = "boards.fireden.net";
- $archives[] = "arch.b4k.co";
- break;
-
- case "vm":
- $archives[] = "arch.b4k.co";
- break;
-
- case "vmg":
- $archives[] = "arch.b4k.co";
- break;
-
- case "vp":
- $archives[] = "arch.b4k.co";
- break;
-
- case "vr":
- $archives[] = "desuarchive.org";
- $archives[] = "warosu.org";
- break;
-
- case "vrpg":
- $archives[] = "arch.b4k.co";
- break;
-
- case "vst":
- $archives[] = "arch.b4k.co";
- break;
-
- case "co":
- $archives[] = "desuarchive.org";
- break;
-
- case "g":
- $archives[] = "desuarchive.org";
- $archives[] = "arch.b4k.co";
- break;
-
- case "tv":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "k":
- $archives[] = "desuarchive.org";
- break;
-
- case "o":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "an":
- $archives[] = "desuarchive.org";
- break;
-
- case "tg":
- $archives[] = "desuarchive.org";
- $archives[] = "archive.4plebs.org";
- break;
-
- case "sp":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "xs":
- $archives[] = "eientei.xyz";
- break;
-
- case "pw":
- break;
-
- case "sci":
- $archives[] = "boards.fireden.net";
- $archives[] = "warosu.org";
- $archives[] = "eientei.xyz";
- break;
-
- case "his":
- $archives[] = "desuarchive.org";
- break;
-
- case "int":
- $archives[] = "desuarchive.org";
- break;
-
- case "out":
- break;
-
- case "toy":
- break;
-
- case "i":
- $archives[] = "archiveofsins.com";
- $archives[] = "eientei.xyz";
- break;
-
- case "po":
- break;
-
- case "p":
- break;
-
- case "ck":
- $archives[] = "warosu.org";
- break;
-
- case "ic":
- $archives[] = "boards.fireden.net";
- $archives[] = "warosu.org";
- break;
-
- case "wg":
- break;
-
- case "lit":
- $archives[] = "warosu.org";
- break;
-
- case "mu":
- $archives[] = "desuarchive.org";
- break;
-
- case "fa":
- $archives[] = "warosu.org";
- break;
-
- case "3":
- $archives[] = "warosu.org";
- $archives[] = "eientei.xyz";
- break;
-
- case "gd":
- break;
-
- case "diy":
- $archives[] = "warosu.org";
- break;
-
- case "wsg":
- $archives[] = "desuarchive.org";
- break;
-
- case "qst":
- break;
-
- case "biz":
- $archives[] = "warosu.org";
- break;
-
- case "trv":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "fit":
- $archives[] = "desuarchive.org";
- break;
-
- case "x":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "adv":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "lgbt":
- $archives[] = "archiveofsins.com";
- break;
-
- case "mlp":
- $archives[] = "desuarchive.org";
- $archives[] = "arch.b4k.co";
- break;
-
- case "news":
- break;
-
- case "wsr":
- break;
-
- case "vip":
- break;
-
- case "b":
- $archives[] = "thebarchive.com";
- break;
-
- case "r9k":
- $archives[] = "desuarchive.org";
- break;
-
- case "pol":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "bant":
- $archives[] = "thebarchive.com";
- break;
-
- case "soc":
- $archives[] = "archiveofsins.com";
- break;
-
- case "s4s":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "s":
- $archives[] = "archiveofsins.com";
- break;
-
- case "hc":
- $archives[] = "archiveofsins.com";
- break;
-
- case "hm":
- $archives[] = "archiveofsins.com";
- break;
-
- case "h":
- $archives[] = "archiveofsins.com";
- break;
-
- case "e":
- break;
-
- case "u":
- $archives[] = "archiveofsins.com";
- break;
-
- case "d":
- $archives[] = "desuarchive.org";
- break;
-
- case "y":
- $archives[] = "boards.fireden.net";
- break;
-
- case "t":
- $archives[] = "archiveofsins.com";
- break;
-
- case "hr":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "gif":
- break;
-
- case "aco":
- $archives[] = "desuarchive.org";
- break;
-
- case "r":
- $archives[] = "archiveofsins.com";
- break;
-
- default:
- $isboard = false;
- break;
- }
-
- if($isboard === true){
-
- $archives[] = "archived.moe";
- }
-
- $trail = "";
-
- if(
- isset($path[2]) &&
- isset($path[3]) &&
- $path[2] == "thread"
- ){
-
- $trail .= "/" . $path[1] . "/thread/" . $path[3];
- }elseif($isboard){
-
- $trail = "/" . $path[1] . "/";
- }
-
- for($i=0; $i<count($archives); $i++){
-
- $payload .=
- '<a href="https://' . $archives[$i] . $trail . '" class="list" target="_BLANK">' .
- '<img src="/favicon?s=https://' . $archives[$i] . '" alt="' . $archives[$i][0] . $archives[$i][1] . '">' .
- $archives[$i] .
- '</a>';
- }
- }
- }
-
- $payload .=
- '<a href="https://webcache.googleusercontent.com/search?q=cache:' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://google.com" alt="go">Google cache</a>' .
- '<a href="https://web.archive.org/web/' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.org" alt="ar">Archive.org</a>' .
- '<a href="https://archive.is/newest/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.is" alt="ar">Archive.is</a>' .
- '<a href="https://ghostarchive.org/search?term=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://ghostarchive.org" alt="gh">Ghostarchive</a>' .
- '<a href="https://www.bing.com/search?q=url%3A' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://bing.com" alt="bi">Bing cache</a>' .
- '<a href="https://megalodon.jp/?url=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://megalodon.jp" alt="me">Megalodon</a>' .
- '</div>';
-
- /*
- Draw link
- */
- $parts = explode("/", $link);
- $clickurl = "";
-
- // remove trailing /
- $c = count($parts) - 1;
- if($parts[$c] == ""){
-
- $parts[$c - 1] = $parts[$c - 1] . "/";
- unset($parts[$c]);
- }
-
- // merge https://site together
- $parts = [
- $parts[0] . $parts[1] . '//' . $parts[2],
- ...array_slice($parts, 3, count($parts) - 1)
- ];
-
- $c = count($parts);
- for($i=0; $i<$c; $i++){
-
- if($i !== 0){ $clickurl .= "/"; }
-
- $clickurl .= $parts[$i];
-
- if($i === $c - 1){
-
- $parts[$i] = rtrim($parts[$i], "/");
- }
-
- $payload .=
- '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
- htmlspecialchars(urldecode($parts[$i])) .
- '</a>';
-
- if($i !== $c - 1){
-
- $payload .= '<span class="separator"></span>';
- }
- }
-
- return $payload . '</div>';
- }
-
- public function getscraperfilters($page){
-
- $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
-
- if(
- isset($_GET["scraper"]) &&
- is_string($_GET["scraper"])
- ){
-
- $get_scraper = $_GET["scraper"];
- }else{
-
- if(
- isset($_GET["npt"]) &&
- is_string($_GET["npt"])
- ){
-
- $get_scraper = explode(".", $_GET["npt"], 2)[0];
-
- $get_scraper =
- preg_replace(
- '/[0-9]+$/',
- "",
- $get_scraper
- );
- }
- }
-
- // add search field
- $filters =
- [
- "s" => [
- "option" => "_SEARCH"
- ]
- ];
-
- // define default scrapers
- switch($page){
-
- case "web":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "ddg" => "DuckDuckGo",
- "brave" => "Brave",
- "yandex" => "Yandex",
- "google" => "Google",
- "mojeek" => "Mojeek",
- "marginalia" => "Marginalia",
- "wiby" => "wiby"
- ]
- ];
- break;
-
- case "images":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "ddg" => "DuckDuckGo",
- "yandex" => "Yandex",
- "brave" => "Brave",
- "google" => "Google",
- "yep" => "Yep",
- //"pinterest" => "Pinterest",
- "imgur" => "Imgur",
- "ftm" => "FindThatMeme"
- ]
- ];
- break;
-
- case "videos":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "yt" => "YouTube",
- //"fb" => "Facebook videos",
- "ddg" => "DuckDuckGo",
- "brave" => "Brave",
- "yandex" => "Yandex",
- "google" => "Google"
- ]
- ];
- break;
-
- case "news":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "ddg" => "DuckDuckGo",
- "brave" => "Brave",
- "google" => "Google",
- "mojeek" => "Mojeek"
- ]
- ];
- break;
-
- case "music":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "sc" => "SoundCloud"
- ]
- ];
- break;
- }
-
- // get scraper name from user input, or default out to preferred scraper
- $scraper_out = null;
- $first = true;
-
- foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
-
- if($first === true){
-
- $first = $scraper_name;
- }
-
- if($scraper_name == $get_scraper){
-
- $scraper_out = $scraper_name;
- }
- }
-
- if($scraper_out === null){
-
- $scraper_out = $first;
- }
-
- switch($scraper_out){
-
- case "ddg":
- include "scraper/ddg.php";
- $lib = new ddg();
- break;
-
- case "brave":
- include "scraper/brave.php";
- $lib = new brave();
- break;
-
- case "yt";
- include "scraper/youtube.php";
- $lib = new youtube();
- break;
-
- case "yandex":
- include "scraper/yandex.php";
- $lib = new yandex();
- break;
-
- case "google":
- include "scraper/google.php";
- $lib = new google();
- break;
- /*
- case "fb":
- include "scraper/facebook.php";
- $lib = new facebook();
- break;*/
-
- case "mojeek":
- include "scraper/mojeek.php";
- $lib = new mojeek();
- break;
-
- case "marginalia":
- include "scraper/marginalia.php";
- $lib = new marginalia();
- break;
-
- case "wiby":
- include "scraper/wiby.php";
- $lib = new wiby();
- break;
-
- case "yep":
- include "scraper/yep.php";
- $lib = new yep();
- break;
-
- case "sc":
- include "scraper/sc.php";
- $lib = new sc();
- break;
-
- case "pinterest":
- include "scraper/pinterest.php";
- $lib = new pinterest();
- break;
-
- case "imgur":
- include "scraper/imgur.php";
- $lib = new imgur();
- break;
-
- case "ftm":
- include "scraper/ftm.php";
- $lib = new ftm();
- break;
- }
-
- // set scraper on $_GET
- $_GET["scraper"] = $scraper_out;
-
- // set nsfw on $_GET
- if(
- isset($_COOKIE["nsfw"]) &&
- !isset($_GET["nsfw"])
- ){
-
- $_GET["nsfw"] = $_COOKIE["nsfw"];
- }
-
- return
- [
- $lib,
- array_merge_recursive(
- $filters,
- $lib->getfilters($page)
- )
- ];
- }
-
- public function parsegetfilters($parameters, $whitelist){
-
- $sanitized = [];
-
- // add npt token
- if(
- isset($parameters["npt"]) &&
- is_string($parameters["npt"])
- ){
-
- $sanitized["npt"] = $parameters["npt"];
- }else{
-
- $sanitized["npt"] = false;
- }
-
- // we're iterating over $whitelist, so
- // you can't polluate $sanitized with useless
- // parameters
- foreach($whitelist as $parameter => $value){
-
- if(isset($parameters[$parameter])){
-
- if(!is_string($parameters[$parameter])){
-
- $sanitized[$parameter] = null;
- continue;
- }
-
- // parameter is already set, use that value
- $sanitized[$parameter] = $parameters[$parameter];
- }else{
-
- // parameter is not set, add it
- if(is_string($value["option"])){
-
- // special field: set default value manually
- switch($value["option"]){
-
- case "_DATE":
- // no date set
- $sanitized[$parameter] = false;
- break;
-
- case "_SEARCH":
- // no search set
- $sanitized[$parameter] = "";
- break;
- }
-
- }else{
-
- // set a default value
- $sanitized[$parameter] = array_keys($value["option"])[0];
- }
- }
-
- // sanitize input
- if(is_array($value["option"])){
- if(
- !in_array(
- $sanitized[$parameter],
- $keys = array_keys($value["option"])
- )
- ){
-
- $sanitized[$parameter] = $keys[0];
- }
- }else{
-
- // sanitize search & string
- switch($value["option"]){
-
- case "_DATE":
- if($sanitized[$parameter] !== false){
-
- $sanitized[$parameter] = strtotime($sanitized[$parameter]);
- if($sanitized[$parameter] <= 0){
-
- $sanitized[$parameter] = false;
- }
- }
- break;
-
- case "_SEARCH":
- // get search string
- $sanitized["s"] = trim($sanitized[$parameter]);
- }
- }
- }
-
- // invert dates if needed
- if(
- isset($sanitized["older"]) &&
- isset($sanitized["newer"]) &&
- $sanitized["newer"] !== false &&
- $sanitized["older"] !== false &&
- $sanitized["newer"] > $sanitized["older"]
- ){
-
- // invert
- [
- $sanitized["older"],
- $sanitized["newer"]
- ] = [
- $sanitized["newer"],
- $sanitized["older"]
- ];
- }
-
- return $sanitized;
- }
- public function s_to_timestamp($seconds){
-
- if(is_string($seconds)){
-
- return "LIVE";
- }
-
- return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
- }
-
- public function generatehtmltabs($page, $query){
-
- $html = null;
-
- foreach(["web", "images", "videos", "news", "music"] as $type){
-
- $html .= '<a href="/' . $type . '?s=' . urlencode($query);
-
- if(!empty($params)){
-
- $html .= $params;
- }
-
- $html .= '" class="tab';
-
- if($type == $page){
-
- $html .= ' selected';
- }
-
- $html .= '">' . ucfirst($type) . '</a>';
- }
-
- return $html;
- }
-
- public function generatehtmlfilters($filters, $params){
-
- $html = null;
-
- foreach($filters as $filter_name => $filter_values){
-
- if(!isset($filter_values["display"])){
-
- continue;
- }
-
- $output = true;
- $tmp =
- '<div class="filter">' .
- '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
-
- if(is_array($filter_values["option"])){
-
- $tmp .= '<select name="' . $filter_name . '">';
-
- foreach($filter_values["option"] as $option_name => $option_title){
-
- $tmp .= '<option value="' . $option_name . '"';
-
- if($params[$filter_name] == $option_name){
-
- $tmp .= ' selected';
- }
-
- $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
- }
-
- $tmp .= '</select>';
- }else{
-
- switch($filter_values["option"]){
-
- case "_DATE":
- $tmp .= '<input type="date" name="' . $filter_name . '"';
-
- if($params[$filter_name] !== false){
-
- $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
- }
-
- $tmp .= '>';
- break;
-
- default:
- $output = false;
- break;
- }
- }
-
- $tmp .= '</div>';
-
- if($output === true){
-
- $html .= $tmp;
- }
- }
-
- return $html;
- }
-
- public function buildquery($gets, $ommit = false){
-
- $out = [];
- foreach($gets as $key => $value){
-
- if(
- $value == null ||
- $value == false ||
- $key == "npt" ||
- $key == "extendedsearch" ||
- $value == "any" ||
- $value == "all" ||
- $key == "spellcheck" ||
- (
- $ommit === true &&
- $key == "s"
- )
- ){
-
- continue;
- }
-
- if(
- $key == "older" ||
- $key == "newer"
- ){
-
- $value = date("Y-m-d", (int)$value);
- }
-
- $out[$key] = $value;
- }
-
- return http_build_query($out);
- }
-
- public function htmlimage($image, $format){
-
- if(
- preg_match(
- '/^data:/',
- $image
- )
- ){
-
- return htmlspecialchars($image);
- }
-
- return "/proxy?i=" . urlencode($image) . "&s=" . $format;
- }
-
- public function htmlnextpage($gets, $npt, $page){
-
- $query = $this->buildquery($gets);
-
- return $page . "?" . $query . "&npt=" . $npt;
- }
- }
|