Created
November 29, 2014 02:25
-
-
Save samueljon/79b5d00c5763f0f463a4 to your computer and use it in GitHub Desktop.
NTLM enabled SOAPCLIENT. Needs NTLMStream.inc for it to work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User: samueljon <samuel.jon.gunnarsson _at_ gmail.com> | |
* Date: 14.10.14 | |
* Time: 08:16 | |
*/ | |
/** | |
* Class NTLMSoapClient | |
* | |
* Provides Client that can handle NTLM authentication. | |
*/ | |
class NTLMSoapClient extends SoapClient | |
{ | |
function __doRequest($request, $location, $action, $version, $one_way = NULL) | |
{ | |
$headers = array( | |
'Method: POST', | |
'Connection: Keep-Alive', | |
'User-Agent: PHP-SOAP-CURL', | |
'Content-Type: text/xml; charset=utf-8', | |
'SOAPAction: "' . $action . '"', | |
); | |
$this->__last_request_headers = $headers; | |
$ch = curl_init($location); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); | |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); | |
curl_setopt($ch, CURLOPT_USERPWD, USERPWD); | |
$response = curl_exec($ch); | |
return $response; | |
} | |
function __getLastRequestHeaders() | |
{ | |
return implode("\n", $this->__last_request_headers) . "\n"; | |
} | |
} | |
/** | |
* @param $page SOAP subpage or section that will be queried. | |
* @param $params get or set parameters. | |
* @return $query result from the SOAP Query. | |
*/ | |
function LsSoapQuery($page,$params) | |
{ | |
// We unregister the current HTTP wrapper | |
stream_wrapper_unregister('http'); | |
// we register the new HTTP wrapper | |
stream_wrapper_register('http', 'NTLMStream') or die("Failed to register protocol"); | |
// Connection Parameters | |
$baseURL = 'https://<ipaddress>:<port>/<identifier>/WS/'; | |
$companyUrl = '<company>'; | |
$pageURL = $baseURL . $companyUrl . '/Page/'. $page; | |
// Try to establish connection for the Soap Client otherwise catch the exception | |
try{ | |
$connection = new NTLMSoapClient($pageURL); | |
}catch (Exception $e){ | |
//Handle Exception | |
echo "<h1>Error:</h1>"; | |
var_dump($e); | |
} | |
$query = $connection; | |
stream_wrapper_restore('http'); | |
return $query; | |
} | |
// NTLMSoapClient.inc ends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NTLMStream | |
{ | |
private $path; | |
private $mode; | |
private $options; | |
private $opened_path; | |
private $buffer; | |
private $pos; | |
/** | |
* Open the stream | |
* | |
* @param unknown_type $path | |
* @param unknown_type $mode | |
* @param unknown_type $options | |
* @param unknown_type $opened_path | |
* @return unknown | |
*/ | |
public function stream_open($path, $mode, $options, $opened_path) | |
{ | |
$this->path = $path; | |
$this->mode = $mode; | |
$this->options = $options; | |
$this->opened_path = $opened_path; | |
$this->createBuffer($path); | |
return true; | |
} | |
/** | |
* Create the buffer by requesting the url through cURL | |
* | |
* @param unknown_type $path | |
*/ | |
private function createBuffer($path) | |
{ | |
if ($this->buffer) { | |
return; | |
} | |
$this->ch = curl_init($path); | |
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); | |
curl_setopt($this->ch, CURLOPT_USERPWD, USERPWD); | |
$this->buffer = curl_exec($this->ch); | |
$this->pos = 0; | |
} | |
/** | |
* Close the stream | |
* | |
*/ | |
public function stream_close() | |
{ | |
curl_close($this->ch); | |
} | |
/** | |
* Read the stream | |
* | |
* @param int $count number of bytes to read | |
* @return content from pos to count | |
*/ | |
public function stream_read($count) | |
{ | |
if (strlen($this->buffer) == 0) { | |
return false; | |
} | |
$read = substr($this->buffer, $this->pos, $count); | |
$this->pos += $count; | |
return $read; | |
} | |
/** | |
* write the stream | |
* | |
* @param int $count number of bytes to read | |
* @return content from pos to count | |
*/ | |
public function stream_write($data) | |
{ | |
if (strlen($this->buffer) == 0) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* | |
* @return true if eof else false | |
*/ | |
public function stream_eof() | |
{ | |
return ($this->pos > strlen($this->buffer)); | |
} | |
/** | |
* @return int the position of the current read pointer | |
*/ | |
public function stream_tell() | |
{ | |
return $this->pos; | |
} | |
/** | |
* Flush stream data | |
*/ | |
public function stream_flush() | |
{ | |
$this->buffer = null; | |
$this->pos = null; | |
} | |
/** | |
* Stat the file, return only the size of the buffer | |
* | |
* @return array stat information | |
*/ | |
public function stream_stat() | |
{ | |
$this->createBuffer($this->path); | |
$stat = array( | |
'size' => strlen($this->buffer), | |
); | |
return $stat; | |
} | |
/** | |
* Stat the url, return only the size of the buffer | |
* | |
* @return array stat information | |
*/ | |
public function url_stat($path, $flags) | |
{ | |
$this->createBuffer($path); | |
$stat = array( | |
'size' => strlen($this->buffer), | |
); | |
return $stat; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment