Rearranging (2)
[squirrelmail.git] / src / left_main.php
CommitLineData
59177427 1<?php
21c3249f 2 /**
a09387f4 3 ** left_main.php
ef870322 4 ** Copyright (c) 1999-2000 The SquirrelMail development team
5 ** Licensed under the GNU GPL. For full terms see the file COPYING.
21c3249f 6 **
7 ** This is the code for the left bar. The left bar shows the folders
8 ** available, and has cookie information.
9 **
245a6892 10 ** $Id$
21c3249f 11 **/
12
a6d2e0de 13 require_once('../src/validate.php');
14 require_once('../functions/array.php');
15 require_once('../functions/imap.php');
16 require_once('../functions/plugin.php');
142499d4 17
a6d2e0de 18 /* These constants are used for folder stuff. */
19 define('SM_BOX_UNCOLLAPSED', 0);
20 define('SM_BOX_COLLAPSED', 1);
21
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
f7bfc9de 24
a6d2e0de 25 displayHtmlHeader();
21c3249f 26
7a906c8e 27 if ($auto_create_special && ! isset($auto_create_done)) {
d6def997 28 if (isset ($sent_folder) && $sent_folder != 'none') {
f7bfc9de 29 if (!sqimap_mailbox_exists ($imapConnection, $sent_folder)) {
d6def997 30 sqimap_mailbox_create ($imapConnection, $sent_folder, '');
8d636967 31 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $sent_folder)) {
99c7a62c 32 sqimap_subscribe($imapConnection, $sent_folder);
f7bfc9de 33 }
34 }
d6def997 35 if (isset ($trash_folder) && $trash_folder != 'none') {
f7bfc9de 36 if (!sqimap_mailbox_exists ($imapConnection, $trash_folder)) {
d6def997 37 sqimap_mailbox_create ($imapConnection, $trash_folder, '');
8d636967 38 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $trash_folder)) {
99c7a62c 39 sqimap_subscribe($imapConnection, $trash_folder);
f7bfc9de 40 }
d17145d9 41 if (isset ($draft_folder) && $draft_folder != 'none') {
42 if (!sqimap_mailbox_exists ($imapConnection, $draft_folder)) {
43 sqimap_mailbox_create ($imapConnection, $draft_folder, '');
44 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $draft_folder)) {
45 sqimap_subscribe($imapConnection, $draft_folder);
46 }
47 }
48 }
7a906c8e 49 $auto_create_done = true;
50 session_register('auto_create_done');
f7bfc9de 51 }
52
235a65d5 53 function formatMailboxName($imapConnection, $box_array, $delimeter) {
5bdd7223 54 global $folder_prefix, $trash_folder, $sent_folder;
55 global $color, $move_to_sent, $move_to_trash;
235a65d5 56 global $unseen_notify, $unseen_type, $collapse_folders;
d17145d9 57 global $draft_folder, $save_as_draft;
235a65d5 58
59 $real_box = $box_array['unformatted'];
a6d2e0de 60 $mailbox = str_replace('&nbsp;','',$box_array['formatted']);
5c42b0dd 61 $mailboxURL = urlencode($real_box);
a6d2e0de 62
63 /* Strip down the mailbox name. */
64 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
65 $mailbox = $regs[2];
66 }
0cd84d75 67
235a65d5 68 $unseen = 0;
4d2c01e3 69
d6def997 70 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
235a65d5 71 $unseen_notify == 3) {
4d2c01e3 72 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
24fc5dd2 73 if ($unseen_type == 1 && $unseen > 0) {
74 $unseen_string = "($unseen)";
75 $unseen_found = true;
76 } else if ($unseen_type == 2) {
77 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
78 $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
79 $unseen_found = true;
80 }
81 }
5bdd7223 82
7ce342dc 83 $special_color = false;
d6def997 84 if ((strtolower($real_box) == 'inbox') ||
235a65d5 85 (($real_box == $trash_folder) && ($move_to_trash)) ||
4dea7315 86 (($real_box == $sent_folder) && ($move_to_sent)) ||
d17145d9 87 (($real_box == $draft_folder) && ($save_as_draft)))
88 $special_color = true;
235a65d5 89
a6d2e0de 90 /* Start off with a blank line. */
91 $line = '';
92
93 /* If there are unseen message, bold the line. */
94 if ($unseen > 0) { $line .= '<B>'; }
95
96 /* Crate the link for this folder. */
0afa6558 97 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
235a65d5 98 if ($special_color == true)
99 $line .= "<FONT COLOR=\"$color[11]\">";
d3b3cda7 100 $line .= str_replace(' ','&nbsp;',$mailbox);
235a65d5 101 if ($special_color == true)
a6d2e0de 102 $line .= "</FONT>";
103 $line .= '</A>';
7ce342dc 104
a6d2e0de 105 /* If there are unseen message, close bolding. */
106 if ($unseen > 0) { $line .= "</B>"; }
235a65d5 107
a6d2e0de 108 /* Print unseen information. */
245a6892 109 if (isset($unseen_found) && $unseen_found) {
a6d2e0de 110 $line .= "&nbsp;<SMALL>$unseen_string</SMALL>";
7ce342dc 111 }
112
1e0628fb 113 if (($move_to_trash == true) && ($real_box == $trash_folder)) {
a6d2e0de 114 if (! isset($numMessages)) {
b4cf620b 115 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
a6d2e0de 116 }
b4cf620b 117
a6d2e0de 118 if ($numMessages > 0) {
b4cf620b 119 $urlMailbox = urlencode($real_box);
120 $line .= "\n<small>\n";
121 $line .= " &nbsp; (<B><A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("purge")."</A></B>)";
122 $line .= "\n</small>\n";
12d61439 123 }
d92b6f31 124 }
d92b6f31 125
a6d2e0de 126 /* Return the final product. */
127 return ($line);
469eb37b 128 }
a6d2e0de 129
d6def997 130 if (isset($left_refresh) && ($left_refresh != 'None') && ($left_refresh != '')) {
a6d2e0de 131 echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n";
132 echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
133 echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
134 }
813eba2f 135
a6d2e0de 136 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
235a65d5 137
a6d2e0de 138 do_hook("left_main_before");
235a65d5 139
a6d2e0de 140 $boxes = sqimap_mailbox_list($imapConnection);
141
d6def997 142 echo '<CENTER><FONT SIZE=4><B>';
a6d2e0de 143 echo _("Folders") . "</B><BR></FONT>\n\n";
144
d6def997 145 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">';
a6d2e0de 146 echo _("refresh folder list");
d6def997 147 echo '</A>)</SMALL></CENTER><BR>';
a6d2e0de 148 $delimeter = sqimap_get_delimiter($imapConnection);
149
150 if (isset($collapse_folders) && $collapse_folders) {
151 /* If directed, collapse or uncollapse a folder. */
152 if (isset($fold)) {
153 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED);
154 } else if (isset($unfold)) {
155 setPref($data_dir, $username, 'collapse_folder_' . $unfold, $SM_BOX_UNCOLLAPSED);
156 }
157 }
158
159 /* Prepare do do out collapsedness and visibility computation. */
160 $curbox = 0;
161 $boxcount = count($boxes);
162
163 /* Compute the collapsedness and visibility of each box. */
164 while ($curbox < $boxcount) {
165 $boxes[$curbox]['visible'] = true;
166 compute_folder_children($curbox, $boxcount);
167 }
168
169 for ($i = 0;$i < count($boxes); $i++) {
170 if ($boxes[$i]['visible'] == true) {
171 $mailbox = $boxes[$i]['formatted'];
172 $mblevel = substr_count($boxes[$i]['unformatted'], $delimeter) + 1;
235a65d5 173
a6d2e0de 174 /* Create the prefix for the folder name and link. */
175 $prefix = str_repeat(' ',$mblevel);
176 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
177 $prefix = str_replace(' ','&nbsp;',substr($prefix,0,strlen($prefix)-2));
178 $prefix .= create_collapse_link($i) . '&nbsp;';
179 } else {
180 $prefix = str_replace(' ','&nbsp;',$prefix);
235a65d5 181 }
a6d2e0de 182 $line = "<NOBR><TT>$prefix</TT>";
183
184 /* Add the folder name and link. */
185 if (in_array('noselect', $boxes[$i]['flags'])) {
186 $line .= "<FONT COLOR=\"$color[10]\">";
187 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
188 $line .= str_replace(' ', '&nbsp;', $mailbox);
189 }
190 $line .= '</FONT>';
191 } else {
192 $line .= formatMailboxName($imapConnection, $boxes[$i], $delimeter);
193 }
194
195 /* Put the final touches on our folder line. */
196 $line .= "</NOBR><BR>\n";
197
198 /* Output the line for this folder. */
199 echo $line;
200 }
201 }
202 sqimap_logout($imapConnection);
203 do_hook("left_main_after");
204
205 /**
206 * Create the link for a parent folder that will allow that
207 * parent folder to either be collapsed or expaned, as is
208 * currently appropriate.
209 */
210 function create_collapse_link($boxnum) {
211 global $boxes;
212 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
213
214 /* Create the link for this collapse link. */
215 $link = '<a target="left" style="text-decoration:none" ';
216 $link .= 'href="left_main.php?';
217 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED) {
218 $link .= "unfold=$mailbox\">+";
219 } else {
220 $link .= "fold=$mailbox\">-";
221 }
222 $link .= '</a>';
223
224 /* Return the finished product. */
225 return ($link);
226 }
235a65d5 227
a6d2e0de 228 /**
229 * This simple function checks if a box is another box's parent.
230 */
231 function is_parent_box($curbox_name, $parbox_name) {
232 global $delimeter;
233
234 /* Extract the name of the parent of the current box. */
235 $curparts = explode($delimeter, $curbox_name);
236 $curname = array_pop($curparts);
237 $actual_parname = implode($delimeter, $curparts);
238 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
239
240 /* Compare the actual with the given parent name. */
241 return ($parbox_name == $actual_parname);
242 }
243
244 /**
245 * Recursive function that computes the collapsed status and parent
246 * (or not parent) status of this box, and the visiblity and collapsed
247 * status and parent (or not parent) status for all children boxes.
248 */
249 function compute_folder_children(&$parbox, $boxcount) {
4aaea201 250 global $boxes, $data_dir, $username;
a6d2e0de 251 $nextbox = $parbox + 1;
252
253 /* Retreive the name for the parent box. */
254 $parbox_name = $boxes[$parbox]['unformatted'];
255
256 /* 'Initialize' this parent box to childless. */
257 $boxes[$parbox]['parent'] = false;
258
259 /* Compute the collapse status for this box. */
260 $collapse = 0;
261 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
262 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED : $collapse);
263 $boxes[$parbox]['collapse'] = $collapse;
264
265 /* Otherwise, get the name of the next box. */
4aaea201 266 if (isset($boxes[$nextbox]['unformatted']))
267 $nextbox_name = $boxes[$nextbox]['unformatted'];
268 else
269 $nextbox_name = '';
a6d2e0de 270
271 /* Compute any children boxes for this box. */
272 while (($nextbox < $boxcount) &&
273 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
274
275 /* Note that this 'parent' box has at least one child. */
276 $boxes[$parbox]['parent'] = true;
277
278 /* Compute the visiblity of this box. */
279 if ($boxes[$parbox]['visible'] &&
280 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED)) {
281 $boxes[$nextbox]['visible'] = true;
282 } else {
283 $boxes[$nextbox]['visible'] = false;
284 }
285
286 /* Compute the visibility of any child boxes. */
287 compute_folder_children($nextbox, $boxcount);
288 }
289
290 /* Set the parent box to the current next box. */
291 $parbox = $nextbox;
292 }
d6def997 293 echo "</BODY></HTML>\n";
4aaea201 294?>