MetaGerSearch.php 4.68 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
        #if($metager->getFokus() !== "bilder" )
        #{
20
21
        # Nach Spezialsuchen überprüfen:
        $metager->checkSpecialSearches($request);
22
        #}
23
24
        # Alle Suchmaschinen erstellen
        $metager->createSearchEngines($request);
25

26
27
28
        # Alle Ergebnisse vor der Zusammenführung ranken:
        $metager->rankAll();

29
        # Ergebnisse der Suchmaschinen kombinieren:
Phil Höfer's avatar
Phil Höfer committed
30
        $metager->prepareResults();
31

32
33
        # Die Ausgabe erstellen:
        return $metager->createView();
Dominik Hebeler's avatar
Dominik Hebeler committed
34
35
    }

36
    public function quicktips(Request $request)
37
    {
38
39
40
41
        $q = $request->input('q', '');

        # Zunächst den Spruch
        $spruecheFile = storage_path() . "/app/public/sprueche.txt";
42
        if (file_exists($spruecheFile) && $request->has('sprueche')) {
43
            $sprueche = file($spruecheFile);
44
45
            $spruch   = $sprueche[array_rand($sprueche)];
        } else {
46
            $spruch = "";
47
        }
48

49
50
        # Die manuellen Quicktips:
        $file = storage_path() . "/app/public/qtdata.csv";
51

52
        $mquicktips = [];
53
        if (file_exists($file) && $q !== '') {
54
            $file = fopen($file, 'r');
55
56
57
58
59
60
61
62
63
            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;
                    }
64
                }
65
66
67
68
69
70
                if ($isIn === true) {
                    $quicktip          = array('QT_Type' => "MQT");
                    $quicktip["URL"]   = $line[0];
                    $quicktip["title"] = $line[1];
                    $quicktip["descr"] = $line[2];
                    $mquicktips[]      = $quicktip;
71
                }
72
73
            }
            fclose($file);
74
75
76
77
        }

        # Wikipedia Quicktip
        $quicktips = [];
78
79
80
81
82
83
        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";
        }

84
        $decodedResponse = json_decode($this->get($url), true);
85
86
87
88
89
        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];
90
            $quicktip['gefVon'] = "aus <a href=\"https://de.wikipedia.org\" target=\"_blank\" rel=\"noopener\">Wikipedia, der freien Enzyklopädie</a>";
91
92

            $quicktips[] = $quicktip;
93
        }
94

95
96
        $mquicktips = array_merge($mquicktips, $quicktips);

97
98
99
100
101
        # 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)];
102

103
            $mquicktips[] = ['title' => 'Wussten Sie schon?', 'descr' => $tip, 'URL' => '/tips'];
104
        }
105

106
        # Uns die Werbelinks:
107
        $file = storage_path() . "/app/public/ads.txt";
108
        if (file_exists($file)) {
109
            $ads = json_decode(file_get_contents($file), true);
110
            $ad  = $ads[array_rand($ads)];
111

112
113
            $mquicktips[] = ['title' => $ad['title'], 'descr' => $ad['descr'], 'URL' => $ad['URL']];
        }
Phil Höfer's avatar
Phil Höfer committed
114

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

118
119
120
121
        return view('quicktip')
            ->with('spruch', $spruch)
            ->with('mqs', $mquicktips);

122
123
    }

124
125
    public function tips()
    {
126
        $file = storage_path() . "/app/public/tips.txt";
127
        $tips = [];
128
129
        if (file_exists($file)) {
            $tips = file($file);
130
131
132
133
134
135
        }
        return view('tips')
            ->with('title', 'MetaGer - Tipps & Tricks')
            ->with('tips', $tips);
    }

136
137
    public function get($url)
    {
138
        return file_get_contents($url);
139
140
    }
}