frontend.php 23 KB

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