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

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

class MetaGer
{
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    # 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             = [];
Dominik Hebeler's avatar
Dominik Hebeler committed
32
    protected $products        = [];
33
34
35
    protected $warnings        = [];
    protected $errors          = [];
    protected $addedHosts      = [];
36
    protected $startCount      = 0;
37
    protected $canCache        = false;
38
39
40
41
42
43
44
45
46
    # Daten über die Abfrage
    protected $ip;
    protected $language;
    protected $agent;
    # Konfigurationseinstellungen:
    protected $sumaFile;
    protected $mobile;
    protected $resultCount;
    protected $sprueche;
47
    protected $domainsBlacklisted = [];
48
    protected $urlsBlacklisted    = [];
49
50
51
    protected $url;
    protected $languageDetect;

52
53
    public function __construct()
    {
54
        # Timer starten
55
        $this->starttime = microtime(true);
56
57

        # Versuchen Blacklists einzulesen
58
59
        if (file_exists(config_path() . "/blacklistDomains.txt") && file_exists(config_path() . "/blacklistUrl.txt")) {
            $tmp                      = file_get_contents(config_path() . "/blacklistDomains.txt");
60
            $this->domainsBlacklisted = explode("\n", $tmp);
61
62
63
            $tmp                      = file_get_contents(config_path() . "/blacklistUrl.txt");
            $this->urlsBlacklisted    = explode("\n", $tmp);
        } else {
64
            Log::warning("Achtung: Eine, oder mehrere Blacklist Dateien, konnten nicht geöffnet werden");
65
66
        }

67
        # Parser Skripte einhängen
68
69
70
71
72
73
74
75
        $dir = app_path() . "/Models/parserSkripte/";
        foreach (scandir($dir) as $filename) {
            $path = $dir . $filename;
            if (is_file($path)) {
                require $path;
            }
        }

76
        # Cachebarkeit testen
77
78
79
80
81
82
        try {
            Cache::has('test');
            $this->canCache = true;
        } catch (ConnectionException $e) {
            $this->canCache = false;
        }
83
    }
84

85
    # Erstellt aus den gesammelten Ergebnissen den View
86
87
88
    public function createView()
    {
        $viewResults = [];
89
        # Wir extrahieren alle notwendigen Variablen und geben Sie an unseren View:
90
        foreach ($this->results as $result) {
91
92
93
94
95
96
            $viewResults[] = get_object_vars($result);
        }

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

97
98
        if ($this->fokus === "bilder") {
            switch ($this->out) {
99
100
101
102
103
104
105
106
                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)
107
                        ->with('browser', (new Agent())->browser());
108
109
110
111
112
113
114
115
                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)
116
                        ->with('browser', (new Agent())->browser());
117
            }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
        } else {
            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)
                        ->with('browser', (new Agent())->browser());
                    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")
                        ->with('browser', (new Agent())->browser());
                    break;
                default:
                    return view('metager3')
                        ->with('eingabe', $this->eingabe)
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
                        ->with('metager', $this)
                        ->with('browser', (new Agent())->browser());
                    break;
            }
151
152
153
        }
    }

Phil Höfer's avatar
Phil Höfer committed
154
    public function prepareResults()
