marginalia.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. class marginalia{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/backend.php";
  7. $this->backend = new backend("marginalia");
  8. }
  9. public function getfilters($page){
  10. if(config::MARGINALIA_API_KEY === null){
  11. $base = [
  12. "adtech" => [
  13. "display" => "Reduce adtech",
  14. "option" => [
  15. "no" => "No",
  16. "yes" => "Yes"
  17. ]
  18. ],
  19. "recent" => [
  20. "display" => "Recent results",
  21. "option" => [
  22. "no" => "No",
  23. "yes" => "Yes"
  24. ]
  25. ],
  26. "intitle" => [
  27. "display" => "Search in title",
  28. "option" => [
  29. "no" => "No",
  30. "yes" => "Yes"
  31. ]
  32. ]
  33. ];
  34. }else{
  35. $base = [];
  36. }
  37. return array_merge(
  38. $base,
  39. [
  40. "format" => [
  41. "display" => "Format",
  42. "option" => [
  43. "any" => "Any format",
  44. "html5" => "html5",
  45. "xhtml" => "xhtml",
  46. "html123" => "html123"
  47. ]
  48. ],
  49. "file" => [
  50. "display" => "Filetype",
  51. "option" => [
  52. "any" => "Any filetype",
  53. "nomedia" => "Deny media",
  54. "media" => "Contains media",
  55. "audio" => "Contains audio",
  56. "video" => "Contains video",
  57. "archive" => "Contains archive",
  58. "document" => "Contains document"
  59. ]
  60. ],
  61. "javascript" => [
  62. "display" => "Javascript",
  63. "option" => [
  64. "any" => "Allow JS",
  65. "deny" => "Deny JS",
  66. "require" => "Require JS"
  67. ]
  68. ],
  69. "trackers" => [
  70. "display" => "Trackers",
  71. "option" => [
  72. "any" => "Allow trackers",
  73. "deny" => "Deny trackers",
  74. "require" => "Require trackers"
  75. ]
  76. ],
  77. "cookies" => [
  78. "display" => "Cookies",
  79. "option" => [
  80. "any" => "Allow cookies",
  81. "deny" => "Deny cookies",
  82. "require" => "Require cookies"
  83. ]
  84. ],
  85. "affiliate" => [
  86. "display" => "Affiliate links in body",
  87. "option" => [
  88. "any" => "Allow affiliate links",
  89. "deny" => "Deny affiliate links",
  90. "require" => "Require affiliate links"
  91. ]
  92. ]
  93. ]
  94. );
  95. }
  96. private function get($proxy, $url, $get = []){
  97. $headers = [
  98. "User-Agent: " . config::USER_AGENT,
  99. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  100. "Accept-Language: en-US,en;q=0.5",
  101. "Accept-Encoding: gzip",
  102. "DNT: 1",
  103. "Connection: keep-alive",
  104. "Upgrade-Insecure-Requests: 1",
  105. "Sec-Fetch-Dest: document",
  106. "Sec-Fetch-Mode: navigate",
  107. "Sec-Fetch-Site: none",
  108. "Sec-Fetch-User: ?1"
  109. ];
  110. $curlproc = curl_init();
  111. if($get !== []){
  112. $get = http_build_query($get);
  113. $url .= "?" . $get;
  114. }
  115. curl_setopt($curlproc, CURLOPT_URL, $url);
  116. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  117. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  118. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  119. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  120. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  121. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  122. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  123. $this->backend->assign_proxy($curlproc, $proxy);
  124. $data = curl_exec($curlproc);
  125. if(curl_errno($curlproc)){
  126. throw new Exception(curl_error($curlproc));
  127. }
  128. curl_close($curlproc);
  129. return $data;
  130. }
  131. public function web($get){
  132. $search = [$get["s"]];
  133. if(strlen($get["s"]) === 0){
  134. throw new Exception("Search term is empty!");
  135. }
  136. $format = $get["format"];
  137. $file = $get["file"];
  138. foreach(
  139. [
  140. "javascript" => $get["javascript"],
  141. "trackers" => $get["trackers"],
  142. "cookies" => $get["cookies"],
  143. "affiliate" => $get["affiliate"]
  144. ]
  145. as $key => $value
  146. ){
  147. if($value == "any"){ continue; }
  148. switch($key){
  149. case "javascript": $str = "js:true"; break;
  150. case "trackers": $str = "special:tracking"; break;
  151. case "cookies": $str = "special:cookies"; break;
  152. case "affiliate": $str = "special:affiliate"; break;
  153. }
  154. if($value == "deny"){
  155. $str = "-" . $str;
  156. }
  157. $search[] = $str;
  158. }
  159. if($format != "any"){
  160. $search[] = "format:$format";
  161. }
  162. switch($file){
  163. case "any": break;
  164. case "nomedia": $search[] = "-special:media"; break;
  165. case "media": $search[] = "special:media"; break;
  166. default:
  167. $search[] = "file:$file";
  168. }
  169. $search = implode(" ", $search);
  170. $out = [
  171. "status" => "ok",
  172. "spelling" => [
  173. "type" => "no_correction",
  174. "using" => null,
  175. "correction" => null
  176. ],
  177. "npt" => null,
  178. "answer" => [],
  179. "web" => [],
  180. "image" => [],
  181. "video" => [],
  182. "news" => [],
  183. "related" => []
  184. ];
  185. if(config::MARGINALIA_API_KEY !== null){
  186. try{
  187. $json =
  188. $this->get(
  189. $this->backend->get_ip(), // no nextpage
  190. "https://api.marginalia.nu/" . config::MARGINALIA_API_KEY . "/search/" . urlencode($search),
  191. [
  192. "count" => 20
  193. ]
  194. );
  195. }catch(Exception $error){
  196. throw new Exception("Failed to get JSON");
  197. }
  198. if($json == "Slow down"){
  199. throw new Exception("The API key used is rate limited. Please try again in a few minutes.");
  200. }
  201. $json = json_decode($json, true);
  202. foreach($json["results"] as $result){
  203. $out["web"][] = [
  204. "title" => $result["title"],
  205. "description" => str_replace("\n", " ", $result["description"]),
  206. "url" => $result["url"],
  207. "date" => null,
  208. "type" => "web",
  209. "thumb" => [
  210. "url" => null,
  211. "ratio" => null
  212. ],
  213. "sublink" => [],
  214. "table" => []
  215. ];
  216. }
  217. return $out;
  218. }
  219. // no more cloudflare!! Parse html by default
  220. $params = [
  221. "query" => $search
  222. ];
  223. foreach(["adtech", "recent", "intitle"] as $v){
  224. if($get[$v] == "yes"){
  225. switch($v){
  226. case "adtech": $params["adtech"] = "reduce"; break;
  227. case "recent": $params["recent"] = "recent"; break;
  228. case "adtech": $params["searchTitle"] = "title"; break;
  229. }
  230. }
  231. }
  232. try{
  233. $html =
  234. $this->get(
  235. $this->backend->get_ip(),
  236. "https://search.marginalia.nu/search",
  237. $params
  238. );
  239. }catch(Exception $error){
  240. throw new Exception("Failed to get HTML");
  241. }
  242. $this->fuckhtml->load($html);
  243. $sections =
  244. $this->fuckhtml
  245. ->getElementsByClassName(
  246. "card search-result",
  247. "section"
  248. );
  249. foreach($sections as $section){
  250. $this->fuckhtml->load($section);
  251. $title =
  252. $this->fuckhtml
  253. ->getElementsByClassName(
  254. "title",
  255. "a"
  256. )[0];
  257. $description =
  258. $this->fuckhtml
  259. ->getElementsByClassName(
  260. "description",
  261. "p"
  262. );
  263. if(count($description) !== 0){
  264. $description =
  265. $this->fuckhtml
  266. ->getTextContent(
  267. $description[0]
  268. );
  269. }else{
  270. $description = null;
  271. }
  272. $sublinks = [];
  273. $sublink_html =
  274. $this->fuckhtml
  275. ->getElementsByClassName("additional-results");
  276. if(count($sublink_html) !== 0){
  277. $this->fuckhtml->load($sublink_html[0]);
  278. $links =
  279. $this->fuckhtml
  280. ->getElementsByTagName("a");
  281. foreach($links as $link){
  282. $sublinks[] = [
  283. "title" =>
  284. $this->fuckhtml
  285. ->getTextContent(
  286. $link
  287. ),
  288. "date" => null,
  289. "description" => null,
  290. "url" =>
  291. $this->fuckhtml
  292. ->getTextContent(
  293. $link["attributes"]["href"]
  294. )
  295. ];
  296. }
  297. }
  298. $out["web"][] = [
  299. "title" =>
  300. $this->fuckhtml
  301. ->getTextContent(
  302. $title
  303. ),
  304. "description" => $description,
  305. "url" =>
  306. $this->fuckhtml
  307. ->getTextContent(
  308. $title["attributes"]["href"]
  309. ),
  310. "date" => null,
  311. "type" => "web",
  312. "thumb" => [
  313. "url" => null,
  314. "ratio" => null
  315. ],
  316. "sublink" => $sublinks,
  317. "table" => []
  318. ];
  319. }
  320. return $out;
  321. }
  322. }