frontend.php 27 KB

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