1
0

frontend.php 27 KB

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