MetaGerSearch.php 5.13 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
89
        }

        # Wikipedia Quicktip
        $quicktips = [];
90
91
92
93
94
95
        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";
        }

96
        $decodedResponse = json_decode($this->get($url), true);
97
98
99
100
101
        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];
102
            $quicktip['gefVon'] = "aus <a href=\"https://de.wikipedia.org\" target=\"_blank\" rel=\"noopener\">Wikipedia, der freien Enzyklopädie</a>";
103
104

            $quicktips[] = $quicktip;
105
        }
106

107
108
        $mquicktips = array_merge($mquicktips, $quicktips);

109
110
111
112
113
        # 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)];
114

115
            $mquicktips[] = ['title' => 'Wussten Sie schon?', 'descr' => $tip, 'URL' => '/tips'];
116
        }
117

118
        # Uns die Werbelinks:
119
        $file = storage_path() . "/app/public/ads.txt";
120
        if (file_exists($file)) {
121
            $ads = json_decode(file_get_contents($file), true);
122
            $ad  = $ads[array_rand($ads)];
123

124
125
            $mquicktips[] = ['title' => $ad['title'], 'descr' => $ad['descr'], 'URL' => $ad['URL']];
        }
Phil Höfer's avatar
Phil Höfer committed
126

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

130
131
132
133
        return view('quicktip')
            ->with('spruch', $spruch)
            ->with('mqs', $mquicktips);

134
135
    }

136
137
    public function tips()
    {
138
        $file = storage_path() . "/app/public/tips.txt";
139
        $tips = [];
140
141
        if (file_exists($file)) {
            $tips = file($file);
142
143
144
145
146
147
        }
        return view('tips')
            ->with('title', 'MetaGer - Tipps & Tricks')
            ->with('tips', $tips);
    }

148
149
    public function get($url)
    {
150
        return file_get_contents($url);
151
152
    }
}