frontend.php 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. //"fget" => "fget",
  484. "ddg" => "DuckDuckGo",
  485. //"yahoo" => "Yahoo!",
  486. "brave" => "Brave",
  487. "yandex" => "Yandex",
  488. "google" => "Google",
  489. "google_api" => "Google API",
  490. "google_cse" => "Google CSE",
  491. "yahoo_japan" => "Yahoo! JAPAN",
  492. "startpage" => "Startpage",
  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. "purili" => "Purili",
  502. "wiby" => "wiby"
  503. ]
  504. ];
  505. break;
  506. case "images":
  507. $filters["scraper"] = [
  508. "display" => "Scraper",
  509. "option" => [
  510. "ddg" => "DuckDuckGo",
  511. "yandex" => "Yandex",
  512. "brave" => "Brave",
  513. "google" => "Google",
  514. "google_api" => "Google API",
  515. "google_cse" => "Google CSE",
  516. "yahoo_japan" => "Yahoo! JAPAN",
  517. "startpage" => "Startpage",
  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. "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 htmlimage($image, $format){
  811. if(
  812. preg_match(
  813. '/^data:/',
  814. $image
  815. )
  816. ){
  817. return htmlspecialchars($image);
  818. }
  819. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  820. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  821. }
  822. public function htmlnextpage($gets, $npt, $page){
  823. $query = $this->buildquery($gets);
  824. return $page . "?" . $query . "&npt=" . $npt;
  825. }
  826. }