~ubuntu-branches/ubuntu/hardy/squirrelmail/hardy-updates

« back to all changes in this revision

Viewing changes to functions/imap_mailbox.php

  • Committer: Bazaar Package Importer
  • Author(s): Thijs Kinkhorst
  • Date: 2005-02-06 21:41:51 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050206214151-z4n1o8mnttgzuj0y
Tags: 2:1.4.4-3
* Move default_pref config file from /var to /etc, as per Debian policy
  (Closes: #293281)
* [JvW] (finally) override two lintian warnings about nonstandard
  permissions that are intentional (Closes: #293366)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
/**
4
4
 * imap_mailbox.php
5
5
 *
6
 
 * Copyright (c) 1999-2003 The SquirrelMail Project Team
 
6
 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7
7
 * Licensed under the GNU GPL. For full terms see the file COPYING.
8
8
 *
9
9
 * This impliments all functions that manipulate mailboxes
10
10
 *
11
 
 * $Id: imap_mailbox.php,v 1.212 2004/01/28 05:17:13 ebullient Exp $
 
11
 * @version $Id: imap_mailbox.php,v 1.172.2.15 2004/12/27 15:03:44 kink Exp $
12
12
 * @package squirrelmail
 
13
 * @subpackage imap
13
14
 */
14
15
 
15
16
/** UTF7 support */
17
18
 
18
19
global $boxesnew;
19
20
 
20
 
/**
21
 
 * Mailboxes class
22
 
 * 
23
 
 * FIXME. This class should be extracted and placed in a separate file that 
24
 
 * can be included before we start the session. That makes caching of the tree
25
 
 * possible. On a refresh mailboxes from left_main.php the only function that 
26
 
 * should be called is the sqimap_get_status_mbx_tree. In case of subscribe 
27
 
 * / rename / delete / new we have to create methods for adding/changing the 
28
 
 * mailbox in the mbx_tree without the need for a refresh.
29
 
 * @package squirrelmail
30
 
*/
31
 
 
32
21
class mailboxes {
33
 
    var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
 
22
    var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
34
23
        $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
35
 
        $is_trash = false, $is_draft = false,  $mbxs = array(), 
 
24
        $is_trash = false, $is_draft = false,  $mbxs = array(),
36
25
        $unseen = false, $total = false;
37
26
 
38
27
    function addMbx($mbx, $delimiter, $start, $specialfirst) {
39
28
        $ary = explode($delimiter, $mbx->mailboxname_full);
40
 
        $mbx_parent =& $this;
 
29
        $mbx_parent = &$this;
41
30
        for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
42
 
            $mbx_childs =& $mbx_parent->mbxs;
 
31
            $mbx_childs = &$mbx_parent->mbxs;
43
32
            $found = false;
44
33
            if ($mbx_childs) {
45
34
                foreach ($mbx_childs as $key => $parent) {
46
35
                    if ($parent->mailboxname_sub == $ary[$i]) {
47
 
                        $mbx_parent =& $mbx_parent->mbxs[$key];
 
36
                        $mbx_parent = &$mbx_parent->mbxs[$key];
48
37
                        $found = true;
49
 
                        break;
50
38
                    }
51
39
                }
52
40
            }
85
73
    } else {
86
74
        $bcmp = '2' . $b->mailboxname_full;
87
75
    }
88
 
    return strnatcasecmp($acmp, $bcmp); 
89
 
}
90
 
 
91
 
function compact_mailboxes_response($ary)
92
 
{
93
 
    /*
94
 
     * Workaround for mailboxes returned as literal
95
 
     * FIXME : Doesn't work if the mailbox name is multiple lines 
96
 
     * (larger then fgets buffer)
97
 
     */
98
 
    for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
99
 
        if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
100
 
            if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
101
 
                 $ary[$i], $regs)) {
102
 
                $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
103
 
                array_splice($ary, $i+1, 2);
104
 
            }
105
 
        }
106
 
    }
107
 
    /* remove duplicates and ensure array is contiguous */
108
 
    return array_values(array_unique($ary));
109
 
}
110
 
 
111
 
/**
112
 
 * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
113
 
 * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
114
 
 * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
115
 
 */
116
 
function find_mailbox_name($line)
117
 
{
118
 
    if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
119
 
        if (substr($regs[1], 0, 1) == '"')
120
 
            return stripslashes(substr($regs[1], 1, -1));
121
 
        return $regs[1];
122
 
    }
123
 
    return '';
 
76
    if ($acmp == $bcmp) return 0;
 
77
    return ($acmp > $bcmp) ? 1: -1;
 
78
}
 
79
 
 
80
function find_mailbox_name ($mailbox) {
 
81
    if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
 
82
        return $regs[1];
 
83
    if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
 
84
        return $regs[1];
 
85
    ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
 
86
    return $regs[1];
124
87
}
125
88
 
126
89
/**
127
90
 * @return bool whether this is a Noselect mailbox.
128
91
 */
129
92
function check_is_noselect ($lsub_line) {
130
 
    return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
131
 
}
132
 
 
133
 