155
    {
Phil Höfer's avatar
Phil Höfer committed
156
        $engines = $this->engines;
157

Phil Höfer's avatar
Phil Höfer committed
158
159
        // combine
        $combinedResults = $this->combineResults($engines);
160
161
162
163

        # Wir bestimmen die Sprache eines jeden Suchergebnisses
        $this->results = $this->addLangCodes($this->results);

Phil Höfer's avatar
Phil Höfer committed
164
165
166
167
168
169
        // sort
        //$sortedResults = $this->sortResults($engines);
        // filter
        // augment (boost&adgoal)
        // authorize
        // misc (WiP)
170
171
        uasort($this->results, function ($a, $b) {
            if ($a->getRank() == $b->getRank()) {
172
                return 0;
173
174
            }

175
176
            return ($a->getRank() < $b->getRank()) ? 1 : -1;
        });
177

178
179
        # Validate Results
        $newResults = [];
180
181
        foreach ($this->results as $result) {
            if ($result->isValid($this)) {
182
                $newResults[] = $result;
183
184
            }

185
186
187
        }
        $this->results = $newResults;

188
189
190
191
192
193
        # Boost implementation
        $this->results = $this->parseBoost($this->results);

        #Adgoal Implementation
        $this->results = $this->parseAdgoal($this->results);

194
        $counter   = 0;
195
        $firstRank = 0;
196

197
        if (isset($this->startForwards)) {
198
            $this->startCount = $this->startForwards;
199
        } elseif (isset($this->startBackwards)) {
200
            $this->startCount = $this->startBackwards - count($this->results) - 1;
201
        } else {
202
203
204
            $this->startCount = 0;
        }

205
206
        foreach ($this->results as $result) {
            if ($counter === 0) {
207
                $firstRank = $result->rank;
208
209
            }

210
            $counter++;
211
            $result->number = $counter + $this->startCount;
212
213
214
215
            $confidence     = 0;
            if ($firstRank > 0) {
                $confidence = $result->rank / $firstRank;
            } else {
216
                $confidence = 0;
217
218
219
            }

            if ($confidence > 0.65) {
220
                $result->color = "#FF4000";
221
            } elseif ($confidence > 0.4) {
222
                $result->color = "#FF0080";
223
            } elseif ($confidence > 0.2) {
224
                $result->color = "#C000C0";
225
            } else {
226
                $result->color = "#000000";
227
228
            }

229
230
        }

231
        if (LaravelLocalization::getCurrentLocale() === "en") {
232
233
234
            $this->ads = [];
        }

235
        $this->validated = false;
236
        if (isset($this->password)) {
237
238
239
            # 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');
240
            $eingabe  = $this->eingabe;
241
            $password = md5($eingabe . $password);
242
243
            if ($this->password === $password) {
                $this->ads       = [];
244
                $this->products  = [];
245
246
247
                $this->validated = true;
            }
        }
248
249

        if (count($this->results) <= 0) {
250
            $this->errors[] = trans('metaGer.results.failed');
251
        }
252

253
        if ($this->canCache() && isset($this->next) && count($this->next) > 0 && count($this->results) > 0) {
254
            $page       = $this->page + 1;
255
            $this->next = [
256
257
258
                'page'          => $page,
                'startForwards' => $this->results[count($this->results) - 1]->number,
                'engines'       => $this->next,
259
260
            ];
            Cache::put(md5(serialize($this->next)), serialize($this->next), 60);
261
262
        } else {
            $this->next = [];
263
264
        }

265
    }
266

267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    private function addLangCodes($results)
    {
        # Bei der Spracheinstellung "all" wird nicht gefiltert
        if ($this->getLang() === "all") {
            return $results;
        } else {
            # Ansonsten müssen wir jedem Result einen Sprachcode hinzufügen
            $id          = 0;
            $langStrings = [];
            foreach ($results as $result) {
                # Wir geben jedem Ergebnis eine ID um später die Sprachcodes zuordnen zu können
                $result->id = $id;

                $langStrings["result_" . $id] = utf8_encode($result->getLangString());

                $id++;
            }
            # Wir schreiben die Strings in eine temporäre JSON-Datei,
            # Da das Array unter umständen zu groß ist für eine direkte Übergabe an das Skript
            $filename = "/tmp/" . getmypid();
            file_put_contents($filename, json_encode($langStrings));
            $langDetectorPath = app_path() . "/Models/lang.pl";
            $lang             = exec("echo '$filename' | $langDetectorPath");
            $lang             = json_decode($lang, true);

            # Wir haben nun die Sprachcodes der einzelnen Ergebnisse.
            # Diese müssen wir nur noch korrekt zuordnen, dann sind wir fertig.
            foreach ($lang as $key => $langCode) {
                # Prefix vom Key entfernen:
                $id = intval(str_replace("result_", "", $key));
                foreach ($this->results as $result) {
                    if ($result->id === $id) {
                        $result->langCode = $langCode;
                        break;
                    }
                }
            }
            return $results;
        }
    }

