PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ezsoap/classes/ezsoapclient.php

https://github.com/eeggenberger/ezpublish
PHP | 270 lines | 165 code | 23 blank | 82 comment | 28 complexity | 37739a9654362fc5f7f5e73b3232b5d6 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZSOAPClient class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZSOAPClient ezsoapclient.php
  12. \ingroup eZSOAP
  13. \brief eZSOAPClient is a class which can be used as a SOAP client
  14. eZSOAPClient handles communication with a SOAP server.
  15. \code
  16. // create a new client
  17. $client = new eZSOAPClient( "nextgen.bf.dvh1.ez.no", "/sdk/ezsoap/view/server" );
  18. $namespace = "http://soapinterop.org/";
  19. // create the SOAP request object
  20. $request = new eZSOAPRequest( "addNumbers", "http://calkulator.com/simplecalculator" );
  21. // add parameters to the request
  22. $request->addParameter( "valueA", 42 );
  23. $request->addParameter( "valueB", 17 );
  24. // send the request to the server and fetch the response
  25. $response = $client->send( $request );
  26. // check if the server returned a fault, if not print out the result
  27. if ( $response->isFault() )
  28. {
  29. print( "SOAP fault: " . $response->faultCode(). " - " . $response->faultString() . "" );
  30. }
  31. else
  32. print( "Returned SOAP value was: \"" . $response->value() . "\"" );
  33. \endcode
  34. \sa eZSOAPServer eZSOAPRequest eZSOAPResponse
  35. */
  36. class eZSOAPClient
  37. {
  38. /*!
  39. Creates a new SOAP client.
  40. \param $server The remote server to connect to
  41. \param $path The path to the SOAP service on the remote server
  42. \param $port The port to connect to, 80 by default. You can use 'ssl' as well to specify that you want to use port 443 over SSL,
  43. but omit the last parameter $useSSL of this method then or set it to true. When $port equals 443, SSL will also be
  44. used if $useSSL is omitted or set to true.
  45. \param $useSSL If we need to connect to the remote server with (https://) or without (http://) SSL
  46. */
  47. function eZSOAPClient( $server, $path = '/', $port = 80, $useSSL = null )
  48. {
  49. $this->Login = "";
  50. $this->Password = "";
  51. $this->Server = $server;
  52. $this->Path = $path;
  53. $this->Port = $port;
  54. if ( is_numeric( $port ) )
  55. {
  56. $this->Port = $port;
  57. if ( $port == 443 )
  58. {
  59. $this->UseSSL = true;
  60. }
  61. }
  62. elseif ( strtolower( $port ) == 'ssl' )
  63. {
  64. $this->UseSSL = true;
  65. $this->Port = 443;
  66. }
  67. else
  68. {
  69. $this->Port = 80;
  70. }
  71. if ( $useSSL === true )
  72. {
  73. $this->UseSSL = true;
  74. }
  75. else if ( $useSSL === false )
  76. {
  77. $this->UseSSL = false;
  78. }
  79. }
  80. /*!
  81. Sends a SOAP message and returns the response object.
  82. */
  83. function send( $request )
  84. {
  85. if ( !$this->UseSSL || !in_array( "curl", get_loaded_extensions() ) )
  86. {
  87. if ( $this->Timeout != 0 )
  88. {
  89. $fp = fsockopen( $this->Server,
  90. $this->Port,
  91. $this->errorNumber,
  92. $this->errorString,
  93. $this->Timeout );
  94. }
  95. else
  96. {
  97. $fp = fsockopen( $this->Server,
  98. $this->Port,
  99. $this->errorNumber,
  100. $this->errorString );
  101. }
  102. if ( $fp == 0 )
  103. {
  104. $this->ErrorString = '<b>Error:</b> eZSOAPClient::send() : Unable to open connection to ' . $this->Server . '.';
  105. return 0;
  106. }
  107. $payload = $request->payload();
  108. $authentification = "";
  109. if ( ( $this->login() != "" ) )
  110. {
  111. $authentification = "Authorization: Basic " . base64_encode( $this->login() . ":" . $this->password() ) . "\r\n" ;
  112. }
  113. $HTTPRequest = "POST " . $this->Path . " HTTP/1.0\r\n" .
  114. "User-Agent: eZ soap client\r\n" .
  115. "Host: " . $this->Server . ":" . $this->Port . "\r\n" .
  116. $authentification .
  117. "Content-Type: text/xml\r\n" .
  118. "SOAPAction: \"" . $request->ns() . '/' . $request->name() . "\"\r\n" .
  119. "Content-Length: " . strlen( $payload ) . "\r\n\r\n" .
  120. $payload;
  121. if ( !fputs( $fp, $HTTPRequest, strlen( $HTTPRequest ) ) )
  122. {
  123. $this->ErrorString = "<b>Error:</b> could not send the SOAP request. Could not write to the socket.";
  124. $response = 0;
  125. return $response;
  126. }
  127. $rawResponse = "";
  128. // fetch the SOAP response
  129. while ( $data = fread( $fp, 32768 ) )
  130. {
  131. $rawResponse .= $data;
  132. }
  133. // close the socket
  134. fclose( $fp );
  135. }
  136. else //SOAP With SSL
  137. {
  138. if ( $request instanceof eZSOAPRequest )
  139. {
  140. $URL = "https://" . $this->Server . ":" . $this->Port . $this->Path;
  141. $ch = curl_init ( $URL );
  142. if ( $this->Timeout != 0 )
  143. {
  144. curl_setopt( $ch, CURLOPT_TIMEOUT, $this->TimeOut );
  145. }
  146. $payload = $request->payload();
  147. if ( $ch != 0 )
  148. {
  149. $HTTPCall = "POST " . $this->Path . " HTTP/1.0\r\n" .
  150. "User-Agent: eZ soap client\r\n" .
  151. "Host: " . $this->Server . ":" . $this->Port . "\r\n" .
  152. "Content-Type: text/xml\r\n" .
  153. "SOAPAction: \"" . $request->ns() . '/' . $request->name() . "\"\r\n" .
  154. "Content-Length: " . strlen( $payload ) . "\r\n";
  155. if ( $this->login() != '' )
  156. {
  157. $HTTPCall .= "Authorization: Basic " . base64_encode( $this->login() . ":" . $this->Password() ) . "\r\n";
  158. }
  159. $HTTPCall .= "\r\n" . $payload;
  160. curl_setopt( $ch, CURLOPT_URL, $URL );
  161. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
  162. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 1 );
  163. curl_setopt( $ch, CURLOPT_HEADER, 1 );
  164. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  165. curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $HTTPCall ); // Don't use CURLOPT_CUSTOMREQUEST without making sure your server supports the custom request method first.
  166. unset( $rawResponse );
  167. if ( $ch != 0 )
  168. {
  169. $rawResponse = curl_exec( $ch );
  170. }
  171. if ( !$rawResponse )
  172. {
  173. $this->ErrorString = "<b>Error:</b> could not send the XML-SOAP with SSL call. Could not write to the socket.";
  174. $response = 0;
  175. return $response;
  176. }
  177. }
  178. curl_close( $ch );
  179. }
  180. }
  181. $response = new eZSOAPResponse();
  182. $response->decodeStream( $request, $rawResponse );
  183. return $response;
  184. }
  185. /*!
  186. Set timeout value
  187. \param timeout value in seconds. Set to 0 for unlimited.
  188. */
  189. function setTimeout( $timeout )
  190. {
  191. $this->Timeout = $timeout;
  192. }
  193. /*!
  194. Sets the HTTP login
  195. */
  196. function setLogin( $login )
  197. {
  198. $this->Login = $login;
  199. }
  200. /*!
  201. Returns the login, used for HTTP authentification
  202. */
  203. function login()
  204. {
  205. return $this->Login;
  206. }
  207. /*!
  208. Sets the HTTP password
  209. */
  210. function setPassword( $password )
  211. {
  212. $this->Password = $password;
  213. }
  214. /*!
  215. Returns the password, used for HTTP authentification
  216. */
  217. function password()
  218. {
  219. return $this->Password;
  220. }
  221. /// The name or IP of the server to communicate with
  222. public $Server;
  223. /// The path to the SOAP server
  224. public $Path;
  225. /// The port of the server to communicate with.
  226. public $Port;
  227. /// How long to wait for the call.
  228. public $Timeout = 0;
  229. /// HTTP login for HTTP authentification
  230. public $Login;
  231. /// HTTP password for HTTP authentification
  232. public $Password;
  233. private $UseSSL;
  234. }
  235. ?>