MetaGer.php 34.7 KB
Newer Older
1
2
3
4
<?php
namespace App;

use App;
5
use App\lib\TextLanguageDetect\TextLanguageDetect;
6
7
use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
Dominik Hebeler's avatar
Bugfix    
Dominik Hebeler committed
8
use LaravelLocalization;
9
10
use Log;
use Redis;
11
12
13

class MetaGer
{
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    # Einstellungen für die Suche
    protected $fokus;
    protected $eingabe;
    protected $q;
    protected $category;
    protected $time;
    protected $page;
    protected $lang;
    protected $cache = "";
    protected $site;
    protected $hostBlacklist   = [];
    protected $domainBlacklist = [];
    protected $stopWords       = [];
    protected $phrases         = [];
    protected $engines         = [];
    protected $results         = [];
    protected $ads             = [];
    protected $warnings        = [];
    protected $errors          = [];
    protected $addedHosts      = [];
    # Daten über die Abfrage
    protected $ip;
    protected $language;
    protected $agent;
    # Konfigurationseinstellungen:
    protected $sumaFile;
    protected $mobile;
    protected $resultCount;
    protected $sprueche;
43
    protected $domainsBlacklisted = [];
44
    protected $urlsBlacklisted    = [];
45
46
47
    protected $url;
    protected $languageDetect;

48
49
50
51
    public function __construct()
    {
        $this->starttime = microtime(true);
        if (file_exists(config_path() . "/blacklistDomains.txt") && file_exists(config_path() . "/blacklistUrl.txt")) {
52
            # Blacklists einlesen:
53
            $tmp                      = file_get_contents(config_path() . "/blacklistDomains.txt");
54
            $this->domainsBlacklisted = explode("\n", $tmp);
55
56
57
            $tmp                      = file_get_contents(config_path() . "/blacklistUrl.txt");
            $this->urlsBlacklisted    = explode("\n", $tmp);
        } else {
58
59
60
61
62
            Log::warning("Achtung: Eine, oder mehrere Blacklist Dateien, konnten nicht geöffnet werden");
        }

        $this->languageDetect = new TextLanguageDetect();
        $this->languageDetect->setNameMode("2");
63
    }
64

65
    public function getHashCode()
66
67
68
69
70
    {
        $string = url()->full();
        return md5($string);
    }

71
    public function rankAll()
72
    {
73
        foreach ($this->engines as $engine) {
74
75
76
            $engine->rank($this);
        }
    }
77

78
79
80
    public function createView()
    {
        $viewResults = [];
81

82
        # Wir extrahieren alle notwendigen Variablen und geben Sie an unseren View:
83
        foreach ($this->results as $result) {
84
85
86
87
88
89
            $viewResults[] = get_object_vars($result);
        }

        # Wir müssen natürlich noch den Log für die durchgeführte Suche schreiben:
        $this->createLogs();

90
91
        if ($this->fokus === "bilder") {
            switch ($this->out) {
92
93
94
95
96
97
98
99
                case 'results':
                    return view('metager3bilderresults')
                        ->with('results', $viewResults)
                        ->with('eingabe', $this->eingabe)
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
                        ->with('metager', $this)
100
                        ->with('browser', (new Agent())->browser());
101
102
103
104
105
106
107
108
                default:
                    return view('metager3bilder')
                        ->with('results', $viewResults)
                        ->with('eingabe', $this->eingabe)
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
                        ->with('metager', $this)
109
                        ->with('browser', (new Agent())->browser());
110
111
112
113
114
115
116
117
118
119
120
121
            }
        }

        switch ($this->out) {
            case 'results':
                return view('metager3results')
                    ->with('results', $viewResults)
                    ->with('eingabe', $this->eingabe)
                    ->with('mobile', $this->mobile)
                    ->with('warnings', $this->warnings)
                    ->with('errors', $this->errors)
                    ->with('metager', $this)
122
                    ->with('browser', (new Agent())->browser());
123
124
125
126
127
128
129
130
131
132
                break;
            case 'results-with-style':
                return view('metager3')
                    ->with('results', $viewResults)
                    ->with('eingabe', $this->eingabe)
                    ->with('mobile', $this->mobile)
                    ->with('warnings', $this->warnings)
                    ->with('errors', $this->errors)
                    ->with('metager', $this)
                    ->with('suspendheader', "yes")
133
                    ->with('browser', (new Agent())->browser());
134
135
136
137
138
139
140
141
                break;
            default:
                return view('metager3')
                    ->with('eingabe', $this->eingabe)
                    ->with('mobile', $this->mobile)
                    ->with('warnings', $this->warnings)
                    ->with('errors', $this->errors)
                    ->with('metager', $this)
142
                    ->with('browser', (new Agent())->browser());
143
144
                break;
        }
145
    }
146

147
148
149
150
151
152
    private function createLogs()
    {
        $redis = Redis::connection('redisLogs');
        try
        {
            $logEntry = "";
153
            $logEntry .= "[" . date(DATE_RFC822, mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"))) . "]";
154
155
156
157
158
159
            $logEntry .= " pid=" . getmypid();
            $logEntry .= " ref=" . $this->request->header('Referer');
            $useragent = $this->request->header('User-Agent');
            $useragent = str_replace("(", " ", $useragent);
            $useragent = str_replace(")", " ", $useragent);
            $useragent = str_replace(" ", "", $useragent);
160
            $logEntry .= " time=" . round((microtime(true) - $this->starttime), 2) . " serv=" . $this->fokus;
161
162
            $logEntry .= " search=" . $this->eingabe;
            $redis->rpush('logs.search', $logEntry);
163
        } catch (\Exception $e) {
164
165
166
167
            return;
        }
    }

168
    public function removeInvalids()
169
170
    {
        $results = [];
171
172
        foreach ($this->results as $result) {
            if ($result->isValid($this)) {
173
                $results[] = $result;
174
175
            }

176
177
178
        }
        #$this->results = $results;
    }
179

180
181
182
183
184
    public function combineResults()
    {
        foreach ($this->engines as $engine) {
            foreach ($engine->results as $result) {
                if ($result->valid) {
185
                    $this->results[] = $result;
186
187
                }

188
            }
189
            foreach ($engine->ads as $ad) {
190
191
                $this->ads[] = $ad;
            }
192
193
194
        }
        uasort($this->results, function ($a, $b) {
            if ($a->getRank() == $b->getRank()) {
195
                return 0;
196
197
            }

198
199
200
201
            return ($a->getRank() < $b->getRank()) ? 1 : -1;
        });
        # Validate Results
        $newResults = [];
202
203
        foreach ($this->results as $result) {
            if ($result->isValid($this)) {
204
                $newResults[] = $result;
205
206
            }

207
208
209
        }
        $this->results = $newResults;

210
        $counter   = 0;
211
        $firstRank = 0;
212
213
        foreach ($this->results as $result) {
            if ($counter === 0) {
214
                $firstRank = $result->rank;
215
216
            }

217
218
            $counter++;
            $result->number = $counter;
219
220
221
222
            $confidence     = 0;
            if ($firstRank > 0) {
                $confidence = $result->rank / $firstRank;
            } else {
223
                $confidence = 0;
224
225
226
            }

            if ($confidence > 0.65) {
227
                $result->color = "#FF4000";
228
            } elseif ($confidence > 0.4) {
229
                $result->color = "#FF0080";
230
            } elseif ($confidence > 0.2) {
231
                $result->color = "#C000C0";
232
            } else {
233
                $result->color = "#000000";
234
235
            }

236
237
        }

238
        if (LaravelLocalization::getCurrentLocale() === "en") {
239
240
241
            $this->ads = [];
        }

242
        $this->validated = false;
243
        if (isset($this->password)) {
244
245
246
            # Wir bieten einen bezahlten API-Zugriff an, bei dem dementsprechend die Werbung ausgeblendet wurde:
            # Aktuell ist es nur die Uni-Mainz. Deshalb überprüfen wir auch nur diese.
            $password = getenv('mainz');
247
            $eingabe  = $this->eingabe;
248
            $password = md5($eingabe . $password);
249
250
            if ($this->password === $password) {
                $this->ads       = [];
251
252
253
                $this->validated = true;
            }
        }
254
255

        if (count($this->results) <= 0) {
Dominik Hebeler's avatar
Dominik Hebeler committed
256
            $this->errors[] = "Leider konnten wir zu Ihrer Sucheingabe keine passenden Ergebnisse finden.";
257
        }
258

259
    }
260

261
262
    public function parseBoost($results)
    {
263
264
265
266
267
268
269
270
271
272
273
274
        foreach ($results as $result) {
            if (preg_match('/^(http[s]?\:\/\/)?(www.)?amazon\.de/', $result->anzeigeLink)) {
                if (preg_match('/\?/', $result->anzeigeLink)) {
                    $result->link .= '&tag=boostmg01-21';
                } else {
                    $result->link .= '?tag=boostmg01-21';
                }
                $result->partnershop = true;

            }
        }
        return $results;
275
276
277
    }
    public function parseAdgoal($results)
    {
278
        $publicKey  = getenv('adgoal_public');
279
        $privateKey = getenv('adgoal_private');
280
        if ($publicKey === false) {
281
282
283
            return $results;
        }
        $tldList = "";
284
285
        try {
            foreach ($results as $result) {
286
                $link = $result->anzeigeLink;
287
                if (strpos($link, "http") !== 0) {
288
289
290
291
292
293
294
295
296
297
                    $link = "http://" . $link;
                }
                $tldList .= parse_url($link, PHP_URL_HOST) . ",";
                $result->tld = parse_url($link, PHP_URL_HOST);
            }
            $tldList = rtrim($tldList, ",");

            # Hashwert
            $hash = md5("meta" . $publicKey . $tldList . "GER");

298
            # Query
299
300
            $query = urlencode($this->q);

301
            $link   = "https://api.smartredirect.de/api_v2/CheckForAffiliateUniversalsearchMetager.php?p=" . $publicKey . "&k=" . $hash . "&tld=" . $tldList . "&q=" . $query;
302
303
304
            $answer = json_decode(file_get_contents($link));

            # Nun müssen wir nur noch die Links für die Advertiser ändern:
305
            foreach ($answer as $el) {
306
                $hoster = $el[0];
307
                $hash   = $el[1];
308

309
310
                foreach ($results as $result) {
                    if ($hoster === $result->tld) {
311
312
                        # Hier ist ein Advertiser:
                        # Das Logo hinzufügen:
313
                        if ($result->image !== "") {
314
                            $result->logo = "https://img.smartredirect.de/logos_v2/60x30/" . $hash . ".gif";
315
                        } else {
316
                            $result->image = "https://img.smartredirect.de/logos_v2/120x60/" . $hash . ".gif";
317
318
                        }

319
320
321
                        # Den Link hinzufügen:
                        $publicKey = $publicKey;
                        $targetUrl = $result->anzeigeLink;
322
                        if (strpos($targetUrl, "http") !== 0) {
323
                            $targetUrl = "http://" . $targetUrl;
324
325
326
327
328
                        }

                        $gateHash            = md5($targetUrl . $privateKey);
                        $newLink             = "https://api.smartredirect.de/api_v2/ClickGate.php?p=" . $publicKey . "&k=" . $gateHash . "&url=" . urlencode($targetUrl) . "&q=" . $query;
                        $result->link        = $newLink;
329
330
331
332
                        $result->partnershop = true;
                    }
                }
            }
333
        } catch (\ErrorException $e) {
334
335
336
337
338
            return $results;
        }

        return $results;
    }
339

340
341
    public function createSearchEngines(Request $request)
    {
342

343
        if (!$request->has("eingabe")) {
344
            return;
345
        }
346

347
348
        # Überprüfe, welche Sumas eingeschaltet sind
        $xml                  = simplexml_load_file($this->sumaFile);
349
        $enabledSearchengines = [];
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
        $overtureEnabled      = false;
        $countSumas           = 0;
        $sumas                = $xml->xpath("suma");
        if ($this->fokus === "angepasst") {
            foreach ($sumas as $suma) {
                if ($request->has($suma["name"])
                    || ($this->fokus !== "bilder"
                        && ($suma["name"]->__toString() === "qualigo"
                            || $suma["name"]->__toString() === "similar_product_ads"
                            || (!$overtureEnabled && $suma["name"]->__toString() === "overtureAds")
                        )
                    )
                ) {

                    if (!(isset($suma['disabled']) && $suma['disabled']->__toString() === "1")) {
                        if ($suma["name"]->__toString() === "overture" || $suma["name"]->__toString() === "overtureAds") {
                            $overtureEnabled = true;
367
                        }
368
                        if ($suma["name"]->__toString() !== "qualigo" && $suma["name"]->__toString() !== "similar_product_ads" && $suma["name"]->__toString() !== "overtureAds") {
369
                            $countSumas += 1;
370
371
                        }

372
373
374
375
                        $enabledSearchengines[] = $suma;
                    }
                }
            }
376
377
378
379
380
381
382
383
384
385
386
387
388
389
        } else {
            foreach ($sumas as $suma) {
                $types = explode(",", $suma["type"]);
                if (in_array($this->fokus, $types)
                    || ($this->fokus !== "bilder"
                        && ($suma["name"]->__toString() === "qualigo"
                            || $suma["name"]->__toString() === "similar_product_ads"
                            || (!$overtureEnabled && $suma["name"]->__toString() === "overtureAds")
                        )
                    )
                ) {
                    if (!(isset($suma['disabled']) && $suma['disabled']->__toString() === "1")) {
                        if ($suma["name"]->__toString() === "overture" || $suma["name"]->__toString() === "overtureAds") {
                            $overtureEnabled = true;
390
                        }
391
                        if ($suma["name"]->__toString() !== "qualigo" && $suma["name"]->__toString() !== "similar_product_ads" && $suma["name"]->__toString() !== "overtureAds") {
392
                            $countSumas += 1;
393
394
                        }

395
396
397
398
399
400
401
402
                        $enabledSearchengines[] = $suma;
                    }
                }
            }
        }

        # Sonderregelung für alle Suchmaschinen, die zu den Minisuchern gehören. Diese können alle gemeinsam über einen Link abgefragt werden
        $subcollections = [];
403
404
405
        $tmp            = [];
        foreach ($enabledSearchengines as $engine) {
            if (isset($engine['minismCollection'])) {
406
                $subcollections[] = $engine['minismCollection']->__toString();
407
            } else {
408
                $tmp[] = $engine;
409
410
            }

411
412
        }
        $enabledSearchengines = $tmp;
413
414
415
416
        if (sizeof($subcollections) > 0) {
            $count                        = sizeof($subcollections) * 10;
            $minisucherEngine             = $xml->xpath('suma[@name="minism"]')[0];
            $subcollections               = urlencode("(" . implode(" OR ", $subcollections) . ")");
417
418
            $minisucherEngine["formData"] = str_replace("<<SUBCOLLECTIONS>>", $subcollections, $minisucherEngine["formData"]);
            $minisucherEngine["formData"] = str_replace("<<COUNT>>", $count, $minisucherEngine["formData"]);
419
            $enabledSearchengines[]       = $minisucherEngine;
420
421
422
423
        }

        #die(var_dump($enabledSearchengines));

424
        if ($countSumas <= 0) {
425
426
            $this->errors[] = "Achtung: Sie haben in ihren Einstellungen keine Suchmaschine ausgewählt.";
        }
427
        $engines = [];
428

429
        $siteSearchFailed = false;
430
        if (strlen($this->site) > 0) {
431
432
            # Wenn eine Sitesearch durchgeführt werden soll, überprüfen wir ob eine der Suchmaschinen überhaupt eine Sitesearch unterstützt:
            $enginesWithSite = 0;
433
434
            foreach ($enabledSearchengines as $engine) {
                if (isset($engine['hasSiteSearch']) && $engine['hasSiteSearch']->__toString() === "1") {
435
436
437
                    $enginesWithSite++;
                }
            }
438
439
            if ($enginesWithSite === 0) {
                $this->errors[]   = "Sie wollten eine Sitesearch auf " . $this->site . " durchführen. Leider unterstützen die eingestellten Suchmaschinen diese nicht. Sie können <a href=\"" . $this->generateSearchLink("web", false) . "\">hier</a> die Sitesearch im Web-Fokus durchführen. Es werden ihnen Ergebnisse ohne Sitesearch angezeigt.";
440
                $siteSearchFailed = true;
441
            } else {
442
443
444
445
446
447
                $this->warnings[] = "Sie führen eine Sitesearch durch. Es werden nur Ergebnisse von der Seite: <a href=\"http://" . $this->site . "\" target=\"_blank\">\"" . $this->site . "\"</a> angezeigt.";
            }

        }

        $typeslist = [];
448
        $counter   = 0;
449

450
451
452
        foreach ($enabledSearchengines as $engine) {

            if (!$siteSearchFailed && strlen($this->site) > 0 && (!isset($engine['hasSiteSearch']) || $engine['hasSiteSearch']->__toString() === "0")) {
453

454
455
456
457
458
                continue;
            }
            # Wenn diese Suchmaschine gar nicht eingeschaltet sein soll
            $path = "App\Models\parserSkripte\\" . ucfirst($engine["package"]->__toString());

459
            if (!file_exists(app_path() . "/Models/parserSkripte/" . ucfirst($engine["package"]->__toString()) . ".php")) {
460
461
462
463
464
465
466
467
468
                Log::error("Konnte " . $engine["name"] . " nicht abfragen, da kein Parser existiert");
                continue;
            }

            $time = microtime();

            try
            {
                $tmp = new $path($engine, $this);
469
            } catch (\ErrorException $e) {
470
471
472
473
                Log::error("Konnte " . $engine["name"] . " nicht abfragen." . var_dump($e));
                continue;
            }

474
475
            if ($tmp->enabled && isset($this->debug)) {
                $this->warnings[] = $tmp->service . "   Connection_Time: " . $tmp->connection_time . "    Write_Time: " . $tmp->write_time . " Insgesamt:" . ((microtime() - $time) / 1000);
476
477
            }

478
479
            if ($tmp->isEnabled()) {
                $engines[]                 = $tmp;
480
481
                $this->sockets[$tmp->name] = $tmp->fp;
            }
482

483
        }
484

485
486
        # Jetzt werden noch alle Kategorien der Settings durchgegangen und die jeweils enthaltenen namen der Suchmaschinen gespeichert.
        $foki = [];
487
488
489
        foreach ($sumas as $suma) {
            if ((!isset($suma['disabled']) || $suma['disabled'] === "") && (!isset($suma['userSelectable']) || $suma['userSelectable']->__toString() === "1")) {
                if (isset($suma['type'])) {
490
                    $f = explode(",", $suma['type']->__toString());
491
492
                    foreach ($f as $tmp) {
                        $name                                    = $suma['name']->__toString();
493
494
                        $foki[$tmp][$suma['name']->__toString()] = $name;
                    }
495
496
                } else {
                    $name                                        = $suma['name']->__toString();
497
498
499
500
501
502
503
                    $foki["andere"][$suma['name']->__toString()] = $name;
                }
            }
        }

        # Es werden auch die Namen der aktuell aktiven Suchmaschinen abgespeichert.
        $realEngNames = [];
504
        foreach ($enabledSearchengines as $realEng) {
505
            $nam = $realEng["name"]->__toString();
506
            if ($nam !== "qualigo" && $nam !== "overtureAds") {
507
508
509
510
                $realEngNames[] = $nam;
            }
        }
        # Anschließend werden diese beiden Listen verglichen (jeweils eine der Fokuslisten für jeden Fokus), um herauszufinden ob sie vielleicht identisch sind. Ist dies der Fall, so hat der Nutzer anscheinend Suchmaschinen eines kompletten Fokus eingestellt. Der Fokus wird dementsprechend angepasst.
511
512
        foreach ($foki as $fok => $engs) {
            $isFokus      = true;
513
            $fokiEngNames = [];
514
            foreach ($engs as $eng) {
515
516
                $fokiEngNames[] = $eng;
            }
517
518
            foreach ($fokiEngNames as $fen) {
                if (!in_array($fen, $realEngNames)) {
519
520
521
                    $isFokus = false;
                }
            }
522
523
            foreach ($realEngNames as $ren) {
                if (!in_array($ren, $fokiEngNames)) {
524
525
526
                    $isFokus = false;
                }
            }
527
            if ($isFokus) {
528
529
530
531
532
533
534
535
536
                $this->fokus = $fok;
            }
        }

        # Nun passiert ein elementarer Schritt.
        # Wir warten auf die Antwort der Suchmaschinen, da wir vorher nicht weiter machen können.
        # aber natürlich nicht ewig.
        # Die Verbindung steht zu diesem Zeitpunkt und auch unsere Request wurde schon gesendet.
        # Wir geben der Suchmaschine nun bis zu 500ms Zeit zu antworten.
537
538
539

        # Wir zählen die Suchmaschinen, die durch den Cache beantwortet wurden:
        $enginesToLoad = 0;
540
541
542
        $canBreak      = false;
        foreach ($engines as $engine) {
            if ($engine->cached) {
543
                $enginesToLoad--;
544
                if ($overtureEnabled && ($engine->name === "overture" || $engine->name === "overtureAds")) {
545
                    $canBreak = true;
546
547
                }

548
549
550
            }
        }
        $enginesToLoad += count($engines);
551
        $loadedEngines = 0;
552
553
554
        $timeStart     = microtime(true);
        while (true) {
            $time          = (microtime(true) - $timeStart) * 1000;
555
            $loadedEngines = intval(Redis::hlen('search.' . $this->getHashCode()));
556
            if ($overtureEnabled && (Redis::hexists('search.' . $this->getHashCode(), 'overture') || Redis::hexists('search.' . $this->getHashCode(), 'overtureAds'))) {
557
                $canBreak = true;
558
            }
559
560

            # Abbruchbedingung
561
562
            if ($time < 500) {
                if (($enginesToLoad === 0 || $loadedEngines >= $enginesToLoad) && $canBreak) {
563
                    break;
564
565
566
567
                }

            } elseif ($time >= 500 && $time < $this->time) {
                if (($enginesToLoad === 0 || ($loadedEngines / ($enginesToLoad * 1.0)) >= 0.8) && $canBreak) {
568
                    break;
569
570
571
                }

            } else {
572
573
574
575
576
                break;
            }
            usleep(50000);
        }

577
        #exit;
578
579
580
        foreach ($engines as $engine) {
            if (!$engine->loaded) {
                try {
581
                    $engine->retrieveResults();
582
                } catch (\ErrorException $e) {
583
                    Log::error($e);
584

585
586
587
                }
            }
        }
588

589
        # und verwerfen den Rest:
590
591
        foreach ($engines as $engine) {
            if (!$engine->loaded) {
592
                $engine->shutdown();
593
594
            }

595
596
597
        }

        $this->engines = $engines;
598
599
600
601
602
603
604
605
606
607
608
609
    }