Phil Höfer's avatar
Phil Höfer committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    public function combineResults($engines)
    {
        foreach ($engines as $engine) {
            if (isset($engine->next)) {
                $this->next[] = $engine->next;
            }
            if (isset($engine->last)) {
                $this->last[] = $engine->last;
            }
            foreach ($engine->results as $result) {
                if ($result->valid) {
                    $this->results[] = $result;
                }
            }
            foreach ($engine->ads as $ad) {
                $this->ads[] = $ad;
            }
            foreach ($engine->products as $product) {
                $this->products[] = $product;
            }
        }

    }

332
333
    public function parseBoost($results)
    {
334
335
336
337
338
339
340
341
342
343
344
345
        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;
346
    }
347

348
349
    public function parseAdgoal($results)
    {
350
        $publicKey  = getenv('adgoal_public');
351
        $privateKey = getenv('adgoal_private');
352
        if ($publicKey === false) {
353
354
355
            return $results;
        }
        $tldList = "";
356
357
        try {
            foreach ($results as $result) {
358
                $link = $result->anzeigeLink;
359
                if (strpos($link, "http") !== 0) {
360
361
362
363
364
365
366
367
368
369
                    $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");

370
            # Query
371
372
            $query = urlencode($this->q);

373
            $link   = "https://api.smartredirect.de/api_v2/CheckForAffiliateUniversalsearchMetager.php?p=" . $publicKey . "&k=" . $hash . "&tld=" . $tldList . "&q=" . $query;
374
375
376
            $answer = json_decode(file_get_contents($link));

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

381
382
                foreach ($results as $result) {
                    if ($hoster === $result->tld) {
383
384
                        # Hier ist ein Advertiser:
                        # Das Logo hinzufügen:
385
                        if ($result->image !== "") {
386
                            $result->logo = "https://img.smartredirect.de/logos_v2/60x30/" . $hash . ".gif";
387
                        } else {
388
                            $result->image = "https://img.smartredirect.de/logos_v2/120x60/" . $hash . ".gif";
389
390
                        }

391
392
393
                        # Den Link hinzufügen:
                        $publicKey = $publicKey;
                        $targetUrl = $result->anzeigeLink;
394
                        if (strpos($targetUrl, "http") !== 0) {
395
                            $targetUrl = "http://" . $targetUrl;
396
397
398
399
400
                        }

                        $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;
401
402
403
404
                        $result->partnershop = true;
                    }
                }
            }
405
        } catch (\ErrorException $e) {
406
407
408
409
410
            return $results;
        }

        return $results;
    }
411

Karl's avatar
Karl committed
412
413
414
415
    /*
     * Die Erstellung der Suchmaschinen bis die Ergebnisse da sind mit Unterfunktionen
     */

416
417
    public function createSearchEngines(Request $request)
    {
418
        # Wenn es kein Suchwort gibt
419
        if (!$request->has("eingabe")) {
420
            return;
421
        }
422

423
        $xml                  = simplexml_load_file($this->sumaFile);
424
        $sumas                = $xml->xpath("suma");
425
        $enabledSearchengines = [];
426
        $overtureEnabled      = false;
427
428
        $sumaCount            = 0;

Karl's avatar
Karl committed
429
        /* Erstellt die Liste der eingestellten Sumas
430
431
432
         * Der einzige Unterschied bei angepasstem Suchfokus ist,
         * dass nicht nach den Typen einer Suma,
         * sondern den im Request mitgegebenen Typen entschieden wird.
Karl's avatar
Karl committed
433
434
435
436
437
438
         * Ansonsten wird genau das selbe geprüft und gemacht:
         * Handelt es sich um spezielle Suchmaschinen die immer an sein müssen
         * Wenn es Overture ist vermerken dass Overture an ist
         * Suma Zähler erhöhen
         * Zu Liste hinzufügen
         */
439
        foreach ($sumas as $suma) {
440
            if (($this->sumaIsSelected($suma, $request)
441
                || ($this->isBildersuche()
442
                    && $this->sumaIsAdsuche($suma, $overtureEnabled)))
443
444
445
                && (!$this->sumaIsDisabled($suma))) {
                if ($this->sumaIsOverture($suma)) {
                    $overtureEnabled = true;
446
                }
447
448
                if ($this->sumaIsNotAdsuche($suma)) {
                    $sumaCount += 1;
449
                }
450
                $enabledSearchengines[] = $suma;
451
452
453
454
455
            }
        }

        # Sonderregelung für alle Suchmaschinen, die zu den Minisuchern gehören. Diese können alle gemeinsam über einen Link abgefragt werden
        $subcollections = [];
456
457

        $tmp = [];
458
459
        foreach ($enabledSearchengines as $engine) {
            if (isset($engine['minismCollection'])) {
460
                $subcollections[] = $engine['minismCollection']->__toString();
461
            } else {
462
                $tmp[] = $engine;
463
464
            }

465
466
        }
        $enabledSearchengines = $tmp;
467
        if (sizeof($subcollections) > 0) {
468
            $enabledSearchengines[] = $this->loadMiniSucher($xml, $subcollections);
469
470
        }

471
        if ($sumaCount <= 0) {
472
            $this->errors[] = trans('metaGer.settings.noneSelected');
473
        }
474

475
        $engines = [];
476

477
        # Wenn eine Sitesearch durchgeführt werden soll, überprüfen wir ob überhaupt eine der Suchmaschinen eine Sitesearch unterstützt
478
        $siteSearchFailed = $this->checkCanNotSitesearch($enabledSearchengines);
479
480

        $typeslist = [];
481
        $counter   = 0;
482

483
484
        if ($this->requestIsCached($request)) {
            $engines = $this->getCachedEngines($request);
485
        } else {
486
            $engines = $this->actuallyCreateSearchEngines($enabledSearchengines, $siteSearchFailed);
487
        }
488

489
        # Wir starten alle Suchen
490
491
        foreach ($engines as $engine) {
            $engine->startSearch($this);
492
        }
493

494
495
        $this->adjustFocus($sumas, $enabledSearchengines);

496
497
498
499
500
        /* Wir warten auf die Antwort der Suchmaschinen
         * Die Verbindung steht zu diesem Zeitpunkt und auch unsere Requests wurden schon gesendet.
         * Wir zählen die Suchmaschinen, die durch den Cache beantwortet wurden:
         * $enginesToLoad zählt einerseits die Suchmaschinen auf die wir warten und andererseits
         * welche Suchmaschinen nicht rechtzeitig geantwortet haben.
501
502
         */

503
        $enginesToLoad = [];
504
505
506
507
508
509
        $canBreak      = false;
        foreach ($engines as $engine) {
            if ($engine->cached) {
                if ($overtureEnabled && ($engine->name === "overture" || $engine->name === "overtureAds")) {
                    $canBreak = true;
                }
510
511
            } else {
                $enginesToLoad[$engine->name] = false;
512
513
514
515
516
517
518
519
            }
        }

        $this->waitForResults($enginesToLoad, $overtureEnabled, $canBreak);

        $this->retrieveResults($engines);
    }

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
    # Spezielle Suchen und Sumas

    public function sumaIsSelected($suma, $request)
    {
        if ($this->fokus === "angepasst") {
            if ($request->has($suma["name"])) {
                return true;
            }
        } else {
            $types = explode(",", $suma["type"]);
            if (in_array($this->fokus, $types)) {
                return true;
            }
        }
        return false;
    }

    public function actuallyCreateSearchEngines($enabledSearchengines, $siteSearchFailed)
    {
        $engines = [];
        foreach ($enabledSearchengines as $engine) {

            # Wenn diese Suchmaschine gar nicht eingeschaltet sein soll
            if (!$siteSearchFailed
                && strlen($this->site) > 0
                && (!isset($engine['hasSiteSearch'])
                    || $engine['hasSiteSearch']->__toString() === "0")) {
                continue;
            }

            # Setze Pfad zu Parser
            $path = "App\Models\parserSkripte\\" . ucfirst($engine["package"]->__toString());

            # Prüfe ob Parser vorhanden
            if (!file_exists(app_path() . "/Models/parserSkripte/" . ucfirst($engine["package"]->__toString()) . ".php")) {
555
                Log::error("Konnte " . $engine["name"] . " nicht abfragen, da kein Parser existiert");
556
                $this->errors[] = trans('metaGer.engines.noParser', ['engine' => $engine["name"]]);
557
558
559
560
561
562
563
564
                continue;
            }

            # Es wird versucht die Suchengine zu erstellen
            $time = microtime();
            try {
                $tmp = new $path($engine, $this);
            } catch (\ErrorException $e) {
565
                Log::error("Konnte " . $engine["name"] . " nicht abfragen. " . var_dump($e));
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
                continue;
            }

            # Ausgabe bei Debug-Modus
            if ($tmp->enabled && isset($this->debug)) {
                $this->warnings[] = $tmp->service . "   Connection_Time: " . $tmp->connection_time . "    Write_Time: " . $tmp->write_time . " Insgesamt:" . ((microtime() - $time) / 1000);
            }

            # Wenn die neu erstellte Engine eingeschaltet ist, wird sie der Liste hinzugefügt
            if ($tmp->isEnabled()) {
                $engines[] = $tmp;
            }
        }
        return $engines;
    }

    public function isBildersuche()
    {
        return $this->fokus !== "bilder";
    }

    public function sumaIsAdsuche($suma, $overtureEnabled)
    {
        return
        $suma["name"]->__toString() === "qualigo"
        || $suma["name"]->__toString() === "similar_product_ads"
        || (!$overtureEnabled
            && $suma["name"]->__toString() === "overtureAds")
        || $suma["name"]->__toString() == "rlvproduct";
    }

    public function sumaIsDisabled($suma)
    {
        return
        isset($suma['disabled'])
        && $suma['disabled']->__toString() === "1";
    }

    public function sumaIsOverture($suma)
    {
        return
        $suma["name"]->__toString() === "overture"
        || $suma["name"]->__toString() === "overtureAds";
    }

    public function sumaIsNotAdsuche($suma)
    {
        return
        $suma["name"]->__toString() !== "qualigo"
        && $suma["name"]->__toString() !== "similar_product_ads"
        && $suma["name"]->__toString() !== "overtureAds";
    }

    public function requestIsCached($request)
    {
        return
        $request->has('next')
        && Cache::has($request->input('next'))
        && unserialize(Cache::get($request->input('next')))['page'] > 1;
    }

    public function getCachedEngines($request)
    {
        $next       = unserialize(Cache::get($request->input('next')));
        $this->page = $next['page'];
        $engines    = $next['engines'];
        if (isset($next['startForwards'])) {
            $this->startForwards = $next['startForwards'];
        }
        if (isset($next['startBackwards'])) {
            $this->startBackwards = $next['startBackwards'];
        }
        return $engines;
    }

    public function loadMiniSucher($xml, $subcollections)
    {
        $minisucherEngine             = $xml->xpath('suma[@name="minism"]')[0];
        $subcollections               = urlencode("(" . implode(" OR ", $subcollections) . ")");
        $minisucherEngine["formData"] = str_replace("<<SUBCOLLECTIONS>>", $subcollections, $minisucherEngine["formData"]);
        $minisucherEngine["formData"] = str_replace("<<COUNT>>", sizeof($subcollections) * 10, $minisucherEngine["formData"]);
        return $minisucherEngine;
    }

650
    # Passt den Suchfokus an, falls für einen Fokus genau alle vorhandenen Sumas eingeschaltet sind
651
652
    public function adjustFocus($sumas, $enabledSearchengines)
    {
653
654
        # Findet für alle Foki die enthaltenen Sumas
        $foki = []; # [fokus][suma] => [suma]
655
656
657
        foreach ($sumas as $suma) {
            if ((!isset($suma['disabled']) || $suma['disabled'] === "") && (!isset($suma['userSelectable']) || $suma['userSelectable']->__toString() === "1")) {
                if (isset($suma['type'])) {
658
659
660
                    # Wenn foki für diese Suchmaschine angegeben sind
                    $focuses = explode(",", $suma['type']->__toString());
                    foreach ($focuses as $foc) {
661
662
663
664
665
                        if (isset($suma['minismCollection'])) {
                            $foki[$foc][] = "minism";
                        } else {
                            $foki[$foc][] = $suma['name']->__toString();
                        }
666
                    }
667
                } else {
668
                    # Wenn keine foki für diese Suchmaschine angegeben sind
669
670
671
672
673
                    if (isset($suma['minismCollection'])) {
                        $foki["andere"][] = "minism";
                    } else {
                        $foki["andere"][] = $suma['name']->__toString();
                    }
674
675
676
677
                }
            }
        }

678
        # Findet die Namen der aktuell eingeschalteten Sumas
679
        $realEngNames = [];
680
        foreach ($enabledSearchengines as $realEng) {
681
            $nam = $realEng["name"]->__toString();
682
            if ($nam !== "qualigo" && $nam !== "overtureAds" && $nam !== "rlvproduct") {
683
684
685
                $realEngNames[] = $nam;
            }
        }
686

687
        # 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.
688
        foreach ($foki as $fok => $engines) {
689
            $isFokus      = true;
690
            $fokiEngNames = [];
691
            foreach ($engines as $eng) {
692
693
                $fokiEngNames[] = $eng;
            }
694
            # Jede eingeschaltete Engine ist für diesen Fokus geeignet
695
            foreach ($fokiEngNames as $fen) {
696
697
                # Bei Bildersuchen ist uns egal, ob alle Suchmaschinen aus dem Suchfokus eingeschaltet sind, da wir sie eh als Bildersuche anzeigen müssen
                if (!in_array($fen, $realEngNames) && $fok !== "bilder") {
698
699
700
                    $isFokus = false;
                }
            }
701
            # Jede im Fokus erwartete Engine ist auch eingeschaltet
702
703
            foreach ($realEngNames as $ren) {
                if (!in_array($ren, $fokiEngNames)) {
704
705
706
                    $isFokus = false;
                }
            }
707
            # Wenn die Listen identisch sind, setze den Fokus um
708
            if ($isFokus) {
709
710
711
                $this->fokus = $fok;
            }
        }
712
    }
713

714
715
716
717
718
719
720
    public function checkCanNotSitesearch($enabledSearchengines)
    {
        if (strlen($this->site) > 0) {
            $enginesWithSite = 0;
            foreach ($enabledSearchengines as $engine) {
                if (isset($engine['hasSiteSearch']) && $engine['hasSiteSearch']->__toString() === "1") {
                    $enginesWithSite++;
721
                }
722
723
            }
            if ($enginesWithSite === 0) {
724
                $this->errors[] = trans('metaGer.sitesearch.failed', ['site' => $this->site, 'searchLink' => $this->generateSearchLink("web", false)]);
725
726
                return true;
            } else {
727
                $this->warnings[] = trans('metaGer.sitesearch.success', ['site' => $this->site]);
728
                return false;
729
730
            }
        }
731
732
733
734
    }

    public function waitForResults($enginesToLoad, $overtureEnabled, $canBreak)
    {
735
        $loadedEngines = 0;
736
        $timeStart     = microtime(true);
737
738
739
740

        # Auf wie viele Suchmaschinen warten wir?
        $engineCount = count($enginesToLoad);

741
742
        while (true) {
            $time          = (microtime(true) - $timeStart) * 1000;
743
            $loadedEngines = intval(Redis::hlen('search.' . $this->getHashCode()));
744
            if ($overtureEnabled && (Redis::hexists('search.' . $this->getHashCode(), 'overture') || Redis::hexists('search.' . $this->getHashCode(), 'overtureAds'))) {
745
                $canBreak = true;
746
            }
747
748

            # Abbruchbedingung
749
            if ($time < 500) {
750
                if (($engineCount === 0 || $loadedEngines >= $engineCount) && $canBreak) {
751
                    break;
752
753
754
                }

            } elseif ($time >= 500 && $time < $this->time) {
755
                if (($engineCount === 0 || ($loadedEngines / ($engineCount * 1.0)) >= 0.8) && $canBreak) {
756
                    break;
757
758
759
                }

            } else {
760
761
762
763
                break;
            }
            usleep(50000);
        }
764
765
766
767
768
769
770

        # Wir haben nun so lange wie möglich gewartet. Wir registrieren nun noch die Suchmaschinen, die geanwortet haben.
        $answered = Redis::hgetall('search.' . $this->getHashCode());
        foreach ($answered as $key => $value) {
            $enginesToLoad[$key] = true;
        }
        $this->enginesToLoad = $enginesToLoad;
771
    }
772

773
774
775
    public function retrieveResults($engines)
    {
        # Von geladenen Engines die Ergebnisse holen
776
777
778
        foreach ($engines as $engine) {
            if (!$engine->loaded) {
                try {
779
                    $engine->retrieveResults($this);
780
                } catch (\ErrorException $e) {
781
782
783
784
                    Log::error($e);
                }
            }
        }
785

786
        # Nicht fertige Engines verwefen
787
788
        foreach ($engines as $engine) {
            if (!$engine->loaded) {
789
                $engine->shutdown();
790
            }
791
792
793
        }

        $this->engines = $engines;
794
795
    }

796
797
798
/*
 * Ende Suchmaschinenerstellung und Ergebniserhalt
 */
Karl's avatar
Karl committed
799

800
801
    public function parseFormData(Request $request)
    {
802
        # Sichert, dass der request in UTF-8 formatiert ist
803
804
805
806
807
808
809
810
        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);
        }
