frontend.php 26 KB

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