Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Net_IMAP provides an implementation of the IMAP protocol
4
 *
5
 * PHP Version 4
6
 *
7
 * @category  Networking
8
 * @package   Net_IMAP
9
 * @author    Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
10
 * @copyright 1997-2003 The PHP Group
11
 * @license   PHP license
12
 * @version   CVS: $Id: IMAP.php,v 1.27 2008/03/09 22:47:35 hudeldudel Exp $
13
 * @link      http://pear.php.net/package/Net_IMAP
14
 */
15
 
16
/**
17
 * IMAPProtocol.php holds protocol implementation for IMAP.php
18
 */
19
require_once 'Net/IMAPProtocol.php';
20
 
21
 
22
/**
23
 * Provides an implementation of the IMAP protocol using PEAR's
24
 * Net_Socket:: class.
25
 *
26
 * @category Networking
27
 * @package  Net_IMAP
28
 * @author   Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
29
 * @license  PHP license
30
 * @link     http://pear.php.net/package/Net_IMAP
31
 */
32
class Net_IMAP extends Net_IMAPProtocol
33
{
34
    /**
35
     * Constructor
36
     *
37
     * Instantiates a new Net_SMTP object, overriding any defaults
38
     * with parameters that are passed in.
39
     *
40
     * @param string $host           The server to connect to.
41
     * @param int    $port           The port to connect to.
42
     * @param bool   $enableSTARTTLS Enable STARTTLS support
43
     */
44
    function Net_IMAP($host = 'localhost', $port = 143, $enableSTARTTLS = true)
45
    {
46
        $this->Net_IMAPProtocol();
47
        $ret = $this->connect($host, $port, $enableSTARTTLS);
48
    }
49
 
50
 
51
 
52
    /**
53
     * Attempt to connect to the IMAP server located at $host $port
54
     *
55
     * @param string $host           The IMAP server
56
     * @param string $port           The IMAP port
57
     * @param bool   $enableSTARTTLS enable STARTTLS support
58
     *
59
     *          It is only useful in a very few circunstances
60
     *          because the contructor already makes this job
61
     *
62
     * @return true on success or PEAR_Error
63
     *
64
     * @access  public
65
     * @since   1.0
66
     */
67
    function connect($host, $port, $enableSTARTTLS = true)
68
    {
69
        $ret = $this->cmdConnect($host, $port);
70
        if ($ret === true) {
71
            // Determine server capabilities
72
            $res = $this->cmdCapability();
73
 
74
            // check if we can enable TLS via STARTTLS
75
            // (requires PHP 5 >= 5.1.0RC1 for stream_socket_enable_crypto)
76
            if ($this->hasCapability('STARTTLS') === true
77
                && $enableSTARTTLS === true
78
                && function_exists('stream_socket_enable_crypto') === true) {
79
                if (PEAR::isError($res = $this->cmdStartTLS())) {
80
                    return $res;
81
                }
82
            }
83
            return $ret;
84
        }
85
        if (empty($ret)) {
86
            return new PEAR_Error("Unexpected response on connection");
87
        }
88
        if (PEAR::isError($ret)) {
89
            return $ret;
90
        }
91
        if (isset($ret['RESPONSE']['CODE'])) {
92
            if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
93
                return new PEAR_Error($ret['RESPONSE']['CODE']
94
                                      . ', '
95
                                      . $ret['RESPONSE']['STR_CODE']);
96
            }
97
        }
98
 
99
        return $ret;
100
    }
101
 
102
 
103
 
104
    /**
105
     * Attempt to authenticate to the IMAP server.
106
     *
107
     * @param string  $user            The userid to authenticate as.
108
     * @param string  $pass            The password to authenticate with.
109
     * @param string  $useauthenticate true: authenticate using
110
     *        the IMAP AUTHENTICATE command. false: authenticate using
111
     *        the IMAP AUTHENTICATE command. 'string': authenticate using
112
     *        the IMAP AUTHENTICATE command but using the authMethod in 'string'
113
     * @param boolean $selectMailbox   automaticaly select inbox on login
114
     *        (false does not)
115
     *
116
     * @return  true on success or PEAR_Error
117
     *
118
     * @access  public
119
     * @since   1.0
120
     */
121
    function login($user, $pass, $useauthenticate = true, $selectMailbox=true)
122
    {
123
        if ($useauthenticate) {
124
            // $useauthenticate is a string if the user hardcodes an AUTHMethod
125
            // (the user calls $imap->login("user","password","CRAM-MD5"); for
126
            // example!
127
 
128
            if (is_string($useauthenticate)) {
129
                $method = $useauthenticate;
130
            } else {
131
                $method = null;
132
            }
133
 
134
            //Try the selected Auth method
135
            if (PEAR::isError($ret = $this->cmdAuthenticate($user,
136
                                                            $pass,
137
                                                            $method))) {
138
                // Verify the methods that we have in common with the server
139
                if (is_array($this->_serverAuthMethods)) {
140
                    $commonMethods = array_intersect($this->supportedAuthMethods, $this->_serverAuthMethods);
141
                } else {
142
                    $this->_serverAuthMethods = null;
143
                }
144
                if ($this->_serverAuthMethods == null
145
                    || count($commonMethods) == 0
146
                    || $this->supportedAuthMethods == null) {
147
                    // The server does not have any auth method, so I try LOGIN
148
                    if ( PEAR::isError($ret = $this->cmdLogin($user, $pass))) {
149
                        return $ret;
150
                    }
151
                } else {
152
                    return $ret;
153
                }
154
            }
155
            if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
156
                return new PEAR_Error($ret['RESPONSE']['CODE']
157
                                      . ', '
158
                                      . $ret['RESPONSE']['STR_CODE']);
159
            }
160
        } else {
161
            //The user request "PLAIN"  auth, we use the login command
162
            if (PEAR::isError($ret = $this->cmdLogin($user, $pass))) {
163
                return $ret;
164
            }
165
            if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
166
                return new PEAR_Error($ret['RESPONSE']['CODE']
167
                                      . ', '
168
                                      . $ret['RESPONSE']['STR_CODE']);
169
            }
170
        }
171
 
172
        if ($selectMailbox) {
173
            //Select INBOX
174
            if (PEAR::isError($ret = $this->cmdSelect($this->getCurrentMailbox()))) {
175
                return $ret;
176
            }
177
        }
178
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
179
            return new PEAR_Error($ret['RESPONSE']['CODE']
180
                                  . ', '
181
                                  . $ret['RESPONSE']['STR_CODE']);
182
        }
183
        return true;
184
    }
185
 
186
 
187
 
188
    /**
189
     * Disconnect function. Sends the QUIT command
190
     * and closes the socket.
191
     *
192
     * @param boolean $expungeOnExit (default = false)
193
     *
194
     * @return  mixed   true on success / Pear_Error on failure
195
     *
196
     * @access  public
197
     */
198
    function disconnect($expungeOnExit = false)
199
    {
200
        if ($expungeOnExit) {
201
            if (PEAR::isError($ret = $this->cmdExpunge())) {
202
                return $ret;
203
            }
204
            if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
205
                $ret = $this->cmdLogout();
206
                return new PEAR_Error($ret['RESPONSE']['CODE']
207
                                      . ', '
208
                                      . $ret['RESPONSE']['STR_CODE']);
209
            }
210
        }
211
 
212
        if (PEAR::isError($ret = $this->cmdLogout())) {
213
            return $ret;
214
        }
215
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
216
            return new PEAR_Error($ret['RESPONSE']['CODE']
217
                                  . ', '
218
                                  . $ret['RESPONSE']['STR_CODE']);
219
        }
220
 
221
        return true;
222
    }
223
 
224
 
225
 
226
    /**
227
     * Changes the default/current mailbox to $mailbox
228
     *
229
     * @param string $mailbox Mailbox to select
230
     *
231
     * @return mixed true on success / Pear_Error on failure
232
     *
233
     * @access public
234
     */
235
    function selectMailbox($mailbox)
236
    {
237
        if (PEAR::isError($ret = $this->cmdSelect($mailbox))) {
238
            return $ret;
239
        }
240
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
241
            return new PEAR_Error($ret['RESPONSE']['CODE']
242
                                  . ', '
243
                                  . $ret['RESPONSE']['STR_CODE']);
244
        }
245
        return true;
246
    }
247
 
248
 
249
 
250
    /**
251
     * Checks the mailbox $mailbox
252
     *
253
     * @param string $mailbox Mailbox to examine
254
     *
255
     * @return mixed true on success / Pear_Error on failure
256
     *
257
     * @access public
258
     */
259
    function examineMailbox($mailbox)
260
    {
261
        if (PEAR::isError($ret = $this->cmdExamine($mailbox))) {
262
            return $ret;
263
        }
264
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
265
            return new PEAR_Error($ret['RESPONSE']['CODE']
266
                                  . ', '
267
                                  . $ret['RESPONSE']['STR_CODE']);
268
        }
269
 
270
        //$ret_aux["EXISTS"]=$ret["PARSED"]["EXISTS"];
271
        //$ret_aux["RECENT"]=$ret["PARSED"]["RECENT"];
272
        $ret = $ret['PARSED'];
273
        return $ret;
274
    }
275
 
276
 
277
 
278
    /**
279
     * Returns the raw headers of the specified message.
280
     *
281
     * @param int     $msg_id   Message number
282
     * @param string  $part_id  Part ID
283
     * @param boolean $uidFetch msg_id contains UID's instead of Message
284
     *                          Sequence Number if set to true
285
     *
286
     * @return mixed Either raw headers or false on error
287
     *
288
     * @access public
289
     */
290
    function getRawHeaders($msg_id, $part_id = '', $uidFetch = false)
291
    {
292
        if ($part_id != '') {
293
            $command = 'BODY[' . $part_id . '.HEADER]';
294
        } else {
295
            $command = 'BODY[HEADER]';
296
        }
297
        if ($uidFetch == true) {
298
            $ret = $this->cmdUidFetch($msg_id, $command);
299
        } else {
300
            $ret = $this->cmdFetch($msg_id, $command);
301
        }
302
        if (PEAR::isError($ret)) {
303
            return $ret;
304
        }
305
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
306
            return new PEAR_Error($ret['RESPONSE']['CODE']
307
                                  . ', '
308
                                  . $ret['RESPONSE']['STR_CODE']);
309
        }
310
        $ret = $ret['PARSED'][0]['EXT'][$command]['CONTENT'];
311
        return $ret;
312
    }
313
 
314
 
315
 
316
    /**
317
     * Returns the  headers of the specified message in an
318
     * associative array. Array keys are the header names, array
319
     * values are the header values. In the case of multiple headers
320
     * having the same names, eg Received:, the array value will be
321
     * an indexed array of all the header values.
322
     *
323
     * @param int     $msg_id      Message number
324
     * @param boolean $keysToUpper false (default) original header names
325
     *                             true change keys (header names) toupper
326
     * @param string  $part_id     Part ID
327
     * @param boolean $uidFetch    msg_id contains UID's instead of Message
328
     *                             Sequence Number if set to true
329
     *
330
     * @return mixed Either array of headers or false on error
331
     *
332
     * @access public
333
     */
334
    function getParsedHeaders($msg_id,
335
                              $keysToUpper = false,
336
                              $part_id = '',
337
                              $uidFetch = false)
338
    {
339
        if (PEAR::isError($ret = $this->getRawHeaders($msg_id,
340
                                                      $part_id,
341
                                                      $uidFetch))) {
342
            return $ret;
343
        }
344
 
345
        $raw_headers = rtrim($ret);
346
        // Unfold headers
347
        $raw_headers = preg_replace("/\r\n[ \t]+/", ' ', $raw_headers);
348
        $raw_headers = explode("\r\n", $raw_headers);
349
        foreach ($raw_headers as $value) {
350
            $name = substr($value, 0, $pos = strpos($value, ':'));
351
            if ($keysToUpper) {
352
                $name = strtoupper($name);
353
            }
354
            $value = ltrim(substr($value, $pos + 1));
355
            if (isset($headers[$name]) && is_array($headers[$name])) {
356
                $headers[$name][] = $value;
357
            } elseif (isset($headers[$name])) {
358
                $headers[$name] = array($headers[$name], $value);
359
            } else {
360
                $headers[$name] = $value;
361
            }
362
        }
363
        return $headers;
364
    }
365
 
366
 
367
 
368
    /**
369
     * Returns an array containing the message ID, the size and the UID
370
     * of each message selected.
371
     * message selection can be a valid IMAP command, a number or an array of
372
     * messages
373
     *
374
     * @param string $msg_id Message number
375
     *
376
     * @return mixed Either array of message data or PearError on error
377
     *
378
     * @access public
379
     */
