NXS_XMLRPC_ClientSSL

Client for communicating with a XML-RPC Server over HTTPS.

Defined (1)

The class is defined in the following location(s).

/inc-cl/apis/xmlrpc-client.php  
  1. class NXS_XMLRPC_ClientSSL extends NXS_XMLRPC_Client 
  2. /** 
  3. * Filename of the SSL Client Certificate 
  4. * @access private 
  5. * @since 0.1.0 
  6. * @var string 
  7. */ 
  8. var $_certFile; 
  9.  
  10. /** 
  11. * Filename of the SSL CA Certificate 
  12. * @access private 
  13. * @since 0.1.0 
  14. * @var string 
  15. */ 
  16. var $_caFile; 
  17.  
  18. /** 
  19. * Filename of the SSL Client Private Key 
  20. * @access private 
  21. * @since 0.1.0 
  22. * @var string 
  23. */ 
  24. var $_keyFile; 
  25.  
  26. /** 
  27. * Passphrase to unlock the private key 
  28. * @access private 
  29. * @since 0.1.0 
  30. * @var string 
  31. */ 
  32. var $_passphrase; 
  33.  
  34. /** 
  35. * Constructor 
  36. * @param string $server URL of the Server to connect to 
  37. * @since 0.1.0 
  38. */ 
  39. function NXS_XMLRPC_ClientSSL($server, $path = false, $port = 443, $timeout = false) 
  40. parent::NXS_XMLRPC_Client($server, $path, $port, $timeout); 
  41. $this->useragent = 'The Incutio XML-RPC PHP Library for SSL'; 
  42.  
  43. // Set class fields 
  44. $this->_certFile=false; 
  45. $this->_caFile=false; 
  46. $this->_keyFile=false; 
  47. $this->_passphrase=''; 
  48.  
  49. /** 
  50. * Set the client side certificates to communicate with the server. 
  51. * @since 0.1.0 
  52. * @param string $certificateFile Filename of the client side certificate to use 
  53. * @param string $keyFile Filename of the client side certificate's private key 
  54. * @param string $keyPhrase Passphrase to unlock the private key 
  55. */ 
  56. function setCertificate($certificateFile, $keyFile, $keyPhrase='') 
  57. // Check the files all exist 
  58. if (is_file($certificateFile)) { 
  59. $this->_certFile = $certificateFile; 
  60. } else { 
  61. die('Could not open certificate: ' . $certificateFile); 
  62.  
  63. if (is_file($keyFile)) { 
  64. $this->_keyFile = $keyFile; 
  65. } else { 
  66. die('Could not open private key: ' . $keyFile); 
  67.  
  68. $this->_passphrase=(string)$keyPhrase; 
  69.  
  70. function setCACertificate($caFile) 
  71. if (is_file($caFile)) { 
  72. $this->_caFile = $caFile; 
  73. } else { 
  74. die('Could not open CA certificate: ' . $caFile); 
  75.  
  76. /** 
  77. * Sets the connection timeout (in seconds) 
  78. * @param int $newTimeOut Timeout in seconds 
  79. * @returns void 
  80. * @since 0.1.2 
  81. */ 
  82. function setTimeOut($newTimeOut) 
  83. $this->timeout = (int)$newTimeOut; 
  84.  
  85. /** 
  86. * Returns the connection timeout (in seconds) 
  87. * @returns int 
  88. * @since 0.1.2 
  89. */ 
  90. function getTimeOut() 
  91. return $this->timeout; 
  92.  
  93. /** 
  94. * Set the query to send to the XML-RPC Server 
  95. * @since 0.1.0 
  96. */ 
  97. function query() 
  98. $args = func_get_args(); 
  99. $method = array_shift($args); 
  100. $request = new NXS_XMLRPC_Request($method, $args); 
  101. $length = $request->getLength(); 
  102. $xml = $request->getXml(); 
  103.  
  104. if ($this->debug) { 
  105. echo '<pre>'.htmlspecialchars($xml)."\n</pre>\n\n"; 
  106.  
  107. //This is where we deviate from the normal query() 
  108. //Rather than open a normal sock, we will actually use the cURL 
  109. //extensions to make the calls, and handle the SSL stuff. 
  110.  
  111. //Since 04Aug2004 (0.1.3) - Need to include the port (duh...) 
  112. //Since 06Oct2004 (0.1.4) - Need to include the colon!!! 
  113. // (I swear I've fixed this before... ESP in live... But anyhu...) 
  114. $curl=curl_init('https://' . $this->server . ':' . $this->port . $this->path); 
  115. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  116.  
  117. //Since 23Jun2004 (0.1.2) - Made timeout a class field 
  118. curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); 
  119.  
  120. if ($this->debug) { 
  121. curl_setopt($curl, CURLOPT_VERBOSE, 1); 
  122.  
  123. curl_setopt($curl, CURLOPT_HEADER, 1); 
  124. curl_setopt($curl, CURLOPT_POST, 1); 
  125. curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); 
  126. curl_setopt($curl, CURLOPT_PORT, $this->port); 
  127. curl_setopt($curl, CURLOPT_HTTPHEADER, array( 
  128. "Content-Type: text/xml",  
  129. "Content-length: {$length}")); 
  130.  
  131. // Process the SSL certificates, etc. to use 
  132. if (!($this->_certFile === false)) { 
  133. // We have a certificate file set, so add these to the cURL handler 
  134. curl_setopt($curl, CURLOPT_SSLCERT, $this->_certFile); 
  135. curl_setopt($curl, CURLOPT_SSLKEY, $this->_keyFile); 
  136.  
  137. if ($this->debug) { 
  138. echo "SSL Cert at : " . $this->_certFile . "\n"; 
  139. echo "SSL Key at : " . $this->_keyFile . "\n"; 
  140.  
  141. // See if we need to give a passphrase 
  142. if (!($this->_passphrase === '')) { 
  143. curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $this->_passphrase); 
  144.  
  145. if ($this->_caFile === false) { 
  146. // Don't verify their certificate, as we don't have a CA to verify against 
  147. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
  148. } else { 
  149. // Verify against a CA 
  150. curl_setopt($curl, CURLOPT_CAINFO, $this->_caFile); 
  151. global $nxs_skipSSLCheck; if ($nxs_skipSSLCheck===true) curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  152. // Call cURL to do it's stuff and return us the content 
  153. $contents = curl_exec($curl); $err = curl_errno($curl); $errmsg = curl_error($curl); 
  154. curl_close($curl); 
  155.  
  156. // Check for 200 Code in $contents 
  157. if (!strstr($contents, '200 OK')) { 
  158. //There was no "200 OK" returned - we failed 
  159. $this->error = new NXS_XMLRPC_Error(-32300, 'transport error - HTTP status code was not 200! - '.$err.' - '.$errmsg.' | '.print_r($contents, true)); 
  160. return false; 
  161.  
  162. if ($this->debug) { 
  163. echo '<pre>'.htmlspecialchars($contents)."\n</pre>\n\n"; 
  164. // Now parse what we've got back 
  165. // Since 20Jun2004 (0.1.1) - We need to remove the headers first 
  166. // Why I have only just found this, I will never know... 
  167. // So, remove everything before the first < 
  168. $contents = substr($contents, strpos($contents, '<')); 
  169.  
  170. $this->message = new NXS_XMLRPC_Message($contents); 
  171. if (!$this->message->parse()) { 
  172. // XML error 
  173. $this->error = new NXS_XMLRPC_Error(-32700, 'parse error. not well formed'); 
  174. return false; 
  175. // Is the message a fault? 
  176. if ($this->message->messageType == 'fault') { 
  177. $this->error = new NXS_XMLRPC_Error($this->message->faultCode, $this->message->faultString); 
  178. return false; 
  179.  
  180. // Message must be OK 
  181. return true;