From c635bd4ce23a721ee9d346a07fb060340539e303 Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Mon, 22 Jul 2024 11:27:58 +0200
Subject: [PATCH] link diretly to metager webextension in some cases

---
 .../app/Http/Controllers/TilesController.php  |  33 ++++-
 metager/app/Models/Tile.php                   |   4 +-
 metager/composer.json                         |   7 +-
 metager/composer.lock                         | 125 +++++++++++++++++-
 metager/resources/views/parts/tile.blade.php  |   3 +-
 5 files changed, 163 insertions(+), 9 deletions(-)

diff --git a/metager/app/Http/Controllers/TilesController.php b/metager/app/Http/Controllers/TilesController.php
index ce9e3e62f..0b94eebe4 100644
--- a/metager/app/Http/Controllers/TilesController.php
+++ b/metager/app/Http/Controllers/TilesController.php
@@ -4,19 +4,22 @@ namespace App\Http\Controllers;
 
 use App\Localization;
 use App\Models\Authorization\Authorization;
+use DeviceDetector\Cache\LaravelCache;
+use DeviceDetector\ClientHints;
+use DeviceDetector\DeviceDetector;
 use Exception;
 use Illuminate\Support\Facades\Redis;
 use App\Models\Tile;
 use App\SearchSettings;
 use Cache;
-use Illuminate\Http\Request;
 use Log;
