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
|
4 |
|
public function __construct(Data $data, int $timeout = 10) |
|
|
|
|
34
|
|
|
{ |
|
|
|
|
35
|
4 |
|
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
|
4 |
|
$this->_curl = new PhpCurl(); |
|
|
|
|
42
|
4 |
|
$this->_data = $data; |
|
|
|
|
43
|
4 |
|
$this->_timeout = $timeout; |
44
|
4 |
|
$this->_setDefaultHeaders(); |
45
|
4 |
|
} |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute a CURL request, and return current object for further processing. |
49
|
|
|
* |
50
|
|
|
* @return PhpCurl |
51
|
|
|
*/ |
52
|
3 |
|
public function execute(): PhpCurl |
53
|
|
|
{ |
|
|
|
|
54
|
3 |
|
$this->_curl->setOpt(CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
55
|
3 |
|
$this->_curl->setOpt(CURLOPT_CONNECTTIMEOUT, $this->_timeout); |
56
|
3 |
|
$this->_curl->setOpt(CURLOPT_HTTPHEADER, $this->_defaultHeaders); |
57
|
3 |
|
$this->_curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
|
58
|
3 |
|
$this->_curl->setOpt(CURLOPT_SSL_VERIFYHOST, false); |
|
|
|
|
59
|
3 |
|
$this->_curl->get($this->_data->shodanApiUrl()); |
60
|
|
|
|
61
|
3 |
|
return $this->_curl; |
62
|
|
|
} |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the data retrieved from executing CURL request, in JSON format. |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
* |
69
|
|
|
* @see \RattfieldNz\SafeUrls\Libraries\Curl\Curl->execute(). |
70
|
|
|
*/ |
71
|
3 |
|
public function getData() |
72
|
|
|
{ |
|
|
|
|
73
|
3 |
|
$dataObject = $this->execute(); |
74
|
|
|
|
75
|
3 |
|
$status = $dataObject->getHttpStatus(); |
76
|
|
|
|
77
|
3 |
|
$response = $dataObject->error_code != 0 ? |
|
|
|
|
78
|
1 |
|
['message' => $dataObject->error_message] : |
|
|
|
|
79
|
3 |
|
json_decode($dataObject->response, true); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$data = [ |
|
|
|
|
82
|
3 |
|
'status' => $status, |
|
|
|
|
83
|
3 |
|
'response' => $response, |
|
|
|
|
84
|
|
|
]; |
|
|
|
|
85
|
|
|
|
86
|
3 |
|
return $data; |
87
|
|
|
} |
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the default headers to use for CURL request. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
4 |
|
private function _setDefaultHeaders(): void |
95
|
|
|
{ |
|
|
|
|
96
|
4 |
|
$this->_defaultHeaders = [ |
|
|
|
|
97
|
|
|
'Content-Type: application/json', |
|
|
|
|
98
|
|
|
'Connection: Keep-Alive', |
|
|
|
|
99
|
|
|
]; |
|
|
|
|
100
|
4 |
|
} |
|
|
|
|
101
|
|
|
} |
|
|
|
|
102
|
|
|
|