1
0

frontend.php 27 KB

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