    public function parseFormData(Request $request)
    {
        if ($request->input('encoding', '') !== "utf8") {
            # In früheren Versionen, als es den Encoding Parameter noch nicht gab, wurden die Daten in ISO-8859-1 übertragen
            $input = $request->all();
            foreach ($input as $key => $value) {
                $input[$key] = mb_convert_encoding("$value", "UTF-8", "ISO-8859-1");
            }
            $request->replace($input);
        }
610
        $this->url = $request->url();
611
        # Zunächst überprüfen wir die eingegebenen Einstellungen:
612
613
        # FOKUS
        $this->fokus = trans('fokiNames.'
614
615
            . $request->input('focus', 'web'));
        if (strpos($this->fokus, ".")) {
616
617
618
619
            $this->fokus = trans('fokiNames.web');
        }

        # SUMA-FILE
620
        if (App::isLocale("en")) {
621
            $this->sumaFile = config_path() . "/sumas.xml";
622
        } else {
623
624
            $this->sumaFile = config_path() . "/sumas.xml";
        }
625
        if (!file_exists($this->sumaFile)) {
626
627
628
629
630
            die("Suma-File konnte nicht gefunden werden");
        }

        # Sucheingabe:
        $this->eingabe = trim($request->input('eingabe', ''));
631
        if (strlen($this->eingabe) === 0) {
632
633
634
635
636
637
638
639
            $this->warnings[] = 'Achtung: Sie haben keinen Suchbegriff eingegeben. Sie können ihre Suchbegriffe oben eingeben und es erneut versuchen.';
        }
        $this->q = $this->eingabe;

        # IP:
        $this->ip = $request->ip();

        # Language:
640
        if (isset($_SERVER['HTTP_LANGUAGE'])) {
641
            $this->language = $_SERVER['HTTP_LANGUAGE'];
642
        } else {
643
644
645
646
647
648
            $this->language = "";
        }
        # Category
        $this->category = $request->input('category', '');
        # Request Times:
        $this->time = $request->input('time', 1000);
649

650
651
652
653
        # Page
        $this->page = $request->input('page', 1);
        # Lang
        $this->lang = $request->input('lang', 'all');
654
655
        if ($this->lang !== "de" && $this->lang !== "en" && $this->lang !== "all") {
            $this->lang = "all";
656
        }
657
        $this->agent  = new Agent();
658
659
660
661
        $this->mobile = $this->agent->isMobile();

        #Sprüche
        $this->sprueche = $request->input('sprueche', 'off');
662
        if ($this->sprueche === "off") {
663
            $this->sprueche = true;
664
        } else {
665
            $this->sprueche = false;
666
667
        }

668
669
670
671
        # Ergebnisse pro Seite:
        $this->resultCount = $request->input('resultCount', '20');

        # Manchmal müssen wir Parameter anpassen um den Sucheinstellungen gerecht zu werden:
672
673
674
        if ($request->has('dart')) {
            $this->time       = 10000;
            $this->warnings[] = "Hinweis: Sie haben Dart-Europe aktiviert. Die Suche kann deshalb länger dauern und die maximale Suchzeit wurde auf 10 Sekunden hochgesetzt.";
675
        }
676
677
        if ($this->time <= 500 || $this->time > 20000) {
            $this->time = 1000;
678
        }
679
680
681
682
683
684
685
686
687
        if ($request->has('minism') && ($request->has('fportal') || $request->has('harvest'))) {
            $input    = $request->all();
            $newInput = [];
            foreach ($input as $key => $value) {
                if ($key !== "fportal" && $key !== "harvest") {
                    $newInput[$key] = $value;
                }
            }
            $request->replace($newInput);
688
        }
689
690
        if (App::isLocale("en")) {
            $this->sprueche = "off";
691
        }
692
693
        if ($this->resultCount <= 0 || $this->resultCount > 200) {
            $this->resultCount = 1000;
694
        }
695
696
697
698
699
700
        if ($request->has('onenewspageAll') || $request->has('onenewspageGermanyAll')) {
            $this->time  = 5000;
            $this->cache = "cache";
        }
        if ($request->has('tab')) {
            if ($request->input('tab') === "off") {
701
                $this->tab = "_blank";
702
            } else {
703
704
                $this->tab = "_self";
            }
705
        } else {
706
707
            $this->tab = "_blank";
        }
708
        if ($request->has('password')) {
709
            $this->password = $request->input('password');
710
711
712
        }

        if ($request->has('quicktips')) {
713
            $this->quicktips = false;
714
        } else {
715
            $this->quicktips = true;
716
        }
717
718

        $this->out = $request->input('out', "html");
719
        if ($this->out !== "html" && $this->out !== "json" && $this->out !== "results" && $this->out !== "results-with-style") {
720
            $this->out = "html";
721
722
        }

723
        $this->request = $request;
724
725
726
727
728
729
730
731
732
733
    }