/**
134
 
 * @return bool whether this is a Noinferiors mailbox.
135
 
 */
136
 
function check_is_noinferiors ($lsub_line) {
137
 
    return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
 
93
    return preg_match("/^\* LSUB \([^\)]*\\Noselect[^\)]*\)/i", $lsub_line);
138
94
}
139
95
 
140
96
/**
156
112
    return( $ret );
157
113
}
158
114
 
159
 
/** 
 
115
/**
160
116
 * Check if $subbox is below the specified $parentbox
161
117
 */
162
118
function isBoxBelow( $subbox, $parentbox ) {
163
119
    global $delimiter;
164
 
    /* 
165
 
     * Eliminate the obvious mismatch, where the 
 
120
    /*
 
121
     * Eliminate the obvious mismatch, where the
166
122
     * subfolder path is shorter than that of the potential parent
167
123
     */
168
124
    if ( strlen($subbox) < strlen($parentbox) ) {
184
140
 * "special" one: INBOX, Trash, Sent or Draft.
185
141
 */
186
142
function isSpecialMailbox( $box ) {
 
143
    global $trash_folder, $sent_folder, $draft_folder,
 
144
           $move_to_trash, $move_to_sent, $save_as_draft;
 
145
 
187
146
    $ret = ( (strtolower($box) == 'inbox') ||
188
147
             isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
189
148
 
249
208
}
250
209
 
251
210
/**
252
 
 * Expunge specified message, updated $msgs and $msort
253
 
 * 
254
 
 * Until Marc and I come up with a better way to maintain 
255
 
 * these stupid arrays, we'll use this wrapper function to 
256
 
 * remove the message with the matching UID .. the order
257
 
 * won't be changed - the array element for the message
258
 
 * will just be removed.
259
 
 */
260
 
function sqimap_mailbox_expunge_dmn($message_id)
261
 
{
262
 
    global $msgs, $msort, $sort, $imapConnection, 
263
 
           $mailbox, $uid_support, $mbx_response, $auto_expunge, 
264
 
           $sort, $allow_server_sort, $thread_sort_messages, $allow_thread_sort,
265
 
           $username, $data_dir;
266
 
 
267
 
    // Got to grab this out of prefs, since it isn't saved from mailbox_view.php
268
 
    if ($allow_thread_sort) {
269
 
        $thread_sort_messages = getPref($data_dir, $username, "thread_$mailbox",0); 
270
 
    }
271
 
 
272
 
    for ($i = 0; $i < count($msort); $i++) {
273
 
        if ($msgs[$i]['ID'] == $message_id) {
274
 
            break;   
275
 
        }
276
 
    }
277
 
 
278
 
    unset($msgs[$i]);
279
 
    unset($msort[$i]);
280
 
 
281
 
    $msgs = array_values($msgs);
282
 
    $msort = array_values($msort);
283
 
 
284
 
    sqsession_register($msgs, 'msgs');
285
 
    sqsession_register($msort, 'msort');
286
 
 
287
 
    if ($auto_expunge) {
288
 
         sqimap_mailbox_expunge($imapConnection, $mailbox, true);
289
 
    }
290
 
 
291
 
    // And after all that mucking around, update the sort list!
292
 
    // Remind me why the hell we need those two arrays again?!
293
 
    if ( $allow_thread_sort && $thread_sort_messages ) {
294
 
        $server_sort_array = get_thread_sort($imapConnection);
295
 
    } elseif ( $allow_server_sort ) {
296
 
        $server_sort_array = sqimap_get_sort_order($imapConnection, $sort, $mbx_response);
297
 
    } elseif ( $uid_support ) {
298
 
        $server_sort_array = sqimap_get_php_sort_order($imapConnection, $mbx_response);
299
 
    }
300
 
 
301
 
}
302
 
 
303
 
/**
304
211
 * Checks whether or not the specified mailbox exists
305
212
 */
306
213
function sqimap_mailbox_exists ($imap_stream, $mailbox) {
307
214
    if (!isset($mailbox) || empty($mailbox)) {
308
215
        return false;
309
216
    }
310
 
    $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
 
217
    $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
311
218
                              true, $response, $message);
312
219
    return isset($mbx[0]);
313
220
}
322
229
        return;
323
230
    }
324
231
 
325
 
    $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
 
232
    $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
326
233
                               true, $response, $message);
327
234
    $result = array();
328
235
    for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
359
266
        $mailbox .= $delimiter;
360
267
    }
361
268
 
362
 
    $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
363
 
                                   sqimap_encode_mailbox_name($mailbox),
 
269
    $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
364
270
                                   true, $response, $message);
365
271
    sqimap_subscribe ($imap_stream, $mailbox);
366
272
}
369
275
 * Subscribes to an existing folder.
370
276
 */
371
277
function sqimap_subscribe ($imap_stream, $mailbox) {
372
 
    $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
373
 
                                   sqimap_encode_mailbox_name($mailbox),
 
278
    $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
374
279
                                   true, $response, $message);