811
        $this->url = $request->url();
812
        # Zunächst überprüfen wir die eingegebenen Einstellungen:
813
        # Fokus
814
        $this->fokus = $request->input('focus', 'web');
815
        # Suma-File
816
        if (App::isLocale("en")) {
817
            $this->sumaFile = config_path() . "/sumas.xml";
818
        } else {
819
820
            $this->sumaFile = config_path() . "/sumas.xml";
        }
821
        if (!file_exists($this->sumaFile)) {
822
            die(trans('metaGer.formdata.cantLoad'));
823
        }
824
        # Sucheingabe
825
        $this->eingabe = trim($request->input('eingabe', ''));
826
        if (strlen($this->eingabe) === 0) {
827
            $this->warnings[] = trans('metaGer.formdata.noSearch');
828
829
        }
        $this->q = $this->eingabe;
830
        # IP
831
        $this->ip = $request->ip();
832
        # Language
833
        if (isset($_SERVER['HTTP_LANGUAGE'])) {
834
            $this->language = $_SERVER['HTTP_LANGUAGE'];
835
        } else {
836
837
838
839
            $this->language = "";
        }
        # Category
        $this->category = $request->input('category', '');
840
        # Request Times
841
842
        $this->time = $request->input('time', 1000);
        # Page
843
        $this->page = 1;
