1
0

frontend.php 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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="/proxy?i=' . urlencode($site["thumb"]["url"]) . '&s=' . $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. "google" => "Google"
  676. ]
  677. ];
  678. break;
  679. case "videos":
  680. $filters["scraper"] = [
  681. "display" => "Scraper",
  682. "option" => [
  683. "yt" => "YouTube",
  684. "ddg" => "DuckDuckGo",
  685. "google" => "Google"
  686. ]
  687. ];
  688. break;
  689. case "news":
  690. $filters["scraper"] = [
  691. "display" => "Scraper",
  692. "option" => [
  693. "ddg" => "DuckDuckGo",
  694. "brave" => "Brave",
  695. "google" => "Google",
  696. "mojeek" => "Mojeek"
  697. ]
  698. ];
  699. break;
  700. }
  701. // get scraper name from user input, or default out to preferred scraper
  702. $scraper_out = null;
  703. $first = true;
  704. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  705. if($first === true){
  706. $first = $scraper_name;
  707. }
  708. if($scraper_name == $get_scraper){
  709. $scraper_out = $scraper_name;
  710. }
  711. }
  712. if($scraper_out === null){
  713. $scraper_out = $first;
  714. }
  715. switch($scraper_out){
  716. case "ddg":
  717. include "scraper/ddg.php";
  718. $lib = new ddg();
  719. break;
  720. case "brave":
  721. include "scraper/brave.php";
  722. $lib = new brave();
  723. break;
  724. case "yt";
  725. include "scraper/youtube.php";
  726. $lib = new youtube();
  727. break;
  728. case "yandex":
  729. include "scraper/yandex.php";
  730. $lib = new yandex();
  731. break;
  732. case "google":
  733. include "scraper/google.php";
  734. $lib = new google();
  735. break;
  736. case "mojeek":
  737. include "scraper/mojeek.php";
  738. $lib = new mojeek();
  739. break;
  740. case "marginalia":
  741. include "scraper/marginalia.php";
  742. $lib = new marginalia();
  743. break;
  744. case "wiby":
  745. include "scraper/wiby.php";
  746. $lib = new wiby();
  747. break;
  748. }
  749. // set scraper on $_GET
  750. $_GET["scraper"] = $scraper_out;
  751. // set nsfw on $_GET
  752. if(
  753. isset($_COOKIE["nsfw"]) &&
  754. !isset($_GET["nsfw"])
  755. ){
  756. $_GET["nsfw"] = $_COOKIE["nsfw"];
  757. }
  758. return
  759. [
  760. $lib,
  761. array_merge_recursive(
  762. $filters,
  763. $lib->getfilters($page)
  764. )
  765. ];
  766. }
  767. public function parsegetfilters($parameters, $whitelist){
  768. $sanitized = [];
  769. // add npt token
  770. if(
  771. isset($parameters["npt"]) &&
  772. is_string($parameters["npt"])
  773. ){
  774. $sanitized["npt"] = $parameters["npt"];
  775. }else{
  776. $sanitized["npt"] = false;
  777. }
  778. // we're iterating over $whitelist, so
  779. // you can't polluate $sanitized with useless
  780. // parameters
  781. foreach($whitelist as $parameter => $value){
  782. if(isset($parameters[$parameter])){
  783. if(!is_string($parameters[$parameter])){
  784. $sanitized[$parameter] = null;
  785. continue;
  786. }
  787. // parameter is already set, use that value
  788. $sanitized[$parameter] = $parameters[$parameter];
  789. }else{
  790. // parameter is not set, add it
  791. if(is_string($value["option"])){
  792. // special field: set default value manually
  793. switch($value["option"]){
  794. case "_DATE":
  795. // no date set
  796. $sanitized[$parameter] = false;
  797. break;
  798. case "_SEARCH":
  799. // no search set
  800. $sanitized[$parameter] = "";
  801. break;
  802. }
  803. }else{
  804. // set a default value
  805. $sanitized[$parameter] = array_keys($value["option"])[0];
  806. }
  807. }
  808. // sanitize input
  809. if(is_array($value["option"])){
  810. if(
  811. !in_array(
  812. $sanitized[$parameter],
  813. $keys = array_keys($value["option"])
  814. )
  815. ){
  816. $sanitized[$parameter] = $keys[0];
  817. }
  818. }else{
  819. // sanitize search & string
  820. switch($value["option"]){
  821. case "_DATE":
  822. if($sanitized[$parameter] !== false){
  823. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  824. if($sanitized[$parameter] <= 0){
  825. $sanitized[$parameter] = false;
  826. }
  827. }
  828. break;
  829. case "_SEARCH":
  830. // get search string & bang
  831. $sanitized[$parameter] = trim($sanitized[$parameter]);
  832. $sanitized["bang"] = "";
  833. if(
  834. strlen($sanitized[$parameter]) !== 0 &&
  835. $sanitized[$parameter][0] == "!"
  836. ){
  837. $sanitized[$parameter] = explode(" ", $sanitized[$parameter], 2);
  838. $sanitized["bang"] = trim($sanitized[$parameter][0]);
  839. if(count($sanitized[$parameter]) === 2){
  840. $sanitized[$parameter] = trim($sanitized[$parameter][1]);
  841. }else{
  842. $sanitized[$parameter] = "";
  843. }
  844. $sanitized["bang"] = ltrim($sanitized["bang"], "!");
  845. }
  846. $sanitized[$parameter] = ltrim($sanitized[$parameter], "! \n\r\t\v\x00");
  847. }
  848. }
  849. }
  850. // invert dates if needed
  851. if(
  852. isset($sanitized["older"]) &&
  853. isset($sanitized["newer"]) &&
  854. $sanitized["newer"] !== false &&
  855. $sanitized["older"] !== false &&
  856. $sanitized["newer"] > $sanitized["older"]
  857. ){
  858. // invert
  859. [
  860. $sanitized["older"],
  861. $sanitized["newer"]
  862. ] = [
  863. $sanitized["newer"],
  864. $sanitized["older"]
  865. ];
  866. }
  867. return $sanitized;
  868. }
  869. public function s_to_timestamp($seconds){
  870. if(is_string($seconds)){
  871. return "LIVE";
  872. }
  873. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  874. }
  875. public function generatehtmltabs($page, $query){
  876. $html = null;
  877. foreach(["web", "images", "videos", "news"] as $type){
  878. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  879. if(!empty($params)){
  880. $html .= $params;
  881. }
  882. $html .= '" class="tab';
  883. if($type == $page){
  884. $html .= ' selected';
  885. }
  886. $html .= '">' . ucfirst($type) . '</a>';
  887. }
  888. return $html;
  889. }
  890. public function generatehtmlfilters($filters, $params){
  891. $html = null;
  892. foreach($filters as $filter_name => $filter_values){
  893. if(!isset($filter_values["display"])){
  894. continue;
  895. }
  896. $output = true;
  897. $tmp =
  898. '<div class="filter">' .
  899. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  900. if(is_array($filter_values["option"])){
  901. $tmp .= '<select name="' . $filter_name . '">';
  902. foreach($filter_values["option"] as $option_name => $option_title){
  903. $tmp .= '<option value="' . $option_name . '"';
  904. if($params[$filter_name] == $option_name){
  905. $tmp .= ' selected';
  906. }
  907. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  908. }
  909. $tmp .= '</select>';
  910. }else{
  911. switch($filter_values["option"]){
  912. case "_DATE":
  913. $tmp .= '<input type="date" name="' . $filter_name . '"';
  914. if($params[$filter_name] !== false){
  915. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  916. }
  917. $tmp .= '>';
  918. break;
  919. default:
  920. $output = false;
  921. break;
  922. }
  923. }
  924. $tmp .= '</div>';
  925. if($output === true){
  926. $html .= $tmp;
  927. }
  928. }
  929. return $html;
  930. }
  931. public function buildquery($gets, $ommit = false){
  932. $out = [];
  933. foreach($gets as $key => $value){
  934. if(
  935. $value == null ||
  936. $value == false ||
  937. $key == "npt" ||
  938. $key == "extendedsearch" ||
  939. $value == "any" ||
  940. $value == "all" ||
  941. (
  942. $ommit === true &&
  943. $key == "s"
  944. )
  945. ){
  946. continue;
  947. }
  948. $out[$key] = $value;
  949. }
  950. return http_build_query($out);
  951. }
  952. public function htmlnextpage($gets, $npt, $page){
  953. $query = $this->buildquery($gets);
  954. return $page . "?" . $query . "&npt=" . $npt;
  955. }
  956. }