frontend.php 22 KB

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