844
845
        # Lang
        $this->lang = $request->input('lang', 'all');
846
847
        if ($this->lang !== "de" && $this->lang !== "en" && $this->lang !== "all") {
            $this->lang = "all";
848
        }
849
        $this->agent  = new Agent();
850
        $this->mobile = $this->agent->isMobile();
851
        # Sprüche
852
        $this->sprueche = $request->input('sprueche', 'off');
853
        if ($this->sprueche === "off") {
854
            $this->sprueche = true;
855
        } else {
856
            $this->sprueche = false;
857
        }
858
        # Theme
Phil Höfer's avatar
Phil Höfer committed
859
        $this->theme = preg_replace("/[^[:alnum:][:space:]]/u", '', $request->input('theme', 'default'));
860
861
862
        # Ergebnisse pro Seite:
        $this->resultCount = $request->input('resultCount', '20');
        # Manchmal müssen wir Parameter anpassen um den Sucheinstellungen gerecht zu werden:
863
864
        if ($request->has('dart')) {
            $this->time       = 10000;
865
            $this->warnings[] = trans('metaGer.formdata.dartEurope');
866
        }
867
868
        if ($this->time <= 500 || $this->time > 20000) {
            $this->time = 1000;
869
        }
870
871
872
873
874
875
876
877
878
        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);