375
280
}
376
281
 
378
283
 * Unsubscribes from an existing folder
379
284
 */
380
285
function sqimap_unsubscribe ($imap_stream, $mailbox) {
381
 
    $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
382
 
                                   sqimap_encode_mailbox_name($mailbox),
 
286
    $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
383
287
                                   false, $response, $message);
384
288
}
385
289
 
389
293
function sqimap_mailbox_delete ($imap_stream, $mailbox) {
390
294
    global $data_dir, $username;
391
295
    sqimap_unsubscribe ($imap_stream, $mailbox);
392
 
    $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
393
 
                                   sqimap_encode_mailbox_name($mailbox),
 
296
    $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
394
297
                                   true, $response, $message);
395
298
    if ($response !== 'OK') {
396
299
        // subscribe again
429
332
        }
430
333
 
431
334
        $boxesall = sqimap_mailbox_list($imap_stream);
432
 
        $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
433
 
                     ' ' . sqimap_encode_mailbox_name($new_name);
 
335
        $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
434
336
        $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
435
337
        sqimap_unsubscribe($imap_stream, $old_name.$postfix);
436
338
        $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
485
387
        }
486
388
 
487
389
        /* Count number of delimiters ($delimiter) in folder name */
488
 
        $mailbox  = /*trim(*/$line_lsub[$g]/*)*/;
 
390
        $mailbox  = $line_lsub[$g];
489
391
        $dm_count = substr_count($mailbox, $delimiter);
490
392
        if (substr($mailbox, -1) == $delimiter) {
491
393
            /* If name ends in delimiter, decrement count by one */
524
426
        $boxesall[$g]['flags'] = array();
525
427
        if (isset($line[$g])) {
526
428
            ereg("\(([^)]*)\)",$line[$g],$regs);
527
 
            // FIXME Flags do contain the \ character. \NoSelect \NoInferiors 
528
 
            // and $MDNSent <= last one doesn't have the \
529
 
            // It's better to follow RFC3501 instead of using our own naming.
530
429
            $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
531
430
            if ($flags) {
532
431
                $boxesall[$g]['flags'] = explode(' ', $flags);
537
436
}
538
437
 
539
438
/**
 
439
 * Sorting function used to sort mailbox names.
 
440
 *     + Original patch from dave_michmerhuizen@yahoo.com
 
441
 *     + Allows case insensitivity when sorting folders
 
442
 *     + Takes care of the delimiter being sorted to the end, causing
 
443
 *       subfolders to be listed in below folders that are prefixed
 
444
 *       with their parent folders name.
 
445
 *
 
446
 *       For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
 
447
 *       Without special sort function: foobar between foo and foo.bar
 
448
 *       With special sort function: foobar AFTER foo and foo.bar :)
 
449
 */
 
450
function user_strcasecmp($a, $b) {
 
451
    return  strnatcasecmp($a, $b);
 
452
}
 
453
 
 
454
/**
540
455
 * Returns list of options (to be echoed into select statement
541
456
 * based on available mailboxes and separators
542
 
 * Caller should surround options with <SELECT..> </SELECT> and
 
457
 * Caller should surround options with <select ...> </select> and
543
458
 * any formatting.
544
459
 *   $imap_stream - $imapConnection to query for mailboxes
545
460
 *   $show_selected - array containing list of mailboxes to pre-select (0 if none)
550
465
 *           'noselect' by default to remove unselectable mailboxes.
551
466
 *           'noinferiors' used to filter out folders that can not contain subfolders.
552
467
 *           NULL to avoid flag check entirely.
553
 
 *           NOTE: noselect and noiferiors are used internally. The IMAP representation is
554
 
 *                 \NoSelect and \NoInferiors
555
468
 *   $use_long_format - override folder display preference and always show full folder name.
556
469
 */
557
 
function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0, 
 
470
function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
558
471
                                    $flag = 'noselect', $use_long_format = false ) {
559
472
    global $username, $data_dir;
560
473
    $mbox_options = '';
575
488
            if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
576
489
                continue;
577
490
            }
578
 
            $lowerbox = strtolower($box); 
 
491
            $lowerbox = strtolower($box);
579
492
            // mailboxes are casesensitive => inbox.sent != inbox.Sent
580
493
            // nevermind, to many dependencies this should be fixed!
581
 
         
 
494
 
582
495
            if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
583
496
                $box2 = _("INBOX");
584
 
            } else { 
 
497
            } else {
585
498
                switch ($shorten_box_names)
586
499
                {
587
500
                  case 2:   /* delimited, style = 2 */
595
508
                    break;
596
509
                }
597
510
            }
 
511
            $box2 = str_replace(array('<','>'), array('&lt;','&gt;') , $box2);
 
512
 
598
513
            if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
599
 
                $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'" SELECTED>'.$box2.'</OPTION>' . "\n";
 
514
                $mbox_options .= '<option value="' . htmlspecialchars($box) .'" selected="selected">'.$box2.'</option>' . "\n";
