frontend.php 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. "yep" => "Yep",
  494. "mwmbl" => "Mwmbl",
  495. "mojeek" => "Mojeek",
  496. "naver" => "Naver",
  497. "baidu" => "Baidu",
  498. "coccoc" => "Cốc Cốc",
  499. "solofield" => "Solofield",
  500. "marginalia" => "Marginalia",
  501. "wiby" => "wiby"
  502. ]
  503. ];
  504. break;
  505. case "images":
  506. $filters["scraper"] = [
  507. "display" => "Scraper",
  508. "option" => [
  509. "ddg" => "DuckDuckGo",
  510. "yandex" => "Yandex",
  511. "brave" => "Brave",
  512. "google" => "Google",
  513. "google_api" => "Google API",
  514. "google_cse" => "Google CSE",
  515. "yahoo_japan" => "Yahoo! JAPAN",
  516. "startpage" => "Startpage",
  517. "qwant" => "Qwant",
  518. "naver" => "Naver",
  519. "baidu" => "Baidu",
  520. "solofield" => "Solofield",
  521. "pinterest" => "Pinterest",
  522. "cara" => "Cara",
  523. "flickr" => "Flickr",
  524. "pexels" => "Pexels",
  525. "pixabay" => "Pixabay",
  526. "unsplash" => "Unsplash",
  527. "fivehpx" => "500px",
  528. "vsco" => "VSCO",
  529. "imgur" => "Imgur",
  530. "ftm" => "FindThatMeme"
  531. ]
  532. ];
  533. break;
  534. case "videos":
  535. $filters["scraper"] = [
  536. "display" => "Scraper",
  537. "option" => [
  538. "yt" => "YouTube",
  539. //"archiveorg" => "Archive.org",
  540. "vimeo" => "Vimeo",
  541. //"odysee" => "Odysee",
  542. "sepiasearch" => "Sepia Search",
  543. //"fb" => "Facebook videos",
  544. "ddg" => "DuckDuckGo",
  545. "brave" => "Brave",
  546. "yandex" => "Yandex",
  547. "google" => "Google",
  548. "yahoo_japan" => "Yahoo! JAPAN",
  549. "startpage" => "Startpage",
  550. "qwant" => "Qwant",
  551. "naver" => "Naver",
  552. "baidu" => "Baidu",
  553. "coccoc" => "Cốc Cốc",
  554. "solofield" => "Solofield"
  555. ]
  556. ];
  557. break;
  558. case "news":
  559. $filters["scraper"] = [
  560. "display" => "Scraper",
  561. "option" => [
  562. "ddg" => "DuckDuckGo",
  563. "brave" => "Brave",
  564. "google" => "Google",
  565. "yahoo_japan" => "Yahoo! JAPAN",
  566. "startpage" => "Startpage",
  567. "qwant" => "Qwant",
  568. "mojeek" => "Mojeek",
  569. "baidu" => "Baidu"
  570. ]
  571. ];
  572. break;
  573. case "music":
  574. $filters["scraper"] = [
  575. "display" => "Scraper",
  576. "option" => [
  577. "sc" => "SoundCloud",
  578. "swisscows" => "Swisscows (SoundCloud)"
  579. //"spotify" => "Spotify"
  580. ]
  581. ];
  582. break;
  583. case "booru":
  584. $filters["scraper"] = [
  585. "display" => "Scraper",
  586. "option" => [
  587. "safebooru" => "Safebooru",
  588. "konachan" => "Konachan",
  589. "tbib" => "The Big Imageboard",
  590. "gelbooru" => "Gelbooru",
  591. "yandere" => "Yande.re",
  592. "tbib" => "The Big Imageboard",
  593. "sankakucomplex" => "SankakuComplex",
  594. "soybooru" => "SoyBooru"
  595. ]
  596. ];
  597. break;
  598. }
  599. // get scraper name from user input, or default out to preferred scraper
  600. $scraper_out = null;
  601. $first = true;
  602. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  603. if($first === true){
  604. $first = $scraper_name;
  605. }
  606. if($scraper_name == $get_scraper){
  607. $scraper_out = $scraper_name;
  608. }
  609. }
  610. if($scraper_out === null){
  611. $scraper_out = $first;
  612. }
  613. include "scraper/$scraper_out.php";
  614. $lib = new $scraper_out();
  615. // set scraper on $_GET
  616. $_GET["scraper"] = $scraper_out;
  617. // set nsfw on $_GET
  618. if(
  619. isset($_COOKIE["nsfw"]) &&
  620. !isset($_GET["nsfw"])
  621. ){
  622. $_GET["nsfw"] = $_COOKIE["nsfw"];
  623. }
  624. return
  625. [
  626. $lib,
  627. array_merge_recursive(
  628. $filters,
  629. $lib->getfilters($page)
  630. )
  631. ];
  632. }
  633. public function parsegetfilters($parameters, $whitelist){
  634. $sanitized = [];
  635. // add npt token
  636. if(
  637. isset($parameters["npt"]) &&
  638. is_string($parameters["npt"])
  639. ){
  640. $sanitized["npt"] = $parameters["npt"];
  641. }else{
  642. $sanitized["npt"] = false;
  643. }
  644. // we're iterating over $whitelist, so
  645. // you can't polluate $sanitized with useless
  646. // parameters
  647. foreach($whitelist as $parameter => $value){
  648. if(isset($parameters[$parameter])){
  649. if(!is_string($parameters[$parameter])){
  650. $sanitized[$parameter] = null;
  651. continue;
  652. }
  653. // parameter is already set, use that value
  654. $sanitized[$parameter] = $parameters[$parameter];
  655. }else{
  656. // parameter is not set, add it
  657. if(is_string($value["option"])){
  658. // special field: set default value manually
  659. switch($value["option"]){
  660. case "_DATE":
  661. // no date set
  662. $sanitized[$parameter] = false;
  663. break;
  664. case "_SEARCH":
  665. // no search set
  666. $sanitized[$parameter] = "";
  667. break;
  668. }
  669. }else{
  670. // set a default value
  671. $sanitized[$parameter] = array_keys($value["option"])[0];
  672. }
  673. }
  674. // sanitize input
  675. if(is_array($value["option"])){
  676. if(
  677. !in_array(
  678. $sanitized[$parameter],
  679. $keys = array_keys($value["option"])
  680. )
  681. ){
  682. $sanitized[$parameter] = $keys[0];
  683. }
  684. }else{
  685. // sanitize search & string
  686. switch($value["option"]){
  687. case "_DATE":
  688. if($sanitized[$parameter] !== false){
  689. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  690. if($sanitized[$parameter] <= 0){
  691. $sanitized[$parameter] = false;
  692. }
  693. }
  694. break;
  695. case "_SEARCH":
  696. // get search string
  697. $sanitized["s"] = trim($sanitized[$parameter]);
  698. }
  699. }
  700. }
  701. // invert dates if needed
  702. if(
  703. isset($sanitized["older"]) &&
  704. isset($sanitized["newer"]) &&
  705. $sanitized["newer"] !== false &&
  706. $sanitized["older"] !== false &&
  707. $sanitized["newer"] > $sanitized["older"]
  708. ){
  709. // invert
  710. [
  711. $sanitized["older"],
  712. $sanitized["newer"]
  713. ] = [
  714. $sanitized["newer"],
  715. $sanitized["older"]
  716. ];
  717. }
  718. return $sanitized;
  719. }
  720. public function s_to_timestamp($seconds){
  721. if(is_string($seconds)){
  722. return "LIVE";
  723. }
  724. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  725. }
  726. public function generatehtmltabs($page, $query){
  727. $html = null;
  728. //foreach(["web", "images", "videos", "news", "music", "booru"] as $type){
  729. foreach(["web", "images", "videos", "news", "music"] as $type){
  730. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  731. if(!empty($params)){
  732. $html .= $params;
  733. }
  734. $html .= '" class="tab';
  735. if($type == $page){
  736. $html .= ' selected';
  737. }
  738. $html .= '">' . ucfirst($type) . '</a>';
  739. }
  740. return $html;
  741. }
  742. public function generatehtmlfilters($filters, $params){
  743. $html = null;
  744. foreach($filters as $filter_name => $filter_values){
  745. if(!isset($filter_values["display"])){
  746. continue;
  747. }
  748. $output = true;
  749. $tmp =
  750. '<div class="filter">' .
  751. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  752. if(is_array($filter_values["option"])){
  753. $tmp .= '<select name="' . $filter_name . '">';
  754. foreach($filter_values["option"] as $option_name => $option_title){
  755. $tmp .= '<option value="' . $option_name . '"';
  756. if($params[$filter_name] == $option_name){
  757. $tmp .= ' selected';
  758. }
  759. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  760. }
  761. $tmp .= '</select>';
  762. }else{
  763. switch($filter_values["option"]){
  764. case "_DATE":
  765. $tmp .= '<input type="date" name="' . $filter_name . '"';
  766. if($params[$filter_name] !== false){
  767. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  768. }
  769. $tmp .= '>';
  770. break;
  771. default:
  772. $output = false;
  773. break;
  774. }
  775. }
  776. $tmp .= '</div>';
  777. if($output === true){
  778. $html .= $tmp;
  779. }
  780. }
  781. return $html;
  782. }
  783. public function buildquery($gets, $ommit = false){
  784. $out = [];
  785. foreach($gets as $key => $value){
  786. if(
  787. $value == null ||
  788. $value == false ||
  789. $key == "npt" ||
  790. $key == "extendedsearch" ||
  791. $value == "any" ||
  792. $value == "all" ||
  793. $key == "spellcheck" ||
  794. (
  795. $ommit === true &&
  796. $key == "s"
  797. )
  798. ){
  799. continue;
  800. }
  801. if(
  802. $key == "older" ||
  803. $key == "newer"
  804. ){
  805. $value = date("Y-m-d", (int)$value);
  806. }
  807. $out[$key] = $value;
  808. }
  809. return http_build_query($out);
  810. }
  811. public function htmlimage($image, $format){
  812. if(
  813. preg_match(
  814. '/^data:/',
  815. $image
  816. )
  817. ){
  818. return htmlspecialchars($image);
  819. }
  820. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  821. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  822. }
  823. public function htmlnextpage($gets, $npt, $page){
  824. $query = $this->buildquery($gets);
  825. return $page . "?" . $query . "&npt=" . $npt;
  826. }
  827. }