frontend.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. <?php
  2. class frontend{
  3. public function load($template, $replacements = []){
  4. $handle = fopen("template/{$template}", "r");
  5. $data = fread($handle, filesize("template/{$template}"));
  6. fclose($handle);
  7. $data = explode("\n", $data);
  8. $html = "";
  9. for($i=0; $i<count($data); $i++){
  10. $html .= trim($data[$i]);
  11. }
  12. foreach($replacements as $key => $value){
  13. $html =
  14. str_replace(
  15. "{%{$key}%}",
  16. $value,
  17. $html
  18. );
  19. }
  20. return trim($html);
  21. }
  22. public function getthemeclass($raw = true){
  23. if(
  24. isset($_COOKIE["theme"]) &&
  25. $_COOKIE["theme"] == "cream"
  26. ){
  27. $body_class = "theme-white ";
  28. }else{
  29. $body_class = "";
  30. }
  31. if(
  32. $raw &&
  33. $body_class != ""
  34. ){
  35. return ' class="' . rtrim($body_class) . '"';
  36. }
  37. return $body_class;
  38. }
  39. public function loadheader(array $get, array $filters, string $page){
  40. echo
  41. $this->load("header.html", [
  42. "title" => trim($get["s"] . " ({$page})"),
  43. "description" => ucfirst($page) . ' search results for &quot;' . htmlspecialchars($get["s"]) . '&quot;',
  44. "index" => "no",
  45. "search" => htmlspecialchars($get["s"]),
  46. "tabs" => $this->generatehtmltabs($page, $get["s"]),
  47. "filters" => $this->generatehtmlfilters($filters, $get),
  48. "body_class" => $this->getthemeclass()
  49. ]);
  50. if(
  51. preg_match(
  52. '/bot|wget|curl|python-requests|scrapy|feedfetcher|go-http-client|ruby|universalfeedparser|yahoo\! slurp|spider|rss/i',
  53. $_SERVER["HTTP_USER_AGENT"]
  54. )
  55. ){
  56. // bot detected !!
  57. echo
  58. $this->drawerror(
  59. "Tshh, blocked!",
  60. 'You were blocked from viewing this page. If you wish to scrape data from 4get, please consider running <a href="https://git.lolcat.ca/lolcat/4get" rel="noreferrer nofollow">your own 4get instance</a> or using <a href="/api.txt">the API</a>.',
  61. );
  62. die();
  63. }
  64. }
  65. public function drawerror($title, $error){
  66. return
  67. $this->load("search.html", [
  68. "class" => "",
  69. "right-left" => "",
  70. "right-right" => "",
  71. "left" =>
  72. '<div class="infobox">' .
  73. '<h1>' . htmlspecialchars($title) . '</h1>' .
  74. $error .
  75. '</div>'
  76. ]);
  77. }
  78. public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
  79. $payload =
  80. '<div class="text-result">';
  81. // add favicon, link and archive links
  82. $payload .= $this->drawlink($site["url"]);
  83. /*
  84. Draw title + description + filetype
  85. */
  86. $payload .=
  87. '<a href="' . htmlspecialchars($site["url"]) . '" class="hover" rel="noreferrer nofollow"';
  88. if($tabindex === false){
  89. $payload .= ' tabindex="-1"';
  90. }
  91. $payload .= '>';
  92. if($site["thumb"]["url"] !== null){
  93. $payload .=
  94. '<div class="thumb-wrap';
  95. switch($site["thumb"]["ratio"]){
  96. case "16:9":
  97. $size = "landscape";
  98. break;
  99. case "9:16":
  100. $payload .= " portrait";
  101. $size = "portrait";
  102. break;
  103. case "1:1":
  104. $payload .= " square";
  105. $size = "square";
  106. break;
  107. }
  108. $payload .=
  109. '">' .
  110. '<img class="thumb" src="' . $this->htmlimage($site["thumb"]["url"], $size) . '" alt="thumb">';
  111. if($duration !== null){
  112. $payload .=
  113. '<div class="duration">' .
  114. htmlspecialchars($duration) .
  115. '</div>';
  116. }
  117. $payload .=
  118. '</div>';
  119. }
  120. $payload .=
  121. '<div class="title">';
  122. if(
  123. isset($site["type"]) &&
  124. $site["type"] != "web"
  125. ){
  126. $payload .= '<div class="type">' . strtoupper($site["type"]) . '</div>';
  127. }
  128. $payload .=
  129. $this->highlighttext($keywords, $site["title"]) .
  130. '</div>';
  131. if($greentext !== null){
  132. $payload .=
  133. '<div class="greentext">' .
  134. htmlspecialchars($greentext) .
  135. '</div>';
  136. }
  137. if($site["description"] !== null){
  138. $payload .=
  139. '<div class="description">' .
  140. $this->highlighttext($keywords, $site["description"]) .
  141. '</div>';
  142. }
  143. $payload .= $customhtml;
  144. $payload .= '</a>';
  145. /*
  146. Sublinks
  147. */
  148. if(
  149. isset($site["sublink"]) &&
  150. !empty($site["sublink"])
  151. ){
  152. usort($site["sublink"], function($a, $b){
  153. return strlen($a["description"]) > strlen($b["description"]);
  154. });
  155. $payload .=
  156. '<div class="sublinks">' .
  157. '<table>';
  158. $opentr = false;
  159. for($i=0; $i<count($site["sublink"]); $i++){
  160. if(($i % 2) === 0){
  161. $opentr = true;
  162. $payload .= '<tr>';
  163. }else{
  164. $opentr = false;
  165. }
  166. $payload .=
  167. '<td>' .
  168. '<a href="' . htmlspecialchars($site["sublink"][$i]["url"]) . '" rel="noreferrer nofollow">' .
  169. '<div class="title">' .
  170. htmlspecialchars($site["sublink"][$i]["title"]) .
  171. '</div>';
  172. if(!empty($site["sublink"][$i]["date"])){
  173. $payload .=
  174. '<div class="greentext">' .
  175. date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
  176. '</div>';
  177. }
  178. if(!empty($site["sublink"][$i]["description"])){
  179. $payload .=
  180. '<div class="description">' .
  181. $this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
  182. '</div>';
  183. }
  184. $payload .= '</a></td>';
  185. if($opentr === false){
  186. $payload .= '</tr>';
  187. }
  188. }
  189. if($opentr === true){
  190. $payload .= '<td></td></tr>';
  191. }
  192. $payload .= '</table></div>';
  193. }
  194. if(
  195. isset($site["table"]) &&
  196. !empty($site["table"])
  197. ){
  198. $payload .= '<table class="info-table">';
  199. foreach($site["table"] as $title => $value){
  200. $payload .=
  201. '<tr>' .
  202. '<td>' . htmlspecialchars($title) . '</td>' .
  203. '<td>' . htmlspecialchars($value) . '</td>' .
  204. '</tr>';
  205. }
  206. $payload .= '</table>';
  207. }
  208. return $payload . '</div>';
  209. }
  210. public function highlighttext($keywords, $text){
  211. $text = htmlspecialchars($text);
  212. $keywords = explode(" ", $keywords);
  213. $regex = [];
  214. foreach($keywords as $word){
  215. $regex[] = "\b" . preg_quote($word, "/") . "\b";
  216. }
  217. $regex = "/" . implode("|", $regex) . "/i";
  218. return
  219. preg_replace(
  220. $regex,
  221. '<b>${0}</b>',
  222. $text
  223. );
  224. }
  225. function highlightcode($text){
  226. // https://www.php.net/highlight_string
  227. ini_set("highlight.comment", "c-comment");
  228. ini_set("highlight.default", "c-default");
  229. ini_set("highlight.html", "c-default");
  230. ini_set("highlight.keyword", "c-keyword");
  231. ini_set("highlight.string", "c-string");
  232. $text =
  233. trim(
  234. preg_replace(
  235. '/<\/span>$/',
  236. "", // remove stray ending span because of the <?php stuff
  237. str_replace(
  238. [
  239. '<br />',
  240. '&nbsp;'
  241. ],
  242. [
  243. "\n", // replace <br> with newlines
  244. " " // replace html entity to space
  245. ],
  246. str_replace(
  247. [
  248. // leading <?php garbage
  249. "<span style=\"color: c-default\">\n&lt;?php&nbsp;",
  250. "<code>",
  251. "</code>"
  252. ],
  253. "",
  254. highlight_string("<?php " . $text, true)
  255. )
  256. )
  257. )
  258. );
  259. // replace colors
  260. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  261. foreach($classes as $class){
  262. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  263. }
  264. return $text;
  265. }
  266. public function drawlink($link){
  267. /*
  268. Add favicon
  269. */
  270. $host = parse_url($link);
  271. $esc =
  272. explode(
  273. ".",
  274. $host["host"],
  275. 2
  276. );
  277. if(
  278. count($esc) === 2 &&
  279. $esc[0] == "www"
  280. ){
  281. $esc = $esc[1];
  282. }else{
  283. $esc = $esc[0];
  284. }
  285. $esc = substr($esc, 0, 2);
  286. $urlencode = urlencode($link);
  287. $payload =
  288. '<div class="url">' .
  289. '<button class="favicon" tabindex="-1">' .
  290. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  291. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  292. '</button>' .
  293. '<div class="favicon-dropdown">';
  294. /*
  295. Add archive links
  296. */
  297. if(
  298. $host["host"] == "boards.4chan.org" ||
  299. $host["host"] == "boards.4channel.org"
  300. ){
  301. $archives = [];
  302. $path = explode("/", $host["path"]);
  303. $count = count($path);
  304. // /pol/thread/417568063/post-shitty-memes-if-you-want-to
  305. if($count !== 0){
  306. $isboard = true;
  307. switch($path[1]){
  308. case "con":
  309. break;
  310. case "q":
  311. $archives[] = "desuarchive.org";
  312. break;
  313. case "qa":
  314. $archives[] = "desuarchive.org";
  315. break;
  316. case "qb":
  317. $archives[] = "arch.b4k.co";
  318. break;
  319. case "trash":
  320. $archives[] = "desuarchive.org";
  321. break;
  322. case "a":
  323. $archives[] = "desuarchive.org";
  324. break;
  325. case "c":
  326. $archives[] = "desuarchive.org";
  327. break;
  328. case "w":
  329. break;
  330. case "m":
  331. $archives[] = "desuarchive.org";
  332. break;
  333. case "cgl":
  334. $archives[] = "desuarchive.org";
  335. $archives[] = "warosu.org";
  336. break;
  337. case "cm":
  338. $archives[] = "boards.fireden.net";
  339. break;
  340. case "f":
  341. $archives[] = "archive.4plebs.org";
  342. break;
  343. case "n":
  344. break;
  345. case "jp":
  346. $archives[] = "warosu.org";
  347. break;
  348. case "vt":
  349. $archives[] = "warosu.org";
  350. break;
  351. case "v":
  352. $archives[] = "boards.fireden.net";
  353. $archives[] = "arch.b4k.co";
  354. break;
  355. case "vg":
  356. $archives[] = "boards.fireden.net";
  357. $archives[] = "arch.b4k.co";
  358. break;
  359. case "vm":
  360. $archives[] = "arch.b4k.co";
  361. break;
  362. case "vmg":
  363. $archives[] = "arch.b4k.co";
  364. break;
  365. case "vp":
  366. $archives[] = "arch.b4k.co";
  367. break;
  368. case "vr":
  369. $archives[] = "desuarchive.org";
  370. $archives[] = "warosu.org";
  371. break;
  372. case "vrpg":
  373. $archives[] = "arch.b4k.co";
  374. break;
  375. case "vst":
  376. $archives[] = "arch.b4k.co";
  377. break;
  378. case "co":
  379. $archives[] = "desuarchive.org";
  380. break;
  381. case "g":
  382. $archives[] = "desuarchive.org";
  383. $archives[] = "arch.b4k.co";
  384. break;
  385. case "tv":
  386. $archives[] = "archive.4plebs.org";
  387. break;
  388. case "k":
  389. $archives[] = "desuarchive.org";
  390. break;
  391. case "o":
  392. $archives[] = "archive.4plebs.org";
  393. break;
  394. case "an":
  395. $archives[] = "desuarchive.org";
  396. break;
  397. case "tg":
  398. $archives[] = "desuarchive.org";
  399. $archives[] = "archive.4plebs.org";
  400. break;
  401. case "sp":
  402. $archives[] = "archive.4plebs.org";
  403. break;
  404. case "xs":
  405. $archives[] = "eientei.xyz";
  406. break;
  407. case "pw":
  408. break;
  409. case "sci":
  410. $archives[] = "boards.fireden.net";
  411. $archives[] = "warosu.org";
  412. $archives[] = "eientei.xyz";
  413. break;
  414. case "his":
  415. $archives[] = "desuarchive.org";
  416. break;
  417. case "int":
  418. $archives[] = "desuarchive.org";
  419. break;
  420. case "out":
  421. break;
  422. case "toy":
  423. break;
  424. case "i":
  425. $archives[] = "archiveofsins.com";
  426. $archives[] = "eientei.xyz";
  427. break;
  428. case "po":
  429. break;
  430. case "p":
  431. break;
  432. case "ck":
  433. $archives[] = "warosu.org";
  434. break;
  435. case "ic":
  436. $archives[] = "boards.fireden.net";
  437. $archives[] = "warosu.org";
  438. break;
  439. case "wg":
  440. break;
  441. case "lit":
  442. $archives[] = "warosu.org";
  443. break;
  444. case "mu":
  445. $archives[] = "desuarchive.org";
  446. break;
  447. case "fa":
  448. $archives[] = "warosu.org";
  449. break;
  450. case "3":
  451. $archives[] = "warosu.org";
  452. $archives[] = "eientei.xyz";
  453. break;
  454. case "gd":
  455. break;
  456. case "diy":
  457. $archives[] = "warosu.org";
  458. break;
  459. case "wsg":
  460. $archives[] = "desuarchive.org";
  461. break;
  462. case "qst":
  463. break;
  464. case "biz":
  465. $archives[] = "warosu.org";
  466. break;
  467. case "trv":
  468. $archives[] = "archive.4plebs.org";
  469. break;
  470. case "fit":
  471. $archives[] = "desuarchive.org";
  472. break;
  473. case "x":
  474. $archives[] = "archive.4plebs.org";
  475. break;
  476. case "adv":
  477. $archives[] = "archive.4plebs.org";
  478. break;
  479. case "lgbt":
  480. $archives[] = "archiveofsins.com";
  481. break;
  482. case "mlp":
  483. $archives[] = "desuarchive.org";
  484. $archives[] = "arch.b4k.co";
  485. break;
  486. case "news":
  487. break;
  488. case "wsr":
  489. break;
  490. case "vip":
  491. break;
  492. case "b":
  493. $archives[] = "thebarchive.com";
  494. break;
  495. case "r9k":
  496. $archives[] = "desuarchive.org";
  497. break;
  498. case "pol":
  499. $archives[] = "archive.4plebs.org";
  500. break;
  501. case "bant":
  502. $archives[] = "thebarchive.com";
  503. break;
  504. case "soc":
  505. $archives[] = "archiveofsins.com";
  506. break;
  507. case "s4s":
  508. $archives[] = "archive.4plebs.org";
  509. break;
  510. case "s":
  511. $archives[] = "archiveofsins.com";
  512. break;
  513. case "hc":
  514. $archives[] = "archiveofsins.com";
  515. break;
  516. case "hm":
  517. $archives[] = "archiveofsins.com";
  518. break;
  519. case "h":
  520. $archives[] = "archiveofsins.com";
  521. break;
  522. case "e":
  523. break;
  524. case "u":
  525. $archives[] = "archiveofsins.com";
  526. break;
  527. case "d":
  528. $archives[] = "desuarchive.org";
  529. break;
  530. case "y":
  531. $archives[] = "boards.fireden.net";
  532. break;
  533. case "t":
  534. $archives[] = "archiveofsins.com";
  535. break;
  536. case "hr":
  537. $archives[] = "archive.4plebs.org";
  538. break;
  539. case "gif":
  540. break;
  541. case "aco":
  542. $archives[] = "desuarchive.org";
  543. break;
  544. case "r":
  545. $archives[] = "archiveofsins.com";
  546. break;
  547. default:
  548. $isboard = false;
  549. break;
  550. }
  551. if($isboard === true){
  552. $archives[] = "archived.moe";
  553. }
  554. $trail = "";
  555. if(
  556. isset($path[2]) &&
  557. isset($path[3]) &&
  558. $path[2] == "thread"
  559. ){
  560. $trail .= "/" . $path[1] . "/thread/" . $path[3];
  561. }elseif($isboard){
  562. $trail = "/" . $path[1] . "/";
  563. }
  564. for($i=0; $i<count($archives); $i++){
  565. $payload .=
  566. '<a href="https://' . $archives[$i] . $trail . '" class="list" target="_BLANK">' .
  567. '<img src="/favicon?s=https://' . $archives[$i] . '" alt="' . $archives[$i][0] . $archives[$i][1] . '">' .
  568. $archives[$i] .
  569. '</a>';
  570. }
  571. }
  572. }
  573. $payload .=
  574. '<a href="https://webcache.googleusercontent.com/search?q=cache:' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://google.com" alt="go">Google cache</a>' .
  575. '<a href="https://web.archive.org/web/' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.org" alt="ar">Archive.org</a>' .
  576. '<a href="https://archive.is/newest/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.is" alt="ar">Archive.is</a>' .
  577. '<a href="https://ghostarchive.org/search?term=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://ghostarchive.org" alt="gh">Ghostarchive</a>' .
  578. '<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>' .
  579. '<a href="https://megalodon.jp/?url=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://megalodon.jp" alt="me">Megalodon</a>' .
  580. '</div>';
  581. /*
  582. Draw link
  583. */
  584. $parts = explode("/", $link);
  585. $clickurl = "";
  586. // remove trailing /
  587. $c = count($parts) - 1;
  588. if($parts[$c] == ""){
  589. $parts[$c - 1] = $parts[$c - 1] . "/";
  590. unset($parts[$c]);
  591. }
  592. // merge https://site together
  593. $parts = [
  594. $parts[0] . $parts[1] . '//' . $parts[2],
  595. ...array_slice($parts, 3, count($parts) - 1)
  596. ];
  597. $c = count($parts);
  598. for($i=0; $i<$c; $i++){
  599. if($i !== 0){ $clickurl .= "/"; }
  600. $clickurl .= $parts[$i];
  601. if($i === $c - 1){
  602. $parts[$i] = rtrim($parts[$i], "/");
  603. }
  604. $payload .=
  605. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  606. htmlspecialchars(urldecode($parts[$i])) .
  607. '</a>';
  608. if($i !== $c - 1){
  609. $payload .= '<span class="separator"></span>';
  610. }
  611. }
  612. return $payload . '</div>';
  613. }
  614. public function getscraperfilters($page){
  615. $get_scraper = null;
  616. switch($page){
  617. case "web":
  618. $get_scraper = isset($_COOKIE["scraper_web"]) ? $_COOKIE["scraper_web"] : null;
  619. break;
  620. case "images":
  621. $get_scraper = isset($_COOKIE["scraper_images"]) ? $_COOKIE["scraper_images"] : null;
  622. break;
  623. case "videos":
  624. $get_scraper = isset($_COOKIE["scraper_videos"]) ? $_COOKIE["scraper_videos"] : null;
  625. break;
  626. case "news":
  627. $get_scraper = isset($_COOKIE["scraper_news"]) ? $_COOKIE["scraper_news"] : null;
  628. break;
  629. case "music":
  630. $get_scraper = isset($_COOKIE["scraper_news"]) ? $_COOKIE["scraper_news"] : null;
  631. break;
  632. }
  633. if(
  634. isset($_GET["scraper"]) &&
  635. is_string($_GET["scraper"])
  636. ){
  637. $get_scraper = $_GET["scraper"];
  638. }else{
  639. if(
  640. isset($_GET["npt"]) &&
  641. is_string($_GET["npt"])
  642. ){
  643. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  644. $get_scraper =
  645. preg_replace(
  646. '/[0-9]+$/',
  647. "",
  648. $get_scraper
  649. );
  650. }
  651. }
  652. // add search field
  653. $filters =
  654. [
  655. "s" => [
  656. "option" => "_SEARCH"
  657. ]
  658. ];
  659. // define default scrapers
  660. switch($page){
  661. case "web":
  662. $filters["scraper"] = [
  663. "display" => "Scraper",
  664. "option" => [
  665. "ddg" => "DuckDuckGo",
  666. "brave" => "Brave",
  667. "yandex" => "Yandex",
  668. //"google" => "Google",
  669. "mojeek" => "Mojeek",
  670. "marginalia" => "Marginalia",
  671. "wiby" => "wiby"
  672. ]
  673. ];
  674. break;
  675. case "images":
  676. $filters["scraper"] = [
  677. "display" => "Scraper",
  678. "option" => [
  679. "ddg" => "DuckDuckGo",
  680. "yandex" => "Yandex",
  681. "brave" => "Brave",
  682. "google" => "Google"
  683. ]
  684. ];
  685. break;
  686. case "videos":
  687. $filters["scraper"] = [
  688. "display" => "Scraper",
  689. "option" => [
  690. "yt" => "YouTube",
  691. //"fb" => "Facebook videos",
  692. "ddg" => "DuckDuckGo",
  693. "brave" => "Brave",
  694. "yandex" => "Yandex"
  695. //"google" => "Google"
  696. ]
  697. ];
  698. break;
  699. case "news":
  700. $filters["scraper"] = [
  701. "display" => "Scraper",
  702. "option" => [
  703. "ddg" => "DuckDuckGo",
  704. "brave" => "Brave",
  705. //"google" => "Google",
  706. "mojeek" => "Mojeek"
  707. ]
  708. ];
  709. case "music":
  710. $filters["scraper"] = [
  711. "display" => "Scraper",
  712. "option" => [
  713. "sc" => "SoundCloud"
  714. ]
  715. ];
  716. break;
  717. }
  718. // get scraper name from user input, or default out to preferred scraper
  719. $scraper_out = null;
  720. $first = true;
  721. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  722. if($first === true){
  723. $first = $scraper_name;
  724. }
  725. if($scraper_name == $get_scraper){
  726. $scraper_out = $scraper_name;
  727. }
  728. }
  729. if($scraper_out === null){
  730. $scraper_out = $first;
  731. }
  732. switch($scraper_out){
  733. case "ddg":
  734. include "scraper/ddg.php";
  735. $lib = new ddg();
  736. break;
  737. case "brave":
  738. include "scraper/brave.php";
  739. $lib = new brave();
  740. break;
  741. case "yt";
  742. include "scraper/youtube.php";
  743. $lib = new youtube();
  744. break;
  745. case "yandex":
  746. include "scraper/yandex.php";
  747. $lib = new yandex();
  748. break;
  749. case "google":
  750. include "scraper/google.php";
  751. $lib = new google();
  752. break;
  753. /*
  754. case "fb":
  755. include "scraper/facebook.php";
  756. $lib = new facebook();
  757. break;*/
  758. case "mojeek":
  759. include "scraper/mojeek.php";
  760. $lib = new mojeek();
  761. break;
  762. case "marginalia":
  763. include "scraper/marginalia.php";
  764. $lib = new marginalia();
  765. break;
  766. case "wiby":
  767. include "scraper/wiby.php";
  768. $lib = new wiby();
  769. break;
  770. case "sc":
  771. include "scraper/sc.php";
  772. $lib = new sc();
  773. break;
  774. }
  775. // set scraper on $_GET
  776. $_GET["scraper"] = $scraper_out;
  777. // set nsfw on $_GET
  778. if(
  779. isset($_COOKIE["nsfw"]) &&
  780. !isset($_GET["nsfw"])
  781. ){
  782. $_GET["nsfw"] = $_COOKIE["nsfw"];
  783. }
  784. return
  785. [
  786. $lib,
  787. array_merge_recursive(
  788. $filters,
  789. $lib->getfilters($page)
  790. )
  791. ];
  792. }
  793. public function parsegetfilters($parameters, $whitelist){
  794. $sanitized = [];
  795. // add npt token
  796. if(
  797. isset($parameters["npt"]) &&
  798. is_string($parameters["npt"])
  799. ){
  800. $sanitized["npt"] = $parameters["npt"];
  801. }else{
  802. $sanitized["npt"] = false;
  803. }
  804. // we're iterating over $whitelist, so
  805. // you can't polluate $sanitized with useless
  806. // parameters
  807. foreach($whitelist as $parameter => $value){
  808. if(isset($parameters[$parameter])){
  809. if(!is_string($parameters[$parameter])){
  810. $sanitized[$parameter] = null;
  811. continue;
  812. }
  813. // parameter is already set, use that value
  814. $sanitized[$parameter] = $parameters[$parameter];
  815. }else{
  816. // parameter is not set, add it
  817. if(is_string($value["option"])){
  818. // special field: set default value manually
  819. switch($value["option"]){
  820. case "_DATE":
  821. // no date set
  822. $sanitized[$parameter] = false;
  823. break;
  824. case "_SEARCH":
  825. // no search set
  826. $sanitized[$parameter] = "";
  827. break;
  828. }
  829. }else{
  830. // set a default value
  831. $sanitized[$parameter] = array_keys($value["option"])[0];
  832. }
  833. }
  834. // sanitize input
  835. if(is_array($value["option"])){
  836. if(
  837. !in_array(
  838. $sanitized[$parameter],
  839. $keys = array_keys($value["option"])
  840. )
  841. ){
  842. $sanitized[$parameter] = $keys[0];
  843. }
  844. }else{
  845. // sanitize search & string
  846. switch($value["option"]){
  847. case "_DATE":
  848. if($sanitized[$parameter] !== false){
  849. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  850. if($sanitized[$parameter] <= 0){
  851. $sanitized[$parameter] = false;
  852. }
  853. }
  854. break;
  855. case "_SEARCH":
  856. // get search string & bang
  857. $sanitized[$parameter] = trim($sanitized[$parameter]);
  858. $sanitized["bang"] = "";
  859. if(
  860. strlen($sanitized[$parameter]) !== 0 &&
  861. $sanitized[$parameter][0] == "!"
  862. ){
  863. $sanitized[$parameter] = explode(" ", $sanitized[$parameter], 2);
  864. $sanitized["bang"] = trim($sanitized[$parameter][0]);
  865. if(count($sanitized[$parameter]) === 2){
  866. $sanitized[$parameter] = trim($sanitized[$parameter][1]);
  867. }else{
  868. $sanitized[$parameter] = "";
  869. }
  870. $sanitized["bang"] = ltrim($sanitized["bang"], "!");
  871. }
  872. $sanitized[$parameter] = ltrim($sanitized[$parameter], "! \n\r\t\v\x00");
  873. }
  874. }
  875. }
  876. // invert dates if needed
  877. if(
  878. isset($sanitized["older"]) &&
  879. isset($sanitized["newer"]) &&
  880. $sanitized["newer"] !== false &&
  881. $sanitized["older"] !== false &&
  882. $sanitized["newer"] > $sanitized["older"]
  883. ){
  884. // invert
  885. [
  886. $sanitized["older"],
  887. $sanitized["newer"]
  888. ] = [
  889. $sanitized["newer"],
  890. $sanitized["older"]
  891. ];
  892. }
  893. return $sanitized;
  894. }
  895. public function s_to_timestamp($seconds){
  896. if(is_string($seconds)){
  897. return "LIVE";
  898. }
  899. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  900. }
  901. public function generatehtmltabs($page, $query){
  902. $html = null;
  903. foreach(["web", "images", "videos", "news", "music"] as $type){
  904. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  905. if(!empty($params)){
  906. $html .= $params;
  907. }
  908. $html .= '" class="tab';
  909. if($type == $page){
  910. $html .= ' selected';
  911. }
  912. $html .= '">' . ucfirst($type) . '</a>';
  913. }
  914. return $html;
  915. }
  916. public function generatehtmlfilters($filters, $params){
  917. $html = null;
  918. foreach($filters as $filter_name => $filter_values){
  919. if(!isset($filter_values["display"])){
  920. continue;
  921. }
  922. $output = true;
  923. $tmp =
  924. '<div class="filter">' .
  925. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  926. if(is_array($filter_values["option"])){
  927. $tmp .= '<select name="' . $filter_name . '">';
  928. foreach($filter_values["option"] as $option_name => $option_title){
  929. $tmp .= '<option value="' . $option_name . '"';
  930. if($params[$filter_name] == $option_name){
  931. $tmp .= ' selected';
  932. }
  933. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  934. }
  935. $tmp .= '</select>';
  936. }else{
  937. switch($filter_values["option"]){
  938. case "_DATE":
  939. $tmp .= '<input type="date" name="' . $filter_name . '"';
  940. if($params[$filter_name] !== false){
  941. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  942. }
  943. $tmp .= '>';
  944. break;
  945. default:
  946. $output = false;
  947. break;
  948. }
  949. }
  950. $tmp .= '</div>';
  951. if($output === true){
  952. $html .= $tmp;
  953. }
  954. }
  955. return $html;
  956. }
  957. public function buildquery($gets, $ommit = false){
  958. $out = [];
  959. foreach($gets as $key => $value){
  960. if(
  961. $value == null ||
  962. $value == false ||
  963. $key == "npt" ||
  964. $key == "extendedsearch" ||
  965. $value == "any" ||
  966. $value == "all" ||
  967. (
  968. $ommit === true &&
  969. $key == "s"
  970. )
  971. ){
  972. continue;
  973. }
  974. if(
  975. $key == "older" ||
  976. $key == "newer"
  977. ){
  978. $value = date("Y-m-d", (int)$value);
  979. }
  980. $out[$key] = $value;
  981. }
  982. return http_build_query($out);
  983. }
  984. public function htmlimage($image, $format){
  985. if(
  986. preg_match(
  987. '/^data:/',
  988. $image
  989. )
  990. ){
  991. return htmlspecialchars($image);
  992. }
  993. return "/proxy.php?i=" . urlencode($image) . "&s=" . $format;
  994. }
  995. public function htmlnextpage($gets, $npt, $page){
  996. $query = $this->buildquery($gets);
  997. return $page . "?" . $query . "&npt=" . $npt;
  998. }
  999. }