frontend.php 27 KB

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