MetaGerSearch.php 7.33 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1
2
3
4
<?php

namespace App\Http\Controllers;

5
use App;
6
use App\MetaGer;
7
use Illuminate\Http\Request;
Phil Höfer's avatar
Phil Höfer committed
8
use LaravelLocalization;
Dominik Hebeler's avatar
Dominik Hebeler committed
9
10
11

class MetaGerSearch extends Controller
{
12
    public function search(Request $request, MetaGer $metager)
13
    {
14
        #die($request->header('User-Agent'));
15
        $time = microtime();
16
17
        # Mit gelieferte Formulardaten parsen und abspeichern:
        $metager->parseFormData($request);
18
19
20
21
22
23

        # Ein Schutz gegen bestimmte Bot-Angriffe, die uns passiert sind.
        if ($metager->doBotProtection($request->input('bot', ""))) {
            return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), url("/noaccess", ['redirect' => base64_encode(url()->full())])));
        }

24
25
        # Nach Spezialsuchen überprüfen:
        $metager->checkSpecialSearches($request);
26

27
28
        # Alle Suchmaschinen erstellen
        $metager->createSearchEngines($request);
29

30
31
32
        # Alle Ergebnisse vor der Zusammenführung ranken:
        $metager->rankAll();

33
        # Ergebnisse der Suchmaschinen kombinieren:
Phil Höfer's avatar
Phil Höfer committed
34
        $metager->prepareResults();
35

36
37
        # Die Ausgabe erstellen:
        return $metager->createView();
Dominik Hebeler's avatar
Dominik Hebeler committed
38
39
    }

40
41
42
43
44
45
46
47
    public function botProtection($redirect)
    {
        $hash = md5(date('YmdHi'));
        return view('botProtection')
            ->with('hash', $hash)
            ->with('r', $redirect);
    }

48
    public function quicktips(Request $request)
