frontend.php 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. '/<\/span>$/',
  301. "", // remove stray ending span because of the <?php stuff
  302. str_replace(
  303. [
  304. '<br />',
  305. '&nbsp;'
  306. ],
  307. [
  308. "\n", // replace <br> with newlines
  309. " " // replace html entity to space
  310. ],
  311. str_replace(
  312. [
  313. // leading <?php garbage
  314. "<span style=\"color: c-default\">\n&lt;?php&nbsp;",
  315. "<code>",
  316. "</code>"
  317. ],
  318. "",
  319. highlight_string("<?php " . $text, true)
  320. )
  321. )
  322. )
  323. );
  324. // replace colors
  325. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  326. foreach($classes as $class){
  327. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  328. }
  329. return $text;
  330. }
  331. public function drawlink($link){
  332. /*
  333. Add favicon
  334. */
  335. $host = parse_url($link);
  336. $esc =
  337. explode(
  338. ".",
  339. $host["host"],
  340. 2
  341. );
  342. if(
  343. count($esc) === 2 &&
  344. $esc[0] == "www"
  345. ){
  346. $esc = $esc[1];
  347. }else{
  348. $esc = $esc[0];
  349. }
  350. $esc = substr($esc, 0, 2);
  351. $urlencode = urlencode($link);
  352. $payload =
  353. '<div class="url">' .
  354. '<button class="favicon" tabindex="-1">' .
  355. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  356. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  357. '</button>' .
  358. '<div class="favicon-dropdown">';
  359. /*
  360. Add archive links
  361. */
  362. if(
  363. $host["host"] == "boards.4chan.org" ||
  364. $host["host"] == "boards.4channel.org"
  365. ){
  366. $archives = [];
  367. $path = explode("/", $host["path"]);
  368. $count = count($path);
  369. // /pol/thread/417568063/post-shitty-memes-if-you-want-to
  370. if($count !== 0){
  371. $isboard = true;
  372. switch($path[1]){
  373. case "con":
  374. break;
  375. case "q":
  376. $archives[] = "desuarchive.org";
  377. break;
  378. case "qa":
  379. $archives[] = "desuarchive.org";
  380. break;
  381. case "qb":
  382. $archives[] = "arch.b4k.co";
  383. break;
  384. case "trash":
  385. $archives[] = "desuarchive.org";
  386. break;
  387. case "a":
  388. $archives[] = "desuarchive.org";
  389. break;
  390. case "c":
  391. $archives[] = "desuarchive.org";
  392. break;
  393. case "w":
  394. break;
  395. case "m":
  396. $archives[] = "desuarchive.org";
  397. break;
  398. case "cgl":
  399. $archives[] = "desuarchive.org";
  400. $archives[] = "warosu.org";
  401. break;
  402. case "f":
  403. $archives[] = "archive.4plebs.org";
  404. break;
  405. case "n":
  406. break;
  407. case "jp":
  408. $archives[] = "warosu.org";
  409. break;
  410. case "vt":
  411. $archives[] = "warosu.org";
  412. break;
  413. case "v":
  414. $archives[] = "arch.b4k.co";
  415. break;
  416. case "vg":
  417. $archives[] = "arch.b4k.co";
  418. break;
  419. case "vm":
  420. $archives[] = "arch.b4k.co";
  421. break;
  422. case "vmg":
  423. $archives[] = "arch.b4k.co";
  424. break;
  425. case "vp":
  426. $archives[] = "arch.b4k.co";
  427. break;
  428. case "vr":
  429. $archives[] = "desuarchive.org";
  430. $archives[] = "warosu.org";
  431. break;
  432. case "vrpg":
  433. $archives[] = "arch.b4k.co";
  434. break;
  435. case "vst":
  436. $archives[] = "arch.b4k.co";
  437. break;
  438. case "co":
  439. $archives[] = "desuarchive.org";
  440. break;
  441. case "g":
  442. $archives[] = "desuarchive.org";
  443. $archives[] = "arch.b4k.co";
  444. break;
  445. case "tv":
  446. $archives[] = "archive.4plebs.org";
  447. break;
  448. case "k":
  449. $archives[] = "desuarchive.org";
  450. break;
  451. case "o":
  452. $archives[] = "archive.4plebs.org";
  453. break;
  454. case "an":
  455. $archives[] = "desuarchive.org";
  456. break;
  457. case "tg":
  458. $archives[] = "desuarchive.org";
  459. $archives[] = "archive.4plebs.org";
  460. break;
  461. case "sp":
  462. $archives[] = "archive.4plebs.org";
  463. break;
  464. case "xs":
  465. $archives[] = "eientei.xyz";
  466. break;
  467. case "pw":
  468. break;
  469. case "sci":
  470. $archives[] = "warosu.org";
  471. $archives[] = "eientei.xyz";
  472. break;
  473. case "his":
  474. $archives[] = "desuarchive.org";
  475. break;
  476. case "int":
  477. $archives[] = "desuarchive.org";
  478. break;
  479. case "out":
  480. break;
  481. case "toy":
  482. break;
  483. case "i":
  484. $archives[] = "archiveofsins.com";
  485. $archives[] = "eientei.xyz";
  486. break;
  487. case "po":
  488. break;
  489. case "p":
  490. break;
  491. case "ck":
  492. $archives[] = "warosu.org";
  493. break;
  494. case "ic":
  495. $archives[] = "warosu.org";
  496. break;
  497. case "wg":
  498. break;
  499. case "lit":
  500. $archives[] = "warosu.org";
  501. break;
  502. case "mu":
  503. $archives[] = "desuarchive.org";
  504. break;
  505. case "fa":
  506. $archives[] = "warosu.org";
  507. break;
  508. case "3":
  509. $archives[] = "warosu.org";
  510. $archives[] = "eientei.xyz";
  511. break;
  512. case "gd":
  513. break;
  514. case "diy":
  515. $archives[] = "warosu.org";
  516. break;
  517. case "wsg":
  518. $archives[] = "desuarchive.org";
  519. break;
  520. case "qst":
  521. break;
  522. case "biz":
  523. $archives[] = "warosu.org";
  524. break;
  525. case "trv":
  526. $archives[] = "archive.4plebs.org";
  527. break;
  528. case "fit":
  529. $archives[] = "desuarchive.org";
  530. break;
  531. case "x":
  532. $archives[] = "archive.4plebs.org";
  533. break;
  534. case "adv":
  535. $archives[] = "archive.4plebs.org";
  536. break;
  537. case "lgbt":
  538. $archives[] = "archiveofsins.com";
  539. break;
  540. case "mlp":
  541. $archives[] = "desuarchive.org";
  542. $archives[] = "arch.b4k.co";
  543. break;
  544. case "news":
  545. break;
  546. case "wsr":
  547. break;
  548. case "vip":
  549. break;
  550. case "b":
  551. $archives[] = "thebarchive.com";
  552. break;
  553. case "r9k":
  554. $archives[] = "desuarchive.org";
  555. break;
  556. case "pol":
  557. $archives[] = "archive.4plebs.org";
  558. break;
  559. case "bant":
  560. $archives[] = "thebarchive.com";
  561. break;
  562. case "soc":
  563. $archives[] = "archiveofsins.com";
  564. break;
  565. case "s4s":
  566. $archives[] = "archive.4plebs.org";
  567. break;
  568. case "s":
  569. $archives[] = "archiveofsins.com";
  570. break;
  571. case "hc":
  572. $archives[] = "archiveofsins.com";
  573. break;
  574. case "hm":
  575. $archives[] = "archiveofsins.com";
  576. break;
  577. case "h":
  578. $archives[] = "archiveofsins.com";
  579. break;
  580. case "e":
  581. break;
  582. case "u":
  583. $archives[] = "archiveofsins.com";
  584. break;
  585. case "d":
  586. $archives[] = "desuarchive.org";
  587. break;
  588. case "t":
  589. $archives[] = "archiveofsins.com";
  590. break;
  591. case "hr":
  592. $archives[] = "archive.4plebs.org";
  593. break;
  594. case "gif":
  595. break;
  596. case "aco":
  597. $archives[] = "desuarchive.org";
  598. break;
  599. case "r":
  600. $archives[] = "archiveofsins.com";
  601. break;
  602. default:
  603. $isboard = false;
  604. break;
  605. }
  606. if($isboard === true){
  607. $archives[] = "archived.moe";
  608. }
  609. $trail = "";
  610. if(
  611. isset($path[2]) &&
  612. isset($path[3]) &&
  613. $path[2] == "thread"
  614. ){
  615. $trail .= "/" . $path[1] . "/thread/" . $path[3];
  616. }elseif($isboard){
  617. $trail = "/" . $path[1] . "/";
  618. }
  619. for($i=0; $i<count($archives); $i++){
  620. $payload .=
  621. '<a href="https://' . $archives[$i] . $trail . '" class="list" target="_BLANK">' .
  622. '<img src="/favicon?s=https://' . $archives[$i] . '" alt="' . $archives[$i][0] . $archives[$i][1] . '">' .
  623. $archives[$i] .
  624. '</a>';
  625. }
  626. }
  627. }
  628. $payload .=
  629. '<a href="https://web.archive.org/web/' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.org" alt="ar">Archive.org</a>' .
  630. '<a href="https://archive.ph/newest/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.is" alt="ar">Archive.is</a>' .
  631. '<a href="https://ghostarchive.org/search?term=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://ghostarchive.org" alt="gh">Ghostarchive</a>' .
  632. '<a href="https://arquivo.pt/wayback/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://arquivo.pt" alt="ar">Arquivo.pt</a>' .
  633. '<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>' .
  634. '<a href="https://megalodon.jp/?url=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://megalodon.jp" alt="me">Megalodon</a>' .
  635. '</div>';
  636. /*
  637. Draw link
  638. */
  639. $parts = explode("/", $link);
  640. $clickurl = "";
  641. // remove trailing /
  642. $c = count($parts) - 1;
  643. if($parts[$c] == ""){
  644. $parts[$c - 1] = $parts[$c - 1] . "/";
  645. unset($parts[$c]);
  646. }
  647. // merge https://site together
  648. $parts = [
  649. $parts[0] . $parts[1] . '//' . $parts[2],
  650. ...array_slice($parts, 3, count($parts) - 1)
  651. ];
  652. $c = count($parts);
  653. for($i=0; $i<$c; $i++){
  654. if($i !== 0){ $clickurl .= "/"; }
  655. $clickurl .= $parts[$i];
  656. if($i === $c - 1){
  657. $parts[$i] = rtrim($parts[$i], "/");
  658. }
  659. $payload .=
  660. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  661. htmlspecialchars(urldecode($parts[$i])) .
  662. '</a>';
  663. if($i !== $c - 1){
  664. $payload .= '<span class="separator"></span>';
  665. }
  666. }
  667. return $payload . '</div>';
  668. }
  669. public function getscraperfilters($page){
  670. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  671. if(
  672. isset($_GET["scraper"]) &&
  673. is_string($_GET["scraper"])
  674. ){
  675. $get_scraper = $_GET["scraper"];
  676. }else{
  677. if(
  678. isset($_GET["npt"]) &&
  679. is_string($_GET["npt"])
  680. ){
  681. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  682. $get_scraper =
  683. preg_replace(
  684. '/[0-9]+$/',
  685. "",
  686. $get_scraper
  687. );
  688. }
  689. }
  690. // add search field
  691. $filters =
  692. [
  693. "s" => [
  694. "option" => "_SEARCH"
  695. ]
  696. ];
  697. // define default scrapers
  698. switch($page){
  699. case "web":
  700. $filters["scraper"] = [
  701. "display" => "Scraper",
  702. "option" => [
  703. "ddg" => "DuckDuckGo",
  704. "brave" => "Brave",
  705. "yandex" => "Yandex",
  706. "google" => "Google",
  707. "google_cse" => "Google CSE",
  708. "startpage" => "Startpage",
  709. "qwant" => "Qwant",
  710. "ghostery" => "Ghostery",
  711. "yep" => "Yep",
  712. "greppr" => "Greppr",
  713. "crowdview" => "Crowdview",
  714. "mwmbl" => "Mwmbl",
  715. "mojeek" => "Mojeek",
  716. "solofield" => "Solofield",
  717. "marginalia" => "Marginalia",
  718. "wiby" => "wiby",
  719. "curlie" => "Curlie"
  720. ]
  721. ];
  722. break;
  723. case "images":
  724. $filters["scraper"] = [
  725. "display" => "Scraper",
  726. "option" => [
  727. "ddg" => "DuckDuckGo",
  728. "yandex" => "Yandex",
  729. "brave" => "Brave",
  730. "google" => "Google",
  731. "google_cse" => "Google CSE",
  732. "startpage" => "Startpage",
  733. "qwant" => "Qwant",
  734. "yep" => "Yep",
  735. "solofield" => "Solofield",
  736. //"pinterest" => "Pinterest",
  737. "imgur" => "Imgur",
  738. "ftm" => "FindThatMeme"
  739. ]
  740. ];
  741. break;
  742. case "videos":
  743. $filters["scraper"] = [
  744. "display" => "Scraper",
  745. "option" => [
  746. "yt" => "YouTube",
  747. //"fb" => "Facebook videos",
  748. "ddg" => "DuckDuckGo",
  749. "brave" => "Brave",
  750. "yandex" => "Yandex",
  751. "google" => "Google",
  752. "startpage" => "Startpage",
  753. "qwant" => "Qwant",
  754. "solofield" => "Solofield"
  755. ]
  756. ];
  757. break;
  758. case "news":
  759. $filters["scraper"] = [
  760. "display" => "Scraper",
  761. "option" => [
  762. "ddg" => "DuckDuckGo",
  763. "brave" => "Brave",
  764. "google" => "Google",
  765. "startpage" => "Startpage",
  766. "qwant" => "Qwant",
  767. "yep" => "Yep",
  768. "mojeek" => "Mojeek"
  769. ]
  770. ];
  771. break;
  772. case "music":
  773. $filters["scraper"] = [
  774. "display" => "Scraper",
  775. "option" => [
  776. "sc" => "SoundCloud"
  777. //"spotify" => "Spotify"
  778. ]
  779. ];
  780. break;
  781. }
  782. // get scraper name from user input, or default out to preferred scraper
  783. $scraper_out = null;
  784. $first = true;
  785. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  786. if($first === true){
  787. $first = $scraper_name;
  788. }
  789. if($scraper_name == $get_scraper){
  790. $scraper_out = $scraper_name;
  791. }
  792. }
  793. if($scraper_out === null){
  794. $scraper_out = $first;
  795. }
  796. include "scraper/$scraper_out.php";
  797. $lib = new $scraper_out();
  798. // set scraper on $_GET
  799. $_GET["scraper"] = $scraper_out;
  800. // set nsfw on $_GET
  801. if(
  802. isset($_COOKIE["nsfw"]) &&
  803. !isset($_GET["nsfw"])
  804. ){
  805. $_GET["nsfw"] = $_COOKIE["nsfw"];
  806. }
  807. return
  808. [
  809. $lib,
  810. array_merge_recursive(
  811. $filters,
  812. $lib->getfilters($page)
  813. )
  814. ];
  815. }
  816. public function parsegetfilters($parameters, $whitelist){
  817. $sanitized = [];
  818. // add npt token
  819. if(
  820. isset($parameters["npt"]) &&
  821. is_string($parameters["npt"])
  822. ){
  823. $sanitized["npt"] = $parameters["npt"];
  824. }else{
  825. $sanitized["npt"] = false;
  826. }
  827. // we're iterating over $whitelist, so
  828. // you can't polluate $sanitized with useless
  829. // parameters
  830. foreach($whitelist as $parameter => $value){
  831. if(isset($parameters[$parameter])){
  832. if(!is_string($parameters[$parameter])){
  833. $sanitized[$parameter] = null;
  834. continue;
  835. }
  836. // parameter is already set, use that value
  837. $sanitized[$parameter] = $parameters[$parameter];
  838. }else{
  839. // parameter is not set, add it
  840. if(is_string($value["option"])){
  841. // special field: set default value manually
  842. switch($value["option"]){
  843. case "_DATE":
  844. // no date set
  845. $sanitized[$parameter] = false;
  846. break;
  847. case "_SEARCH":
  848. // no search set
  849. $sanitized[$parameter] = "";
  850. break;
  851. }
  852. }else{
  853. // set a default value
  854. $sanitized[$parameter] = array_keys($value["option"])[0];
  855. }
  856. }
  857. // sanitize input
  858. if(is_array($value["option"])){
  859. if(
  860. !in_array(
  861. $sanitized[$parameter],
  862. $keys = array_keys($value["option"])
  863. )
  864. ){
  865. $sanitized[$parameter] = $keys[0];
  866. }
  867. }else{
  868. // sanitize search & string
  869. switch($value["option"]){
  870. case "_DATE":
  871. if($sanitized[$parameter] !== false){
  872. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  873. if($sanitized[$parameter] <= 0){
  874. $sanitized[$parameter] = false;
  875. }
  876. }
  877. break;
  878. case "_SEARCH":
  879. // get search string
  880. $sanitized["s"] = trim($sanitized[$parameter]);
  881. }
  882. }
  883. }
  884. // invert dates if needed
  885. if(
  886. isset($sanitized["older"]) &&
  887. isset($sanitized["newer"]) &&
  888. $sanitized["newer"] !== false &&
  889. $sanitized["older"] !== false &&
  890. $sanitized["newer"] > $sanitized["older"]
  891. ){
  892. // invert
  893. [
  894. $sanitized["older"],
  895. $sanitized["newer"]
  896. ] = [
  897. $sanitized["newer"],
  898. $sanitized["older"]
  899. ];
  900. }
  901. return $sanitized;
  902. }
  903. public function s_to_timestamp($seconds){
  904. if(is_string($seconds)){
  905. return "LIVE";
  906. }
  907. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  908. }
  909. public function generatehtmltabs($page, $query){
  910. $html = null;
  911. foreach(["web", "images", "videos", "news", "music"] as $type){
  912. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  913. if(!empty($params)){
  914. $html .= $params;
  915. }
  916. $html .= '" class="tab';
  917. if($type == $page){
  918. $html .= ' selected';
  919. }
  920. $html .= '">' . ucfirst($type) . '</a>';
  921. }
  922. return $html;
  923. }
  924. public function generatehtmlfilters($filters, $params){
  925. $html = null;
  926. foreach($filters as $filter_name => $filter_values){
  927. if(!isset($filter_values["display"])){
  928. continue;
  929. }
  930. $output = true;
  931. $tmp =
  932. '<div class="filter">' .
  933. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  934. if(is_array($filter_values["option"])){
  935. $tmp .= '<select name="' . $filter_name . '">';
  936. foreach($filter_values["option"] as $option_name => $option_title){
  937. $tmp .= '<option value="' . $option_name . '"';
  938. if($params[$filter_name] == $option_name){
  939. $tmp .= ' selected';
  940. }
  941. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  942. }
  943. $tmp .= '</select>';
  944. }else{
  945. switch($filter_values["option"]){
  946. case "_DATE":
  947. $tmp .= '<input type="date" name="' . $filter_name . '"';
  948. if($params[$filter_name] !== false){
  949. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  950. }
  951. $tmp .= '>';
  952. break;
  953. default:
  954. $output = false;
  955. break;
  956. }
  957. }
  958. $tmp .= '</div>';
  959. if($output === true){
  960. $html .= $tmp;
  961. }
  962. }
  963. return $html;
  964. }
  965. public function buildquery($gets, $ommit = false){
  966. $out = [];
  967. foreach($gets as $key => $value){
  968. if(
  969. $value == null ||
  970. $value == false ||
  971. $key == "npt" ||
  972. $key == "extendedsearch" ||
  973. $value == "any" ||
  974. $value == "all" ||
  975. $key == "spellcheck" ||
  976. (
  977. $ommit === true &&
  978. $key == "s"
  979. )
  980. ){
  981. continue;
  982. }
  983. if(
  984. $key == "older" ||
  985. $key == "newer"
  986. ){
  987. $value = date("Y-m-d", (int)$value);
  988. }
  989. $out[$key] = $value;
  990. }
  991. return http_build_query($out);
  992. }
  993. public function htmlimage($image, $format){
  994. if(
  995. preg_match(
  996. '/^data:/',
  997. $image
  998. )
  999. ){
  1000. return htmlspecialchars($image);
  1001. }
  1002. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  1003. }
  1004. public function htmlnextpage($gets, $npt, $page){
  1005. $query = $this->buildquery($gets);
  1006. return $page . "?" . $query . "&npt=" . $npt;
  1007. }
  1008. }