1 | <?php |
||
40 | class NTLMSoapClient extends SoapClient |
||
41 | { |
||
42 | /** |
||
43 | * Username for authentication on the exchnage server |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $user; |
||
48 | |||
49 | /** |
||
50 | * Password for authentication on the exchnage server |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $password; |
||
55 | |||
56 | /** |
||
57 | * Whether or not to validate ssl certificates |
||
58 | * |
||
59 | * @var boolean |
||
60 | */ |
||
61 | protected $validate = false; |
||
62 | |||
63 | private $httpClient; |
||
64 | |||
65 | protected $__last_request_headers; |
||
66 | |||
67 | protected $_responseCode; |
||
68 | |||
69 | /** |
||
70 | * An array of headers for us to store or use. Since not all requests use all headers (DeleteItem and SyncItems |
||
71 | * don't allow you to pass a Timezone for example), we need to be able to smartly decide what headers to include |
||
72 | * and exclude from a request. Until we have propper selection (an array of all known operations and what headers |
||
73 | * are allowed for example), this seems like a decent solution for storing the headers before we decide if they |
||
74 | * belong in the request or not) |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $ewsHeaders = array( |
||
79 | 'version' => null, |
||
80 | 'impersonation' => null, |
||
81 | 'timezone' => null |
||
82 | ); |
||
83 | |||
84 | protected $auth; |
||
85 | |||
86 | protected $callsWithoutTimezone = array( |
||
87 | 'DeleteItem', |
||
88 | 'SyncFolderItems', |
||
89 | 'GetServerTimeZones', |
||
90 | 'ConvertId' |
||
91 | ); |
||
92 | |||
93 | /** |
||
94 | * @TODO: Make this smarter. It should know and search what headers to remove on what actions |
||
95 | * |
||
96 | * @param string $name |
||
97 | * @param string $args |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function __call($name, $args) |
||
101 | { |
||
102 | $this->__setSoapHeaders(null); |
||
103 | |||
104 | $headers = array( |
||
105 | $this->ewsHeaders['version'], |
||
106 | $this->ewsHeaders['impersonation'], |
||
107 | ); |
||
108 | |||
109 | if (!in_array($name, $this->callsWithoutTimezone)) { |
||
110 | $headers[] = $this->ewsHeaders['timezone']; |
||
111 | } |
||
112 | |||
113 | $headers = array_filter($headers, function ($header) { |
||
114 | if (!($header instanceof SoapHeader)) { |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | return true; |
||
119 | }); |
||
120 | |||
121 | $this->__setSoapHeaders($headers); |
||
122 | return parent::__call($name, $args); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param string $location |
||
127 | * @param string $wsdl |
||
128 | * @param array $options |
||
129 | */ |
||
130 | 36 | public function __construct($location, $auth, $wsdl, $options = array()) |
|
131 | { |
||
132 | 36 | $this->auth = $auth; |
|
133 | |||
134 | 36 | $options = array_replace_recursive([ |
|
135 | 'httpPlayback' => [ |
||
136 | 'mode' => null |
||
137 | 36 | ] |
|
138 | 36 | ], $options); |
|
139 | |||
140 | 36 | $options['location'] = $location; |
|
141 | |||
142 | // If a version was set then add it to the headers. |
||
143 | 36 | if (!empty($options['version'])) { |
|
144 | 35 | $this->ewsHeaders['version'] = new SoapHeader( |
|
145 | 35 | 'http://schemas.microsoft.com/exchange/services/2006/types', |
|
146 | 35 | 'RequestServerVersion Version="'.$options['version'].'"' |
|
147 | 35 | ); |
|
148 | 35 | } |
|
149 | |||
150 | // If impersonation was set then add it to the headers. |
||
151 | 36 | if (!empty($options['impersonation'])) { |
|
152 | $impersonation = $options['impersonation']; |
||
153 | if (is_string($impersonation)) { |
||
154 | $impersonation = ExchangeImpersonation::fromEmailAddress($options['impersonation']); |
||
155 | } |
||
156 | |||
157 | $this->ewsHeaders['impersonation'] = new SoapHeader( |
||
158 | 'http://schemas.microsoft.com/exchange/services/2006/types', |
||
159 | 'ExchangeImpersonation', |
||
160 | $impersonation->toXmlObject() |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | 36 | if (!empty($options['timezone'])) { |
|
165 | $this->ewsHeaders['timezone'] = new SoapHeader( |
||
166 | 'http://schemas.microsoft.com/exchange/services/2006/types', |
||
167 | 'TimeZoneContext', |
||
168 | array( |
||
169 | 'TimeZoneDefinition' => array( |
||
170 | 'Id' => $options['timezone'] |
||
171 | ) |
||
172 | ) |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | 36 | $this->httpClient = Factory::getInstance($options['httpPlayback']); |
|
177 | |||
178 | 36 | parent::__construct($wsdl, $options); |
|
|
|||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Performs a SOAP request |
||
183 | * |
||
184 | * @link http://php.net/manual/en/function.soap-soapclient-dorequest.php |
||
185 | * |
||
186 | * @param string $request the xml soap request |
||
187 | * @param string $location the url to request |
||
188 | * @param string $action the soap action. |
||
189 | * @param integer $version the soap version |
||
190 | * @param integer $one_way |
||
191 | * @return string the xml soap response. |
||
192 | */ |
||
193 | public function __doRequest($request, $location, $action, $version, $one_way = 0) |
||
194 | { |
||
195 | $postOptions = array( |
||
196 | 'body' => $request, |
||
197 | 'headers' => array( |
||
198 | 'Connection' => 'Keep-Alive', |
||
199 | 'User-Agent' => 'PHP-SOAP-CURL', |
||
200 | 'Content-Type' => 'text/xml; charset=utf-8', |
||
201 | 'SOAPAction' => $action |
||
202 | ), |
||
203 | 'verify' => $this->validate, |
||
204 | 'http_errors' => false |
||
205 | ); |
||
206 | |||
207 | $postOptions = array_replace_recursive($postOptions, $this->auth); |
||
208 | |||
209 | $response = $this->httpClient->post($location, $postOptions); |
||
210 | |||
211 | $this->__last_request_headers = $postOptions['headers']; |
||
212 | $this->_responseCode = $response->getStatusCode(); |
||
213 | |||
214 | return $response->getBody()->__toString(); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Returns last SOAP request headers |
||
219 | * |
||
220 | * @link http://php.net/manual/en/function.soap-soapclient-getlastrequestheaders.php |
||
221 | * |
||
222 | * @return string the last soap request headers |
||
223 | */ |
||
224 | public function __getLastRequestHeaders() |
||
225 | { |
||
226 | return implode('n', $this->__last_request_headers)."\n"; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Set validation certificate |
||
231 | * |
||
232 | * @param bool $validate |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function validateCertificate($validate = true) |
||
236 | { |
||
237 | $this->validate = $validate; |
||
238 | |||
239 | return $this; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Returns the response code from the last request |
||
244 | * |
||
245 | * @return integer |
||
246 | */ |
||
247 | public function getResponseCode() |
||
251 | } |
||
252 |
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: