Completed
Pull Request — master (#218)
by
unknown
02:45
created

CrawlerDetect::setUserAgent()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of Crawler Detect - the web crawler detection library.
5
 *
6
 * (c) Mark Beech <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Jaybizzle\CrawlerDetect;
13
14
use Jaybizzle\CrawlerDetect\Fixtures\Headers;
15
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers;
16
use Jaybizzle\CrawlerDetect\Fixtures\Exclusions;
17
18
class CrawlerDetect
19
{
20
    /**
21
     * The user agent.
22
     *
23
     * @var null
24
     */
25
    protected $userAgent = null;
26
27
    /**
28
     * Headers that contain a user agent.
29
     *
30
     * @var array
31
     */
32
    protected $httpHeaders = array();
33
34
    /**
35
     * Store regex matches.
36
     *
37
     * @var array
38
     */
39
    protected $matches = array();
40
41
    /**
42
     * Crawlers object.
43
     *
44
     * @var \Jaybizzle\CrawlerDetect\Fixtures\Crawlers
45
     */
46
    protected $crawlers;
47
48
    /**
49
     * Exclusions object.
50
     *
51
     * @var \Jaybizzle\CrawlerDetect\Fixtures\Exclusions
52
     */
53
    protected $exclusions;
54
55
    /**
56
     * Headers object.
57
     *
58
     * @var \Jaybizzle\CrawlerDetect\Fixtures\Headers
59
     */
60
    protected $uaHttpHeaders;
61
62
    /**
63
     * Prefetch Headers object.
64
     *
65
     * @var \Jaybizzle\CrawlerDetect\Fixtures\Headers
66
     */
67
    protected $prefetchHttpHeaders;
68
69
    /**
70
     * The compiled regex string.
71
     *
72
     * @var string
73
     */
74
    protected $compiledRegex;
75
76
    /**
77
     * The compiled exclusions regex string.
78
     *
79
     * @var string
80
     */
81
    protected $compiledExclusions;
82
83
    /**
84
     * Class constructor.
85
     */
86
    public function __construct(array $headers = null, $userAgent = null)
87
    {
88
        $this->crawlers = new Crawlers();
89
        $this->exclusions = new Exclusions();
90
        $this->uaHttpHeaders = new Headers();
91
        $this->prefetchHttpHeaders = new Headers()->getPrefetchHeaders();
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_OBJECT_OPERATOR
Loading history...
92
93
        $this->compiledRegex = $this->compileRegex($this->crawlers->getAll());
94
        $this->compiledExclusions = $this->compileRegex($this->exclusions->getAll());
95
96
        $this->setHttpHeaders($headers);
97
        $this->userAgent = $this->setUserAgent($userAgent);
98
    }
99
100
    /**
101
     * Compile the regex patterns into one regex string.
102
     *
103
     * @param array
104
     *
105
     * @return string
106
     */
107
    public function compileRegex($patterns)
108
    {
109
        return '('.implode('|', $patterns).')';
110
    }
111
112
    /**
113
     * Set HTTP headers.
114
     *
115
     * @param array|null $httpHeaders
116
     */
117
    public function setHttpHeaders($httpHeaders)
118
    {
119
        // Use global _SERVER if $httpHeaders aren't defined.
120
        if (! is_array($httpHeaders) || ! count($httpHeaders)) {
121
            $httpHeaders = $_SERVER;
122
        }
123
124
        // Clear existing headers.
125
        $this->httpHeaders = array();
126
127
        // Only save HTTP headers. In PHP land, that means
128
        // only _SERVER vars that start with HTTP_.
129
        foreach ($httpHeaders as $key => $value) {
130
            if (strpos($key, 'HTTP_') === 0) {
131
                $this->httpHeaders[$key] = $value;
132
            }
133
        }
134
    }
135
136
    /**
137
     * Return user agent headers.
138
     *
139
     * @return array
140
     */
141
    public function getUaHttpHeaders()
142
    {
143
        return $this->uaHttpHeaders->getAll();
144
    }
145
146
    /**
147
     * Set the user agent.
148
     *
149
     * @param string $userAgent
150
     */
151
    public function setUserAgent($userAgent)
152
    {
153
        if (is_null($userAgent)) {
154
            foreach ($this->getUaHttpHeaders() as $altHeader) {
155
                if (isset($this->httpHeaders[$altHeader])) {
156
                    $userAgent .= $this->httpHeaders[$altHeader].' ';
157
                }
158
            }
159
        }
160
161
        return $userAgent;
162
    }
163
164
    /**
165
     * Check user agent string against the regex.
166
     *
167
     * @param string|null $userAgent
168
     *
169
     * @return bool
170
     */
171
    public function isCrawler($userAgent = null)
172
    {
173
        $prefetchHeaders = $this->hasPrefetchHeaderSet;
174
        if ($prefetchHeaders) {
175
            $this->matches = $prefetchHeaders;
176
            return true;
177
        }
178
179
        $agent = $userAgent ?: $this->userAgent;
180
181
        $agent = preg_replace('/'.$this->compiledExclusions.'/i', '', $agent);
182
183
        if (strlen(trim($agent)) == 0) {
184
            return false;
185
        }
186
187
        $result = preg_match('/'.$this->compiledRegex.'/i', trim($agent), $matches);
188
189
        if ($matches) {
190
            $this->matches = $matches;
191
        }
192
193
        return (bool) $result;
194
    }
195
196
    /**
197
     * Return the matches.
198
     *
199
     * @return string|null
200
     */
201
    public function getMatches()
202
    {
203
        return isset($this->matches[0]) ? $this->matches[0] : null;
204
    }
205
206
    /**
207
     * Return true if headers contain a prefetch setting
208
     *
209
     * @return null
210
     */
211
    public function hasPrefetchHeaderSet()
212
    {
213
        foreach ($this->prefetchHttpHeaders as $header => $value) {
214
            if (isset($this->uaHttpHeaders[$header]) && $this->uaHttpHeaders[$header] == $value) {
215
                return [$header => $value];
216
            }
217
        }
218
219
        return false;
220
    }
221
}
222