frontend.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. <?php
  2. class frontend{
  3. public function validateurl($url, $net_validate = false){
  4. $url_parts = parse_url($url);
  5. // check if required parts are there
  6. if(
  7. !isset($url_parts["scheme"]) ||
  8. !(
  9. $url_parts["scheme"] == "http" ||
  10. $url_parts["scheme"] == "https"
  11. ) ||
  12. !isset($url_parts["host"])
  13. ){
  14. return false;
  15. }
  16. if($net_validate){
  17. $ip =
  18. str_replace(
  19. ["[", "]"], // handle ipv6
  20. "",
  21. $url_parts["host"]
  22. );
  23. // if its not an IP
  24. if(!filter_var($ip, FILTER_VALIDATE_IP)){
  25. // resolve domain's IP
  26. $ip = gethostbyname($url_parts["host"] . ".");
  27. }
  28. // check if its localhost
  29. if(
  30. filter_var(
  31. $ip,
  32. FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
  33. ) === false
  34. ){
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. public function load($template, $replacements = []){
  41. $replacements["server_name"] = htmlspecialchars(config::SERVER_NAME);
  42. $replacements["version"] = config::VERSION;
  43. if(isset($_COOKIE["theme"])){
  44. $theme = str_replace(["/". "."], "", $_COOKIE["theme"]);
  45. if(
  46. $theme != "Dark" &&
  47. !is_file("static/themes/" . $theme . ".css")
  48. ){
  49. $theme = config::DEFAULT_THEME;
  50. }
  51. }else{
  52. $theme = config::DEFAULT_THEME;
  53. }
  54. if($theme != "Dark"){
  55. $replacements["style"] = '<link rel="stylesheet" href="/static/themes/' . rawurlencode($theme) . '.css?v' . config::VERSION . '">';
  56. }else{
  57. $replacements["style"] = "";
  58. }
  59. if(isset($_COOKIE["scraper_ac"])){
  60. $replacements["ac"] = '?ac=' . htmlspecialchars($_COOKIE["scraper_ac"]);
  61. }else{
  62. $replacements["ac"] = '';
  63. }
  64. if(
  65. isset($replacements["timetaken"]) &&
  66. $replacements["timetaken"] !== null
  67. ){
  68. $replacements["timetaken"] = '<div class="timetaken">Took ' . number_format(microtime(true) - $replacements["timetaken"], 2) . 's</div>';
  69. }
  70. $handle = fopen("template/{$template}", "r");
  71. $data = fread($handle, filesize("template/{$template}"));
  72. fclose($handle);
  73. $data = explode("\n", $data);
  74. $html = "";
  75. for($i=0; $i<count($data); $i++){
  76. $html .= trim($data[$i]);
  77. }
  78. foreach($replacements as $key => $value){
  79. $html =
  80. str_replace(
  81. "{%{$key}%}",
  82. $value,
  83. $html
  84. );
  85. }
  86. return trim($html);
  87. }
  88. public function loadheader(array $get, array $filters, string $page, bool $increment_bot_counter = true){
  89. echo
  90. $this->load("header.html", [
  91. "title" => trim(htmlspecialchars($get["s"]) . " ({$page})"),
  92. "description" => ucfirst($page) . ' search results for &quot;' . htmlspecialchars($get["s"]) . '&quot;',
  93. "index" => "no",
  94. "search" => htmlspecialchars($get["s"]),
  95. "tabs" => $this->generatehtmltabs($page, $get["s"]),
  96. "filters" => $this->generatehtmlfilters($filters, $get)
  97. ]);
  98. $headers_raw = getallheaders();
  99. $header_keys = [];
  100. $user_agent = "";
  101. $bad_header = false;
  102. // block bots that present X-Forwarded-For, Via, etc
  103. foreach($headers_raw as $headerkey => $headervalue){
  104. $headerkey = strtolower($headerkey);
  105. if($headerkey == "user-agent"){
  106. $user_agent = $headervalue;
  107. continue;
  108. }
  109. // check header key
  110. if(in_array($headerkey, config::FILTERED_HEADER_KEYS)){
  111. $bad_header = true;
  112. break;
  113. }
  114. }
  115. // SSL check
  116. $bad_ssl = false;
  117. if(
  118. isset($_SERVER["https"]) &&
  119. $_SERVER["https"] == "on" &&
  120. isset($_SERVER["SSL_CIPHER"]) &&
  121. in_array($_SERVER["SSL_CIPHER"], config::FILTERED_HEADER_KEYS)
  122. ){
  123. $bad_ssl = true;
  124. }
  125. if(
  126. $bad_header === true ||
  127. $bad_ssl === true ||
  128. $user_agent == "" ||
  129. // user agent check
  130. preg_match(
  131. config::HEADER_REGEX,
  132. $user_agent
  133. )
  134. ){
  135. // bot detected !!
  136. if($increment_bot_counter){
  137. apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
  138. }
  139. $this->drawerror(
  140. "Tshh, blocked!",
  141. '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>.'
  142. );
  143. }
  144. }
  145. public function drawerror($title, $error, $timetaken = null){
  146. if($timetaken === null){
  147. $timetaken = microtime(true);
  148. }
  149. echo
  150. $this->load("search.html", [
  151. "timetaken" => $timetaken,
  152. "class" => "",
  153. "right-left" => "",
  154. "right-right" => "",
  155. "left" =>
  156. '<div class="infobox">' .
  157. '<h1>' . htmlspecialchars($title) . '</h1>' .
  158. $error .
  159. '</div>'
  160. ]);
  161. die();
  162. }
  163. public function drawscrapererror($error, $get, $target, $timetaken = null){
  164. if($timetaken === null){
  165. $timetaken = microtime(true);
  166. }
  167. $this->drawerror(
  168. "Shit",
  169. 'This scraper returned an error:' .
  170. '<div class="code">' . htmlspecialchars($error) . '</div>' .
  171. 'Things you can try:' .
  172. '<ul>' .
  173. '<li>Use a different scraper</li>' .
  174. '<li>Remove keywords that could cause errors</li>' .
  175. '<li><a href="/instances?target=' . $target . "&" . $this->buildquery($get, false) . '">Try your search on another 4get instance</a></li>' .
  176. '</ul><br>' .
  177. 'If the error persists, please <a href="/about">contact the administrator</a>.',
  178. $timetaken
  179. );
  180. }
  181. public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
  182. $payload =
  183. '<div class="text-result">';
  184. // add favicon, link and archive links
  185. $payload .= $this->drawlink($site["url"]);
  186. /*
  187. Draw title + description + filetype
  188. */
  189. $payload .=
  190. '<a href="' . htmlspecialchars($site["url"]) . '" class="hover" rel="noreferrer nofollow"';
  191. if($tabindex === false){
  192. $payload .= ' tabindex="-1"';
  193. }
  194. $payload .= '>';
  195. if($site["thumb"]["url"] !== null){
  196. $payload .=
  197. '<div class="thumb-wrap';
  198. switch($site["thumb"]["ratio"]){
  199. case "16:9":
  200. $size = "landscape";
  201. break;
  202. case "9:16":
  203. $payload .= " portrait";
  204. $size = "portrait";
  205. break;
  206. case "1:1":
  207. $payload .= " square";
  208. $size = "square";
  209. break;
  210. }
  211. $payload .=
  212. '">' .
  213. '<img class="thumb" src="' . $this->htmlimage($site["thumb"]["url"], $size) . '" alt="thumb">';
  214. if($duration !== null){
  215. $payload .=
  216. '<div class="duration">' .
  217. htmlspecialchars($duration) .
  218. '</div>';
  219. }
  220. $payload .=
  221. '</div>';
  222. }
  223. $payload .=
  224. '<div class="title">';
  225. if(
  226. isset($site["type"]) &&
  227. $site["type"] != "web"
  228. ){
  229. $payload .= '<div class="type">' . strtoupper($site["type"]) . '</div>';
  230. }
  231. $payload .=
  232. $this->highlighttext($keywords, $site["title"]) .
  233. '</div>';
  234. if($greentext !== null){
  235. $payload .=
  236. '<div class="greentext">' .
  237. htmlspecialchars($greentext) .
  238. '</div>';
  239. }
  240. if($site["description"] !== null){
  241. $payload .=
  242. '<div class="description">' .
  243. $this->highlighttext($keywords, $site["description"]) .
  244. '</div>';
  245. }
  246. $payload .= $customhtml;
  247. $payload .= '</a>';
  248. /*
  249. Sublinks
  250. */
  251. if(
  252. isset($site["sublink"]) &&
  253. !empty($site["sublink"])
  254. ){
  255. usort($site["sublink"], function($a, $b){
  256. return strlen($a["description"]) > strlen($b["description"]);
  257. });
  258. $payload .=
  259. '<div class="sublinks">' .
  260. '<table>';
  261. $opentr = false;
  262. for($i=0; $i<count($site["sublink"]); $i++){
  263. if(($i % 2) === 0){
  264. $opentr = true;
  265. $payload .= '<tr>';
  266. }else{
  267. $opentr = false;
  268. }
  269. $payload .=
  270. '<td>' .
  271. '<a href="' . htmlspecialchars($site["sublink"][$i]["url"]) . '" rel="noreferrer nofollow">' .
  272. '<div class="title">' .
  273. htmlspecialchars($site["sublink"][$i]["title"]) .
  274. '</div>';
  275. if(!empty($site["sublink"][$i]["date"])){
  276. $payload .=
  277. '<div class="greentext">' .
  278. date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
  279. '</div>';
  280. }
  281. if(!empty($site["sublink"][$i]["description"])){
  282. $payload .=
  283. '<div class="description">' .
  284. $this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
  285. '</div>';
  286. }
  287. $payload .= '</a></td>';
  288. if($opentr === false){
  289. $payload .= '</tr>';
  290. }
  291. }
  292. if($opentr === true){
  293. $payload .= '<td></td></tr>';
  294. }
  295. $payload .= '</table></div>';
  296. }
  297. if(
  298. isset($site["table"]) &&
  299. !empty($site["table"])
  300. ){
  301. $payload .= '<table class="info-table">';
  302. foreach($site["table"] as $title => $value){
  303. $payload .=
  304. '<tr>' .
  305. '<td>' . htmlspecialchars($title) . '</td>' .
  306. '<td>' . htmlspecialchars($value) . '</td>' .
  307. '</tr>';
  308. }
  309. $payload .= '</table>';
  310. }
  311. return $payload . '</div>';
  312. }
  313. public function highlighttext($keywords, $text){
  314. $text = htmlspecialchars($text);
  315. $keywords = explode(" ", $keywords);
  316. $regex = [];
  317. foreach($keywords as $word){
  318. $regex[] = "\b" . preg_quote($word, "/") . "\b";
  319. }
  320. $regex = "/" . implode("|", $regex) . "/i";
  321. return
  322. preg_replace(
  323. $regex,
  324. '<b>${0}</b>',
  325. $text
  326. );
  327. }
  328. function highlightcode($text){
  329. // https://www.php.net/highlight_string
  330. ini_set("highlight.comment", "c-comment");
  331. ini_set("highlight.default", "c-default");
  332. ini_set("highlight.html", "c-default");
  333. ini_set("highlight.keyword", "c-keyword");
  334. ini_set("highlight.string", "c-string");
  335. $text =
  336. trim(
  337. preg_replace(
  338. '/<code [^>]+>/',
  339. "",
  340. str_replace(
  341. [
  342. "<br />",
  343. "&nbsp;",
  344. "<pre>",
  345. "</pre>",
  346. "</code>"
  347. ],
  348. [
  349. "\n",
  350. " ",
  351. "",
  352. "",
  353. ""
  354. ],
  355. explode(
  356. "&lt;?php",
  357. highlight_string("<?php " . $text, true),
  358. 2
  359. )[1]
  360. )
  361. )
  362. );
  363. // replace colors
  364. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  365. foreach($classes as $class){
  366. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  367. }
  368. return $text;
  369. }
  370. public function drawlink($link){
  371. /*
  372. Add favicon
  373. */
  374. $host = parse_url($link);
  375. // special case for when we're not drawing a full url
  376. if(!isset($host["host"])){
  377. $payload =
  378. '<div class="url">' .
  379. '<button class="favicon" tabindex="-1">' .
  380. '<img src="/favicon?s=404" alt="xx">' .
  381. '</button>';
  382. }else{
  383. $esc =
  384. explode(
  385. ".",
  386. $host["host"],
  387. 2
  388. );
  389. if(
  390. count($esc) === 2 &&
  391. $esc[0] == "www"
  392. ){
  393. $esc = $esc[1];
  394. }else{
  395. $esc = $esc[0];
  396. }
  397. $esc = substr($esc, 0, 2);
  398. $urlencode = urlencode($link);
  399. $payload =
  400. '<div class="url">' .
  401. '<button class="favicon" tabindex="-1">' .
  402. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  403. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  404. '</button>' .
  405. '<div class="favicon-dropdown">';
  406. $archives = [];
  407. // add website-specific archives
  408. switch($host["host"]){
  409. case "www.youtube.com":
  410. case "music.youtube.com":
  411. case "youtube.com":
  412. case "m.youtube.com":
  413. case "youtu.be":
  414. if(
  415. (
  416. $host["host"] == "youtu.be" &&
  417. isset($host["path"]) &&
  418. preg_match(
  419. '/^\/([A-Za-z0-9_-]+)/',
  420. $host["path"],
  421. $slug
  422. )
  423. ) ||
  424. (
  425. isset($host["query"]) &&
  426. preg_match(
  427. '/v=([A-Za-z0-9_-]+)/',
  428. $host["query"],
  429. $slug
  430. )
  431. )
  432. ){
  433. // for watch?v=slug
  434. $archives[] = [
  435. "url" => "https://findyoutubevideo.thetechrobo.ca/?q=" . $slug[1],
  436. "favicon" => "findyoutubevideo.thetechrobo.ca",
  437. "favicon_alt" => "fi",
  438. "title" => "FindYouTubeVideo"
  439. ];
  440. $archives[] = [
  441. "url" => "https://filmot.com/video/" . $slug[1] . "/",
  442. "favicon" => "filmot.com",
  443. "favicon_alt" => "fi",
  444. "title" => "Filmot (metadata only)"
  445. ];
  446. }elseif(
  447. preg_match(
  448. '/^\/(?:channel)\/@?([A-Za-z0-9_-]+)/',
  449. $host["path"],
  450. $username
  451. )
  452. ){
  453. $archives[] = [
  454. "url" => "https://filmot.com/channel/" . $username[1] . "/",
  455. "favicon" => "filmot.com",
  456. "favicon_alt" => "fi",
  457. "title" => "Filmot (metadata only)"
  458. ];
  459. }
  460. break;
  461. case "twitch.tv":
  462. case "player.twitch.tv":
  463. case "www.twitch.tv":
  464. if(
  465. isset($host["path"]) &&
  466. preg_match(
  467. '/^\/([A-Za-z0-9_.]+)/',
  468. $host["path"],
  469. $username
  470. )
  471. ){
  472. $archives[] = [
  473. "url" => "https://vodvod.top/channels/@" . $username[1],
  474. "favicon" => "vodvod.top",
  475. "favicon_alt" => "vo",
  476. "title" => "vodvod"
  477. ];
  478. $archives[] = [
  479. "url" => "https://twitchtracker.com/" . $username[1],
  480. "favicon" => "twitchtracker.com",
  481. "favicon_alt" => "tw",
  482. "title" => "TwitchTracker"
  483. ];
  484. }
  485. break;
  486. case "kick.com":
  487. case "player.kick.com":
  488. if(
  489. isset($host["path"]) &&
  490. preg_match(
  491. '/^\/([A-Za-z0-9_.]+)/',
  492. $host["path"],
  493. $username
  494. )
  495. ){
  496. $archives[] = [
  497. "url" => "https://lick.lolcat.ca/channel?name=" . $username[1],
  498. "favicon" => "lick.lolcat.ca",
  499. "favicon_alt" => "li",
  500. "title" => "lick"
  501. ];
  502. $archives[] = [
  503. "url" => "https://kicktracker.net/" . $username[1],
  504. "favicon" => "kicktracker.net",
  505. "favicon_alt" => "ki",
  506. "title" => "Kick tracker"
  507. ];
  508. }
  509. break;
  510. case "x.com":
  511. case "twitter.com":
  512. if(
  513. isset($host["path"]) &&
  514. preg_match(
  515. '/^\/([A-Za-z0-9_.]+)/',
  516. $host["path"],
  517. $username
  518. )
  519. ){
  520. $archives[] = [
  521. "url" => "https://web.archive.org/web/*/https://x.com/" . $username[1] . "/status*",
  522. "favicon" => "archive.org",
  523. "favicon_alt" => "ar",
  524. "title" => "Archive.org: Tweets &gt;2023"
  525. ];
  526. $archives[] = [
  527. "url" => "https://web.archive.org/web/*/https://twitter.com/" . $username[1] . "/status*",
  528. "favicon" => "archive.org",
  529. "favicon_alt" => "ar",
  530. "title" => "Archive.org: Tweets &lt;2023"
  531. ];
  532. }
  533. break;
  534. case "www.instagram.com":
  535. case "instagram.com":
  536. if(
  537. isset($host["path"]) &&
  538. preg_match(
  539. '/^\/([A-Za-z0-9_.]+)/',
  540. $host["path"],
  541. $username
  542. ) &&
  543. $username[1] != "p"
  544. ){
  545. $archives[] = [
  546. "url" => "https://instarchiver.net/users?q=" . $username[1],
  547. "favicon" => "instarchiver.net",
  548. "favicon_alt" => "in",
  549. "title" => "Instarchiver"
  550. ];
  551. $archives[] = [
  552. "url" => "https://www.storiesdb.ch/" . $username[1],
  553. "favicon" => "www.storiesdb.ch",
  554. "favicon_alt" => "st",
  555. "title" => "StoriesDB"
  556. ];
  557. }
  558. break;
  559. case "reddit.com":
  560. case "old.reddit.com":
  561. case "www.reddit.com":
  562. if(isset($host["path"])){
  563. // direct thread lookup
  564. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=pullpush
  565. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=artic_shift
  566. if(
  567. preg_match(
  568. '/^\/r\/[^\/]+\/comments\/[^?&]+/',
  569. $host["path"],
  570. $slug
  571. )
  572. ){
  573. $archives[] = [
  574. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=artic_shift",
  575. "favicon" => "reddit.com",
  576. "favicon_alt" => "re",
  577. "title" => "Artic Shift"
  578. ];
  579. $archives[] = [
  580. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=pullpush",
  581. "favicon" => "reddit.com",
  582. "favicon_alt" => "re",
  583. "title" => "PullPush"
  584. ];
  585. }
  586. // subreddit thread search
  587. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=submissions&sort=desc
  588. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=submissions&sort=desc
  589. // subreddit comment search
  590. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=comments&sort=desc
  591. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=comments&sort=desc
  592. elseif(
  593. preg_match(
  594. '/^\/r\/([^\/]+)\/(?:$|\?|&|search|wiki)/',
  595. $host["path"],
  596. $slug
  597. )
  598. ){
  599. $archives[] = [
  600. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=submissions&sort=desc",
  601. "favicon" => "reddit.com",
  602. "favicon_alt" => "re",
  603. "title" => "Artic Shift (Search threads)"
  604. ];
  605. $archives[] = [
  606. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=submissions&sort=desc",
  607. "favicon" => "reddit.com",
  608. "favicon_alt" => "re",
  609. "title" => "PullPush (Search threads)"
  610. ];
  611. $archives[] = [
  612. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=comments&sort=desc",
  613. "favicon" => "reddit.com",
  614. "favicon_alt" => "re",
  615. "title" => "Artic Shift (Search comments)"
  616. ];
  617. $archives[] = [
  618. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=comments&sort=desc",
  619. "favicon" => "reddit.com",
  620. "favicon_alt" => "re",
  621. "title" => "PullPush (Search comments)"
  622. ];
  623. }
  624. // user thread lookup
  625. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=artic_shift
  626. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=pullpush
  627. // user comment lookup
  628. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=artic_shift
  629. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=pullpush
  630. elseif(
  631. preg_match(
  632. '/^\/(?:u|user)\/([^\/]+)\//',
  633. $host["path"],
  634. $slug
  635. )
  636. ){
  637. $archives[] = [
  638. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=artic_shift",
  639. "favicon" => "reddit.com",
  640. "favicon_alt" => "re",
  641. "title" => "Artic Shift (Search threads)"
  642. ];
  643. $archives[] = [
  644. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=pullpush",
  645. "favicon" => "reddit.com",
  646. "favicon_alt" => "re",
  647. "title" => "PullPush (Search threads)"
  648. ];
  649. $archives[] = [
  650. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=artic_shift",
  651. "favicon" => "reddit.com",
  652. "favicon_alt" => "re",
  653. "title" => "Artic Shift (Search comments)"
  654. ];
  655. $archives[] = [
  656. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=pullpush",
  657. "favicon" => "reddit.com",
  658. "favicon_alt" => "re",
  659. "title" => "PullPush (Search comments)"
  660. ];
  661. }
  662. }
  663. break;
  664. }
  665. // detect git
  666. if(
  667. (
  668. stripos(
  669. $host["host"],
  670. "git."
  671. ) !== false ||
  672. $host["host"] == "codeberg.org" ||
  673. $host["host"] == "sourceforge.net" ||
  674. $host["host"] == "github.com"
  675. ) &&
  676. isset($host["path"]) &&
  677. preg_match(
  678. '/^(\/[^\/]+\/[^\/]+\/?)/',
  679. $host["path"],
  680. $edge
  681. )
  682. ){
  683. $archives[] = [
  684. "url" => "https://archive.softwareheritage.org/browse/origin/directory/?origin_url=" . urlencode($host["scheme"] . "://" . $host["host"] . $edge[1]),
  685. "favicon" => "www.softwareheritage.org",
  686. "favicon_alt" => "so",
  687. "title" => "SoftwareHeritage"
  688. ];
  689. }
  690. $archives =
  691. array_merge(
  692. $archives,
  693. [
  694. [
  695. "url" => "https://web.archive.org/web/" . $urlencode,
  696. "favicon" => "archive.org",
  697. "favicon_alt" => "ar",
  698. "title" => "Archive.org"
  699. ],
  700. [
  701. "url" => "https://archive.ph/newest/" . htmlspecialchars($link),
  702. "favicon" => "archive.ph",
  703. "favicon_alt" => "ar",
  704. "title" => "Archive.ph"
  705. ],
  706. [
  707. "url" => "https://yandex.com/search/?text=" . urlencode("url:" . $link),
  708. "favicon" => "yandex.com",
  709. "favicon_alt" => "ya",
  710. "title" => "Yandex cache"
  711. ],
  712. [
  713. "url" => "https://ghostarchive.org/search?term=" . $urlencode,
  714. "favicon" => "ghostarchive.org",
  715. "favicon_alt" => "gh",
  716. "title" => "Ghostarchive"
  717. ],
  718. [
  719. "url" => "https://arquivo.pt/wayback/" . htmlspecialchars($link),
  720. "favicon" => "arquivo.pt",
  721. "favicon_alt" => "ar",
  722. "title" => "Arquivo.pt"
  723. ],
  724. [
  725. "url" => "https://megalodon.jp/?url=" . $urlencode,
  726. "favicon" => "megalodon.jp",
  727. "favicon_alt" => "me",
  728. "title" => "Megalodon"
  729. ],
  730. [
  731. "url" => "https://www.webcitation.org/query?url=" . $urlencode,
  732. "favicon" => "webcitation.org",
  733. "favicon_alt" => "we",
  734. "title" => "Webcitation"
  735. ]
  736. ]
  737. );
  738. foreach($archives as $archive){
  739. $payload .= '<a href="' . $archive["url"] . '" class="list" target="_BLANK"><img src="/favicon?s=https://' . $archive["favicon"] . '" alt="' . $archive["favicon_alt"] . '">' . $archive["title"] . '</a>';
  740. }
  741. $payload .= '</div>';
  742. }
  743. /*
  744. Draw link
  745. */
  746. $parts = explode("/", $link);
  747. $clickurl = "";
  748. // remove trailing /
  749. $c = count($parts) - 1;
  750. if($parts[$c] == ""){
  751. $parts[$c - 1] = $parts[$c - 1] . "/";
  752. unset($parts[$c]);
  753. }
  754. // merge https://site together
  755. if(isset($host["host"])){
  756. $parts = [
  757. $parts[0] . $parts[1] . '//' . $parts[2],
  758. ...array_slice($parts, 3, count($parts) - 1)
  759. ];
  760. }
  761. $c = count($parts);
  762. for($i=0; $i<$c; $i++){
  763. if($i !== 0){ $clickurl .= "/"; }
  764. $clickurl .= $parts[$i];
  765. if($i === $c - 1){
  766. $parts[$i] = rtrim($parts[$i], "/");
  767. }
  768. $payload .=
  769. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  770. htmlspecialchars(urldecode($parts[$i])) .
  771. '</a>';
  772. if($i !== $c - 1){
  773. $payload .= '<span class="separator"></span>';
  774. }
  775. }
  776. return $payload . '</div>';
  777. }
  778. public function getscraperfilters($page){
  779. // hack: enable error reporting if configured
  780. if(config::DISPLAY_ERRORS === true){
  781. ini_set('display_errors', 1);
  782. ini_set('display_startup_errors', 1);
  783. }
  784. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  785. if(
  786. isset($_GET["scraper"]) &&
  787. is_string($_GET["scraper"])
  788. ){
  789. $get_scraper = $_GET["scraper"];
  790. }else{
  791. if(
  792. isset($_GET["npt"]) &&
  793. is_string($_GET["npt"])
  794. ){
  795. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  796. $get_scraper =
  797. preg_replace(
  798. '/[0-9]+$/',
  799. "",
  800. $get_scraper
  801. );
  802. }
  803. }
  804. // add search field
  805. $filters =
  806. [
  807. "s" => [
  808. "option" => "_SEARCH"
  809. ]
  810. ];
  811. // define default scrapers
  812. switch($page){
  813. case "web":
  814. $filters["scraper"] = [
  815. "display" => "Scraper",
  816. "option" => [
  817. //"fget" => "fget",
  818. "ddg" => "DuckDuckGo",
  819. //"yahoo" => "Yahoo!",
  820. "brave" => "Brave",
  821. "yandex" => "Yandex",
  822. "google" => "Google",
  823. "google_api" => "Google API",
  824. "google_cse" => "Google CSE",
  825. "yahoo_japan" => "Yahoo! JAPAN",
  826. "startpage" => "Startpage",
  827. "yep" => "Yep",
  828. "mwmbl" => "Mwmbl",
  829. "mojeek" => "Mojeek",
  830. "naver" => "Naver",
  831. "baidu" => "Baidu",
  832. "coccoc" => "Cốc Cốc",
  833. "solofield" => "Solofield",
  834. "marginalia" => "Marginalia",
  835. "purili" => "Purili",
  836. "wiby" => "wiby"
  837. ]
  838. ];
  839. break;
  840. case "images":
  841. $filters["scraper"] = [
  842. "display" => "Scraper",
  843. "option" => [
  844. "ddg" => "DuckDuckGo",
  845. "yandex" => "Yandex",
  846. "brave" => "Brave",
  847. "google" => "Google",
  848. "google_api" => "Google API",
  849. "google_cse" => "Google CSE",
  850. "yahoo_japan" => "Yahoo! JAPAN",
  851. "startpage" => "Startpage",
  852. "naver" => "Naver",
  853. "baidu" => "Baidu",
  854. "solofield" => "Solofield",
  855. "pinterest" => "Pinterest",
  856. "flickr" => "Flickr",
  857. "pexels" => "Pexels",
  858. "pixabay" => "Pixabay",
  859. "unsplash" => "Unsplash",
  860. "fivehpx" => "500px",
  861. "vsco" => "VSCO",
  862. "imgur" => "Imgur",
  863. "ftm" => "FindThatMeme"
  864. ]
  865. ];
  866. break;
  867. case "videos":
  868. $filters["scraper"] = [
  869. "display" => "Scraper",
  870. "option" => [
  871. "yt" => "YouTube",
  872. //"archiveorg" => "Archive.org",
  873. //"dailymotion" => "Dailymotion",
  874. "vimeo" => "Vimeo",
  875. //"odysee" => "Odysee",
  876. "sepiasearch" => "Sepia Search",
  877. //"fb" => "Facebook videos",
  878. "ddg" => "DuckDuckGo",
  879. "brave" => "Brave",
  880. "yandex" => "Yandex",
  881. "google" => "Google",
  882. "yahoo_japan" => "Yahoo! JAPAN",
  883. "startpage" => "Startpage",
  884. "naver" => "Naver",
  885. "baidu" => "Baidu",
  886. "coccoc" => "Cốc Cốc",
  887. "purili" => "Purili",
  888. "solofield" => "Solofield"
  889. ]
  890. ];
  891. break;
  892. case "news":
  893. $filters["scraper"] = [
  894. "display" => "Scraper",
  895. "option" => [
  896. "ddg" => "DuckDuckGo",
  897. "brave" => "Brave",
  898. "google" => "Google",
  899. "yahoo_japan" => "Yahoo! JAPAN",
  900. "startpage" => "Startpage",
  901. //"mojeek" => "Mojeek",
  902. "baidu" => "Baidu"
  903. ]
  904. ];
  905. break;
  906. case "music":
  907. $filters["scraper"] = [
  908. "display" => "Scraper",
  909. "option" => [
  910. "sc" => "SoundCloud",
  911. "swisscows" => "Swisscows (SoundCloud)"
  912. //"spotify" => "Spotify"
  913. ]
  914. ];
  915. break;
  916. case "booru":
  917. $filters["scraper"] = [
  918. "display" => "Scraper",
  919. "option" => [
  920. "safebooru" => "Safebooru",
  921. "konachan" => "Konachan",
  922. "tbib" => "The Big Imageboard",
  923. "gelbooru" => "Gelbooru",
  924. "yandere" => "Yande.re",
  925. "tbib" => "The Big Imageboard",
  926. "sankakucomplex" => "SankakuComplex",
  927. "soybooru" => "SoyBooru"
  928. ]
  929. ];
  930. break;
  931. }
  932. // get scraper name from user input, or default out to preferred scraper
  933. $scraper_out = null;
  934. $first = true;
  935. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  936. if($first === true){
  937. $first = $scraper_name;
  938. }
  939. if($scraper_name == $get_scraper){
  940. $scraper_out = $scraper_name;
  941. }
  942. }
  943. if($scraper_out === null){
  944. $scraper_out = $first;
  945. }
  946. include "scraper/$scraper_out.php";
  947. $lib = new $scraper_out();
  948. // set scraper on $_GET
  949. $_GET["scraper"] = $scraper_out;
  950. // set nsfw on $_GET
  951. if(
  952. isset($_COOKIE["nsfw"]) &&
  953. !isset($_GET["nsfw"])
  954. ){
  955. $_GET["nsfw"] = $_COOKIE["nsfw"];
  956. }
  957. return
  958. [
  959. $lib,
  960. array_merge_recursive(
  961. $filters,
  962. $lib->getfilters($page)
  963. )
  964. ];
  965. }
  966. public function parsegetfilters($parameters, $whitelist){
  967. $sanitized = [];
  968. // add npt token
  969. if(
  970. isset($parameters["npt"]) &&
  971. is_string($parameters["npt"])
  972. ){
  973. $sanitized["npt"] = $parameters["npt"];
  974. }else{
  975. $sanitized["npt"] = false;
  976. }
  977. // we're iterating over $whitelist, so
  978. // you can't polluate $sanitized with useless
  979. // parameters
  980. foreach($whitelist as $parameter => $value){
  981. if(isset($parameters[$parameter])){
  982. if(!is_string($parameters[$parameter])){
  983. $sanitized[$parameter] = null;
  984. continue;
  985. }
  986. // parameter is already set, use that value
  987. $sanitized[$parameter] = $parameters[$parameter];
  988. }else{
  989. // parameter is not set, add it
  990. if(is_string($value["option"])){
  991. // special field: set default value manually
  992. switch($value["option"]){
  993. case "_DATE":
  994. // no date set
  995. $sanitized[$parameter] = false;
  996. break;
  997. case "_SEARCH":
  998. // no search set
  999. $sanitized[$parameter] = "";
  1000. break;
  1001. }
  1002. }else{
  1003. // set a default value
  1004. $sanitized[$parameter] = array_keys($value["option"])[0];
  1005. }
  1006. }
  1007. // sanitize input
  1008. if(is_array($value["option"])){
  1009. if(
  1010. !in_array(
  1011. $sanitized[$parameter],
  1012. $keys = array_keys($value["option"])
  1013. )
  1014. ){
  1015. $sanitized[$parameter] = $keys[0];
  1016. }
  1017. }else{
  1018. // sanitize search & string
  1019. switch($value["option"]){
  1020. case "_DATE":
  1021. if($sanitized[$parameter] !== false){
  1022. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  1023. if($sanitized[$parameter] <= 0){
  1024. $sanitized[$parameter] = false;
  1025. }
  1026. }
  1027. break;
  1028. case "_SEARCH":
  1029. // get search string
  1030. $sanitized["s"] = trim($sanitized[$parameter]);
  1031. }
  1032. }
  1033. }
  1034. // invert dates if needed
  1035. if(
  1036. isset($sanitized["older"]) &&
  1037. isset($sanitized["newer"]) &&
  1038. $sanitized["newer"] !== false &&
  1039. $sanitized["older"] !== false &&
  1040. $sanitized["newer"] > $sanitized["older"]
  1041. ){
  1042. // invert
  1043. [
  1044. $sanitized["older"],
  1045. $sanitized["newer"]
  1046. ] = [
  1047. $sanitized["newer"],
  1048. $sanitized["older"]
  1049. ];
  1050. }
  1051. return $sanitized;
  1052. }
  1053. public function s_to_timestamp($seconds){
  1054. if(is_string($seconds)){
  1055. return "LIVE";
  1056. }
  1057. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  1058. }
  1059. public function generatehtmltabs($page, $query){
  1060. $html = null;
  1061. //foreach(["web", "images", "videos", "news", "music", "booru"] as $type){
  1062. foreach(["web", "images", "videos", "news", "music"] as $type){
  1063. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  1064. if(!empty($params)){
  1065. $html .= $params;
  1066. }
  1067. $html .= '" class="tab';
  1068. if($type == $page){
  1069. $html .= ' selected';
  1070. }
  1071. $html .= '">' . ucfirst($type) . '</a>';
  1072. }
  1073. return $html;
  1074. }
  1075. public function generatehtmlfilters($filters, $params){
  1076. $html = null;
  1077. foreach($filters as $filter_name => $filter_values){
  1078. if(!isset($filter_values["display"])){
  1079. continue;
  1080. }
  1081. $output = true;
  1082. $tmp =
  1083. '<div class="filter">' .
  1084. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  1085. if(is_array($filter_values["option"])){
  1086. $tmp .= '<select name="' . $filter_name . '">';
  1087. foreach($filter_values["option"] as $option_name => $option_title){
  1088. $tmp .= '<option value="' . $option_name . '"';
  1089. if($params[$filter_name] == $option_name){
  1090. $tmp .= ' selected';
  1091. }
  1092. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  1093. }
  1094. $tmp .= '</select>';
  1095. }else{
  1096. switch($filter_values["option"]){
  1097. case "_DATE":
  1098. $tmp .= '<input type="date" name="' . $filter_name . '"';
  1099. if($params[$filter_name] !== false){
  1100. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  1101. }
  1102. $tmp .= '>';
  1103. break;
  1104. default:
  1105. $output = false;
  1106. break;
  1107. }
  1108. }
  1109. $tmp .= '</div>';
  1110. if($output === true){
  1111. $html .= $tmp;
  1112. }
  1113. }
  1114. return $html;
  1115. }
  1116. public function buildquery($gets, $ommit = false){
  1117. $out = [];
  1118. foreach($gets as $key => $value){
  1119. if(
  1120. $value == null ||
  1121. $value == false ||
  1122. $key == "npt" ||
  1123. $key == "extendedsearch" ||
  1124. $value == "any" ||
  1125. $value == "all" ||
  1126. $key == "spellcheck" ||
  1127. (
  1128. $ommit === true &&
  1129. $key == "s"
  1130. )
  1131. ){
  1132. continue;
  1133. }
  1134. if(
  1135. $key == "older" ||
  1136. $key == "newer"
  1137. ){
  1138. $value = date("Y-m-d", (int)$value);
  1139. }
  1140. $out[$key] = $value;
  1141. }
  1142. return http_build_query($out);
  1143. }
  1144. public function increment_real_reqs($scraper){
  1145. apcu_inc(intdiv(time(), 3600) . ".real_requests", 1, $s, 262800);
  1146. apcu_inc(intdiv(time(), 3600) . ".$scraper.requests", 1, $s, 262800);
  1147. }
  1148. public function htmlimage($image, $format){
  1149. if(
  1150. preg_match(
  1151. '/^data:/',
  1152. $image
  1153. )
  1154. ){
  1155. return htmlspecialchars($image);
  1156. }
  1157. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  1158. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  1159. }
  1160. public function htmlnextpage($gets, $npt, $page){
  1161. $query = $this->buildquery($gets);
  1162. return $page . "?" . $query . "&npt=" . $npt;
  1163. }
  1164. }