600
515
            } else {
601
 
                $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'">'.$box2.'</OPTION>' . "\n";
 
516
                $mbox_options .= '<option value="' . htmlspecialchars($box) .'">'.$box2.'</option>' . "\n";
602
517
            }
603
518
        }
604
519
    }
606
521
}
607
522
 
608
523
/**
609
 
 * Returns sorted mailbox lists in several different ways. 
 
524
 * Mailboxes with some chars (like -) can mess up the order, this fixes it
 
525
 */
 
526
function mailtree_sort(&$lsub) {
 
527
    if(!is_array($lsub)) return;
 
528
 
 
529
    foreach($lsub as $index => $mailbox)
 
530
        $lsub[$index] = str_replace('.',' -#- ',$lsub[$index]);
 
531
 
 
532
    usort($lsub, 'user_strcasecmp');
 
533
 
 
534
    foreach($lsub as $index => $mailbox)
 
535
        $lsub[$index] = str_replace(' -#- ','.',$lsub[$index]);
 
536
}
 
537
 
 
538
/**
 
539
 * Returns sorted mailbox lists in several different ways.
610
540
 * See comment on sqimap_mailbox_parse() for info about the returned array.
611
541
 */
612
 
function sqimap_mailbox_list($imap_stream) {
 
542
 
 
543
 
 
544
function sqimap_mailbox_list($imap_stream, $force=false) {
613
545
    global $default_folder_prefix;
614
546
 
615
 
    if (!isset($boxesnew)) {
 
547
    if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
616
548
        global $data_dir, $username, $list_special_folders_first,
617
549
               $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
618
550
               $move_to_trash, $move_to_sent, $save_as_draft,
619
551
               $delimiter, $noselect_fix_enable;
620
 
 
621
552
        $inbox_in_list = false;
622
553
        $inbox_subscribed = false;
623
554
 
631
562
        /* LSUB array */
632
563
        $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
633
564
                                        true, $response, $message);
634
 
        $lsub_ary = compact_mailboxes_response($lsub_ary);
635
565
 
636
566
        $sorted_lsub_ary = array();
637
567
        for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
 
568
            /*
 
569
             * Workaround for mailboxes returned as literal
 
570
             * Doesn't work if the mailbox name is multiple lines
 
571
             * (larger then fgets buffer)
 
572
             */
 
573
            if (isset($lsub_ary[$i + 1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
 
574
                if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
 
575
                     $lsub_ary[$i], $regs)) {
 
576
                        $i++;
 
577
                        $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
 
578
                }
 
579
            }
638
580
            $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
639
581
            $sorted_lsub_ary[] = $temp_mailbox_name;
640
582
            if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
641
583
                $inbox_subscribed = true;
642
584
            }
643
585
        }
 
586
        /* remove duplicates */
 
587
        $sorted_lsub_ary = array_unique($sorted_lsub_ary);
644
588
 
645
589
        /* natural sort mailboxes */
646
590
        if (isset($sorted_lsub_ary)) {
647
 
            usort($sorted_lsub_ary, 'strnatcasecmp');
 
591
            mailtree_sort($sorted_lsub_ary);
648
592
        }
649
593
        /*
650
594
         * The LSUB response doesn't provide us information about \Noselect
652
596
         * call to retrieve the flags for the mailbox
653
597
           * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
654
598
           * in other words, we cannot rely on it.
655
 
        */
 
599
         */
656
600
        $sorted_list_ary = array();
657
601
        for ($i=0; $i < count($sorted_lsub_ary); $i++) {
658
602
            if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
661
605
            else {
662
606
                $mbx = $sorted_lsub_ary[$i];
663
607
            }
664
 
            $mbx = stripslashes($mbx);
665
 
            $read = sqimap_run_command ($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mbx),
 
608
 
 
609
            $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
666
610
                                        true, $response, $message);
667
 
            $read = compact_mailboxes_response($read);
 
611
 
 
612
            /* Another workaround for literals */
 
613
 
 
614
            if (isset($read[1]) && substr($read[1],-3) == "}\r\n") {
 
615
                if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
 
616
                     $read[0], $regs)) {
 
617
                    $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
 
618
                }
 
619
            }
 
620
 
668
621
            if (isset($read[0])) {
669
622
                $sorted_list_ary[$i] = $read[0];
670
623
            } else {
671
624
                $sorted_list_ary[$i] = '';
672
625
            }
673
626
        }
 
627
 
674
628
        /*
675
629
         * Just in case they're not subscribed to their inbox,
676
630
         * we'll get it for them anyway
677
631
         */
678
632
        if (!$inbox_subscribed) {
679
 
            $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" INBOX',
 
633
            $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
680
634
                                             true, $response, $message);
681
 
            $sorted_list_ary[] = implode('', compact_mailboxes_response($inbox_ary));
 
635
            /* Another workaround for literals */
 
636
            if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
 
637
                if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
 
638
                     $inbox_ary[0], $regs)) {
 
639
                    $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
 
640
                                '"' . $regs[2];
 
641
                }
 
642
            }
 
