frontend.php 25 KB

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