879
        }
880
881
        if (App::isLocale("en")) {
            $this->sprueche = "off";
882
        }
883
884
        if ($this->resultCount <= 0 || $this->resultCount > 200) {
            $this->resultCount = 1000;
885
        }
886
887
888
889
890
891
        if ($request->has('onenewspageAll') || $request->has('onenewspageGermanyAll')) {
            $this->time  = 5000;
            $this->cache = "cache";
        }
        if ($request->has('tab')) {
            if ($request->input('tab') === "off") {
892
                $this->tab = "_blank";
893
            } else {
894
895
                $this->tab = "_self";
            }
896
        } else {
897
898
            $this->tab = "_blank";
        }
899
        if ($request->has('password')) {
900
            $this->password = $request->input('password');
901
902
        }
        if ($request->has('quicktips')) {
903
            $this->quicktips = false;
904
        } else {
905
            $this->quicktips = true;
906
        }
907
        $this->out = $request->input('out', "html");
908
        # Standard output format html
909
        if ($this->out !== "html" && $this->out !== "json" && $this->out !== "results" && $this->out !== "results-with-style") {
910
            $this->out = "html";
911
        }
912
        $this->request = $request;
913
914
915
916
    }

    public function checkSpecialSearches(Request $request)
    {
917
918
919
920
921
922
923
924
925
        $this->searchCheckSitesearch($request);
        $this->searchCheckHostBlacklist();
        $this->searchCheckDomainBlacklist();
        $this->searchCheckStopwords();
        $this->searchCheckPhrase();
    }

    public function searchCheckSitesearch($request)
    {
926
927
928
929
930
        if (preg_match("/(.*)\bsite:(\S+)(.*)/si", $this->q, $match)) {
            $this->site = $match[2];
            $this->q    = $match[1] . $match[3];
        }
        if ($request->has('site')) {
931
932
            $this->site = $request->input('site');
        }
933
    }
