From 287b168a4595ea3f311ca8307c4517eaa209166e Mon Sep 17 00:00:00 2001 From: Dariusz Wrzesien Date: Fri, 10 Apr 2015 22:50:57 +0200 Subject: [PATCH 1/3] added PSR-2 format, protected for options and conditional for logger --- src/NTLMSoap/Client.php | 201 ++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 99 deletions(-) diff --git a/src/NTLMSoap/Client.php b/src/NTLMSoap/Client.php index d0fc055..92ac080 100644 --- a/src/NTLMSoap/Client.php +++ b/src/NTLMSoap/Client.php @@ -1,99 +1,102 @@ -setLogger($logger); - } - - $this->options = $data; - - if(empty($data['ntlm_username']) && empty($data['ntlm_password'])){ - parent::__construct($url, $data); - }else{ - $this->use_ntlm = true; - HttpStream\NTLM::$user = $data['ntlm_username']; - HttpStream\NTLM::$password = $data['ntlm_password']; - - stream_wrapper_unregister('http'); - if(!stream_wrapper_register('http', '\\NTLMSoap\\HttpStream\\NTLM')){ - throw new Exception("Unable to register HTTP Handler"); - } - - $time_start = microtime(true); - parent::__construct($url, $data); - - if(!empty($this->logger) && (($end_time = microtime(true) - $time_start) > 0.1)){ - $this->logger->debug("WSDL Timer", Array("time" => $end_time, "url" => $url)); - } - - stream_wrapper_restore('http'); - } - - } - - /** - * (non-PHPdoc) - * @see SoapClient::__doRequest() - */ - public function __doRequest($request, $location, $action, $version, $one_way=0) { - $this->__last_request = $request; - $start_time = microtime(true); - - $ch = curl_init($location); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - - curl_setopt($ch, CURLOPT_HTTPHEADER, array( - 'Method: POST', - 'User-Agent: PHP-SOAP-CURL', - 'Content-Type: text/xml; charset=utf-8', - 'SOAPAction: "' . $action . '"', - )); - - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $request); - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); - - if(!empty($this->options['ntlm_username']) && !empty($this->options['ntlm_password'])){ - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); - curl_setopt($ch, CURLOPT_USERPWD, $this->options['ntlm_username'].':'. $this->options['ntlm_password']); - } - - $response = curl_exec($ch); - - // Log as an error if the curl call isn't a success - $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); - $log_func = $http_status == 200 ? 'debug' : 'error'; - - // Log the call - $this->logger->$log_func("SoapCall: " . $action, [ - "Location" => $location, - "HttpStatus" => $http_status, - "Request" => $request, - "Response" => strlen($response) > 2000 ? substr($response, 0, 2000) . "..." : $response, - "RequestTime" => curl_getinfo($ch, CURLINFO_TOTAL_TIME), - "RequestConnectTime" => curl_getinfo($ch, CURLINFO_CONNECT_TIME), - "Time" => microtime(true) - $start_time - ]); - - return $response; - } - -} \ No newline at end of file +setLogger($logger); + } + + $this->options = $data; + + if (empty($data['ntlm_username']) && empty($data['ntlm_password'])) { + parent::__construct($url, $data); + } else { + $this->use_ntlm = true; + HttpStream\NTLM::$user = $data['ntlm_username']; + HttpStream\NTLM::$password = $data['ntlm_password']; + + stream_wrapper_unregister('http'); + if (! stream_wrapper_register('http', '\\NTLMSoap\\HttpStream\\NTLM')) { + throw new Exception("Unable to register HTTP Handler"); + } + + $time_start = microtime(true); + parent::__construct($url, $data); + + if (! empty($this->logger) && (($end_time = microtime(true) - $time_start) > 0.1)) { + $this->logger->debug("WSDL Timer", array("time" => $end_time, "url" => $url)); + } + + stream_wrapper_restore('http'); + } + + } + + /** + * @see SoapClient::__doRequest() + */ + public function __doRequest($request, $location, $action, $version, $one_way = 0) + { + $this->__last_request = $request; + $start_time = microtime(true); + + $ch = curl_init($location); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Method: POST', + 'User-Agent: PHP-SOAP-CURL', + 'Content-Type: text/xml; charset=utf-8', + 'SOAPAction: "' . $action . '"', + )); + + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $request); + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); + + if (! empty($this->options['ntlm_username']) && !empty($this->options['ntlm_password'])) { + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); + curl_setopt($ch, CURLOPT_USERPWD, $this->options['ntlm_username'].':'. $this->options['ntlm_password']); + } + + $response = curl_exec($ch); + + // Log as an error if the curl call isn't a success + $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $log_func = $http_status == 200 ? 'debug' : 'error'; + + if (null !== $this->logger) { + // Log the call + $this->logger->$log_func("SoapCall: " . $action, [ + "Location" => $location, + "HttpStatus" => $http_status, + "Request" => $request, + "RequestTime" => curl_getinfo($ch, CURLINFO_TOTAL_TIME), + "Response" => strlen($response) > 2000 ? substr($response, 0, 2000) . "..." : $response, + "RequestConnectTime" => curl_getinfo($ch, CURLINFO_CONNECT_TIME), + "Time" => microtime(true) - $start_time + ]); + } + + return $response; + } +} From 6aaf1ff93dc5c02cfe2d1d4403ac4d0b89630a27 Mon Sep 17 00:00:00 2001 From: Dariusz Wrzesien Date: Fri, 10 Apr 2015 23:17:36 +0200 Subject: [PATCH 2/3] comment --- src/NTLMSoap/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NTLMSoap/Client.php b/src/NTLMSoap/Client.php index 92ac080..b374b09 100644 --- a/src/NTLMSoap/Client.php +++ b/src/NTLMSoap/Client.php @@ -51,9 +51,9 @@ public function __construct($url, $data, LoggerInterface $logger = null) } - /** - * @see SoapClient::__doRequest() - */ + /** + * @see SoapClient::__doRequest() + */ public function __doRequest($request, $location, $action, $version, $one_way = 0) { $this->__last_request = $request; From e86539966c6f5b6cddfe1a4871a698584a400bb0 Mon Sep 17 00:00:00 2001 From: Dariusz Wrzesien Date: Fri, 10 Apr 2015 23:19:19 +0200 Subject: [PATCH 3/3] format --- src/NTLMSoap/Client.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/NTLMSoap/Client.php b/src/NTLMSoap/Client.php index b374b09..06d86a8 100644 --- a/src/NTLMSoap/Client.php +++ b/src/NTLMSoap/Client.php @@ -63,10 +63,10 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( - 'Method: POST', - 'User-Agent: PHP-SOAP-CURL', - 'Content-Type: text/xml; charset=utf-8', - 'SOAPAction: "' . $action . '"', + 'Method: POST', + 'User-Agent: PHP-SOAP-CURL', + 'Content-Type: text/xml; charset=utf-8', + 'SOAPAction: "' . $action . '"', )); curl_setopt($ch, CURLOPT_POST, true);