1
0

frontend.php 26 KB

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