frontend.php 22 KB

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