frontend.php 27 KB

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