-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsslLabsApi.php
216 lines (194 loc) · 4.7 KB
/
sslLabsApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* PHP-SSLLabs-API
*
* This PHP library provides basic access to the SSL Labs API
* and is build upon the official API documentation at
* https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
*
* @author Björn Roland <https://github.com/bjoernr-de>
* @license GNU GENERAL PUBLIC LICENSE v3
*/
class sslLabsApi
{
CONST API_URL = "https://api.ssllabs.com/api/v2";
private $returnJsonObjects;
/**
* sslLabsApi::__construct()
*/
public function __construct($returnJsonObjects = false)
{
$this->returnJsonObjects = (boolean) $returnJsonObjects;
}
/**
* sslLabsApi::fetchApiInfo()
*
* API Call: info
* @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
*/
public function fetchApiInfo()
{
return ($this->sendApiRequest('info'));
}
/**
* sslLabsApi::fetchHostInformation()
*
* API Call: analyze
* @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
*
* @param string $host Hostname to analyze
* @param boolean $publish
* @param boolean $startNew
* @param boolean $fromCache
* @param int $maxAge
* @param string $all
* @param boolean $ignoreMismatch
*/
public function fetchHostInformation($host, $publish = false, $startNew = false, $fromCache = false, $maxAge = NULL, $all = NULL, $ignoreMismatch = false)
{
$apiRequest = $this->sendApiRequest
(
'analyze',
array
(
'host' => $host,
'publish' => $publish,
'startNew' => $startNew,
'fromCache' => $fromCache,
'maxAge' => $maxAge,
'all' => $all,
'ignoreMismatch' => $ignoreMismatch
)
);
return ($apiRequest);
}
/**
* sslLabsApi::fetchHostInformationCached()
*
* API Call: analyze
* Same as fetchHostInformation() but prefer to receive cached information
*
* @param string $host
* @param int $maxAge
* @param string $publish
* @param string $ignoreMismatch
*/
public function fetchHostInformationCached($host, $maxAge, $publish = false, $ignoreMismatch = false)
{
return($this->fetchHostInformation($host, $publish, false, true, $maxAge, 'done', $ignoreMismatch));
}
/**
* sslLabsApi::fetchEndpointData()
*
* API Call: getEndpointData
* @see https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs.md
*
* @param string $host
* @param string $s
* @param string $fromCache
* @return string
*/
public function fetchEndpointData($host, $s, $fromCache = false)
{
$apiRequest = $this->sendApiRequest
(
'getEndpointData',
array
(
'host' => $host,
's' => $s,
'fromCache' => $fromCache
)
);
return ($apiRequest);
}
/**
* sslLabsApi::fetchStatusCodes()
*
* API Call: getStatusCodes
*/
public function fetchStatusCodes()
{
return ($this->sendApiRequest('getStatusCodes'));
}
/**
* sslLabsApi::sendApiRequest()
*
* Send API request
*
* @param string $apiCall
* @param array $parameters
* @return string JSON from API
*/
public function sendApiRequest($apiCall, $parameters = array())
{
//we also want content from failed api responses
$context = stream_context_create
(
array
(
'http' => array
(
'ignore_errors' => true
)
)
);
$apiResponse = file_get_contents(self::API_URL . '/' . $apiCall . $this->buildGetParameterString($parameters), false, $context);
if($this->returnJsonObjects)
{
return (json_decode($apiResponse));
}
return ($apiResponse);
}
/**
* sslLabsApi::setReturnJsonObjects()
*
* Setter for returnJsonObjects
* Set true to return all API responses as JSON object, false returns it as simple JSON strings (default)
*
* @param boolean $returnJsonObjects
*/
public function setReturnJsonObjects($returnJsonObjects)
{
$this->returnJsonObjects = (boolean) $returnJsonObjects;
}
/**
* sslLabsApi::getReturnJsonObjects()
*
* Getter for returnJsonObjects
*
* @return boolean true returns all API responses as JSON object, false returns it as simple JSON string
*/
public function getReturnJsonObjects()
{
return ($this->returnJsonObjects);
}
/**
* sslLabsApi::buildGetParameterString()
*
* Helper function to build get parameter string for URL
*
* @param array $parameters
* @return string
*/
private function buildGetParameterString($parameters)
{
$string = '';
$counter = 0;
foreach($parameters as $name => $value)
{
if(!is_string($name) || (!is_string($value) && !is_bool($value) && !is_int($value)))
{
continue;
}
if(is_bool($value))
{
$value = ($value) ? 'on' : 'off';
}
$string .= ($counter == 0) ? '?' : '&';
$string .= urlencode($name) . '=' . urlencode($value);
$counter++;
}
return ($string);
}
}