934

935
936
    public function searchCheckHostBlacklist()
    {
937
938
939
940
941
942
943
944
945
946
        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, ", ");
947
            $this->warnings[] = trans('metaGer.formdata.hostBlacklist', ['host' => $hostString]);
948
        }
949
    }
950

951
952
    public function searchCheckDomainBlacklist()
    {
953
954
955
956
957
958
959
960
961
962
        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, ", ");
963
            $this->warnings[] = trans('metaGer.formdata.domainBlacklist', ['domain' => $domainString]);
964
        }
965
    }
966

967
968
    public function searchCheckStopwords()
    {
969
970
971
972
973
974
975
976
977
978
        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, ", ");
979
            $this->warnings[] = trans('metaGer.formdata.stopwords', ['stopwords' => $stopwordsString]);
980
        }
981
    }
982

983
984
    public function searchCheckPhrase()
    {
985
        $p   = "";
986
        $tmp = $this->q;
987
988
        while (preg_match("/(.*)\"(.+)\"(.*)/si", $tmp, $match)) {
            $tmp             = $match[1] . $match[3];
989
            $this->phrases[] = strtolower($match[2]);
990
991
        }
        foreach ($this->phrases as $phrase) {
992
993
994
            $p .= "\"$phrase\", ";
        }
        $p = rtrim($p, ", ");
995
        if (sizeof($this->phrases) > 0) {
996
            $this->warnings[] = trans('metaGer.formdata.phrase', ['phrase' => $p]);
997
998
999
        }
    }

1000
    public function nextSearchLink()
For faster browsing, not all history is shown. View entire blame