+use Request;
 
 class TilesController extends Controller
 {
     const CACHE_DURATION_SECONDS = 300;
 
-    public function loadTakeTiles(Request $request)
+    public function loadTakeTiles(\Illuminate\Http\Request $request)
     {
         if (!$request->filled("ckey") || !Cache::has($request->input("ckey"))) {
             abort(404);
@@ -59,10 +62,34 @@ class TilesController extends Controller
     private static function STATIC_TILES(): array
     {
         $tiles = [];
+
+        $dd = new DeviceDetector(Request::header("user-agent"), ClientHints::factory($_SERVER));
+        $dd->setCache(new LaravelCache());
+        $dd->parse();
+        $plugin_url = route("plugin");
+        $browser = $dd->getClient("name");
+        $version = $dd->getClient("version");
+        $os = $dd->getOs("name");
+        $target = "__self";
+        $classes = "";
+        if (!$dd->isMobile() && $browser === "Firefox" && version_compare($version, "115.0", "ge")) {
+            $plugin_url = "https://addons.mozilla.org/firefox/downloads/latest/metager-suche";
+            $classes .= "orange";
+        } elseif (!$dd->isMobile() && $browser === "Chrome" && $os === "Windows") {
+            $plugin_url = "https://chromewebstore.google.com/detail/metager-suche/gjfllojpkdnjaiaokblkmjlebiagbphd";
+            $target = "__BLANK";
+            $classes .= "orange";
+        } elseif ($browser === "Microsoft Edge") {
+            $plugin_url = "https://microsoftedge.microsoft.com/addons/detail/fdckbcmhkcoohciclcedgjmchbdeijog";
+            $target = "__BLANK";
+            $classes .= "orange";
+        }
+        $tiles[] = new Tile(title: __('index.plugin'), image: "/img/svg-icons/plug-in.svg", url: $plugin_url, image_alt: "MetaGer Plugin Logo", classes: $classes, target: $target);
+
         $tiles[] = new Tile(title: "Unser Trägerverein", image: "/img/tiles/sumaev.png", url: "https://suma-ev.de", image_alt: "SUMA_EV Logo");
         //$tiles[] = new Tile(title: "Maps", image: "/img/tiles/maps.png", url: "https://maps.metager.de", image_alt: "MetaGer Maps Logo");
         $tiles[] = new Tile(title: __('sidebar.nav28'), image: "/img/icon-settings.svg", url: route("settings", ["focus" => app(SearchSettings::class)->fokus, "url" => url()->full()]), image_alt: "Settings Logo", image_classes: "invert-dm");
-        $tiles[] = new Tile(title: __('index.plugin'), image: "/img/svg-icons/plug-in.svg", url: route("plugin"), image_alt: "MetaGer Plugin Logo", classes: "orange");
+
         return $tiles;
     }
 
diff --git a/metager/app/Models/Tile.php b/metager/app/Models/Tile.php
index 54eb28758..3c006ff54 100644
--- a/metager/app/Models/Tile.php
+++ b/metager/app/Models/Tile.php
@@ -15,6 +15,7 @@ class Tile implements \JsonSerializable
     public string $image;
     public string $image_alt;
     public string $url;
+    public string $target;
     public string $classes = "";
     public string $image_classes = "";
     public bool $advertisement = false;
@@ -27,7 +28,7 @@ class Tile implements \JsonSerializable
      * @param string $classes Additional css classes to append for the tile
      * @param string $image_classes Additional css classes to append to the image of the tile
      */
-    public function __construct(string $title, string $image, string $image_alt, string $url, string $classes = "", string $image_classes = "", bool $advertisement = false)
+    public function __construct(string $title, string $image, string $image_alt, string $url, string $target = "_SELF", string $classes = "", string $image_classes = "", bool $advertisement = false)
     {
         $this->title = $title;
         try {
@@ -40,6 +41,7 @@ class Tile implements \JsonSerializable
         $this->image = $image;
         $this->image_alt = $image_alt;
         $this->url = $url;
+        $this->target = $target;
         $this->classes = $classes;
         $this->image_classes = $image_classes;
         $this->advertisement = $advertisement;
diff --git a/metager/composer.json b/metager/composer.json
index f010b8832..1bf939e8c 100644
--- a/metager/composer.json
+++ b/metager/composer.json
@@ -9,12 +9,13 @@
     "license": "MIT",
     "require": {
         "php": "^8.2",
-        "laravel/framework": "^11.0",
-        "laravel/tinker": "^2.9",
         "endroid/qr-code": "^5.0",
         "globalcitizen/php-iban": "^4.2.3",
         "jenssegers/agent": "^2.6.4",
+        "laravel/framework": "^11.0",
         "laravel/sanctum": "^4.0",
+        "laravel/tinker": "^2.9",
+        "matomo/device-detector": "^6.3",
         "mcamara/laravel-localization": "2.0.1",
         "mews/captcha": "^3.3.3",
         "mlocati/ip-lib": "^1.18",
@@ -79,4 +80,4 @@
     },
     "minimum-stability": "stable",
     "prefer-stable": true
-}
\ No newline at end of file
+}
diff --git a/metager/composer.lock b/metager/composer.lock
index d04d99401..2ee4ec69f 100644
--- a/metager/composer.lock
+++ b/metager/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "4a254e2a694e41945232688fc0b50468",
+    "content-hash": "791aa52ccb009d4044584609ff1b78d9",
     "packages": [
         {
             "name": "bacon/bacon-qr-code",
@@ -2326,6 +2326,75 @@
             ],
             "time": "2024-01-28T23:22:08+00:00"
         },
+        {
+            "name": "matomo/device-detector",
+            "version": "6.3.2",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/matomo-org/device-detector.git",
+                "reference": "fd4042cb6a7f3f985a81aedc075dd59e0b991a51"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/matomo-org/device-detector/zipball/fd4042cb6a7f3f985a81aedc075dd59e0b991a51",
+                "reference": "fd4042cb6a7f3f985a81aedc075dd59e0b991a51",
+                "shasum": ""
+            },
+            "require": {
+                "mustangostang/spyc": "*",
+                "php": "^7.2|^8.0"
+            },
+            "replace": {
+                "piwik/device-detector": "self.version"
+            },
+            "require-dev": {
+                "matthiasmullie/scrapbook": "^1.4.7",
+                "mayflower/mo4-coding-standard": "^v9.0.0",
+                "phpstan/phpstan": "^1.10.44",
+                "phpunit/phpunit": "^8.5.8",
+                "psr/cache": "^1.0.1",
+                "psr/simple-cache": "^1.0.1",
+                "symfony/yaml": "^5.1.7"
+            },
+            "suggest": {
+                "doctrine/cache": "Can directly be used for caching purpose",
+                "ext-yaml": "Necessary for using the Pecl YAML parser"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "DeviceDetector\\": ""
+                },
+                "exclude-from-classmap": [
+                    "Tests/"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "LGPL-3.0-or-later"
+            ],
+            "authors": [
+                {
+                    "name": "The Matomo Team",
+                    "email": "hello@matomo.org",
+                    "homepage": "https://matomo.org/team/"
+                }
+            ],
+            "description": "The Universal Device Detection library, that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, media players, mobile apps, feed readers, libraries, etc), operating systems, devices, brands and models.",
+            "homepage": "https://matomo.org",
+            "keywords": [
+                "devicedetection",
+                "parser",
+                "useragent"
+            ],
+            "support": {
+                "forum": "https://forum.matomo.org/",
+                "issues": "https://github.com/matomo-org/device-detector/issues",
+                "source": "https://github.com/matomo-org/matomo",
+                "wiki": "https://dev.matomo.org/"
+            },
+            "time": "2024-05-28T10:16:19+00:00"
+        },
         {
             "name": "mcamara/laravel-localization",
             "version": "v2.0.1",
@@ -2709,6 +2778,60 @@
             ],
             "time": "2024-04-12T21:02:21+00:00"
         },
+        {
+            "name": "mustangostang/spyc",
+            "version": "0.6.3",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/mustangostang/spyc.git",
+                "reference": "4627c838b16550b666d15aeae1e5289dd5b77da0"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/mustangostang/spyc/zipball/4627c838b16550b666d15aeae1e5289dd5b77da0",
+                "reference": "4627c838b16550b666d15aeae1e5289dd5b77da0",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.1"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "4.3.*@dev"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "0.5.x-dev"
+                }
+            },
+            "autoload": {
+                "files": [
+                    "Spyc.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "mustangostang",
+                    "email": "vlad.andersen@gmail.com"
+                }
+            ],
+            "description": "A simple YAML loader/dumper class for PHP",
+            "homepage": "https://github.com/mustangostang/spyc/",
+            "keywords": [
+                "spyc",
+                "yaml",
+                "yml"
+            ],
+            "support": {
+                "issues": "https://github.com/mustangostang/spyc/issues",
+                "source": "https://github.com/mustangostang/spyc/tree/0.6.3"
+            },
+            "time": "2019-09-10T13:16:29+00:00"
+        },
         {
             "name": "nesbot/carbon",
             "version": "3.3.1",
diff --git a/metager/resources/views/parts/tile.blade.php b/metager/resources/views/parts/tile.blade.php
index ebe3a635c..b588175ff 100644
--- a/metager/resources/views/parts/tile.blade.php
+++ b/metager/resources/views/parts/tile.blade.php
@@ -1,4 +1,5 @@
-<a href="{{$tile->url}}" class="{{ $tile->advertisement ? "advertisement " : "" }}{{ $tile->classes }}" {{ $tile->advertisement ? 'rel=nofollow target=_blank' : ''}}>
+<a href="{{$tile->url}}" target="{{ $tile->target }}"
+    class="{{ $tile->advertisement ? "advertisement " : "" }}{{ $tile->classes }}" {{ $tile->advertisement ? 'rel=nofollow target=_blank' : ''}}>
     <div class="image">
         <img src="{{$tile->image}}" alt="{{$tile->image_alt}}" class="{{$tile->image_classes}}">
     </div>
-- 
GitLab