frontend.php 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. <?php
  2. class frontend{
  3. public function load($template, $replacements = []){
  4. $replacements["server_name"] = htmlspecialchars(config::SERVER_NAME);
  5. $replacements["version"] = config::VERSION;
  6. if(isset($_COOKIE["theme"])){
  7. $theme = str_replace(["/". "."], "", $_COOKIE["theme"]);
  8. if(
  9. $theme != "Dark" &&
  10. !is_file("static/themes/" . $theme . ".css")
  11. ){
  12. $theme = config::DEFAULT_THEME;
  13. }
  14. }else{
  15. $theme = config::DEFAULT_THEME;
  16. }
  17. if($theme != "Dark"){
  18. $replacements["style"] = '<link rel="stylesheet" href="/static/themes/' . rawurlencode($theme) . '.css?v' . config::VERSION . '">';
  19. }else{
  20. $replacements["style"] = "";
  21. }
  22. if(isset($_COOKIE["scraper_ac"])){
  23. $replacements["ac"] = '?ac=' . htmlspecialchars($_COOKIE["scraper_ac"]);
  24. }else{
  25. $replacements["ac"] = '';
  26. }
  27. if(
  28. isset($replacements["timetaken"]) &&
  29. $replacements["timetaken"] !== null
  30. ){
  31. $replacements["timetaken"] = '<div class="timetaken">Took ' . number_format(microtime(true) - $replacements["timetaken"], 2) . 's</div>';
  32. }
  33. $handle = fopen("template/{$template}", "r");
  34. $data = fread($handle, filesize("template/{$template}"));
  35. fclose($handle);
  36. $data = explode("\n", $data);
  37. $html = "";
  38. for($i=0; $i<count($data); $i++){
  39. $html .= trim($data[$i]);
  40. }
  41. foreach($replacements as $key => $value){
  42. $html =
  43. str_replace(
  44. "{%{$key}%}",
  45. $value,
  46. $html
  47. );
  48. }
  49. return trim($html);
  50. }
  51. public function loadheader(array $get, array $filters, string $page){
  52. echo
  53. $this->load("header.html", [
  54. "title" => trim(htmlspecialchars($get["s"]) . " ({$page})"),
  55. "description" => ucfirst($page) . ' search results for &quot;' . htmlspecialchars($get["s"]) . '&quot;',
  56. "index" => "no",
  57. "search" => htmlspecialchars($get["s"]),
  58. "tabs" => $this->generatehtmltabs($page, $get["s"]),
  59. "filters" => $this->generatehtmlfilters($filters, $get)
  60. ]);
  61. $headers_raw = getallheaders();
  62. $header_keys = [];
  63. $user_agent = "";
  64. $bad_header = false;
  65. // block bots that present X-Forwarded-For, Via, etc
  66. foreach($headers_raw as $headerkey => $headervalue){
  67. $headerkey = strtolower($headerkey);
  68. if($headerkey == "user-agent"){
  69. $user_agent = $headervalue;
  70. continue;
  71. }
  72. // check header key
  73. if(in_array($headerkey, config::FILTERED_HEADER_KEYS)){
  74. $bad_header = true;
  75. break;
  76. }
  77. }
  78. // SSL check
  79. $bad_ssl = false;
  80. if(
  81. isset($_SERVER["https"]) &&
  82. $_SERVER["https"] == "on" &&
  83. isset($_SERVER["SSL_CIPHER"]) &&
  84. in_array($_SERVER["SSL_CIPHER"], config::FILTERED_HEADER_KEYS)
  85. ){
  86. $bad_ssl = true;
  87. }
  88. if(
  89. $bad_header === true ||
  90. $bad_ssl === true ||
  91. $user_agent == "" ||
  92. // user agent check
  93. preg_match(
  94. config::HEADER_REGEX,
  95. $user_agent
  96. )
  97. ){
  98. // bot detected !!
  99. apcu_inc("captcha_gen");
  100. $this->drawerror(
  101. "Tshh, blocked!",
  102. '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>.'
  103. );
  104. die();
  105. }
  106. }
  107. public function drawerror($title, $error, $timetaken = null){
  108. if($timetaken === null){
  109. $timetaken = microtime(true);
  110. }
  111. echo
  112. $this->load("search.html", [
  113. "timetaken" => $timetaken,
  114. "class" => "",
  115. "right-left" => "",
  116. "right-right" => "",
  117. "left" =>
  118. '<div class="infobox">' .
  119. '<h1>' . htmlspecialchars($title) . '</h1>' .
  120. $error .
  121. '</div>'
  122. ]);
  123. die();
  124. }
  125. public function drawscrapererror($error, $get, $target, $timetaken = null){
  126. if($timetaken === null){
  127. $timetaken = microtime(true);
  128. }
  129. $this->drawerror(
  130. "Shit",
  131. 'This scraper returned an error:' .
  132. '<div class="code">' . htmlspecialchars($error) . '</div>' .
  133. 'Things you can try:' .
  134. '<ul>' .
  135. '<li>Use a different scraper</li>' .
  136. '<li>Remove keywords that could cause errors</li>' .
  137. '<li><a href="/instances?target=' . $target . "&" . $this->buildquery($get, false) . '">Try your search on another 4get instance</a></li>' .
  138. '</ul><br>' .
  139. 'If the error persists, please <a href="/about">contact the administrator</a>.',
  140. $timetaken
  141. );
  142. }
  143. public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
  144. $payload =
  145. '<div class="text-result">';
  146. // add favicon, link and archive links
  147. $payload .= $this->drawlink($site["url"]);
  148. /*
  149. Draw title + description + filetype
  150. */
  151. $payload .=
  152. '<a href="' . htmlspecialchars($site["url"]) . '" class="hover" rel="noreferrer nofollow"';
  153. if($tabindex === false){
  154. $payload .= ' tabindex="-1"';
  155. }
  156. $payload .= '>';
  157. if($site["thumb"]["url"] !== null){
  158. $payload .=
  159. '<div class="thumb-wrap';
  160. switch($site["thumb"]["ratio"]){
  161. case "16:9":
  162. $size = "landscape";
  163. break;
  164. case "9:16":
  165. $payload .= " portrait";
  166. $size = "portrait";
  167. break;
  168. case "1:1":
  169. $payload .= " square";
  170. $size = "square";
  171. break;
  172. }
  173. $payload .=
  174. '">' .
  175. '<img class="thumb" src="' . $this->htmlimage($site["thumb"]["url"], $size) . '" alt="thumb">';
  176. if($duration !== null){
  177. $payload .=
  178. '<div class="duration">' .
  179. htmlspecialchars($duration) .
  180. '</div>';
  181. }
  182. $payload .=
  183. '</div>';
  184. }
  185. $payload .=
  186. '<div class="title">';
  187. if(
  188. isset($site["type"]) &&
  189. $site["type"] != "web"
  190. ){
  191. $payload .= '<div class="type">' . strtoupper($site["type"]) . '</div>';
  192. }
  193. $payload .=
  194. $this->highlighttext($keywords, $site["title"]) .
  195. '</div>';
  196. if($greentext !== null){
  197. $payload .=
  198. '<div class="greentext">' .
  199. htmlspecialchars($greentext) .
  200. '</div>';
  201. }
  202. if($site["description"] !== null){
  203. $payload .=
  204. '<div class="description">' .
  205. $this->highlighttext($keywords, $site["description"]) .
  206. '</div>';
  207. }
  208. $payload .= $customhtml;
  209. $payload .= '</a>';
  210. /*
  211. Sublinks
  212. */
  213. if(
  214. isset($site["sublink"]) &&
  215. !empty($site["sublink"])
  216. ){
  217. usort($site["sublink"], function($a, $b){
  218. return strlen($a["description"]) > strlen($b["description"]);
  219. });
  220. $payload .=
  221. '<div class="sublinks">' .
  222. '<table>';
  223. $opentr = false;
  224. for($i=0; $i<count($site["sublink"]); $i++){
  225. if(($i % 2) === 0){
  226. $opentr = true;
  227. $payload .= '<tr>';
  228. }else{
  229. $opentr = false;
  230. }
  231. $payload .=
  232. '<td>' .
  233. '<a href="' . htmlspecialchars($site["sublink"][$i]["url"]) . '" rel="noreferrer nofollow">' .
  234. '<div class="title">' .
  235. htmlspecialchars($site["sublink"][$i]["title"]) .
  236. '</div>';
  237. if(!empty($site["sublink"][$i]["date"])){
  238. $payload .=
  239. '<div class="greentext">' .
  240. date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
  241. '</div>';
  242. }
  243. if(!empty($site["sublink"][$i]["description"])){
  244. $payload .=
  245. '<div class="description">' .
  246. $this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
  247. '</div>';
  248. }
  249. $payload .= '</a></td>';
  250. if($opentr === false){
  251. $payload .= '</tr>';
  252. }
  253. }
  254. if($opentr === true){
  255. $payload .= '<td></td></tr>';
  256. }
  257. $payload .= '</table></div>';
  258. }
  259. if(
  260. isset($site["table"]) &&
  261. !empty($site["table"])
  262. ){
  263. $payload .= '<table class="info-table">';
  264. foreach($site["table"] as $title => $value){
  265. $payload .=
  266. '<tr>' .
  267. '<td>' . htmlspecialchars($title) . '</td>' .
  268. '<td>' . htmlspecialchars($value) . '</td>' .
  269. '</tr>';
  270. }
  271. $payload .= '</table>';
  272. }
  273. return $payload . '</div>';
  274. }
  275. public function highlighttext($keywords, $text){
  276. $text = htmlspecialchars($text);
  277. $keywords = explode(" ", $keywords);
  278. $regex = [];
  279. foreach($keywords as $word){
  280. $regex[] = "\b" . preg_quote($word, "/") . "\b";
  281. }
  282. $regex = "/" . implode("|", $regex) . "/i";
  283. return
  284. preg_replace(
  285. $regex,
  286. '<b>${0}</b>',
  287. $text
  288. );
  289. }
  290. function highlightcode($text){
  291. // https://www.php.net/highlight_string
  292. ini_set("highlight.comment", "c-comment");
  293. ini_set("highlight.default", "c-default");
  294. ini_set("highlight.html", "c-default");
  295. ini_set("highlight.keyword", "c-keyword");
  296. ini_set("highlight.string", "c-string");
  297. $text =
  298. trim(
  299. preg_replace(
  300. '/<code [^>]+>/',
  301. "",
  302. str_replace(
  303. [
  304. "<br />",
  305. "&nbsp;",
  306. "<pre>",
  307. "</pre>",
  308. "</code>"
  309. ],
  310. [
  311. "\n",
  312. " ",
  313. "",
  314. "",
  315. ""
  316. ],
  317. explode(
  318. "&lt;?php",
  319. highlight_string("<?php " . $text, true),
  320. 2
  321. )[1]
  322. )
  323. )
  324. );
  325. // replace colors
  326. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  327. foreach($classes as $class){
  328. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  329. }
  330. return $text;
  331. }
  332. public function drawlink($link){
  333. /*
  334. Add favicon
  335. */
  336. $host = parse_url($link);
  337. // special case for when we're not drawing a full url
  338. if(!isset($host["host"])){
  339. $payload =
  340. '<div class="url">' .
  341. '<button class="favicon" tabindex="-1">' .
  342. '<img src="/favicon?s=404" alt="xx">' .
  343. '</button>';
  344. }else{
  345. $esc =
  346. explode(
  347. ".",
  348. $host["host"],
  349. 2
  350. );
  351. if(
  352. count($esc) === 2 &&
  353. $esc[0] == "www"
  354. ){
  355. $esc = $esc[1];
  356. }else{
  357. $esc = $esc[0];
  358. }
  359. $esc = substr($esc, 0, 2);
  360. $urlencode = urlencode($link);
  361. $payload =
  362. '<div class="url">' .
  363. '<button class="favicon" tabindex="-1">' .
  364. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  365. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  366. '</button>' .
  367. '<div class="favicon-dropdown">';
  368. $payload .=
  369. '<a href="https://web.archive.org/web/' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.org" alt="ar">Archive.org</a>' .
  370. '<a href="https://archive.ph/newest/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.is" alt="ar">Archive.is</a>' .
  371. '<a href="https://ghostarchive.org/search?term=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://ghostarchive.org" alt="gh">Ghostarchive</a>' .
  372. '<a href="https://arquivo.pt/wayback/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://arquivo.pt" alt="ar">Arquivo.pt</a>' .
  373. '<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>' .
  374. '<a href="https://megalodon.jp/?url=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://megalodon.jp" alt="me">Megalodon</a>' .
  375. '</div>';
  376. }
  377. /*
  378. Draw link
  379. */
  380. $parts = explode("/", $link);
  381. $clickurl = "";
  382. // remove trailing /
  383. $c = count($parts) - 1;
  384. if($parts[$c] == ""){
  385. $parts[$c - 1] = $parts[$c - 1] . "/";
  386. unset($parts[$c]);
  387. }
  388. // merge https://site together
  389. if(isset($host["host"])){
  390. $parts = [
  391. $parts[0] . $parts[1] . '//' . $parts[2],
  392. ...array_slice($parts, 3, count($parts) - 1)
  393. ];
  394. }
  395. $c = count($parts);
  396. for($i=0; $i<$c; $i++){
  397. if($i !== 0){ $clickurl .= "/"; }
  398. $clickurl .= $parts[$i];
  399. if($i === $c - 1){
  400. $parts[$i] = rtrim($parts[$i], "/");
  401. }
  402. $payload .=
  403. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  404. htmlspecialchars(urldecode($parts[$i])) .
  405. '</a>';
  406. if($i !== $c - 1){
  407. $payload .= '<span class="separator"></span>';
  408. }
  409. }
  410. return $payload . '</div>';
  411. }
  412. public function getscraperfilters($page){
  413. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  414. if(
  415. isset($_GET["scraper"]) &&
  416. is_string($_GET["scraper"])
  417. ){
  418. $get_scraper = $_GET["scraper"];
  419. }else{
  420. if(
  421. isset($_GET["npt"]) &&
  422. is_string($_GET["npt"])
  423. ){
  424. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  425. $get_scraper =
  426. preg_replace(
  427. '/[0-9]+$/',
  428. "",
  429. $get_scraper
  430. );
  431. }
  432. }
  433. // add search field
  434. $filters =
  435. [
  436. "s" => [
  437. "option" => "_SEARCH"
  438. ]
  439. ];
  440. // define default scrapers
  441. switch($page){
  442. case "web":
  443. $filters["scraper"] = [
  444. "display" => "Scraper",
  445. "option" => [
  446. "ddg" => "DuckDuckGo",
  447. //"yahoo" => "Yahoo!",
  448. "brave" => "Brave",
  449. "yandex" => "Yandex",
  450. "google" => "Google",
  451. "google_api" => "Google API",
  452. "google_cse" => "Google CSE",
  453. "yahoo_japan" => "Yahoo! JAPAN",
  454. "startpage" => "Startpage",
  455. "qwant" => "Qwant",
  456. "ghostery" => "Ghostery",
  457. "yep" => "Yep",
  458. "greppr" => "Greppr",
  459. "crowdview" => "Crowdview",
  460. "mwmbl" => "Mwmbl",
  461. "mojeek" => "Mojeek",
  462. "baidu" => "Baidu",
  463. "coccoc" => "Cốc Cốc",
  464. "solofield" => "Solofield",
  465. "marginalia" => "Marginalia",
  466. "wiby" => "wiby",
  467. "curlie" => "Curlie"
  468. ]
  469. ];
  470. break;
  471. case "images":
  472. $filters["scraper"] = [
  473. "display" => "Scraper",
  474. "option" => [
  475. "ddg" => "DuckDuckGo",
  476. "yandex" => "Yandex",
  477. "brave" => "Brave",
  478. "google" => "Google",
  479. "google_cse" => "Google CSE",
  480. "yahoo_japan" => "Yahoo! JAPAN",
  481. "startpage" => "Startpage",
  482. "qwant" => "Qwant",
  483. "yep" => "Yep",
  484. "baidu" => "Baidu",
  485. "solofield" => "Solofield",
  486. "pinterest" => "Pinterest",
  487. "cara" => "Cara",
  488. "flickr" => "Flickr",
  489. "pexels" => "Pexels",
  490. "pixabay" => "Pixabay",
  491. "unsplash" => "Unsplash",
  492. "fivehpx" => "500px",
  493. "vsco" => "VSCO",
  494. "imgur" => "Imgur",
  495. "ftm" => "FindThatMeme",
  496. //"sankakucomplex" => "SankakuComplex"
  497. ]
  498. ];
  499. break;
  500. case "videos":
  501. $filters["scraper"] = [
  502. "display" => "Scraper",
  503. "option" => [
  504. "yt" => "YouTube",
  505. //"archiveorg" => "Archive.org",
  506. "vimeo" => "Vimeo",
  507. //"odysee" => "Odysee",
  508. "sepiasearch" => "Sepia Search",
  509. //"fb" => "Facebook videos",
  510. "ddg" => "DuckDuckGo",
  511. "brave" => "Brave",
  512. "yandex" => "Yandex",
  513. "google" => "Google",
  514. "yahoo_japan" => "Yahoo! JAPAN",
  515. "startpage" => "Startpage",
  516. "qwant" => "Qwant",
  517. "baidu" => "Baidu",
  518. "coccoc" => "Cốc Cốc",
  519. "solofield" => "Solofield"
  520. ]
  521. ];
  522. break;
  523. case "news":
  524. $filters["scraper"] = [
  525. "display" => "Scraper",
  526. "option" => [
  527. "ddg" => "DuckDuckGo",
  528. "brave" => "Brave",
  529. "google" => "Google",
  530. "yahoo_japan" => "Yahoo! JAPAN",
  531. "startpage" => "Startpage",
  532. "qwant" => "Qwant",
  533. "yep" => "Yep",
  534. "mojeek" => "Mojeek",
  535. "baidu" => "Baidu"
  536. ]
  537. ];
  538. break;
  539. case "music":
  540. $filters["scraper"] = [
  541. "display" => "Scraper",
  542. "option" => [
  543. "sc" => "SoundCloud",
  544. "swisscows" => "Swisscows (SoundCloud)"
  545. //"spotify" => "Spotify"
  546. ]
  547. ];
  548. break;
  549. }
  550. // get scraper name from user input, or default out to preferred scraper
  551. $scraper_out = null;
  552. $first = true;
  553. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  554. if($first === true){
  555. $first = $scraper_name;
  556. }
  557. if($scraper_name == $get_scraper){
  558. $scraper_out = $scraper_name;
  559. }
  560. }
  561. if($scraper_out === null){
  562. $scraper_out = $first;
  563. }
  564. include "scraper/$scraper_out.php";
  565. $lib = new $scraper_out();
  566. // set scraper on $_GET
  567. $_GET["scraper"] = $scraper_out;
  568. // set nsfw on $_GET
  569. if(
  570. isset($_COOKIE["nsfw"]) &&
  571. !isset($_GET["nsfw"])
  572. ){
  573. $_GET["nsfw"] = $_COOKIE["nsfw"];
  574. }
  575. return
  576. [
  577. $lib,
  578. array_merge_recursive(
  579. $filters,
  580. $lib->getfilters($page)
  581. )
  582. ];
  583. }
  584. public function parsegetfilters($parameters, $whitelist){
  585. $sanitized = [];
  586. // add npt token
  587. if(
  588. isset($parameters["npt"]) &&
  589. is_string($parameters["npt"])
  590. ){
  591. $sanitized["npt"] = $parameters["npt"];
  592. }else{
  593. $sanitized["npt"] = false;
  594. }
  595. // we're iterating over $whitelist, so
  596. // you can't polluate $sanitized with useless
  597. // parameters
  598. foreach($whitelist as $parameter => $value){
  599. if(isset($parameters[$parameter])){
  600. if(!is_string($parameters[$parameter])){
  601. $sanitized[$parameter] = null;
  602. continue;
  603. }
  604. // parameter is already set, use that value
  605. $sanitized[$parameter] = $parameters[$parameter];
  606. }else{
  607. // parameter is not set, add it
  608. if(is_string($value["option"])){
  609. // special field: set default value manually
  610. switch($value["option"]){
  611. case "_DATE":
  612. // no date set
  613. $sanitized[$parameter] = false;
  614. break;
  615. case "_SEARCH":
  616. // no search set
  617. $sanitized[$parameter] = "";
  618. break;
  619. }
  620. }else{
  621. // set a default value
  622. $sanitized[$parameter] = array_keys($value["option"])[0];
  623. }
  624. }
  625. // sanitize input
  626. if(is_array($value["option"])){
  627. if(
  628. !in_array(
  629. $sanitized[$parameter],
  630. $keys = array_keys($value["option"])
  631. )
  632. ){
  633. $sanitized[$parameter] = $keys[0];
  634. }
  635. }else{
  636. // sanitize search & string
  637. switch($value["option"]){
  638. case "_DATE":
  639. if($sanitized[$parameter] !== false){
  640. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  641. if($sanitized[$parameter] <= 0){
  642. $sanitized[$parameter] = false;
  643. }
  644. }
  645. break;
  646. case "_SEARCH":
  647. // get search string
  648. $sanitized["s"] = trim($sanitized[$parameter]);
  649. }
  650. }
  651. }
  652. // invert dates if needed
  653. if(
  654. isset($sanitized["older"]) &&
  655. isset($sanitized["newer"]) &&
  656. $sanitized["newer"] !== false &&
  657. $sanitized["older"] !== false &&
  658. $sanitized["newer"] > $sanitized["older"]
  659. ){
  660. // invert
  661. [
  662. $sanitized["older"],
  663. $sanitized["newer"]
  664. ] = [
  665. $sanitized["newer"],
  666. $sanitized["older"]
  667. ];
  668. }
  669. return $sanitized;
  670. }
  671. public function s_to_timestamp($seconds){
  672. if(is_string($seconds)){
  673. return "LIVE";
  674. }
  675. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  676. }
  677. public function generatehtmltabs($page, $query){
  678. $html = null;
  679. foreach(["web", "images", "videos", "news", "music"] as $type){
  680. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  681. if(!empty($params)){
  682. $html .= $params;
  683. }
  684. $html .= '" class="tab';
  685. if($type == $page){
  686. $html .= ' selected';
  687. }
  688. $html .= '">' . ucfirst($type) . '</a>';
  689. }
  690. return $html;
  691. }
  692. public function generatehtmlfilters($filters, $params){
  693. $html = null;
  694. foreach($filters as $filter_name => $filter_values){
  695. if(!isset($filter_values["display"])){
  696. continue;
  697. }
  698. $output = true;
  699. $tmp =
  700. '<div class="filter">' .
  701. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  702. if(is_array($filter_values["option"])){
  703. $tmp .= '<select name="' . $filter_name . '">';
  704. foreach($filter_values["option"] as $option_name => $option_title){
  705. $tmp .= '<option value="' . $option_name . '"';
  706. if($params[$filter_name] == $option_name){
  707. $tmp .= ' selected';
  708. }
  709. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  710. }
  711. $tmp .= '</select>';
  712. }else{
  713. switch($filter_values["option"]){
  714. case "_DATE":
  715. $tmp .= '<input type="date" name="' . $filter_name . '"';
  716. if($params[$filter_name] !== false){
  717. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  718. }
  719. $tmp .= '>';
  720. break;
  721. default:
  722. $output = false;
  723. break;
  724. }
  725. }
  726. $tmp .= '</div>';
  727. if($output === true){
  728. $html .= $tmp;
  729. }
  730. }
  731. return $html;
  732. }
  733. public function buildquery($gets, $ommit = false){
  734. $out = [];
  735. foreach($gets as $key => $value){
  736. if(
  737. $value == null ||
  738. $value == false ||
  739. $key == "npt" ||
  740. $key == "extendedsearch" ||
  741. $value == "any" ||
  742. $value == "all" ||
  743. $key == "spellcheck" ||
  744. (
  745. $ommit === true &&
  746. $key == "s"
  747. )
  748. ){
  749. continue;
  750. }
  751. if(
  752. $key == "older" ||
  753. $key == "newer"
  754. ){
  755. $value = date("Y-m-d", (int)$value);
  756. }
  757. $out[$key] = $value;
  758. }
  759. return http_build_query($out);
  760. }
  761. public function htmlimage($image, $format){
  762. if(
  763. preg_match(
  764. '/^data:/',
  765. $image
  766. )
  767. ){
  768. return htmlspecialchars($image);
  769. }
  770. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  771. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  772. }
  773. public function htmlnextpage($gets, $npt, $page){
  774. $query = $this->buildquery($gets);
  775. return $page . "?" . $query . "&npt=" . $npt;
  776. }
  777. }