Advertisement
Guest User

Untitled

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