frontend.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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)\/([^\/]+)\//',
  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 "boosty.to": // https://boosty.to/ <username>
  712. case "www.subscribestar.com": // https://www.subscribestar.com/ <username>
  713. case "subscribestar.com":
  714. case "www.dlsite.com": // https://www.dlsite.com/ <username> /
  715. case "dlsite.com":
  716. case "candfans.com": // https://candfans.com/ <username>
  717. case "afdian.com": // https://afdian.com/a/ <username>
  718. if(
  719. isset($host["path"]) &&
  720. (
  721. (
  722. $host["host"] == "afdian.com" &&
  723. preg_match(
  724. '/^\/a\/([A-Za-z0-9-_.]+)/',
  725. $host["path"],
  726. $username
  727. )
  728. ) ||
  729. preg_match(
  730. '/^\/([A-Za-z0-9-_.]+)/',
  731. $host["path"],
  732. $username
  733. )
  734. )
  735. ){
  736. foreach($fansites as $fansite){
  737. $archives[] = [
  738. "url" => $fansite["url"] . $this->sanitize_slug($username[1]),
  739. "favicon" => $fansite["favicon"],
  740. "favicon_alt" => $fansite["favicon_alt"],
  741. "title" => $fansite["title"]
  742. ];
  743. }
  744. }
  745. break;
  746. }
  747. // special fansite cases
  748. if(
  749. preg_match(
  750. '/^([A-Za-z0-9_]+)\.(?:fanbox\.cc|gumroad\.com)/',
  751. $host["host"],
  752. $username
  753. )
  754. ){
  755. foreach($fansites as $fansite){
  756. $archives[] = [
  757. "url" => $fansite["url"] . $this->sanitize_slug($username[1]),
  758. "favicon" => $fansite["favicon"],
  759. "favicon_alt" => $fansite["favicon_alt"],
  760. "title" => $fansite["title"]
  761. ];
  762. }
  763. }
  764. // detect git
  765. if(
  766. (
  767. stripos(
  768. $host["host"],
  769. "git."
  770. ) !== false ||
  771. $host["host"] == "codeberg.org" ||
  772. $host["host"] == "sourceforge.net" ||
  773. $host["host"] == "github.com"
  774. ) &&
  775. isset($host["path"]) &&
  776. preg_match(
  777. '/^(\/[^\/]+\/[^\/]+\/?)/',
  778. $host["path"],
  779. $edge
  780. )
  781. ){
  782. $archives[] = [
  783. "url" => "https://archive.softwareheritage.org/browse/origin/directory/?origin_url=" . urlencode($host["scheme"] . "://" . $host["host"] . $edge[1]),
  784. "favicon" => "www.softwareheritage.org",
  785. "favicon_alt" => "so",
  786. "title" => "SoftwareHeritage"
  787. ];
  788. }
  789. $archives =
  790. array_merge(
  791. $archives,
  792. [
  793. [
  794. "url" => "https://web.archive.org/web/" . $urlencode,
  795. "favicon" => "archive.org",
  796. "favicon_alt" => "ar",
  797. "title" => "Archive.org"
  798. ],
  799. [
  800. "url" => "https://archive.ph/newest/" . htmlspecialchars($link),
  801. "favicon" => "archive.ph",
  802. "favicon_alt" => "ar",
  803. "title" => "Archive.ph"
  804. ],
  805. [
  806. "url" => "https://yandex.com/search/?text=" . urlencode("url:" . $link),
  807. "favicon" => "yandex.com",
  808. "favicon_alt" => "ya",
  809. "title" => "Yandex cache"
  810. ],
  811. [
  812. "url" => "https://ghostarchive.org/search?term=" . $urlencode,
  813. "favicon" => "ghostarchive.org",
  814. "favicon_alt" => "gh",
  815. "title" => "Ghostarchive"
  816. ],
  817. [
  818. "url" => "https://arquivo.pt/wayback/" . htmlspecialchars($link),
  819. "favicon" => "arquivo.pt",
  820. "favicon_alt" => "ar",
  821. "title" => "Arquivo.pt"
  822. ],
  823. [
  824. "url" => "https://megalodon.jp/?url=" . $urlencode,
  825. "favicon" => "megalodon.jp",
  826. "favicon_alt" => "me",
  827. "title" => "Megalodon"
  828. ],
  829. [
  830. "url" => "https://www.webcitation.org/query?url=" . $urlencode,
  831. "favicon" => "webcitation.org",
  832. "favicon_alt" => "we",
  833. "title" => "Webcitation"
  834. ]
  835. ]
  836. );
  837. foreach($archives as $archive){
  838. $payload .= '<a href="' . $archive["url"] . '" class="list" target="_BLANK"><img src="/favicon?s=https://' . $archive["favicon"] . '" alt="' . $archive["favicon_alt"] . '">' . $archive["title"] . '</a>';
  839. }
  840. $payload .= '</div>';
  841. }
  842. /*
  843. Draw link
  844. */
  845. $parts = explode("/", $link);
  846. $clickurl = "";
  847. // remove trailing /
  848. $c = count($parts) - 1;
  849. if($parts[$c] == ""){
  850. $parts[$c - 1] = $parts[$c - 1] . "/";
  851. unset($parts[$c]);
  852. }
  853. // merge https://site together
  854. if(isset($host["host"])){
  855. $parts = [
  856. $parts[0] . $parts[1] . '//' . $parts[2],
  857. ...array_slice($parts, 3, count($parts) - 1)
  858. ];
  859. }
  860. $c = count($parts);
  861. for($i=0; $i<$c; $i++){
  862. if($i !== 0){ $clickurl .= "/"; }
  863. $clickurl .= $parts[$i];
  864. if($i === $c - 1){
  865. $parts[$i] = rtrim($parts[$i], "/");
  866. }
  867. $payload .=
  868. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  869. htmlspecialchars(urldecode($parts[$i])) .
  870. '</a>';
  871. if($i !== $c - 1){
  872. $payload .= '<span class="separator"></span>';
  873. }
  874. }
  875. return $payload . '</div>';
  876. }
  877. public function getscraperfilters($page){
  878. // hack: enable error reporting if configured
  879. if(config::DISPLAY_ERRORS === true){
  880. ini_set('display_errors', 1);
  881. ini_set('display_startup_errors', 1);
  882. }
  883. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  884. if(
  885. isset($_GET["scraper"]) &&
  886. is_string($_GET["scraper"])
  887. ){
  888. $get_scraper = $_GET["scraper"];
  889. }else{
  890. if(
  891. isset($_GET["npt"]) &&
  892. is_string($_GET["npt"])
  893. ){
  894. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  895. $get_scraper =
  896. preg_replace(
  897. '/[0-9]+$/',
  898. "",
  899. $get_scraper
  900. );
  901. }
  902. }
  903. // add search field
  904. $filters =
  905. [
  906. "s" => [
  907. "option" => "_SEARCH"
  908. ]
  909. ];
  910. // define default scrapers
  911. switch($page){
  912. case "web":
  913. $filters["scraper"] = [
  914. "display" => "Scraper",
  915. "option" => [
  916. //"fget" => "fget",
  917. "ddg" => "DuckDuckGo",
  918. //"yahoo" => "Yahoo!",
  919. "brave" => "Brave",
  920. "yandex" => "Yandex",
  921. "google" => "Google",
  922. "google_api" => "Google API",
  923. "google_cse" => "Google CSE",
  924. "yahoo_japan" => "Yahoo! JAPAN",
  925. "startpage" => "Startpage",
  926. "yep" => "Yep",
  927. "mwmbl" => "Mwmbl",
  928. "mojeek" => "Mojeek",
  929. "naver" => "Naver",
  930. "baidu" => "Baidu",
  931. "coccoc" => "Cốc Cốc",
  932. "solofield" => "Solofield",
  933. "marginalia" => "Marginalia",
  934. "purili" => "Purili",
  935. "wiby" => "wiby"
  936. ]
  937. ];
  938. break;
  939. case "images":
  940. $filters["scraper"] = [
  941. "display" => "Scraper",
  942. "option" => [
  943. "ddg" => "DuckDuckGo",
  944. "yandex" => "Yandex",
  945. "brave" => "Brave",
  946. "google" => "Google",
  947. "google_api" => "Google API",
  948. "google_cse" => "Google CSE",
  949. "yahoo_japan" => "Yahoo! JAPAN",
  950. "startpage" => "Startpage",
  951. "naver" => "Naver",
  952. "baidu" => "Baidu",
  953. "solofield" => "Solofield",
  954. "pinterest" => "Pinterest",
  955. "flickr" => "Flickr",
  956. "pexels" => "Pexels",
  957. "pixabay" => "Pixabay",
  958. "unsplash" => "Unsplash",
  959. "fivehpx" => "500px",
  960. "vsco" => "VSCO",
  961. "imgur" => "Imgur",
  962. "ftm" => "FindThatMeme"
  963. ]
  964. ];
  965. break;
  966. case "videos":
  967. $filters["scraper"] = [
  968. "display" => "Scraper",
  969. "option" => [
  970. "yt" => "YouTube",
  971. //"archiveorg" => "Archive.org",
  972. //"dailymotion" => "Dailymotion",
  973. "vimeo" => "Vimeo",
  974. //"odysee" => "Odysee",
  975. "sepiasearch" => "Sepia Search",
  976. //"fb" => "Facebook videos",
  977. "ddg" => "DuckDuckGo",
  978. "brave" => "Brave",
  979. "yandex" => "Yandex",
  980. "google" => "Google",
  981. "yahoo_japan" => "Yahoo! JAPAN",
  982. "startpage" => "Startpage",
  983. "naver" => "Naver",
  984. "baidu" => "Baidu",
  985. "coccoc" => "Cốc Cốc",
  986. "purili" => "Purili",
  987. "solofield" => "Solofield"
  988. ]
  989. ];
  990. break;
  991. case "news":
  992. $filters["scraper"] = [
  993. "display" => "Scraper",
  994. "option" => [
  995. "ddg" => "DuckDuckGo",
  996. "brave" => "Brave",
  997. "google" => "Google",
  998. "yahoo_japan" => "Yahoo! JAPAN",
  999. "startpage" => "Startpage",
  1000. //"mojeek" => "Mojeek",
  1001. "baidu" => "Baidu"
  1002. ]
  1003. ];
  1004. break;
  1005. case "music":
  1006. $filters["scraper"] = [
  1007. "display" => "Scraper",
  1008. "option" => [
  1009. "sc" => "SoundCloud",
  1010. "swisscows" => "Swisscows (SoundCloud)"
  1011. //"spotify" => "Spotify"
  1012. ]
  1013. ];
  1014. break;
  1015. case "booru":
  1016. $filters["scraper"] = [
  1017. "display" => "Scraper",
  1018. "option" => [
  1019. "safebooru" => "Safebooru",
  1020. "konachan" => "Konachan",
  1021. "tbib" => "The Big Imageboard",
  1022. "gelbooru" => "Gelbooru",
  1023. "yandere" => "Yande.re",
  1024. "tbib" => "The Big Imageboard",
  1025. "sankakucomplex" => "SankakuComplex",
  1026. "soybooru" => "SoyBooru"
  1027. ]
  1028. ];
  1029. break;
  1030. }
  1031. // get scraper name from user input, or default out to preferred scraper
  1032. $scraper_out = null;
  1033. $first = true;
  1034. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  1035. if($first === true){
  1036. $first = $scraper_name;
  1037. }
  1038. if($scraper_name == $get_scraper){
  1039. $scraper_out = $scraper_name;
  1040. }
  1041. }
  1042. if($scraper_out === null){
  1043. $scraper_out = $first;
  1044. }
  1045. include "scraper/$scraper_out.php";
  1046. $lib = new $scraper_out();
  1047. // set scraper on $_GET
  1048. $_GET["scraper"] = $scraper_out;
  1049. // set nsfw on $_GET
  1050. if(
  1051. isset($_COOKIE["nsfw"]) &&
  1052. !isset($_GET["nsfw"])
  1053. ){
  1054. $_GET["nsfw"] = $_COOKIE["nsfw"];
  1055. }
  1056. return
  1057. [
  1058. $lib,
  1059. array_merge_recursive(
  1060. $filters,
  1061. $lib->getfilters($page)
  1062. )
  1063. ];
  1064. }
  1065. public function parsegetfilters($parameters, $whitelist){
  1066. $sanitized = [];
  1067. // add npt token
  1068. if(
  1069. isset($parameters["npt"]) &&
  1070. is_string($parameters["npt"])
  1071. ){
  1072. $sanitized["npt"] = $parameters["npt"];
  1073. }else{
  1074. $sanitized["npt"] = false;
  1075. }
  1076. // we're iterating over $whitelist, so
  1077. // you can't polluate $sanitized with useless
  1078. // parameters
  1079. foreach($whitelist as $parameter => $value){
  1080. if(isset($parameters[$parameter])){
  1081. if(!is_string($parameters[$parameter])){
  1082. $sanitized[$parameter] = null;
  1083. continue;
  1084. }
  1085. // parameter is already set, use that value
  1086. $sanitized[$parameter] = $parameters[$parameter];
  1087. }else{
  1088. // parameter is not set, add it
  1089. if(is_string($value["option"])){
  1090. // special field: set default value manually
  1091. switch($value["option"]){
  1092. case "_DATE":
  1093. // no date set
  1094. $sanitized[$parameter] = false;
  1095. break;
  1096. case "_SEARCH":
  1097. // no search set
  1098. $sanitized[$parameter] = "";
  1099. break;
  1100. }
  1101. }else{
  1102. // set a default value
  1103. $sanitized[$parameter] = array_keys($value["option"])[0];
  1104. }
  1105. }
  1106. // sanitize input
  1107. if(is_array($value["option"])){
  1108. if(
  1109. !in_array(
  1110. $sanitized[$parameter],
  1111. $keys = array_keys($value["option"])
  1112. )
  1113. ){
  1114. $sanitized[$parameter] = $keys[0];
  1115. }
  1116. }else{
  1117. // sanitize search & string
  1118. switch($value["option"]){
  1119. case "_DATE":
  1120. if($sanitized[$parameter] !== false){
  1121. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  1122. if($sanitized[$parameter] <= 0){
  1123. $sanitized[$parameter] = false;
  1124. }
  1125. }
  1126. break;
  1127. case "_SEARCH":
  1128. // get search string
  1129. $sanitized["s"] = trim($sanitized[$parameter]);
  1130. }
  1131. }
  1132. }
  1133. // invert dates if needed
  1134. if(
  1135. isset($sanitized["older"]) &&
  1136. isset($sanitized["newer"]) &&
  1137. $sanitized["newer"] !== false &&
  1138. $sanitized["older"] !== false &&
  1139. $sanitized["newer"] > $sanitized["older"]
  1140. ){
  1141. // invert
  1142. [
  1143. $sanitized["older"],
  1144. $sanitized["newer"]
  1145. ] = [
  1146. $sanitized["newer"],
  1147. $sanitized["older"]
  1148. ];
  1149. }
  1150. return $sanitized;
  1151. }
  1152. public function s_to_timestamp($seconds){
  1153. if(is_string($seconds)){
  1154. return "LIVE";
  1155. }
  1156. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  1157. }
  1158. public function generatehtmltabs($page, $query){
  1159. $html = null;
  1160. //foreach(["web", "images", "videos", "news", "music", "booru"] as $type){
  1161. foreach(["web", "images", "videos", "news", "music"] as $type){
  1162. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  1163. if(!empty($params)){
  1164. $html .= $params;
  1165. }
  1166. $html .= '" class="tab';
  1167. if($type == $page){
  1168. $html .= ' selected';
  1169. }
  1170. $html .= '">' . ucfirst($type) . '</a>';
  1171. }
  1172. return $html;
  1173. }
  1174. public function generatehtmlfilters($filters, $params){
  1175. $html = null;
  1176. foreach($filters as $filter_name => $filter_values){
  1177. if(!isset($filter_values["display"])){
  1178. continue;
  1179. }
  1180. $output = true;
  1181. $tmp =
  1182. '<div class="filter">' .
  1183. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  1184. if(is_array($filter_values["option"])){
  1185. $tmp .= '<select name="' . $filter_name . '">';
  1186. foreach($filter_values["option"] as $option_name => $option_title){
  1187. $tmp .= '<option value="' . $option_name . '"';
  1188. if($params[$filter_name] == $option_name){
  1189. $tmp .= ' selected';
  1190. }
  1191. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  1192. }
  1193. $tmp .= '</select>';
  1194. }else{
  1195. switch($filter_values["option"]){
  1196. case "_DATE":
  1197. $tmp .= '<input type="date" name="' . $filter_name . '"';
  1198. if($params[$filter_name] !== false){
  1199. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  1200. }
  1201. $tmp .= '>';
  1202. break;
  1203. default:
  1204. $output = false;
  1205. break;
  1206. }
  1207. }
  1208. $tmp .= '</div>';
  1209. if($output === true){
  1210. $html .= $tmp;
  1211. }
  1212. }
  1213. return $html;
  1214. }
  1215. public function buildquery($gets, $ommit = false){
  1216. $out = [];
  1217. foreach($gets as $key => $value){
  1218. if(
  1219. $value == null ||
  1220. $value == false ||
  1221. $key == "npt" ||
  1222. $key == "extendedsearch" ||
  1223. $value == "any" ||
  1224. $value == "all" ||
  1225. $key == "spellcheck" ||
  1226. (
  1227. $ommit === true &&
  1228. $key == "s"
  1229. )
  1230. ){
  1231. continue;
  1232. }
  1233. if(
  1234. $key == "older" ||
  1235. $key == "newer"
  1236. ){
  1237. $value = date("Y-m-d", (int)$value);
  1238. }
  1239. $out[$key] = $value;
  1240. }
  1241. return http_build_query($out);
  1242. }
  1243. private function sanitize_slug($username){
  1244. return urlencode(preg_replace('/[-_]/', " ", $username));
  1245. }
  1246. public function increment_real_reqs($scraper){
  1247. apcu_inc(intdiv(time(), 3600) . ".real_requests", 1, $s, 262800);
  1248. apcu_inc(intdiv(time(), 3600) . ".$scraper.requests", 1, $s, 262800);
  1249. }
  1250. public function htmlimage($image, $format){
  1251. if(
  1252. preg_match(
  1253. '/^data:/',
  1254. $image
  1255. )
  1256. ){
  1257. return htmlspecialchars($image);
  1258. }
  1259. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  1260. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  1261. }
  1262. public function htmlnextpage($gets, $npt, $page){
  1263. $query = $this->buildquery($gets);
  1264. return $page . "?" . $query . "&npt=" . $npt;
  1265. }
  1266. }