262a97175823828a5fe6d2117d84e69fcb5e2f86
[squirrelmail.git] / src / left_main.php
1 <?php
2
3 /**
4 * left_main.php
5 *
6 * This is the code for the left bar. The left bar shows the folders
7 * available, and has cookie information.
8 *
9 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
19 define('SM_PATH','../');
20
21 /* SquirrelMail required files. */
22 include_once(SM_PATH . 'include/validate.php');
23 require_once(SM_PATH . 'functions/imap.php');
24 require_once(SM_PATH . 'functions/plugin.php');
25 require_once(SM_PATH . 'functions/page_header.php');
26 require_once(SM_PATH . 'functions/html.php');
27 require_once(SM_PATH . 'functions/date.php');
28
29 /** add required includes */
30 include_once(SM_PATH . 'templates/util_global.php');
31 include_once(SM_PATH . 'templates/util_left_main.php');
32
33 /* These constants are used for folder stuff. */
34 define('SM_BOX_UNCOLLAPSED', 0);
35 define('SM_BOX_COLLAPSED', 1);
36
37 /* --------------------- FUNCTIONS ------------------------- */
38
39
40
41 /**
42 * Recursive functions to output a tree of folders.
43 * They are called on a list of boxes and iterates over that tree.
44 *
45 * NOTE: These functions are deprecated and replaced with templates in 1.5.2.
46 * They remain until the advanced tree tempalte is completed also,
47 * at which point both functions below will be removed
48 *
49 * @since 1.3.0
50 * @deprecated
51 */
52 function ListBoxes ($boxes, $j=0) {
53 return '';
54 }
55
56 function ListAdvancedBoxes ($boxes, $mbx, $j='ID.0000' ) {
57 global $data_dir, $username, $color, $unseen_notify, $unseen_type, $unseen_cum,
58 $move_to_trash, $trash_folder, $collapse_folders, $use_special_folder_color;
59
60 if (empty($boxes)) {
61 return;
62 }
63
64 /* use_folder_images only works if the images exist in ../images */
65 $use_folder_images = true;
66
67 $pre = '';
68 $end = '';
69 $collapse = false;
70 $unseen_found = false;
71 $unseen = 0;
72
73 $mailbox = $boxes->mailboxname_full;
74 $mailboxURL = urlencode($mailbox);
75
76 /* get collapse information */
77 if ($collapse_folders) {
78 $form_entry = $j.'F';
79 if (isset($mbx) && isset($mbx[$form_entry])) {
80 $collapse = $mbx[$form_entry];
81 setPref($data_dir, $username, 'collapse_folder_'.$boxes->mailboxname_full ,
82 $collapse ? SM_BOX_COLLAPSED : SM_BOX_UNCOLLAPSED);
83 } else {
84 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $mailbox);
85 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED : $collapse);
86 }
87 $img_src = ($collapse ? '../images/plus.png' : '../images/minus.png');
88 $collapse_link = '<a href="javascript:void(0)">' .
89 " <img src=\"$img_src\" border=\"1\" id=$j onclick=\"hidechilds(this)\" style=\"cursor:hand\" /></a>";
90 } else {
91 $collapse_link='';
92 }
93
94 /* get unseen/total messages information */
95 /* Only need to display info when option is set */
96 if (isset($unseen_notify) && ($unseen_notify > 1)) {
97 /* handle Cumulative Unread Message Notification */
98 if ($collapse && $unseen_cum) {
99 foreach ($boxes->mbxs as $cumn_box) {
100 if (!empty($cumn_box->unseen)) $boxes->unseen += $cumn_box->unseen;
101 if (!empty($cumn_box->total)) $boxes->total += $cumn_box->total;
102 }
103 }
104 if (($boxes->unseen !== false) || ($boxes->total !== false)) {
105 if ($boxes->unseen !== false) $unseen = $boxes->unseen;
106 /*
107 * Should only display unseen info if the folder is inbox
108 * or you set the option for all folders
109 */
110 if ((strtolower($mailbox) == 'inbox') || ($unseen_notify == 3)) {
111 $unseen_string = $unseen;
112
113 /* If users requests, display message count too */
114 if (isset($unseen_type) && ($unseen_type == 2) && ($boxes->total !== false)) {
115 $unseen_string .= '/' . $boxes->total;
116 }
117 if (isset($boxes->recent) && $boxes->recent > 0) {
118 $unseen_string = "<span class=\"leftrecent\">($unseen_string)</span>";
119 } else {
120 $unseen_string = "<span class=\"leftunseen\">($unseen_string)</span>";
121 }
122
123 /*
124 * Finally allow the script to display the values by setting a boolean.
125 * This can only occur if the unseen count is great than 0 (if you have
126 * unseen count only), or you have the message count too.
127 */
128 if (($unseen > 0) || (isset($unseen_type) && ($unseen_type ==2))) {
129 $unseen_found = true;
130 }
131 }
132 }
133 }
134
135 /* If there are unseen message, bold the line. */
136 if ($unseen > 0) { $pre .= '<b>'; }
137
138 /* color special boxes */
139 if ($use_special_folder_color && $boxes->is_special) {
140 $pre .= "<span class=\"leftspecial\">";
141 $end .= '</span>';
142 }
143
144 /* If there are unseen message, close bolding. */
145 if ($unseen > 0) { $end .= '</b>'; }
146
147 /* Print unseen information. */
148 if ($unseen_found) {
149 $end .= "&nbsp;$unseen_string";
150 }
151
152 if (($move_to_trash) && ($mailbox == $trash_folder)) {
153 $pre = "<a class=\"mbx_link\" href=\"right_main.php?PG_SHOWALL=0&amp;startMessage=1&amp;mailbox=$mailboxURL\" target=\"right\">" . $pre;
154 $end .= '</a>';
155 $end .= "\n<small>\n" .
156 '&nbsp;&nbsp;[<a class="mbx_link" href="empty_trash.php">'._("Purge").'</a>]'.
157 '</small>';
158 } else {
159 if (!$boxes->is_noselect) { /* \Noselect boxes can't be selected */
160 $pre = "<a class=\"mbx_link\" href=\"right_main.php?PG_SHOWALL=0&amp;startMessage=1&amp;mailbox=$mailboxURL\" target=\"right\">" . $pre;
161 $end .= '</a>';
162 }
163 }
164
165 // let plugins fiddle with end of line
166 global $imapConnection;
167 $end .= concat_hook_function('left_main_after_each_folder',
168 array(isset($numMessages) ? $numMessages : '',
169 $boxes->mailboxname_full, $imapConnection));
170
171 $out = '';
172 if (!$boxes->is_root) {
173 if ($use_folder_images) {
174 if ($boxes->is_inbox) {
175 $folder_img = '../images/inbox.png';
176 } else if ($boxes->is_sent) {
177 $folder_img = '../images/senti.png';
178 } else if ($boxes->is_trash) {
179 $folder_img = '../images/delitem.png';
180 } else if ($boxes->is_draft) {
181 $folder_img = '../images/draft.png';
182 } else if ($boxes->is_noinferiors) {
183 $folder_img = '../images/folder_noinf.png';
184 } else {
185 $folder_img = '../images/folder.png';
186 }
187 $folder_img = '&nbsp;<img src="'.$folder_img.'" height="15" />&nbsp;';
188 } else {
189 $folder_img = '';
190 }
191 if (!isset($boxes->mbxs[0])) {
192 $out .= ' ' . html_tag( 'div',
193 $pre . $folder_img .
194 str_replace( array(' ','<','>'),
195 array('&nbsp;','&lt;','&gt;'),
196 $boxes->mailboxname_sub) .
197 $end,
198 'left', '', 'class="mbx_sub" id="' .$j. '"' ) . "\n";
199 } else {
200
201 $out .= ' ' . html_tag( 'div',
202 $collapse_link . $pre . $folder_img . '&nbsp;'. $boxes->mailboxname_sub . $end ,
203 'left', '', 'class="mbx_par" id="' .$j. 'P"' ) . "\n";
204 $out .= ' <input type="hidden" name="mbx['.$j. 'F]" value="'.$collapse.'" id="mbx['.$j.'F]" />'."\n";
205 }
206 }
207
208 $visible = ($collapse ? ' style="display:none"' : ' style="display:block"');
209 if (isset($boxes->mbxs[0]) && !$boxes->is_root) /* mailbox contains childs */
210 $out .= html_tag( 'div', '', 'left', '', 'class="par_area" id='.$j.'.0000 '. $visible ) . "\n";
211
212 if ($j !='ID.0000') {
213 $j = $j .'.0000';
214 }
215 for ($i = 0; $i <count($boxes->mbxs); $i++) {
216 $j++;
217 $out .= ListAdvancedBoxes($boxes->mbxs[$i],$mbx,$j);
218 }
219 if (isset($boxes->mbxs[0]) && !$boxes->is_root) {
220 $out .= '</div>'."\n\n";
221 }
222
223 return $out;
224 }
225
226
227
228
229 /* -------------------- MAIN ------------------------ */
230
231 /* get globals */
232 sqgetGlobalVar('username', $username, SQ_SESSION);
233 sqgetGlobalVar('key', $key, SQ_COOKIE);
234 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
235 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
236
237 sqgetGlobalVar('fold', $fold, SQ_GET);
238 sqgetGlobalVar('unfold', $unfold, SQ_GET);
239
240 /* end globals */
241
242 // open a connection on the imap port (143)
243 // why hide the output?
244 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, true);
245
246 /**
247 * Using stristr since very old preferences may contain "None" and "none".
248 */
249 if (!empty($left_refresh) &&
250 !stristr($left_refresh, 'none')){
251 $xtra = "\n<meta http-equiv=\"Expires\" content=\"Thu, 01 Dec 1994 16:00:00 GMT\" />\n" .
252 "<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n".
253 "<meta http-equiv=\"REFRESH\" content=\"$left_refresh;URL=left_main.php\" />\n";
254 } else {
255 $xtra = '';
256 }
257
258 /**
259 * $advanced_tree and is a boolean var which is default set to default
260 * SM behaviour.
261 * Setting $advanced tree to true causes SM to display a experimental
262 * mailbox-tree with dhtml behaviour.
263 *
264 * See templates/default/left_main_advanced.tpl
265 **/
266
267 /* set this to true if you want to see a nicer mailboxtree */
268 if (empty($advanced_tree)) {
269 $advanced_tree=false;
270 }
271
272 // get mailbox list and cache it
273 $mailboxes=sqimap_get_mailboxes($imapConnection,false,$show_only_subscribed_folders);
274
275 displayHtmlHeader( 'SquirrelMail', $xtra );
276
277 sqgetGlobalVar('auto_create_done',$auto_create_done,SQ_SESSION);
278 /* If requested and not yet complete, attempt to autocreate folders. */
279 if ($auto_create_special && !isset($auto_create_done)) {
280 $autocreate = array($sent_folder, $trash_folder, $draft_folder);
281 $folders_created = false;
282 foreach( $autocreate as $folder ) {
283 if (($folder != '') && ($folder != 'none')) {
284 // use $mailboxes array for checking if mailbox exists
285 if ( !sqimap_mailbox_exists($imapConnection, $folder, $mailboxes)) {
286 sqimap_mailbox_create($imapConnection, $folder, '');
287 $folders_created = true;
288 } else {
289 // check for subscription is useless and expensive, just
290 // surpress the NO response. Unless we're on Mecury, which
291 // will just subscribe a folder again if it's already
292 // subscribed.
293 if ( strtolower($imap_server_type) != 'mercury32' ||
294 !sqimap_mailbox_is_subscribed($imapConnection, $folder) ) {
295 sqimap_subscribe($imapConnection, $folder, false);
296 $folders_created = true;
297 }
298 }
299 }
300 }
301
302 /* Let the world know that autocreation is complete! Hurrah! */
303 $auto_create_done = TRUE;
304 sqsession_register($auto_create_done, 'auto_create_done');
305 // reload mailbox list
306 if ($folders_created)
307 $mailboxes=sqimap_get_mailboxes($imapConnection,true,$show_only_subscribed_folders);
308 }
309
310 $clock = '';
311 if ($date_format != 6) {
312 /* First, display the clock. */
313 if ($hour_format == 1) {
314 $hr = 'H:i';
315 if ($date_format == 4) {
316 $hr .= ':s';
317 }
318 } else {
319 if ($date_format == 4) {
320 $hr = 'g:i:s a';
321 } else {
322 $hr = 'g:i a';
323 }
324 }
325
326 switch( $date_format ) {
327 case 0:
328 $clk = date('Y-m-d '.$hr. ' T', time());
329 break;
330 case 1:
331 $clk = date('m/d/y '.$hr, time());
332 break;
333 case 2:
334 $clk = date('d/m/y '.$hr, time());
335 break;
336 case 4:
337 case 5:
338 $clk = date($hr, time());
339 break;
340 default:
341 $clk = getDayAbrv( date( 'w', time() ) ) . date( ', ' . $hr, time() );
342 }
343 $clk = str_replace(' ','&nbsp;',$clk);
344
345 $clock = '<small><span style="white-space: nowrap;">'
346 . str_replace(' ', '&nbsp;', _("Last Refresh"))
347 . ":</span><br /><span style=\"white-space: nowrap;\">$clk</span></small><br />\n";
348 }
349
350 if ( $collapse_folders ) {
351 /* If directed, collapse or uncollapse a folder. */
352 if (isset($fold)) {
353 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED);
354 } else if (isset($unfold)) {
355 setPref($data_dir, $username, 'collapse_folder_' . $unfold, SM_BOX_UNCOLLAPSED);
356 }
357 }
358
359 /* Get unseen/total display prefs */
360 $unseen_type = getPref( $data_dir , $username , 'unseen_type' );
361 $unseen_notify = getPref( $data_dir , $username , 'unseen_notify' );
362
363 if (empty($unseen_type)) {
364 if (!empty($default_unseen_type)) {
365 $unseen_type = $default_unseen_type;
366 } else {
367 $unseen_type = 1;
368 }
369 }
370
371 if (empty($unseen_notify)) {
372 if (!empty($default_unseen_notify)) {
373 $unseen_notify = $default_unseen_notify;
374 } else {
375 $unseen_notify = 0;
376 }
377 }
378
379 /**
380 * pass $mailboxes now instead of $imapconnection - sqimap_get_mailboxes() has been separated from
381 * sqimap_mailbox_tree() so that the cached mailbox list can be used elsewhere in left_main and beyond
382 */
383 $boxes = sqimap_mailbox_tree($imapConnection,$mailboxes,$show_only_subscribed_folders);
384
385 $mailbox_listing = '';
386 if (isset($advanced_tree) && $advanced_tree) {
387 $mailbox_listing = '<form name="collapse" action="left_main.php" method="post" ' .
388 'enctype="multipart/form-data">'."\n";
389 $mailbox_listing .= '<button type="submit" class="button" onmouseover="buttonover(this,true)" onmouseout="buttonover(this,false)" onmousedown="buttonclick(this,true)" onmouseup="buttonclick(this,false)">'. _("Save folder tree") .'</button><br /><br />';
390 $mailbox_listing .= '<div id="mailboxes" class="mailboxes">'."\n\n";
391 sqgetGlobalVar('mbx', $mbx, SQ_POST);
392 if (!isset($mbx)) $mbx=NULL;
393 $mailbox_listing .=ListAdvancedBoxes($boxes, $mbx);
394 $mailbox_listing .= '</div>';
395 $mailbox_listing .= '</form>'."\n";
396 } else {
397 $mailbox_listing = ListBoxes($boxes);
398 }
399
400 $mailbox_structure = getBoxStructure($boxes);
401
402 $oTemplate->assign('clock', $clock);
403 $oTemplate->assign('mailbox_listing', $mailbox_listing);
404 $oTemplate->assign('location_of_bar', $location_of_bar);
405 $oTemplate->assign('left_size', $left_size);
406
407 $oTemplate->assign('mailboxes', $mailbox_structure);
408 $oTemplate->assign('imapConnection', $imapConnection);
409
410 $oTemplate->assign('unread_notification_enabled', $unseen_notify!=1);
411 $oTemplate->assign('unread_notification_cummulative', $unseen_cum==1);
412 $oTemplate->assign('unread_notification_allFolders', $unseen_notify == 3);
413 $oTemplate->assign('unread_notification_displayTotal', $unseen_type == 2);
414 $oTemplate->assign('collapsable_folders_enabled', $collapse_folders==1);
415 $oTemplate->assign('icon_theme_path', $icon_theme_path);
416 $oTemplate->assign('use_special_folder_color', $use_special_folder_color);
417 $oTemplate->assign('message_recycling_enabled', $move_to_trash);
418 $oTemplate->assign('trash_folder_name', $trash_folder);
419
420 if (isset($advanced_tree) && $advanced_tree) {
421 $oTemplate->display('left_main_advanced.tpl');
422 } else {
423 $oTemplate->display('left_main.tpl');
424 }
425
426 sqimap_logout($imapConnection);
427 $oTemplate->display('footer.tpl');
428 ?>