380
    function getMessagesList($msg_id = null)
381
    {
382
        if ($msg_id != null) {
383
            if (is_array($msg_id)) {
384
                $message_set = $this->_getSearchListFromArray($msg_id);
385
            } else {
386
                $message_set = $msg_id;
387
            }
388
        } else {
389
            $message_set = '1:*';
390
        }
391
        if (PEAR::isError($ret = $this->cmdFetch($message_set,
392
                                                 '(RFC822.SIZE UID)'))) {
393
            return $ret;
394
        }
395
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
396
            return new PEAR_Error($ret['RESPONSE']['CODE']
397
                                  . ', '
398
                                  . $ret['RESPONSE']['STR_CODE']);
399
        }
400
        foreach ($ret['PARSED'] as $msg) {
401
            $ret_aux[] = array('msg_id' => $msg['NRO'],
402
                               'size'   => $msg['EXT']['RFC822.SIZE'],
403
                               'uidl'   => $msg['EXT']['UID']);
404
        }
405
        return $ret_aux;
406
    }
407
 
408
 
409
 
410
    /**
411
     * Message summary
412
     *
413
     * @param mixed   $msg_id   Message number
414
     * @param boolean $uidFetch msg_id contains UID's instead of Message
415
     *                          Sequence Number if set to true
416
     *
417
     * @return mixed Either array of headers or PEAR::Error on error
418
     *
419
     * @access public
420
     */
421
    function getSummary($msg_id = null, $uidFetch = false)
422
    {
423
        if ($msg_id != null) {
424
            if (is_array($msg_id)) {
425
                $message_set = $this->_getSearchListFromArray($msg_id);
426
            } else {
427
                $message_set = $msg_id;
428
            }
429
        } else {
430
            $message_set = '1:*';
431
        }
432
        if ($uidFetch) {
433
            $ret = $this->cmdUidFetch($message_set,
434
                                      '(RFC822.SIZE UID FLAGS ENVELOPE INTERNALDATE BODY.PEEK[HEADER.FIELDS (CONTENT-TYPE)])');
435
        } else {
436
            $ret = $this->cmdFetch($message_set,
437
                                   '(RFC822.SIZE UID FLAGS ENVELOPE INTERNALDATE BODY.PEEK[HEADER.FIELDS (CONTENT-TYPE)])');
438
        }
439
        // $ret=$this->cmdFetch($message_set,"(RFC822.SIZE UID FLAGS ENVELOPE INTERNALDATE BODY[1.MIME])");
440
        if (PEAR::isError($ret)) {
441
            return $ret;
442
        }
443
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
444
            return new PEAR_Error($ret['RESPONSE']['CODE']
445
                                  . ', '
446
                                  . $ret['RESPONSE']['STR_CODE']);
447
        }
448
 
449
        // print "<hr>"; var_dump($ret["PARSED"]); print "<hr>";
450
 
451
        if (isset($ret['PARSED'])) {
452
            for ($i=0; $i<count($ret['PARSED']); $i++) {
453
                $a                 = $ret['PARSED'][$i]['EXT']['ENVELOPE'];
454
                $a['MSG_NUM']      = $ret["PARSED"][$i]['NRO'];
455
                $a['UID']          = $ret["PARSED"][$i]['EXT']['UID'];
456
                $a['FLAGS']        = $ret["PARSED"][$i]['EXT']['FLAGS'];
457
                $a['INTERNALDATE'] = $ret["PARSED"][$i]['EXT']['INTERNALDATE'];
458
                $a['SIZE']         = $ret["PARSED"][$i]['EXT']['RFC822.SIZE'];
459
                if (isset($ret['PARSED'][$i]['EXT']['BODY[HEADER.FIELDS (CONTENT-TYPE)]']['CONTENT'])) {
460
                    if (preg_match('/^content-type: (.*);/iU', $ret['PARSED'][$i]['EXT']['BODY[HEADER.FIELDS (CONTENT-TYPE)]']['CONTENT'], $matches)) {
461
                        $a['MIMETYPE'] = strtolower($matches[1]);
462
                    }
463
                } elseif (isset($ret['PARSED'][$i]['EXT']['BODY[HEADER.FIELDS ("CONTENT-TYPE")]']['CONTENT'])) {
464
                    // some versions of cyrus send "CONTENT-TYPE" and CONTENT-TYPE only
465
                    if (preg_match('/^content-type: (.*);/iU', $ret['PARSED'][$i]['EXT']['BODY[HEADER.FIELDS ("CONTENT-TYPE")]']['CONTENT'], $matches)) {
466
                        $a['MIMETYPE'] = strtolower($matches[1]);
467
                    }
468
                }
469
                $env[] = $a;
470
                $a     = null;
471
            }
472
            return $env;
473
        }
474
 
475
        //return $ret;
476
    }
477
 
478
 
479
 
480
    /**
481
     * Returns the body of the message with given message number.
482
     *
483
     * @param string  $msg_id   Message number
484
     * @param boolean $uidFetch msg_id contains UID's instead of Message
485
     *                          Sequence Number if set to true
486
     * @param string  $partId   Part ID
487
     *
488
     * @return mixed Either message body or PEAR_Error on failure
489
     * @access public
490
     */
491
    function getBody($msg_id, $uidFetch = false, $partId = '')
492
    {
493
        if ($partId != '') {
494
            $partId = '.' . strtoupper($partId);
495
        }
496
        if ($uidFetch) {
497
            $ret = $this->cmdUidFetch($msg_id, 'BODY' . $partId . '[TEXT]');
498
        } else {
499
            $ret = $this->cmdFetch($msg_id, 'BODY' . $partId . '[TEXT]');
500
        }
501
        if (PEAR::isError($ret)) {
502
            return $ret;
503
        }
504
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
505
            return new PEAR_Error($ret['RESPONSE']['CODE']
506
                                  . ', '
507
                                  . $ret['RESPONSE']['STR_CODE']);
508
        }
509
        $ret = $ret['PARSED'][0]['EXT']['BODY[TEXT]']['CONTENT'];
510
        // $ret=$resp["PARSED"][0]["EXT"]["RFC822"]["CONTENT"];
511
        return $ret;
512
    }
513
 
514
 
515
    /**
516
     * Returns the body of the message with given message number.
517
     *
518
     * @param int     $msg_id   Message number
519
     * @param string  $partId   Message number
520
     * @param boolean $uidFetch msg_id contains UID's instead of Message
521
     *                          Sequence Number if set to true
522
     *
523
     * @return mixed Either message body or false on error
524
     *
525
     * @access public
526
     */
527
    function getBodyPart($msg_id, $partId, $uidFetch = false)
528
    {
529
        if ($uidFetch) {
530
            $ret = $this->cmdUidFetch($msg_id, 'BODY[' . $partId . ']');
531
        } else {
532
            $ret = $this->cmdFetch($msg_id, 'BODY[' . $partId . ']');
533
        }
534
        if (PEAR::isError($ret)) {
535
            return $ret;
536
        }
537
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
538
            return new PEAR_Error($ret['RESPONSE']['CODE']
539
                                  . ', '
540
                                  . $ret['RESPONSE']['STR_CODE']);
541
        }
542
        $ret = $ret['PARSED'][0]['EXT']['BODY[' . $partId . ']']['CONTENT'];
543
        //$ret=$resp["PARSED"][0]["EXT"]["RFC822"]["CONTENT"];
544
        return $ret;
545
    }
546
 
547
 
548
 
549
    /**
550
     * Returns the body of the message with given message number.
551
     *
552
     * @param int     $msg_id   Message number
553
     * @param boolean $uidFetch msg_id contains UID's instead of Message
554
     *                          Sequence Number if set to true
555
     *
556
     * @return mixed Either message body or false on error
557
     *
558
     * @access public
559
     */
560
    function getStructure($msg_id, $uidFetch = false)
561
    {
562
        // print "IMAP.php::getStructure<pre>";
563
        // $this->setDebug(true);
564
        // print "<pre>";
565
        if ($uidFetch) {
566
            $ret = $this->cmdUidFetch($msg_id, 'BODYSTRUCTURE');
567
        } else {
568
            $ret = $this->cmdFetch($msg_id, 'BODYSTRUCTURE');
569
        }
570
        if (PEAR::isError($ret)) {
571
            return $ret;
572
        }
573
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
574
            return new PEAR_Error($ret['RESPONSE']['CODE']
575
                                  . ', '
576
                                  . $ret['RESPONSE']['STR_CODE']);
577
        }
578
        $ret = $ret['PARSED'][0]['EXT']['BODYSTRUCTURE'][0];
579
 
580
        $mimeParts = array();
581
        $this->_parseStructureArray($ret, $mimeParts);
582
 
583
        return array_shift($mimeParts);
584
    }
585
 
586
 
587
    /**
588
     * Parse structure array
589
     *
590
     * @param array $_structure  Structure array
591
     * @param array &$_mimeParts Mime parts
592
     * @param int   $_partID     Part ID
593
     *
594
     * @return nothing
595
     *
596
     * @access private
597
     */
598
    function _parseStructureArray($_structure, &$_mimeParts, $_partID = '')
599
    {
600
        // something went wrong
601
        if (!is_array($_structure)) {
602
            return false;
603
        }
604
 
605
        // print "Net_IMAP::_parseStructureArray _partID: $_partID";
606
        $mimeParts = array();
607
        $subPartID = 1;
608
        if ($_partID == '') {
609
            $partID = '';
610
        } else {
611
            $partID = $_partID.'.';
612
        }
613
        if (is_array($_structure[0])) {
614
            $this->_parseStructureMultipartArray($_structure, $_mimeParts, $_partID);
615
        } else {
616
            switch (strtoupper($_structure[0])) {
617
            case 'TEXT':
618
                $this->_parseStructureTextArray($_structure,
619
                                                $_mimeParts,
620
                                                $partID . $subPartID);
621
                break;
622
 
623
            case 'MESSAGE':
624
                $this->_parseStructureMessageArray($_structure,
625
                                                   $_mimeParts,
626
                                                   $partID . $subPartID);
627
                break;
628
 
629
            default:
630
                $this->_parseStructureApplicationArray($_structure,
631
                                                       $_mimeParts,
632
                                                       $partID . $subPartID);
633
                break;
634
            }
635
        }
636
    }
637
 
638
 
639
 
640
    /**
641
     * Parse multibpart structure array
642
     *
643
     * @param array   $_structure       Structure array
644
     * @param array   &$_mimeParts      Mime parts
645
     * @param int     $_partID          Part ID
646
     * @param boolean $_parentIsMessage Parent is message/rfc822
647
     *
648
     * @return noting
649
     *
650
     * @access private
651
     */
652
    function _parseStructureMultipartArray($_structure,
653
                                           &$_mimeParts,
654
                                           $_partID,
655
                                           $_parentIsMessage = false)
656
    {
657
        // a multipart/mixed, multipart/report or multipart/alternative
658
        // or multipart/related get's no own partid, if the parent
659
        // is message/rfc822
660
        if ($_parentIsMessage == true && is_array($_structure[0])) {
661
            foreach ($_structure as $structurePart) {
662
                if (!is_array($structurePart)) {
663
                    $subType = strtolower($structurePart);
664
                    break;
665
                }
666
            }
667
            if ($subType == 'mixed'
668
                || $subType == 'report'
669
                || $subType == 'alternative'
670
                || $subType == 'related') {
671
                $_partID = substr($_partID, 0, strrpos($_partID, '.'));
672
            }
673
        }
674
 
675
        $subPartID = 1;
676
        if ($_partID == '') {
677
            $partID = '';
678
        } else {
679
            $partID = $_partID . '.';
680
        }
681
        $subMimeParts = array();
682
        foreach ($_structure as $structurePart) {
683
            if (is_array($structurePart)) {
684
                if (is_array($structurePart[0])) {
685
                    // another multipart inside the multipart
686
                    $this->_parseStructureMultipartArray($structurePart,
687
                                                         $subMimeParts,
688
                                                         $partID . $subPartID);
689
                } else {
690
                    switch(strtoupper($structurePart[0])) {
691
                    case 'IMAGE':
692
                        $this->_parseStructureImageArray($structurePart,
693
                                                         $subMimeParts,
694
                                                         $partID . $subPartID);
695
                        break;
696
                    case 'MESSAGE':
697
                        $this->_parseStructureMessageArray($structurePart,
698
                                                           $subMimeParts,
699
                                                           $partID . $subPartID);
700
                        break;
701
                    case 'TEXT':
702
                        $this->_parseStructureTextArray($structurePart,
703
                                                        $subMimeParts,
704
                                                        $partID . $subPartID);
705
                        break;
706
                    default:
707
                        $this->_parseStructureApplicationArray($structurePart,
708
                                                               $subMimeParts,
709
                                                               $partID . $subPartID);
710
                        break;
711
                    }
712
                }
713
                $subPartID++;
714
            } else {
715
                $part           = new stdClass;
716
                $part->type     = 'MULTIPART';
717
                $part->subType  = strtoupper($structurePart);
718
                $part->subParts = $subMimeParts;
719
 
720
                if ($_partID == '') {
721
                    $part->partID = 0;
722
                    $_mimeParts   = array(0 => $part);
723
                } else {
724
                    $part->partID         = $_partID;
725
                    $_mimeParts[$_partID] = $part;
726
                }
727
                return;
728
            }
729
        }
730
    }
