0
|
1 <?php
|
|
2 /**
|
|
3 * This file contains the Net_Sieve class.
|
|
4 *
|
|
5 * PHP version 5
|
|
6 *
|
|
7 * +-----------------------------------------------------------------------+
|
|
8 * | All rights reserved. |
|
|
9 * | |
|
|
10 * | Redistribution and use in source and binary forms, with or without |
|
|
11 * | modification, are permitted provided that the following conditions |
|
|
12 * | are met: |
|
|
13 * | |
|
|
14 * | o Redistributions of source code must retain the above copyright |
|
|
15 * | notice, this list of conditions and the following disclaimer. |
|
|
16 * | o Redistributions in binary form must reproduce the above copyright |
|
|
17 * | notice, this list of conditions and the following disclaimer in the |
|
|
18 * | documentation and/or other materials provided with the distribution.|
|
|
19 * | |
|
|
20 * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
21 * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
22 * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
23 * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
24 * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
25 * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
26 * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
27 * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
28 * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
29 * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
30 * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
31 * +-----------------------------------------------------------------------+
|
|
32 *
|
|
33 * @category Networking
|
|
34 * @package Net_Sieve
|
|
35 * @author Richard Heyes <richard@phpguru.org>
|
|
36 * @author Damian Fernandez Sosa <damlists@cnba.uba.ar>
|
|
37 * @author Anish Mistry <amistry@am-productions.biz>
|
|
38 * @author Jan Schneider <jan@horde.org>
|
|
39 * @copyright 2002-2003 Richard Heyes
|
|
40 * @copyright 2006-2008 Anish Mistry
|
|
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD
|
|
42 * @link http://pear.php.net/package/Net_Sieve
|
|
43 */
|
|
44
|
|
45 require_once 'PEAR.php';
|
|
46 require_once 'Net/Socket.php';
|
|
47
|
|
48 /**
|
35
|
49 * Disconnected state
|
0
|
50 *
|
|
51 * @const NET_SIEVE_STATE_DISCONNECTED
|
|
52 */
|
|
53 define('NET_SIEVE_STATE_DISCONNECTED', 1, true);
|
|
54
|
|
55 /**
|
|
56 * Authorisation state
|
35
|
57 *
|
0
|
58 * @const NET_SIEVE_STATE_AUTHORISATION
|
|
59 */
|
|
60 define('NET_SIEVE_STATE_AUTHORISATION', 2, true);
|
|
61
|
|
62 /**
|
|
63 * Transaction state
|
35
|
64 *
|
0
|
65 * @const NET_SIEVE_STATE_TRANSACTION
|
|
66 */
|
|
67 define('NET_SIEVE_STATE_TRANSACTION', 3, true);
|
|
68
|
|
69
|
|
70 /**
|
|
71 * A class for talking to the timsieved server which comes with Cyrus IMAP.
|
|
72 *
|
|
73 * @category Networking
|
|
74 * @package Net_Sieve
|
|
75 * @author Richard Heyes <richard@phpguru.org>
|
|
76 * @author Damian Fernandez Sosa <damlists@cnba.uba.ar>
|
|
77 * @author Anish Mistry <amistry@am-productions.biz>
|
|
78 * @author Jan Schneider <jan@horde.org>
|
35
|
79 * @author Neil Munday <neil@mundayweb.com>
|
0
|
80 * @copyright 2002-2003 Richard Heyes
|
|
81 * @copyright 2006-2008 Anish Mistry
|
|
82 * @license http://www.opensource.org/licenses/bsd-license.php BSD
|
|
83 * @version Release: @package_version@
|
|
84 * @link http://pear.php.net/package/Net_Sieve
|
|
85 * @link http://tools.ietf.org/html/rfc5228 RFC 5228 (Sieve: An Email
|
|
86 * Filtering Language)
|
|
87 * @link http://tools.ietf.org/html/rfc5804 RFC 5804 A Protocol for
|
|
88 * Remotely Managing Sieve Scripts
|
|
89 */
|
|
90 class Net_Sieve
|
|
91 {
|
|
92 /**
|
|
93 * The authentication methods this class supports.
|
|
94 *
|
|
95 * Can be overwritten if having problems with certain methods.
|
|
96 *
|
|
97 * @var array
|
|
98 */
|
|
99 var $supportedAuthMethods = array(
|
|
100 'DIGEST-MD5',
|
|
101 'CRAM-MD5',
|
|
102 'EXTERNAL',
|
|
103 'PLAIN' ,
|
35
|
104 'LOGIN',
|
|
105 'GSSAPI'
|
0
|
106 );
|
|
107
|
|
108 /**
|
|
109 * SASL authentication methods that require Auth_SASL.
|
|
110 *
|
|
111 * @var array
|
|
112 */
|
|
113 var $supportedSASLAuthMethods = array('DIGEST-MD5', 'CRAM-MD5');
|
|
114
|
|
115 /**
|
|
116 * The socket handle.
|
|
117 *
|
|
118 * @var resource
|
|
119 */
|
|
120 var $_sock;
|
|
121
|
|
122 /**
|
|
123 * Parameters and connection information.
|
|
124 *
|
|
125 * @var array
|
|
126 */
|
|
127 var $_data;
|
|
128
|
|
129 /**
|
|
130 * Current state of the connection.
|
|
131 *
|
|
132 * One of the NET_SIEVE_STATE_* constants.
|
|
133 *
|
|
134 * @var integer
|
|
135 */
|
|
136 var $_state;
|
|
137
|
|
138 /**
|
|
139 * PEAR object to avoid strict warnings.
|
|
140 *
|
|
141 * @var PEAR_Error
|
|
142 */
|
|
143 var $_pear;
|
|
144
|
|
145 /**
|
|
146 * Constructor error.
|
|
147 *
|
|
148 * @var PEAR_Error
|
|
149 */
|
|
150 var $_error;
|
|
151
|
|
152 /**
|
|
153 * Whether to enable debugging.
|
|
154 *
|
|
155 * @var boolean
|
|
156 */
|
|
157 var $_debug = false;
|
|
158
|
|
159 /**
|
|
160 * Debug output handler.
|
|
161 *
|
|
162 * This has to be a valid callback.
|
|
163 *
|
|
164 * @var string|array
|
|
165 */
|
|
166 var $_debug_handler = null;
|
|
167
|
|
168 /**
|
|
169 * Whether to pick up an already established connection.
|
|
170 *
|
|
171 * @var boolean
|
|
172 */
|
|
173 var $_bypassAuth = false;
|
|
174
|
|
175 /**
|
|
176 * Whether to use TLS if available.
|
|
177 *
|
|
178 * @var boolean
|
|
179 */
|
|
180 var $_useTLS = true;
|
|
181
|
|
182 /**
|
|
183 * Additional options for stream_context_create().
|
|
184 *
|
|
185 * @var array
|
|
186 */
|
|
187 var $_options = null;
|
|
188
|
|
189 /**
|
|
190 * Maximum number of referral loops
|
|
191 *
|
|
192 * @var array
|
|
193 */
|
|
194 var $_maxReferralCount = 15;
|
|
195
|
|
196 /**
|
35
|
197 * Kerberos service principal to use for GSSAPI authentication.
|
|
198 *
|
|
199 * @var string
|
|
200 */
|
|
201 var $_gssapiPrincipal = null;
|
|
202
|
|
203 /**
|
|
204 * Kerberos service cname to use for GSSAPI authentication.
|
|
205 *
|
|
206 * @var string
|
|
207 */
|
|
208 var $_gssapiCN = null;
|
|
209
|
|
210 /**
|
0
|
211 * Constructor.
|
|
212 *
|
|
213 * Sets up the object, connects to the server and logs in. Stores any
|
|
214 * generated error in $this->_error, which can be retrieved using the
|
|
215 * getError() method.
|
|
216 *
|
|
217 * @param string $user Login username.
|
|
218 * @param string $pass Login password.
|
|
219 * @param string $host Hostname of server.
|
|
220 * @param string $port Port of server.
|
|
221 * @param string $logintype Type of login to perform (see
|
|
222 * $supportedAuthMethods).
|
|
223 * @param string $euser Effective user. If authenticating as an
|
|
224 * administrator, login as this user.
|
|
225 * @param boolean $debug Whether to enable debugging (@see setDebug()).
|
|
226 * @param string $bypassAuth Skip the authentication phase. Useful if the
|
|
227 * socket is already open.
|
|
228 * @param boolean $useTLS Use TLS if available.
|
|
229 * @param array $options Additional options for
|
|
230 * stream_context_create().
|
|
231 * @param mixed $handler A callback handler for the debug output.
|
35
|
232 * @param string $principal Kerberos service principal to use
|
|
233 * with GSSAPI authentication.
|
|
234 * @param string $cname Kerberos service cname to use
|
|
235 * with GSSAPI authentication.
|
0
|
236 */
|
|
237 function __construct($user = null, $pass = null, $host = 'localhost',
|
|
238 $port = 2000, $logintype = '', $euser = '',
|
|
239 $debug = false, $bypassAuth = false, $useTLS = true,
|
35
|
240 $options = null, $handler = null, $principal = null, $cname = null
|
0
|
241 ) {
|
|
242 $this->_pear = new PEAR();
|
|
243 $this->_state = NET_SIEVE_STATE_DISCONNECTED;
|
|
244 $this->_data['user'] = $user;
|
|
245 $this->_data['pass'] = $pass;
|
|
246 $this->_data['host'] = $host;
|
|
247 $this->_data['port'] = $port;
|
|
248 $this->_data['logintype'] = $logintype;
|
|
249 $this->_data['euser'] = $euser;
|
|
250 $this->_sock = new Net_Socket();
|
|
251 $this->_bypassAuth = $bypassAuth;
|
|
252 $this->_useTLS = $useTLS;
|
|
253 $this->_options = (array) $options;
|
35
|
254 $this->_gssapiPrincipal = $principal;
|
|
255 $this->_gssapiCN = $cname;
|
|
256
|
0
|
257 $this->setDebug($debug, $handler);
|
|
258
|
|
259 /* Try to include the Auth_SASL package. If the package is not
|
|
260 * available, we disable the authentication methods that depend upon
|
|
261 * it. */
|
|
262 if ((@include_once 'Auth/SASL.php') === false) {
|
|
263 $this->_debug('Auth_SASL not present');
|
|
264 $this->supportedAuthMethods = array_diff(
|
|
265 $this->supportedAuthMethods,
|
|
266 $this->supportedSASLAuthMethods
|
|
267 );
|
|
268 }
|
|
269
|
|
270 if (strlen($user) && strlen($pass)) {
|
|
271 $this->_error = $this->_handleConnectAndLogin();
|
|
272 }
|
|
273 }
|
|
274
|
|
275 /**
|
|
276 * Returns any error that may have been generated in the constructor.
|
|
277 *
|
|
278 * @return boolean|PEAR_Error False if no error, PEAR_Error otherwise.
|
|
279 */
|
|
280 function getError()
|
|
281 {
|
|
282 return is_a($this->_error, 'PEAR_Error') ? $this->_error : false;
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Sets the debug state and handler function.
|
|
287 *
|
|
288 * @param boolean $debug Whether to enable debugging.
|
|
289 * @param string $handler A custom debug handler. Must be a valid callback.
|
|
290 *
|
|
291 * @return void
|
|
292 */
|
|
293 function setDebug($debug = true, $handler = null)
|
|
294 {
|
|
295 $this->_debug = $debug;
|
|
296 $this->_debug_handler = $handler;
|
|
297 }
|
|
298
|
|
299 /**
|
35
|
300 * Sets the Kerberos service principal for use with GSSAPI
|
|
301 * authentication.
|
|
302 *
|
|
303 * @param string $principal The Kerberos service principal
|
|
304 *
|
|
305 * @return void
|
|
306 */
|
|
307 function setServicePrincipal($principal)
|
|
308 {
|
|
309 $this->_gssapiPrincipal = $principal;
|
|
310 }
|
|
311
|
|
312 /**
|
|
313 * Sets the Kerberos service CName for use with GSSAPI
|
|
314 * authentication.
|
|
315 *
|
|
316 * @param string $cname The Kerberos service principal
|
|
317 *
|
|
318 * @return void
|
|
319 */
|
|
320 function setServiceCN($cname)
|
|
321 {
|
|
322 $this->_gssapiCN = $cname;
|
|
323 }
|
|
324
|
|
325 /**
|
0
|
326 * Connects to the server and logs in.
|
|
327 *
|
|
328 * @return boolean True on success, PEAR_Error on failure.
|
|
329 */
|
|
330 function _handleConnectAndLogin()
|
|
331 {
|
|
332 $res = $this->connect($this->_data['host'], $this->_data['port'], $this->_options, $this->_useTLS);
|
|
333 if (is_a($res, 'PEAR_Error')) {
|
|
334 return $res;
|
|
335 }
|
|
336 if ($this->_bypassAuth === false) {
|
|
337 $res = $this->login($this->_data['user'], $this->_data['pass'], $this->_data['logintype'], $this->_data['euser'], $this->_bypassAuth);
|
|
338 if (is_a($res, 'PEAR_Error')) {
|
|
339 return $res;
|
|
340 }
|
|
341 }
|
|
342 return true;
|
|
343 }
|
|
344
|
|
345 /**
|
|
346 * Handles connecting to the server and checks the response validity.
|
|
347 *
|
|
348 * @param string $host Hostname of server.
|
|
349 * @param string $port Port of server.
|
|
350 * @param array $options List of options to pass to
|
|
351 * stream_context_create().
|
|
352 * @param boolean $useTLS Use TLS if available.
|
|
353 *
|
|
354 * @return boolean True on success, PEAR_Error otherwise.
|
|
355 */
|
|
356 function connect($host, $port, $options = null, $useTLS = true)
|
|
357 {
|
|
358 $this->_data['host'] = $host;
|
|
359 $this->_data['port'] = $port;
|
|
360 $this->_useTLS = $useTLS;
|
|
361
|
|
362 if (is_array($options)) {
|
|
363 $this->_options = array_merge($this->_options, $options);
|
|
364 }
|
|
365
|
|
366 if (NET_SIEVE_STATE_DISCONNECTED != $this->_state) {
|
|
367 return $this->_pear->raiseError('Not currently in DISCONNECTED state', 1);
|
|
368 }
|
|
369
|
|
370 $res = $this->_sock->connect($host, $port, false, 5, $options);
|
|
371 if (is_a($res, 'PEAR_Error')) {
|
|
372 return $res;
|
|
373 }
|
|
374
|
|
375 if ($this->_bypassAuth) {
|
|
376 $this->_state = NET_SIEVE_STATE_TRANSACTION;
|
35
|
377
|
|
378 // Reset capabilities
|
|
379 $this->_parseCapability('');
|
0
|
380 } else {
|
|
381 $this->_state = NET_SIEVE_STATE_AUTHORISATION;
|
35
|
382
|
0
|
383 $res = $this->_doCmd();
|
|
384 if (is_a($res, 'PEAR_Error')) {
|
|
385 return $res;
|
|
386 }
|
35
|
387
|
|
388 // Reset capabilities (use unattended capabilities)
|
|
389 $this->_parseCapability($res);
|
0
|
390 }
|
|
391
|
35
|
392 // Explicitly ask for the capabilities if needed
|
|
393 if (empty($this->_capability['implementation'])) {
|
|
394 $res = $this->_cmdCapability();
|
|
395 if (is_a($res, 'PEAR_Error')) {
|
|
396 return $this->_pear->raiseError(
|
|
397 'Failed to connect, server said: ' . $res->getMessage(), 2
|
|
398 );
|
|
399 }
|
0
|
400 }
|
|
401
|
|
402 // Check if we can enable TLS via STARTTLS.
|
|
403 if ($useTLS && !empty($this->_capability['starttls'])
|
|
404 && function_exists('stream_socket_enable_crypto')
|
|
405 ) {
|
|
406 $res = $this->_startTLS();
|
|
407 if (is_a($res, 'PEAR_Error')) {
|
|
408 return $res;
|
|
409 }
|
|
410 }
|
|
411
|
|
412 return true;
|
|
413 }
|
|
414
|
|
415 /**
|
|
416 * Disconnect from the Sieve server.
|
|
417 *
|
|
418 * @param boolean $sendLogoutCMD Whether to send LOGOUT command before
|
|
419 * disconnecting.
|
|
420 *
|
|
421 * @return boolean True on success, PEAR_Error otherwise.
|
|
422 */
|
|
423 function disconnect($sendLogoutCMD = true)
|
|
424 {
|
|
425 return $this->_cmdLogout($sendLogoutCMD);
|
|
426 }
|
|
427
|
|
428 /**
|
|
429 * Logs into server.
|
|
430 *
|
|
431 * @param string $user Login username.
|
|
432 * @param string $pass Login password.
|
|
433 * @param string $logintype Type of login method to use.
|
|
434 * @param string $euser Effective UID (perform on behalf of $euser).
|
|
435 * @param boolean $bypassAuth Do not perform authentication.
|
|
436 *
|
|
437 * @return boolean True on success, PEAR_Error otherwise.
|
|
438 */
|
|
439 function login($user, $pass, $logintype = null, $euser = '', $bypassAuth = false)
|
|
440 {
|
|
441 $this->_data['user'] = $user;
|
|
442 $this->_data['pass'] = $pass;
|
|
443 $this->_data['logintype'] = $logintype;
|
|
444 $this->_data['euser'] = $euser;
|
|
445 $this->_bypassAuth = $bypassAuth;
|
|
446
|
|
447 if (NET_SIEVE_STATE_AUTHORISATION != $this->_state) {
|
|
448 return $this->_pear->raiseError('Not currently in AUTHORISATION state', 1);
|
|
449 }
|
|
450
|
|
451 if (!$bypassAuth ) {
|
|
452 $res = $this->_cmdAuthenticate($user, $pass, $logintype, $euser);
|
|
453 if (is_a($res, 'PEAR_Error')) {
|
|
454 return $res;
|
|
455 }
|
|
456 }
|
35
|
457
|
0
|
458 $this->_state = NET_SIEVE_STATE_TRANSACTION;
|
|
459
|
|
460 return true;
|
|
461 }
|
|
462
|
|
463 /**
|
|
464 * Returns an indexed array of scripts currently on the server.
|
|
465 *
|
35
|
466 * @param string $active Will be set to the name of the active script
|
|
467 *
|
|
468 * @return array Indexed array of scriptnames, PEAR_Error on failure
|
0
|
469 */
|
35
|
470 function listScripts(&$active = null)
|
0
|
471 {
|
|
472 if (is_array($scripts = $this->_cmdListScripts())) {
|
35
|
473 if (isset($scripts[1])) {
|
|
474 $active = $scripts[1];
|
|
475 }
|
|
476
|
0
|
477 return $scripts[0];
|
|
478 }
|
35
|
479
|
|
480 return $scripts;
|
0
|
481 }
|
|
482
|
|
483 /**
|
|
484 * Returns the active script.
|
|
485 *
|
|
486 * @return string The active scriptname.
|
|
487 */
|
|
488 function getActive()
|
|
489 {
|
|
490 if (is_array($scripts = $this->_cmdListScripts())) {
|
|
491 return $scripts[1];
|
|
492 }
|
|
493 }
|
|
494
|
|
495 /**
|
|
496 * Sets the active script.
|
|
497 *
|
|
498 * @param string $scriptname The name of the script to be set as active.
|
|
499 *
|
|
500 * @return boolean True on success, PEAR_Error on failure.
|
|
501 */
|
|
502 function setActive($scriptname)
|
|
503 {
|
|
504 return $this->_cmdSetActive($scriptname);
|
|
505 }
|
|
506
|
|
507 /**
|
|
508 * Retrieves a script.
|
|
509 *
|
|
510 * @param string $scriptname The name of the script to be retrieved.
|
|
511 *
|
|
512 * @return string The script on success, PEAR_Error on failure.
|
|
513 */
|
|
514 function getScript($scriptname)
|
|
515 {
|
|
516 return $this->_cmdGetScript($scriptname);
|
|
517 }
|
|
518
|
|
519 /**
|
|
520 * Adds a script to the server.
|
|
521 *
|
|
522 * @param string $scriptname Name of the script.
|
|
523 * @param string $script The script content.
|
|
524 * @param boolean $makeactive Whether to make this the active script.
|
|
525 *
|
|
526 * @return boolean True on success, PEAR_Error on failure.
|
|
527 */
|
|
528 function installScript($scriptname, $script, $makeactive = false)
|
|
529 {
|
|
530 $res = $this->_cmdPutScript($scriptname, $script);
|
|
531 if (is_a($res, 'PEAR_Error')) {
|
|
532 return $res;
|
|
533 }
|
35
|
534
|
0
|
535 if ($makeactive) {
|
|
536 return $this->_cmdSetActive($scriptname);
|
|
537 }
|
35
|
538
|
0
|
539 return true;
|
|
540 }
|
|
541
|
|
542 /**
|
|
543 * Removes a script from the server.
|
|
544 *
|
|
545 * @param string $scriptname Name of the script.
|
|
546 *
|
|
547 * @return boolean True on success, PEAR_Error on failure.
|
|
548 */
|
|
549 function removeScript($scriptname)
|
|
550 {
|
|
551 return $this->_cmdDeleteScript($scriptname);
|
|
552 }
|
|
553
|
|
554 /**
|
|
555 * Checks if the server has space to store the script by the server.
|
|
556 *
|
|
557 * @param string $scriptname The name of the script to mark as active.
|
|
558 * @param integer $size The size of the script.
|
|
559 *
|
|
560 * @return boolean|PEAR_Error True if there is space, PEAR_Error otherwise.
|
|
561 *
|
|
562 * @todo Rename to hasSpace()
|
|
563 */
|
|
564 function haveSpace($scriptname, $size)
|
|
565 {
|
|
566 if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
|
|
567 return $this->_pear->raiseError('Not currently in TRANSACTION state', 1);
|
|
568 }
|
|
569
|
|
570 $res = $this->_doCmd(sprintf('HAVESPACE %s %d', $this->_escape($scriptname), $size));
|
|
571 if (is_a($res, 'PEAR_Error')) {
|
|
572 return $res;
|
|
573 }
|
35
|
574
|
0
|
575 return true;
|
|
576 }
|
|
577
|
|
578 /**
|
|
579 * Returns the list of extensions the server supports.
|
|
580 *
|
|
581 * @return array List of extensions or PEAR_Error on failure.
|
|
582 */
|
|
583 function getExtensions()
|
|
584 {
|
|
585 if (NET_SIEVE_STATE_DISCONNECTED == $this->_state) {
|
|
586 return $this->_pear->raiseError('Not currently connected', 7);
|
|
587 }
|
35
|
588
|
0
|
589 return $this->_capability['extensions'];
|
|
590 }
|
|
591
|
|
592 /**
|
|
593 * Returns whether the server supports an extension.
|
|
594 *
|
|
595 * @param string $extension The extension to check.
|
|
596 *
|
|
597 * @return boolean Whether the extension is supported or PEAR_Error on
|
|
598 * failure.
|
|
599 */
|
|
600 function hasExtension($extension)
|
|
601 {
|
|
602 if (NET_SIEVE_STATE_DISCONNECTED == $this->_state) {
|
|
603 return $this->_pear->raiseError('Not currently connected', 7);
|
|
604 }
|
|
605
|
|
606 $extension = trim($this->_toUpper($extension));
|
|
607 if (is_array($this->_capability['extensions'])) {
|
|
608 foreach ($this->_capability['extensions'] as $ext) {
|
|
609 if ($ext == $extension) {
|
|
610 return true;
|
|
611 }
|
|
612 }
|
|
613 }
|
|
614
|
|
615 return false;
|
|
616 }
|
|
617
|
|
618 /**
|
|
619 * Returns the list of authentication methods the server supports.
|
|
620 *
|
|
621 * @return array List of authentication methods or PEAR_Error on failure.
|
|
622 */
|
|
623 function getAuthMechs()
|
|
624 {
|
|
625 if (NET_SIEVE_STATE_DISCONNECTED == $this->_state) {
|
|
626 return $this->_pear->raiseError('Not currently connected', 7);
|
|
627 }
|
35
|
628
|
0
|
629 return $this->_capability['sasl'];
|
|
630 }
|
|
631
|
|
632 /**
|
|
633 * Returns whether the server supports an authentication method.
|
|
634 *
|
|
635 * @param string $method The method to check.
|
|
636 *
|
|
637 * @return boolean Whether the method is supported or PEAR_Error on
|
|
638 * failure.
|
|
639 */
|
|
640 function hasAuthMech($method)
|
|
641 {
|
|
642 if (NET_SIEVE_STATE_DISCONNECTED == $this->_state) {
|
|
643 return $this->_pear->raiseError('Not currently connected', 7);
|
|
644 }
|
|
645
|
|
646 $method = trim($this->_toUpper($method));
|
35
|
647
|
0
|
648 if (is_array($this->_capability['sasl'])) {
|
|
649 foreach ($this->_capability['sasl'] as $sasl) {
|
|
650 if ($sasl == $method) {
|
|
651 return true;
|
|
652 }
|
|
653 }
|
|
654 }
|
|
655
|
|
656 return false;
|
|
657 }
|
|
658
|
|
659 /**
|
|
660 * Handles the authentication using any known method.
|
|
661 *
|
|
662 * @param string $uid The userid to authenticate as.
|
|
663 * @param string $pwd The password to authenticate with.
|
|
664 * @param string $userMethod The method to use. If empty, the class chooses
|
|
665 * the best (strongest) available method.
|
|
666 * @param string $euser The effective uid to authenticate as.
|
|
667 *
|
|
668 * @return void
|
|
669 */
|
|
670 function _cmdAuthenticate($uid, $pwd, $userMethod = null, $euser = '')
|
|
671 {
|
|
672 $method = $this->_getBestAuthMethod($userMethod);
|
|
673 if (is_a($method, 'PEAR_Error')) {
|
|
674 return $method;
|
|
675 }
|
35
|
676
|
0
|
677 switch ($method) {
|
|
678 case 'DIGEST-MD5':
|
|
679 return $this->_authDigestMD5($uid, $pwd, $euser);
|
|
680 case 'CRAM-MD5':
|
|
681 $result = $this->_authCRAMMD5($uid, $pwd, $euser);
|
|
682 break;
|
|
683 case 'LOGIN':
|
|
684 $result = $this->_authLOGIN($uid, $pwd, $euser);
|
|
685 break;
|
|
686 case 'PLAIN':
|
|
687 $result = $this->_authPLAIN($uid, $pwd, $euser);
|
|
688 break;
|
|
689 case 'EXTERNAL':
|
|
690 $result = $this->_authEXTERNAL($uid, $pwd, $euser);
|
|
691 break;
|
35
|
692 case 'GSSAPI':
|
|
693 $result = $this->_authGSSAPI($pwd);
|
|
694 break;
|
0
|
695 default :
|
|
696 $result = $this->_pear->raiseError(
|
|
697 $method . ' is not a supported authentication method'
|
|
698 );
|
|
699 break;
|
|
700 }
|
|
701
|
|
702 $res = $this->_doCmd();
|
|
703 if (is_a($res, 'PEAR_Error')) {
|
|
704 return $res;
|
|
705 }
|
|
706
|
|
707 if ($this->_pear->isError($res = $this->_cmdCapability())) {
|
|
708 return $this->_pear->raiseError(
|
|
709 'Failed to connect, server said: ' . $res->getMessage(), 2
|
|
710 );
|
|
711 }
|
|
712
|
|
713 return $result;
|
|
714 }
|
|
715
|
|
716 /**
|
|
717 * Authenticates the user using the PLAIN method.
|
|
718 *
|
|
719 * @param string $user The userid to authenticate as.
|
|
720 * @param string $pass The password to authenticate with.
|
|
721 * @param string $euser The effective uid to authenticate as.
|
|
722 *
|
|
723 * @return void
|
|
724 */
|
|
725 function _authPLAIN($user, $pass, $euser)
|
|
726 {
|
|
727 return $this->_sendCmd(
|
|
728 sprintf(
|
|
729 'AUTHENTICATE "PLAIN" "%s"',
|
|
730 base64_encode($euser . chr(0) . $user . chr(0) . $pass)
|
|
731 )
|
|
732 );
|
|
733 }
|
|
734
|
|
735 /**
|
35
|
736 * Authenticates the user using the GSSAPI method.
|
|
737 *
|
|
738 * @note the PHP krb5 extension is required and the service principal and cname
|
|
739 * must have been set.
|
|
740 * @see setServicePrincipal()
|
|
741 *
|
|
742 * @return void
|
|
743 */
|
|
744 function _authGSSAPI()
|
|
745 {
|
|
746 if (!extension_loaded('krb5')) {
|
|
747 return $this->_pear->raiseError('The krb5 extension is required for GSSAPI authentication', 2);
|
|
748 }
|
|
749
|
|
750 if (!$this->_gssapiPrincipal) {
|
|
751 return $this->_pear->raiseError('No Kerberos service principal set', 2);
|
|
752 }
|
|
753
|
|
754 if (!$this->_gssapiCN) {
|
|
755 return $this->_pear->raiseError('No Kerberos service CName set', 2);
|
|
756 }
|
|
757
|
|
758 putenv('KRB5CCNAME=' . $this->_gssapiCN);
|
|
759
|
|
760 try {
|
|
761 $ccache = new KRB5CCache();
|
|
762 $ccache->open($this->_gssapiCN);
|
|
763
|
|
764 $gssapicontext = new GSSAPIContext();
|
|
765 $gssapicontext->acquireCredentials($ccache);
|
|
766
|
|
767 $token = '';
|
|
768 $success = $gssapicontext->initSecContext($this->_gssapiPrincipal, null, null, null, $token);
|
|
769 $token = base64_encode($token);
|
|
770 }
|
|
771 catch (Exception $e) {
|
|
772 return $this->_pear->raiseError('GSSAPI authentication failed: ' . $e->getMessage());
|
|
773 }
|
|
774
|
|
775 $this->_sendCmd("AUTHENTICATE \"GSSAPI\" {" . strlen($token) . "+}");
|
|
776
|
|
777 $response = $this->_doCmd($token, true);
|
|
778
|
|
779 try {
|
|
780 $challenge = base64_decode(substr($response, 1, -1));
|
|
781 $gssapicontext->unwrap($challenge, $challenge);
|
|
782 $gssapicontext->wrap($challenge, $challenge, true);
|
|
783 }
|
|
784 catch (Exception $e) {
|
|
785 return $this->_pear->raiseError('GSSAPI authentication failed: ' . $e->getMessage());
|
|
786 }
|
|
787
|
|
788 $response = base64_encode($challenge);
|
|
789
|
|
790 $this->_sendCmd("{" . strlen($response) . "+}");
|
|
791
|
|
792 return $this->_sendCmd($response);
|
|
793 }
|
|
794
|
|
795 /**
|
0
|
796 * Authenticates the user using the LOGIN method.
|
|
797 *
|
|
798 * @param string $user The userid to authenticate as.
|
|
799 * @param string $pass The password to authenticate with.
|
|
800 * @param string $euser The effective uid to authenticate as. Not used.
|
|
801 *
|
|
802 * @return void
|
|
803 */
|
|
804 function _authLOGIN($user, $pass, $euser)
|
|
805 {
|
|
806 $result = $this->_sendCmd('AUTHENTICATE "LOGIN"');
|
|
807 if (is_a($result, 'PEAR_Error')) {
|
|
808 return $result;
|
|
809 }
|
35
|
810
|
0
|
811 $result = $this->_doCmd('"' . base64_encode($user) . '"', true);
|
|
812 if (is_a($result, 'PEAR_Error')) {
|
|
813 return $result;
|
|
814 }
|
35
|
815
|
0
|
816 return $this->_doCmd('"' . base64_encode($pass) . '"', true);
|
|
817 }
|
|
818
|
|
819 /**
|
|
820 * Authenticates the user using the CRAM-MD5 method.
|
|
821 *
|
|
822 * @param string $user The userid to authenticate as.
|
|
823 * @param string $pass The password to authenticate with.
|
|
824 * @param string $euser The effective uid to authenticate as. Not used.
|
|
825 *
|
|
826 * @return void
|
|
827 */
|
|
828 function _authCRAMMD5($user, $pass, $euser)
|
|
829 {
|
|
830 $challenge = $this->_doCmd('AUTHENTICATE "CRAM-MD5"', true);
|
|
831 if (is_a($challenge, 'PEAR_Error')) {
|
|
832 return $challenge;
|
|
833 }
|
|
834
|
|
835 $auth_sasl = new Auth_SASL;
|
|
836 $cram = $auth_sasl->factory('crammd5');
|
|
837 $challenge = base64_decode(trim($challenge));
|
|
838 $response = $cram->getResponse($user, $pass, $challenge);
|
|
839
|
|
840 if (is_a($response, 'PEAR_Error')) {
|
|
841 return $response;
|
|
842 }
|
|
843
|
|
844 return $this->_sendStringResponse(base64_encode($response));
|
|
845 }
|
|
846
|
|
847 /**
|
|
848 * Authenticates the user using the DIGEST-MD5 method.
|
|
849 *
|
|
850 * @param string $user The userid to authenticate as.
|
|
851 * @param string $pass The password to authenticate with.
|
|
852 * @param string $euser The effective uid to authenticate as.
|
|
853 *
|
|
854 * @return void
|
|
855 */
|
|
856 function _authDigestMD5($user, $pass, $euser)
|
|
857 {
|
|
858 $challenge = $this->_doCmd('AUTHENTICATE "DIGEST-MD5"', true);
|
|
859 if (is_a($challenge, 'PEAR_Error')) {
|
|
860 return $challenge;
|
|
861 }
|
|
862
|
|
863 $auth_sasl = new Auth_SASL;
|
|
864 $digest = $auth_sasl->factory('digestmd5');
|
|
865 $challenge = base64_decode(trim($challenge));
|
|
866
|
|
867 // @todo Really 'localhost'?
|
|
868 $response = $digest->getResponse($user, $pass, $challenge, 'localhost', 'sieve', $euser);
|
|
869 if (is_a($response, 'PEAR_Error')) {
|
|
870 return $response;
|
|
871 }
|
|
872
|
|
873 $result = $this->_sendStringResponse(base64_encode($response));
|
|
874 if (is_a($result, 'PEAR_Error')) {
|
|
875 return $result;
|
|
876 }
|
35
|
877
|
0
|
878 $result = $this->_doCmd('', true);
|
|
879 if (is_a($result, 'PEAR_Error')) {
|
|
880 return $result;
|
|
881 }
|
35
|
882
|
0
|
883 if ($this->_toUpper(substr($result, 0, 2)) == 'OK') {
|
|
884 return;
|
|
885 }
|
|
886
|
|
887 /* We don't use the protocol's third step because SIEVE doesn't allow
|
|
888 * subsequent authentication, so we just silently ignore it. */
|
|
889 $result = $this->_sendStringResponse('');
|
|
890 if (is_a($result, 'PEAR_Error')) {
|
|
891 return $result;
|
|
892 }
|
|
893
|
|
894 return $this->_doCmd();
|
|
895 }
|
|
896
|
|
897 /**
|
|
898 * Authenticates the user using the EXTERNAL method.
|
|
899 *
|
|
900 * @param string $user The userid to authenticate as.
|
|
901 * @param string $pass The password to authenticate with.
|
|
902 * @param string $euser The effective uid to authenticate as.
|
|
903 *
|
|
904 * @return void
|
|
905 *
|
35
|
906 * @since 1.1.7
|
0
|
907 */
|
|
908 function _authEXTERNAL($user, $pass, $euser)
|
|
909 {
|
|
910 $cmd = sprintf(
|
|
911 'AUTHENTICATE "EXTERNAL" "%s"',
|
|
912 base64_encode(strlen($euser) ? $euser : $user)
|
|
913 );
|
35
|
914
|
0
|
915 return $this->_sendCmd($cmd);
|
|
916 }
|
|
917
|
|
918 /**
|
|
919 * Removes a script from the server.
|
|
920 *
|
|
921 * @param string $scriptname Name of the script to delete.
|
|
922 *
|
|
923 * @return boolean True on success, PEAR_Error otherwise.
|
|
924 */
|
|
925 function _cmdDeleteScript($scriptname)
|
|
926 {
|
|
927 if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
|
|
928 return $this->_pear->raiseError('Not currently in AUTHORISATION state', 1);
|
|
929 }
|
|
930
|
|
931 $res = $this->_doCmd(sprintf('DELETESCRIPT %s', $this->_escape($scriptname)));
|
|
932 if (is_a($res, 'PEAR_Error')) {
|
|
933 return $res;
|
|
934 }
|
35
|
935
|
0
|
936 return true;
|
|
937 }
|
|
938
|
|
939 /**
|
|
940 * Retrieves the contents of the named script.
|
|
941 *
|
|
942 * @param string $scriptname Name of the script to retrieve.
|
|
943 *
|
|
944 * @return string The script if successful, PEAR_Error otherwise.
|
|
945 */
|
|
946 function _cmdGetScript($scriptname)
|
|
947 {
|
|
948 if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
|
|
949 return $this->_pear->raiseError('Not currently in AUTHORISATION state', 1);
|
|
950 }
|
|
951
|
|
952 $res = $this->_doCmd(sprintf('GETSCRIPT %s', $this->_escape($scriptname)));
|
|
953 if (is_a($res, 'PEAR_Error')) {
|
|
954 return $res;
|
|
955 }
|
|
956
|
|
957 return preg_replace('/^{[0-9]+}\r\n/', '', $res);
|
|
958 }
|
|
959
|
|
960 /**
|
|
961 * Sets the active script, i.e. the one that gets run on new mail by the
|
|
962 * server.
|
|
963 *
|
|
964 * @param string $scriptname The name of the script to mark as active.
|
|
965 *
|
|
966 * @return boolean True on success, PEAR_Error otherwise.
|
|
967 */
|
|
968 function _cmdSetActive($scriptname)
|
|
969 {
|
|
970 if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
|
|
971 return $this->_pear->raiseError('Not currently in AUTHORISATION state', 1);
|
|
972 }
|
|
973
|
|
974 $res = $this->_doCmd(sprintf('SETACTIVE %s', $this->_escape($scriptname)));
|
|
975 if (is_a($res, 'PEAR_Error')) {
|
|
976 return $res;
|
|
977 }
|
|
978
|
|
979 return true;
|
|
980 }
|
|
981
|
|
982 /**
|
|
983 * Returns the list of scripts on the server.
|
|
984 *
|
|
985 * @return array An array with the list of scripts in the first element
|
|
986 * and the active script in the second element on success,
|
|
987 * PEAR_Error otherwise.
|
|
988 */
|
|
989 function _cmdListScripts()
|
|
990 {
|
|
991 if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
|
|
992 return $this->_pear->raiseError('Not currently in AUTHORISATION state', 1);
|
|
993 }
|
|
994
|
|
995 $res = $this->_doCmd('LISTSCRIPTS');
|
|
996 if (is_a($res, 'PEAR_Error')) {
|
|
997 return $res;
|
|
998 }
|
|
999
|
|
1000 $scripts = array();
|
|
1001 $activescript = null;
|
|
1002 $res = explode("\r\n", $res);
|
|
1003 foreach ($res as $value) {
|
|
1004 if (preg_match('/^"(.*)"( ACTIVE)?$/i', $value, $matches)) {
|
|
1005 $script_name = stripslashes($matches[1]);
|
|
1006 $scripts[] = $script_name;
|
|
1007 if (!empty($matches[2])) {
|
|
1008 $activescript = $script_name;
|
|
1009 }
|
|
1010 }
|
|
1011 }
|
|
1012
|
|
1013 return array($scripts, $activescript);
|
|
1014 }
|
|
1015
|
|
1016 /**
|
|
1017 * Adds a script to the server.
|
|
1018 *
|
|
1019 * @param string $scriptname Name of the new script.
|
|
1020 * @param string $scriptdata The new script.
|
|
1021 *
|
|
1022 * @return boolean True on success, PEAR_Error otherwise.
|
|
1023 */
|
|
1024 function _cmdPutScript($scriptname, $scriptdata)
|
|
1025 {
|
|
1026 if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
|
|
1027 return $this->_pear->raiseError('Not currently in AUTHORISATION state', 1);
|
|
1028 }
|
|
1029
|
|
1030 $stringLength = $this->_getLineLength($scriptdata);
|
|
1031 $command = sprintf(
|
|
1032 "PUTSCRIPT %s {%d+}\r\n%s",
|
|
1033 $this->_escape($scriptname),
|
|
1034 $stringLength,
|
|
1035 $scriptdata
|
|
1036 );
|
|
1037
|
|
1038 $res = $this->_doCmd($command);
|
|
1039 if (is_a($res, 'PEAR_Error')) {
|
|
1040 return $res;
|
|
1041 }
|
|
1042
|
|
1043 return true;
|
|
1044 }
|
|
1045
|
|
1046 /**
|
|
1047 * Logs out of the server and terminates the connection.
|
|
1048 *
|
|
1049 * @param boolean $sendLogoutCMD Whether to send LOGOUT command before
|
|
1050 * disconnecting.
|
|
1051 *
|
|
1052 * @return boolean True on success, PEAR_Error otherwise.
|
|
1053 */
|
|
1054 function _cmdLogout($sendLogoutCMD = true)
|
|
1055 {
|
|
1056 if (NET_SIEVE_STATE_DISCONNECTED == $this->_state) {
|
|
1057 return $this->_pear->raiseError('Not currently connected', 1);
|
|
1058 }
|
|
1059
|
|
1060 if ($sendLogoutCMD) {
|
|
1061 $res = $this->_doCmd('LOGOUT');
|
|
1062 if (is_a($res, 'PEAR_Error')) {
|
|
1063 return $res;
|
|
1064 }
|
|
1065 }
|
|
1066
|
|
1067 $this->_sock->disconnect();
|
|
1068 $this->_state = NET_SIEVE_STATE_DISCONNECTED;
|
|
1069
|
|
1070 return true;
|
|
1071 }
|
|
1072
|
|
1073 /**
|
|
1074 * Sends the CAPABILITY command
|
|
1075 *
|
|
1076 * @return boolean True on success, PEAR_Error otherwise.
|
|
1077 */
|
|
1078 function _cmdCapability()
|
|
1079 {
|
|
1080 if (NET_SIEVE_STATE_DISCONNECTED == $this->_state) {
|
|
1081 return $this->_pear->raiseError('Not currently connected', 1);
|
|
1082 }
|
|
1083 $res = $this->_doCmd('CAPABILITY');
|
|
1084 if (is_a($res, 'PEAR_Error')) {
|
|
1085 return $res;
|
|
1086 }
|
|
1087 $this->_parseCapability($res);
|
|
1088 return true;
|
|
1089 }
|
|
1090
|
|
1091 /**
|
|
1092 * Parses the response from the CAPABILITY command and stores the result
|
|
1093 * in $_capability.
|
|
1094 *
|
|
1095 * @param string $data The response from the capability command.
|
|
1096 *
|
|
1097 * @return void
|
|
1098 */
|
|
1099 function _parseCapability($data)
|
|
1100 {
|
|
1101 // Clear the cached capabilities.
|
|
1102 $this->_capability = array('sasl' => array(),
|
|
1103 'extensions' => array());
|
|
1104
|
|
1105 $data = preg_split('/\r?\n/', $this->_toUpper($data), -1, PREG_SPLIT_NO_EMPTY);
|
|
1106
|
|
1107 for ($i = 0; $i < count($data); $i++) {
|
|
1108 if (!preg_match('/^"([A-Z]+)"( "(.*)")?$/', $data[$i], $matches)) {
|
|
1109 continue;
|
|
1110 }
|
|
1111 switch ($matches[1]) {
|
|
1112 case 'IMPLEMENTATION':
|
|
1113 $this->_capability['implementation'] = $matches[3];
|
|
1114 break;
|
|
1115
|
|
1116 case 'SASL':
|
35
|
1117 if (!empty($matches[3])) {
|
|
1118 $this->_capability['sasl'] = preg_split('/\s+/', $matches[3]);
|
|
1119 }
|
0
|
1120 break;
|
|
1121
|
|
1122 case 'SIEVE':
|
35
|
1123 if (!empty($matches[3])) {
|
|
1124 $this->_capability['extensions'] = preg_split('/\s+/', $matches[3]);
|
|
1125 }
|
0
|
1126 break;
|
|
1127
|
|
1128 case 'STARTTLS':
|
|
1129 $this->_capability['starttls'] = true;
|
|
1130 break;
|
|
1131 }
|
|
1132 }
|
|
1133 }
|
|
1134
|
|
1135 /**
|
|
1136 * Sends a command to the server
|
|
1137 *
|
|
1138 * @param string $cmd The command to send.
|
|
1139 *
|
|
1140 * @return void
|
|
1141 */
|
|
1142 function _sendCmd($cmd)
|
|
1143 {
|
|
1144 $status = $this->_sock->getStatus();
|
|
1145 if (is_a($status, 'PEAR_Error') || $status['eof']) {
|
|
1146 return $this->_pear->raiseError('Failed to write to socket: connection lost');
|
|
1147 }
|
|
1148 $error = $this->_sock->write($cmd . "\r\n");
|
|
1149 if (is_a($error, 'PEAR_Error')) {
|
|
1150 return $this->_pear->raiseError(
|
|
1151 'Failed to write to socket: ' . $error->getMessage()
|
|
1152 );
|
|
1153 }
|
|
1154 $this->_debug("C: $cmd");
|
|
1155 }
|
|
1156
|
|
1157 /**
|
|
1158 * Sends a string response to the server.
|
|
1159 *
|
|
1160 * @param string $str The string to send.
|
|
1161 *
|
|
1162 * @return void
|
|
1163 */
|
|
1164 function _sendStringResponse($str)
|
|
1165 {
|
|
1166 return $this->_sendCmd('{' . $this->_getLineLength($str) . "+}\r\n" . $str);
|
|
1167 }
|
|
1168
|
|
1169 /**
|
|
1170 * Receives a single line from the server.
|
|
1171 *
|
|
1172 * @return string The server response line.
|
|
1173 */
|
|
1174 function _recvLn()
|
|
1175 {
|
|
1176 $lastline = $this->_sock->gets(8192);
|
|
1177 if (is_a($lastline, 'PEAR_Error')) {
|
|
1178 return $this->_pear->raiseError(
|
|
1179 'Failed to read from socket: ' . $lastline->getMessage()
|
|
1180 );
|
|
1181 }
|
|
1182
|
|
1183 $lastline = rtrim($lastline);
|
|
1184 $this->_debug("S: $lastline");
|
|
1185
|
|
1186 if ($lastline === '') {
|
|
1187 return $this->_pear->raiseError('Failed to read from socket');
|
|
1188 }
|
|
1189
|
|
1190 return $lastline;
|
|
1191 }
|
|
1192
|
|
1193 /**
|
|
1194 * Receives a number of bytes from the server.
|
|
1195 *
|
|
1196 * @param integer $length Number of bytes to read.
|
|
1197 *
|
|
1198 * @return string The server response.
|
|
1199 */
|
|
1200 function _recvBytes($length)
|
|
1201 {
|
|
1202 $response = '';
|
|
1203 $response_length = 0;
|
|
1204 while ($response_length < $length) {
|
|
1205 $response .= $this->_sock->read($length - $response_length);
|
|
1206 $response_length = $this->_getLineLength($response);
|
|
1207 }
|
|
1208 $this->_debug('S: ' . rtrim($response));
|
|
1209 return $response;
|
|
1210 }
|
|
1211
|
|
1212 /**
|
|
1213 * Send a command and retrieves a response from the server.
|
|
1214 *
|
|
1215 * @param string $cmd The command to send.
|
|
1216 * @param boolean $auth Whether this is an authentication command.
|
|
1217 *
|
|
1218 * @return string|PEAR_Error Reponse string if an OK response, PEAR_Error
|
|
1219 * if a NO response.
|
|
1220 */
|
|
1221 function _doCmd($cmd = '', $auth = false)
|
|
1222 {
|
|
1223 $referralCount = 0;
|
|
1224 while ($referralCount < $this->_maxReferralCount) {
|
|
1225 if (strlen($cmd)) {
|
|
1226 $error = $this->_sendCmd($cmd);
|
|
1227 if (is_a($error, 'PEAR_Error')) {
|
|
1228 return $error;
|
|
1229 }
|
|
1230 }
|
|
1231
|
|
1232 $response = '';
|
|
1233 while (true) {
|
|
1234 $line = $this->_recvLn();
|
|
1235 if (is_a($line, 'PEAR_Error')) {
|
|
1236 return $line;
|
|
1237 }
|
|
1238
|
|
1239 if (preg_match('/^(OK|NO)/i', $line, $tag)) {
|
|
1240 // Check for string literal message.
|
|
1241 if (preg_match('/{([0-9]+)}$/', $line, $matches)) {
|
|
1242 $line = substr($line, 0, -(strlen($matches[1]) + 2))
|
|
1243 . str_replace(
|
|
1244 "\r\n", ' ', $this->_recvBytes($matches[1] + 2)
|
|
1245 );
|
|
1246 }
|
|
1247
|
|
1248 if ('OK' == $this->_toUpper($tag[1])) {
|
|
1249 $response .= $line;
|
|
1250 return rtrim($response);
|
|
1251 }
|
|
1252
|
|
1253 return $this->_pear->raiseError(trim($response . substr($line, 2)), 3);
|
|
1254 }
|
|
1255
|
|
1256 if (preg_match('/^BYE/i', $line)) {
|
|
1257 $error = $this->disconnect(false);
|
|
1258 if (is_a($error, 'PEAR_Error')) {
|
|
1259 return $this->_pear->raiseError(
|
|
1260 'Cannot handle BYE, the error was: '
|
|
1261 . $error->getMessage(),
|
|
1262 4
|
|
1263 );
|
|
1264 }
|
|
1265 // Check for referral, then follow it. Otherwise, carp an
|
|
1266 // error.
|
|
1267 if (preg_match('/^bye \(referral "(sieve:\/\/)?([^"]+)/i', $line, $matches)) {
|
|
1268 // Replace the old host with the referral host
|
|
1269 // preserving any protocol prefix.
|
|
1270 $this->_data['host'] = preg_replace(
|
|
1271 '/\w+(?!(\w|\:\/\/)).*/', $matches[2],
|
|
1272 $this->_data['host']
|
|
1273 );
|
|
1274 $error = $this->_handleConnectAndLogin();
|
|
1275 if (is_a($error, 'PEAR_Error')) {
|
|
1276 return $this->_pear->raiseError(
|
|
1277 'Cannot follow referral to '
|
|
1278 . $this->_data['host'] . ', the error was: '
|
|
1279 . $error->getMessage(),
|
|
1280 5
|
|
1281 );
|
|
1282 }
|
|
1283 break;
|
|
1284 }
|
|
1285 return $this->_pear->raiseError(trim($response . $line), 6);
|
|
1286 }
|
|
1287
|
|
1288 if (preg_match('/^{([0-9]+)}/', $line, $matches)) {
|
|
1289 // Matches literal string responses.
|
|
1290 $line = $this->_recvBytes($matches[1] + 2);
|
|
1291 if (!$auth) {
|
|
1292 // Receive the pending OK only if we aren't
|
|
1293 // authenticating since string responses during
|
|
1294 // authentication don't need an OK.
|
|
1295 $this->_recvLn();
|
|
1296 }
|
|
1297 return $line;
|
|
1298 }
|
|
1299
|
|
1300 if ($auth) {
|
|
1301 // String responses during authentication don't need an
|
|
1302 // OK.
|
|
1303 $response .= $line;
|
|
1304 return rtrim($response);
|
|
1305 }
|
|
1306
|
|
1307 $response .= $line . "\r\n";
|
|
1308 $referralCount++;
|
|
1309 }
|
|
1310 }
|
|
1311
|
|
1312 return $this->_pear->raiseError('Max referral count (' . $referralCount . ') reached. Cyrus murder loop error?', 7);
|
|
1313 }
|
|
1314
|
|
1315 /**
|
|
1316 * Returns the name of the best authentication method that the server
|
|
1317 * has advertised.
|
|
1318 *
|
|
1319 * @param string $userMethod Only consider this method as available.
|
|
1320 *
|
|
1321 * @return string The name of the best supported authentication method or
|
|
1322 * a PEAR_Error object on failure.
|
|
1323 */
|
|
1324 function _getBestAuthMethod($userMethod = null)
|
|
1325 {
|
|
1326 if (!isset($this->_capability['sasl'])) {
|
|
1327 return $this->_pear->raiseError('This server doesn\'t support any authentication methods. SASL problem?');
|
|
1328 }
|
|
1329 if (!$this->_capability['sasl']) {
|
|
1330 return $this->_pear->raiseError('This server doesn\'t support any authentication methods.');
|
|
1331 }
|
|
1332
|
|
1333 if ($userMethod) {
|
|
1334 if (in_array($userMethod, $this->_capability['sasl'])) {
|
|
1335 return $userMethod;
|
|
1336 }
|
|
1337
|
|
1338 $msg = 'No supported authentication method found. The server supports these methods: %s, but we want to use: %s';
|
|
1339 return $this->_pear->raiseError(
|
|
1340 sprintf($msg, implode(', ', $this->_capability['sasl']), $userMethod)
|
|
1341 );
|
|
1342 }
|
|
1343
|
|
1344 foreach ($this->supportedAuthMethods as $method) {
|
|
1345 if (in_array($method, $this->_capability['sasl'])) {
|
|
1346 return $method;
|
|
1347 }
|
|
1348 }
|
|
1349
|
|
1350 $msg = 'No supported authentication method found. The server supports these methods: %s, but we only support: %s';
|
|
1351 return $this->_pear->raiseError(
|
|
1352 sprintf($msg, implode(', ', $this->_capability['sasl']), implode(', ', $this->supportedAuthMethods))
|
|
1353 );
|
|
1354 }
|
|
1355
|
|
1356 /**
|
|
1357 * Starts a TLS connection.
|
|
1358 *
|
|
1359 * @return boolean True on success, PEAR_Error on failure.
|
|
1360 */
|
|
1361 function _startTLS()
|
|
1362 {
|
|
1363 $res = $this->_doCmd('STARTTLS');
|
|
1364 if (is_a($res, 'PEAR_Error')) {
|
|
1365 return $res;
|
|
1366 }
|
|
1367
|
|
1368 if (isset($this->_options['ssl']['crypto_method'])) {
|
|
1369 $crypto_method = $this->_options['ssl']['crypto_method'];
|
35
|
1370 } else {
|
0
|
1371 // There is no flag to enable all TLS methods. Net_SMTP
|
|
1372 // handles enabling TLS similarly.
|
|
1373 $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT
|
|
1374 | @STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
|
|
1375 | @STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
|
|
1376 }
|
|
1377
|
|
1378 if (!stream_socket_enable_crypto($this->_sock->fp, true, $crypto_method)) {
|
|
1379 return $this->_pear->raiseError('Failed to establish TLS connection', 2);
|
|
1380 }
|
|
1381
|
|
1382 $this->_debug('STARTTLS negotiation successful');
|
|
1383
|
|
1384 // The server should be sending a CAPABILITY response after
|
|
1385 // negotiating TLS. Read it, and ignore if it doesn't.
|
|
1386 // Unfortunately old Cyrus versions are broken and don't send a
|
|
1387 // CAPABILITY response, thus we would wait here forever. Parse the
|
|
1388 // Cyrus version and work around this broken behavior.
|
|
1389 if (!preg_match('/^CYRUS TIMSIEVED V([0-9.]+)/', $this->_capability['implementation'], $matches)
|
|
1390 || version_compare($matches[1], '2.3.10', '>=')
|
|
1391 ) {
|
35
|
1392 $res = $this->_doCmd();
|
0
|
1393 }
|
|
1394
|
35
|
1395 // Reset capabilities (use unattended capabilities)
|
|
1396 $this->_parseCapability(is_string($res) ? $res : '');
|
|
1397
|
|
1398 // Query the server capabilities again now that we are under encryption.
|
|
1399 if (empty($this->_capability['implementation'])) {
|
|
1400 $res = $this->_cmdCapability();
|
|
1401 if (is_a($res, 'PEAR_Error')) {
|
|
1402 return $this->_pear->raiseError(
|
|
1403 'Failed to connect, server said: ' . $res->getMessage(), 2
|
|
1404 );
|
|
1405 }
|
0
|
1406 }
|
|
1407
|
|
1408 return true;
|
|
1409 }
|
|
1410
|
|
1411 /**
|
|
1412 * Returns the length of a string.
|
|
1413 *
|
|
1414 * @param string $string A string.
|
|
1415 *
|
|
1416 * @return integer The length of the string.
|
|
1417 */
|
|
1418 function _getLineLength($string)
|
|
1419 {
|
|
1420 if (extension_loaded('mbstring')) {
|
35
|
1421 return mb_strlen($string, '8bit');
|
0
|
1422 } else {
|
|
1423 return strlen($string);
|
|
1424 }
|
|
1425 }
|
|
1426
|
|
1427 /**
|
|
1428 * Locale independant strtoupper() implementation.
|
|
1429 *
|
|
1430 * @param string $string The string to convert to lowercase.
|
|
1431 *
|
|
1432 * @return string The lowercased string, based on ASCII encoding.
|
|
1433 */
|
|
1434 function _toUpper($string)
|
|
1435 {
|
|
1436 $language = setlocale(LC_CTYPE, 0);
|
|
1437 setlocale(LC_CTYPE, 'C');
|
|
1438 $string = strtoupper($string);
|
|
1439 setlocale(LC_CTYPE, $language);
|
|
1440 return $string;
|
|
1441 }
|
|
1442
|
|
1443 /**
|
|
1444 * Converts strings into RFC's quoted-string or literal-c2s form.
|
|
1445 *
|
|
1446 * @param string $string The string to convert.
|
|
1447 *
|
|
1448 * @return string Result string.
|
|
1449 */
|
|
1450 function _escape($string)
|
|
1451 {
|
|
1452 // Some implementations don't allow UTF-8 characters in quoted-string,
|
|
1453 // use literal-c2s.
|
|
1454 if (preg_match('/[^\x01-\x09\x0B-\x0C\x0E-\x7F]/', $string)) {
|
|
1455 return sprintf("{%d+}\r\n%s", $this->_getLineLength($string), $string);
|
|
1456 }
|
|
1457
|
|
1458 return '"' . addcslashes($string, '\\"') . '"';
|
|
1459 }
|
|
1460
|
|
1461 /**
|
|
1462 * Write debug text to the current debug output handler.
|
|
1463 *
|
|
1464 * @param string $message Debug message text.
|
|
1465 *
|
|
1466 * @return void
|
|
1467 */
|
|
1468 function _debug($message)
|
|
1469 {
|
|
1470 if ($this->_debug) {
|
|
1471 if ($this->_debug_handler) {
|
|
1472 call_user_func_array($this->_debug_handler, array(&$this, $message));
|
|
1473 } else {
|
|
1474 echo "$message\n";
|
|
1475 }
|
|
1476 }
|
|
1477 }
|
|
1478 }
|