From ff1db195d1fe9d49fad2b7f7803e8790e6597cae Mon Sep 17 00:00:00 2001 From: davidovski Date: Sun, 6 Aug 2023 13:09:06 +0100 Subject: [PATCH 1/8] Add disable automatic redirection setting --- config.php.example | 3 +++ engines/google/text.php | 6 +++++- settings.php | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/config.php.example b/config.php.example index d53280e..009de42 100644 --- a/config.php.example +++ b/config.php.example @@ -20,6 +20,9 @@ "disable_hidden_service_search" => false, + // You can disable automatic redirection from your instance + "automatic_redirection" => true, + /* Preset privacy friendly frontends for users, these can be overwritten by users in the settings e.g.: Preset the invidious instance URL: "instance_url" => "https://yewtu.be", diff --git a/engines/google/text.php b/engines/google/text.php index c598cc1..070e046 100644 --- a/engines/google/text.php +++ b/engines/google/text.php @@ -8,6 +8,7 @@ $results = array(); $domain = $config->google_domain; + $disable_automatic_redirection = isset($_COOKIE["disable_automatic_redirection"]); $site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $config->google_language_site; $results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $config->google_language_results; $number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $config->google_number_of_results; @@ -70,7 +71,10 @@ do { curl_multi_exec($mh, $running); } while ($running); - if (curl_getinfo($google_ch)['http_code'] == '302') { + + if (!$disabled_automatic_redirection + && $config->automatic_redirection + && curl_getinfo($google_ch)['http_code'] == '302') { $instances_json = json_decode(file_get_contents("instances.json"), true); $instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet']))); header("Location: " . $instances[array_rand($instances)] . "search.php?q=$query"); diff --git a/settings.php b/settings.php index fa41f3d..914e456 100644 --- a/settings.php +++ b/settings.php @@ -103,6 +103,13 @@

Google settings

