frontend.php 22 KB

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