From bf2adabce07e385cdb110677b8f71df755b5d4d2 Mon Sep 17 00:00:00 2001
From: Giuseppe Amato <gam@bit4id.com>
Date: Fri, 17 Feb 2023 12:10:07 +0100
Subject: [PATCH] added user selectable search domain, checked against a
 supported domains list updated docker files to support changes in
 config/settings

---
 config.php.example        |   2 +
 docker/README.md          |  14 ++---
 docker/attributes.sh      |   7 ++-
 docker/php/config.php     | 107 ++++++++++++++++++++++++++++++++------
 docker/php/php.dockerfile |   4 +-
 settings.php              |  19 +++++++
 6 files changed, 129 insertions(+), 24 deletions(-)

diff --git a/config.php.example b/config.php.example
index 481a276a..19273c62 100644
--- a/config.php.example
+++ b/config.php.example
@@ -1,6 +1,8 @@
 <?php
     return (object) array(
 
+        # user selectable google domains, form https://www.google.com/supported_domains
+        "google_allowed_domains" => explode(" ", "com ad ae com.af com.ag com.ai al am co.ao com.ar as at com.au az ba com.bd be bf bg com.bh bi bj com.bn com.bo com.br bs bt co.bw by com.bz ca cd cf cg ch ci co.ck cl cm cn com.co co.cr com.cu cv com.cy cz de dj dk dm com.do dz com.ec ee com.eg es com.et fi com.fj fm fr ga ge gg com.gh com.gi gl gm gr com.gt gy com.hk hn hr ht hu co.id ie co.il im co.in iq is it je com.jm jo co.jp co.ke com.kh ki kg co.kr com.kw kz la com.lb li lk co.ls lt lu lv com.ly co.ma md me mg mk ml com.mm mn ms com.mt mu mv mw com.mx com.my co.mz com.na com.ng com.ni ne nl no com.np nr nu co.nz com.om com.pa com.pe com.pg com.ph com.pk pl pn com.pr ps pt com.py com.qa ro ru rw com.sa com.sb sc se com.sg sh si sk com.sl sn so sm sr st com.sv td tg co.th com.tj tl tm tn to com.tr tt com.tw co.tz com.ua co.ug co.uk com.uy co.uz com.vc co.ve vg co.vi com.vn vu ws rs co.za co.zm co.zw cat"),
         // e.g.: fr -> https://google.fr/
         "google_domain" => "com",
 
diff --git a/docker/README.md b/docker/README.md
index adc3b97e..09cc6327 100644
--- a/docker/README.md
+++ b/docker/README.md
@@ -26,7 +26,7 @@ To run librex in a docker container, you can simply use the command:
 docker run -d --name librex \
     -e TZ="America/New_York" \
     -e CONFIG_GOOGLE_DOMAIN="com" \
-    -e CONFIG_GOOGLE_LANGUAGE="en" \
+    -e CONFIG_GOOGLE_LANGUAGE_SITE="en" \
     -p 8080:8080 \
     librex/librex:latest
 ```
@@ -48,9 +48,9 @@ services:
       - PUID=1000
       - PGID=1000
       - VERSION=docker
-      - TZ="America/New_York"
-      - CONFIG_GOOGLE_DOMAIN="com"
-      - CONFIG_GOOGLE_LANGUAGE="en"
+      - TZ=America/New_York
+      - CONFIG_GOOGLE_DOMAIN=com
+      - CONFIG_GOOGLE_LANGUAGE_SITE=en
     volumes:
       - ./nginx_logs:/var/log/nginx
       - ./php_logs:/var/log/php7
@@ -82,7 +82,9 @@ This docker image was developed with high configurability in mind, so here is th
 | Variables | Default | Examples | Description |
 |:----------|:-------------|:---------|:------|
 | CONFIG_GOOGLE_DOMAIN |  "com" | "com", "com.br", "com.es" | Defines which Google domain the search will be done, change according to your country |
-| CONFIG_GOOGLE_LANGUAGE | "en" | "pt", "es", "ru" | Defines the language in which searches will be done, see the list of supported languages [here](https://developers.google.com/custom-search/docs/ref_languages). |
+| CONFIG_GOOGLE_ALLOWED_DOMAINS |  see Description | "com co.uk ca it fr de" | Defines a list of allowed Google domains selectable by user in settings; defaults to a list of supported domains got from [here](https://www.google.com/supported_domains).|
+| CONFIG_GOOGLE_LANGUAGE_SITE | "en" | "pt", "es", "ru" | Defines the language in which searches will be done, see the list of supported languages [here](https://developers.google.com/custom-search/docs/ref_languages). |
+| CONFIG_GOOGLE_LANGUAGE_RESULTS | "" | "en", "pt", "es", "ru" | Defines the language of searche results|
 | CONFIG_INVIDIOUS_INSTANCE | "https://invidious.namazso.eu" | string | Defines the host that will be used to do video searches using invidious |
 | CONFIG_HIDDEN_SERVICE_SEARCH | false | boolean | Defines whether safesearch will be enabled or disabled |
 | CONFIG_DISABLE_BITTORRENT_SEARCH | false | boolean | Defines whether bittorrent support will be enabled or disabled |
@@ -148,7 +150,7 @@ docker build https://github.com/hnhx/librex.git -t librex:latest
 ```sh
 docker run -d --name librex \
     -e CONFIG_GOOGLE_DOMAIN="com" \
-    -e CONFIG_GOOGLE_LANGUAGE="en" \
+    -e CONFIG_GOOGLE_LANGUAGE_SITE="en" \
     -p 8080:8080 \
     librex:latest
 ```
diff --git a/docker/attributes.sh b/docker/attributes.sh
index 012c7c29..c6d4d33c 100755
--- a/docker/attributes.sh
+++ b/docker/attributes.sh
@@ -19,8 +19,11 @@ export OPEN_SEARCH_HOST=${OPEN_SEARCH_HOST:-"127.0.0.1"}
 
 # Replace the 'config.php' script, which contains the most common search engine configurations, with these environment setups
 # These environment setups can be found in 'config.php', and the default configurations can be useful for most use cases
-export CONFIG_GOOGLE_DOMAIN=${CONFIG_GOOGLE_DOMAIN:-".com"}
-export CONFIG_GOOGLE_LANGUAGE=${CONFIG_GOOGLE_LANGUAGE:-"en"}
+# allowed domains from https://www.google.com/supported_domains
+export CONFIG_GOOGLE_ALLOWED_DOMAINS=${CONFIG_GOOGLE_ALLOWED_DOMAINS:-"com ad ae com.af com.ag com.ai al am co.ao com.ar as at com.au az ba com.bd be bf bg com.bh bi bj com.bn com.bo com.br bs bt co.bw by com.bz ca cd cf cg ch ci co.ck cl cm cn com.co co.cr com.cu cv com.cy cz de dj dk dm com.do dz com.ec ee com.eg es com.et fi com.fj fm fr ga ge gg com.gh com.gi gl gm gr com.gt gy com.hk hn hr ht hu co.id ie co.il im co.in iq is it je com.jm jo co.jp co.ke com.kh ki kg co.kr com.kw kz la com.lb li lk co.ls lt lu lv com.ly co.ma md me mg mk ml com.mm mn ms com.mt mu mv mw com.mx com.my co.mz com.na com.ng com.ni ne nl no com.np nr nu co.nz com.om com.pa com.pe com.pg com.ph com.pk pl pn com.pr ps pt com.py com.qa ro ru rw com.sa com.sb sc se com.sg sh si sk com.sl sn so sm sr st com.sv td tg co.th com.tj tl tm tn to com.tr tt com.tw co.tz com.ua co.ug co.uk com.uy co.uz com.vc co.ve vg co.vi com.vn vu ws rs co.za co.zm co.zw cat"}
+export CONFIG_GOOGLE_DOMAIN=${CONFIG_GOOGLE_DOMAIN:-"com"}
+export CONFIG_GOOGLE_LANGUAGE_SITE=${CONFIG_GOOGLE_LANGUAGE_SITE:-"en"}
+export CONFIG_GOOGLE_LANGUAGE_RESULTS=${CONFIG_GOOGLE_LANGUAGE_RESULTS:-""}
 export CONFIG_INVIDIOUS_INSTANCE=${CONFIG_INVIDIOUS_INSTANCE:-"invidious.namazso.eu"}
 export CONFIG_HIDDEN_SERVICE_SEARCH=${CONFIG_HIDDEN_SERVICE_SEARCH:-false}
 export CONFIG_DISABLE_BITTORRENT_SEARCH=${CONFIG_DISABLE_BITTORRENT_SEARCH:-false}
diff --git a/docker/php/config.php b/docker/php/config.php
index ded967da..b42cf0a7 100644
--- a/docker/php/config.php
+++ b/docker/php/config.php
@@ -1,28 +1,105 @@
 <?php
     return (object) array(
+    
+        "google_allowed_domains" => explode(" ", "${CONFIG_GOOGLE_ALLOWED_DOMAINS}"),
+        
         "google_domain" => "${CONFIG_GOOGLE_DOMAIN}",
-        "google_language" => "${CONFIG_GOOGLE_LANGUAGE}",
+        "google_language_site" => "${CONFIG_GOOGLE_LANGUAGE_SITE}",
+        "google_language_results" => "${CONFIG_GOOGLE_LANGUAGE_RESULTS}",
         "invidious_instance_for_video_results" => "${CONFIG_INVIDIOUS_INSTANCE}",
 
         "disable_bittorent_search" => ${CONFIG_DISABLE_BITTORRENT_SEARCH},
         "bittorent_trackers" => "${CONFIG_BITTORRENT_TRACKERS}",
         "disable_hidden_service_search" => ${CONFIG_HIDDEN_SERVICE_SEARCH},
 
-        "invidious" => "${APP_INVIDIOUS}", // youtube
-        "bibliogram" => "${APP_BIBLIOGRAM}", // instagram
-        "rimgo" => "${APP_RIMGO}", // imgur
-        "scribe" => "${APP_SCRIBE}", // medium
-        "librarian" => "${APP_LIBRARIAN}", // odysee
-        "gothub" => "${APP_GOTHUB}", // github
-        "nitter" => "${APP_NITTER}", // twitter
-        "libreddit" => "${APP_LIBREREDDIT}", // reddit
-        "proxitok" => "${APP_PROXITOK}", // tiktok
-        "wikiless" => "${APP_WIKILESS}", // wikipedia
-        "quetre" => "${APP_QUETRE}", // quora
-        "libremdb" => "${APP_LIBREMDB}", // imdb,
-        "breezewiki" => "${APP_BREEZEWIKI}", // fandom,
-        "anonymousoverflow" => "${APP_ANONYMOUS_OVERFLOW}", // stackoverflow
+        "frontends" => array(
+            "invidious" => array(
+                "instance_url" => "${APP_INVIDIOUS}",
+                "project_url" => "https://docs.invidious.io/instances/",
+                "original_name" => "YouTube",
+                "original_url" => "youtube.com"
+            ),
+            "bibliogram" => array(
+                "instance_url" => "${APP_BIBLIOGRAM}",
+                "project_url" => "https://git.sr.ht/~cadence/bibliogram-docs/tree/master/docs/Instances.md",
+                "original_name" => "Instagram",
+                "original_url" => "instagram.com"
+            ),
+            "rimgo" => array(
+                "instance_url" => "${APP_RIMGO}",
+                "project_url" => "https://codeberg.org/video-prize-ranch/rimgo#instances",
+                "original_name" => "Imgur",
+                "original_url" => "imgur.com"
+            ),
+            "scribe" => array(
+                "instance_url" => "${APP_SCRIBE}",
+                "project_url" => "https://git.sr.ht/~edwardloveall/scribe/tree/main/docs/instances.md",
+                "original_name" => "Medium",
+                "original_url" => "medium.com"
+            ),
+            "gothub" => array(
+                "instance_url" => "${APP_GOTHUB}",
+                "project_url" => "https://codeberg.org/gothub/gothub/wiki/Instances",
+                "original_name" => "GitHub",
+                "original_url" => "github.com"
+            ),
+            "librarian" => array(
+                "instance_url" => "${APP_LIBRARIAN}",
+                "project_url" => "https://codeberg.org/librarian/librarian#clearnet",
+                "original_name" => "Odysee",
+                "original_url" => "odysee.com"
+            ),
 
+            "nitter" => array(
+                "instance_url" => "${APP_NITTER}",
+                "project_url" => "https://github.com/zedeus/nitter/wiki/Instances",
+                "original_name" => "Twitter",
+                "original_url" => "twitter.com"
+            ),
+
+            "libreddit" => array(
+                "instance_url" => "${APP_LIBREREDDIT}",
+                "project_url" => "https://github.com/spikecodes/libreddit",
+                "original_name" => "Reddit",
+                "original_url" => "reddit.com"
+            ),
+            "proxitok" => array(
+                "instance_url" => "${APP_PROXITOK}",
+                "project_url" => "https://github.com/pablouser1/ProxiTok/wiki/Public-instances",
+                "original_name" => "TikTok",
+                "original_url" => "tiktok.com"
+            ),
+            "wikiless" => array(
+                "instance_url" => "${APP_WIKILESS}",
+                "project_url" => "https://github.com/Metastem/wikiless#instances",
+                "original_name" => "Wikipedia",
+                "original_url" => "wikipedia.com"
+            ),
+            "quetre" => array(
+                "instance_url" => "${APP_QUETRE}",
+                "project_url" => "https://github.com/zyachel/quetre",
+                "original_name" => "Quora",
+                "original_url" => "quora.com"
+            ),
+            "libremdb" => array(
+                "instance_url" => "${APP_LIBREMDB}",
+                "project_url" => "https://github.com/zyachel/libremdb",
+                "original_name" => "IMDb",
+                "original_url" => "imdb.com"
+            ),
+            "breezewiki" => array(
+                "instance_url" => "${APP_BREEZEWIKI}",
+                "project_url" => "https://gitdab.com/cadence/breezewiki",
+                "original_name" => "Fandom",
+                "original_url" => "fandom.com"
+            ),
+            "anonymousoverflow" => array(
+                "instance_url" => "${APP_ANONYMOUS_OVERFLOW}",
+                "project_url" => "https://github.com/httpjamesm/AnonymousOverflow#clearnet-instances",
+                "original_name" => "StackOverflow",
+                "original_url" => "stackoverflow.com"
+            )
+        ),
         "curl_settings" => array(
             CURLOPT_PROXY => "",
             CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
diff --git a/docker/php/php.dockerfile b/docker/php/php.dockerfile
index 403918a4..e28376fe 100644
--- a/docker/php/php.dockerfile
+++ b/docker/php/php.dockerfile
@@ -11,8 +11,10 @@ ENV OPEN_SEARCH_HOST="http://127.0.0.1:${NGINX_PORT}"
 
 # Replace the 'config.php' script, which contains the most common search engine configurations, with these environment setups
 # These environment setups can be found in 'config.php', and the default configurations can be useful for most use cases
+ENV CONFIG_GOOGLE_ALLOWED_DOMAINS=""
 ENV CONFIG_GOOGLE_DOMAIN="com"
-ENV CONFIG_GOOGLE_LANGUAGE="en"
+ENV CONFIG_GOOGLE_LANGUAGE_SITE="en"
+ENV CONFIG_GOOGLE_LANGUAGE_RESULTS=""
 ENV CONFIG_INVIDIOUS_INSTANCE="https://invidious.namazso.eu"
 ENV CONFIG_HIDDEN_SERVICE_SEARCH=false
 ENV CONFIG_DISABLE_BITTORRENT_SEARCH=false
diff --git a/settings.php b/settings.php
index 3e9e36fd..d8478ef6 100644
--- a/settings.php
+++ b/settings.php
@@ -1,6 +1,7 @@
 <?php
                 $config = require "config.php";
 
+                $ok = true;
                 if (isset($_REQUEST["reset"]))
                 {
                     if (isset($_SERVER["HTTP_COOKIE"]))
@@ -22,10 +23,21 @@
                         if (!empty($value))
                         {
                             setcookie($key, $value, time() + (86400 * 90), '/');
+                            $_COOKIE[$key] = $value;
                         }
                         else
                         {
                             setcookie($key, "", time() - 1000);
+                            unset($_COOKIE[$key]);
+                        }
+                    }
+                    if (isset($_COOKIE["google_domain"]))
+                    {
+                        if (!in_array($_COOKIE["google_domain"], $config->google_allowed_domains))
+                        {
+                            unset($_COOKIE["google_domain"]);
+                            setcookie("google_domain", null, -1, '/');
+                            $ok = false;
                         }
                     }
                 }
@@ -99,6 +111,13 @@
                 <h2>Google settings</h2>
                 <div class="settings-textbox-container">
                     <div>
+                    <div>
+                        <span>Google Domain</span>
+                        <?php
+                            echo "<input type=\"text\" name=\"google_domain\" placeholder=\"E.g.: co.uk\" value=\"";
+                            echo isset($_COOKIE["google_domain"]) ? htmlspecialchars($_COOKIE["google_domain"]) : $config->google_domain;
+                        ?>">
+                        </div>
                         <span>Site language</span>
                         <?php
                             echo "<input type=\"text\" name=\"google_language_site\" placeholder=\"E.g.: en\" value=\"";