643
            $sorted_list_ary[] = $inbox_ary[0];
682
644
            $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
683
645
        }
684
646
 
713
675
                $boxesnew[] = $boxesall[$k];
714
676
            }
715
677
        }
 
678
        sqsession_register($boxesnew,'boxesnew');
716
679
    }
717
 
    
718
680
    return $boxesnew;
719
681
}
720
682
 
724
686
function sqimap_mailbox_list_all($imap_stream) {
725
687
    global $list_special_folders_first, $folder_prefix, $delimiter;
726
688
 
727
 
    $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
728
 
    $read_ary = compact_mailboxes_response($read_ary);
729
 
 
 
689
    $ssid = sqimap_session_id();
 
690
    $lsid = strlen( $ssid );
 
691
    fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
 
692
    $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
730
693
    $g = 0;
731
694
    $phase = 'inbox';
732
695
    $fld_pre_length = strlen($folder_prefix);
 
696
 
733
697
    for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
734
 
        /* Store the raw IMAP reply */
735
 
        $boxes[$g]['raw'] = $read_ary[$i];
736
 
 
737
 
        /* Count number of delimiters ($delimiter) in folder name */
738
 
        $mailbox = find_mailbox_name($read_ary[$i]);
739
 
        $dm_count =  substr_count($mailbox, $delimiter);
740
 
        if (substr($mailbox, -1) == $delimiter) {
741
 
            /* If name ends in delimiter - decrement count by one */
742
 
            $dm_count--;
743
 
        }
744
 
 
745
 
        /* Format folder name, but only if it's a INBOX.* or has a parent. */
746
 
        $boxesallbyname[$mailbox] = $g;
747
 
        $parentfolder = readMailboxParent($mailbox, $delimiter);
748
 
        if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
749
 
           (ereg('^'.$folder_prefix, $mailbox)) ||
750
 
           ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
751
 
            if ($dm_count) {
752
 
                $boxes[$g]['formatted']  = str_repeat('&nbsp;&nbsp;', $dm_count);
753
 
            } else {
754
 
                $boxes[$g]['formatted'] = '';
755
 
            }
756
 
            $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
757
 
        } else {
758
 
            $boxes[$g]['formatted']  = imap_utf7_decode_local($mailbox);
759
 
        }
760
 
 
761
 
        $boxes[$g]['unformatted-dm'] = $mailbox;
762
 
        if (substr($mailbox, -1) == $delimiter) {
763
 
            $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
764
 
        }
765
 
        $boxes[$g]['unformatted'] = $mailbox;
766
 
        $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
767
 
 
768
 
        $boxes[$g]['id'] = $g;
769
 
 
770
 
        /* Now lets get the flags for this mailbox */
771
 
        $read_mlbx = $read_ary[$i];
772
 
        $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
773
 
        $flags = substr($flags, 0, strpos($flags, ')'));
774
 
        $flags = str_replace('\\', '', $flags);
775
 
        $flags = trim(strtolower($flags));
776
 
        if ($flags) {
777
 
            $boxes[$g]['flags'] = explode(' ', $flags);
778
 
        } else {
779
 
            $boxes[$g]['flags'] = array();
 
698
        /* Another workaround for EIMS */
 
699
        if (isset($read_ary[$i + 1]) &&
 
700
            ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
 
701
                 $read_ary[$i], $regs)) {
 
702
            $i ++;
 
703
            $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
 
704
        }
 
705
        if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
 
706
            /* Store the raw IMAP reply */
 
707
            $boxes[$g]['raw'] = $read_ary[$i];
 
708
 
 
709
            /* Count number of delimiters ($delimiter) in folder name */
 
710
            $mailbox = find_mailbox_name($read_ary[$i]);
 
711
            $dm_count =  substr_count($mailbox, $delimiter);
 
712
            if (substr($mailbox, -1) == $delimiter) {
 
713
                /* If name ends in delimiter - decrement count by one */
 
714
                $dm_count--;
 
715
            }
 
716
 
 
717
            /* Format folder name, but only if it's a INBOX.* or has a parent. */
 
718
            $boxesallbyname[$mailbox] = $g;
 
719
            $parentfolder = readMailboxParent($mailbox, $delimiter);
 
720
            if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
 
721
               (ereg('^'.$folder_prefix, $mailbox)) ||
 
722
               ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
 
723
                if ($dm_count) {
 
724
                    $boxes[$g]['formatted']  = str_repeat('&nbsp;&nbsp;', $dm_count);
 
725
                } else {
 
726
                    $boxes[$g]['formatted'] = '';
 
727
                }
 
728
                $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
 
729
            } else {
 
730
                $boxes[$g]['formatted']  = imap_utf7_decode_local($mailbox);
 
731
            }
 
732
 
 
733
            $boxes[$g]['unformatted-dm'] = $mailbox;
 
734
            if (substr($mailbox, -1) == $delimiter) {
 
735
                $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
 
736
            }
 
737
            $boxes[$g]['unformatted'] = $mailbox;
 
738
            $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
 
