mirror of https://github.com/hnhx/librex.git
Compare commits
2 Commits
7214e4e729
...
b783acb506
Author | SHA1 | Date |
---|---|---|
davidovski | b783acb506 | |
davidovski | 3f2cd3fdb3 |
|
@ -1,27 +1,46 @@
|
|||
<?php
|
||||
|
||||
function get_librex_results($query, $page)
|
||||
{
|
||||
global $config;
|
||||
|
||||
if (!$config->instance_fallback)
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
$instances_json = json_decode(file_get_contents("instances.json"), true);
|
||||
|
||||
if (empty($instances_json["instances"]))
|
||||
return array();
|
||||
|
||||
|
||||
$instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet'])));
|
||||
shuffle($instances);
|
||||
|
||||
$query_encoded = urlencode($query);
|
||||
|
||||
$results = array();
|
||||
$tries = 0;
|
||||
|
||||
do {
|
||||
$instance = $instances[array_rand($instances)];
|
||||
$tries++;
|
||||
|
||||
// after "too many" requests, give up
|
||||
if ($tries > 5)
|
||||
return array();
|
||||
|
||||
$instance = array_pop($instances);
|
||||
|
||||
if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
|
||||
continue;
|
||||
|
||||
$url = $instance . "api.php?q=$query_encoded&p=$page&t=0";
|
||||
|
||||
$librex_ch = curl_init($url);
|
||||
curl_setopt_array($librex_ch, $config->curl_settings);
|
||||
copy_cookies($librex_ch);
|
||||
$response = curl_exec($librex_ch);
|
||||
curl_close($librex_ch);
|
||||
|
||||
$code = curl_getinfo($librex_ch)["http_code"];
|
||||
$results = json_decode($response, true);
|
||||
|
||||
|
|
|
@ -230,4 +230,11 @@
|
|||
echo "<button type=\"submit\">$text</button>";
|
||||
echo "</form>";
|
||||
}
|
||||
|
||||
function copy_cookies($curl)
|
||||
{
|
||||
if (array_key_exists("HTTP_COOKIE", $_SERVER))
|
||||
curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] );
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue