SearchViaCurl::getRedirection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace rOpenDev\Google;
4
5
use rOpenDev\curl\CurlRequest;
6
7
class SearchViaCurl extends Search
8
{
9
    protected $referrer;
10
11
    protected $cookie;
12
13
    /**
14
     * @return string|false Contenu html de la page
15
     */
16
    protected function requestGoogle(string $url, bool $redir = false)
17
    {
18
        $cache = $this->getCache($url);
19
        if ('' !== $cache) {
20
            return $cache;
21
        }
22
23
        $curl = new CurlRequest($url);
24
        $curl->setDefaultGetOptions()->setReturnHeader()->setEncodingGzip();
25
26
        if (isset($this->language)) {
27
            $curl->setOpt(\CURLOPT_HTTPHEADER, ['Accept-Language: '.$this->language]);
28
        }
29
        if (isset($this->userAgent)) {
30
            $curl->setUserAgent($this->userAgent);
31
        } else {
32
            if ($this->mobile) {
33
                $curl->setDestkopUserAgent();
34
            } else {
35
                $curl->setMobileUserAgent();
36
            }
37
        }
38
39
        if (isset($this->proxy)) {
40
            $curl->setProxy($this->proxy);
41
        }
42
        if (isset($this->referrer)) {
43
            $curl->setReferrer($this->referrer);
44
        }
45
        if (isset($this->cookie)) {
46
            $curl->setCookie($this->cookie);
47
        }
48
49
        $output = $curl->execute();
50
        file_put_contents('debug.html', $output);
51
52
        /* Erreur logs de l'éxecution du cURL **/
53
        if ($curl->hasError()) {
54
            $this->cErrors = $curl->getErrors();
55
            $this->error = 3;
56
57
            return false;
58
        }
59
60
        $amIKicked = $this->amIKickedByGoogleThePowerful($output);
61
        if (false !== $amIKicked) {
62
            $this->error = $amIKicked;
63
64
            return false;
65
        }
66
67
        if ($redirection = $this->getRedirection($output)) {
68
            if (true === $redir) {
69
                $this->error = 4;
70
71
                return false;
72
            }
73
74
            return $this->requestGoogle('https://www.google.'.$this->tld.$redirection, true);
75
        }
76
77
        /* Tout est Ok, on enregistre et on renvoit le html **/
78
        $this->setCache($url, $output);
79
80
        $this->cookie = $curl->getCookies();
81
        $this->referrer = $curl->getEffectiveUrl();
82
        $this->execSleep();
83
84
        return $output;
85
    }
86
87
    /**
88
     * Retrieve a meta refresh.
89
     */
90
    private function getRedirection(string $html): string
91
    {
92
        if (preg_match('/content="\d+;url=(.*?)"/i', $html, $match)) {
93
            return str_replace('&amp;', '&', $match[1]);
94
        }
95
96
        return '';
97
    }
98
}
99