739
 
 
740
            $boxes[$g]['id'] = $g;
 
741
 
 
742
            /* Now lets get the flags for this mailbox */
 
743
            $read_mlbx = $read_ary[$i];
 
744
 
 
745
//            $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
 
746
//                                             true, $response, $message);
 
747
 
 
748
            /* Another workaround for EIMS */
 
749
//            if (isset($read_mlbx[1]) &&
 
750
//                ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
 
751
//                $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
 
752
//            }
 
753
//            echo  $read_mlbx[0] .' raw 2 <br>';
 
754
 
 
755
            $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
 
756
            $flags = substr($flags, 0, strpos($flags, ')'));
 
757
            $flags = str_replace('\\', '', $flags);
 
758
            $flags = trim(strtolower($flags));
 
759
            if ($flags) {
 
760
                $boxes[$g]['flags'] = explode(' ', $flags);
 
761
            } else {
 
762
                $boxes[$g]['flags'] = array();
 
763
            }
780
764
        }
781
765
        $g++;
782
766
    }
792
776
    if (!isset($boxesnew)) {
793
777
 
794
778
        global $data_dir, $username, $list_special_folders_first,
795
 
               $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
796
 
               $imap_server_type;
 
779
               $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
797
780
 
798
781
 
799
782
        $inbox_in_list = false;
800
783
        $inbox_subscribed = false;
801
 
        $noselect = false;
802
 
        $noinferiors = false;
803
784
 
804
785
        require_once(SM_PATH . 'include/load_prefs.php');
805
786
 
806
787
        /* LSUB array */
807
788
        $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
808
789
                                        true, $response, $message);
809
 
        $lsub_ary = compact_mailboxes_response($lsub_ary);
810
 
 
811
 
        /* Check to see if we have an INBOX */
812
 
        $has_inbox = false;
813
 
 
814
 
        for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
815
 
            if (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
816
 
                $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
817
 
                $has_inbox = true;
818
 
                break;
819
 
            }
820
 
        }
821
 
 
822
 
        if ($has_inbox == false) {
823
 
            $lsub_ary[] = '* LSUB () NIL INBOX';
824
 
        }
825
790
 
826
791
        /*
827
 
         * Section about removing the last element was removed 
 
792
         * Section about removing the last element was removed
828
793
         * We don't return "* OK" anymore from sqimap_read_data
829
794
         */
830
 
 
831
795
        $sorted_lsub_ary = array();
832
796
        $cnt = count($lsub_ary);
833
797
        for ($i = 0; $i < $cnt; $i++) {
 
798
            /*
 
799
             * Workaround for EIMS
 
800
             * Doesn't work if the mailbox name is multiple lines
 
801
             */
 
802
            if (isset($lsub_ary[$i + 1]) &&
 
803
                ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
 
804
                     $lsub_ary[$i], $regs)) {
 
805
                $i++;
 
806
                $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
 
807
            }
 
808
 
 
809
            /*
 
810
            if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
 
811
                $flag = $regs[1];
 
812
                $mbx = trim($regs[3]);
 
813
                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
 
814
            }
 
815
            */
834
816
            $mbx = find_mailbox_name($lsub_ary[$i]);
835
 
 
836
 
            // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
837
 
            if ($imap_server_type != "uw") {                
838
 
                $noselect = check_is_noselect($lsub_ary[$i]);
839
 
                $noinferiors = check_is_noinferiors($lsub_ary[$i]);
840
 
            }
841
 
            if (substr($mbx, -1) == $delimiter) {
842
 
                $mbx = substr($mbx, 0, strlen($mbx) - 1);
843
 
            }
844
 
            $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors); 
845
 
        }
846
 
        // FIX ME this requires a config setting inside conf.pl instead of checking on server type
847
 
        if ($imap_server_type == "uw") {
848
 
           $aQuery = array();
849
 
           $aTag = array();
850
 
           // prepare an array with queries
851
 
           foreach ($sorted_lsub_ary as $aMbx) {
852
 
               $mbx = stripslashes($aMbx['mbx']);
853
 
               sqimap_prepare_pipelined_query('LIST "" ' . sqimap_encode_mailbox_name($mbx), $tag, $aQuery, false);
854
 
               $aTag[$tag] = $mbx;
855
 
           } 
856
 
           $sorted_lsub_ary = array();
857
 
           // execute all the queries at once
858
 
           $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
859
 
           foreach($aTag as $tag => $mbx) {
860
 
               if ($aServerResponse[$tag] == 'OK') {
861
 
                   $sResponse = implode('', $aResponse[$tag]);
862
 
                   $noselect = check_is_noselect($sResponse);
863
 
                   $noinferiors = check_is_noinferiors($sResponse);
864
 
                   $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
865
 
               }
866
 
           }
867
 
           $cnt = count($sorted_lsub_ary);
868
 
       }
869
 
       $sorted_lsub_ary = array_values($sorted_lsub_ary);
870
 
       array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
871
 
       $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
872
 
       return $boxesnew;
 