    public function checkSpecialSearches(Request $request)
    {
        # Site Search:
        if (preg_match("/(.*)\bsite:(\S+)(.*)/si", $this->q, $match)) {
            $this->site = $match[2];
            $this->q    = $match[1] . $match[3];
        }
        if ($request->has('site')) {
734
735
            $this->site = $request->input('site');
        }
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
        # Wenn die Suchanfrage um das Schlüsselwort "-host:*" ergänzt ist, sollen bestimmte Hosts nicht eingeblendet werden
        # Wir prüfen, ob das hier der Fall ist:
        while (preg_match("/(.*)(^|\s)-host:(\S+)(.*)/si", $this->q, $match)) {
            $this->hostBlacklist[] = $match[3];
            $this->q               = $match[1] . $match[4];
        }
        if (sizeof($this->hostBlacklist) > 0) {
            $hostString = "";
            foreach ($this->hostBlacklist as $host) {
                $hostString .= $host . ", ";
            }
            $hostString       = rtrim($hostString, ", ");
            $this->warnings[] = "Ergebnisse von folgenden Hosts werden nicht angezeigt: \"" . $hostString . "\"";
        }
        # Wenn die Suchanfrage um das Schlüsselwort "-domain:*" ergänzt ist, sollen bestimmte Domains nicht eingeblendet werden
        # Wir prüfen, ob das hier der Fall ist:
        while (preg_match("/(.*)(^|\s)-domain:(\S+)(.*)/si", $this->q, $match)) {
            $this->domainBlacklist[] = $match[3];
            $this->q                 = $match[1] . $match[4];
        }
        if (sizeof($this->domainBlacklist) > 0) {
            $domainString = "";
            foreach ($this->domainBlacklist as $domain) {
                $domainString .= $domain . ", ";
            }
            $domainString     = rtrim($domainString, ", ");
            $this->warnings[] = "Ergebnisse von folgenden Domains werden nicht angezeigt: \"" . $domainString . "\"";
        }

        # Alle mit "-" gepräfixten Worte sollen aus der Suche ausgeschlossen werden.
        # Wir prüfen, ob das hier der Fall ist:
        while (preg_match("/(.*)(^|\s)-(\S+)(.*)/si", $this->q, $match)) {
            $this->stopWords[] = $match[3];
            $this->q           = $match[1] . $match[4];
        }
        if (sizeof($this->stopWords) > 0) {
            $stopwordsString = "";
            foreach ($this->stopWords as $stopword) {
                $stopwordsString .= $stopword . ", ";
            }
            $stopwordsString  = rtrim($stopwordsString, ", ");
            $this->warnings[] = "Sie machen eine Ausschlusssuche. Ergebnisse mit folgenden Wörtern werden nicht angezeigt: \"" . $stopwordsString . "\"";
        }

        # Meldung über eine Phrasensuche
        $p   = "";
782
        $tmp = $this->q;
783
784
        while (preg_match("/(.*)\"(.+)\"(.*)/si", $tmp, $match)) {
            $tmp             = $match[1] . $match[3];
785
            $this->phrases[] = strtolower($match[2]);
786
787
        }
        foreach ($this->phrases as $phrase) {
788
789
790
            $p .= "\"$phrase\", ";
        }
        $p = rtrim($p, ", ");
791
        if (sizeof($this->phrases) > 0) {
792
            $this->warnings[] = "Sie führen eine Phrasensuche durch: $p";
793
        }
794

795
796
797
    }

