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

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
8
9
#use App\MetaGer\Forwarder;
#use App\MetaGer\Results;
#use App\MetaGer\Search;
10
use App;
11
12
use App\MetaGer;

Dominik Hebeler's avatar
Dominik Hebeler committed
13
14
15

class MetaGerSearch extends Controller
{
16
    public function search(Request $request, MetaGer $metager)
17
    {
18
        #die($request->header('User-Agent'));
19
        $time = microtime();
20
21
        # Mit gelieferte Formulardaten parsen und abspeichern:
        $metager->parseFormData($request);
22
23
        #if($metager->getFokus() !== "bilder" )
        #{
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
34
        # Ergebnisse der Suchmaschinen kombinieren:
        $metager->combineResults();
35
36

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

41
    public function quicktips(Request $request)
42
    {
43
44
45
46
        $q = $request->input('q', '');

        # Zunächst den Spruch
        $spruecheFile = storage_path() . "/app/public/sprueche.txt";
47
        if( file_exists($spruecheFile) && $request->has('sprueche') )
48
49
50
        {
            $sprueche = file($spruecheFile);
            $spruch = $sprueche[array_rand($sprueche)];
51
52
53
        }else
        {
            $spruch = "";
54
        }
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        # Die manuellen Quicktips:
        $file = storage_path() . "/app/public/qtdata.csv";
        
        $mquicktips = [];
        if( file_exists($file) && $q !== '')
        {
            $file = fopen($file, 'r');
            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;
                        }
                }
                if($isIn === TRUE){
                        $quicktip = array('QT_Type' => "MQT");
                        $quicktip["URL"] = $line[0];
                        $quicktip["title"] = $line[1];
                        $quicktip["descr"] = $line[2];
                        $mquicktips[] = $quicktip;
                }
        }
        fclose($file);
        }

        # Wikipedia Quicktip
        $quicktips = [];
        $url = "http://de.wikipedia.org/w/api.php?action=query&titles=".urlencode(implode("_",array_diff(explode(" ",$q),array("wikipedia"))))."&prop=info|extracts|categories&inprop=url|displaytitle&exintro&exsentences=3&format=json";
        $decodedResponse = json_decode($this->get($url), true);
88
        if( isset($decodedResponse["query"]["pages"]) )
89
        {
90
            foreach($decodedResponse["query"]["pages"] as $result)
91
            {
92
93
94
95
96
97
98
                if( isset($result['displaytitle']) && isset($result['fullurl']) && isset($result['extract']) )
                {
                    $quicktip = [];
                    $quicktip["title"] = $result['displaytitle'];
                    $quicktip["URL"] = $result['fullurl'];
                    $quicktip["descr"] = strip_tags($result['extract']);
                    $quicktip['gefVon'] = "aus <a href=\"https://de.wikipedia.org\" target=\"_blank\">Wikipedia, der freien Enzyklopädie</a>";
99

100
101
                    $quicktips[] = $quicktip;
                }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
            }
        }
        $mquicktips = array_merge($mquicktips, $quicktips);

        # Uns 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)];

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

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

            $mquicktips[] = ['title' => $ad['title'], 'descr' => $ad['descr'], 'URL' => $ad['URL']];   
        }   
        return view('quicktip')
            ->with('spruch', $spruch)
            ->with('mqs', $mquicktips);

            
130
131
    }

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

    function get($url) {
146
        return file_get_contents($url);
147
148
    } 

Dominik Hebeler's avatar
Dominik Hebeler committed
149
}