817
            $noselect = check_is_noselect($lsub_ary[$i]);
 
818
            if (substr($mbx, -1) == $delimiter) {
 
819
                $mbx = substr($mbx, 0, strlen($mbx) - 1);
 
820
            }
 
821
            $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
 
822
        }
 
823
        array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
 
824
 
 
825
        for($i = 0; $i < $cnt; $i++) {
 
826
            if ($sorted_lsub_ary[$i]['mbx'] == 'INBOX') {
 
827
                $inbox_in_list = true;
 
828
                break;
 
829
            }
 
830
        }
 
831
 
 
832
        /*
 
833
         * Just in case they're not subscribed to their inbox,
 
834
         * we'll get it for them anyway
 
835
         */
 
836
        if (!$inbox_in_list) {
 
837
            $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
 
838
                                             true, $response, $message);
 
839
            /* Another workaround for EIMS */
 
840
            if (isset($inbox_ary[1]) &&
 
841
                ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
 
842
                     $inbox_ary[0], $regs)) {
 
843
                $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
 
844
                                '"' . $regs[2];
 
845
            }
 
846
            $mbx = find_mailbox_name($inbox_ary[0]);
 
847
            if (substr($mbx, -1) == $delimiter) {
 
848
                $mbx = substr($mbx, 0, strlen($mbx) - 1);
 
849
            }
 
850
            if ($mbx == 'INBOX') {
 
851
                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
 
852
                sqimap_subscribe($imap_stream, 'INBOX');
 
853
                $cnt++;
 
854
            }
 
855
 
 
856
            /*
 
857
            if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
 
858
                $flag = $regs[1];
 
859
                $mbx = trim($regs[3]);
 
860
                if (substr($mbx, -1) == $delimiter) {
 
861
                    $mbx = substr($mbx, 0, strlen($mbx) - 1);
 
862
                }
 
863
                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
 
864
            }
 
865
            */
 
866
        }
 
867
        for ($i = 0 ; $i < $cnt; $i++) {
 
868
            $mbx = $sorted_lsub_ary[$i]['mbx'];
 
869
            if (($unseen_notify == 2 && $mbx == 'INBOX') ||
 
870
                ($unseen_notify == 3) ||
 
871
                ($move_to_trash && ($mbx == $trash_folder))) {
 
872
                if($sorted_lsub_ary[$i]['noselect']) {
 
873
                    $sorted_lsub_ary[$i]['unseen'] = 0;
 
874
                } else {
 
875
                    $sorted_lsub_ary[$i]['unseen'] =
 
876
                        sqimap_unseen_messages($imap_stream, $mbx);
 
877
                }
 
878
                if (($unseen_type == 2) ||
 
879
                    ($move_to_trash && ($mbx == $trash_folder)) ||
 
880
                    ($mbx == $trash_folder)) {
 
881
                    if($sorted_lsub_ary[$i]['noselect']) {
 
882
                        $sorted_lsub_ary[$i]['nummessages'] = 0;
 
883
                    } else {
 
884
                        $sorted_lsub_ary[$i]['nummessages'] =
 
885
                            sqimap_get_num_messages($imap_stream, $mbx);
 
886
                    }
 
887
                }
 
888
            }
 
889
        }
 
890
        $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
 
891
        return $boxesnew;
873
892
    }
874
893
}
875
894
 
876
 
function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
 
895
function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
877
896
    global $data_dir, $username, $list_special_folders_first,
878
897
           $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
879
898
           $move_to_trash, $move_to_sent, $save_as_draft,
880
 
           $delimiter, $imap_server_type;
 
899
           $delimiter;
881
900
 
882
901
    $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
883
902
 
885
904
    $mailboxes= new mailboxes();
886
905
    $mailboxes->is_root = true;
887
906
    $trail_del = false;
888
 
    $start = 0;
889
 
 
890
907
 
891
908
    if (isset($folder_prefix) && ($folder_prefix != '')) {
892
909
        $start = substr_count($folder_prefix,$delimiter);
907
924
        if ($mbx_ary[$i]['mbx'] !='' ) {
908
925
            $mbx = new mailboxes();
909
926
            $mailbox = $mbx_ary[$i]['mbx'];
910
 
 
911
 
            /* 
912
 
                sent subfolders messes up using existing code as subfolders
913
 
                were marked, but the parents were ordered somewhere else in
914
 
                the list, despite having "special folders at top" option set.
915
 
                Need a better method than this.
916
 
            */
917
 
/*
918
 
            if ($mailbox == 'INBOX') {
919
 
                $mbx->is_special = true;
920
 
            } elseif (stristr($trash_folder , $mailbox)) {
921
 
                $mbx->is_special = true;
922
 
            } elseif (stristr($sent_folder , $mailbox)) {
923
 
                $mbx->is_special = true;
924
 
            } elseif (stristr($draft_folder , $mailbox)) {
925
 
                $mbx->is_special = true;
926
 
            }
927
 
 
928
927
            switch ($mailbox) {
929
928
                case 'INBOX':
930
929
                    $mbx->is_inbox = true;
931
930
                    $mbx->is_special = true;
932
 
                    $mbx_ary[$i]['noselect'] = false;
933
931
                    break;
934
932
                case $trash_folder:
935
933
                    $mbx->is_trash = true;
944
942
                    $mbx->is_special = true;
945
943
                    break;
946
944
            }
947
 
*/
948
 
            $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
949
 
            $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
950
 
            $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
951
 
            $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
952
 
            if (!$mbx->is_special)
953
 
                $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
954
 
            
 
945
 
955
946
            if (isset($mbx_ary[$i]['unseen'])) {
956
947
                $mbx->unseen = $mbx_ary[$i]['unseen'];
957
948
            }
960
951
            }
961
952
 
962
953
            $mbx->is_noselect = $mbx_ary[$i]['noselect'];
963
 
            $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
964
954
 
965
955
            $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
966
956
            if ($r_del_pos) {
973
963
            $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
974
964
        }
975
965
    }
