1
0

frontend.php 27 KB

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