4 ** Copyright (c) 1999-2000 The SquirrelMail development team
5 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 ** This is the code for the left bar. The left bar shows the folders
8 ** available, and has cookie information.
13 require_once('../src/validate.php');
14 require_once('../functions/array.php');
15 require_once('../functions/imap.php');
16 require_once('../functions/plugin.php');
18 /* These constants are used for folder stuff. */
19 define('SM_BOX_UNCOLLAPSED', 0);
20 define('SM_BOX_COLLAPSED', 1);
22 // open a connection on the imap port (143)
23 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); // the 10 is to hide the output
27 if ($auto_create_special && ! isset($auto_create_done)) {
28 if (isset ($sent_folder) && $sent_folder != 'none') {
29 if (!sqimap_mailbox_exists ($imapConnection, $sent_folder)) {
30 sqimap_mailbox_create ($imapConnection, $sent_folder, '');
31 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $sent_folder)) {
32 sqimap_subscribe($imapConnection, $sent_folder);
35 if (isset ($trash_folder) && $trash_folder != 'none') {
36 if (!sqimap_mailbox_exists ($imapConnection, $trash_folder)) {
37 sqimap_mailbox_create ($imapConnection, $trash_folder, '');
38 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $trash_folder)) {
39 sqimap_subscribe($imapConnection, $trash_folder);
42 $auto_create_done = true;
43 session_register('auto_create_done');
46 function formatMailboxName($imapConnection, $box_array, $delimeter) {
47 global $folder_prefix, $trash_folder, $sent_folder;
48 global $color, $move_to_sent, $move_to_trash;
49 global $unseen_notify, $unseen_type, $collapse_folders;
51 $real_box = $box_array['unformatted'];
52 $mailbox = str_replace(' ','',$box_array['formatted']);
53 $mailboxURL = urlencode($real_box);
55 /* Strip down the mailbox name. */
56 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
62 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
63 $unseen_notify == 3) {
64 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
65 if ($unseen_type == 1 && $unseen > 0) {
66 $unseen_string = "($unseen)";
68 } else if ($unseen_type == 2) {
69 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
70 $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
75 $special_color = false;
76 if ((strtolower($real_box) == 'inbox') ||
77 (($real_box == $trash_folder) && ($move_to_trash)) ||
78 (($real_box == $sent_folder) && ($move_to_sent)))
79 $special_color = true;
81 /* Start off with a blank line. */
84 /* If there are unseen message, bold the line. */
85 if ($unseen > 0) { $line .= '<B>'; }
87 /* Crate the link for this folder. */
88 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
89 if ($special_color == true)
90 $line .= "<FONT COLOR=\"$color[11]\">";
91 $line .= str_replace(' ',' ',$mailbox);
92 if ($special_color == true)
96 /* If there are unseen message, close bolding. */
97 if ($unseen > 0) { $line .= "</B>"; }
99 /* Print unseen information. */
100 if (isset($unseen_found) && $unseen_found) {
101 $line .= " <SMALL>$unseen_string</SMALL>";
104 if (($move_to_trash == true) && ($real_box == $trash_folder)) {
105 if (! isset($numMessages)) {
106 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
109 if ($numMessages > 0) {
110 $urlMailbox = urlencode($real_box);
111 $line .= "\n<small>\n";
112 $line .= " (<B><A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("purge")."</A></B>)";
113 $line .= "\n</small>\n";
117 /* Return the final product. */
121 if (isset($left_refresh) && ($left_refresh != 'None') && ($left_refresh != '')) {
122 echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n";
123 echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
124 echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
127 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
129 do_hook("left_main_before");
131 $boxes = sqimap_mailbox_list($imapConnection);
133 echo '<CENTER><FONT SIZE=4><B>';
134 echo _("Folders") . "</B><BR></FONT>\n\n";
136 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">';
137 echo _("refresh folder list");
138 echo '</A>)</SMALL></CENTER><BR>';
139 $delimeter = sqimap_get_delimiter($imapConnection);
141 if (isset($collapse_folders) && $collapse_folders) {
142 /* If directed, collapse or uncollapse a folder. */
144 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED
);
145 } else if (isset($unfold)) {
146 setPref($data_dir, $username, 'collapse_folder_' . $unfold, $SM_BOX_UNCOLLAPSED);
150 /* Prepare do do out collapsedness and visibility computation. */
152 $boxcount = count($boxes);
154 /* Compute the collapsedness and visibility of each box. */
155 while ($curbox < $boxcount) {
156 $boxes[$curbox]['visible'] = true;
157 compute_folder_children($curbox, $boxcount);
160 for ($i = 0;$i < count($boxes); $i++
) {
161 if ($boxes[$i]['visible'] == true) {
162 $mailbox = $boxes[$i]['formatted'];
163 $mblevel = substr_count($boxes[$i]['unformatted'], $delimeter) +
1;
165 /* Create the prefix for the folder name and link. */
166 $prefix = str_repeat(' ',$mblevel);
167 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
168 $prefix = str_replace(' ',' ',substr($prefix,0,strlen($prefix)-2));
169 $prefix .= create_collapse_link($i) . ' ';
171 $prefix = str_replace(' ',' ',$prefix);
173 $line = "<NOBR><TT>$prefix</TT>";
175 /* Add the folder name and link. */
176 if (in_array('noselect', $boxes[$i]['flags'])) {
177 $line .= "<FONT COLOR=\"$color[10]\">";
178 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
179 $line .= str_replace(' ', ' ', $mailbox);
183 $line .= formatMailboxName($imapConnection, $boxes[$i], $delimeter);
186 /* Put the final touches on our folder line. */
187 $line .= "</NOBR><BR>\n";
189 /* Output the line for this folder. */
193 sqimap_logout($imapConnection);
194 do_hook("left_main_after");
197 * Create the link for a parent folder that will allow that
198 * parent folder to either be collapsed or expaned, as is
199 * currently appropriate.
201 function create_collapse_link($boxnum) {
203 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
205 /* Create the link for this collapse link. */
206 $link = '<a target="left" style="text-decoration:none" ';
207 $link .= 'href="left_main.php?';
208 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED
) {
209 $link .= "unfold=$mailbox\">+";
211 $link .= "fold=$mailbox\">-";
215 /* Return the finished product. */
220 * This simple function checks if a box is another box's parent.
222 function is_parent_box($curbox_name, $parbox_name) {
225 /* Extract the name of the parent of the current box. */
226 $curparts = explode($delimeter, $curbox_name);
227 $curname = array_pop($curparts);
228 $actual_parname = implode($delimeter, $curparts);
229 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
231 /* Compare the actual with the given parent name. */
232 return ($parbox_name == $actual_parname);
236 * Recursive function that computes the collapsed status and parent
237 * (or not parent) status of this box, and the visiblity and collapsed
238 * status and parent (or not parent) status for all children boxes.
240 function compute_folder_children(&$parbox, $boxcount) {
241 global $boxes, $data_dir, $username;
242 $nextbox = $parbox +
1;
244 /* Retreive the name for the parent box. */
245 $parbox_name = $boxes[$parbox]['unformatted'];
247 /* 'Initialize' this parent box to childless. */
248 $boxes[$parbox]['parent'] = false;
250 /* Compute the collapse status for this box. */
252 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
253 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED
: $collapse);
254 $boxes[$parbox]['collapse'] = $collapse;
256 /* Otherwise, get the name of the next box. */
257 if (isset($boxes[$nextbox]['unformatted']))
258 $nextbox_name = $boxes[$nextbox]['unformatted'];
262 /* Compute any children boxes for this box. */
263 while (($nextbox < $boxcount) &&
264 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
266 /* Note that this 'parent' box has at least one child. */
267 $boxes[$parbox]['parent'] = true;
269 /* Compute the visiblity of this box. */
270 if ($boxes[$parbox]['visible'] &&
271 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED
)) {
272 $boxes[$nextbox]['visible'] = true;
274 $boxes[$nextbox]['visible'] = false;
277 /* Compute the visibility of any child boxes. */
278 compute_folder_children($nextbox, $boxcount);
281 /* Set the parent box to the current next box. */
284 echo "</BODY></HTML>\n";