This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace rOpenDev\curl; |
||
3 | |||
4 | /** |
||
5 | * PHP POO cURL wrapper |
||
6 | * Make it easy to request a URL (or few) |
||
7 | * PSR-2 Coding Style, PSR-4 Autoloading |
||
8 | * |
||
9 | * @author Robin <[email protected]> http://www.robin-d.fr/ |
||
10 | * @link https://github.com/RobinDev/curlRequest |
||
11 | * @since File available since Release 2014.04.29 |
||
12 | */ |
||
13 | class CurlRequest |
||
14 | { |
||
15 | /** |
||
16 | * If set to true (via self::setReturnHeaderOnly()), headers only are returned (via self::execute()) |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $headerOnly = false; |
||
20 | |||
21 | /** |
||
22 | * Curl resource handle |
||
23 | * @var resource |
||
24 | */ |
||
25 | public static $ch; |
||
26 | |||
27 | /* |
||
28 | * If set to true (via self::setEncodingGzip($gzip)), self::execute() will try to uncompress cURL output |
||
29 | * @var bool |
||
30 | */ |
||
31 | protected $gzip = false; |
||
32 | |||
33 | /** |
||
34 | * If set to true (via self::setReturnHeader()), self::execute() will extract HTTP header |
||
35 | * from the cURL output and stock it in self::$header wich can be get with self::getHeader() |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $rHeader = false; |
||
39 | |||
40 | /** |
||
41 | * Contain (after self::execute()) header returned by curl request |
||
42 | * @var string $header |
||
43 | */ |
||
44 | protected $header; |
||
45 | |||
46 | /** |
||
47 | * Constructor |
||
48 | * |
||
49 | * @param string $url The URL to request |
||
50 | * @param bool $usePreviousSession If the query must use the previous session (so using same connexion if it's the same host) |
||
51 | */ |
||
52 | public function __construct($url, $usePreviousSession = false) |
||
53 | { |
||
54 | if ($usePreviousSession === false || !isset(self::$ch)) { |
||
55 | self::$ch = curl_init($url); |
||
56 | } else { |
||
57 | curl_reset(self::$ch); |
||
58 | $this->setOpt(CURLOPT_URL, $url); |
||
59 | } |
||
60 | $this->setOpt(CURLOPT_RETURNTRANSFER, 1); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Change the URL to cURL |
||
65 | * |
||
66 | * @param string $url URL to cURL |
||
67 | * @param bool $reset True if you want to remove cURLs params setted before calling this function |
||
68 | * |
||
69 | * @return self |
||
70 | */ |
||
71 | public function setUrl($url, $reset = false) |
||
72 | { |
||
73 | if ($reset) { |
||
74 | curl_reset(self::$ch); |
||
75 | } |
||
76 | $this->setOpt(CURLOPT_URL, $url); |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Add a cURL's option |
||
83 | * |
||
84 | * @param int $option cURL Predefined Constant |
||
85 | * @param mixed $value |
||
86 | * |
||
87 | * @return self |
||
88 | */ |
||
89 | public function setOpt($option, $value) |
||
90 | { |
||
91 | curl_setopt(self::$ch, $option, $value); |
||
92 | |||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * A short way to set some classic options to cURL a web page |
||
98 | * |
||
99 | * @return self |
||
100 | */ |
||
101 | public function setDefaultGetOptions($connectTimeOut = 5, $timeOut = 10, $dnsCacheTimeOut = 600, $followLocation = true, $maxRedirs = 5) |
||
102 | { |
||
103 | $this->setOpt(CURLOPT_AUTOREFERER, 1) |
||
104 | ->setOpt(CURLOPT_FOLLOWLOCATION, $followLocation) |
||
105 | ->setOpt(CURLOPT_MAXREDIRS, $maxRedirs) |
||
106 | ->setOpt(CURLOPT_CONNECTTIMEOUT, $connectTimeOut) |
||
107 | ->setOpt(CURLOPT_DNS_CACHE_TIMEOUT, $dnsCacheTimeOut) |
||
108 | ->setOpt(CURLOPT_TIMEOUT, $timeOut) |
||
109 | ->setOpt(CURLOPT_SSL_VERIFYPEER, 0); |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * A short way to set some classic options to cURL a web page quickly (but lossing some data like header, cookie...) |
||
116 | * |
||
117 | * @return self |
||
118 | */ |
||
119 | public function setDefaultSpeedOptions() |
||
120 | { |
||
121 | $this->setOpt(CURLOPT_SSL_VERIFYHOST, 0) |
||
122 | ->setOpt(CURLOPT_SSL_VERIFYPEER, 0) |
||
123 | ->setOpt(CURLOPT_HEADER, 0) |
||
124 | ->setOpt(CURLOPT_COOKIE, 0) |
||
125 | ->setOpt(CURLOPT_MAXREDIRS, 1); |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Call it if you want header informations. |
||
132 | * After self::execute(), you would have this informations with getHeader(); |
||
133 | * |
||
134 | * @return self |
||
135 | */ |
||
136 | public function setReturnHeader() |
||
137 | { |
||
138 | $this->setOpt(CURLOPT_HEADER, 1); |
||
139 | $this->rHeader = true; |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Call it if you want header informations only. |
||
146 | * After self::execute(), you would have this informations with getHeader(); |
||
147 | * |
||
148 | * @return self |
||
149 | */ |
||
150 | public function setReturnHeaderOnly() |
||
151 | { |
||
152 | $this->headerOnly = true; |
||
153 | $this->setOpt(CURLOPT_HEADER, 1) |
||
154 | ->setOpt(CURLOPT_NOBODY, 1); |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * An self::setOpt()'s alias to add a cookie to your request |
||
161 | * |
||
162 | * @param string $cookie |
||
163 | * |
||
164 | * @return self |
||
165 | */ |
||
166 | public function setCookie($cookie) |
||
167 | { |
||
168 | $this->setOpt(CURLOPT_COOKIE, $cookie); |
||
169 | |||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * An self::setOpt()'s alias to add a referrer to your request |
||
175 | * |
||
176 | * @param string $referrer |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | public function setReferrer($referrer) |
||
181 | { |
||
182 | $this->setOpt(CURLOPT_REFERER, $referrer); |
||
183 | |||
184 | return $this; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * An self::setOpt()'s alias to add an user-agent to your request |
||
189 | * |
||
190 | * @param string $ua |
||
191 | * |
||
192 | * @return self |
||
193 | */ |
||
194 | public function setUserAgent($ua) |
||
195 | { |
||
196 | $this->setOpt(CURLOPT_USERAGENT, $ua); |
||
197 | |||
198 | return $this; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * An self::setUserAgent()'s alias to add an user-agent wich correspond to a Destkop PC |
||
203 | * |
||
204 | * @return self |
||
205 | */ |
||
206 | public function setDestkopUserAgent() |
||
207 | { |
||
208 | $this->setUserAgent('Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0'); |
||
209 | |||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * An self::setUserAgent()'s alias to add an user-agent wich correspond to a mobile |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | public function setMobileUserAgent() |
||
219 | { |
||
220 | $this->setUserAgent('Mozilla/5.0 (Linux; Android 7.0; SM-G892A Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/60.0.3112.107 Mobile Safari/537.36'); |
||
221 | |||
222 | return $this; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * An self::setUserAgent()'s alias to add an user-agent wich correspond to a webrowser without javascript |
||
227 | * |
||
228 | * @return self |
||
229 | */ |
||
230 | public function setLessJsUserAgent() |
||
231 | { |
||
232 | $this->setUserAgent('NokiaN70-1/5.0609.2.0.1 Series60/2.8 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Link/6.3.1.13.0'); |
||
233 | |||
234 | return $this; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * A short way to set post's options to cURL a web page |
||
239 | * |
||
240 | * @param array $post_array Contain data (key=>vvalue) to post |
||
241 | * |
||
242 | * @return self |
||
243 | */ |
||
244 | public function setPost($post_array) |
||
245 | { |
||
246 | $this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST'); |
||
247 | $this->setOpt(CURLOPT_POST, 1); |
||
248 | $this->setOpt(CURLOPT_POSTFIELDS, http_build_query($post_array)); |
||
249 | |||
250 | return $this; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * If you want to request the URL and hope get the result gzipped. |
||
255 | * The output will be automatically uncompress with execute(); |
||
256 | * |
||
257 | * @return self |
||
258 | */ |
||
259 | public function setEncodingGzip($decode = false) |
||
260 | { |
||
261 | $this->setOpt(CURLOPT_ENCODING, 'gzip, deflate'); |
||
262 | $this->gzip = $decode; |
||
263 | |||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * If you want to request the URL with a (http|socks...) proxy (public or private) |
||
269 | * |
||
270 | * @param string $proxy [scheme]IP:PORT[:LOGIN:PASSWORD] (Eg. : socks5://98.023.023.02:1098:cUrlRequestProxId:SecretPassword) |
||
271 | * |
||
272 | * @return self |
||
273 | */ |
||
274 | public function setProxy($proxy) |
||
275 | { |
||
276 | if (!empty($proxy)) { |
||
277 | $scheme = self::getSchemeFrom($proxy); |
||
278 | $proxy = explode(':', $proxy); |
||
279 | $this->setOpt(CURLOPT_HTTPPROXYTUNNEL, 1); |
||
280 | $this->setOpt(CURLOPT_PROXY, $scheme.$proxy[0].':'.$proxy[1]); |
||
281 | if (isset($proxy[2])) { |
||
282 | $this->setOpt(CURLOPT_PROXYUSERPWD, $proxy[2].':'.$proxy[3]); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | return $this; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Return scheme from proxy string and remove Scheme From proxy |
||
291 | * |
||
292 | * @param string $proxy |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | protected static function getSchemeFrom(&$proxy) |
||
297 | { |
||
298 | if (!preg_match('@^([a-z0-9]*)://@', $proxy, $match)) { |
||
299 | return 'http://'; |
||
300 | } |
||
301 | |||
302 | $scheme = $match[1].'://'; |
||
303 | $proxy = str_replace($scheme, '', $proxy); |
||
304 | |||
305 | return $scheme; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Execute the request |
||
310 | * |
||
311 | * @return string wich is the request's result without the header (you can obtain with self::getHeader() now) |
||
312 | */ |
||
313 | public function execute() |
||
314 | { |
||
315 | if ($this->headerOnly) { |
||
316 | return $this->header = curl_exec(self::$ch); |
||
317 | } |
||
318 | $html = curl_exec(self::$ch); |
||
319 | |||
320 | if ($this->gzip && self::gzdecode($html)) { |
||
321 | $html = self::gzdecode($html); |
||
322 | } |
||
323 | |||
324 | if ($this->rHeader) { |
||
325 | $this->header = substr($html, 0, $sHeader = curl_getinfo(self::$ch, CURLINFO_HEADER_SIZE)); |
||
326 | $html = substr($html, $sHeader); |
||
327 | } |
||
328 | |||
329 | return $html; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Return header's data return by the request |
||
334 | * |
||
335 | * @param bool $arrayFormatted True to get an array, false to get a string |
||
336 | * |
||
337 | * @return array|string containing header's data |
||
338 | */ |
||
339 | public function getHeader($arrayFormatted = true) |
||
340 | { |
||
341 | if (isset($this->header)) { |
||
342 | return $arrayFormatted === true ? self::http_parse_headers($this->header) : $this->header; |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Return current effective url |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | function getEffectiveUrl() |
||
0 ignored issues
–
show
|
|||
352 | { |
||
353 | return curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Return the cookie(s) returned by the request (if there are) |
||
358 | * |
||
359 | * @return null|array containing the cookies |
||
360 | */ |
||
361 | public function getCookies() |
||
362 | { |
||
363 | if (isset($this->header)) { |
||
364 | $header = $this->getHeader(); |
||
365 | if (isset($header['Set-Cookie'])) { |
||
366 | return is_array($header['Set-Cookie']) ? implode('; ', $header['Set-Cookie']) : $header['Set-Cookie']; |
||
367 | } |
||
368 | } |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Return the last error number (curl_errno) |
||
373 | * |
||
374 | * @return int the error number or 0 (zero) if no error occurred. |
||
375 | */ |
||
376 | public function hasError() |
||
377 | { |
||
378 | return curl_errno(self::$ch); |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Return a string containing the last error for the current session (curl_error) |
||
383 | * |
||
384 | * @return string the error message or '' (the empty string) if no error occurred. |
||
385 | */ |
||
386 | public function getErrors() |
||
387 | { |
||
388 | return curl_error(self::$ch); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Get information regarding the request |
||
393 | * |
||
394 | * @return bool|array an associative array with the following elements (which correspond to opt), or FALSE on failure |
||
395 | */ |
||
396 | public function getInfo() |
||
397 | { |
||
398 | return curl_getinfo(self::$ch); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Close the connexion |
||
403 | * Call curl_reset function |
||
404 | */ |
||
405 | public function close() |
||
406 | { |
||
407 | curl_reset(self::$ch); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Parse HTTP headers (php HTTP functions but generally, this packet isn't installed) |
||
412 | * @source http://www.php.net/manual/en/function.http-parse-headers.php#112917 |
||
413 | * |
||
414 | * @param string $raw_headers Contain HTTP headers |
||
415 | * |
||
416 | * @return bool|array an array on success or FALSE on failure. |
||
417 | */ |
||
418 | public static function http_parse_headers($raw_headers) |
||
419 | { |
||
420 | if (function_exists('http_parse_headers')) { |
||
421 | http_parse_headers($raw_headers); |
||
422 | } |
||
423 | |||
424 | $headers = []; |
||
425 | $key = ''; |
||
426 | |||
427 | foreach (explode("\n", $raw_headers) as $i => $h) { |
||
428 | $h = explode(':', $h, 2); |
||
429 | |||
430 | if (isset($h[1])) { |
||
431 | if (!isset($headers[$h[0]])) { |
||
432 | $headers[$h[0]] = trim($h[1]); |
||
433 | } elseif (is_array($headers[$h[0]])) { |
||
434 | $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); |
||
435 | } else { |
||
436 | $headers[$h[0]] = array_merge([$headers[$h[0]]], [trim($h[1])]); |
||
437 | } |
||
438 | |||
439 | $key = $h[0]; |
||
440 | } else { |
||
441 | if (substr($h[0], 0, 1) == "\t") { |
||
442 | $headers[$key] .= "\r\n\t".trim($h[0]); |
||
443 | } elseif (!$key) { |
||
444 | $headers[0] = trim($h[0]); |
||
445 | } |
||
446 | trim($h[0]); |
||
447 | } |
||
448 | } |
||
449 | |||
450 | return $headers; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Decode a string |
||
455 | * |
||
456 | * @param string $str String to decode |
||
457 | */ |
||
458 | public static function gzdecode($str) |
||
459 | { |
||
460 | return gzinflate(substr($str, 10, -8)); |
||
461 | } |
||
462 | } |
||
463 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.