frontend.php 28 KB

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