731
 
732
 
733
 
734
    /**
735
     * Parse structure image array
736
     *
737
     * @param array $_structure  Structure array
738
     * @param array &$_mimeParts Mime parts
739
     * @param int   $_partID     Part ID
740
     *
741
     * @return noting
742
     *
743
     * @access private
744
     */
745
    function _parseStructureImageArray($_structure, &$_mimeParts, $_partID)
746
    {
747
        // print "Net_IMAP::_parseStructureImageArray _partID: $_partID<br>";
748
        $part         = $this->_parseStructureCommonFields($_structure);
749
        $part->cid    = $_structure[3];
750
        $part->partID = $_partID;
751
 
752
        $_mimeParts[$_partID] = $part;
753
    }
754
 
755
 
756
 
757
    /**
758
     * Parse structure application array
759
     *
760
     * @param array $_structure  Structure array
761
     * @param array &$_mimeParts Mime parts
762
     * @param int   $_partID     Part ID
763
     *
764
     * @return noting
765
     *
766
     * @access private
767
     */
768
    function _parseStructureApplicationArray($_structure,
769
                                             &$_mimeParts,
770
                                             $_partID)
771
    {
772
        // print "Net_IMAP::_parseStructureApplicationArray _partID: $_partID<br>";
773
        $part = $this->_parseStructureCommonFields($_structure);
774
        if (is_array($_structure[8])) {
775
            if (isset($_structure[8][0]) && $_structure[8][0] != 'NIL') {
776
                $part->disposition = strtoupper($_structure[8][0]);
777
            }
778
            if (is_array($_structure[8][1])) {
779
                foreach ($_structure[8][1] as $key => $value) {
780
                    if ($key%2 == 0) {
781
                        $part->dparameters[strtoupper($_structure[8][1][$key])] = $_structure[8][1][$key+1];
782
                    }
783
                }
784
            }
785
        }
786
        $part->partID = $_partID;
787
 
788
        $_mimeParts[$_partID] = $part;
789
    }
790
 
791
 
792
 
793
    /**
794
     * Parse structure message array
795
     *
796
     * @param array $_structure  Structure array
797
     * @param array &$_mimeParts Mime parts
798
     * @param int   $_partID     Part ID
799
     *
800
     * @return nothing
801
     *
802
     * @access private
803
     */
804
    function _parseStructureMessageArray($_structure, &$_mimeParts, $_partID)
805
    {
806
        // print "Net_IMAP::_parseStructureMessageArray _partID: $_partID<br>";
807
        $part = $this->_parseStructureCommonFields($_structure);
808
 
809
        if (is_array($_structure[8][0])) {
810
            $this->_parseStructureMultipartArray($_structure[8],
811
                                                 $subMimeParts,
812
                                                 $_partID . '.1',
813
                                                 true);
814
        } else {
815
            $this->_parseStructureArray($_structure[8],
816
                                        $subMimeParts,
817
                                        $_partID);
818
        }
819
 
820
        if (is_array($subMimeParts)) {
821
            $part->subParts = $subMimeParts;
822
        }
823
        $part->partID = $_partID;
824
 
825
        $_mimeParts[$_partID] = $part;
826
    }
827
 
828
 
829
 
830
    /**
831
     * Parse structure text array
832
     *
833
     * @param array $_structure  Structure array
834
     * @param array &$_mimeParts Mime parts
835
     * @param int   $_partID     Part ID
836
     *
837
     * @return nothing
838
     *
839
     * @access private
840
     */
841
    function _parseStructureTextArray($_structure, &$_mimeParts, $_partID)
842
    {
843
        // print "Net_IMAP::_parseStructureTextArray _partID: $_partID<br>";
844
        $part        = $this->_parseStructureCommonFields($_structure);
845
        $part->lines = $_structure[7];
846
 
847
        if (is_array($_structure[8])) {
848
            if (isset($_structure[8][0]) && $_structure[8][0] != 'NIL') {
849
                $part->disposition = strtoupper($_structure[8][0]);
850
            }
851
            if (is_array($_structure[8][1])) {
852
                foreach ($_structure[8][1] as $key => $value) {
853
                    if ($key%2 == 0) {
854
                        $part->dparameters[strtoupper($_structure[8][1][$key])] = $_structure[8][1][$key+1];
855
                    }
856
                }
857
            }
858
        }
859
 
860
        if (is_array($_structure[9])) {
861
            if (isset($_structure[9][0]) && $_structure[9][0] != 'NIL') {
862
                $part->disposition = strtoupper($_structure[9][0]);
863
            }
864
            if (is_array($_structure[9][1])) {
865
                foreach ($_structure[9][1] as $key => $value) {
866
                    if ($key%2 == 0) {
867
                        $part->dparameters[strtoupper($_structure[9][1][$key])] = $_structure[9][1][$key+1];
868
                    }
869
                }
870
            }
871
        }
872
 
873
        $part->partID = $_partID;
874
 
875
        $_mimeParts[$_partID] = $part;
876
    }
877
 
878
 
879
 
880
    /**
881
     * Parse structure common fields
882
     *
883
     * @param array &$_structure Structure array
884
     *
885
     * @return object part object (stdClass)
886
     *
887
     * @access private
888
     */
889
    function _parseStructureCommonFields(&$_structure)
890
    {
891
        // print "Net_IMAP::_parseStructureTextArray _partID: $_partID<br>";
892
        $part          = new stdClass;
893
        $part->type    = strtoupper($_structure[0]);
894
        $part->subType = strtoupper($_structure[1]);
895
 
896
        if (is_array($_structure[2])) {
897
            foreach ($_structure[2] as $key => $value) {
898
                if ($key%2 == 0) {
899
                    $part->parameters[strtoupper($_structure[2][$key])] = $_structure[2][$key+1];
900
                }
901
            }
902
        }
903
        $part->encoding = strtoupper($_structure[5]);
904
        $part->bytes    = $_structure[6];
905
 
906
        return $part;
907
    }
908
 
909
 
910
 
911
    /**
912
     * Returns the entire message with given message number.
913
     *
914
     * @param int     $msg_id               Message number (default = null)
915
     * @param boolean $indexIsMessageNumber true if index is a message number
916
     *                                      (default = true)
917
     *
918
     * @return mixed  Either entire message or false on error
919
     *
920
     * @access public
921
     */
922
    function getMessages($msg_id = null, $indexIsMessageNumber=true)
923
    {
924
        // $resp=$this->cmdFetch($msg_id,"(BODY[TEXT] BODY[HEADER])");
925
        if ($msg_id != null) {
926
            if (is_array($msg_id)) {
927
                $message_set = $this->_getSearchListFromArray($msg_id);
928
            } else {
929
                $message_set = $msg_id;
930
            }
931
        } else {
932
            $message_set = '1:*';
933
        }
934
 
935
        if (!$indexIsMessageNumber) {
936
            $ret = $this->cmdUidFetch($message_set, 'RFC822');
937
        } else {
938
            $ret = $this->cmdFetch($message_set, 'RFC822');
939
        }
940
        if (PEAR::isError($ret)) {
941
            return $ret;
942
        }
943
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
944
            return new PEAR_Error($ret['RESPONSE']['CODE']
945
                                  . ', '
946
                                  . $ret['RESPONSE']['STR_CODE']);
947
        }
948
        if (isset($ret['PARSED'])) {
949
            foreach ($ret['PARSED'] as $msg) {
950
                if (isset($msg['EXT']['RFC822']['CONTENT'])) {
951
                    if ($indexIsMessageNumber) {
952
                        $ret_aux[$msg['NRO']] = $msg['EXT']['RFC822']['CONTENT'];
953
                    } else {
954
                        $ret_aux[] = $msg['EXT']['RFC822']['CONTENT'];
955
                    }
956
                }
957
            }
958
            return $ret_aux;
959
        }
960
        return array();
961
    }
962
 
963
 
964
 
965
    /**
966
     * Returns number of messages in this mailbox
967
     *
968
     * @param string $mailbox the mailbox (default is current mailbox)
969
     *
970
     * @return mixed Either number of messages or Pear_Error on failure
971
     *
972
     * @access public
973
     */
974
    function getNumberOfMessages($mailbox = '')
975
    {
976
        if ($mailbox == '' || $mailbox == null) {
977
            $mailbox = $this->getCurrentMailbox();
978
        }
979
        if (PEAR::isError($ret = $this->cmdStatus($mailbox, 'MESSAGES'))) {
980
            return $ret;
981
        }
982
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
983
            return new PEAR_Error($ret['RESPONSE']['CODE']
984
                                  . ', '
985
                                  . $ret['RESPONSE']['STR_CODE']);
986
        }
987
        if (isset($ret['PARSED']['STATUS']['ATTRIBUTES']['MESSAGES'])) {
988
            if (!is_numeric($ret['PARSED']['STATUS']['ATTRIBUTES']['MESSAGES'])) {
989
                // if this array does not exists means that there is no
990
                // messages in the mailbox
991
                return 0;
992
            } else {
993
                return $ret['PARSED']['STATUS']['ATTRIBUTES']['MESSAGES'];
994
            }
995
 
996
        }
997
        return 0;
998
    }
999
 
1000
 
1001
 
1002
    /**
1003
     * Returns number of UnSeen messages in this mailbox
1004
     *
1005
     * @param string $mailbox the mailbox (default is current mailbox)
1006
     *
1007
     * @return mixed Either number of messages or Pear_Error on failure
1008
     *
1009
     * @access public
1010
     */
1011
    function getNumberOfUnSeenMessages($mailbox = '')
1012
    {
1013
        if ($mailbox == '' ) {
1014
            $mailbox = $this->getCurrentMailbox();
1015
        }
1016
        if (PEAR::isError($ret = $this->cmdStatus($mailbox, 'UNSEEN'))) {
1017
            return $ret;
1018
        }
1019
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1020
            return new PEAR_Error($ret["RESPONSE"]["CODE"]
1021
                                  . ', '
1022
                                  . $ret['RESPONSE']['STR_CODE']);
1023
        }
1024
        if (isset($ret['PARSED']['STATUS']['ATTRIBUTES']['UNSEEN'])) {
1025
            if (!is_numeric($ret['PARSED']['STATUS']['ATTRIBUTES']['UNSEEN'])) {
1026
                // if this array does not exists means that there is no messages in the mailbox
1027
                return 0;
1028
            } else {
1029
                return $ret['PARSED']['STATUS']['ATTRIBUTES']['UNSEEN'];
1030
            }
1031
 
1032
        }
1033
        return 0;
1034
    }
1035
 
1036
 
1037
 
1038
    /**
1039
     * Returns number of UnSeen messages in this mailbox
1040
     *
1041
     * @param string $mailbox the mailbox (default is current mailbox)
1042
     *
1043
     * @return mixed Either number of messages or Pear_Error on failure
1044
     *
1045
     * @access public
1046
     */
1047
    function getNumberOfRecentMessages($mailbox = '')
1048
    {
1049
        if ($mailbox == '' ) {
1050
            $mailbox = $this->getCurrentMailbox();
1051
        }
1052
        if (PEAR::isError($ret = $this->cmdStatus($mailbox, 'RECENT'))) {
1053
            return $ret;
1054
        }
1055
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1056
            return new PEAR_Error($ret['RESPONSE']['CODE']
1057
                                  . ', '
1058
                                  . $ret['RESPONSE']['STR_CODE']);
1059
        }
