1
0

frontend.php 26 KB

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