976
 
    sqimap_utf7_decode_mbx_tree($mailboxes);
977
 
    sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
 
966
 
978
967
    return $mailboxes;
979
968
}
980
969
 
981
 
function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
982
 
   if (strtoupper($mbx_tree->mailboxname_sub) == 'INBOX')
983
 
       $mbx_tree->mailboxname_sub = _("INBOX");
984
 
   else
985
 
       $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
986
 
   if ($mbx_tree->mbxs) {
987
 
      $iCnt = count($mbx_tree->mbxs);
988
 
      for ($i=0;$i<$iCnt;++$i) {
989
 
          $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
990
 
      }
991
 
   }
992
 
}
993
 
 
994
 
 
995
 
function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
996
 
   if ($mbx_tree)
997
 
   $aMbxs[] =& $mbx_tree;
998
 
   if ($mbx_tree->mbxs) {
999
 
      $iCnt = count($mbx_tree->mbxs);
1000
 
      for ($i=0;$i<$iCnt;++$i) {
1001
 
         sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
1002
 
      }
1003
 
   }
1004
 
1005
 
 
1006
 
function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
1007
 
    global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
1008
 
    $aMbxs = $aQuery = $aTag = array();
1009
 
    sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
1010
 
    // remove the root node
1011
 
    array_shift($aMbxs);
1012
 
 
1013
 
    if($unseen_notify == 3) {
1014
 
        $cnt = count($aMbxs);
1015
 
        for($i=0;$i<$cnt;++$i) {
1016
 
            $oMbx =& $aMbxs[$i];
1017
 
            if (!$oMbx->is_noselect) {
1018
 
                $mbx = $oMbx->mailboxname_full;
1019
 
                if ($unseen_type == 2 ||
1020
 
                   ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
1021
 
                   $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
1022
 
                } else {
1023
 
                   $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
1024
 
                }
1025
 
                sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
1026
 
            } else {
1027
 
                $oMbx->unseen = $oMbx->total = false;
1028
 
                $tag = false;
1029
 
            }
1030
 
            $oMbx->tag = $tag;
1031
 
            $aMbxs[$i] =& $oMbx;
1032
 
        }
1033
 
        // execute all the queries at once
1034
 
        $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
1035
 
        $cnt = count($aMbxs);
1036
 
        for($i=0;$i<$cnt;++$i) {
1037
 
            $oMbx =& $aMbxs[$i];
1038
 
            $tag = $oMbx->tag;
1039
 
            if ($tag && $aServerResponse[$tag] == 'OK') {
1040
 
                $sResponse = implode('', $aResponse[$tag]);
1041
 
                if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
1042
 
                    $oMbx->unseen = $regs[1];
1043
 
                }
1044
 
                if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
1045
 
                    $oMbx->total = $regs[1];
1046
 
                }
1047
 
           }
1048
 
           unset($oMbx->tag);
1049
 
        }
1050
 
    } else if ($unseen_notify == 2) { // INBOX only
1051
 
        $cnt = count($aMbxs);
1052
 
        for($i=0;$i<$cnt;++$i) {
1053
 
            $oMbx =& $aMbxs[$i];
1054
 
            if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
1055
 
               ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
1056
 
                 if ($unseen_type == 2 || 
1057
 
                   ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
1058
 
                    $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
1059
 
                    $oMbx->unseen = $aStatus['UNSEEN'];
1060
 
                    $oMbx->total  = $aStatus['MESSAGES'];
1061
 
                } else {
1062
 
                    $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
1063
 
                }
1064
 
                $aMbxs[$i] =& $oMbx;
1065
 
                if (!$move_to_trash && $trash_folder) {
1066
 
                    break;
1067
 
                } else {
1068
 
                   // trash comes after INBOX
1069
 
                   if ($oMbx->mailboxname_full == $trash_folder) {
1070
 
                      break;
1071
 
                   }
1072
 
                }
1073
 
            }
1074
 
        }
1075
 
    }       
1076
 
1077
 
 
1078
 
?>
 
970
?>
 
 
b'\\ No newline at end of file'