12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340 |
- <?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"] = '';
- }
-
- if(
- isset($replacements["timetaken"]) &&
- $replacements["timetaken"] !== null
- ){
-
- $replacements["timetaken"] = '<div class="timetaken">Took ' . number_format(microtime(true) - $replacements["timetaken"], 2) . 's</div>';
- }
-
- $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(htmlspecialchars($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)
- ]);
-
- $headers_raw = getallheaders();
- $header_keys = [];
- $user_agent = "";
- $bad_header = false;
-
- // block bots that present X-Forwarded-For, Via, etc
- foreach($headers_raw as $headerkey => $headervalue){
-
- $headerkey = strtolower($headerkey);
- if($headerkey == "user-agent"){
-
- $user_agent = $headervalue;
- continue;
- }
-
- // check header key
- if(in_array($headerkey, config::FILTERED_HEADER_KEYS)){
-
- $bad_header = true;
- break;
- }
- }
-
- // SSL check
- $bad_ssl = false;
- if(
- isset($_SERVER["https"]) &&
- $_SERVER["https"] == "on" &&
- isset($_SERVER["SSL_CIPHER"]) &&
- in_array($_SERVER["SSL_CIPHER"], config::FILTERED_HEADER_KEYS)
- ){
-
- $bad_ssl = true;
- }
-
- if(
- $bad_header === true ||
- $bad_ssl === true ||
- $user_agent == "" ||
- // user agent check
- preg_match(
- config::HEADER_REGEX,
- $user_agent
- )
- ){
-
- // bot detected !!
- apcu_inc("captcha_gen");
-
- $this->drawerror(
- "Tshh, blocked!",
- 'Your browser, IP or IP range has been blocked from this 4get instance. If this is an error, please <a href="/about">contact the administrator</a>.'
- );
- die();
- }
- }
-
- public function drawerror($title, $error, $timetaken = null){
-
- if($timetaken === null){
-
- $timetaken = microtime(true);
- }
-
- echo
- $this->load("search.html", [
- "timetaken" => $timetaken,
- "class" => "",
- "right-left" => "",
- "right-right" => "",
- "left" =>
- '<div class="infobox">' .
- '<h1>' . htmlspecialchars($title) . '</h1>' .
- $error .
- '</div>'
- ]);
- die();
- }
-
- public function drawscrapererror($error, $get, $target, $timetaken = null){
-
- if($timetaken === null){
-
- $timetaken = microtime(true);
- }
-
- $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>.',
- $timetaken
- );
- }
-
- 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 "f":
- $archives[] = "archive.4plebs.org";
- break;
-
- case "n":
- break;
-
- case "jp":
- $archives[] = "warosu.org";
- break;
-
- case "vt":
- $archives[] = "warosu.org";
- break;
-
- case "v":
- $archives[] = "arch.b4k.co";
- break;
-
- case "vg":
- $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[] = "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[] = "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 "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://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.ph/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://arquivo.pt/wayback/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://arquivo.pt" alt="ar">Arquivo.pt</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",
- "google_cse" => "Google CSE",
- "startpage" => "Startpage",
- "qwant" => "Qwant",
- "ghostery" => "Ghostery",
- "yep" => "Yep",
- "greppr" => "Greppr",
- "crowdview" => "Crowdview",
- "mwmbl" => "Mwmbl",
- "mojeek" => "Mojeek",
- "solofield" => "Solofield",
- "marginalia" => "Marginalia",
- "wiby" => "wiby",
- "curlie" => "Curlie"
- ]
- ];
- break;
-
- case "images":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "ddg" => "DuckDuckGo",
- "yandex" => "Yandex",
- "brave" => "Brave",
- "google" => "Google",
- "google_cse" => "Google CSE",
- "startpage" => "Startpage",
- "qwant" => "Qwant",
- "yep" => "Yep",
- "solofield" => "Solofield",
- //"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",
- "startpage" => "Startpage",
- "qwant" => "Qwant",
- "solofield" => "Solofield"
- ]
- ];
- break;
-
- case "news":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "ddg" => "DuckDuckGo",
- "brave" => "Brave",
- "google" => "Google",
- "startpage" => "Startpage",
- "qwant" => "Qwant",
- "yep" => "Yep",
- "mojeek" => "Mojeek"
- ]
- ];
- break;
-
- case "music":
- $filters["scraper"] = [
- "display" => "Scraper",
- "option" => [
- "sc" => "SoundCloud"
- //"spotify" => "Spotify"
- ]
- ];
- 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;
- }
-
- include "scraper/$scraper_out.php";
- $lib = new $scraper_out();
-
- // 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;
- }
- }
|