    public function getFokus()
798
799
800
801
    {
        return $this->fokus;
    }

802
    public function getIp()
803
804
805
806
    {
        return $this->ip;
    }

807
    public function getEingabe()
808
809
810
811
    {
        return $this->eingabe;
    }

812
    public function getQ()
813
    {
814
        return $this->q;
815
816
    }

817
    public function getUrl()
818
819
820
    {
        return $this->url;
    }
821
    public function getTime()
822
823
824
825
    {
        return $this->time;
    }

826
    public function getLanguage()
827
828
829
830
    {
        return $this->language;
    }

831
    public function getLang()
832
833
834
835
    {
        return $this->lang;
    }

836
    public function getSprueche()
837
838
839
840
    {
        return $this->sprueche;
    }

841
    public function getCategory()
842
843
844
845
    {
        return $this->category;
    }

846
    public function getPhrases()
847
848
849
850
    {
        return $this->phrases;
    }

851
    public function getSumaFile()
852
853
854
855
    {
        return $this->sumaFile;
    }

856
    public function getUserHostBlacklist()
857
858
859
860
    {
        return $this->hostBlacklist;
    }

861
    public function getUserDomainBlacklist()
862
863
864
865
    {
        return $this->domainBlacklist;
    }

866
    public function getDomainBlacklist()
867
868
869
870
    {
        return $this->domainsBlacklisted;
    }

871
    public function getUrlBlacklist()
872
873
874
    {
        return $this->urlsBlacklisted;
    }
875
    public function getLanguageDetect()
876
877
878
    {
        return $this->languageDetect;
    }
879
    public function getStopWords()
880
881
882
883
884
    {
        return $this->stopWords;
    }
    public function getHostCount($host)
    {
885
        if (isset($this->addedHosts[$host])) {
886
            return $this->addedHosts[$host];
887
        } else {
888
889
890
891
892
893
            return 0;
        }
    }
    public function addHostCount($host)
    {
        $hash = md5($host);
894
        if (isset($this->addedHosts[$hash])) {
895
            $this->addedHosts[$hash] += 1;
896
        } else {
897
898
899
900
901
902
903
904
905
            $this->addedHosts[$hash] = 1;
        }
    }
    public function getSite()
    {
        return $this->site;
    }
    public function addLink($link)
    {
906
        if (strpos($link, "http://") === 0) {
907
            $link = substr($link, 7);
908
909
910
        }

        if (strpos($link, "https://") === 0) {
911
            $link = substr($link, 8);
912
913
914
        }

        if (strpos($link, "www.") === 0) {
915
            $link = substr($link, 4);
916
917
        }

918
919
        $link = trim($link, "/");
        $hash = md5($link);
920
        if (isset($this->addedLinks[$hash])) {
921
            return false;
922
        } else {
923
924
925
926
927
928
929
930
            $this->addedLinks[$hash] = 1;

            return true;
        }
    }