49
    {
50
51
52
53
        $q = $request->input('q', '');

        # Zunächst den Spruch
        $spruecheFile = storage_path() . "/app/public/sprueche.txt";
54
        if (file_exists($spruecheFile) && $request->has('sprueche')) {
55
            $sprueche = file($spruecheFile);
56
57
            $spruch   = $sprueche[array_rand($sprueche)];
        } else {
58
            $spruch = "";
59
        }
60

61
62
        # Die manuellen Quicktips:
        $file = storage_path() . "/app/public/qtdata.csv";
63

64
        $mquicktips = [];
65
        if (file_exists($file) && $q !== '') {
66
            $file = fopen($file, 'r');
67
68
69
70
71
72
73
74
75
            while (($line = fgetcsv($file)) !== false) {
                $words = array_slice($line, 3);
                $isIn  = false;
                foreach ($words as $word) {
                    $word = strtolower($word);
                    if (strpos($q, $word) !== false) {
                        $isIn = true;
                        break;
                    }
76
                }
77
78
79
80
81
82
                if ($isIn === true) {
                    $quicktip          = array('QT_Type' => "MQT");
                    $quicktip["URL"]   = $line[0];
                    $quicktip["title"] = $line[1];
                    $quicktip["descr"] = $line[2];
                    $mquicktips[]      = $quicktip;
83
                }
84
85
            }
            fclose($file);
86
87
88
        }

        $quicktips = [];
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

        # Das Wetter
        try {
            if (App::isLocale('en')) {
                $url = "http://api.openweathermap.org/data/2.5/weather?type=like&units=metric&lang=en&q=" . urlencode($q) . "&APPID=" . getenv("openweathermap");
            } else {
                $url = "http://api.openweathermap.org/data/2.5/weather?type=like&units=metric&lang=de&q=" . urlencode($q) . "&APPID=" . getenv("openweathermap");
            }

            $result           = json_decode($this->get($url), true);
            $weather          = [];
            $weather["title"] = "Wetter in " . $result["name"];
            $weather["URL"]   = "http://openweathermap.org/city/" . $result["id"];

            $descr   = '<b class="detail-short">' . $result["main"]["temp"] . " °C, " . $result["weather"][0]["description"] . "</b>";
            $details = '<table  class="table table-condensed"><tr><td>Temperatur</td><td>' . $result["main"]["temp_min"] . " bis " . $result["main"]["temp_max"] . " °C</td></tr>";
            $details .= "<tr><td>Druck</td><td>" . $result["main"]["pressure"] . " hPa</td></tr>";
            $details .= "<tr><td>Luftfeuchtigkeit</td><td>" . $result["main"]["humidity"] . " %</td></tr>";
            $details .= "<tr><td>Wind</td><td>" . $result["wind"]["speed"] . " m/s, " . $result["wind"]["deg"] . "°</td></tr>";
            $details .= "<tr><td>Bewölkung</td><td>" . $result["clouds"]["all"] . " %</td></tr>";
            if (isset($result->rain)) {
                $details .= " | Regen letzte drei Stunden: " . $result["rain"]["3h"] . " h";
            }
            $details .= "</table>";
            $weather["descr"]     = $descr;
            $weather["details"]   = $details;
            $weather["gefVon"]    = "von <a href = \"https://openweathermap.org\" target=\"_blank\" rel=\"noopener\">Openweathermap</a>";
            $requestData          = [];
            $requestData["url"]   = "http://openweathermap.org/img/w/";
            $weather["image"]     = action('Pictureproxy@get', $requestData) . $result["weather"][0]["icon"] . ".png";
            $weather["image-alt"] = $result["weather"][0]["main"];
            $mquicktips[]         = $weather;
        } catch (\ErrorException $e) {

        }

        # Wikipedia Quicktip
126
127
128
129
130
131
        if (App::isLocale('en')) {
            $url = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" . urlencode($q) . "&limit=1&namespace=0&format=json";
        } else {
            $url = "https://de.wikipedia.org/w/api.php?action=opensearch&search=" . urlencode($q) . "&limit=1&namespace=0&format=json";
        }

132
        $decodedResponse = json_decode($this->get($url), true);
133
134
135
136
137
        if (isset($decodedResponse[1][0]) && isset($decodedResponse[2][0]) && isset($decodedResponse[3][0])) {
            $quicktip           = [];
            $quicktip["title"]  = $decodedResponse[1][0];
            $quicktip["URL"]    = $decodedResponse[3][0];
            $quicktip["descr"]  = $decodedResponse[2][0];
138
            $quicktip['gefVon'] = "aus <a href=\"https://de.wikipedia.org\" target=\"_blank\" rel=\"noopener\">Wikipedia, der freien Enzyklopädie</a>";
139
140

            $quicktips[] = $quicktip;
141
        }
142

143
144
        $mquicktips = array_merge($mquicktips, $quicktips);

145
146
147
148
149
        # Und Natürlich das wussten Sie schon:
        $file = storage_path() . "/app/public/tips.txt";
        if (file_exists($file)) {
            $tips = file($file);
            $tip  = $tips[array_rand($tips)];
150

151
            $mquicktips[] = ['title' => 'Wussten Sie schon?', 'descr' => $tip, 'URL' => '/tips'];
152
        }
153

154
        # Uns die Werbelinks:
155
        $file = storage_path() . "/app/public/ads.txt";
156
        if (file_exists($file)) {
157
            $ads = json_decode(file_get_contents($file), true);
158
            $ad  = $ads[array_rand($ads)];
159

160
161
            $mquicktips[] = ['title' => $ad['title'], 'descr' => $ad['descr'], 'URL' => $ad['URL']];
        }
Phil Höfer's avatar
Phil Höfer committed
162

163
        # Und en Spendenaufruf:
Phil Höfer's avatar
Phil Höfer committed
164
165
        $mquicktips[] = ['title' => trans('quicktip.spende.title'), 'descr' => trans('quicktip.spende.descr'), 'URL' => LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), "spendenaufruf")];

166
167
168
169
        return view('quicktip')
            ->with('spruch', $spruch)
            ->with('mqs', $mquicktips);

170
171
    }

172
173
    public function tips()
    {
174
        $file = storage_path() . "/app/public/tips.txt";
175
        $tips = [];
176
177
        if (file_exists($file)) {
            $tips = file($file);
178
179
180
181
182
183
        }
        return view('tips')
            ->with('title', 'MetaGer - Tipps & Tricks')
            ->with('tips', $tips);
    }

184
185
    public function get($url)
    {
186
        return file_get_contents($url);
187
188
    }
}