1060
        if (isset($ret['PARSED']['STATUS']['ATTRIBUTES']['RECENT'])) {
1061
            if (!is_numeric($ret['PARSED']['STATUS']['ATTRIBUTES']['RECENT'])) {
1062
                // if this array does not exists means that there is no messages in the mailbox
1063
                return 0;
1064
            } else {
1065
                return $ret['PARSED']['STATUS']['ATTRIBUTES']['RECENT'];
1066
            }
1067
 
1068
        }
1069
        return 0;
1070
    }
1071
 
1072
 
1073
 
1074
    /**
1075
     * Returns number of UnSeen messages in this mailbox
1076
     *
1077
     * @param string $mailbox the mailbox (default is current mailbox)
1078
     *
1079
     * @return mixed Either number of messages or Pear_Error on error
1080
     *
1081
     * @access public
1082
     */
1083
    function getStatus($mailbox = '')
1084
    {
1085
        if ($mailbox == '') {
1086
            $mailbox = $this->getCurrentMailbox();
1087
        }
1088
        if (PEAR::isError($ret = $this->cmdStatus($mailbox, array('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN')))) {
1089
            return $ret;
1090
        }
1091
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1092
            return new PEAR_Error($ret['RESPONSE']['CODE']
1093
                                  . ', '
1094
                                  . $ret['RESPONSE']['STR_CODE']);
1095
        }
1096
        if (isset($ret['PARSED']['STATUS']['ATTRIBUTES']['RECENT'])) {
1097
            return $ret['PARSED']['STATUS']['ATTRIBUTES'];
1098
        }
1099
        return 0;
1100
    }
1101
 
1102
 
1103
 
1104
    /**
1105
     * Returns an array containing the message envelope
1106
     *
1107
     * @param string  $mailbox  get's not used anywhere (will be removed
1108
     *                          with next major release)
1109
     * @param mixed   $msg_id   Message number (default = null)
1110
     * @param boolean $uidFetch msg_id contains UID's instead of Message
1111
     *                          Sequence Number if set to true
1112
     *
1113
     * @return mixed Either the envelopes or Pear_Error on error
1114
     *
1115
     * @access public
1116
     */
1117
    function getEnvelope($mailbox = '', $msg_id = null, $uidFetch = false)
1118
    {
1119
        if ($mailbox == '') {
1120
            $mailbox = $this->getCurrentMailbox();
1121
        }
1122
 
1123
        if ($msg_id != null) {
1124
            if (is_array($msg_id)) {
1125
                $message_set = $this->_getSearchListFromArray($msg_id);
1126
            } else {
1127
                $message_set = $msg_id;
1128
            }
1129
        } else {
1130
            $message_set = '1:*';
1131
        }
1132
 
1133
 
1134
        if ($uidFetch) {
1135
            $ret = $this->cmdUidFetch($message_set, 'ENVELOPE');
1136
        } else {
1137
            $ret = $this->cmdFetch($message_set, 'ENVELOPE');
1138
        }
1139
        if (PEAR::isError($ret)) {
1140
            return $ret;
1141
        }
1142
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1143
            return new PEAR_Error($ret['RESPONSE']['CODE']
1144
                                  . ', '
1145
                                  . $ret['RESPONSE']['STR_CODE']);
1146
        }
1147
 
1148
        if (isset($ret['PARSED'])) {
1149
            for ($i=0; $i<count($ret['PARSED']); $i++) {
1150
                $a            = $ret['PARSED'][$i]['EXT']['ENVELOPE'];
1151
                $a['MSG_NUM'] = $ret['PARSED'][$i]['NRO'];
1152
                $env[]        = $a;
1153
            }
1154
            return $env;
1155
        }
1156
 
1157
        return new PEAR_Error('Error, undefined number of messages');
1158
    }
1159
 
1160
 
1161
 
1162
    /**
1163
     * Returns the sum of all the sizes of messages in $mailbox
1164
     *           WARNING!!!  The method's performance is not good
1165
     *                       if you have a lot of messages in the mailbox
1166
     *                       Use with care!
1167
     *
1168
     * @param string $mailbox The mailbox (default is current mailbox)
1169
     *
1170
     * @return mixed Either size of maildrop or false on error
1171
     *
1172
     * @access public
1173
     */
1174
    function getMailboxSize($mailbox = '')
1175
    {
1176
 
1177
        if ($mailbox != '' && $mailbox != $this->getCurrentMailbox()) {
1178
            // store the actual selected mailbox name
1179
            $mailbox_aux = $this->getCurrentMailbox();
1180
            if (PEAR::isError($ret = $this->selectMailbox($mailbox))) {
1181
                return $ret;
1182
            }
1183
        }
1184
 
1185
        $ret = $this->cmdFetch('1:*', 'RFC822.SIZE');
1186
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1187
            // Restore the default mailbox if it was changed
1188
            if ($mailbox != '' && $mailbox != $this->getCurrentMailbox()) {
1189
                if (PEAR::isError($ret = $this->selectMailbox($mailbox_aux))) {
1190
                        return $ret;
1191
                }
1192
            }
1193
            // return 0 because the server says that there is no message
1194
            // in the mailbox
1195
            return 0;
1196
        }
1197
 
1198
        $sum = 0;
1199
 
1200
        if (!isset($ret['PARSED'])) {
1201
            // if the server does not return a "PARSED"  part
1202
            // we think that it does not suppoprt select or has no messages
1203
            // in it.
1204
            return 0;
1205
        }
1206
        foreach ($ret['PARSED'] as $msgSize) {
1207
            if (isset($msgSize['EXT']['RFC822.SIZE'])) {
1208
                $sum += $msgSize['EXT']['RFC822.SIZE'];
1209
            }
1210
        }
1211
 
1212
        if ($mailbox != '' && $mailbox != $this->getCurrentMailbox()) {
1213
            // re-select the mailbox
1214
            if (PEAR::isError($ret = $this->selectMailbox($mailbox_aux))) {
1215
                return $ret;
1216
            }
1217
        }
1218
 
1219
        return $sum;
1220
    }
1221
 
1222
 
1223
 
1224
    /**
1225
     * Marks a message for deletion. Only will be deleted if the
1226
     * disconnect() method is called with auto-expunge on true or expunge()
1227
     * method is called.
1228
     *
1229
     * @param int     $msg_id   Message to delete (default = null)
1230
     * @param boolean $uidStore msg_id contains UID's instead of Message
1231
     *                          Sequence Number if set to true (default = false)
1232
     *
1233
     * @return mixed true on success / Pear_Error on failure
1234
     *
1235
     * @access public
1236
     */
1237
    function deleteMessages($msg_id = null, $uidStore = false)
1238
    {
1239
        /* As said in RFC2060...
1240
        C: A003 STORE 2:4 +FLAGS (\Deleted)
1241
                S: * 2 FETCH FLAGS (\Deleted \Seen)
1242
                S: * 3 FETCH FLAGS (\Deleted)
1243
                S: * 4 FETCH FLAGS (\Deleted \Flagged \Seen)
1244
                S: A003 OK STORE completed
1245
        */
1246
        // Called without parammeters deletes all the messages in the mailbox
1247
        // You can also provide an array of numbers to delete those emails
1248
        if ($msg_id != null) {
1249
            if (is_array($msg_id)) {
1250
                $message_set = $this->_getSearchListFromArray($msg_id);
1251
            } else {
1252
                $message_set = $msg_id;
1253
            }
1254
        } else {
1255
            $message_set = '1:*';
1256
        }
1257
 
1258
 
1259
        $dataitem = '+FLAGS.SILENT';
1260
        $value    = '\Deleted';
1261
        if ($uidStore == true) {
1262
            $ret = $this->cmdUidStore($message_set, $dataitem, $value);
1263
        } else {
1264
            $ret = $this->cmdStore($message_set, $dataitem, $value);
1265
        }
1266
        if (PEAR::isError($ret)) {
1267
            return $ret;
1268
        }
1269
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1270
            return new PEAR_Error($ret['RESPONSE']['CODE']
1271
                                  . ', '
1272
                                  . $ret['RESPONSE']['STR_CODE']);
1273
        }
1274
        return true;
1275
    }
1276
 
1277
 
1278
 
1279
    /**
1280
     * Copies mail from one folder to another
1281
     *
1282
     * @param string  $dest_mailbox   mailbox name to copy sessages to
1283
     * @param mixed   $msg_id         the messages that I want to copy (all
1284
     *                                by default) it also can be an array
1285
     * @param string  $source_mailbox mailbox name from where the messages are
1286
     *                                copied (default is current mailbox)
1287
     * @param boolean $uidCopy        msg_id contains UID's instead of Message
1288
     *                                Sequence Number if set to true
1289
     *
1290
     * @return mixed true on Success/PearError on Failure
1291
     *
1292
     * @access  public
1293
     * @since   1.0
1294
     */
1295
    function copyMessages($dest_mailbox,
1296
                          $msg_id = null,
1297
                          $source_mailbox = null,
1298
                          $uidCopy = false)
1299
    {
1300
        if ($source_mailbox == null) {
1301
            $source_mailbox = $this->getCurrentMailbox();
1302
        } else {
1303
            if (PEAR::isError($ret = $this->selectMailbox($source_mailbox))) {
1304
                return $ret;
1305
            }
1306
        }
1307
        // Called without parammeters copies all messages in the mailbox
1308
        // You can also provide an array of numbers to copy those emails
1309
        if ($msg_id != null) {
1310
            if (is_array($msg_id)) {
1311
                $message_set = $this->_getSearchListFromArray($msg_id);
1312
            } else {
1313
                $message_set = $msg_id;
1314
            }
1315
        } else {
1316
            $message_set = '1:*';
1317
        }
1318
 
1319
        if ($uidCopy == true) {
1320
            $ret = $this->cmdUidCopy($message_set, $dest_mailbox);
1321
        } else {
1322
            $ret = $this->cmdCopy($message_set, $dest_mailbox);
1323
        }
1324
        if (PEAR::isError($ret)) {
1325
            return $ret;
1326
        }
1327
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1328
            return new PEAR_Error($ret['RESPONSE']['CODE']
1329
                                  . ', '
1330
                                  . $ret['RESPONSE']['STR_CODE']);
1331
        }
1332
 
1333
        return true;
1334
    }
1335
 
1336
 
1337
 
1338
    /**
1339
     * Appends a mail to  a mailbox
1340
     *
1341
     * @param string $rfc_message the message to append in RFC822 format
1342
     * @param string $mailbox     mailbox name to append to (default is
1343
     *                            current mailbox)
1344
     * @param string $flags_list  set flags appended message
1345
     *
1346
     * @return mixed true on success / Pear_Error on failure
1347
     *
1348
     * @access  public
1349
     * @since   1.0
1350
     */
1351
    function appendMessage($rfc_message, $mailbox = null, $flags_list = '')
1352
    {
1353
        if ($mailbox == null) {
1354
            $mailbox = $this->getCurrentMailbox();
1355
        }
1356
        $ret = $this->cmdAppend($mailbox, $rfc_message, $flags_list);
1357
        if (PEAR::isError($ret)) {
1358
            return $ret;
1359
        }
1360
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1361
            return new PEAR_Error($ret['RESPONSE']['CODE']
1362
                                  . ', '
1363
                                  . $ret['RESPONSE']['STR_CODE']);
1364
        }
1365
        return true;
1366
    }
1367
 
1368
 
1369
 
1370
    /**
1371
     * Returns the namespace
1372
     *
1373
     * @return mixed namespace or PearError on failure
1374
     *
1375
     * @access public
1376
     * @since 1.1
1377
     */
1378
    function getNamespace()
1379
    {
1380
        if (PEAR::isError($ret = $this->cmdNamespace())) {
1381
            return $ret;
1382
        }
1383
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1384
            return new PEAR_Error($ret['RESPONSE']['CODE']
1385
                                  . ', '
1386
                                  . $ret['RESPONSE']['STR_CODE']);
1387
        }
1388
 
1389
        foreach ($ret['PARSED']['NAMESPACES'] as $type => $singleNameSpace) {
1390
            if (!is_array($singleNameSpace)) {
1391
                continue;
1392
            }
1393
 
1394
            foreach ($singleNameSpace as $nameSpaceData) {
1395
                $nameSpaces[$type][] = array(
1396
                    'name'      => $this->utf7Decode($nameSpaceData[0]),
1397
                    'delimter'  => $this->utf7Decode($nameSpaceData[1])
1398
                );
1399
            }
1400
        }
1401
 
1402
        return $nameSpaces;
1403
    }
1404
 
1405
 
1406
 
1407
    /******************************************************************
1408
    **                                                               **
1409
    **           MAILBOX RELATED METHODS                             **
1410
    **                                                               **
1411
    ******************************************************************/
