frontend.php 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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, bool $increment_bot_counter = true){
  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. if($increment_bot_counter){
  137. apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
  138. }
  139. $this->drawerror(
  140. "Tshh, blocked!",
  141. '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>.'
  142. );
  143. }
  144. }
  145. public function drawerror($title, $error, $timetaken = null){
  146. if($timetaken === null){
  147. $timetaken = microtime(true);
  148. }
  149. echo
  150. $this->load("search.html", [
  151. "timetaken" => $timetaken,
  152. "class" => "",
  153. "right-left" => "",
  154. "right-right" => "",
  155. "left" =>
  156. '<div class="infobox">' .
  157. '<h1>' . htmlspecialchars($title) . '</h1>' .
  158. $error .
  159. '</div>'
  160. ]);
  161. die();
  162. }
  163. public function drawscrapererror($error, $get, $target, $timetaken = null){
  164. if($timetaken === null){
  165. $timetaken = microtime(true);
  166. }
  167. $this->drawerror(
  168. "Shit",
  169. 'This scraper returned an error:' .
  170. '<div class="code">' . htmlspecialchars($error) . '</div>' .
  171. 'Things you can try:' .
  172. '<ul>' .
  173. '<li>Use a different scraper</li>' .
  174. '<li>Remove keywords that could cause errors</li>' .
  175. '<li><a href="/instances?target=' . $target . "&" . $this->buildquery($get, false) . '">Try your search on another 4get instance</a></li>' .
  176. '</ul><br>' .
  177. 'If the error persists, please <a href="/about">contact the administrator</a>.',
  178. $timetaken
  179. );
  180. }
  181. public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
  182. $payload =
  183. '<div class="text-result">';
  184. // add favicon, link and archive links
  185. $payload .= $this->drawlink($site["url"]);
  186. /*
  187. Draw title + description + filetype
  188. */
  189. $payload .=
  190. '<a href="' . htmlspecialchars($site["url"]) . '" class="hover" rel="noreferrer nofollow"';
  191. if($tabindex === false){
  192. $payload .= ' tabindex="-1"';
  193. }
  194. $payload .= '>';
  195. if($site["thumb"]["url"] !== null){
  196. $payload .=
  197. '<div class="thumb-wrap';
  198. switch($site["thumb"]["ratio"]){
  199. case "16:9":
  200. $size = "landscape";
  201. break;
  202. case "9:16":
  203. $payload .= " portrait";
  204. $size = "portrait";
  205. break;
  206. case "1:1":
  207. $payload .= " square";
  208. $size = "square";
  209. break;
  210. }
  211. $payload .=
  212. '">' .
  213. '<img class="thumb" src="' . $this->htmlimage($site["thumb"]["url"], $size) . '" alt="thumb">';
  214. if($duration !== null){
  215. $payload .=
  216. '<div class="duration">' .
  217. htmlspecialchars($duration) .
  218. '</div>';
  219. }
  220. $payload .=
  221. '</div>';
  222. }
  223. $payload .=
  224. '<div class="title">';
  225. if(
  226. isset($site["type"]) &&
  227. $site["type"] != "web"
  228. ){
  229. $payload .= '<div class="type">' . strtoupper($site["type"]) . '</div>';
  230. }
  231. $payload .=
  232. $this->highlighttext($keywords, $site["title"]) .
  233. '</div>';
  234. if($greentext !== null){
  235. $payload .=
  236. '<div class="greentext">' .
  237. htmlspecialchars($greentext) .
  238. '</div>';
  239. }
  240. if($site["description"] !== null){
  241. $payload .=
  242. '<div class="description">' .
  243. $this->highlighttext($keywords, $site["description"]) .
  244. '</div>';
  245. }
  246. $payload .= $customhtml;
  247. $payload .= '</a>';
  248. /*
  249. Sublinks
  250. */
  251. if(
  252. isset($site["sublink"]) &&
  253. !empty($site["sublink"])
  254. ){
  255. usort($site["sublink"], function($a, $b){
  256. return strlen($a["description"]) > strlen($b["description"]);
  257. });
  258. $payload .=
  259. '<div class="sublinks">' .
  260. '<table>';
  261. $opentr = false;
  262. for($i=0; $i<count($site["sublink"]); $i++){
  263. if(($i % 2) === 0){
  264. $opentr = true;
  265. $payload .= '<tr>';
  266. }else{
  267. $opentr = false;
  268. }
  269. $payload .=
  270. '<td>' .
  271. '<a href="' . htmlspecialchars($site["sublink"][$i]["url"]) . '" rel="noreferrer nofollow">' .
  272. '<div class="title">' .
  273. htmlspecialchars($site["sublink"][$i]["title"]) .
  274. '</div>';
  275. if(!empty($site["sublink"][$i]["date"])){
  276. $payload .=
  277. '<div class="greentext">' .
  278. date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
  279. '</div>';
  280. }
  281. if(!empty($site["sublink"][$i]["description"])){
  282. $payload .=
  283. '<div class="description">' .
  284. $this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
  285. '</div>';
  286. }
  287. $payload .= '</a></td>';
  288. if($opentr === false){
  289. $payload .= '</tr>';
  290. }
  291. }
  292. if($opentr === true){
  293. $payload .= '<td></td></tr>';
  294. }
  295. $payload .= '</table></div>';
  296. }
  297. if(
  298. isset($site["table"]) &&
  299. !empty($site["table"])
  300. ){
  301. $payload .= '<table class="info-table">';
  302. foreach($site["table"] as $title => $value){
  303. $payload .=
  304. '<tr>' .
  305. '<td>' . htmlspecialchars($title) . '</td>' .
  306. '<td>' . htmlspecialchars($value) . '</td>' .
  307. '</tr>';
  308. }
  309. $payload .= '</table>';
  310. }
  311. return $payload . '</div>';
  312. }
  313. public function highlighttext($keywords, $text){
  314. $text = htmlspecialchars($text);
  315. $keywords = explode(" ", $keywords);
  316. $regex = [];
  317. foreach($keywords as $word){
  318. $regex[] = "\b" . preg_quote($word, "/") . "\b";
  319. }
  320. $regex = "/" . implode("|", $regex) . "/i";
  321. return
  322. preg_replace(
  323. $regex,
  324. '<b>${0}</b>',
  325. $text
  326. );
  327. }
  328. function highlightcode($text){
  329. // https://www.php.net/highlight_string
  330. ini_set("highlight.comment", "c-comment");
  331. ini_set("highlight.default", "c-default");
  332. ini_set("highlight.html", "c-default");
  333. ini_set("highlight.keyword", "c-keyword");
  334. ini_set("highlight.string", "c-string");
  335. $text =
  336. trim(
  337. preg_replace(
  338. '/<code [^>]+>/',
  339. "",
  340. str_replace(
  341. [
  342. "<br />",
  343. "&nbsp;",
  344. "<pre>",
  345. "</pre>",
  346. "</code>"
  347. ],
  348. [
  349. "\n",
  350. " ",
  351. "",
  352. "",
  353. ""
  354. ],
  355. explode(
  356. "&lt;?php",
  357. highlight_string("<?php " . $text, true),
  358. 2
  359. )[1]
  360. )
  361. )
  362. );
  363. // replace colors
  364. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  365. foreach($classes as $class){
  366. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  367. }
  368. return $text;
  369. }
  370. public function drawlink($link){
  371. /*
  372. Add favicon
  373. */
  374. $host = parse_url($link);
  375. // special case for when we're not drawing a full url
  376. if(!isset($host["host"])){
  377. $payload =
  378. '<div class="url">' .
  379. '<button class="favicon" tabindex="-1">' .
  380. '<img src="/favicon?s=404" alt="xx">' .
  381. '</button>';
  382. }else{
  383. $esc =
  384. explode(
  385. ".",
  386. $host["host"],
  387. 2
  388. );
  389. if(
  390. count($esc) === 2 &&
  391. $esc[0] == "www"
  392. ){
  393. $esc = $esc[1];
  394. }else{
  395. $esc = $esc[0];
  396. }
  397. $esc = substr($esc, 0, 2);
  398. $urlencode = urlencode($link);
  399. $payload =
  400. '<div class="url">' .
  401. '<button class="favicon" tabindex="-1">' .
  402. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  403. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  404. '</button>' .
  405. '<div class="favicon-dropdown">';
  406. $payload .=
  407. '<a href="https://web.archive.org/web/' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.org" alt="ar">Archive.org</a>' .
  408. '<a href="https://archive.ph/newest/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.is" alt="ar">Archive.is</a>' .
  409. '<a href="https://ghostarchive.org/search?term=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://ghostarchive.org" alt="gh">Ghostarchive</a>' .
  410. '<a href="https://arquivo.pt/wayback/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://arquivo.pt" alt="ar">Arquivo.pt</a>' .
  411. '<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>' .
  412. '<a href="https://megalodon.jp/?url=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://megalodon.jp" alt="me">Megalodon</a>' .
  413. '</div>';
  414. }
  415. /*
  416. Draw link
  417. */
  418. $parts = explode("/", $link);
  419. $clickurl = "";
  420. // remove trailing /
  421. $c = count($parts) - 1;
  422. if($parts[$c] == ""){
  423. $parts[$c - 1] = $parts[$c - 1] . "/";
  424. unset($parts[$c]);
  425. }
  426. // merge https://site together
  427. if(isset($host["host"])){
  428. $parts = [
  429. $parts[0] . $parts[1] . '//' . $parts[2],
  430. ...array_slice($parts, 3, count($parts) - 1)
  431. ];
  432. }
  433. $c = count($parts);
  434. for($i=0; $i<$c; $i++){
  435. if($i !== 0){ $clickurl .= "/"; }
  436. $clickurl .= $parts[$i];
  437. if($i === $c - 1){
  438. $parts[$i] = rtrim($parts[$i], "/");
  439. }
  440. $payload .=
  441. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  442. htmlspecialchars(urldecode($parts[$i])) .
  443. '</a>';
  444. if($i !== $c - 1){
  445. $payload .= '<span class="separator"></span>';
  446. }
  447. }
  448. return $payload . '</div>';
  449. }
  450. public function getscraperfilters($page){
  451. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  452. if(
  453. isset($_GET["scraper"]) &&
  454. is_string($_GET["scraper"])
  455. ){
  456. $get_scraper = $_GET["scraper"];
  457. }else{
  458. if(
  459. isset($_GET["npt"]) &&
  460. is_string($_GET["npt"])
  461. ){
  462. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  463. $get_scraper =
  464. preg_replace(
  465. '/[0-9]+$/',
  466. "",
  467. $get_scraper
  468. );
  469. }
  470. }
  471. // add search field
  472. $filters =
  473. [
  474. "s" => [
  475. "option" => "_SEARCH"
  476. ]
  477. ];
  478. // define default scrapers
  479. switch($page){
  480. case "web":
  481. $filters["scraper"] = [
  482. "display" => "Scraper",
  483. "option" => [
  484. //"fget" => "fget",
  485. "ddg" => "DuckDuckGo",
  486. //"yahoo" => "Yahoo!",
  487. "brave" => "Brave",
  488. "yandex" => "Yandex",
  489. "google" => "Google",
  490. "google_api" => "Google API",
  491. "google_cse" => "Google CSE",
  492. "yahoo_japan" => "Yahoo! JAPAN",
  493. "startpage" => "Startpage",
  494. "yep" => "Yep",
  495. "mwmbl" => "Mwmbl",
  496. "mojeek" => "Mojeek",
  497. "naver" => "Naver",
  498. "baidu" => "Baidu",
  499. "coccoc" => "Cốc Cốc",
  500. "solofield" => "Solofield",
  501. "marginalia" => "Marginalia",
  502. "purili" => "Purili",
  503. "wiby" => "wiby"
  504. ]
  505. ];
  506. break;
  507. case "images":
  508. $filters["scraper"] = [
  509. "display" => "Scraper",
  510. "option" => [
  511. "ddg" => "DuckDuckGo",
  512. "yandex" => "Yandex",
  513. "brave" => "Brave",
  514. "google" => "Google",
  515. "google_api" => "Google API",
  516. "google_cse" => "Google CSE",
  517. "yahoo_japan" => "Yahoo! JAPAN",
  518. "startpage" => "Startpage",
  519. "naver" => "Naver",
  520. "baidu" => "Baidu",
  521. "solofield" => "Solofield",
  522. "pinterest" => "Pinterest",
  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. "naver" => "Naver",
  551. "baidu" => "Baidu",
  552. "coccoc" => "Cốc Cốc",
  553. "purili" => "Purili",
  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. //"mojeek" => "Mojeek",
  568. "baidu" => "Baidu"
  569. ]
  570. ];
  571. break;
  572. case "music":
  573. $filters["scraper"] = [
  574. "display" => "Scraper",
  575. "option" => [
  576. "sc" => "SoundCloud",
  577. "swisscows" => "Swisscows (SoundCloud)"
  578. //"spotify" => "Spotify"
  579. ]
  580. ];
  581. break;
  582. case "booru":
  583. $filters["scraper"] = [
  584. "display" => "Scraper",
  585. "option" => [
  586. "safebooru" => "Safebooru",
  587. "konachan" => "Konachan",
  588. "tbib" => "The Big Imageboard",
  589. "gelbooru" => "Gelbooru",
  590. "yandere" => "Yande.re",
  591. "tbib" => "The Big Imageboard",
  592. "sankakucomplex" => "SankakuComplex",
  593. "soybooru" => "SoyBooru"
  594. ]
  595. ];
  596. break;
  597. }
  598. // get scraper name from user input, or default out to preferred scraper
  599. $scraper_out = null;
  600. $first = true;
  601. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  602. if($first === true){
  603. $first = $scraper_name;
  604. }
  605. if($scraper_name == $get_scraper){
  606. $scraper_out = $scraper_name;
  607. }
  608. }
  609. if($scraper_out === null){
  610. $scraper_out = $first;
  611. }
  612. include "scraper/$scraper_out.php";
  613. $lib = new $scraper_out();
  614. // set scraper on $_GET
  615. $_GET["scraper"] = $scraper_out;
  616. // set nsfw on $_GET
  617. if(
  618. isset($_COOKIE["nsfw"]) &&
  619. !isset($_GET["nsfw"])
  620. ){
  621. $_GET["nsfw"] = $_COOKIE["nsfw"];
  622. }
  623. return
  624. [
  625. $lib,
  626. array_merge_recursive(
  627. $filters,
  628. $lib->getfilters($page)
  629. )
  630. ];
  631. }
  632. public function parsegetfilters($parameters, $whitelist){
  633. $sanitized = [];
  634. // add npt token
  635. if(
  636. isset($parameters["npt"]) &&
  637. is_string($parameters["npt"])
  638. ){
  639. $sanitized["npt"] = $parameters["npt"];
  640. }else{
  641. $sanitized["npt"] = false;
  642. }
  643. // we're iterating over $whitelist, so
  644. // you can't polluate $sanitized with useless
  645. // parameters
  646. foreach($whitelist as $parameter => $value){
  647. if(isset($parameters[$parameter])){
  648. if(!is_string($parameters[$parameter])){
  649. $sanitized[$parameter] = null;
  650. continue;
  651. }
  652. // parameter is already set, use that value
  653. $sanitized[$parameter] = $parameters[$parameter];
  654. }else{
  655. // parameter is not set, add it
  656. if(is_string($value["option"])){
  657. // special field: set default value manually
  658. switch($value["option"]){
  659. case "_DATE":
  660. // no date set
  661. $sanitized[$parameter] = false;
  662. break;
  663. case "_SEARCH":
  664. // no search set
  665. $sanitized[$parameter] = "";
  666. break;
  667. }
  668. }else{
  669. // set a default value
  670. $sanitized[$parameter] = array_keys($value["option"])[0];
  671. }
  672. }
  673. // sanitize input
  674. if(is_array($value["option"])){
  675. if(
  676. !in_array(
  677. $sanitized[$parameter],
  678. $keys = array_keys($value["option"])
  679. )
  680. ){
  681. $sanitized[$parameter] = $keys[0];
  682. }
  683. }else{
  684. // sanitize search & string
  685. switch($value["option"]){
  686. case "_DATE":
  687. if($sanitized[$parameter] !== false){
  688. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  689. if($sanitized[$parameter] <= 0){
  690. $sanitized[$parameter] = false;
  691. }
  692. }
  693. break;
  694. case "_SEARCH":
  695. // get search string
  696. $sanitized["s"] = trim($sanitized[$parameter]);
  697. }
  698. }
  699. }
  700. // invert dates if needed
  701. if(
  702. isset($sanitized["older"]) &&
  703. isset($sanitized["newer"]) &&
  704. $sanitized["newer"] !== false &&
  705. $sanitized["older"] !== false &&
  706. $sanitized["newer"] > $sanitized["older"]
  707. ){
  708. // invert
  709. [
  710. $sanitized["older"],
  711. $sanitized["newer"]
  712. ] = [
  713. $sanitized["newer"],
  714. $sanitized["older"]
  715. ];
  716. }
  717. return $sanitized;
  718. }
  719. public function s_to_timestamp($seconds){
  720. if(is_string($seconds)){
  721. return "LIVE";
  722. }
  723. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  724. }
  725. public function generatehtmltabs($page, $query){
  726. $html = null;
  727. //foreach(["web", "images", "videos", "news", "music", "booru"] as $type){
  728. foreach(["web", "images", "videos", "news", "music"] as $type){
  729. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  730. if(!empty($params)){
  731. $html .= $params;
  732. }
  733. $html .= '" class="tab';
  734. if($type == $page){
  735. $html .= ' selected';
  736. }
  737. $html .= '">' . ucfirst($type) . '</a>';
  738. }
  739. return $html;
  740. }
  741. public function generatehtmlfilters($filters, $params){
  742. $html = null;
  743. foreach($filters as $filter_name => $filter_values){
  744. if(!isset($filter_values["display"])){
  745. continue;
  746. }
  747. $output = true;
  748. $tmp =
  749. '<div class="filter">' .
  750. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  751. if(is_array($filter_values["option"])){
  752. $tmp .= '<select name="' . $filter_name . '">';
  753. foreach($filter_values["option"] as $option_name => $option_title){
  754. $tmp .= '<option value="' . $option_name . '"';
  755. if($params[$filter_name] == $option_name){
  756. $tmp .= ' selected';
  757. }
  758. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  759. }
  760. $tmp .= '</select>';
  761. }else{
  762. switch($filter_values["option"]){
  763. case "_DATE":
  764. $tmp .= '<input type="date" name="' . $filter_name . '"';
  765. if($params[$filter_name] !== false){
  766. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  767. }
  768. $tmp .= '>';
  769. break;
  770. default:
  771. $output = false;
  772. break;
  773. }
  774. }
  775. $tmp .= '</div>';
  776. if($output === true){
  777. $html .= $tmp;
  778. }
  779. }
  780. return $html;
  781. }
  782. public function buildquery($gets, $ommit = false){
  783. $out = [];
  784. foreach($gets as $key => $value){
  785. if(
  786. $value == null ||
  787. $value == false ||
  788. $key == "npt" ||
  789. $key == "extendedsearch" ||
  790. $value == "any" ||
  791. $value == "all" ||
  792. $key == "spellcheck" ||
  793. (
  794. $ommit === true &&
  795. $key == "s"
  796. )
  797. ){
  798. continue;
  799. }
  800. if(
  801. $key == "older" ||
  802. $key == "newer"
  803. ){
  804. $value = date("Y-m-d", (int)$value);
  805. }
  806. $out[$key] = $value;
  807. }
  808. return http_build_query($out);
  809. }
  810. public function increment_real_reqs($scraper){
  811. apcu_inc(intdiv(time(), 3600) . ".real_requests", 1, $s, 262800);
  812. apcu_inc(intdiv(time(), 3600) . ".$scraper.requests", 1, $s, 262800);
  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. }