1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace RattfieldNz\Shodan\Libraries\Curl; |
4
|
|
|
|
5
|
|
|
use Curl\Curl as PhpCurl; |
6
|
|
|
use RattfieldNz\Shodan\Libraries\Data\Data; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Curl. |
10
|
|
|
* |
11
|
|
|
* @category PHP |
|
|
|
|
12
|
|
|
* |
13
|
|
|
* @author Rob Attfield <[email protected]> |
|
|
|
|
14
|
|
|
* @license https://github.com/rattfieldnz/shodan/blob/master/LICENSE MIT |
|
|
|
|
15
|
|
|
*/ |
|
|
|
|
16
|
|
|
class Curl |
17
|
|
|
{ |
|
|
|
|
18
|
|
|
private $_data; |
|
|
|
|
19
|
|
|
private $_defaultHeaders; |
|
|
|
|
20
|
|
|
private $_timeout; |
|
|
|
|
21
|
|
|
private $_curl; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* Curl constructor. |
25
|
|
|
* |
26
|
|
|
* Set the needed properties to do a CURL request. |
27
|
|
|
* |
28
|
|
|
* @param Data $data Data to use when executing cURL. |
|
|
|
|
29
|
|
|
* @param int $timeout Timeout in seconds to complete a CURL request. Default is 10. |
|
|
|
|
30
|
|
|
* |
31
|
|
|
* @throws \ErrorException Will throw an exception if PHP ext-curl is not installed. |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
public function __construct(Data $data, int $timeout = 10) |
|
|
|
|
34
|
|
|
{ |
|
|
|
|
35
|
|
|
if (!extension_loaded('curl')) { |
|
|
|
|
36
|
|
|
throw new \ErrorException( |
|
|
|
|
37
|
|
|
'The cURL extensions is not loaded, make sure you have installed the cURL extension: https://php.net/manual/curl.setup.php' |
|
|
|
|
38
|
|
|
); |
39
|
|
|
} |
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->_curl = new PhpCurl(); |
|
|
|
|
42
|
|
|
$this->_data = $data; |
|
|
|
|
43
|
|
|
$this->_timeout = $timeout; |
|
|
|
|
44
|
|
|
$this->_setDefaultHeaders(); |
|
|
|
|
45
|
|
|
} |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* Execute a CURL request, and return current object for further processing. |
49
|
|
|
* |
50
|
|
|
* @return PhpCurl |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function execute(): PhpCurl |
|
|
|
|
53
|
|
|
{ |
|
|
|
|
54
|
|
|
$this->_curl->setOpt(CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
55
|
|
|
$this->_curl->setOpt(CURLOPT_CONNECTTIMEOUT, $this->_timeout); |
|
|
|
|
56
|
|
|
$this->_curl->setOpt(CURLOPT_HTTPHEADER, $this->_defaultHeaders); |
|
|
|
|
57
|
|
|
$this->_curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
|
58
|
|
|
$this->_curl->setOpt(CURLOPT_SSL_VERIFYHOST, false); |
|
|
|
|
59
|
|
|
$this->_curl->get($this->_data->shodanApiUrl()); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $this->_curl; |
|
|
|
|
62
|
|
|
} |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* Get the data retrieved from executing CURL request, in JSON format. |
66
|
|
|
* |
67
|
|
|
* @return string |
|
|
|
|
68
|
|
|
* |
69
|
|
|
* @see \RattfieldNz\SafeUrls\Libraries\Curl\Curl->execute(). |
|
|
|
|
70
|
|
|
*/ |
71
|
|
|
public function getData() |
|
|
|
|
72
|
|
|
{ |
|
|
|
|
73
|
|
|
$dataObject = $this->execute(); |
|
|
|
|
74
|
|
|
$data = [ |
|
|
|
|
75
|
|
|
'status' => $dataObject->getHttpStatus(), |
|
|
|
|
76
|
|
|
'response' => json_decode($dataObject->response, true), |
|
|
|
|
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
return json_encode($data); |
|
|
|
|
80
|
|
|
} |
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* Sets the default headers to use for CURL request. |
84
|
|
|
* |
85
|
|
|
* @return void |
|
|
|
|
86
|
|
|
*/ |
87
|
|
|
private function _setDefaultHeaders(): void |
|
|
|
|
88
|
|
|
{ |
|
|
|
|
89
|
|
|
$this->_defaultHeaders = [ |
|
|
|
|
90
|
|
|
'Content-Type: application/json', |
|
|
|
|
91
|
|
|
'Connection: Keep-Alive', |
|
|
|
|
92
|
|
|
]; |
93
|
|
|
} |
|
|
|
|
94
|
|
|
} |
|
|
|
|
95
|
|
|
|