1412
 
1413
    /**
1414
     * Gets the HierachyDelimiter character used to create subfolders
1415
     * cyrus users "."
1416
     * and wu-imapd uses "/"
1417
     *
1418
     * @param string $mailbox The mailbox to get the hierarchy from
1419
     *
1420
     * @return string The hierarchy delimiter
1421
     *
1422
     * @access public
1423
     * @since 1.0
1424
     */
1425
    function getHierarchyDelimiter($mailbox = '')
1426
    {
1427
        /* RFC2060 says:
1428
            "the command LIST "" "" means get the hierachy delimiter:
1429
             An empty ("" string) mailbox name argument is a special request to
1430
             return the hierarchy delimiter and the root name of the name given
1431
             in the reference.  The value returned as the root MAY be null if
1432
             the reference is non-rooted or is null.  In all cases, the
1433
             hierarchy delimiter is returned.  This permits a client to get the
1434
             hierarchy delimiter even when no mailboxes by that name currently
1435
             exist."
1436
        */
1437
        if (PEAR::isError($ret = $this->cmdList($mailbox, ''))) {
1438
            return $ret;
1439
        }
1440
        if (isset($ret['PARSED'][0]['EXT']['LIST']['HIERACHY_DELIMITER'])) {
1441
            return $ret['PARSED'][0]['EXT']['LIST']['HIERACHY_DELIMITER'];
1442
        }
1443
        return new PEAR_Error('the IMAP Server does not support HIERACHY_DELIMITER!');
1444
    }
1445
 
1446
 
1447
 
1448
    /**
1449
     * Returns an array containing the names of the selected mailboxes
1450
     *
1451
     * @param string $reference          base mailbox to start the search
1452
     *                                   (default is current mailbox)
1453
     * @param string $restriction_search false or 0 means return all mailboxes
1454
     *                                   true or 1 return only the mailbox that
1455
     *                                   contains that exact name
1456
     *                                   2 return all mailboxes in that
1457
     *                                   hierarchy level
1458
     * @param string $returnAttributes   true means return an assoc array
1459
     *                                   containing mailbox names and mailbox
1460
     *                                   attributes,false - the default - means
1461
     *                                   return an array of mailboxes
1462
     *
1463
     * @return mixed true on success/PearError on failure
1464
     *
1465
     * @access public
1466
     * @since 1.0
1467
     */
1468
    function getMailboxes($reference = '',
1469
                          $restriction_search = 0,
1470
                          $returnAttributes = false)
1471
    {
1472
 
1473
        if (is_bool($restriction_search)) {
1474
            $restriction_search = (int) $restriction_search;
1475
        }
1476
 
1477
        if (is_int($restriction_search)) {
1478
            switch ($restriction_search) {
1479
            case 0:
1480
                $mailbox = '*';
1481
                break;
1482
            case 1:
1483
                $mailbox   = $reference;
1484
                $reference = '';
1485
                break;
1486
            case 2:
1487
                $mailbox = '%';
1488
                break;
1489
            }
1490
        } else {
1491
            if (is_string($restriction_search)) {
1492
                $mailbox = $restriction_search;
1493
            } else {
1494
                return new PEAR_Error('Wrong data for 2nd parameter');
1495
            }
1496
        }
1497
 
1498
        if (PEAR::isError($ret = $this->cmdList($reference, $mailbox))) {
1499
            return $ret;
1500
        }
1501
 
1502
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1503
            return new PEAR_Error($ret['RESPONSE']['CODE']
1504
                                  . ', '
1505
                                  . $ret['RESPONSE']['STR_CODE']);
1506
        }
1507
        $ret_aux = array();
1508
        if (isset($ret['PARSED'])) {
1509
            foreach ($ret['PARSED'] as $mbox) {
1510
 
1511
                // If the folder has the \NoSelect atribute we don't put in
1512
                // the list
1513
                // it solves a bug in wu-imap that crash the IMAP server if
1514
                // we select that mailbox
1515
                if (isset($mbox['EXT']['LIST']['NAME_ATTRIBUTES'])) {
1516
                    // if (!in_array('\NoSelect', $mbox['EXT']['LIST']['NAME_ATTRIBUTES'])) {
1517
                    if ($returnAttributes) {
1518
                        $ret_aux[] = array(
1519
                            'MAILBOX'            => $mbox['EXT']['LIST']['MAILBOX_NAME'],
1520
                            'ATTRIBUTES'         => $mbox['EXT']['LIST']['NAME_ATTRIBUTES'],
1521
                            'HIERACHY_DELIMITER' => $mbox['EXT']['LIST']['HIERACHY_DELIMITER']);
1522
                    } else {
1523
                        $ret_aux[] = $mbox['EXT']['LIST']['MAILBOX_NAME'];
1524
                    }
1525
                    // }
1526
                }
1527
            }
1528
        }
1529
        return $ret_aux;
1530
    }
1531
 
1532
 
1533
 
1534
    /**
1535
     * Check if the mailbox name exists
1536
     *
1537
     * @param string $mailbox Mailbox name to check existance
1538
     *
1539
     * @return mixed boolean true/false or PEAR_Error on failure
1540
     *
1541
     * @access public
1542
     * @since 1.0
1543
     */
1544
    function mailboxExist($mailbox)
1545
    {
1546
        // true means do an exact match
1547
        if (PEAR::isError($ret = $this->getMailboxes($mailbox, true))) {
1548
            return $ret;
1549
        }
1550
        if (count($ret) > 0) {
1551
            foreach ($ret as $mailbox_name) {
1552
                if ($mailbox_name == $mailbox) {
1553
                    return true;
1554
                }
1555
            }
1556
        }
1557
        return false;
1558
    }
1559
 
1560
 
1561
 
1562
    /**
1563
     * Creates the mailbox $mailbox
1564
     *
1565
     * @param string $mailbox Mailbox name to create
1566
     * @param array  $options Options to pass to create (default is no options)
1567
     *
1568
     * @return mixed true on success/PearError on failure
1569
     *
1570
     * @access public
1571
     * @since 1.0
1572
     */
1573
    function createMailbox($mailbox, $options = null)
1574
    {
1575
        if (PEAR::isError($ret = $this->cmdCreate($mailbox, $options))) {
1576
            return $ret;
1577
        }
1578
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1579
            return new PEAR_Error($ret['RESPONSE']['CODE']
1580
                                  . ', '
1581
                                  . $ret['RESPONSE']['STR_CODE']);
1582
        }
1583
        return true;
1584
    }
1585
 
1586
 
1587
 
1588
    /**
1589
     * Deletes the mailbox $mailbox
1590
     *
1591
     * @param string $mailbox Name of the Mailbox that should be deleted
1592
     *
1593
     * @return mixed true on success/PearError on failure
1594
     *
1595
     * @access public
1596
     * @since 1.0
1597
     */
1598
    function deleteMailbox($mailbox)
1599
    {
1600
        // TODO verificar que el mailbox se encuentra vacio y, sino borrar los
1601
        // mensajes antes~!!!!!!
1602
        // ToDo find someone who can translate the above todo
1603
 
1604
        if (PEAR::isError($ret = $this->cmdDelete($mailbox))) {
1605
            return $ret;
1606
        }
1607
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1608
            return new PEAR_Error($ret['RESPONSE']['CODE']
1609
                                  . ', '
1610
                                  . $ret['RESPONSE']['STR_CODE']);
1611
        }
1612
        return true;
1613
    }
1614
 
1615
 
1616
 
1617
    /**
1618
     * Renames the mailbox $mailbox
1619
     *
1620
     * @param string $oldmailbox mailbox name to rename
1621
     * @param string $newmailbox new name for the mailbox
1622
     * @param array  $options    options to pass to rename
1623
     *
1624
     * @return mixed true on success/PearError on failure
1625
     *
1626
     * @access public
1627
     * @since 1.0
1628
     */
1629
    function renameMailbox($oldmailbox, $newmailbox, $options = null)
1630
    {
1631
        if (PEAR::isError($ret = $this->cmdRename($oldmailbox,
1632
                                                  $newmailbox,
1633
                                                  $options))) {
1634
            return $ret;
1635
        }
1636
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1637
            return new PEAR_Error($ret['RESPONSE']['CODE']
1638
                                  . ', '
1639
                                  . $ret['RESPONSE']['STR_CODE']);
1640
        }
1641
        return true;
1642
    }
1643
 
1644
 
1645
 
1646
 
1647
    /******************************************************************
1648
    **                                                               **
1649
    **           SUBSCRIPTION METHODS                                **
1650
    **                                                               **
1651
    ******************************************************************/
1652
 
1653
    /**
1654
     * Subscribes to the selected mailbox
1655
     *
1656
     * @param string $mailbox Mailbox name to subscribe (default is current
1657
     *                        mailbox)
1658
     *
1659
     * @return mixed true on success/PearError on failure
1660
     *
1661
     * @access public
1662
     * @since 1.0
1663
     */
1664
    function subscribeMailbox($mailbox = null)
1665
    {
1666
        if ($mailbox == null) {
1667
            $mailbox = $this->getCurrentMailbox();
1668
        }
1669
        if (PEAR::isError($ret = $this->cmdSubscribe($mailbox))) {
1670
            return $ret;
1671
        }
1672
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1673
            return new PEAR_Error($ret['RESPONSE']['CODE']
1674
                                  . ', '
1675
                                  . $ret['RESPONSE']['STR_CODE']);
1676
        }
1677
        return true;
1678
    }
1679
 
1680
 
1681
 
1682
    /**
1683
     * Removes the subscription to a mailbox
1684
     *
1685
     * @param string $mailbox Mailbox name to unsubscribe (default is current
1686
     *                        mailbox)
1687
     *
1688
     * @return mixed true on success/PearError on failure
1689
     *
1690
     * @access public
1691
     * @since 1.0
1692
     */
1693
    function unsubscribeMailbox($mailbox = null)
1694
    {
1695
        if ($mailbox == null) {
1696
            $mailbox = $this->getCurrentMailbox();
1697
        }
1698
        if (PEAR::isError($ret = $this->cmdUnsubscribe($mailbox))) {
1699
            return $ret;
1700
        }
1701
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1702
            return new PEAR_Error($ret['RESPONSE']['CODE']
1703
                                  . ', '
1704
                                  . $ret['RESPONSE']['STR_CODE']);
1705
        }
1706
        return true;
1707
    }
1708
 
1709
 
1710
 
1711
    /**
1712
     * Lists the subscription to mailboxes
1713
     *
1714
     * @param string  $reference          Mailbox name start the search (see to
1715
     *                                    getMailboxes() )
1716
     * @param string  $restriction_search Mailbox_name Mailbox name filter the
1717
     *                                    search (see to getMailboxes() )
1718
     * @param boolean $returnAttributes   Return the attributes
1719
     *
1720
     * @return mixed true on success/PearError on failure
1721
     *
1722
     * @access public
1723
     * @since 1.0
1724
     */
1725
    function listsubscribedMailboxes($reference = '',
1726
                                     $restriction_search = 0,
1727
                                     $returnAttributes = false)
1728
    {
1729
        if (is_bool($restriction_search)) {
1730
            $restriction_search = (int) $restriction_search;
1731
        }
1732
 
1733
        if (is_int($restriction_search)) {
1734
            switch ($restriction_search) {
1735
            case 0:
1736
                $mailbox = '*';
1737
                break;
1738
            case 1:
1739
                $mailbox   = $reference;
1740
                $reference = '%';
1741
                break;
1742
            case 2:
1743
                $mailbox = '%';
1744
                break;
1745
            }
1746
        } else {
1747
            if (is_string($restriction_search)) {
1748
                $mailbox = $restriction_search;
1749
            } else {
1750
                return new PEAR_Error('UPS... you ');
1751
            }
1752
        }
1753
 
1754
        if (PEAR::isError($ret = $this->cmdLsub($reference, $mailbox))) {
1755
            return $ret;
1756
        }
1757
        // $ret=$this->cmdLsub($mailbox_base, $mailbox_name);
1758
 
1759
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1760
            return new PEAR_Error($ret['RESPONSE']['CODE']
1761
                                  . ', '
1762
                                  . $ret['RESPONSE']['STR_CODE']);
1763
        }
1764
 
1765
        $ret_aux = array();