    public function generateSearchLink($fokus, $results = true)
    {
931
        $requestData          = $this->request->except('page');
932
        $requestData['focus'] = $fokus;
933
        if ($results) {
934
            $requestData['out'] = "results";
935
        } else {
936
            $requestData['out'] = "";
937
938
        }

939
940
941
942
943
944
945
946
947
948
949
950
951
        $link = action('MetaGerSearch@search', $requestData);
        return $link;
    }

    public function generateQuicktipLink()
    {
        $link = action('MetaGerSearch@quicktips');

        return $link;
    }

    public function generateSiteSearchLink($host)
    {
952
953
        $host        = urlencode($host);
        $requestData = $this->request->except(['page', 'out']);
954
955
        $requestData['eingabe'] .= " site:$host";
        $requestData['focus'] = "web";
956
        $link                 = action('MetaGerSearch@search', $requestData);
957
958
959
        return $link;
    }

960
    public function generateRemovedHostLink($host)
961
    {
962
963
        $host        = urlencode($host);
        $requestData = $this->request->except(['page', 'out']);
964
965
966
967
968
        $requestData['eingabe'] .= " -host:$host";
        $link = action('MetaGerSearch@search', $requestData);
        return $link;
    }

969
    public function generateRemovedDomainLink($domain)
970
    {
971
972
        $domain      = urlencode($domain);
        $requestData = $this->request->except(['page', 'out']);
973
974
975
976
977
        $requestData['eingabe'] .= " -domain:$domain";
        $link = action('MetaGerSearch@search', $requestData);
        return $link;
    }

978
    public function getTab()
979
980
981
    {
        return $this->tab;
    }
982
    public function getResults()
983
984
985
986
987
    {
        return $this->results;
    }
    public function popAd()
    {
988
        if (count($this->ads) > 0) {
989
            return get_object_vars(array_shift($this->ads));
990
        } else {
991
            return null;
992
993
        }

994
995
996
    }
    public function getImageProxyLink($link)
    {
997
        $requestData        = [];
998
        $requestData["url"] = $link;
999
        $link               = action('Pictureproxy@get', $requestData);
1000
1001
        return $link;
    }
1002
    public function showQuicktips()
1003
1004
1005
    {
        return $this->quicktips;
    }
Phil Höfer's avatar
Phil Höfer committed
1006
}