Skip to content

Commit bda4209

Browse files
westonruterwillwashburn
authored andcommitted
Add ability for user agent, buffer size, and SSL host/peer verification to be overridden (#13)
* Add ability for user agent to be overridden * Opt for setUserAgent setter instead of constructor arg * Add member variables and setter methods for bufferSize and SSL peer/host verification
1 parent cbf1bad commit bda4209

1 file changed

Lines changed: 68 additions & 8 deletions

File tree

src/FasterImage/FasterImage.php

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,47 @@
1717
class FasterImage
1818
{
1919
/**
20-
* The default timeout
20+
* The default timeout.
2121
*
2222
* @var int
2323
*/
2424
protected $timeout = 10;
2525

26+
/**
27+
* The default buffer size.
28+
*
29+
* @var int
30+
*/
31+
protected $bufferSize = 256;
32+
33+
/**
34+
* The default for whether to verify SSL peer.
35+
*
36+
* @var bool
37+
*/
38+
protected $sslVerifyPeer = false;
39+
40+
/**
41+
* The default for whether to verify SSL host.
42+
*
43+
* @var bool
44+
*/
45+
protected $sslVerifyHost = false;
46+
2647
/**
2748
* If the content length should be included in the result set.
2849
*
2950
* @var bool
3051
*/
3152
protected $includeContentLength = false;
3253

54+
/**
55+
* The default user agent to set for requests.
56+
*
57+
* @var string
58+
*/
59+
protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36';
60+
3361
/**
3462
* Get the size of each of the urls in a list
3563
*
@@ -85,21 +113,53 @@ public function batch(array $urls)
85113
}
86114

87115
/**
88-
* @param $seconds
116+
* @param int $seconds
89117
*/
90118
public function setTimeout($seconds)
91119
{
92-
$this->timeout = $seconds;
120+
$this->timeout = (int) $seconds;
93121
}
94122

95123
/**
96-
* @param $bool
124+
* @param int $bufferSize
125+
*/
126+
public function setBufferSize($bufferSize)
127+
{
128+
$this->bufferSize = (int) $bufferSize;
129+
}
130+
131+
/**
132+
* @param bool $sslVerifyPeer
133+
*/
134+
public function setSslVerifyPeer($sslVerifyPeer)
135+
{
136+
$this->sslVerifyPeer = (bool) $sslVerifyPeer;
137+
}
138+
139+
/**
140+
* @param bool $sslVerifyHost
141+
*/
142+
public function setSslVerifyHost($sslVerifyHost)
143+
{
144+
$this->sslVerifyHost = (bool) $sslVerifyHost;
145+
}
146+
147+
/**
148+
* @param bool $bool
97149
*/
98150
public function setIncludeContentLength($bool)
99151
{
100152
$this->includeContentLength = (bool) $bool;
101153
}
102154

155+
/**
156+
* @param string $userAgent
157+
*/
158+
public function setUserAgent($userAgent)
159+
{
160+
$this->userAgent = $userAgent;
161+
}
162+
103163
/**
104164
* Create the handle for the curl request
105165
*
@@ -120,15 +180,15 @@ protected function handle($url, & $result)
120180
curl_setopt($ch, CURLOPT_URL, $url);
121181
curl_setopt($ch, CURLOPT_HEADER, 0);
122182
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
123-
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
124-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
125-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
183+
curl_setopt($ch, CURLOPT_BUFFERSIZE, $this->bufferSize);
184+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->sslVerifyPeer ? 1 : 0);
185+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->sslVerifyHost ? 2 : 0);
126186
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
127187
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
128188
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
129189

130190
# Some web servers require the useragent to be not a bot. So we are liars.
131-
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36');
191+
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
132192
curl_setopt($ch, CURLOPT_HTTPHEADER, [
133193
"Accept: image/webp,image/*,*/*;q=0.8",
134194
"Cache-Control: max-age=0",

0 commit comments

Comments
 (0)