frontend.php 27 KB

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