frontend.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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. $fansites = [
  408. [
  409. "url" => "https://cum.st/creators?cq=",
  410. "favicon" => "cum.st",
  411. "favicon_alt" => "cu",
  412. "title" => "OnlyHaven"
  413. ],
  414. [
  415. "url" => "https://pawchive.pw/artists?q=",
  416. "favicon" => "pawchive.pw",
  417. "favicon_alt" => "pa",
  418. "title" => "Pawchive"
  419. ],
  420. [
  421. "url" => "https://bakemono.app/creators?q=",
  422. "favicon" => "bakemono.app",
  423. "favicon_alt" => "ba",
  424. "title" => "Bakemono"
  425. ],
  426. [
  427. "url" => "https://kemono.cr/artists?q=",
  428. "favicon" => "kemono.cr",
  429. "favicon_alt" => "ke",
  430. "title" => "Kemono"
  431. ],
  432. [
  433. "url" => "https://coomer.st/artists?q=",
  434. "favicon" => "coomer.st",
  435. "favicon_alt" => "co",
  436. "title" => "Coomer"
  437. ]
  438. ];
  439. // add website-specific archives
  440. switch($host["host"]){
  441. case "www.youtube.com":
  442. case "music.youtube.com":
  443. case "youtube.com":
  444. case "m.youtube.com":
  445. case "youtu.be":
  446. if(
  447. (
  448. $host["host"] == "youtu.be" &&
  449. isset($host["path"]) &&
  450. preg_match(
  451. '/^\/([A-Za-z0-9_-]+)/',
  452. $host["path"],
  453. $slug
  454. )
  455. ) ||
  456. (
  457. isset($host["query"]) &&
  458. preg_match(
  459. '/v=([A-Za-z0-9_-]+)/',
  460. $host["query"],
  461. $slug
  462. )
  463. )
  464. ){
  465. // for watch?v=slug
  466. $archives[] = [
  467. "url" => "https://findyoutubevideo.thetechrobo.ca/?q=" . $slug[1],
  468. "favicon" => "findyoutubevideo.thetechrobo.ca",
  469. "favicon_alt" => "fi",
  470. "title" => "FindYouTubeVideo"
  471. ];
  472. $archives[] = [
  473. "url" => "https://filmot.com/video/" . $slug[1] . "/",
  474. "favicon" => "filmot.com",
  475. "favicon_alt" => "fi",
  476. "title" => "Filmot (metadata only)"
  477. ];
  478. }elseif(
  479. preg_match(
  480. '/^\/(?:channel)\/@?([A-Za-z0-9_-]+)/',
  481. $host["path"],
  482. $username
  483. )
  484. ){
  485. $archives[] = [
  486. "url" => "https://filmot.com/channel/" . $username[1] . "/",
  487. "favicon" => "filmot.com",
  488. "favicon_alt" => "fi",
  489. "title" => "Filmot (metadata only)"
  490. ];
  491. }
  492. break;
  493. case "twitch.tv":
  494. case "player.twitch.tv":
  495. case "www.twitch.tv":
  496. if(
  497. isset($host["path"]) &&
  498. preg_match(
  499. '/^\/([A-Za-z0-9_.]+)/',
  500. $host["path"],
  501. $username
  502. ) &&
  503. $username[1] != "videos"
  504. ){
  505. $archives[] = [
  506. "url" => "https://vodvod.top/channels/@" . $username[1],
  507. "favicon" => "vodvod.top",
  508. "favicon_alt" => "vo",
  509. "title" => "vodvod"
  510. ];
  511. $archives[] = [
  512. "url" => "https://twitchtracker.com/" . $username[1],
  513. "favicon" => "twitchtracker.com",
  514. "favicon_alt" => "tw",
  515. "title" => "TwitchTracker"
  516. ];
  517. }
  518. break;
  519. case "kick.com":
  520. case "player.kick.com":
  521. if(
  522. isset($host["path"]) &&
  523. preg_match(
  524. '/^\/([A-Za-z0-9_.]+)/',
  525. $host["path"],
  526. $username
  527. )
  528. ){
  529. $archives[] = [
  530. "url" => "https://lick.lolcat.ca/channel?name=" . $username[1],
  531. "favicon" => "lick.lolcat.ca",
  532. "favicon_alt" => "li",
  533. "title" => "lick"
  534. ];
  535. $archives[] = [
  536. "url" => "https://kicktracker.net/" . $username[1],
  537. "favicon" => "kicktracker.net",
  538. "favicon_alt" => "ki",
  539. "title" => "Kick tracker"
  540. ];
  541. }
  542. break;
  543. case "x.com":
  544. case "twitter.com":
  545. if(
  546. isset($host["path"]) &&
  547. preg_match(
  548. '/^\/([A-Za-z0-9_.]+)/',
  549. $host["path"],
  550. $username
  551. )
  552. ){
  553. $archives[] = [
  554. "url" => "https://web.archive.org/web/*/https://x.com/" . $username[1] . "/status*",
  555. "favicon" => "archive.org",
  556. "favicon_alt" => "ar",
  557. "title" => "Archive.org: Tweets &gt;2023"
  558. ];
  559. $archives[] = [
  560. "url" => "https://web.archive.org/web/*/https://twitter.com/" . $username[1] . "/status*",
  561. "favicon" => "archive.org",
  562. "favicon_alt" => "ar",
  563. "title" => "Archive.org: Tweets &lt;2023"
  564. ];
  565. }
  566. break;
  567. case "www.instagram.com":
  568. case "instagram.com":
  569. if(
  570. isset($host["path"]) &&
  571. preg_match(
  572. '/^\/([A-Za-z0-9_.]+)/',
  573. $host["path"],
  574. $username
  575. ) &&
  576. $username[1] != "p"
  577. ){
  578. $archives[] = [
  579. "url" => "https://instarchiver.net/users?q=" . $username[1],
  580. "favicon" => "instarchiver.net",
  581. "favicon_alt" => "in",
  582. "title" => "Instarchiver"
  583. ];
  584. $archives[] = [
  585. "url" => "https://www.storiesdb.ch/" . $username[1],
  586. "favicon" => "www.storiesdb.ch",
  587. "favicon_alt" => "st",
  588. "title" => "StoriesDB"
  589. ];
  590. }
  591. break;
  592. case "reddit.com":
  593. case "old.reddit.com":
  594. case "www.reddit.com":
  595. if(isset($host["path"])){
  596. // direct thread lookup
  597. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=pullpush
  598. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=artic_shift
  599. if(
  600. preg_match(
  601. '/^\/r\/[^\/]+\/comments\/[^?&]+/',
  602. $host["path"],
  603. $slug
  604. )
  605. ){
  606. $archives[] = [
  607. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=artic_shift",
  608. "favicon" => "reddit.com",
  609. "favicon_alt" => "re",
  610. "title" => "Artic Shift"
  611. ];
  612. $archives[] = [
  613. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=pullpush",
  614. "favicon" => "reddit.com",
  615. "favicon_alt" => "re",
  616. "title" => "PullPush"
  617. ];
  618. }
  619. // subreddit thread search
  620. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=submissions&sort=desc
  621. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=submissions&sort=desc
  622. // subreddit comment search
  623. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=comments&sort=desc
  624. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=comments&sort=desc
  625. elseif(
  626. preg_match(
  627. '/^\/r\/([^\/]+)\/(?:(?:search|wiki|about)\/?)?(?:$|\?|&)/',
  628. $host["path"],
  629. $slug
  630. )
  631. ){
  632. $archives[] = [
  633. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=submissions&sort=desc",
  634. "favicon" => "reddit.com",
  635. "favicon_alt" => "re",
  636. "title" => "Artic Shift (Search threads)"
  637. ];
  638. $archives[] = [
  639. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=submissions&sort=desc",
  640. "favicon" => "reddit.com",
  641. "favicon_alt" => "re",
  642. "title" => "PullPush (Search threads)"
  643. ];
  644. $archives[] = [
  645. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=comments&sort=desc",
  646. "favicon" => "reddit.com",
  647. "favicon_alt" => "re",
  648. "title" => "Artic Shift (Search comments)"
  649. ];
  650. $archives[] = [
  651. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=comments&sort=desc",
  652. "favicon" => "reddit.com",
  653. "favicon_alt" => "re",
  654. "title" => "PullPush (Search comments)"
  655. ];
  656. }
  657. // user thread lookup
  658. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=artic_shift
  659. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=pullpush
  660. // user comment lookup
  661. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=artic_shift
  662. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=pullpush
  663. elseif(
  664. preg_match(
  665. '/^\/(?:u|user)\/([^\/]+)\/(?:(?:comments|submitted)\/?)?(?:$|\?|&)/',
  666. $host["path"],
  667. $slug
  668. )
  669. ){
  670. $archives[] = [
  671. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=artic_shift",
  672. "favicon" => "reddit.com",
  673. "favicon_alt" => "re",
  674. "title" => "Artic Shift (Search threads)"
  675. ];
  676. $archives[] = [
  677. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=pullpush",
  678. "favicon" => "reddit.com",
  679. "favicon_alt" => "re",
  680. "title" => "PullPush (Search threads)"
  681. ];
  682. $archives[] = [
  683. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=artic_shift",
  684. "favicon" => "reddit.com",
  685. "favicon_alt" => "re",
  686. "title" => "Artic Shift (Search comments)"
  687. ];
  688. $archives[] = [
  689. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=pullpush",
  690. "favicon" => "reddit.com",
  691. "favicon_alt" => "re",
  692. "title" => "PullPush (Search comments)"
  693. ];
  694. }
  695. }
  696. break;
  697. // https://cum.st/creators?cq= onlyfans fansly patreon
  698. // https://pawchive.pw/artists?q= patreon pixiv/fanbox discord
  699. // https://bakemono.app/creators?q= pixiv/fanbox fansly onlyfans patreon
  700. // https://kemono.cr/artists?q= patreon pixiv/fanbox fantia afdian boosty gumroad subscribestar dlsite
  701. // https://coomer.st/artists?q= onlyfans fansly candfans
  702. // cant look up
  703. /*
  704. case "fantia.jp": // https://fantia.jp/fanclubs/ <integer>
  705. */
  706. // normal
  707. case "onlyfans.com": // https://onlyfans.com/ <username>
  708. case "fansly.com": // https://fansly.com/ <username>
  709. case "fanbox.cc":
  710. case "patreon.com": // https://www.patreon.com/ <username>
  711. case "www.patreon.com":
  712. case "boosty.to": // https://boosty.to/ <username>
  713. case "www.subscribestar.com": // https://www.subscribestar.com/ <username>
  714. case "subscribestar.com":
  715. case "www.dlsite.com": // https://www.dlsite.com/ <username> /
  716. case "dlsite.com":
  717. case "candfans.com": // https://candfans.com/ <username>
  718. case "afdian.com": // https://afdian.com/a/ <username>
  719. if(
  720. isset($host["path"]) &&
  721. (
  722. (
  723. $host["host"] == "afdian.com" &&
  724. preg_match(
  725. '/^\/a\/([A-Za-z0-9-_.]+)/',
  726. $host["path"],
  727. $username
  728. )
  729. ) ||
  730. preg_match(
  731. '/^\/([A-Za-z0-9-_.]+)/',
  732. $host["path"],
  733. $username
  734. )
  735. )
  736. ){
  737. foreach($fansites as $fansite){
  738. $archives[] = [
  739. "url" => $fansite["url"] . $this->sanitize_slug($username[1]),
  740. "favicon" => $fansite["favicon"],
  741. "favicon_alt" => $fansite["favicon_alt"],
  742. "title" => $fansite["title"]
  743. ];
  744. }
  745. }
  746. break;
  747. }
  748. // special fansite cases
  749. if(
  750. preg_match(
  751. '/^([A-Za-z0-9_]+)\.(?:fanbox\.cc|gumroad\.com)/',
  752. $host["host"],
  753. $username
  754. )
  755. ){
  756. foreach($fansites as $fansite){
  757. $archives[] = [
  758. "url" => $fansite["url"] . $this->sanitize_slug($username[1]),
  759. "favicon" => $fansite["favicon"],
  760. "favicon_alt" => $fansite["favicon_alt"],
  761. "title" => $fansite["title"]
  762. ];
  763. }
  764. }
  765. // detect git
  766. if(
  767. (
  768. stripos(
  769. $host["host"],
  770. "git."
  771. ) !== false ||
  772. $host["host"] == "codeberg.org" ||
  773. $host["host"] == "sourceforge.net" ||
  774. $host["host"] == "github.com"
  775. ) &&
  776. isset($host["path"]) &&
  777. preg_match(
  778. '/^(\/[^\/]+\/[^\/]+\/?)/',
  779. $host["path"],
  780. $edge
  781. )
  782. ){
  783. $archives[] = [
  784. "url" => "https://archive.softwareheritage.org/browse/origin/directory/?origin_url=" . urlencode($host["scheme"] . "://" . $host["host"] . $edge[1]),
  785. "favicon" => "www.softwareheritage.org",
  786. "favicon_alt" => "so",
  787. "title" => "SoftwareHeritage"
  788. ];
  789. }
  790. $archives =
  791. array_merge(
  792. $archives,
  793. [
  794. [
  795. "url" => "https://web.archive.org/web/" . $urlencode,
  796. "favicon" => "archive.org",
  797. "favicon_alt" => "ar",
  798. "title" => "Archive.org"
  799. ],
  800. [
  801. "url" => "https://archive.ph/newest/" . htmlspecialchars($link),
  802. "favicon" => "archive.ph",
  803. "favicon_alt" => "ar",
  804. "title" => "Archive.ph"
  805. ],
  806. [
  807. "url" => "https://yandex.com/search/?text=" . urlencode("url:" . $link),
  808. "favicon" => "yandex.com",
  809. "favicon_alt" => "ya",
  810. "title" => "Yandex cache"
  811. ],
  812. [
  813. "url" => "https://ghostarchive.org/search?term=" . $urlencode,
  814. "favicon" => "ghostarchive.org",
  815. "favicon_alt" => "gh",
  816. "title" => "Ghostarchive"
  817. ],
  818. [
  819. "url" => "https://arquivo.pt/wayback/" . htmlspecialchars($link),
  820. "favicon" => "arquivo.pt",
  821. "favicon_alt" => "ar",
  822. "title" => "Arquivo.pt"
  823. ],
  824. [
  825. "url" => "https://megalodon.jp/?url=" . $urlencode,
  826. "favicon" => "megalodon.jp",
  827. "favicon_alt" => "me",
  828. "title" => "Megalodon"
  829. ],
  830. [
  831. "url" => "https://www.webcitation.org/query?url=" . $urlencode,
  832. "favicon" => "webcitation.org",
  833. "favicon_alt" => "we",
  834. "title" => "Webcitation"
  835. ]
  836. ]
  837. );
  838. foreach($archives as $archive){
  839. $payload .= '<a href="' . $archive["url"] . '" class="list" target="_BLANK"><img src="/favicon?s=https://' . $archive["favicon"] . '" alt="' . $archive["favicon_alt"] . '">' . $archive["title"] . '</a>';
  840. }
  841. $payload .= '</div>';
  842. }
  843. /*
  844. Draw link
  845. */
  846. $parts = explode("/", $link);
  847. $clickurl = "";
  848. // remove trailing /
  849. $c = count($parts) - 1;
  850. if($parts[$c] == ""){
  851. $parts[$c - 1] = $parts[$c - 1] . "/";
  852. unset($parts[$c]);
  853. }
  854. // merge https://site together
  855. if(isset($host["host"])){
  856. $parts = [
  857. $parts[0] . $parts[1] . '//' . $parts[2],
  858. ...array_slice($parts, 3, count($parts) - 1)
  859. ];
  860. }
  861. $c = count($parts);
  862. for($i=0; $i<$c; $i++){
  863. if($i !== 0){ $clickurl .= "/"; }
  864. $clickurl .= $parts[$i];
  865. if($i === $c - 1){
  866. $parts[$i] = rtrim($parts[$i], "/");
  867. }
  868. $payload .=
  869. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  870. htmlspecialchars(urldecode($parts[$i])) .
  871. '</a>';
  872. if($i !== $c - 1){
  873. $payload .= '<span class="separator"></span>';
  874. }
  875. }
  876. return $payload . '</div>';
  877. }
  878. public function getscraperfilters($page){
  879. // hack: enable error reporting if configured
  880. if(config::DISPLAY_ERRORS === true){
  881. ini_set('display_errors', 1);
  882. ini_set('display_startup_errors', 1);
  883. }
  884. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  885. if(
  886. isset($_GET["scraper"]) &&
  887. is_string($_GET["scraper"])
  888. ){
  889. $get_scraper = $_GET["scraper"];
  890. }else{
  891. if(
  892. isset($_GET["npt"]) &&
  893. is_string($_GET["npt"])
  894. ){
  895. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  896. $get_scraper =
  897. preg_replace(
  898. '/[0-9]+$/',
  899. "",
  900. $get_scraper
  901. );
  902. }
  903. }
  904. // add search field
  905. $filters =
  906. [
  907. "s" => [
  908. "option" => "_SEARCH"
  909. ]
  910. ];
  911. // define default scrapers
  912. switch($page){
  913. case "web":
  914. $filters["scraper"] = [
  915. "display" => "Scraper",
  916. "option" => [
  917. //"fget" => "fget",
  918. "ddg" => "DuckDuckGo",
  919. //"yahoo" => "Yahoo!",
  920. "brave" => "Brave",
  921. "yandex" => "Yandex",
  922. "google" => "Google",
  923. "google_api" => "Google API",
  924. "google_cse" => "Google CSE",
  925. "yahoo_japan" => "Yahoo! JAPAN",
  926. "startpage" => "Startpage",
  927. "yep" => "Yep",
  928. "mwmbl" => "Mwmbl",
  929. "mojeek" => "Mojeek",
  930. "naver" => "Naver",
  931. "baidu" => "Baidu",
  932. "coccoc" => "Cốc Cốc",
  933. "solofield" => "Solofield",
  934. "marginalia" => "Marginalia",
  935. "purili" => "Purili",
  936. "wiby" => "wiby"
  937. ]
  938. ];
  939. break;
  940. case "images":
  941. $filters["scraper"] = [
  942. "display" => "Scraper",
  943. "option" => [
  944. "ddg" => "DuckDuckGo",
  945. "yandex" => "Yandex",
  946. "brave" => "Brave",
  947. "google" => "Google",
  948. "google_api" => "Google API",
  949. "google_cse" => "Google CSE",
  950. "yahoo_japan" => "Yahoo! JAPAN",
  951. "startpage" => "Startpage",
  952. "naver" => "Naver",
  953. "baidu" => "Baidu",
  954. "solofield" => "Solofield",
  955. "pinterest" => "Pinterest",
  956. "flickr" => "Flickr",
  957. "pexels" => "Pexels",
  958. "pixabay" => "Pixabay",
  959. "unsplash" => "Unsplash",
  960. "fivehpx" => "500px",
  961. "vsco" => "VSCO",
  962. "imgur" => "Imgur",
  963. "ftm" => "FindThatMeme"
  964. ]
  965. ];
  966. break;
  967. case "videos":
  968. $filters["scraper"] = [
  969. "display" => "Scraper",
  970. "option" => [
  971. "yt" => "YouTube",
  972. //"archiveorg" => "Archive.org",
  973. //"dailymotion" => "Dailymotion",
  974. "vimeo" => "Vimeo",
  975. //"odysee" => "Odysee",
  976. "sepiasearch" => "Sepia Search",
  977. //"fb" => "Facebook videos",
  978. "ddg" => "DuckDuckGo",
  979. "brave" => "Brave",
  980. "yandex" => "Yandex",
  981. "google" => "Google",
  982. "yahoo_japan" => "Yahoo! JAPAN",
  983. "startpage" => "Startpage",
  984. "naver" => "Naver",
  985. "baidu" => "Baidu",
  986. "coccoc" => "Cốc Cốc",
  987. "purili" => "Purili",
  988. "solofield" => "Solofield"
  989. ]
  990. ];
  991. break;
  992. case "news":
  993. $filters["scraper"] = [
  994. "display" => "Scraper",
  995. "option" => [
  996. "ddg" => "DuckDuckGo",
  997. "brave" => "Brave",
  998. "google" => "Google",
  999. "yahoo_japan" => "Yahoo! JAPAN",
  1000. "startpage" => "Startpage",
  1001. //"mojeek" => "Mojeek",
  1002. "baidu" => "Baidu"
  1003. ]
  1004. ];
  1005. break;
  1006. case "music":
  1007. $filters["scraper"] = [
  1008. "display" => "Scraper",
  1009. "option" => [
  1010. "sc" => "SoundCloud",
  1011. "swisscows" => "Swisscows (SoundCloud)"
  1012. //"spotify" => "Spotify"
  1013. ]
  1014. ];
  1015. break;
  1016. case "booru":
  1017. $filters["scraper"] = [
  1018. "display" => "Scraper",
  1019. "option" => [
  1020. "safebooru" => "Safebooru",
  1021. "konachan" => "Konachan",
  1022. "tbib" => "The Big Imageboard",
  1023. "gelbooru" => "Gelbooru",
  1024. "yandere" => "Yande.re",
  1025. "tbib" => "The Big Imageboard",
  1026. "sankakucomplex" => "SankakuComplex",
  1027. "soybooru" => "SoyBooru"
  1028. ]
  1029. ];
  1030. break;
  1031. }
  1032. // get scraper name from user input, or default out to preferred scraper
  1033. $scraper_out = null;
  1034. $first = true;
  1035. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  1036. if($first === true){
  1037. $first = $scraper_name;
  1038. }
  1039. if($scraper_name == $get_scraper){
  1040. $scraper_out = $scraper_name;
  1041. }
  1042. }
  1043. if($scraper_out === null){
  1044. $scraper_out = $first;
  1045. }
  1046. include "scraper/$scraper_out.php";
  1047. $lib = new $scraper_out();
  1048. // set scraper on $_GET
  1049. $_GET["scraper"] = $scraper_out;
  1050. // set nsfw on $_GET
  1051. if(
  1052. isset($_COOKIE["nsfw"]) &&
  1053. !isset($_GET["nsfw"])
  1054. ){
  1055. $_GET["nsfw"] = $_COOKIE["nsfw"];
  1056. }
  1057. return
  1058. [
  1059. $lib,
  1060. array_merge_recursive(
  1061. $filters,
  1062. $lib->getfilters($page)
  1063. )
  1064. ];
  1065. }
  1066. public function parsegetfilters($parameters, $whitelist){
  1067. $sanitized = [];
  1068. // add npt token
  1069. if(
  1070. isset($parameters["npt"]) &&
  1071. is_string($parameters["npt"])
  1072. ){
  1073. $sanitized["npt"] = $parameters["npt"];
  1074. }else{
  1075. $sanitized["npt"] = false;
  1076. }
  1077. // we're iterating over $whitelist, so
  1078. // you can't polluate $sanitized with useless
  1079. // parameters
  1080. foreach($whitelist as $parameter => $value){
  1081. if(isset($parameters[$parameter])){
  1082. if(!is_string($parameters[$parameter])){
  1083. $sanitized[$parameter] = null;
  1084. continue;
  1085. }
  1086. // parameter is already set, use that value
  1087. $sanitized[$parameter] = $parameters[$parameter];
  1088. }else{
  1089. // parameter is not set, add it
  1090. if(is_string($value["option"])){
  1091. // special field: set default value manually
  1092. switch($value["option"]){
  1093. case "_DATE":
  1094. // no date set
  1095. $sanitized[$parameter] = false;
  1096. break;
  1097. case "_SEARCH":
  1098. // no search set
  1099. $sanitized[$parameter] = "";
  1100. break;
  1101. }
  1102. }else{
  1103. // set a default value
  1104. $sanitized[$parameter] = array_keys($value["option"])[0];
  1105. }
  1106. }
  1107. // sanitize input
  1108. if(is_array($value["option"])){
  1109. if(
  1110. !in_array(
  1111. $sanitized[$parameter],
  1112. $keys = array_keys($value["option"])
  1113. )
  1114. ){
  1115. $sanitized[$parameter] = $keys[0];
  1116. }
  1117. }else{
  1118. // sanitize search & string
  1119. switch($value["option"]){
  1120. case "_DATE":
  1121. if($sanitized[$parameter] !== false){
  1122. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  1123. if($sanitized[$parameter] <= 0){
  1124. $sanitized[$parameter] = false;
  1125. }
  1126. }
  1127. break;
  1128. case "_SEARCH":
  1129. // get search string
  1130. $sanitized["s"] = trim($sanitized[$parameter]);
  1131. }
  1132. }
  1133. }
  1134. // invert dates if needed
  1135. if(
  1136. isset($sanitized["older"]) &&
  1137. isset($sanitized["newer"]) &&
  1138. $sanitized["newer"] !== false &&
  1139. $sanitized["older"] !== false &&
  1140. $sanitized["newer"] > $sanitized["older"]
  1141. ){
  1142. // invert
  1143. [
  1144. $sanitized["older"],
  1145. $sanitized["newer"]
  1146. ] = [
  1147. $sanitized["newer"],
  1148. $sanitized["older"]
  1149. ];
  1150. }
  1151. return $sanitized;
  1152. }
  1153. public function s_to_timestamp($seconds){
  1154. if(is_string($seconds)){
  1155. return "LIVE";
  1156. }
  1157. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  1158. }
  1159. public function generatehtmltabs($page, $query){
  1160. $html = null;
  1161. //foreach(["web", "images", "videos", "news", "music", "booru"] as $type){
  1162. foreach(["web", "images", "videos", "news", "music"] as $type){
  1163. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  1164. if(!empty($params)){
  1165. $html .= $params;
  1166. }
  1167. $html .= '" class="tab';
  1168. if($type == $page){
  1169. $html .= ' selected';
  1170. }
  1171. $html .= '">' . ucfirst($type) . '</a>';
  1172. }
  1173. return $html;
  1174. }
  1175. public function generatehtmlfilters($filters, $params){
  1176. $html = null;
  1177. foreach($filters as $filter_name => $filter_values){
  1178. if(!isset($filter_values["display"])){
  1179. continue;
  1180. }
  1181. $output = true;
  1182. $tmp =
  1183. '<div class="filter">' .
  1184. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  1185. if(is_array($filter_values["option"])){
  1186. $tmp .= '<select name="' . $filter_name . '">';
  1187. foreach($filter_values["option"] as $option_name => $option_title){
  1188. $tmp .= '<option value="' . $option_name . '"';
  1189. if($params[$filter_name] == $option_name){
  1190. $tmp .= ' selected';
  1191. }
  1192. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  1193. }
  1194. $tmp .= '</select>';
  1195. }else{
  1196. switch($filter_values["option"]){
  1197. case "_DATE":
  1198. $tmp .= '<input type="date" name="' . $filter_name . '"';
  1199. if($params[$filter_name] !== false){
  1200. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  1201. }
  1202. $tmp .= '>';
  1203. break;
  1204. default:
  1205. $output = false;
  1206. break;
  1207. }
  1208. }
  1209. $tmp .= '</div>';
  1210. if($output === true){
  1211. $html .= $tmp;
  1212. }
  1213. }
  1214. return $html;
  1215. }
  1216. public function buildquery($gets, $ommit = false){
  1217. $out = [];
  1218. foreach($gets as $key => $value){
  1219. if(
  1220. $value == null ||
  1221. $value == false ||
  1222. $key == "npt" ||
  1223. $key == "extendedsearch" ||
  1224. $value == "any" ||
  1225. $value == "all" ||
  1226. $key == "spellcheck" ||
  1227. (
  1228. $ommit === true &&
  1229. $key == "s"
  1230. )
  1231. ){
  1232. continue;
  1233. }
  1234. if(
  1235. $key == "older" ||
  1236. $key == "newer"
  1237. ){
  1238. $value = date("Y-m-d", (int)$value);
  1239. }
  1240. $out[$key] = $value;
  1241. }
  1242. return http_build_query($out);
  1243. }
  1244. private function sanitize_slug($username){
  1245. return urlencode(preg_replace('/[-_]/', " ", $username));
  1246. }
  1247. public function increment_real_reqs($scraper){
  1248. apcu_inc(intdiv(time(), 3600) . ".real_requests", 1, $s, 262800);
  1249. apcu_inc(intdiv(time(), 3600) . ".$scraper.requests", 1, $s, 262800);
  1250. }
  1251. public function htmlimage($image, $format){
  1252. if(
  1253. preg_match(
  1254. '/^data:/',
  1255. $image
  1256. )
  1257. ){
  1258. return htmlspecialchars($image);
  1259. }
  1260. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  1261. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  1262. }
  1263. public function htmlnextpage($gets, $npt, $page){
  1264. $query = $this->buildquery($gets);
  1265. return $page . "?" . $query . "&npt=" . $npt;
  1266. }
  1267. }