1766
        if (isset($ret['PARSED'])) {
1767
            foreach ($ret['PARSED'] as $mbox) {
1768
                if (isset($mbox['EXT']['LSUB']['MAILBOX_NAME'])) {
1769
                    if ($returnAttributes) {
1770
                        $ret_aux[] = array(
1771
                            'MAILBOX'            => $mbox['EXT']['LSUB']['MAILBOX_NAME'],
1772
                            'ATTRIBUTES'         => $mbox['EXT']['LSUB']['NAME_ATTRIBUTES'],
1773
                            'HIERACHY_DELIMITER' => $mbox['EXT']['LSUB']['HIERACHY_DELIMITER']
1774
                            );
1775
                    } else {
1776
                        $ret_aux[] = $mbox['EXT']['LSUB']['MAILBOX_NAME'];
1777
                    }
1778
                }
1779
            }
1780
        }
1781
        return $ret_aux;
1782
    }
1783
 
1784
 
1785
 
1786
 
1787
    /******************************************************************
1788
    **                                                               **
1789
    **           FLAGS METHODS                                       **
1790
    **                                                               **
1791
    ******************************************************************/
1792
 
1793
    /**
1794
     * Lists the flags of the selected messages
1795
     *
1796
     * @param mixed $msg_id the message list
1797
     *
1798
     * @return mixed array on success/PearError on failure
1799
     *
1800
     * @access public
1801
     * @since 1.0
1802
     */
1803
    function getFlags($msg_id = null)
1804
    {
1805
        // You can also provide an array of numbers to those emails
1806
        if ($msg_id != null) {
1807
            if (is_array($msg_id)) {
1808
                $message_set = $this->_getSearchListFromArray($msg_id);
1809
            } else {
1810
                $message_set = $msg_id;
1811
            }
1812
        } else {
1813
            $message_set = '1:*';
1814
        }
1815
 
1816
 
1817
        if (PEAR::isError($ret = $this->cmdFetch($message_set, 'FLAGS'))) {
1818
            return $ret;
1819
        }
1820
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1821
            return new PEAR_Error($ret['RESPONSE']['CODE']
1822
                                  . ', '
1823
                                  . $ret['RESPONSE']['STR_CODE']);
1824
        }
1825
        $flags = array();
1826
        if (isset($ret['PARSED'])) {
1827
            foreach ($ret['PARSED'] as $msg_flags) {
1828
                if (isset($msg_flags['EXT']['FLAGS'])) {
1829
                    $flags[] = $msg_flags['EXT']['FLAGS'];
1830
                }
1831
            }
1832
        }
1833
        return $flags;
1834
    }
1835
 
1836
 
1837
 
1838
    /**
1839
     * Sets the flags of the selected messages
1840
     *
1841
     * @param mixed   $msg_id   the message list or string "all" for all
1842
     * @param mixed   $flags    flags to set (space separated String or array)
1843
     * @param string  $mod      "set" to set flags (default),
1844
     *                          "add" to add flags,
1845
     *                          "remove" to remove flags
1846
     * @param boolean $uidStore msg_id contains UID's instead of Message
1847
     *                          Sequence Number if set to true
1848
     *
1849
     * @return mixed true on success/PearError on failure
1850
     *
1851
     * @access public
1852
     * @since 1.1
1853
     */
1854
    function setFlags($msg_id, $flags, $mod = 'set', $uidStore = false)
1855
    {
1856
        // you can also provide an array of numbers to those emails
1857
        if ($msg_id == 'all') {
1858
            $message_set = '1:*';
1859
        } else {
1860
            if (is_array($msg_id)) {
1861
                $message_set = $this->_getSearchListFromArray($msg_id);
1862
            } else {
1863
                $message_set = $msg_id;
1864
            }
1865
        }
1866
 
1867
        $flaglist = '';
1868
        if (is_array($flags)) {
1869
            $flaglist = implode(' ', $flags);
1870
        } else {
1871
            $flaglist = $flags;
1872
        }
1873
 
1874
        switch ($mod) {
1875
        case 'set':
1876
            $dataitem = 'FLAGS';
1877
            break;
1878
        case 'add':
1879
            $dataitem = '+FLAGS';
1880
            break;
1881
        case 'remove':
1882
            $dataitem = '-FLAGS';
1883
            break;
1884
        default:
1885
            // Wrong Input
1886
            return new PEAR_Error('wrong input for $mod');
1887
            break;
1888
        }
1889
 
1890
        if ($uidStore == true) {
1891
            $ret = $this->cmdUidStore($message_set, $dataitem, $flaglist);
1892
        } else {
1893
            $ret = $this->cmdStore($message_set, $dataitem, $flaglist);
1894
        }
1895
        if (PEAR::isError($ret)) {
1896
            return $ret;
1897
        }
1898
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
1899
            return new PEAR_Error($ret['RESPONSE']['CODE']
1900
                                  . ', '
1901
                                  . $ret['RESPONSE']['STR_CODE']);
1902
        }
1903
 
1904
        return true;
1905
    }
1906
 
1907
 
1908
 
1909
    /**
1910
     * adds flags to the selected messages
1911
     *
1912
     * @param mixed $msg_id The message list or string "all" for all
1913
     * @param mixed $flags  Flags to set (space separated String or array)
1914
     *
1915
     * @return mixed true on success/PearError on failure
1916
     *
1917
     * @access public
1918
     * @since 1.1
1919
     */
1920
    function addFlags($msg_id, $flags)
1921
    {
1922
        return $this->setFlags($msg_id, $flags, $mod = 'add');
1923
    }
1924
 
1925
 
1926
 
1927
    /**
1928
     * adds the Seen flag (\Seen) to the selected messages
1929
     *
1930
     * @param mixed $msg_id The message list or string "all" for all
1931
     *
1932
     * @return mixed true on success/PearError on failure
1933
     *
1934
     * @access public
1935
     * @since 1.1
1936
     */
1937
    function addSeen($msg_id)
1938
    {
1939
        return $this->setFlags($msg_id, '\Seen', $mod = 'add');
1940
    }
1941
 
1942
 
1943
 
1944
    /**
1945
     * adds the Answered flag (\Answered) to the selected messages
1946
     *
1947
     * @param mixed $msg_id The message list or string "all" for all
1948
     *
1949
     * @return mixed true on success/PearError on failure
1950
     *
1951
     * @access public
1952
     * @since 1.1
1953
     */
1954
    function addAnswered($msg_id)
1955
    {
1956
        return $this->setFlags($msg_id, '\Answered', $mod = 'add');
1957
    }
1958
 
1959
 
1960
 
1961
    /**
1962
     * adds the Deleted flag (\Deleted) to the selected messages
1963
     *
1964
     * @param mixed $msg_id The message list or string "all" for all
1965
     *
1966
     * @return mixed true on success/PearError on failure
1967
     *
1968
     * @access public
1969
     * @since 1.1
1970
     */
1971
    function addDeleted($msg_id)
1972
    {
1973
        return $this->setFlags($msg_id, '\Deleted', $mod = 'add');
1974
    }
1975
 
1976
 
1977
 
1978
    /**
1979
     * adds the Flagged flag (\Flagged) to the selected messages
1980
     *
1981
     * @param mixed $msg_id The message list or string "all" for all
1982
     *
1983
     * @return mixed true on success/PearError on failure
1984
     *
1985
     * @access public
1986
     * @since 1.1
1987
     */
1988
    function addFlagged($msg_id)
1989
    {
1990
        return $this->setFlags($msg_id, '\Flagged', $mod = 'add');
1991
    }
1992
 
1993
 
1994
 
1995
    /**
1996
     * adds the Draft flag (\Draft) to the selected messages
1997
     *
1998
     * @param mixed $msg_id The message list or string "all" for all
1999
     *
2000
     * @return mixed true on success/PearError on failure
2001
     *
2002
     * @access public
2003
     * @since 1.1
2004
     */
2005
    function addDraft($msg_id)
2006
    {
2007
        return $this->setFlags($msg_id, '\Draft', $mod = 'add');
2008
    }
2009
 
2010
 
2011
 
2012
    /**
2013
     * remove flags from the selected messages
2014
     *
2015
     * @param mixed $msg_id The message list or string "all" for all
2016
     * @param mixed $flags  Flags to remove (space separated string or array)
2017
     *
2018
     * @return mixed true on success/PearError on failure
2019
     *
2020
     * @access public
2021
     * @since 1.1
2022
     */
2023
    function removeFlags($msg_id, $flags)
2024
    {
2025
        return $this->setFlags($msg_id, $flags, $mod = 'remove');
2026
    }
2027
 
2028
 
2029
 
2030
    /**
2031
     * remove the Seen flag (\Seen) from the selected messages
2032
     *
2033
     * @param mixed $msg_id The message list or string "all" for all
2034
     *
2035
     * @return mixed true on success/PearError on failure
2036
     *
2037
     * @access public
2038
     * @since 1.1
2039
     */
2040
    function removeSeen($msg_id)
2041
    {
2042
        return $this->setFlags($msg_id, '\Seen', $mod = 'remove');
2043
    }
2044
 
2045
 
2046
 
2047
    /**
2048
     * remove the Answered flag (\Answered) from the selected messages
2049
     *
2050
     * @param mixed $msg_id The message list or string "all" for all
2051
     *
2052
     * @return mixed true on success/PearError on failure
2053
     *
2054
     * @access public
2055
     * @since 1.1
2056
     */
2057
    function removeAnswered($msg_id)
2058
    {
2059
        return $this->setFlags($msg_id, '\Answered', $mod = 'remove');
2060
    }
2061
 
2062
 
2063
 
2064
    /**
2065
     * remove the Deleted flag (\Deleted) from the selected messages
2066
     *
2067
     * @param mixed $msg_id The message list or string "all" for all
2068
     *
2069
     * @return mixed true on success/PearError on failure
2070
     *
2071
     * @access public
2072
     * @since 1.1
2073
     */
2074
    function removeDeleted($msg_id)
2075
    {
2076
        return $this->setFlags($msg_id, '\Deleted', $mod = 'remove');
2077
    }
2078
 
2079
 
2080
 
2081
    /**
2082
     * remove the Flagged flag (\Flagged) from the selected messages
2083
     *
2084
     * @param mixed $msg_id The message list or string "all" for all
2085
     *
2086
     * @return mixed true on success/PearError on failure
2087
     *
2088
     * @access public
2089
     * @since 1.1
2090
     */
2091
    function removeFlagged($msg_id)
2092
    {
2093
        return $this->setFlags($msg_id, '\Flagged', $mod = 'remove');
2094
    }
2095
 
2096
 
2097
 
2098
    /**
2099
     * remove the Draft flag (\Draft) from the selected messages
2100
     *
2101
     * @param mixed $msg_id The message list or string "all" for all
2102
     *
2103
     * @return mixed true on success/PearError on failure
2104
     *
2105
     * @access public
2106
     * @since 1.1
2107
     */
2108
    function removeDraft($msg_id)
2109
    {
2110
        return $this->setFlags($msg_id, '\Draft', $mod = 'remove');
2111
    }
2112
 
2113
 
2114
 
2115
    /**
2116
     * check the Seen flag
2117
     *
2118
     * @param mixed $message_nro The message to check
2119
     *
2120
     * @return mixed true or false if the flag is set PearError on Failure
2121
     *
2122
     * @access public
2123
     * @since 1.0
2124
     */
2125
    function isSeen($message_nro)
2126
    {
2127
        return $this->hasFlag($message_nro, '\Seen');
2128
    }
2129
 
2130
 
2131
 
2132
    /**
2133
     * check the Answered flag
2134
     *
2135
     * @param mixed $message_nro The message to check
2136
     *
2137
     * @return mixed true or false if the flag is set PearError on failure
2138
     *
2139
     * @access public
2140
     * @since 1.0
2141
     */
2142
    function isAnswered($message_nro)
2143
    {
2144
        return $this->hasFlag($message_nro, '\Answered');
2145
    }
2146
 
2147
 
2148
 
2149
    /**
2150
     * check the flagged flag
2151
     *
2152
     * @param mixed $message_nro The message to check
2153
     *
2154
     * @return mixed true or false if the flag is set PearError on failure
2155
     *
2156
     * @access public
2157
     * @since 1.0
2158
     */
2159
    function isFlagged($message_nro)
2160
    {
2161
        return $this->hasFlag($message_nro, '\Flagged');
2162
    }
2163
 
2164
 
2165
 
2166
    /**
2167
     * check the Draft flag
2168
     *
2169
     * @param mixed $message_nro The message to check
2170
     *
2171
     * @return mixed true or false if the flag is set PearError on failure
2172
     *
2173
     * @access public
2174
     * @since 1.0
2175
     */
2176
    function isDraft($message_nro)
2177
    {
2178
        return $this->hasFlag($message_nro, '\Draft');
2179
    }
