1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains \JamesIArmes\PhpNtlm\NTLMSoapClient. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace jamesiarmes\PhpNtlm; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Soap Client using Microsoft's NTLM Authentication. |
10
|
|
|
* |
11
|
|
|
* @package php-ntlm\Soap |
12
|
|
|
*/ |
13
|
|
|
class SoapClient extends \SoapClient |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* cURL resource used to make the SOAP request |
17
|
|
|
* |
18
|
|
|
* @var resource |
19
|
|
|
*/ |
20
|
|
|
protected $ch; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Options passed to the client constructor. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $options; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function __construct($wsdl, array $options = null) |
33
|
|
|
{ |
34
|
|
|
// Set missing indexes to their default value. |
35
|
|
|
$options += array( |
36
|
|
|
'user' => null, |
37
|
|
|
'password' => null, |
38
|
|
|
'curlopts' => array(), |
39
|
|
|
); |
40
|
|
|
$this->options = $options; |
41
|
|
|
|
42
|
|
|
// Verify that a user name and password were entered. |
43
|
|
|
if (empty($options['user']) || empty($options['password'])) { |
44
|
|
|
throw new \BadMethodCallException( |
45
|
|
|
'A username and password is required.' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
parent::__construct($wsdl, $options); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function __doRequest($request, $location, $action, $version, $one_way = 0) |
56
|
|
|
{ |
57
|
|
|
$headers = $this->buildHeaders($action); |
58
|
|
|
$this->__last_request_headers = $headers; |
|
|
|
|
59
|
|
|
|
60
|
|
|
// Only reinitialize curl handle if the location is different. |
61
|
|
|
if (!$this->ch |
62
|
|
|
|| curl_getinfo($this->ch, CURLINFO_EFFECTIVE_URL) != $location) { |
63
|
|
|
$this->ch = curl_init($location); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
curl_setopt_array($this->ch, $this->curlOptions($action, $request)); |
67
|
|
|
$response = curl_exec($this->ch); |
68
|
|
|
|
69
|
|
|
// TODO: Add some real error handling. |
70
|
|
|
// If the response if false than there was an error and we should throw |
71
|
|
|
// an exception. |
72
|
|
|
if ($response === false) { |
73
|
|
|
throw new \RuntimeException( |
74
|
|
|
'Curl error: ' . curl_error($this->ch), |
75
|
|
|
curl_errno($this->ch) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $response; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function __getLastRequestHeaders() |
86
|
|
|
{ |
87
|
|
|
return implode('n', $this->__last_request_headers) . "\n"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the response code from the last request |
92
|
|
|
* |
93
|
|
|
* @return integer |
94
|
|
|
* |
95
|
|
|
* @throws \BadMethodCallException |
96
|
|
|
* If no cURL resource has been initialized. |
97
|
|
|
*/ |
98
|
|
|
public function getResponseCode() |
99
|
|
|
{ |
100
|
|
|
if (empty($this->ch)) { |
101
|
|
|
throw new \BadMethodCallException('No cURL resource has been ' |
102
|
|
|
. 'initialized. This is probably because no request has not ' |
103
|
|
|
. 'been made.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return curl_getinfo($this->ch, CURLINFO_HTTP_CODE); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Builds the headers for the request. |
111
|
|
|
* |
112
|
|
|
* @param string $action |
113
|
|
|
* The SOAP action to be performed. |
114
|
|
|
*/ |
115
|
|
|
protected function buildHeaders($action) |
116
|
|
|
{ |
117
|
|
|
return array( |
118
|
|
|
'Method: POST', |
119
|
|
|
'Connection: Keep-Alive', |
120
|
|
|
'User-Agent: PHP-SOAP-CURL', |
121
|
|
|
'Content-Type: text/xml; charset=utf-8', |
122
|
|
|
"SOAPAction: \"$action\"", |
123
|
|
|
'Expect: 100-continue', |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Builds an array of curl options for the request |
129
|
|
|
* |
130
|
|
|
* @param string $action |
131
|
|
|
* The SOAP action to be performed. |
132
|
|
|
* @param string $request |
133
|
|
|
* The XML SOAP request. |
134
|
|
|
* @return array |
135
|
|
|
* Array of curl options. |
136
|
|
|
*/ |
137
|
|
|
protected function curlOptions($action, $request) |
138
|
|
|
{ |
139
|
|
|
$options = $this->options['curlopts'] + array( |
140
|
|
|
CURLOPT_SSL_VERIFYPEER => true, |
141
|
|
|
CURLOPT_RETURNTRANSFER => true, |
142
|
|
|
CURLOPT_HTTPHEADER => $this->buildHeaders($action), |
143
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
144
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_NTLM, |
145
|
|
|
CURLOPT_USERPWD => $this->options['user'] . ':' |
146
|
|
|
. $this->options['password'], |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
// We shouldn't allow these options to be overridden. |
150
|
|
|
$options[CURLOPT_POST] = true; |
151
|
|
|
$options[CURLOPT_POSTFIELDS] = $request; |
152
|
|
|
|
153
|
|
|
return $options; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: