Standardize syntax
[squirrelmail.git] / src / right_main.php
CommitLineData
59177427 1<?php
a2b193bc 2
c57b0888 3/**
4 * right_main.php
5 *
c57b0888 6 * This is where the mailboxes are listed. This controls most of what
7 * goes on in SquirrelMail.
8 *
4b5049de 9 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
30967a1e 11 * @version $Id$
8f6f9ba5 12 * @package squirrelmail
c57b0888 13 */
14
ebd2391c 15/** This is the right_main page */
16define('PAGE_NAME', 'right_main');
17
202bcbcc 18//xdebug_start_profiling("/var/spool/xdebug/right_main.txt");
19
20
30967a1e 21/**
202bcbcc 22 * Include the SquirrelMail initialization file.
30967a1e 23 */
202bcbcc 24include('../include/init.php');
86725763 25
209e24bb 26/* If email_address not set and admin wants us to ask user for it,
27 * redirect to options page. */
28if ( $ask_user_info && getPref($data_dir, $username,'email_address') == "" ) {
29 header("Location: " . get_location() . "/options.php?optpage=personal");
30 exit;
31}
32
86725763 33/* SquirrelMail required files. */
202bcbcc 34require_once(SM_PATH . 'functions/imap_asearch.php');
35require_once(SM_PATH . 'functions/imap_general.php');
36require_once(SM_PATH . 'functions/imap_messages.php');
86725763 37require_once(SM_PATH . 'functions/date.php');
86725763 38require_once(SM_PATH . 'functions/mime.php');
39require_once(SM_PATH . 'functions/mailbox_display.php');
628bce99 40require_once(SM_PATH . 'functions/compose.php');
c57b0888 41
a32985a5 42/* lets get the global vars we may need */
1e12d1ff 43sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
81de00c0 44sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION);
45if (is_array($delayed_errors)) {
46 $oErrorHandler->AssignDelayedErrors($delayed_errors);
47 sqsession_unregister("delayed_errors");
48}
1e12d1ff 49sqgetGlobalVar('mailbox', $mailbox);
50sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
324ac3c5 51sqgetGlobalVar('targetMailbox', $lastTargetMailbox, SQ_POST);
1e12d1ff 52sqgetGlobalVar('note', $note, SQ_GET);
2ffeb7c5 53sqgetGlobalVar('mail_sent', $mail_sent, SQ_GET);
1e12d1ff 54
324ac3c5 55
54ce41dd 56if ( sqGetGlobalVarMultiple('startMessage', $temp, 'paginator_submit') ) {
324ac3c5 57 $startMessage = (int) $temp;
58} else {
59 $startMessage = 1;
a32985a5 60}
324ac3c5 61// sort => srt because of the changed behaviour which can break new behaviour
62if ( sqgetGlobalVar('srt', $temp, SQ_GET) ) {
63 $srt = (int) $temp;
a32985a5 64}
324ac3c5 65
66if ( sqgetGlobalVar('showall', $temp, SQ_GET) ) {
67 $showall = (int) $temp;
a32985a5 68}
324ac3c5 69
e0a6645e 70if (!sqgetGlobalVar('checkall',$checkall,SQ_GET)) {
71 $checkall = false;
a32985a5 72}
91c27aee 73
74/* future work */
75if ( sqgetGlobalVar('account', $account, SQ_GET) ) {
76 $account = (int) $account;
77} else {
78 $account = 0;
79}
80
a32985a5 81/* end of get globals */
82
83
e0e30169 84/* Open an imap connection */
6f223ace 85
190dc452 86$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
c57b0888 87
e0e30169 88$mailbox = (isset($mailbox) && $mailbox) ? $mailbox : 'INBOX';
324ac3c5 89
324ac3c5 90/**
91 * Set the global settings for a mailbox and merge them with the usersettings
92 * for the mailbox. In the future we can add more mailbox specific preferences
93 * preferences.
94 */
bdfb67f8 95
91c27aee 96$aMailboxPrefSer=getPref($data_dir, $username,'pref_'.$account.'_'.$mailbox);
324ac3c5 97if ($aMailboxPrefSer) {
98 $aMailboxPref = unserialize($aMailboxPrefSer);
91c27aee 99 $aMailboxPref[MBX_PREF_COLUMNS] = $index_order; // index_order contains the columns to show and the order of the columns
324ac3c5 100} else {
91c27aee 101 setUserPref($username,'pref_'.$account.'_'.$mailbox,serialize($default_mailbox_pref));
102 $aMailboxPref = $default_mailbox_pref;
324ac3c5 103}
104if (isset($srt)) {
105 $aMailboxPref[MBX_PREF_SORT] = (int) $srt;
106}
107
91c27aee 108$trash_folder = (isset($trash_folder)) ? $trash_folder : false;
109$sent_folder = (isset($sent_folder)) ? $sent_folder : false;
110$draft_folder = (isset($draft_folder)) ? $draft_folder : false;
111
324ac3c5 112
113/**
114 * until there is no per mailbox option screen to set prefs we override
115 * the mailboxprefs by the default ones
116 */
117$aMailboxPref[MBX_PREF_LIMIT] = (int) $show_num;
118$aMailboxPref[MBX_PREF_AUTO_EXPUNGE] = (bool) $auto_expunge;
119$aMailboxPref[MBX_PREF_INTERNALDATE] = (bool) getPref($data_dir, $username, 'internal_date_sort');
91c27aee 120$aMailboxPref[MBX_PREF_COLUMNS] = $index_order;
121
122/**
123 * Replace From => To in case it concerns a draft or sent folder
124 */
125if (($mailbox == $sent_folder || $mailbox == $draft_folder) &&
126 !in_array(SQM_COL_TO,$aMailboxPref[MBX_PREF_COLUMNS])) {
127 $aNewOrder = array(); // nice var name ;)
128 foreach($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
129 if ($iCol == SQM_COL_FROM) {
130 $iCol = SQM_COL_TO;
131 }
132 $aNewOrder[] = $iCol;
133 }
134 $aMailboxPref[MBX_PREF_COLUMNS] = $aNewOrder;
135 setUserPref($username,'pref_'.$account.'_'.$mailbox,serialize($aMailboxPref));
136}
137
138
139
140/**
141 * Set the config options for the messages list
142 */
143$aColumns = array(); // contains settings per column. Switch to key -> value based array, order is the order of the array keys
144foreach ($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
145 $aColumns[$iCol] = array();
146 switch ($iCol) {
147 case SQM_COL_SUBJ:
148 if ($truncate_subject) {
149 $aColumns[$iCol]['truncate'] = $truncate_subject;
150 }
151 break;
152 case SQM_COL_FROM:
153 case SQM_COL_TO:
154 case SQM_COL_CC:
155 case SQM_COL_BCC:
156 if ($truncate_sender) {
157 $aColumns[$iCol]['truncate'] = $truncate_sender;
158 }
159 break;
160 }
161}
162
163/**
164 * Properties required by showMessagesForMailbox
165 */
166$aProps = array(
167 'columns' => $aColumns, // columns bound settings
168 'config' => array('alt_index_colors' => $alt_index_colors, // alternating row colors (should be a template thing)
169 'highlight_list' => $message_highlight_list, // row highlighting rules
170 'fancy_index_highlite' => $fancy_index_highlite, // highlight rows on hover or on click -> check
171 'show_flag_buttons' => (isset($show_flag_buttons)) ? $show_flag_buttons : true,
172 'lastTargetMailbox' => (isset($lastTargetMailbox)) ? $lastTargetMailbox : '', // last mailbox where messages are moved/copied to
173 'trash_folder' => $trash_folder,
174 'sent_folder' => $sent_folder,
175 'draft_folder' => $draft_folder,
176 'color' => $color,
177 'enablesort' => true // enable sorting on columns
178 ),
179 'mailbox' => $mailbox,
180 'account' => (isset($account)) ? $account : 0, // future usage if we support multiple imap accounts
181 'module' => 'read_body',
182 'email' => false);
fe6efa94 183
fe6efa94 184
324ac3c5 185/**
186 * system wide admin settings and incoming vars.
187 */
188$aConfig = array(
324ac3c5 189 'user' => $username,
190 // incoming vars
91c27aee 191 'offset' => $startMessage // offset in paginator
324ac3c5 192 );
193/**
194 * The showall functionality is for the moment added to the config array
195 * to avoid storage of the showall link in the mailbox pref. We could change
196 * this behaviour later and add it to $aMailboxPref instead
197 */
198if (isset($showall)) {
91c27aee 199 $aConfig['showall'] = $showall; // show all messages in a mailbox (paginator is disabled)
200} else {
201 $showall = false;
26e90c74 202}
203
91c27aee 204
324ac3c5 205/**
206 * Retrieve the mailbox cache from the session.
207 */
208sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
209
91c27aee 210/**
211 * Select the mailbox and retrieve the cached info.
212 */
213$aMailbox = sqm_api_mailbox_select($imapConnection,$account, $mailbox,$aConfig,$aMailboxPref);
324ac3c5 214
91c27aee 215/**
216 * MOVE THIS to a central init section !!!!
217 */
218if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
219 $dir = ( isset( $languages[$squirrelmail_language]['DIR']) ) ? $languages[$squirrelmail_language]['DIR'] : 'ltr';
220 if ( $dir == 'ltr' ) {
221 $align = array('left' => 'left', 'right' => 'right');
222 } else {
223 $align = array('left' => 'right', 'right' => 'left');
224 }
225 sqsession_register($align, 'align');
226}
324ac3c5 227
228/*
229 * After initialisation of the mailbox array it's time to handle the FORM data
230 */
231$sError = handleMessageListForm($imapConnection,$aMailbox);
232if ($sError) {
233 $note = $sError;
fe6efa94 234}
139b3991 235
91c27aee 236
237
324ac3c5 238/*
239 * If we try to forward messages as attachment we have to open a new window
240 * in case of compose in new window or redirect to compose.php
241 */
242if (isset($aMailbox['FORWARD_SESSION'])) {
243 if ($compose_new_win) {
91c27aee 244 /* add the mailbox to the cache */
245 $mailbox_cache[$account.'_'.$aMailbox['NAME']] = $aMailbox;
246 sqsession_register($mailbox_cache,'mailbox_cache');
324ac3c5 247 // write the session in order to make sure that the compose window has
248 // access to the composemessages array which is stored in the session
249 session_write_close();
8517e764 250 // restart the session. Do not use sqsession_is_active because the session_id
251 // isn't empty after a session_write_close
3a1de9f1 252 sqsession_start();
91c27aee 253 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
254 $compose_width = '640';
255 }
256 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
257 $compose_height = '550';
258 }
259 // do not use &amp;, it will break the query string and $session will not be detected!!!
324ac3c5 260 $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
7e2ff844 261 '&session='.urlencode($aMailbox['FORWARD_SESSION']);
8517e764 262 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri', $compose_width, $compose_height);", '');
139b3991 263 } else {
91c27aee 264 $mailbox_cache[$account.'_'.$aMailbox['NAME']] = $aMailbox;
265 sqsession_register($mailbox_cache,'mailbox_cache');
266
324ac3c5 267 // save mailboxstate
268 sqsession_register($aMailbox,'aLastSelectedMailbox');
269 session_write_close();
270 // we have to redirect to the compose page
dc2db59a 271 $location = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
bd466893 272 '&session='.$aMailbox['FORWARD_SESSION'];
324ac3c5 273 header("Location: $location");
274 exit;
139b3991 275 }
276} else {
6f223ace 277 displayPageHeader($color, $mailbox);
278}
324ac3c5 279
0255b986 280// plugins can operate normally here (don't output anything, of course!),
281// but can also return TRUE if they want to enable the MOTD display
282// even when SM's MOTD is empty (there is plugin output that can
6957d227 283// be then hooked into in motd.tpl)
284// NOTE a TRUE return value here will cause the display of the MOTD on
285// *every* page view; if a plugin wants to support true MOTD (one-time
286// message display upon login), it should also check the value of
287// "just_logged_in" in the PHP session before returning TRUE.
0255b986 288//
289$show_motd = boolean_hook_function('right_main_after_header', $null, 1);
2ffeb7c5 290
291/* display a message to the user that their mail has been sent */
292if (isset($mail_sent) && $mail_sent == 'yes') {
d7270cc4 293 $note = _("Your mail has been sent.");
2ffeb7c5 294}
c57b0888 295if (isset($note)) {
c03e9db0 296 $oTemplate->assign('note', htmlspecialchars($note));
297 $oTemplate->display('note.tpl');
c57b0888 298}
299
6957d227 300if (sqgetGlobalVar('just_logged_in', $just_logged_in, SQ_SESSION) || $show_motd) {
301 if ($show_motd || $just_logged_in == true) {
c03e9db0 302 $motd = trim($motd);
0255b986 303 if ($show_motd || strlen($motd) > 0) {
c03e9db0 304 $oTemplate->assign('motd', $motd);
305 $oTemplate->display('motd.tpl');
a32985a5 306 }
6957d227 307
308 $just_logged_in = false;
309// FIXME: not likely to happen as SM is designed now, but the $oTemplate->display() above would send headers, therefore the following line would be broken. If $note is enabled by a plugin or any other core changes, this will break, so let's be safe and fix this up (can it be fixed by changing the order of the MOTD and the $note?)
310 sqsession_register($just_logged_in, 'just_logged_in');
23d6bd09 311 }
c57b0888 312}
91c27aee 313
91c27aee 314
324ac3c5 315if ($aMailbox['EXISTS'] > 0) {
400222be 316 $aTemplateVars = showMessagesForMailbox($imapConnection,$aMailbox,$aProps,$iError);
91c27aee 317 if ($iError) {
318
319 }
320 foreach ($aTemplateVars as $k => $v) {
321 $oTemplate->assign($k, $v);
322 }
323
324 /*
325 * TODO: To many config related vars. We should move all config related vars to
326 * one single associative array and assign that to the template
327 */
328 $oTemplate->assign('page_selector', $page_selector);
329 $oTemplate->assign('page_selector_max', $page_selector_max);
330 $oTemplate->assign('compact_paginator', $compact_paginator);
457e8593 331 $oTemplate->assign('javascript_on', checkForJavascript());
fe8103c2 332 $oTemplate->assign('base_uri', sqm_baseuri());
91c27aee 333 $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false);
29997535 334 $oTemplate->assign('icon_theme_path', $icon_theme_path);
91c27aee 335 $oTemplate->assign('aOrder', array_keys($aColumns));
336 $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ? $alt_index_colors: false);
337 $oTemplate->assign('color', $color);
338 $oTemplate->assign('align', $align);
e0a6645e 339 $oTemplate->assign('checkall', $checkall);
91c27aee 340
341 $oTemplate->display('message_list.tpl');
342
324ac3c5 343} else {
c03e9db0 344 $oTemplate->display('empty_folder.tpl');
324ac3c5 345}
7c612fdd 346
6e515418 347do_hook('right_main_bottom', $null);
c57b0888 348sqimap_logout ($imapConnection);
580e80b8 349$oTemplate->display('footer.tpl');
350
dcc1cc82 351
324ac3c5 352/* add the mailbox to the cache */
91c27aee 353$mailbox_cache[$account.'_'.$aMailbox['NAME']] = $aMailbox;
324ac3c5 354sqsession_register($mailbox_cache,'mailbox_cache');