2180
 
2181
 
2182
 
2183
    /**
2184
     * check the Deleted flag
2185
     *
2186
     * @param mixed $message_nro The message to check
2187
     *
2188
     * @return mixed true or false if the flag is set PearError on failure
2189
     *
2190
     * @access public
2191
     * @since 1.0
2192
     */
2193
    function isDeleted($message_nro)
2194
    {
2195
        return $this->hasFlag($message_nro, '\Deleted');
2196
    }
2197
 
2198
 
2199
 
2200
    /**
2201
     * checks if a flag is set
2202
     *
2203
     * @param mixed  $message_nro The message to check
2204
     * @param string $flag        The flag that should be checked
2205
     *
2206
     * @return mixed true or false if the flag is set PearError on Failure
2207
     *
2208
     * @access public
2209
     * @since 1.0
2210
     */
2211
    function hasFlag($message_nro, $flag)
2212
    {
2213
        if (PEAR::isError($resp = $this->getFlags($message_nro))) {
2214
            return $resp;
2215
        }
2216
        if (isset($resp[0])) {
2217
            if (is_array($resp[0])) {
2218
                if (in_array($flag, $resp[0])) {
2219
                    return true;
2220
                }
2221
            }
2222
        }
2223
        return false;
2224
    }
2225
 
2226
 
2227
 
2228
 
2229
    /******************************************************************
2230
    **                                                               **
2231
    **           MISC METHODS                                        **
2232
    **                                                               **
2233
    ******************************************************************/
2234
 
2235
 
2236
    /**
2237
     * expunge function. Sends the EXPUNGE command
2238
     *
2239
     * @return mixed true on success / PEAR Error on failure
2240
     *
2241
     * @access public
2242
     * @since 1.0
2243
     */
2244
    function expunge()
2245
    {
2246
        if (PEAR::isError($ret = $this->cmdExpunge())) {
2247
            return $ret;
2248
        }
2249
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2250
            return new PEAR_Error($ret['RESPONSE']['CODE']
2251
                                  . ', '
2252
                                  . $ret['RESPONSE']['STR_CODE']);
2253
        }
2254
        return true;
2255
    }
2256
 
2257
 
2258
 
2259
    /**
2260
     * Search function. Sends the SEARCH command
2261
     *
2262
     * @param string  $search_list Search criterias
2263
     * @param boolean $uidSearch   If set to true UID SEARCH is send instead of SEARCH
2264
     *
2265
     * @return mixed Message array or PEAR Error on failure
2266
     *
2267
     * @access public
2268
     * @since 1.0
2269
     */
2270
    function search($search_list, $uidSearch = false)
2271
    {
2272
        if ($uidSearch) {
2273
            $ret = $this->cmdUidSearch($search_list);
2274
        } else {
2275
            $ret = $this->cmdSearch($search_list);
2276
        }
2277
        if (PEAR::isError($ret)) {
2278
            return $ret;
2279
        }
2280
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2281
            return new PEAR_Error($ret['RESPONSE']['CODE']
2282
                                  . ', '
2283
                                  . $ret['RESPONSE']['STR_CODE']);
2284
        }
2285
        return $ret['PARSED']['SEARCH']['SEARCH_LIST'];
2286
    }
2287
 
2288
 
2289
 
2290
    /**
2291
     * sort function. Sends the SORT command
2292
     *
2293
     * @param string  $sort_list   sort program
2294
     * @param string  $charset     charset specification (default = 'US-ASCII')
2295
     * @param string  $search_list searching criteria
2296
     * @param boolean $uidSort     if set to true UID SORT is send instead
2297
     *                             of SORT
2298
     *
2299
     * @return mixed message array or PEAR Error on failure
2300
     *
2301
     * @access public
2302
     * @since 1.1
2303
     */
2304
    function sort($sort_list,
2305
                  $charset = 'US-ASCII',
2306
                  $search_list = '',
2307
                  $uidSort = false)
2308
    {
2309
        $sort_command = sprintf("(%s) %s %s",
2310
                                $sort_list,
2311
                                strtoupper($charset),
2312
                                $search_list);
2313
 
2314
        if ($uidSort) {
2315
            $ret = $this->cmdUidSort($sort_command);
2316
        } else {
2317
            $ret = $this->cmdSort($sort_command);
2318
        }
2319
        if (PEAR::isError($ret)) {
2320
            return $ret;
2321
        }
2322
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2323
            return new PEAR_Error($ret['RESPONSE']['CODE']
2324
                                  . ', '
2325
                                  . $ret['RESPONSE']['STR_CODE']);
2326
        }
2327
        return $ret['PARSED']['SORT']['SORT_LIST'];
2328
    }
2329
 
2330
 
2331
 
2332
 
2333
    /******************************************************************
2334
    **                                                               **
2335
    **           QUOTA METHODS                                       **
2336
    **                                                               **
2337
    ******************************************************************/
2338
 
2339
 
2340
    /**
2341
     * Returns STORAGE quota details
2342
     *
2343
     * @param string $mailbox_name Mailbox to get quota info. (default is
2344
     *                             current mailbox)
2345
     *
2346
     * @return assoc array contaning the quota info on success or
2347
     *                      PEAR_Error on failure
2348
     *
2349
     * @access public
2350
     * @since 1.0
2351
     */
2352
    function getStorageQuotaRoot($mailbox_name = null)
2353
    {
2354
        if ($mailbox_name == null) {
2355
            $mailbox_name = $this->getCurrentMailbox();
2356
        }
2357
 
2358
        if (PEAR::isError($ret = $this->cmdGetQuotaRoot($mailbox_name))) {
2359
            return new PEAR_Error($ret->getMessage());
2360
        }
2361
 
2362
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2363
            // if the error is that the user does not have quota set
2364
            // return  an array and not pear error
2365
            if (substr(strtoupper($ret['RESPONSE']['STR_CODE']),
2366
                       0,
2367
                       9) == 'QUOTAROOT') {
2368
                return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
2369
            }
2370
            return new PEAR_Error($ret['RESPONSE']['CODE']
2371
                                  . ', '
2372
                                  . $ret['RESPONSE']['STR_CODE']);
2373
        }
2374
 
2375
        if (isset($ret['PARSED']['EXT']['QUOTA']['STORAGE'])) {
2376
            return $ret['PARSED']['EXT']['QUOTA']['STORAGE'];
2377
        }
2378
        return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
2379
    }
2380
 
2381
 
2382
 
2383
    /**
2384
     * Returns STORAGE quota details
2385
     *
2386
     * @param string $mailbox_name Mailbox to get quota info. (default is
2387
     *                             current mailbox)
2388
     *
2389
     * @return assoc array contaning the quota info on success or PEAR_Error
2390
     *                     on failure
2391
     *
2392
     * @access public
2393
     * @since 1.0
2394
     */
2395
    function getStorageQuota($mailbox_name = null)
2396
    {
2397
        if ($mailbox_name == null) {
2398
            $mailbox_name = $this->getCurrentMailbox();
2399
        }
2400
 
2401
        if (PEAR::isError($ret = $this->cmdGetQuota($mailbox_name))) {
2402
            return new PEAR_Error($ret->getMessage());
2403
        }
2404
 
2405
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2406
            // if the error is that the user does not have quota set
2407
            // return  an array and not pear error
2408
            if (substr(strtoupper($ret['RESPONSE']['STR_CODE']),
2409
                       0,
2410
                       5) == 'QUOTA') {
2411
                return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
2412
            }
2413
            return new PEAR_Error($ret['RESPONSE']['CODE']
2414
                                  . ', '
2415
                                  . $ret['RESPONSE']['STR_CODE']);
2416
        }
2417
 
2418
        if (isset($ret['PARSED']['EXT']['QUOTA']['STORAGE'])) {
2419
            return $ret['PARSED']['EXT']['QUOTA']['STORAGE'];
2420
        }
2421
        return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
2422
    }
2423
 
2424
 
2425
 
2426
    /**
2427
     * Returns MESSAGES quota details
2428
     *
2429
     * @param string $mailbox_name Mailbox to get quota info. (default is
2430
     *                             current mailbox)
2431
     *
2432
     * @return assoc array contaning the quota info on success or PEAR_Error
2433
     *                     on failure
2434
     *
2435
     * @access public
2436
     * @since 1.0
2437
     */
2438
    function getMessagesQuota($mailbox_name = null)
2439
    {
2440
        if ($mailbox_name == null) {
2441
            $mailbox_name = $this->getCurrentMailbox();
2442
        }
2443
 
2444
        if (PEAR::isError($ret = $this->cmdGetQuota($mailbox_name))) {
2445
            return new PEAR_Error($ret->getMessage());
2446
        }
2447
 
2448
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2449
            // if the error is that the user does not have quota set return
2450
            // an array and not pear error
2451
            if (substr(strtoupper($ret['RESPONSE']['STR_CODE']),
2452
                       0,
2453
                       5) == 'QUOTA') {
2454
                return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
2455
            }
2456
            return new PEAR_Error($ret['RESPONSE']['CODE']
2457
                                  . ', '
2458
                                  . $ret['RESPONSE']['STR_CODE']);
2459
        }
2460
 
2461
        if (isset($ret['PARSED']['EXT']['QUOTA']['MESSAGES'])) {
2462
            return $ret['PARSED']['EXT']['QUOTA']['MESSAGES'];
2463
        }
2464
        return array('USED'=>'NOT SET', 'QMAX'=>'NOT SET');
2465
    }
2466
 
2467
 
2468
 
2469
    /**
2470
     * sets STORAGE quota
2471
     *
2472
     * @param string $mailbox_name Mailbox to set quota
2473
     * @param int    $quota        Quotasize
2474
     *
2475
     * @return true on success or PEAR_Error on failure
2476
     *
2477
     * @access public
2478
     * @since 1.0
2479
     */
2480
    function setStorageQuota($mailbox_name, $quota)
2481
    {
2482
        if (PEAR::isError($ret = $this->cmdSetQuota($mailbox_name, $quota))) {
2483
            return new PEAR_Error($ret->getMessage());
2484
        }
2485
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2486
            return new PEAR_Error($ret['RESPONSE']['CODE']
2487
                                  . ', '
2488
                                  . $ret['RESPONSE']['STR_CODE']);
2489
        }
2490
        return true;
2491
    }
2492
 
2493
 
2494
 
2495
    /**
2496
     * sets MESSAGES quota
2497
     *
2498
     * @param string $mailbox_name Mailbox to set quota
2499
     * @param int    $quota        Quotasize
2500
     *
2501
     * @return true on success or PEAR_Error on failure
2502
     *
2503
     * @access public
2504
     * @since 1.0
2505
     */
2506
    function setMessagesQuota($mailbox_name, $quota)
2507
    {
2508
        if (PEAR::isError($ret = $this->cmdSetQuota($mailbox_name,
2509
                                                    '',
2510
                                                    $quota))) {
2511
            return new PEAR_Error($ret->getMessage());
2512
        }
2513
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2514
            return new PEAR_Error($ret['RESPONSE']['CODE']
2515
                                  . ', '
2516
                                  . $ret['RESPONSE']['STR_CODE']);
2517
        }
2518
        return true;
2519
    }
2520
 
2521
 
2522
 
2523
 
2524
    /******************************************************************
2525
    **                                                               **
2526
    **           ACL METHODS                                         **
2527
    **                                                               **
2528
    ******************************************************************/
2529
 
2530
 
2531
    /**
2532
     * get the Access Control List details
2533
     *
2534
     * @param string $mailbox_name Mailbox to get ACL info. (default is
2535
     *                             current mailbox)
2536
     *
2537
     * @return mixed string on success or false or PEAR_Error on failure
2538
     *
2539
     * @access public
2540
     * @since 1.0
2541
     */
2542
    function getACL($mailbox_name = null)
2543
    {
2544
        if ($mailbox_name == null) {
2545
            $mailbox_name = $this->getCurrentMailbox();
2546
        }
2547
        if (PEAR::isError($ret = $this->cmdGetACL($mailbox_name))) {
2548
            return new PEAR_Error($ret->getMessage());
2549
        }
2550
 
2551
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2552
            return new PEAR_Error($ret['RESPONSE']['CODE']
2553
                                  . ', '
2554
                                  . $ret['RESPONSE']['STR_CODE']);
2555
        }
2556
 
2557
        if (isset($ret['PARSED']['USERS'])) {
2558
            return $ret['PARSED']['USERS'];
2559
        } else {
2560
            return false;
2561
        }
2562
    }
2563
 
