frontend.php 26 KB

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