+ automatic_redirection) : ?> +
+ + > +
+ +
Site language Date: Sun, 6 Aug 2023 13:27:40 +0100 Subject: [PATCH 2/8] Fix checkboxes not unchecking when saving --- engines/google/text.php | 11 +++++++---- settings.php | 7 ++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/engines/google/text.php b/engines/google/text.php index 070e046..a83c23c 100644 --- a/engines/google/text.php +++ b/engines/google/text.php @@ -8,7 +8,7 @@ $results = array(); $domain = $config->google_domain; - $disable_automatic_redirection = isset($_COOKIE["disable_automatic_redirection"]); + $automatic_redirection = isset($_COOKIE["automatic_redirection"]); $site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $config->google_language_site; $results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $config->google_language_results; $number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $config->google_number_of_results; @@ -72,13 +72,16 @@ curl_multi_exec($mh, $running); } while ($running); - if (!$disabled_automatic_redirection - && $config->automatic_redirection - && curl_getinfo($google_ch)['http_code'] == '302') { + if (curl_getinfo($google_ch)['http_code'] != '200') { + if ($automatic_redirection + && $config->automatic_redirection) { $instances_json = json_decode(file_get_contents("instances.json"), true); $instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet']))); header("Location: " . $instances[array_rand($instances)] . "search.php?q=$query"); die(); + } else { + return $results; + } } diff --git a/settings.php b/settings.php index 914e456..07169b0 100644 --- a/settings.php +++ b/settings.php @@ -1,7 +1,8 @@ automatic_redirection) : ?>
- - > + + >
From eecaae4a03dace22268e82ed434a7d10306f1180 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 7 Aug 2023 14:54:25 +0100 Subject: [PATCH 3/8] Use librex as fallback search engine --- config.php.example | 4 ++-- engines/google/text.php | 14 ++++---------- engines/librex/text.php | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 engines/librex/text.php diff --git a/config.php.example b/config.php.example index 009de42..7e1b0ed 100644 --- a/config.php.example +++ b/config.php.example @@ -20,8 +20,8 @@ "disable_hidden_service_search" => false, - // You can disable automatic redirection from your instance - "automatic_redirection" => true, + // Fallback to another librex instance if google search fails + "instance_fallback" => true, /* Preset privacy friendly frontends for users, these can be overwritten by users in the settings diff --git a/engines/google/text.php b/engines/google/text.php index a83c23c..9f72483 100644 --- a/engines/google/text.php +++ b/engines/google/text.php @@ -72,16 +72,10 @@ curl_multi_exec($mh, $running); } while ($running); - if (curl_getinfo($google_ch)['http_code'] != '200') { - if ($automatic_redirection - && $config->automatic_redirection) { - $instances_json = json_decode(file_get_contents("instances.json"), true); - $instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet']))); - header("Location: " . $instances[array_rand($instances)] . "search.php?q=$query"); - die(); - } else { - return $results; - } + if (curl_getinfo($google_ch)['http_code'] != '200') + { + require "engines/librex/text.php"; + return get_librex_results($query, $page); } diff --git a/engines/librex/text.php b/engines/librex/text.php new file mode 100644 index 0000000..9ab090f --- /dev/null +++ b/engines/librex/text.php @@ -0,0 +1,27 @@ +instance_fallback) + { + return array(); + } + + $instances_json = json_decode(file_get_contents("instances.json"), true); + $instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet']))); + $instance = $instances[array_rand($instances)]; + + $query_encoded = urlencode($query); + + // TODO add all the required options + $url = $instance . "api.php?q=$query_encoded&p=$page&t=0"; + + $librex_ch = curl_init($url); + curl_setopt_array($librex_ch, $config->curl_settings); + $response = curl_exec($librex_ch); + curl_close($librex_ch); + + return array_values(json_decode($response, true)); + } +?> From feef1f45251f4823e5f61e430d59646955f9bc63 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 7 Aug 2023 15:05:41 +0100 Subject: [PATCH 4/8] Try different instance if fallback fails --- engines/librex/text.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/engines/librex/text.php b/engines/librex/text.php index 9ab090f..0096da5 100644 --- a/engines/librex/text.php +++ b/engines/librex/text.php @@ -10,18 +10,23 @@ $instances_json = json_decode(file_get_contents("instances.json"), true); $instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet']))); - $instance = $instances[array_rand($instances)]; - $query_encoded = urlencode($query); - // TODO add all the required options - $url = $instance . "api.php?q=$query_encoded&p=$page&t=0"; + $results = array(); - $librex_ch = curl_init($url); - curl_setopt_array($librex_ch, $config->curl_settings); - $response = curl_exec($librex_ch); - curl_close($librex_ch); + do { + $instance = $instances[array_rand($instances)]; + $url = $instance . "api.php?q=$query_encoded&p=$page&t=0"; - return array_values(json_decode($response, true)); + $librex_ch = curl_init($url); + curl_setopt_array($librex_ch, $config->curl_settings); + $response = curl_exec($librex_ch); + curl_close($librex_ch); + $code = curl_getinfo($librex_ch)["http_code"]; + $results = json_decode($response, true); + + } while ( $results == null || empty($results)); + + return array_values($results); } ?> From 4349ef54ea722d762842f6426672cabe682436f2 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 7 Aug 2023 15:13:29 +0100 Subject: [PATCH 5/8] Remove automtic redirection setting --- engines/google/text.php | 1 - settings.php | 7 ------- 2 files changed, 8 deletions(-) diff --git a/engines/google/text.php b/engines/google/text.php index 9f72483..336d2f4 100644 --- a/engines/google/text.php +++ b/engines/google/text.php @@ -8,7 +8,6 @@ $results = array(); $domain = $config->google_domain; - $automatic_redirection = isset($_COOKIE["automatic_redirection"]); $site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $config->google_language_site; $results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $config->google_language_results; $number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $config->google_number_of_results; diff --git a/settings.php b/settings.php index 07169b0..c6cae4a 100644 --- a/settings.php +++ b/settings.php @@ -104,13 +104,6 @@

Google settings

- automatic_redirection) : ?> -
- - > -
- -
Site language Date: Mon, 7 Aug 2023 15:33:57 +0100 Subject: [PATCH 6/8] Don't accept a fallback instance with a single result --- engines/librex/text.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engines/librex/text.php b/engines/librex/text.php index 0096da5..09d1b82 100644 --- a/engines/librex/text.php +++ b/engines/librex/text.php @@ -25,7 +25,7 @@ $code = curl_getinfo($librex_ch)["http_code"]; $results = json_decode($response, true); - } while ( $results == null || empty($results)); + } while ( $results == null || count($results) <= 1); return array_values($results); } From 3f2cd3fdb3684f3cd0bb5ad10276ba3abb70669a Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 7 Aug 2023 17:01:12 +0100 Subject: [PATCH 7/8] Copy settings cookies when making a call to other instance --- engines/librex/text.php | 3 +++ misc/tools.php | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/engines/librex/text.php b/engines/librex/text.php index 09d1b82..5c6f27e 100644 --- a/engines/librex/text.php +++ b/engines/librex/text.php @@ -1,4 +1,5 @@ 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); diff --git a/misc/tools.php b/misc/tools.php index 6735220..a6bec50 100644 --- a/misc/tools.php +++ b/misc/tools.php @@ -230,4 +230,10 @@ echo ""; echo ""; } + + function copy_cookies($curl) + { + curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] ); + } + ?> From b783acb506b111132a573758745211aaf6af96ea Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 7 Aug 2023 18:27:54 +0100 Subject: [PATCH 8/8] Filter reqests to self on fallback --- engines/librex/text.php | 22 +++++++++++++++++++--- misc/tools.php | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/engines/librex/text.php b/engines/librex/text.php index 5c6f27e..fe89a65 100644 --- a/engines/librex/text.php +++ b/engines/librex/text.php @@ -5,18 +5,34 @@ 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); diff --git a/misc/tools.php b/misc/tools.php index a6bec50..80bc370 100644 --- a/misc/tools.php +++ b/misc/tools.php @@ -233,7 +233,8 @@ function copy_cookies($curl) { - curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] ); + if (array_key_exists("HTTP_COOKIE", $_SERVER)) + curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] ); } ?>