Browse Source

added disable special query option, setttings can now be set via query params

hnhx 3 years ago
parent
commit
9e0709d77d
5 changed files with 109 additions and 61 deletions
  1. 5 3
      engines/google/text.php
  2. 2 2
      engines/google/video.php
  3. 9 1
      misc/header.php
  4. 26 11
      misc/tools.php
  5. 67 44
      settings.php

+ 5 - 3
engines/google/text.php

@@ -1,6 +1,9 @@
 <?php
      function check_for_special_search($query)
      {
+        if (isset($_COOKIE["disable_special"]) || isset($_REQUEST["disable_special"]))
+            return 0;
+
          $query_lower = strtolower($query);
          $split_query = explode(" ", $query);
 
@@ -105,10 +108,9 @@
                         continue;
             }
 
-
             $url = $url->textContent;
-            if (substr_count($_SERVER["HTTP_COOKIE"], " ") >= 1)
-              $url = check_for_privacy_friendly_alternative($url);
+
+            $url = privacy_friendly_alternative($url);
 
             $title = $xpath->evaluate(".//h3", $result)[0];
             $description = $xpath->evaluate(".//div[contains(@class, 'VwiC3b')]", $result)[0];

+ 2 - 2
engines/google/video.php

@@ -21,8 +21,8 @@
                     continue;
 
             $url = $url->textContent;
-            if (substr_count($_SERVER["HTTP_COOKIE"], " ") >= 1)
-              $url = check_for_privacy_friendly_alternative($url);
+            
+            $url = privacy_friendly_alternative($url);
 
             $title = $xpath->evaluate(".//h3", $result)[0];
 

+ 9 - 1
misc/header.php

@@ -9,4 +9,12 @@
         <link title="LibreX search" type="application/opensearchdescription+xml" href="/opensearch.xml?method=POST" rel="search"/>
         <link rel="shortcut icon" href="static/images/librex.png" />
 
-        <link rel="stylesheet" type="text/css" href="<?php echo "static/css/" . (isset($_COOKIE["theme"]) ? $_COOKIE["theme"] . ".css" : "dark.css"); ?>"/>
+        <link rel="stylesheet" type="text/css" href="
+        <?php
+                echo "static/css/";
+                if (isset($_COOKIE["theme"]) || isset($_REQUEST["theme"]))
+                    echo (isset($_COOKIE["theme"]) ? $_COOKIE["theme"] : $_REQUEST["theme"]) . ".css";
+                else
+                echo "dark.css";
+        ?>
+        "/>

+ 26 - 11
misc/tools.php

@@ -6,21 +6,36 @@
         return $base_url;
     }
 
-    function check_for_privacy_friendly_alternative($url)
+    function check_for_privacy_friendly_alternative($url, $frontend, $tobereplaced)
     {
-        if (isset($_COOKIE["invidious"]) && strpos($url, "youtube.com"))
-            $url = $_COOKIE["invidious"] . explode("youtube.com", $url)[1];
-        else if (isset($_COOKIE["bibliogram"]) && strpos($url, "instagram.com"))
+        if (isset($_COOKIE[$frontend]) || isset($_REQUEST[$frontend]))
         {
-            if (!strpos($url, "/p/"))
-                $_COOKIE["bibliogram"] .= "/u";
+            $frontend = isset($_COOKIE[$frontend]) ? $_COOKIE[$frontend] : $_REQUEST[$frontend];
 
-            $url = $_COOKIE["bibliogram"] . explode("instagram.com", $url)[1];
+            if ($tobereplaced == "instagram.com") 
+            {
+                if (!strpos($url, "/p/"))
+                    $frontend .= "/u";
+            }
+           
+            $url =  $frontend . explode($tobereplaced, $url)[1];
+
+            return $url;
         }
-        else if (isset($_COOKIE["nitter"]) && strpos($url, "twitter.com"))
-            $url = $_COOKIE["nitter"] . explode("twitter.com", $url)[1];
-        else if (isset($_COOKIE["libreddit"]) && strpos($url, "reddit.com"))
-            $url = $_COOKIE["libreddit"] . explode("reddit.com", $url)[1];
+
+        return $url;
+    }
+
+    function privacy_friendly_alternative($url)
+    {
+       if (strpos($url, "youtube.com"))
+            $url = check_for_privacy_friendly_alternative($url, "invidious", "youtube.com");
+        else if (strpos($url, "instagram.com"))
+            $url = check_for_privacy_friendly_alternative($url, "bibliogram", "instagram.com");
+        else if (strpos($url, "twitter.com"))
+            $url = check_for_privacy_friendly_alternative($url, "nitter", "twitter.com");
+        else if (strpos($url, "reddit.com"))
+            $url = check_for_privacy_friendly_alternative($url, "libreddit", "reddit.com");
 
         return $url;
     }

+ 67 - 44
settings.php

@@ -1,4 +1,44 @@
-<?php require "misc/header.php"; ?>
+
+        <?php
+                require "misc/header.php";
+
+                function better_setcookie($name)
+                {
+                    if (!empty($_REQUEST[$name]))
+                        setcookie($name, $_REQUEST[$name], time() + (86400 * 90));
+                    else if (isset($_COOKIE[$name]))
+                        setcookie($name, "", time() - 1000); 
+                }
+
+                if (isset($_REQUEST["save"]))
+                {
+                    better_setcookie("theme");
+
+                    better_setcookie("disable_special");
+
+                    better_setcookie("invidious");
+                    better_setcookie("bibliogram");
+                    better_setcookie("nitter");
+                    better_setcookie("libreddit");
+                    
+                    header("Location: /settings.php");
+                    die();
+                }
+                else if (isset($_REQUEST["reset"]))
+                {
+                    if (isset($_SERVER["HTTP_COOKIE"])) {
+                        $cookies = explode(";", $_SERVER["HTTP_COOKIE"]);
+                        foreach($cookies as $cookie) {
+                            $parts = explode("=", $cookie);
+                            $name = trim($parts[0]);
+                            setcookie($name, "", time() - 1000);
+                        }
+
+                        header("Location: /settings.php");
+                        die();
+                    }
+                }
+            ?>
 
     <title>LibreX - Settings</title>
     </head>
