frontend.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. str_replace(
  300. [
  301. "<br />",
  302. "&nbsp;",
  303. "<pre>",
  304. "</pre>"
  305. ],
  306. [
  307. "\n",
  308. " ",
  309. "",
  310. ""
  311. ],
  312. explode(
  313. "&lt;?php",
  314. highlight_string("<?php " . $text, true),
  315. 2
  316. )[1]
  317. )
  318. );
  319. // replace colors
  320. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  321. foreach($classes as $class){
  322. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  323. }
  324. return $text;
  325. }
  326. public function drawlink($link){
  327. /*
  328. Add favicon
  329. */
  330. $host = parse_url($link);
  331. $esc =
  332. explode(
  333. ".",
  334. $host["host"],
  335. 2
  336. );
  337. if(
  338. count($esc) === 2 &&
  339. $esc[0] == "www"
  340. ){
  341. $esc = $esc[1];
  342. }else{
  343. $esc = $esc[0];
  344. }
  345. $esc = substr($esc, 0, 2);
  346. $urlencode = urlencode($link);
  347. $payload =
  348. '<div class="url">' .
  349. '<button class="favicon" tabindex="-1">' .
  350. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  351. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  352. '</button>' .
  353. '<div class="favicon-dropdown">';
  354. /*
  355. Add archive links
  356. */
  357. if(
  358. $host["host"] == "boards.4chan.org" ||
  359. $host["host"] == "boards.4channel.org"
  360. ){
  361. $archives = [];
  362. $path = explode("/", $host["path"]);
  363. $count = count($path);
  364. // /pol/thread/417568063/post-shitty-memes-if-you-want-to
  365. if($count !== 0){
  366. $isboard = true;
  367. switch($path[1]){
  368. case "con":
  369. break;
  370. case "q":
  371. $archives[] = "desuarchive.org";
  372. break;
  373. case "qa":
  374. $archives[] = "desuarchive.org";
  375. break;
  376. case "qb":
  377. $archives[] = "arch.b4k.co";
  378. break;
  379. case "trash":
  380. $archives[] = "desuarchive.org";
  381. break;
  382. case "a":
  383. $archives[] = "desuarchive.org";
  384. break;
  385. case "c":
  386. $archives[] = "desuarchive.org";
  387. break;
  388. case "w":
  389. break;
  390. case "m":
  391. $archives[] = "desuarchive.org";
  392. break;
  393. case "cgl":
  394. $archives[] = "desuarchive.org";
  395. $archives[] = "warosu.org";
  396. break;
  397. case "f":
  398. $archives[] = "archive.4plebs.org";
  399. break;
  400. case "n":
  401. break;
  402. case "jp":
  403. $archives[] = "warosu.org";
  404. break;
  405. case "vt":
  406. $archives[] = "warosu.org";
  407. break;
  408. case "v":
  409. $archives[] = "arch.b4k.co";
  410. break;
  411. case "vg":
  412. $archives[] = "arch.b4k.co";
  413. break;
  414. case "vm":
  415. $archives[] = "arch.b4k.co";
  416. break;
  417. case "vmg":
  418. $archives[] = "arch.b4k.co";
  419. break;
  420. case "vp":
  421. $archives[] = "arch.b4k.co";
  422. break;
  423. case "vr":
  424. $archives[] = "desuarchive.org";
  425. $archives[] = "warosu.org";
  426. break;
  427. case "vrpg":
  428. $archives[] = "arch.b4k.co";
  429. break;
  430. case "vst":
  431. $archives[] = "arch.b4k.co";
  432. break;
  433. case "co":
  434. $archives[] = "desuarchive.org";
  435. break;
  436. case "g":
  437. $archives[] = "desuarchive.org";
  438. $archives[] = "arch.b4k.co";
  439. break;
  440. case "tv":
  441. $archives[] = "archive.4plebs.org";
  442. break;
  443. case "k":
  444. $archives[] = "desuarchive.org";
  445. break;
  446. case "o":
  447. $archives[] = "archive.4plebs.org";
  448. break;
  449. case "an":
  450. $archives[] = "desuarchive.org";
  451. break;
  452. case "tg":
  453. $archives[] = "desuarchive.org";
  454. $archives[] = "archive.4plebs.org";
  455. break;
  456. case "sp":
  457. $archives[] = "archive.4plebs.org";
  458. break;
  459. case "xs":
  460. $archives[] = "eientei.xyz";
  461. break;
  462. case "pw":
  463. break;
  464. case "sci":
  465. $archives[] = "warosu.org";
  466. $archives[] = "eientei.xyz";
  467. break;
  468. case "his":
  469. $archives[] = "desuarchive.org";
  470. break;
  471. case "int":
  472. $archives[] = "desuarchive.org";
  473. break;
  474. case "out":
  475. break;
  476. case "toy":
  477. break;
  478. case "i":
  479. $archives[] = "archiveofsins.com";
  480. $archives[] = "eientei.xyz";
  481. break;
  482. case "po":
  483. break;
  484. case "p":
  485. break;
  486. case "ck":
  487. $archives[] = "warosu.org";
  488. break;
  489. case "ic":
  490. $archives[] = "warosu.org";
  491. break;
  492. case "wg":
  493. break;
  494. case "lit":
  495. $archives[] = "warosu.org";
  496. break;
  497. case "mu":
  498. $archives[] = "desuarchive.org";
  499. break;
  500. case "fa":
  501. $archives[] = "warosu.org";
  502. break;
  503. case "3":
  504. $archives[] = "warosu.org";
  505. $archives[] = "eientei.xyz";
  506. break;
  507. case "gd":
  508. break;
  509. case "diy":
  510. $archives[] = "warosu.org";
  511. break;
  512. case "wsg":
  513. $archives[] = "desuarchive.org";
  514. break;
  515. case "qst":
  516. break;
  517. case "biz":
  518. $archives[] = "warosu.org";
  519. break;
  520. case "trv":
  521. $archives[] = "archive.4plebs.org";
  522. break;
  523. case "fit":
  524. $archives[] = "desuarchive.org";
  525. break;
  526. case "x":
  527. $archives[] = "archive.4plebs.org";
  528. break;
  529. case "adv":
  530. $archives[] = "archive.4plebs.org";
  531. break;
  532. case "lgbt":
  533. $archives[] = "archiveofsins.com";
  534. break;
  535. case "mlp":
  536. $archives[] = "desuarchive.org";
  537. $archives[] = "arch.b4k.co";
  538. break;
  539. case "news":
  540. break;
  541. case "wsr":
  542. break;
  543. case "vip":
  544. break;
  545. case "b":
  546. $archives[] = "thebarchive.com";
  547. break;
  548. case "r9k":
  549. $archives[] = "desuarchive.org";
  550. break;
  551. case "pol":
  552. $archives[] = "archive.4plebs.org";
  553. break;
  554. case "bant":
  555. $archives[] = "thebarchive.com";
  556. break;
  557. case "soc":
  558. $archives[] = "archiveofsins.com";
  559. break;
  560. case "s4s":
  561. $archives[] = "archive.4plebs.org";
  562. break;
  563. case "s":
  564. $archives[] = "archiveofsins.com";
  565. break;
  566. case "hc":
  567. $archives[] = "archiveofsins.com";
  568. break;
  569. case "hm":
  570. $archives[] = "archiveofsins.com";
  571. break;
  572. case "h":
  573. $archives[] = "archiveofsins.com";
  574. break;
  575. case "e":
  576. break;
  577. case "u":
  578. $archives[] = "archiveofsins.com";
  579. break;
  580. case "d":
  581. $archives[] = "desuarchive.org";
  582. break;
  583. case "t":
  584. $archives[] = "archiveofsins.com";
  585. break;
  586. case "hr":
  587. $archives[] = "archive.4plebs.org";
  588. break;
  589. case "gif":
  590. break;
  591. case "aco":
  592. $archives[] = "desuarchive.org";
  593. break;
  594. case "r":
  595. $archives[] = "archiveofsins.com";
  596. break;
  597. default:
  598. $isboard = false;
  599. break;
  600. }
  601. if($isboard === true){
  602. $archives[] = "archived.moe";
  603. }
  604. $trail = "";
  605. if(
  606. isset($path[2]) &&
  607. isset($path[3]) &&
  608. $path[2] == "thread"
  609. ){
  610. $trail .= "/" . $path[1] . "/thread/" . $path[3];
  611. }elseif($isboard){
  612. $trail = "/" . $path[1] . "/";
  613. }
  614. for($i=0; $i<count($archives); $i++){
  615. $payload .=
  616. '<a href="https://' . $archives[$i] . $trail . '" class="list" target="_BLANK">' .
  617. '<img src="/favicon?s=https://' . $archives[$i] . '" alt="' . $archives[$i][0] . $archives[$i][1] . '">' .
  618. $archives[$i] .
  619. '</a>';
  620. }
  621. }
  622. }
  623. $payload .=
  624. '<a href="https://web.archive.org/web/' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.org" alt="ar">Archive.org</a>' .
  625. '<a href="https://archive.ph/newest/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.is" alt="ar">Archive.is</a>' .
  626. '<a href="https://ghostarchive.org/search?term=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://ghostarchive.org" alt="gh">Ghostarchive</a>' .
  627. '<a href="https://arquivo.pt/wayback/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://arquivo.pt" alt="ar">Arquivo.pt</a>' .
  628. '<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>' .
  629. '<a href="https://megalodon.jp/?url=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://megalodon.jp" alt="me">Megalodon</a>' .
  630. '</div>';
  631. /*
  632. Draw link
  633. */
  634. $parts = explode("/", $link);
  635. $clickurl = "";
  636. // remove trailing /
  637. $c = count($parts) - 1;
  638. if($parts[$c] == ""){
  639. $parts[$c - 1] = $parts[$c - 1] . "/";
  640. unset($parts[$c]);
  641. }
  642. // merge https://site together
  643. $parts = [
  644. $parts[0] . $parts[1] . '//' . $parts[2],
  645. ...array_slice($parts, 3, count($parts) - 1)
  646. ];
  647. $c = count($parts);
  648. for($i=0; $i<$c; $i++){
  649. if($i !== 0){ $clickurl .= "/"; }
  650. $clickurl .= $parts[$i];
  651. if($i === $c - 1){
  652. $parts[$i] = rtrim($parts[$i], "/");
  653. }
  654. $payload .=
  655. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  656. htmlspecialchars(urldecode($parts[$i])) .
  657. '</a>';
  658. if($i !== $c - 1){
  659. $payload .= '<span class="separator"></span>';
  660. }
  661. }
  662. return $payload . '</div>';
  663. }
  664. public function getscraperfilters($page){
  665. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  666. if(
  667. isset($_GET["scraper"]) &&
  668. is_string($_GET["scraper"])
  669. ){
  670. $get_scraper = $_GET["scraper"];
  671. }else{
  672. if(
  673. isset($_GET["npt"]) &&
  674. is_string($_GET["npt"])
  675. ){
  676. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  677. $get_scraper =
  678. preg_replace(
  679. '/[0-9]+$/',
  680. "",
  681. $get_scraper
  682. );
  683. }
  684. }
  685. // add search field
  686. $filters =
  687. [
  688. "s" => [
  689. "option" => "_SEARCH"
  690. ]
  691. ];
  692. // define default scrapers
  693. switch($page){
  694. case "web":
  695. $filters["scraper"] = [
  696. "display" => "Scraper",
  697. "option" => [
  698. "ddg" => "DuckDuckGo",
  699. "brave" => "Brave",
  700. "yandex" => "Yandex",
  701. "google" => "Google",
  702. //"google_api" => "Google API",
  703. "google_cse" => "Google CSE",
  704. "startpage" => "Startpage",
  705. "qwant" => "Qwant",
  706. "ghostery" => "Ghostery",
  707. "yep" => "Yep",
  708. "greppr" => "Greppr",
  709. "crowdview" => "Crowdview",
  710. "mwmbl" => "Mwmbl",
  711. "mojeek" => "Mojeek",
  712. "baidu" => "Baidu",
  713. "coccoc" => "Cốc Cốc",
  714. //"solofield" => "Solofield",
  715. "marginalia" => "Marginalia",
  716. "wiby" => "wiby",
  717. "curlie" => "Curlie"
  718. ]
  719. ];
  720. break;
  721. case "images":
  722. $filters["scraper"] = [
  723. "display" => "Scraper",
  724. "option" => [
  725. "ddg" => "DuckDuckGo",
  726. "yandex" => "Yandex",
  727. "brave" => "Brave",
  728. "google" => "Google",
  729. "google_cse" => "Google CSE",
  730. "startpage" => "Startpage",
  731. "qwant" => "Qwant",
  732. "yep" => "Yep",
  733. "baidu" => "Baidu",
  734. //"solofield" => "Solofield",
  735. "pinterest" => "Pinterest",
  736. "cara" => "Cara",
  737. "flickr" => "Flickr",
  738. "fivehpx" => "500px",
  739. "vsco" => "VSCO",
  740. "imgur" => "Imgur",
  741. "ftm" => "FindThatMeme",
  742. //"sankakucomplex" => "SankakuComplex"
  743. ]
  744. ];
  745. break;
  746. case "videos":
  747. $filters["scraper"] = [
  748. "display" => "Scraper",
  749. "option" => [
  750. "yt" => "YouTube",
  751. "vimeo" => "Vimeo",
  752. //"odysee" => "Odysee",
  753. "sepiasearch" => "Sepia Search",
  754. //"fb" => "Facebook videos",
  755. "ddg" => "DuckDuckGo",
  756. "brave" => "Brave",
  757. "yandex" => "Yandex",
  758. "google" => "Google",
  759. "startpage" => "Startpage",
  760. "qwant" => "Qwant",
  761. "baidu" => "Baidu",
  762. "coccoc" => "Cốc Cốc"
  763. //"solofield" => "Solofield"
  764. ]
  765. ];
  766. break;
  767. case "news":
  768. $filters["scraper"] = [
  769. "display" => "Scraper",
  770. "option" => [
  771. "ddg" => "DuckDuckGo",
  772. "brave" => "Brave",
  773. "google" => "Google",
  774. "startpage" => "Startpage",
  775. "qwant" => "Qwant",
  776. "yep" => "Yep",
  777. "mojeek" => "Mojeek",
  778. "baidu" => "Baidu"
  779. ]
  780. ];
  781. break;
  782. case "music":
  783. $filters["scraper"] = [
  784. "display" => "Scraper",
  785. "option" => [
  786. "sc" => "SoundCloud"
  787. //"spotify" => "Spotify"
  788. ]
  789. ];
  790. break;
  791. }
  792. // get scraper name from user input, or default out to preferred scraper
  793. $scraper_out = null;
  794. $first = true;
  795. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  796. if($first === true){
  797. $first = $scraper_name;
  798. }
  799. if($scraper_name == $get_scraper){
  800. $scraper_out = $scraper_name;
  801. }
  802. }
  803. if($scraper_out === null){
  804. $scraper_out = $first;
  805. }
  806. include "scraper/$scraper_out.php";
  807. $lib = new $scraper_out();
  808. // set scraper on $_GET
  809. $_GET["scraper"] = $scraper_out;
  810. // set nsfw on $_GET
  811. if(
  812. isset($_COOKIE["nsfw"]) &&
  813. !isset($_GET["nsfw"])
  814. ){
  815. $_GET["nsfw"] = $_COOKIE["nsfw"];
  816. }
  817. return
  818. [
  819. $lib,
  820. array_merge_recursive(
  821. $filters,
  822. $lib->getfilters($page)
  823. )
  824. ];
  825. }
  826. public function parsegetfilters($parameters, $whitelist){
  827. $sanitized = [];
  828. // add npt token
  829. if(
  830. isset($parameters["npt"]) &&
  831. is_string($parameters["npt"])
  832. ){
  833. $sanitized["npt"] = $parameters["npt"];
  834. }else{
  835. $sanitized["npt"] = false;
  836. }
  837. // we're iterating over $whitelist, so
  838. // you can't polluate $sanitized with useless
  839. // parameters
  840. foreach($whitelist as $parameter => $value){
  841. if(isset($parameters[$parameter])){
  842. if(!is_string($parameters[$parameter])){
  843. $sanitized[$parameter] = null;
  844. continue;
  845. }
  846. // parameter is already set, use that value
  847. $sanitized[$parameter] = $parameters[$parameter];
  848. }else{
  849. // parameter is not set, add it
  850. if(is_string($value["option"])){
  851. // special field: set default value manually
  852. switch($value["option"]){
  853. case "_DATE":
  854. // no date set
  855. $sanitized[$parameter] = false;
  856. break;
  857. case "_SEARCH":
  858. // no search set
  859. $sanitized[$parameter] = "";
  860. break;
  861. }
  862. }else{
  863. // set a default value
  864. $sanitized[$parameter] = array_keys($value["option"])[0];
  865. }
  866. }
  867. // sanitize input
  868. if(is_array($value["option"])){
  869. if(
  870. !in_array(
  871. $sanitized[$parameter],
  872. $keys = array_keys($value["option"])
  873. )
  874. ){
  875. $sanitized[$parameter] = $keys[0];
  876. }
  877. }else{
  878. // sanitize search & string
  879. switch($value["option"]){
  880. case "_DATE":
  881. if($sanitized[$parameter] !== false){
  882. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  883. if($sanitized[$parameter] <= 0){
  884. $sanitized[$parameter] = false;
  885. }
  886. }
  887. break;
  888. case "_SEARCH":
  889. // get search string
  890. $sanitized["s"] = trim($sanitized[$parameter]);
  891. }
  892. }
  893. }
  894. // invert dates if needed
  895. if(
  896. isset($sanitized["older"]) &&
  897. isset($sanitized["newer"]) &&
  898. $sanitized["newer"] !== false &&
  899. $sanitized["older"] !== false &&
  900. $sanitized["newer"] > $sanitized["older"]
  901. ){
  902. // invert
  903. [
  904. $sanitized["older"],
  905. $sanitized["newer"]
  906. ] = [
  907. $sanitized["newer"],
  908. $sanitized["older"]
  909. ];
  910. }
  911. return $sanitized;
  912. }
  913. public function s_to_timestamp($seconds){
  914. if(is_string($seconds)){
  915. return "LIVE";
  916. }
  917. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  918. }
  919. public function generatehtmltabs($page, $query){
  920. $html = null;
  921. foreach(["web", "images", "videos", "news", "music"] as $type){
  922. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  923. if(!empty($params)){
  924. $html .= $params;
  925. }
  926. $html .= '" class="tab';
  927. if($type == $page){
  928. $html .= ' selected';
  929. }
  930. $html .= '">' . ucfirst($type) . '</a>';
  931. }
  932. return $html;
  933. }
  934. public function generatehtmlfilters($filters, $params){
  935. $html = null;
  936. foreach($filters as $filter_name => $filter_values){
  937. if(!isset($filter_values["display"])){
  938. continue;
  939. }
  940. $output = true;
  941. $tmp =
  942. '<div class="filter">' .
  943. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  944. if(is_array($filter_values["option"])){
  945. $tmp .= '<select name="' . $filter_name . '">';
  946. foreach($filter_values["option"] as $option_name => $option_title){
  947. $tmp .= '<option value="' . $option_name . '"';
  948. if($params[$filter_name] == $option_name){
  949. $tmp .= ' selected';
  950. }
  951. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  952. }
  953. $tmp .= '</select>';
  954. }else{
  955. switch($filter_values["option"]){
  956. case "_DATE":
  957. $tmp .= '<input type="date" name="' . $filter_name . '"';
  958. if($params[$filter_name] !== false){
  959. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  960. }
  961. $tmp .= '>';
  962. break;
  963. default:
  964. $output = false;
  965. break;
  966. }
  967. }
  968. $tmp .= '</div>';
  969. if($output === true){
  970. $html .= $tmp;
  971. }
  972. }
  973. return $html;
  974. }
  975. public function buildquery($gets, $ommit = false){
  976. $out = [];
  977. foreach($gets as $key => $value){
  978. if(
  979. $value == null ||
  980. $value == false ||
  981. $key == "npt" ||
  982. $key == "extendedsearch" ||
  983. $value == "any" ||
  984. $value == "all" ||
  985. $key == "spellcheck" ||
  986. (
  987. $ommit === true &&
  988. $key == "s"
  989. )
  990. ){
  991. continue;
  992. }
  993. if(
  994. $key == "older" ||
  995. $key == "newer"
  996. ){
  997. $value = date("Y-m-d", (int)$value);
  998. }
  999. $out[$key] = $value;
  1000. }
  1001. return http_build_query($out);
  1002. }
  1003. public function htmlimage($image, $format){
  1004. if(
  1005. preg_match(
  1006. '/^data:/',
  1007. $image
  1008. )
  1009. ){
  1010. return htmlspecialchars($image);
  1011. }
  1012. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  1013. }
  1014. public function htmlnextpage($gets, $npt, $page){
  1015. $query = $this->buildquery($gets);
  1016. return $page . "?" . $query . "&npt=" . $npt;
  1017. }
  1018. }