Passed
Push — master ( 653726...9a5bd9 )
by Dev
03:05
created

SearchViaCurl::getRedirection()   A

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