LibreY- privacy respecting meta search engine |
git clone git://git.acid.vegas/LibreY.git |
Log | Files | Refs | Archive | README | LICENSE |
search_engine.php (5476B)
1 <?php 2 require "misc/cooldowns.php"; 3 abstract class EngineRequest { 4 protected $DO_CACHING = true; 5 function __construct($opts, $mh) { 6 $this->query = $opts->query; 7 $this->page = $opts->page; 8 $this->mh = $mh; 9 $this->opts = $opts; 10 11 $this->url = $this->get_request_url(); 12 if (!$this->url) 13 return; 14 15 if (has_cached_results($this->url)) 16 return; 17 18 $this->ch = curl_init($this->url); 19 20 if ($opts->curl_settings) 21 curl_setopt_array($this->ch, $opts->curl_settings); 22 23 if ($mh) 24 curl_multi_add_handle($mh, $this->ch); 25 } 26 27 public function get_request_url() { 28 return ""; 29 } 30 31 public function successful() { 32 return (isset($this->ch) && curl_getinfo($this->ch)['http_code'] == '200') 33 || has_cached_results($this->url); 34 } 35 36 abstract function parse_results($response); 37 38 public function get_results() { 39 if (!isset($this->url)) 40 return $this->parse_results(null); 41 42 if ($this->DO_CACHING && has_cached_results($this->url)) 43 return fetch_cached_results($this->url); 44 45 if (!isset($this->ch)) 46 return $this->parse_results(null); 47 48 $response = $this->mh ? curl_multi_getcontent($this->ch) : curl_exec($this->ch); 49 $results = $this->parse_results($response) ?? array(); 50 51 if ($this->DO_CACHING && !empty($results)) 52 store_cached_results($this->url, $results, $this->opts->cache_time * 60); 53 54 return $results; 55 } 56 57 public static function print_results($results, $opts) {} 58 } 59 60 function load_opts() { 61 $opts = require "config.php"; 62 63 $opts->request_cooldown ??= 25; 64 $opts->cache_time ??= 25; 65 66 $opts->query = trim($_REQUEST["q"] ?? ""); 67 $opts->type = (int) ($_REQUEST["t"] ?? 0); 68 $opts->page = (int) ($_REQUEST["p"] ?? 0); 69 70 $opts->theme = $_REQUEST["theme"] ?? trim(htmlspecialchars($_COOKIE["theme"] ?? "dark")); 71 72 $opts->safe_search = (int) ($_REQUEST["safe"] ?? 0) == 1 || isset($_COOKIE["safe_search"]); 73 74 $opts->disable_special = (int) ($_REQUEST["ns"] ?? 0) == 1 || isset($_COOKIE["disable_special"]); 75 76 $opts->disable_frontends = (int) ($_REQUEST["nf"] ?? 0) == 1 || isset($_COOKIE["disable_frontends"]); 77 78 $opts->language = $_REQUEST["lang"] ?? trim(htmlspecialchars($_COOKIE["language"] ?? $opts->language ?? "en")); 79 80 $opts->do_fallback = (int) ($_REQUEST["nfb"] ?? 0) == 0; 81 if (!$opts->instance_fallback) { 82 $opts->do_fallback = false; 83 } 84 85 $opts->number_of_results ??= trim(htmlspecialchars($_COOKIE["number_of_results"])); 86 87 foreach (array_keys($opts->frontends ?? array()) as $frontend) { 88 $opts->frontends[$frontend]["instance_url"] = $_COOKIE[$frontend] ?? $opts->frontends[$frontend]["instance_url"]; 89 } 90 91 $opts->curl_settings[CURLOPT_FOLLOWLOCATION] ??= true; 92 93 return $opts; 94 } 95 96 function opts_to_params($opts) { 97 $query = urlencode($opts->query); 98 99 $params = ""; 100 $params .= "p=$opts->page"; 101 $params .= "&q=$query"; 102 $params .= "&t=$opts->type"; 103 $params .= "&safe=" . ($opts->safe_search ? 1 : 0); 104 $params .= "&nf=" . ($opts->disable_frontends ? 1 : 0); 105 $params .= "&ns=" . ($opts->disable_special ? 1 : 0); 106 107 return $params; 108 } 109 110 function init_search($opts, $mh) { 111 switch ($opts->type) 112 { 113 case 1: 114 require "engines/qwant/image.php"; 115 return new QwantImageSearch($opts, $mh); 116 117 case 2: 118 require "engines/invidious/video.php"; 119 return new VideoSearch($opts, $mh); 120 121 case 3: 122 if ($opts->disable_bittorent_search) { 123 echo "<p class=\"text-result-container\">" . TEXTS["feature_disabled"] . "</p>"; 124 break; 125 } 126 127 require "engines/bittorrent/merge.php"; 128 return new TorrentSearch($opts, $mh); 129 130 case 4: 131 if ($opts->disable_hidden_service_search) { 132 echo "<p class=\"text-result-container\">" . TEXTS["feature_disabled"] . "</p>"; 133 break; 134 } 135 require "engines/ahmia/hidden_service.php"; 136 return new TorSearch($opts, $mh); 137 138 default: 139 require "engines/text/text.php"; 140 return new TextSearch($opts, $mh); 141 } 142 } 143 144 function fetch_search_results($opts, $do_print) { 145 $opts->cooldowns = load_cooldowns(); 146 147 $start_time = microtime(true); 148 $mh = curl_multi_init(); 149 $search_category = init_search($opts, $mh); 150 151 $running = null; 152 153 do { 154 curl_multi_exec($mh, $running); 155 } while ($running); 156 157 $results = $search_category->get_results(); 158 159 if (empty($results)) { 160 require "engines/librex/fallback.php"; 161 $results = get_librex_results($opts); 162 } 163 164 if (!$do_print) 165 return $results; 166 167 print_elapsed_time($start_time); 168 $search_category->print_results($results, $opts); 169 170 return $results; 171 } 172 ?>