LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

fallback.php (2268B)

      1 <?php
      2 
      3     class LibreXFallback extends EngineRequest {
      4         public function __construct($instance, $opts, $mh) {
      5             $this->instance = $instance;
      6             parent::__construct($opts, $mh);
      7         }
      8 
      9         public function get_request_url() {
     10            return $this->instance . "api.php?" . opts_to_params($this->opts) . "&nfb=1";
     11         }
     12 
     13         public function parse_results($response) {
     14             $response = json_decode($response, true);
     15             if (!$response)
     16                 return array();
     17 
     18             return array_values($response);
     19         }
     20     }
     21 
     22     function load_instances($cooldowns) {
     23         $instances_json = json_decode(file_get_contents("instances.json"), true);
     24 
     25         if (empty($instances_json["instances"]))
     26             return array();
     27 
     28         $instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet'])));
     29         $instances = array_filter($instances, fn($n) => !has_cooldown($n, $cooldowns));
     30         shuffle($instances);
     31         return $instances;
     32     }
     33 
     34     function get_librex_results($opts) {
     35         if (!$opts->do_fallback)
     36             return array();
     37 
     38         $cooldowns = $opts->cooldowns;
     39         $instances = load_instances($cooldowns);
     40 
     41         $results = array();
     42         $tries = 0;
     43 
     44         do {
     45             $tries++;
     46 
     47             $instance = array_pop($instances);
     48 
     49             if (!$instance)
     50                 break;
     51 
     52             if (!filter_var($instance, FILTER_VALIDATE_URL) || !filter_var($_SERVER["HTTP_HOST"], FILTER_VALIDATE_URL))
     53                 continue;
     54             if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
     55                 continue;
     56 
     57             $librex_request = new LibreXFallback($instance, $opts, null);
     58 
     59             $results = $librex_request->get_results();
     60 
     61             if (!empty($results))
     62                 return $results;
     63 
     64             // on fail then do this
     65             $timeout = ($opts->request_cooldown ?? "1") * 60;
     66             $cooldowns = set_cooldown($instance, $timeout, $cooldowns);
     67 
     68         } while (!empty($instances));
     69 
     70         return array(
     71             "error" => array(
     72                 "message" => TEXTS["failure_fallback"]
     73             )
     74         );
     75     }
     76 
     77 ?>