1
0

frontend.php 26 KB

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