@@ -10,7 +50,6 @@
                 <label for="theme">Theme:</label>
                 <select name="theme">
                 <?php
-
                     $themes = "<option value=\"dark\">Dark</option>
                     <option value=\"light\">Light</option>
                     <option value=\"auto\">Auto</option>
@@ -27,35 +66,41 @@
                     echo $themes;
                 ?>
                 </select>
-              </div>
+                </div>
+                <div>
+                    <label for="special">Disable special queries (e.g.: currency conversion)</label>
+                    <input type="checkbox" name="disable_special"
+                    <?php echo isset($_COOKIE["disable_special"]) ? "checked"  : "\"\""; ?>
+                    >
+                </div>
                 <h2>Privacy friendly frontends</h2>
-                <p>For an example if you want to view YouTube without getting spied on, click on "Invidious", find the instance that is most suitable for you then paste it in (correct format: https://url.domain)</p>
+                <p>For an example if you want to view YouTube without getting spied on, click on "Invidious", find the instance that is most suitable for you then paste it in (correct format: https://example.com)</p>
                 <div class="instances-container">
 
                       <div>
                         <a for="invidious" href="https://docs.invidious.io/Invidious-Instances/" target="_blank">Invidious</a>
-                        <input type="text" name="invidious" placeholder="Replace YouTube" value=
+                        <input pattern="https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)" type="text" name="invidious" placeholder="Replace YouTube" value=
                             <?php echo isset($_COOKIE["invidious"]) ? $_COOKIE["invidious"]  : "\"\""; ?>
                         >
                       </div>
 
                       <div>
                         <a for="bibliogram" href="https://git.sr.ht/~cadence/bibliogram-docs/tree/master/docs/Instances.md" target="_blank">Bibliogram</a>
-                        <input type="text" name="bibliogram" placeholder="Replace Instagram" value=
+                        <input pattern="https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)" type="text" name="bibliogram" placeholder="Replace Instagram" value=
                             <?php echo isset($_COOKIE["bibliogram"]) ? $_COOKIE["bibliogram"]  : "\"\""; ?>
                         >
                       </div>
 
                       <div>
                         <a for="nitter" href="https://github.com/zedeus/nitter/wiki/Instances" target="_blank">Nitter</a>
-                        <input type="text" name="nitter" placeholder="Replace Twitter" value=
+                        <input pattern="https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)" type="text" name="nitter" placeholder="Replace Twitter" value=
                             <?php echo isset($_COOKIE["nitter"]) ? $_COOKIE["nitter"]  : "\"\""; ?>
                         >
                       </div>
 
                       <div>
                         <a for="libreddit" href="https://github.com/spikecodes/libreddit" target="_blank">Libreddit</a>
-                        <input type="text" name="libreddit" placeholder="Replace Reddit" value=
+                        <input pattern="https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)" type="text" name="libreddit" placeholder="Replace Reddit" value=
                             <?php echo isset($_COOKIE["libreddit"]) ? $_COOKIE["libreddit"]  : "\"\""; ?>
                         >
                       </div>
@@ -64,46 +109,24 @@
                   <button type="submit" name="save" value="1">Save</button>
                   <button type="submit" name="reset" value="1">Reset</button>
                 </div>
-            </form>
-
-
-            <?php
-                if (isset($_REQUEST["save"]))
-                {
-                    if (!empty($_REQUEST["invidious"]))
-                        setcookie("invidious", $_REQUEST["invidious"], time() + (86400 * 90));
-
-                    if (!empty($_REQUEST["bibliogram"]))
-                        setcookie("bibliogram", $_REQUEST["bibliogram"], time() + (86400 * 90));
-
-                    if (!empty($_REQUEST["nitter"]))
-                        setcookie("nitter", $_REQUEST["nitter"], time() + (86400 * 90));
-
-                    if (!empty($_REQUEST["libreddit"]))
-                        setcookie("libreddit", $_REQUEST["libreddit"], time() + (86400 * 90));
+                <div>
+                    <?php 
+                        if (!empty($_COOKIE))
+                        {
+                            echo "<p>If you use the Tor browser or just regularly delete cookies you can also set the settings as a query param:</p>";
 
+                            $url = "?";
 
-                    setcookie("theme", $_REQUEST["theme"], time() + (86400 * 90));
+                            foreach ($_COOKIE as $key => $value)
+                                $url .= "&$key=$value";
+                            
+                            $url = substr_replace($url, "", 1, 1);
 
-                    header("Location: /settings.php");
-                    die();
-                }
-                else if (isset($_REQUEST["reset"]))
-                {
-                    if (isset($_SERVER["HTTP_COOKIE"])) {
-                        $cookies = explode(";", $_SERVER["HTTP_COOKIE"]);
-                        foreach($cookies as $cookie) {
-                            $parts = explode("=", $cookie);
-                            $name = trim($parts[0]);
-                            setcookie($name, "", time()-1000);
-                            setcookie($name, "", time()-1000, "/");
+                            echo $url;
                         }
-
-                        header("Location: /settings.php");
-                        die();
-                    }
-                }
-            ?>
+                    ?>
+                </div>
+            </form>
         </div>
 
 <?php require "misc/footer.php"; ?>