2564
 
2565
 
2566
    /**
2567
     * Set ACL on a mailbox
2568
     *
2569
     * @param string $mailbox_name The mailbox
2570
     * @param string $user         User to set the ACL
2571
     * @param string $acl          ACL list
2572
     *
2573
     * @return mixed true on success or PEAR_Error on failure
2574
     *
2575
     * @access public
2576
     * @since 1.0
2577
     */
2578
    function setACL($mailbox_name, $user, $acl)
2579
    {
2580
        if (PEAR::isError($ret = $this->cmdSetACL($mailbox_name, $user, $acl))) {
2581
            return new PEAR_Error($ret->getMessage());
2582
        }
2583
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2584
            return new PEAR_Error($ret['RESPONSE']['CODE']
2585
                                  . ', '
2586
                                  . $ret['RESPONSE']['STR_CODE']);
2587
        }
2588
        return true;
2589
    }
2590
 
2591
 
2592
 
2593
    /**
2594
     * deletes the ACL on a mailbox
2595
     *
2596
     * @param string $mailbox_name The mailbox
2597
     * @param string $user         User to delete the ACL
2598
     *
2599
     * @return mixed true on success, or PEAR_Error on failure
2600
     *
2601
     * @access public
2602
     * @since 1.0
2603
     */
2604
    function deleteACL($mailbox_name, $user)
2605
    {
2606
        if (PEAR::isError($ret = $this->cmdDeleteACL($mailbox_name, $user))) {
2607
            return new PEAR_Error($ret->getMessage());
2608
        }
2609
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2610
            return new PEAR_Error($ret['RESPONSE']['CODE']
2611
                                  . ', '
2612
                                  . $ret['RESPONSE']['STR_CODE']);
2613
        }
2614
        return true;
2615
    }
2616
 
2617
 
2618
 
2619
    /**
2620
     * returns the rights that the user logged on has on the mailbox
2621
     * this method can be used by any user, not only the administrator
2622
     *
2623
     * @param string $mailbox_name The mailbox to query rights (default is
2624
     *                             current mailbox)
2625
     *
2626
     * @return mixed string containing the list of rights on success, or
2627
     *                      PEAR_Error on failure
2628
     *
2629
     * @access public
2630
     * @since 1.0
2631
     */
2632
    function getMyRights($mailbox_name = null)
2633
    {
2634
        if ($mailbox_name == null) {
2635
            $mailbox_name = $this->getCurrentMailbox();
2636
        }
2637
 
2638
        if (PEAR::isError($ret = $this->cmdMyRights($mailbox_name))) {
2639
            return new PEAR_Error($ret->getMessage());
2640
        }
2641
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2642
            return new PEAR_Error($ret['RESPONSE']['CODE']
2643
                                  . ', '
2644
                                  . $ret['RESPONSE']['STR_CODE']);
2645
        }
2646
 
2647
        if (isset($ret['PARSED']['GRANTED'])) {
2648
            return $ret['PARSED']['GRANTED'];
2649
        }
2650
 
2651
        return new PEAR_Error('Bogus response from server!');
2652
    }
2653
 
2654
 
2655
 
2656
    /**
2657
     * returns an array containing the rights for given user on the mailbox
2658
     * this method can be used by any user, not only the administrator
2659
     *
2660
     * @param string $user         The user to query rights
2661
     * @param string $mailbox_name The mailbox to query rights (default is
2662
     *                             current mailbox)
2663
     *
2664
     * @return mixed string containing the list of rights on success, or
2665
     *               PEAR_Error on failure
2666
     *
2667
     * @access public
2668
     * @since 1.0
2669
     */
2670
    function getACLRights($user,$mailbox_name = null)
2671
    {
2672
 
2673
        if ($mailbox_name == null) {
2674
            $mailbox_name = $this->getCurrentMailbox();
2675
        }
2676
 
2677
 
2678
        if (PEAR::isError($ret = $this->cmdListRights($mailbox_name, $user))) {
2679
            return new PEAR_Error($ret->getMessage());
2680
        }
2681
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2682
            return new PEAR_Error($ret['RESPONSE']['CODE']
2683
                                  . ', '
2684
                                  . $ret['RESPONSE']['STR_CODE']);
2685
        }
2686
 
2687
        if (isset($ret['PARSED']['GRANTED'])) {
2688
            return $ret['PARSED']['GRANTED'];
2689
        }
2690
 
2691
        return new PEAR_Error('Bogus response from server!');
2692
    }
2693
 
2694
 
2695
 
2696
 
2697
    /******************************************************************
2698
    **                                                               **
2699
    **           ANNOTATEMORE METHODS                                **
2700
    **                                                               **
2701
    ******************************************************************/
2702
 
2703
 
2704
    /**
2705
     * set annotation
2706
     *
2707
     * @param string $entry        Entry
2708
     * @param array  $values       Values
2709
     * @param string $mailbox_name Mailbox name (default is current mailbox)
2710
     *
2711
     * @return mixed true on success or PEAR Error on failure
2712
     *
2713
     * @access public
2714
     * @since 1.0.2
2715
     */
2716
    function setAnnotation($entry, $values, $mailbox_name = null)
2717
    {
2718
        if ($mailbox_name == null) {
2719
            $mailbox_name = $this->getCurrentMailbox();
2720
        }
2721
 
2722
        if (PEAR::isError($ret = $this->cmdSetAnnotation($mailbox_name,
2723
                                                         $entry,
2724
                                                         $values))) {
2725
            return new PEAR_Error($ret->getMessage());
2726
        }
2727
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2728
            return new PEAR_Error($ret['RESPONSE']['CODE']
2729
                                  . ', '
2730
                                  . $ret['RESPONSE']['STR_CODE']);
2731
        }
2732
        return true;
2733
    }
2734
 
2735
 
2736
    /**
2737
     * delete annotation
2738
     *
2739
     * @param string $entry        Entry
2740
     * @param array  $values       Values
2741
     * @param string $mailbox_name Mailbox name (default is current mailbox)
2742
     *
2743
     * @return mixed true on success or PEAR Error on failure
2744
     *
2745
     * @access public
2746
     * @since 1.0.2
2747
     */
2748
    function deleteAnnotation($entry, $values, $mailbox_name = null)
2749
    {
2750
        if ($mailbox_name == null) {
2751
            $mailbox_name = $this->getCurrentMailbox();
2752
        }
2753
 
2754
        if (PEAR::isError($ret = $this->cmdDeleteAnnotation($mailbox_name,
2755
                                                            $entry,
2756
                                                            $values))) {
2757
            return new PEAR_Error($ret->getMessage());
2758
        }
2759
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2760
            return new PEAR_Error($ret['RESPONSE']['CODE']
2761
                                  . ', '
2762
                                  . $ret['RESPONSE']['STR_CODE']);
2763
        }
2764
        return true;
2765
    }
2766
 
2767
 
2768
    /**
2769
     * get annotation
2770
     *
2771
     * @param string $entries      Entries
2772
     * @param array  $values       Values
2773
     * @param string $mailbox_name Mailbox name (default is current mailbox)
2774
     *
2775
     * @return mixed array containing annotations on success or PEAR Error
2776
     *               on failure
2777
     *
2778
     * @access public
2779
     * @since 1.0.2
2780
     */
2781
    function getAnnotation($entries, $values, $mailbox_name = null)
2782
    {
2783
        if ($mailbox_name == null) {
2784
            $mailbox_name = $this->getCurrentMailbox();
2785
        }
2786
        if (!is_array($entries)) {
2787
            $entries = array($entries);
2788
        }
2789
        if (!is_array($values)) {
2790
            $values = array($values);
2791
        }
2792
 
2793
        if (PEAR::isError($ret = $this->cmdGetAnnotation($mailbox_name,
2794
                                                         $entries,
2795
                                                         $values))) {
2796
            return new PEAR_Error($ret->getMessage());
2797
        }
2798
        if (strtoupper($ret['RESPONSE']['CODE']) != 'OK') {
2799
            return new PEAR_Error($ret['RESPONSE']['CODE']
2800
                                  . ', '
2801
                                  . $ret['RESPONSE']['STR_CODE']);
2802
        }
2803
        $ret_aux = array();
2804
        if (isset($ret['PARSED'])) {
2805
            foreach ($ret['PARSED'] as $mbox) {
2806
                $rawvalues = $mbox['EXT']['ATTRIBUTES'];
2807
                $values    = array();
2808
                for ($i = 0; $i < count($rawvalues); $i += 2) {
2809
                    $values[$rawvalues[$i]] = $rawvalues[$i + 1];
2810
                }
2811
                $mbox['EXT']['ATTRIBUTES'] = $values;
2812
                $ret_aux[]                 = $mbox['EXT'];
2813
            }
2814
        }
2815
        if (count($ret_aux) == 1 && $ret_aux[0]['MAILBOX'] == $mailbox_name) {
2816
            if (count($entries) == 1 && $ret_aux[0]['ENTRY'] == $entries[0]) {
2817
                if (count($ret_aux[0]['ATTRIBUTES']) == 1
2818
                    && count($values) == 1) {
2819
                    $attrs = array_keys($ret_aux[0]['ATTRIBUTES']);
2820
                    $vals  = array_keys($values);
2821
                    if ($attrs[0] == $vals[0]) {
2822
                        return $ret_aux[0]['ATTRIBUTES'][$attrs[0]];
2823
                    }
2824
                }
2825
            }
2826
        }
2827
        return $ret_aux;
2828
    }
2829
 
2830
 
2831
 
2832
    /**
2833
     * Transform an array to a list to be used in the cmdFetch method
2834
     *
2835
     * @param array $arr Array that should be transformed
2836
     *
2837
     * @return string transformed array
2838
     *
2839
     * @access private
2840
     */
2841
    function _getSearchListFromArray($arr)
2842
    {
2843
        $txt = implode(',', $arr);
2844
        return $txt;
2845
    }
2846
 
2847
 
2848
 
2849
 
2850
    /*****************************************************
2851
        Net_POP3 Compatibility functions:
2852
 
2853
        Warning!!!
2854
            Those functions could dissapear in the future
2855
 
2856
    *********************************************************/
2857
 
2858
 
2859
    /**
2860
     * same as getMailboxSize()
2861
     * Net_POP3 Compatibility function
2862
     *
2863
     * @return same as getMailboxSize();
2864
     *
2865
     * @access public
2866
     */
2867
    function getSize()
2868
    {
2869
        return $this->getMailboxSize();
2870
    }
2871
 
2872
    /**
2873
     * same as getNumberOfMessages($mailbox)
2874
     * Net_POP3 Compatibility function
2875
     *
2876
     * @param string $mailbox Mailbox (default is current mailbox)
2877
     *
2878
     * @return same as getNumberOfMessages($mailbox)
2879
     *
2880
     * @access public
2881
     */
2882
    function numMsg($mailbox = null)
2883
    {
2884
        return $this->getNumberOfMessages($mailbox);
2885
    }
2886
 
2887
 
2888
    /**
2889
     * Returns the entire message with given message number.
2890
     * Net_POP3 Compatibility function
2891
     *
2892
     * @param int $msg_id Message number
2893
     *
2894
     * @return mixed Either entire message or PEAR Error on failure
2895
     *
2896
     * @access public
2897
     */
2898
    function getMsg($msg_id)
2899
    {
2900
        if (PEAR::isError($ret = $this->getMessages($msg_id, false))) {
2901
            return $ret;
2902
        }
2903
        // false means that getMessages() must not use the msg number as array key
2904
        if (isset($ret[0])) {
2905
            return $ret[0];
2906
        } else {
2907
            return $ret;
2908
        }
2909
    }
2910
 
2911
 
2912
 
2913
    /**
2914
     * same as getMessagesList($msg_id)
2915
     * Net_POP3 Compatibility function
2916
     *
2917
     * @param int $msg_id Message number
2918
     *
2919
     * @return same as getMessagesList()
2920
     *
2921
     * @access public
2922
     */
2923
    function getListing($msg_id = null)
2924
    {
2925
        return $this->getMessagesList($msg_id);
2926
    }
2927
 
2928
 
2929
 
2930
    /**
2931
     * same as deleteMessages($msg_id)
2932
     * Net_POP3 Compatibility function
2933
     *
2934
     * @param int $msg_id Message number
2935
     *
2936
     * @return same as deleteMessages()
2937
     *
2938
     * @access public
2939
     */
2940
    function deleteMsg($msg_id)
2941
    {
2942
        return $this->deleteMessages($msg_id);
2943
    }
2944
 
2945
}
2946
?>