frontend.php 25 KB

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