59177427 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
0fdc2fb6 |
4 | * mailbox_display.php |
5 | * |
6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This contains functions that display mailbox information, such as the |
10 | * table row that has sender, date, subject, etc... |
11 | * |
763b63fe |
12 | * @version $Id$ |
0fdc2fb6 |
13 | * @package squirrelmail |
14 | */ |
a4c2cd49 |
15 | |
d6c32258 |
16 | /** The standard includes.. */ |
b68edc75 |
17 | require_once(SM_PATH . 'functions/strings.php'); |
18 | require_once(SM_PATH . 'functions/html.php'); |
19 | require_once(SM_PATH . 'class/html.class.php'); |
20 | require_once(SM_PATH . 'functions/imap_mailbox.php'); |
26454147 |
21 | require_once(SM_PATH . 'functions/imap_messages.php'); |
324ac3c5 |
22 | require_once(SM_PATH . 'functions/imap_asearch.php'); |
26454147 |
23 | require_once(SM_PATH . 'functions/mime.php'); |
b531f8ea |
24 | require_once(SM_PATH . 'functions/forms.php'); |
43fdb2a4 |
25 | |
d6c32258 |
26 | /** |
0fdc2fb6 |
27 | * default value for page_selector_max |
28 | */ |
6c930ade |
29 | define('PG_SEL_MAX', 10); |
30 | |
324ac3c5 |
31 | /** |
32 | * The number of pages to cache msg headers |
33 | */ |
34 | define('SQM_MAX_PAGES_IN_CACHE',5); |
35 | |
8cc8ec79 |
36 | /** |
37 | * Sort constants used for sorting of messages |
38 | */ |
03975a39 |
39 | define('SQSORT_NONE',0); |
40 | define('SQSORT_DATE_ASC',1); |
41 | define('SQSORT_DATE_DEC',2); |
42 | define('SQSORT_FROM_ASC',3); |
43 | define('SQSORT_FROM_DEC',4); |
44 | define('SQSORT_SUBJ_ASC',5); |
45 | define('SQSORT_SUBJ_DEC',6); |
46 | define('SQSORT_SIZE_ASC',7); |
47 | define('SQSORT_SIZE_DEC',8); |
48 | define('SQSORT_TO_ASC',9); |
49 | define('SQSORT_TO_DEC',10); |
50 | define('SQSORT_CC_ASC',11); |
51 | define('SQSORT_CC_DEC',12); |
52 | define('SQSORT_INT_DATE_ASC',13); |
53 | define('SQSORT_INT_DATE_DEC',14); |
324ac3c5 |
54 | |
55 | define('SQSORT_THREAD',32); |
56 | |
57 | |
58 | define('MBX_PREF_SORT',0); |
59 | define('MBX_PREF_LIMIT',1); |
60 | define('MBX_PREF_AUTO_EXPUNGE',2); |
61 | define('MBX_PREF_INTERNALDATE',3); |
62 | define('SQM_MAX_MBX_IN_CACHE',3); |
63 | // define('MBX_PREF_FUTURE',unique integer key); |
64 | |
d6c32258 |
65 | /** |
0fdc2fb6 |
66 | * @param mixed $start UNDOCUMENTED |
67 | */ |
68 | function elapsed($start) { |
69 | |
70 | $end = microtime(); |
71 | list($start2, $start1) = explode(" ", $start); |
72 | list($end2, $end1) = explode(" ", $end); |
73 | $diff1 = $end1 - $start1; |
74 | $diff2 = $end2 - $start2; |
75 | if( $diff2 < 0 ){ |
76 | $diff1 -= 1; |
77 | $diff2 += 1.0; |
78 | } |
79 | return $diff2 + $diff1; |
098ea084 |
80 | } |
81 | |
c7df3f1b |
82 | /** |
8cc8ec79 |
83 | * Displays message header row in messages list |
0fdc2fb6 |
84 | * |
8cc8ec79 |
85 | * @param array $aMsg contains all message related parameters |
86 | * @return void |
0fdc2fb6 |
87 | */ |
88 | |
8cc8ec79 |
89 | function printMessageInfo($aMsg) { |
e0e30169 |
90 | // FIX ME, remove these globals as well by adding an array as argument for the user settings |
91 | // specificly meant for header display |
8008456a |
92 | global $checkall, |
e0e30169 |
93 | $color, |
0fdc2fb6 |
94 | $default_use_priority, |
95 | $message_highlight_list, |
96 | $index_order, |
494607f5 |
97 | $truncate_sender, /* number of characters for From/To field (<= 0 for unchanged) */ |
0fdc2fb6 |
98 | $email_address, |
494607f5 |
99 | $show_recipient_instead, /* show recipient name instead of default identity */ |
100 | $use_icons, /* indicates to use icons or text markers */ |
101 | $icon_theme; /* icons theming */ |
459e3347 |
102 | |
e9e5f0fa |
103 | $color_string = $color[4]; |
8008456a |
104 | |
03975a39 |
105 | // initialisation: |
8cc8ec79 |
106 | $mailbox = $aMsg['MAILBOX']; |
107 | $msg = $aMsg['HEADER']; |
108 | $t = $aMsg['INDX']; |
109 | $start_msg = $aMsg['PAGEOFFSET']; |
110 | $last = $aMsg['LAST']; |
111 | if (isset($aMsg['SEARCH']) && count($aMsg['SEARCH']) >1 ) { |
112 | $where = $aMsg['SEARCH'][0]; |
113 | $what = $aMsg['SEARCH'][1]; |
114 | } else { |
115 | $where = false; |
116 | $what = false; |
117 | } |
e0e30169 |
118 | $iIndent = $aMsg['INDENT']; |
8cc8ec79 |
119 | |
120 | $sSubject = (isset($msg['SUBJECT']) && $msg['SUBJECT'] != '') ? $msg['SUBJECT'] : _("(no subject)"); |
121 | $sFrom = (isset($msg['FROM'])) ? $msg['FROM'] : _("Unknown sender"); |
122 | $sTo = (isset($msg['TO'])) ? $msg['TO'] : _("Unknown recipient"); |
123 | $sCc = (isset($msg['CC'])) ? $msg['CC'] : ''; |
124 | $aFlags = (isset($msg['FLAGS'])) ? $msg['FLAGS'] : array(); |
125 | $iPrio = (isset($msg['PRIORITY'])) ? $msg['PRIORITY'] : 3; |
126 | $iSize = (isset($msg['SIZE'])) ? $msg['SIZE'] : 0; |
127 | $sType0 = (isset($msg['TYPE0'])) ? $msg['TYPE0'] : 'text'; |
128 | $sType1 = (isset($msg['TYPE1'])) ? $msg['TYPE1'] : 'plain'; |
e0e30169 |
129 | if (isset($msg['INTERNALDATE'])) { |
130 | $sDate = getDateString(getTimeStamp(explode(' ',$msg['INTERNALDATE']))); |
131 | } else { |
132 | $sDate = (isset($msg['DATE'])) ? getDateString(getTimeStamp(explode(' ',$msg['DATE']))) : ''; |
133 | } |
324ac3c5 |
134 | $iId = (isset($msg['UID'])) ? $msg['UID'] : false; |
8cc8ec79 |
135 | |
8cc8ec79 |
136 | if (!$iId) { |
137 | return; |
138 | } |
03975a39 |
139 | |
8008456a |
140 | if ($GLOBALS['alt_index_colors']) { |
e0e30169 |
141 | if (!($t % 2)) { |
8008456a |
142 | if (!isset($color[12])) { |
143 | $color[12] = '#EAEAEA'; |
144 | } |
145 | $color_string = $color[12]; |
146 | } |
bdfb67f8 |
147 | } |
8008456a |
148 | |
8008456a |
149 | $urlMailbox = urlencode($mailbox); |
e9e5f0fa |
150 | |
e0e30169 |
151 | // FIXME, foldertype should be set in right_main.php |
152 | // in other words, handle as sent is obsoleted from now. |
153 | // We replace that by providing an array to aMailbox with the to shown headers |
154 | // that way we are free to show the user different layouts for different folders |
53ad5f60 |
155 | $bSentFolder = handleAsSent($mailbox); |
1ae2832e |
156 | if ((!$bSentFolder) && ($show_recipient_instead)) { |
157 | // If the From address is the same as $email_address, then handle as Sent |
03975a39 |
158 | $from_array = parseAddress($sFrom, 1); |
1ae2832e |
159 | if (!isset($email_address)) { |
160 | global $datadir, $username; |
161 | $email_address = getPref($datadir, $username, 'email_address'); |
162 | } |
163 | $bHandleAsSent = ((isset($from_array[0][0])) && ($from_array[0][0] == $email_address)); |
8cc8ec79 |
164 | } else { |
1ae2832e |
165 | $bHandleAsSent = $bSentFolder; |
8cc8ec79 |
166 | } |
53ad5f60 |
167 | // If this is a Sent message, display To address instead of From |
03975a39 |
168 | if ($bHandleAsSent) { |
169 | $sFrom = $sTo; |
170 | } |
53ad5f60 |
171 | // Passing 1 below results in only 1 address being parsed, thus defeating the following code |
03975a39 |
172 | $sFrom = parseAddress($sFrom/*,1*/); |
9701e9c8 |
173 | |
0fdc2fb6 |
174 | /* |
e9e5f0fa |
175 | * This is done in case you're looking into Sent folders, |
176 | * because you can have multiple receivers. |
177 | */ |
03975a39 |
178 | $senderNames = $sFrom; |
098ea084 |
179 | $senderName = ''; |
26454147 |
180 | $senderAddress = ''; |
098ea084 |
181 | if (sizeof($senderNames)){ |
182 | foreach ($senderNames as $senderNames_part) { |
e9e5f0fa |
183 | if ($senderName != '') { |
184 | $senderName .= ', '; |
26454147 |
185 | $senderAddress .= ', '; |
e9e5f0fa |
186 | } |
26454147 |
187 | $sender_address_part = htmlspecialchars($senderNames_part[0]); |
53ad5f60 |
188 | $sender_name_part = str_replace(' ',' ', decodeHeader($senderNames_part[1])); |
26454147 |
189 | if ($sender_name_part) { |
190 | $senderName .= $sender_name_part; |
191 | $senderAddress .= $sender_name_part . ' <' . $sender_address_part . '>'; |
098ea084 |
192 | } else { |
26454147 |
193 | $senderName .= $sender_address_part; |
194 | $senderAddress .= $sender_address_part; |
098ea084 |
195 | } |
196 | } |
937c81fa |
197 | } |
53ad5f60 |
198 | // If Sent, prefix with To: but only if not Sent folder |
199 | if ($bHandleAsSent ^ $bSentFolder) { |
200 | $senderName = _("To:") . ' ' . $senderName; |
201 | $senderAddress = _("To:") . ' ' . $senderAddress; |
202 | } |
459e3347 |
203 | |
e0e30169 |
204 | // this is a column property which can apply to multiple columns. Do not use vars for one column |
205 | // only. instead we should use something like this: |
206 | // 1ed column $aMailbox['columns']['SUBJECT'] value: aray with properties ... |
207 | // 2ed column $aMailbox['columns']['FROM'] value: aray with properties ... |
208 | // NB in case of the sentfolder this could be the TO field |
209 | // properties array example: |
210 | // 'truncate' => length (0 is no truncate) |
211 | // 'prefix => if (x in b then do that ) |
8cc8ec79 |
212 | if ($truncate_sender > 0) { |
213 | $senderName = truncateWithEntities($senderName, $truncate_sender); |
bdfb67f8 |
214 | } |
8cc8ec79 |
215 | |
216 | $flag = $flag_end = $bold = $bold_end = $fontstr = $fontstr_end = $italic = $italic_end = ''; |
217 | $bold = '<b>'; |
218 | $bold_end = '</b>'; |
219 | |
8cc8ec79 |
220 | foreach ($aFlags as $sFlag => $value) { |
221 | switch ($sFlag) { |
222 | case '\\flagged': |
223 | if ($value) { |
224 | $flag = "<font color=\"$color[2]\">"; |
225 | $flag_end = '</font>'; |
226 | } |
227 | break; |
228 | case '\\seen': |
229 | if ($value) { |
230 | $bold = ''; |
231 | $bold_end = ''; |
232 | } |
233 | break; |
234 | case '\\deleted': |
235 | if ($value) { |
236 | $fontstr = "<font color=\"$color[9]\">"; |
237 | $fontstr_end = '</font>'; |
238 | } |
239 | break; |
240 | } |
bdfb67f8 |
241 | } |
53ad5f60 |
242 | if ($bHandleAsSent) { |
8008456a |
243 | $italic = '<i>'; |
244 | $italic_end = '</i>'; |
8008456a |
245 | } |
246 | |
d215ca7d |
247 | if ($where && $what) { |
248 | $searchstr = '&where='.$where.'&what='.$what; |
249 | } else { |
250 | $searchstr = ''; |
6a622b59 |
251 | } |
8cc8ec79 |
252 | /* |
253 | * Message highlight code |
254 | */ |
3667f469 |
255 | $matches = array('TO' => 'sTo', 'CC' => 'sCc', 'FROM' => 'sFrom', 'SUBJECT' => 'sSubject'); |
937c81fa |
256 | if (is_array($message_highlight_list) && count($message_highlight_list)) { |
03975a39 |
257 | $sTo = parseAddress($sTo); |
258 | $sCc = parseAddress($sCc); |
8008456a |
259 | foreach ($message_highlight_list as $message_highlight_list_part) { |
260 | if (trim($message_highlight_list_part['value']) != '') { |
c0d04f97 |
261 | $high_val = strtolower($message_highlight_list_part['value']); |
262 | $match_type = strtoupper($message_highlight_list_part['match_type']); |
8415b2d3 |
263 | if($match_type == 'TO_CC') { |
264 | $match = array('TO', 'CC'); |
265 | } else { |
266 | $match = array($match_type); |
267 | } |
268 | foreach($match as $match_type) { |
269 | switch($match_type) { |
270 | case('TO'): |
271 | case('CC'): |
272 | case('FROM'): |
3667f469 |
273 | foreach ($$matches[$match_type] as $address) { |
9701e9c8 |
274 | $address[0] = decodeHeader($address[0], true, false); |
275 | $address[1] = decodeHeader($address[1], true, false); |
8415b2d3 |
276 | if (strstr('^^' . strtolower($address[0]), $high_val) || |
277 | strstr('^^' . strtolower($address[1]), $high_val)) { |
278 | $hlt_color = $message_highlight_list_part['color']; |
279 | break 4; |
280 | } |
937c81fa |
281 | } |
937c81fa |
282 | break; |
8415b2d3 |
283 | default: |
3667f469 |
284 | $headertest = strtolower(decodeHeader($$matches[$match_type], true, false)); |
9701e9c8 |
285 | if (strstr('^^' . $headertest, $high_val)) { |
937c81fa |
286 | $hlt_color = $message_highlight_list_part['color']; |
4669e892 |
287 | break 3; |
937c81fa |
288 | } |
8415b2d3 |
289 | break; |
290 | } |
8008456a |
291 | } |
292 | } |
293 | } |
8cc8ec79 |
294 | } /* end Message highlight code */ |
8008456a |
295 | |
296 | if (!isset($hlt_color)) { |
297 | $hlt_color = $color_string; |
298 | } |
5e0efebf |
299 | $col = 0; |
03975a39 |
300 | $sSubject = str_replace(' ', ' ', decodeHeader($sSubject)); |
e0e30169 |
301 | $subject = processSubject($sSubject, $iIndent); |
8cc8ec79 |
302 | |
303 | echo html_tag( 'tr','','','','VALIGN="top"') . "\n"; |
304 | |
6a622b59 |
305 | if (sizeof($index_order)) { |
8008456a |
306 | foreach ($index_order as $index_order_part) { |
307 | switch ($index_order_part) { |
308 | case 1: /* checkbox */ |
309 | echo html_tag( 'td', |
03975a39 |
310 | addCheckBox("msg[$t]", $checkall, $iId), |
0fdc2fb6 |
311 | 'center', |
312 | $hlt_color ); |
8008456a |
313 | break; |
314 | case 2: /* from */ |
26454147 |
315 | if ($senderAddress != $senderName) { |
316 | $senderAddress = strtr($senderAddress, array_flip(get_html_translation_table(HTML_SPECIALCHARS))); |
317 | $title = ' title="' . str_replace('"', "''", $senderAddress) . '"'; |
8cc8ec79 |
318 | } else { |
26454147 |
319 | $title = ''; |
8cc8ec79 |
320 | } |
8008456a |
321 | echo html_tag( 'td', |
0fdc2fb6 |
322 | $italic . $bold . $flag . $fontstr . $senderName . |
323 | $fontstr_end . $flag_end . $bold_end . $italic_end, |
324 | 'left', |
325 | $hlt_color, $title ); |
8008456a |
326 | break; |
327 | case 3: /* date */ |
03975a39 |
328 | if ($sDate == '') { |
329 | $sDate = _("Unknown date"); |
53d9fbc5 |
330 | } |
8008456a |
331 | echo html_tag( 'td', |
03975a39 |
332 | $bold . $flag . $fontstr . $sDate . |
0fdc2fb6 |
333 | $fontstr_end . $flag_end . $bold_end, |
334 | 'center', |
335 | $hlt_color, |
336 | 'nowrap' ); |
8008456a |
337 | break; |
338 | case 4: /* subject */ |
339 | $td_str = $bold; |
e0e30169 |
340 | if ($iIndent) { |
341 | $td_str .= str_repeat(" ",$iIndent); |
8008456a |
342 | } |
e9e5f0fa |
343 | $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox |
324ac3c5 |
344 | . '&passed_id='. $iId |
6a622b59 |
345 | . '&startMessage='.$start_msg.$searchstr.'"'; |
158fa340 |
346 | $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr)); |
03975a39 |
347 | if ($subject != $sSubject) { |
8008456a |
348 | $title = get_html_translation_table(HTML_SPECIALCHARS); |
349 | $title = array_flip($title); |
03975a39 |
350 | $title = strtr($sSubject, $title); |
8008456a |
351 | $title = str_replace('"', "''", $title); |
352 | $td_str .= " title=\"$title\""; |
353 | } |
354 | $td_str .= ">$flag$subject$flag_end</a>$bold_end"; |
c1c17724 |
355 | echo html_tag( 'td', $td_str, 'left', $hlt_color ); |
8008456a |
356 | break; |
357 | case 5: /* flags */ |
6a622b59 |
358 | |
6395c46d |
359 | // icon message markers |
360 | // |
361 | if ($use_icons && $icon_theme != 'none') { |
362 | $td_str = "<b><small>"; |
03975a39 |
363 | if (isset($aFlags['\\flagged']) && $aFlags['\\flagged'] == true) { |
55103ede |
364 | $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/flagged.png" border="0" height="10" width="10" /> '; |
6395c46d |
365 | } |
366 | if ($default_use_priority) { |
03975a39 |
367 | if ( ($iPrio == 1) || ($iPrio == 2) ) { |
55103ede |
368 | $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/prio_high.png" border="0" height="10" width="5" /> '; |
6395c46d |
369 | } |
03975a39 |
370 | else if ($iPrio == 5) { |
55103ede |
371 | $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/prio_low.png" border="0" height="10" width="5" /> '; |
6395c46d |
372 | } |
0fdc2fb6 |
373 | else { |
55103ede |
374 | $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/transparent.png" border="0" width="5" /> '; |
6395c46d |
375 | } |
376 | } |
03975a39 |
377 | if ($sType1 == 'mixed') { |
55103ede |
378 | $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/attach.png" border="0" height="10" width="6" />'; |
0fdc2fb6 |
379 | } else { |
55103ede |
380 | $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/transparent.png" border="0" width="6" />'; |
6395c46d |
381 | } |
382 | |
383 | $msg_icon = ''; |
03975a39 |
384 | if (!isset($aFlags['\\seen']) || ($aFlags['\\seen']) == false) { |
6395c46d |
385 | $msg_alt = '(' . _("New") . ')'; |
386 | $msg_title = '(' . _("New") . ')'; |
387 | $msg_icon .= SM_PATH . 'images/themes/' . $icon_theme . '/msg_new'; |
0fdc2fb6 |
388 | } else { |
6395c46d |
389 | $msg_alt = '(' . _("Read") . ')'; |
390 | $msg_title = '(' . _("Read") . ')'; |
391 | $msg_icon .= SM_PATH . 'images/themes/' . $icon_theme . '/msg_read'; |
392 | } |
03975a39 |
393 | if (isset($aFlags['\\deleted']) && ($aFlags['\\deleted']) == true) { |
6395c46d |
394 | $msg_icon .= '_deleted'; |
395 | } |
03975a39 |
396 | if (isset($aFlags['\\answered']) && ($aFlags['\\answered']) == true) { |
d3685401 |
397 | $msg_alt = '(' . _("Answered") . ')'; |
398 | $msg_title = '(' . _("Answered") . ')'; |
6395c46d |
399 | $msg_icon .= '_reply'; |
400 | } |
55103ede |
401 | $td_str .= '<img src="' . $msg_icon . '.png" border="0" alt="'. $msg_alt . '" title="' . $msg_title . '" height="12" width="18" />'; |
6395c46d |
402 | $td_str .= '</small></b>'; |
403 | echo html_tag( 'td', |
0fdc2fb6 |
404 | $td_str, |
405 | 'right', |
406 | $hlt_color, |
407 | 'nowrap' ); |
8008456a |
408 | } |
6395c46d |
409 | |
6395c46d |
410 | // plain text message markers |
411 | // |
412 | else { |
413 | $stuff = false; |
414 | $td_str = "<b><small>"; |
03975a39 |
415 | if (isset($aFlags['\\answered']) && $aFlags['\\answered'] == true) { |
6395c46d |
416 | $td_str .= _("A"); |
8008456a |
417 | $stuff = true; |
418 | } |
03975a39 |
419 | if ($sType1 == 'mixed') { |
6395c46d |
420 | $td_str .= '+'; |
8008456a |
421 | $stuff = true; |
422 | } |
6395c46d |
423 | if ($default_use_priority) { |
03975a39 |
424 | if ( ($iPrio == 1) || ($iPrio == 2) ) { |
6395c46d |
425 | $td_str .= "<font color=\"$color[1]\">!</font>"; |
426 | $stuff = true; |
427 | } |
03975a39 |
428 | if ($iPrio == 5) { |
6395c46d |
429 | $td_str .= "<font color=\"$color[8]\">?</font>"; |
430 | $stuff = true; |
431 | } |
432 | } |
03975a39 |
433 | if (isset($aFlags['\\deleted']) && $aFlags['\\deleted'] == true) { |
6395c46d |
434 | $td_str .= "<font color=\"$color[1]\">D</font>"; |
435 | $stuff = true; |
436 | } |
437 | if (!$stuff) { |
438 | $td_str .= ' '; |
439 | } |
440 | $td_str .= '</small></b>'; |
441 | echo html_tag( 'td', |
0fdc2fb6 |
442 | $td_str, |
443 | 'center', |
444 | $hlt_color, |
445 | 'nowrap' ); |
8008456a |
446 | } |
8008456a |
447 | break; |
448 | case 6: /* size */ |
8008456a |
449 | echo html_tag( 'td', |
03975a39 |
450 | $bold . $fontstr . show_readable_size($iSize) . |
0fdc2fb6 |
451 | $fontstr_end . $bold_end, |
452 | 'right', |
453 | $hlt_color ); |
8008456a |
454 | break; |
455 | } |
ed230bc3 |
456 | ++$col; |
8008456a |
457 | } |
bdfb67f8 |
458 | } |
8cc8ec79 |
459 | /* html for separationlines between rows */ |
1c198ef7 |
460 | if ($last) { |
461 | echo '</tr>'."\n"; |
462 | } else { |
55103ede |
463 | echo '</tr>' . "\n" . '<tr><td colspan="' . $col . '" bgcolor="' . |
0fdc2fb6 |
464 | $color[0] . '" height="1"></td></tr>' . "\n"; |
6a622b59 |
465 | } |
e9e5f0fa |
466 | } |
467 | |
324ac3c5 |
468 | |
469 | function setUserPref($username, $pref, $value) { |
470 | global $data_dir; |
471 | setPref($data_dir,$username,$pref,$value); |
472 | } |
473 | |
474 | /** |
475 | * Selects a mailbox for header retrieval. |
476 | * Cache control for message headers is embedded. |
477 | * |
478 | * @param resource $imapConnection imap socket handle |
479 | * @param string $mailbox mailbox to select and retrieve message headers from |
480 | * @param array $aConfig array with system config settings and incoming vars |
481 | * @param array $aProps mailbox specific properties |
482 | * @return array $aMailbox mailbox array with all relevant information |
483 | * @author Marc Groot Koerkamp |
484 | */ |
485 | function sqm_api_mailbox_select($imapConnection,$mailbox,$aConfig,$aProps) { |
486 | /** |
487 | * NB: retrieve this from the session before accessing this function |
488 | * and make sure you write it back at the end of the script after |
489 | * the aMailbox var is added so that the headers are added to the cache |
490 | */ |
491 | global $mailbox_cache; |
492 | /** |
493 | * In case the properties arrays are empty set the defaults. |
494 | */ |
495 | $aDefaultMbxPref = array (); |
496 | // MBX_PREF_SORT => 0, |
497 | // MBX_PREF_LIMIT => 15, |
498 | // MBX_PREF_AUTO_EXPUNGE => 0, |
499 | // MBX_PREF_INTERNALDATE => 0 |
500 | // ); |
501 | /* array_merge doesn't work with integers as keys */ |
502 | // foreach ($aDefaultMbxPref as $key => $value) { |
503 | // if (!isset($aProps[$key])) { |
504 | // $aProps[$key] = $value; |
505 | // } |
506 | // } |
507 | $aDefaultConfigProps = array( |
508 | // 'allow_thread_sort' => 0, |
509 | 'allow_server_sort' => sqimap_capability($imapConnection,'SORT'), |
510 | // 'charset' => 'US-ASCII', |
511 | 'user' => false, /* no pref storage if false */ |
512 | 'setindex' => 0, |
513 | // 'search' => 'ALL', |
514 | 'max_cache_size' => SQM_MAX_MBX_IN_CACHE |
515 | ); |
516 | |
517 | $aConfig = array_merge($aDefaultConfigProps,$aConfig); |
518 | $iSetIndx = $aConfig['setindex']; |
519 | |
520 | $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox); |
521 | |
522 | if ($mailbox_cache) { |
523 | if (isset($mailbox_cache[$mailbox])) { |
524 | $aCachedMailbox = $mailbox_cache[$mailbox]; |
525 | } else { |
526 | $aCachedMailbox = false; |
527 | } |
528 | /* cleanup cache */ |
529 | if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) { |
530 | $aTime = array(); |
531 | foreach($mailbox_cache as $cachedmailbox => $aVal) { |
532 | $aTime[$aVal['TIMESTAMP']] = $cachedmailbox; |
533 | } |
534 | if (ksort($aTime,SORT_NUMERIC)) { |
535 | for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++$i) { |
536 | $sOldestMbx = array_shift($aTime); |
537 | /** |
538 | * Remove only the UIDSET and MSG_HEADERS from cache because those can |
539 | * contain large amounts of data. |
540 | */ |
541 | if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) { |
542 | $mailbox_cache[$sOldestMbx]['UIDSET']= false; |
543 | } |
544 | if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) { |
545 | $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false; |
546 | } |
547 | } |
548 | } |
549 | } |
550 | |
551 | } else { |
552 | $aCachedMailbox = false; |
553 | } |
554 | |
555 | /** |
556 | * Deal with imap servers that do not return the required UIDNEXT or |
557 | * UIDVALIDITY response |
558 | * from a SELECT call (since rfc 3501 it's required). |
559 | */ |
560 | if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) { |
561 | $aStatus = sqimap_status_messages($imapConnection,$mailbox, |
562 | array('UIDNEXT','UIDVALIDITY')); |
563 | $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT']; |
564 | $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY']; |
565 | } |
566 | |
567 | $aMailbox['UIDSET'][$iSetIndx] = false; |
568 | $aMailbox['ID'] = false; |
569 | $aMailbox['SETINDEX'] = $iSetIndx; |
570 | |
571 | if ($aCachedMailbox) { |
572 | /** |
573 | * Validate integrity of cached data |
574 | */ |
575 | if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] && |
576 | $aMbxResponse['EXISTS'] && |
577 | $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] && |
578 | $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] && |
579 | isset($aCachedMailbox['SEARCH'][$iSetIndx]) && |
580 | (!isset($aConfig['search']) || /* always set search from the searchpage */ |
581 | $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) { |
582 | if (isset($aCachedMailbox['MSG_HEADERS'])) { |
583 | $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS']; |
584 | } |
585 | $aMailbox['ID'] = $aCachedMailbox['ID']; |
586 | if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) { |
587 | if (isset($aProps[MBX_PREF_SORT]) && $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT'] ) { |
588 | $newsort = $aProps[MBX_PREF_SORT]; |
589 | $oldsort = $aCachedMailbox['SORT']; |
590 | /** |
591 | * If it concerns a reverse sort we do not need to invalidate |
592 | * the cached sorted UIDSET, a reverse is sufficient. |
593 | */ |
594 | if ((($newsort % 2) && ($newsort + 1 == $oldsort)) || |
595 | (!($newsort % 2) && ($newsort - 1 == $oldsort))) { |
596 | $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]); |
597 | } else { |
598 | $server_sort_array = false; |
599 | $aMailbox['MSG_HEADERS'] = false; |
600 | $aMailbox['ID'] = false; |
601 | } |
602 | // store the new sort value in the mailbox pref |
603 | if ($aConfig['user']) { |
604 | // FIXME, in ideal situation, we write back the |
605 | // prefs at the end of the script |
606 | setUserPref($aConfig['user'],"pref_$mailbox",serialize($aProps)); |
607 | } |
608 | } else { |
609 | $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx]; |
610 | } |
611 | } |
612 | } |
613 | } |
614 | /** |
615 | * Restore the offset in the paginator if no new offset is provided. |
616 | */ |
617 | if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) { |
618 | $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET']; |
619 | $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET']; |
620 | } else { |
621 | $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] -1 : 0; |
622 | $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] : 1; |
623 | } |
624 | |
625 | /** |
626 | * Restore the showall value no new showall value is provided. |
627 | */ |
628 | if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) && |
629 | isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) { |
630 | $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx]; |
631 | } else { |
632 | $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ? 1 : 0; |
633 | } |
634 | |
635 | if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) { |
636 | $aMailbox['SORT'] = $aCachedMailbox['SORT']; |
637 | } else { |
638 | $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT])) ? $aProps[MBX_PREF_SORT] : 0; |
639 | } |
640 | |
641 | if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) { |
642 | $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT']; |
643 | } else { |
644 | $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT])) ? $aProps[MBX_PREF_LIMIT] : 15; |
645 | } |
646 | |
647 | if (!isset($aProps[MBX_PREF_INTERNALDATE]) && isset($aCachedMailbox['INTERNALDATE'])) { |
648 | $aMailbox['INTERNALDATE'] = $aCachedMailbox['INTERNALDATE']; |
649 | } else { |
650 | $aMailbox['INTERNALDATE'] = (isset($aProps[MBX_PREF_INTERNALDATE])) ? $aProps[MBX_PREF_INTERNALDATE] : false; |
651 | } |
652 | |
653 | if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) { |
654 | $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE']; |
655 | } else { |
656 | $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE])) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false; |
657 | } |
658 | |
659 | if (!isset($aConfig['allow_thread_sort']) && isset($aCachedMailbox['ALLOW_THREAD'])) { |
660 | $aMailbox['ALLOW_THREAD'] = $aCachedMailbox['ALLOW_THREAD']; |
661 | } else { |
662 | $aMailbox['ALLOW_THREAD'] = (isset($aConfig['allow_thread_sort'])) ? $aConfig['allow_thread_sort'] : false; |
663 | } |
664 | |
665 | if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) { |
666 | $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx]; |
667 | } else { |
668 | $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ? $aConfig['search'] : 'ALL'; |
669 | } |
670 | |
671 | if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) { |
672 | $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx]; |
673 | } else { |
674 | $aMailbox['CHARSET'][$iSetIndx] = (isset($aConfig['charset'])) ? $aConfig['charset'] : 'US-ASCII'; |
675 | } |
676 | |
677 | $aMailbox['NAME'] = $mailbox; |
678 | $aMailbox['EXISTS'] = $aMbxResponse['EXISTS']; |
679 | $aMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS']; |
680 | $aMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ? $aMbxResponse['RECENT'] : 0; |
681 | $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY']; |
682 | $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT']; |
683 | $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS']; |
684 | $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS']; |
685 | |
686 | |
687 | |
688 | /* decide if we are thread sorting or not */ |
689 | if (!$aMailbox['ALLOW_THREAD']) { |
690 | if ($aMailbox['SORT'] & SQSORT_THREAD) { |
691 | $aMailbox['SORT'] -= SQSORT_THREAD; |
692 | } |
693 | } |
694 | if ($aMailbox['SORT'] & SQSORT_THREAD) { |
695 | $aMailbox['SORT_METHOD'] = 'THREAD'; |
696 | $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT']; |
697 | } else if (isset($aConfig['allow_server_sort']) && $aConfig['allow_server_sort']) { |
698 | $aMailbox['SORT_METHOD'] = 'SERVER'; |
699 | $aMailbox['THREAD_INDENT'] = false; |
700 | } else { |
701 | $aMailbox['SORT_METHOD'] = 'SQUIRREL'; |
702 | $aMailbox['THREAD_INDENT'] = false; |
703 | } |
704 | |
705 | /* set a timestamp for cachecontrol */ |
706 | $aMailbox['TIMESTAMP'] = time(); |
707 | return $aMailbox; |
708 | } |
709 | |
710 | |
711 | |
c7df3f1b |
712 | /** |
324ac3c5 |
713 | * Does the $srt $_GET var to field mapping |
0fdc2fb6 |
714 | * |
324ac3c5 |
715 | * @param int $srt Field to sort on |
03975a39 |
716 | * @param bool $bServerSort Server sorting is true |
324ac3c5 |
717 | * @return string $sSortField Field to sort on |
0fdc2fb6 |
718 | */ |
03975a39 |
719 | function getSortField($sort,$bServerSort) { |
720 | switch($sort) { |
721 | case SQSORT_NONE: |
722 | $sSortField = 'UID'; |
723 | break; |
724 | case SQSORT_DATE_ASC: |
725 | case SQSORT_DATE_DEC: |
726 | $sSortField = 'DATE'; |
727 | break; |
728 | case SQSORT_FROM_ASC: |
729 | case SQSORT_FROM_DEC: |
730 | $sSortField = 'FROM'; |
731 | break; |
732 | case SQSORT_SUBJ_ASC: |
733 | case SQSORT_SUBJ_DEC: |
734 | $sSortField = 'SUBJECT'; |
735 | break; |
736 | case SQSORT_SIZE_ASC: |
737 | case SQSORT_SIZE_DEC: |
738 | $sSortField = ($bServerSort) ? 'SIZE' : 'RFC822.SIZE'; |
739 | break; |
740 | case SQSORT_TO_ASC: |
741 | case SQSORT_TO_DEC: |
742 | $sSortField = 'TO'; |
743 | break; |
744 | case SQSORT_CC_ASC: |
745 | case SQSORT_CC_DEC: |
746 | $sSortField = 'CC'; |
747 | break; |
748 | case SQSORT_INT_DATE_ASC: |
749 | case SQSORT_INT_DATE_DEC: |
750 | $sSortField = ($bServerSort) ? 'ARRIVAL' : 'INTERNALDATE'; |
751 | break; |
e0e30169 |
752 | case SQSORT_THREAD: |
03975a39 |
753 | break; |
e0e30169 |
754 | default: $sSortField = 'UID'; |
755 | break; |
756 | |
fe6efa94 |
757 | } |
03975a39 |
758 | return $sSortField; |
e9e5f0fa |
759 | } |
760 | |
324ac3c5 |
761 | function get_sorted_msgs_list($imapConnection,&$aMailbox,&$error) { |
762 | $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0; |
e0e30169 |
763 | $bDirection = ($aMailbox['SORT'] % 2); |
03975a39 |
764 | $error = false; |
324ac3c5 |
765 | if (!$aMailbox['SEARCH'][$iSetIndx]) { |
766 | $aMailbox['SEARCH'][$iSetIndx] = 'ALL'; |
767 | } |
e0e30169 |
768 | switch ($aMailbox['SORT_METHOD']) { |
8cc8ec79 |
769 | case 'THREAD': |
324ac3c5 |
770 | $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]); |
771 | if ($aRes === false) { |
03975a39 |
772 | $error = '<b><small><center><font color=red>' . |
773 | _("Thread sorting is not supported by your IMAP server.") . '<br />' . |
774 | _("Please report this to the system administrator."). |
775 | '</center></small></b>'; |
324ac3c5 |
776 | $aMailbox['SORT'] -= SQSORT_THREAD; |
777 | } else { |
778 | $aMailbox['UIDSET'][$iSetIndx] = $aRes[0]; |
779 | $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1]; |
03975a39 |
780 | } |
781 | break; |
8cc8ec79 |
782 | case 'SERVER': |
e0e30169 |
783 | $sSortField = getSortField($aMailbox['SORT'],true); |
324ac3c5 |
784 | $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]); |
03975a39 |
785 | if ($id === false) { |
786 | $error = '<b><small><center><font color=red>' . |
787 | _( "Server-side sorting is not supported by your IMAP server.") . '<br />' . |
788 | _("Please report this to the system administrator."). |
789 | '</center></small></b>'; |
324ac3c5 |
790 | } else { |
791 | $aMailbox['UIDSET'][$iSetIndx] = $id; |
83cdc70b |
792 | } |
03975a39 |
793 | break; |
794 | default: |
324ac3c5 |
795 | $id = NULL; |
796 | if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { |
797 | $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]); |
798 | } |
e0e30169 |
799 | $sSortField = getSortField($aMailbox['SORT'],false); |
324ac3c5 |
800 | $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id); |
03975a39 |
801 | break; |
e9e5f0fa |
802 | } |
324ac3c5 |
803 | return $error; |
e9e5f0fa |
804 | } |
805 | |
8cc8ec79 |
806 | |
8cc8ec79 |
807 | |
324ac3c5 |
808 | |
809 | function fetchMessageHeaders($imapConnection, &$aMailbox) { |
810 | |
811 | /** |
812 | * Retrieve the UIDSET. |
813 | * Setindex is used to be able to store multiple uid sets. That will make it |
814 | * possible to display the mailbox multiple times in different sort order |
815 | * or to store serach results separate from normal mailbox view. |
816 | */ |
817 | $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0; |
818 | |
819 | $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT']; |
820 | /** |
821 | * Adjust the start_msg |
822 | */ |
823 | $start_msg = $aMailbox['PAGEOFFSET']; |
824 | if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) { |
825 | $start_msg -= $aMailbox['LIMIT']; |
826 | if($start_msg < 1) { |
827 | $start_msg = 1; |
828 | } |
829 | } |
830 | |
831 | |
832 | if (is_array($aMailbox['UIDSET'])) { |
833 | $aUid =& $aMailbox['UIDSET'][$iSetIndx]; |
f6b262a3 |
834 | } else { |
324ac3c5 |
835 | $aUid = false; |
836 | } |
837 | |
838 | // initialize the fields we want to retrieve: |
839 | $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type'); |
840 | $aFetchItems = array('FLAGS', 'RFC822.SIZE'); |
841 | |
842 | // Are we sorting on internaldate then retrieve the internaldate value as well |
843 | if ($aMailbox['INTERNALDATE']) { |
844 | $aFetchItems[] = 'INTERNALDATE'; |
845 | } |
846 | |
847 | |
848 | /** |
849 | * A uidset with sorted uid's is available. We can use the cache |
850 | */ |
851 | if (($aMailbox['SORT'] != SQSORT_NONE || $aMailbox['SEARCH'][$iSetIndx] != 'ALL') && |
852 | isset($aUid) && $aUid ) { |
853 | |
854 | // limit the cache to SQM_MAX_PAGES_IN_CACHE |
855 | if (!$aMailbox['SHOWALL'][$iSetIndx]) { |
856 | $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE; |
857 | $iCacheSize = count($aMailbox['MSG_HEADERS']); |
858 | if ($iCacheSize > $iMaxMsgs) { |
859 | $iReduce = $iCacheSize - $iMaxMsgs; |
860 | foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) { |
861 | if ($iReduce) { |
862 | unset($aMailbox['MSG_HEADERS'][$iUid]); |
863 | } else { |
864 | break; |
865 | } |
866 | --$iReduce; |
867 | } |
6a622b59 |
868 | } |
e9e5f0fa |
869 | } |
324ac3c5 |
870 | |
871 | $id_slice = array_slice($aUid,$start_msg-1,$iLimit); |
872 | /* do some funky cache checks */ |
873 | $aUidCached = array_keys($aMailbox['MSG_HEADERS']); |
874 | $aUidNotCached = array_values(array_diff($id_slice,$aUidCached)); |
875 | /** |
876 | * $aUidNotCached contains an array with UID's which need to be fetched to |
877 | * complete the needed message headers. |
878 | */ |
879 | if (count($aUidNotCached)) { |
880 | $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached, |
881 | $aHeaderFields,$aFetchItems); |
882 | // append the msgs to the existend headers |
883 | $aMailbox['MSG_HEADERS'] += $aMsgs; |
8cc8ec79 |
884 | } |
6a622b59 |
885 | |
324ac3c5 |
886 | } else { |
887 | /** |
888 | * Initialize the sorted UID list and fetch the visible message headers |
889 | */ |
890 | if ($aMailbox['SORT'] != SQSORT_NONE || $aMailbox['SEARCH'][$iSetIndx] != 'ALL') {// || $aMailbox['SORT_METHOD'] & SQSORT_THREAD 'THREAD') { |
891 | |
892 | $error = false; |
893 | if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) { |
894 | $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]); |
895 | } else { |
896 | $error = get_sorted_msgs_list($imapConnection,$aMailbox,$error); |
897 | $aUid = $aMailbox['UIDSET'][$iSetIndx]; |
03975a39 |
898 | } |
324ac3c5 |
899 | if ($error === false) { |
900 | $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit); |
901 | if (count($id_slice)) { |
902 | $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice, |
903 | $aHeaderFields,$aFetchItems); |
904 | } else { |
905 | return false; |
906 | } |
e0e30169 |
907 | |
03975a39 |
908 | } else { |
324ac3c5 |
909 | // FIX ME, format message and fallback to squirrel sort |
910 | if ($error) { |
911 | echo $error; |
912 | } |
03975a39 |
913 | } |
03975a39 |
914 | } else { |
324ac3c5 |
915 | // limit the cache to SQM_MAX_PAGES_IN_CACHE |
916 | if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) { |
917 | $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE; |
918 | $iCacheSize = count($aMailbox['MSG_HEADERS']); |
919 | if ($iCacheSize > $iMaxMsgs) { |
920 | $iReduce = $iCacheSize - $iMaxMsgs; |
921 | foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) { |
922 | if ($iReduce) { |
923 | $iId = $aMailbox['MSG_HEADERS'][$iUid]['ID']; |
924 | unset($aMailbox['MSG_HEADERS'][$iUid]); |
925 | unset($aMailbox['ID'][$iId]); |
926 | } else { |
927 | break; |
928 | } |
929 | --$iReduce; |
930 | } |
931 | } |
03975a39 |
932 | } |
e0e30169 |
933 | |
324ac3c5 |
934 | /** |
935 | * retrieve messages by sequence id's and fetch the UID to retrieve |
936 | * the UID. for sorted lists this is not needed because a UID FETCH |
937 | * automaticly add the UID value in fetch results |
938 | **/ |
939 | $aFetchItems[] = 'UID'; |
940 | |
941 | //create id range |
942 | $iRangeStart = $aMailbox['EXISTS'] - $aMailbox['OFFSET']; |
943 | $iRangeEnd = ($iRangeStart > $iLimit) ? |
944 | ($iRangeStart - $iLimit+1):1; |
945 | |
946 | $id_slice = range($iRangeStart, $iRangeEnd); |
947 | /** |
948 | * Non sorted mailbox with cached message headers |
949 | */ |
950 | if (isset($aMailbox['ID']) && is_array($aMailbox['ID'])) { |
951 | // the fetched id => uid relation |
952 | $aId = $aMailbox['ID']; |
953 | $aIdCached = array(); |
954 | foreach ($aId as $iId => $iUid) { |
955 | if (isset($aMailbox['MSG_HEADERS'][$iUid])) { |
956 | if ($iId <= $iRangeStart && $iId >= $iRangeEnd) { |
957 | $aIdCached[] = $iId; |
958 | } |
f6b262a3 |
959 | } |
6a622b59 |
960 | } |
324ac3c5 |
961 | $aIdNotCached = array_diff($id_slice,$aIdCached); |
03975a39 |
962 | } else { |
324ac3c5 |
963 | $aIdNotCached = $id_slice; |
8cc8ec79 |
964 | } |
324ac3c5 |
965 | |
966 | if (count($aIdNotCached)) { |
967 | $aMsgs = sqimap_get_small_header_list($imapConnection,$aIdNotCached, |
968 | $aHeaderFields,$aFetchItems); |
969 | // append the msgs to the existend headers |
970 | if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) { |
971 | $aMailbox['MSG_HEADERS'] += $aMsgs; |
972 | } else { |
973 | $aMailbox['MSG_HEADERS'] = $aMsgs; |
974 | } |
975 | // update the ID array |
976 | foreach ($aMsgs as $iUid => $aMsg) { |
977 | if (isset($aMsg['ID'])) { |
978 | $aMailbox['ID'][$aMsg['ID']] = $iUid; |
979 | } |
980 | } |
03975a39 |
981 | } |
7b73b809 |
982 | |
324ac3c5 |
983 | /** |
984 | * In unsorted state we show newest messages first which means |
985 | * that the UIDSET which represents the order of the messages |
986 | * should contain a high to low ordered UID list |
987 | */ |
988 | $aSortedUidList = array(); |
989 | foreach ($id_slice as $iId) { |
990 | if (isset($aMailbox['ID'][$iId])) { |
991 | $aSortedUidList[] = $aMailbox['ID'][$iId]; |
992 | } |
993 | } |
994 | $aMailbox['UIDSET'][$iSetIndx] = $aSortedUidList; |
995 | $aMailbox['OFFSET'] = 0; |
e0e30169 |
996 | } |
324ac3c5 |
997 | } |
998 | return true; |
999 | } |
6a622b59 |
1000 | |
324ac3c5 |
1001 | /** |
1002 | * This function loops through a group of messages in the mailbox |
1003 | * and shows them to the user. |
1004 | * |
1005 | * @param mixed $imapConnection |
1006 | * @param array $aMailbox associative array with mailbox related vars |
1007 | */ |
1008 | function showMessagesForMailbox($imapConnection, &$aMailbox) { |
1009 | global $color; |
1010 | |
1011 | // to retrieve the internaldate pref: (I know this is not the right place to do that, move up in front |
1012 | // and use a properties array as function argument to provide user preferences |
1013 | global $data_dir, $username; |
1014 | |
1015 | if (!fetchMessageHeaders($imapConnection, $aMailbox)) { |
1016 | return false; |
1017 | } |
1018 | $iSetIndx = $aMailbox['SETINDEX']; |
1019 | $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT']; |
1020 | $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ? |
1021 | $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS']; |
6a622b59 |
1022 | |
324ac3c5 |
1023 | $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'], |
1024 | $aMailbox['EXISTS'], $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]); |
6a622b59 |
1025 | |
e0e30169 |
1026 | $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$aMailbox['EXISTS']); |
6a622b59 |
1027 | |
1028 | do_hook('mailbox_index_before'); |
abafb676 |
1029 | ?> |
1030 | <table border="0" width="100%" cellpadding="0" cellspacing="0"> |
0fdc2fb6 |
1031 | <tr> |
abafb676 |
1032 | <td> |
8cc8ec79 |
1033 | <?php mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $paginator_str); ?> |
abafb676 |
1034 | </td> |
0fdc2fb6 |
1035 | </tr> |
1036 | <tr><td height="5" bgcolor="<?php echo $color[4]; ?>"></td></tr> |
1037 | <tr> |
abafb676 |
1038 | <td> |
0fdc2fb6 |
1039 | <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[9]; ?>"> |
abafb676 |
1040 | <tr> |
0fdc2fb6 |
1041 | <td> |
abafb676 |
1042 | <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[5]; ?>"> |
0fdc2fb6 |
1043 | <tr> |
abafb676 |
1044 | <td> |
0fdc2fb6 |
1045 | <?php |
8cc8ec79 |
1046 | printHeader($aMailbox); |
1047 | displayMessageArray($imapConnection, $aMailbox); |
0fdc2fb6 |
1048 | ?> |
abafb676 |
1049 | </td> |
0fdc2fb6 |
1050 | </tr> |
abafb676 |
1051 | </table> |
0fdc2fb6 |
1052 | </td> |
abafb676 |
1053 | </tr> |
0fdc2fb6 |
1054 | </table> |
1055 | <?php |
8cc8ec79 |
1056 | mail_message_listing_end($aMailbox['EXISTS'], $paginator_str, $msg_cnt_str); |
0fdc2fb6 |
1057 | ?> |
abafb676 |
1058 | </td> |
0fdc2fb6 |
1059 | </tr> |
abafb676 |
1060 | </table> |
1061 | <?php |
324ac3c5 |
1062 | |
6c930ade |
1063 | } |
bdfb67f8 |
1064 | |
c7df3f1b |
1065 | /** |
8cc8ec79 |
1066 | * Function to map an uid list with a msg header array by uid |
1067 | * The mapped headers are printed with printMessage |
1068 | * aMailbox parameters contains info about the page we are on, the |
1069 | * used search criteria, the number of messages to show |
0fdc2fb6 |
1070 | * |
8cc8ec79 |
1071 | * @param resource $imapConnection socket handle to imap |
1072 | * @param array $aMailbox array with required elements MSG_HEADERS, UIDSET, OFFSET, LIMIT |
1073 | * @return void |
1074 | **/ |
1075 | function displayMessageArray($imapConnection, $aMailbox) { |
324ac3c5 |
1076 | $iSetIndx = $aMailbox['SETINDEX']; |
1077 | $aId = $aMailbox['UIDSET'][$iSetIndx]; |
8cc8ec79 |
1078 | $aHeaders = $aMailbox['MSG_HEADERS']; |
1079 | $iOffset = $aMailbox['OFFSET']; |
1080 | $sort = $aMailbox['SORT']; |
1081 | $iPageOffset = $aMailbox['PAGEOFFSET']; |
1082 | $sMailbox = $aMailbox['NAME']; |
324ac3c5 |
1083 | $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']])) ? $aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false; |
1084 | $aSearch = ($sSearch) ? array('search.php',$aMailbox['SETINDEX']) : null; |
1085 | |
1086 | if ($aMailbox['SORT'] & SQSORT_THREAD) { |
1087 | $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']]; |
e0e30169 |
1088 | $bThread = true; |
1089 | } else { |
1090 | $bThread = false; |
1091 | } |
6a622b59 |
1092 | /* |
0fdc2fb6 |
1093 | * Loop through and display the info for each message. |
1094 | * ($t is used for the checkbox number) |
1095 | */ |
324ac3c5 |
1096 | $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $iOffset + $aMailbox['LIMIT']; |
8cc8ec79 |
1097 | for ($i=$iOffset,$t=0;$i<$iEnd;++$i) { |
1098 | if (isset($aId[$i])) { |
1099 | $bLast = ((isset($aId[$i+1]) && isset($aHeaders[$aId[$i+1]])) |
1100 | || ($i == $iEnd )) ? false : true; |
e0e30169 |
1101 | if ($bThread) { |
1102 | $indent = (isset($aIndentArray[$aId[$i]])) ? $aIndentArray[$aId[$i]] : 0; |
1103 | } else { |
1104 | $indent = 0; |
1105 | } |
8cc8ec79 |
1106 | $aMsg = array( |
1107 | 'HEADER' => $aHeaders[$aId[$i]], |
1108 | 'INDX' => $t, |
1109 | 'OFFSET' => $iOffset, |
1110 | 'PAGEOFFSET' => $iPageOffset, |
1111 | 'SORT' => $sort, |
1112 | 'SEARCH' => $aSearch, |
1113 | 'MAILBOX' => $sMailbox, |
e0e30169 |
1114 | 'INDENT' => $indent, |
8cc8ec79 |
1115 | 'LAST' => $bLast |
1116 | ); |
324ac3c5 |
1117 | printMessageInfo($aMsg); |
0fdc2fb6 |
1118 | ++$t; |
6a622b59 |
1119 | } else { |
1c198ef7 |
1120 | break; |
6a622b59 |
1121 | } |
94ac35c6 |
1122 | } |
bdfb67f8 |
1123 | } |
1124 | |
c7df3f1b |
1125 | /** |
0fdc2fb6 |
1126 | * Displays the standard message list header. |
1127 | * |
1128 | * To finish the table, you need to do a "</table></table>"; |
1129 | * |
8cc8ec79 |
1130 | * @param resource $imapConnection |
1131 | * @param array $aMailbox associative array with mailbox related information |
1132 | * @param string $msg_cnt_str |
1133 | * @param string $paginator Paginator string |
0fdc2fb6 |
1134 | */ |
6a622b59 |
1135 | function mail_message_listing_beginning ($imapConnection, |
8cc8ec79 |
1136 | $aMailbox, |
1137 | $msg_cnt_str = '', |
1138 | $paginator = ' ' |
1139 | ) { |
324ac3c5 |
1140 | global $color, $show_flag_buttons, $PHP_SELF; |
1141 | global $lastTargetMailbox; |
6a622b59 |
1142 | |
1143 | $php_self = $PHP_SELF; |
324ac3c5 |
1144 | |
8cc8ec79 |
1145 | $urlMailbox = urlencode($aMailbox['NAME']); |
e35045b7 |
1146 | |
6a622b59 |
1147 | if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) { |
1148 | $source_url = $regs[1]; |
1149 | } else { |
1150 | $source_url = $php_self; |
1151 | } |
1152 | |
1153 | if (!isset($msg)) { |
1154 | $msg = ''; |
1155 | } |
abafb676 |
1156 | |
b531f8ea |
1157 | $moveFields = addHidden('msg', $msg). |
8cc8ec79 |
1158 | addHidden('mailbox', $aMailbox['NAME']). |
324ac3c5 |
1159 | addHidden('startMessage', $aMailbox['PAGEOFFSET']); |
8fe969f6 |
1160 | |
5e0539cd |
1161 | /* build thread sorting links */ |
324ac3c5 |
1162 | $sort = $aMailbox['SORT']; |
8cc8ec79 |
1163 | if ($aMailbox['ALLOW_THREAD']) { |
324ac3c5 |
1164 | if ($aMailbox['SORT'] & SQSORT_THREAD) { |
1165 | $sort -= SQSORT_THREAD; |
8cc8ec79 |
1166 | $thread_name = _("Unthread View"); |
1167 | } else { |
8cc8ec79 |
1168 | $thread_name = _("Thread View"); |
324ac3c5 |
1169 | $sort = $aMailbox['SORT'] + SQSORT_THREAD; |
8cc8ec79 |
1170 | } |
324ac3c5 |
1171 | $thread_link_str = '<small>[<a href="' . $source_url . '?srt=' |
1172 | . $sort . '&start_messages=1' |
8cc8ec79 |
1173 | . '&mailbox=' . urlencode($aMailbox['NAME']) . '">' . $thread_name |
1174 | . '</a>]</small>'; |
1175 | } else { |
1176 | $thread_link_str =''; |
1177 | } |
6a622b59 |
1178 | /* |
0fdc2fb6 |
1179 | * This is the beginning of the message list table. |
1180 | * It wraps around all messages |
1181 | */ |
8cc8ec79 |
1182 | $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']); |
3027826a |
1183 | $form_name = "FormMsgs" . $safe_name; |
3da12e67 |
1184 | |
324ac3c5 |
1185 | echo '<form name="' . $form_name . '" method="post" action="'.$php_self.'">' ."\n" |
0fdc2fb6 |
1186 | . $moveFields; |
324ac3c5 |
1187 | |
1188 | |
abafb676 |
1189 | ?> |
0fdc2fb6 |
1190 | <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>"> |
abafb676 |
1191 | <tr> |
0fdc2fb6 |
1192 | <td> |
abafb676 |
1193 | <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0"> |
0fdc2fb6 |
1194 | <tr> |
74091b64 |
1195 | <td align="left"><small><?php echo $paginator . $thread_link_str; ?></small></td> |
d8a255fe |
1196 | <td align="center"></td> |
abafb676 |
1197 | <td align="right"><small><?php echo $msg_cnt_str; ?></small></td> |
0fdc2fb6 |
1198 | </tr> |
abafb676 |
1199 | </table> |
0fdc2fb6 |
1200 | </td> |
abafb676 |
1201 | </tr> |
1202 | <tr width="100%" cellpadding="1" cellspacing="0" border="0" bgcolor="<?php echo $color[0]; ?>"> |
0fdc2fb6 |
1203 | <td> |
abafb676 |
1204 | <table border="0" width="100%" cellpadding="1" cellspacing="0"> |
0fdc2fb6 |
1205 | <tr> |
abafb676 |
1206 | <td align="left"> |
0fdc2fb6 |
1207 | <small><?php |
4669e892 |
1208 | |
fd181f53 |
1209 | // display flag buttons only if supported |
8cc8ec79 |
1210 | if ($show_flag_buttons && |
c075fcfe |
1211 | in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true) ) { |
1a531551 |
1212 | echo getButton('SUBMIT', 'markUnflagged',_("Unflag")); |
fd181f53 |
1213 | echo getButton('SUBMIT', 'markFlagged',_("Flag")); |
1214 | echo ' '; |
fd181f53 |
1215 | } |
c075fcfe |
1216 | if (in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true)) { |
4669e892 |
1217 | echo getButton('SUBMIT', 'markUnread',_("Unread")); |
1218 | echo getButton('SUBMIT', 'markRead',_("Read")); |
1219 | echo ' '; |
1220 | } |
98a9cc03 |
1221 | |
abafb676 |
1222 | echo getButton('SUBMIT', 'attache',_("Forward")); |
1223 | echo ' '; |
c075fcfe |
1224 | if (in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) { |
4669e892 |
1225 | echo getButton('SUBMIT', 'delete',_("Delete")); |
1226 | echo '<input type="checkbox" name="bypass_trash" />' . _("Bypass Trash"); |
1227 | echo ' '; |
1228 | } |
8cc8ec79 |
1229 | if (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY') { |
0fdc2fb6 |
1230 | echo getButton('SUBMIT', 'expungeButton',_("Expunge")) .' ' . _("mailbox") . "\n"; |
1231 | echo ' '; |
abafb676 |
1232 | } |
1233 | do_hook('mailbox_display_buttons'); |
0fdc2fb6 |
1234 | ?></small> |
abafb676 |
1235 | </td> |
4669e892 |
1236 | <?php |
c075fcfe |
1237 | if (in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) { |
4669e892 |
1238 | echo '<td align="right"> |
0fdc2fb6 |
1239 | <small>'; |
324ac3c5 |
1240 | echo ' <small> <tt><select name="targetMailbox">'; |
1241 | echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes); |
1242 | echo ' </select></tt> '; |
4669e892 |
1243 | echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n |
0fdc2fb6 |
1244 | </small>"; |
4669e892 |
1245 | } |
1246 | ?> |
abafb676 |
1247 | </td> |
0fdc2fb6 |
1248 | </tr> |
abafb676 |
1249 | </table> |
0fdc2fb6 |
1250 | </td> |
abafb676 |
1251 | </tr> |
0fdc2fb6 |
1252 | </table> |
a966982b |
1253 | |
abafb676 |
1254 | <?php |
1b91be0c |
1255 | do_hook('mailbox_form_before'); |
e9e5f0fa |
1256 | } |
a966982b |
1257 | |
c7df3f1b |
1258 | /** |
e0e30169 |
1259 | * Function to add the last row in a message list, it contains the paginator and info about |
1260 | * the number of messages. |
0fdc2fb6 |
1261 | * |
8cc8ec79 |
1262 | * @param integer $num_msgs number of messages in a mailbox |
1263 | * @param string $paginator_str Paginator string [Prev | Next] [ 1 2 3 ... 91 92 94 ] [Show all] |
1264 | * @param string $msg_cnt_str Message count string Viewing Messages: 21 to 1861 (20 total) |
0fdc2fb6 |
1265 | */ |
8cc8ec79 |
1266 | function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str) { |
1267 | global $color; |
0fdc2fb6 |
1268 | if ($num_msgs) { |
4b920601 |
1269 | /* space between list and footer */ |
abafb676 |
1270 | ?> |
0fdc2fb6 |
1271 | <tr><td height="5" bgcolor="<?php echo $color[4]; ?>" colspan="1"></td></tr> |
1272 | <tr> |
abafb676 |
1273 | <td> |
0fdc2fb6 |
1274 | <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>"> |
abafb676 |
1275 | <tr> |
0fdc2fb6 |
1276 | <td> |
abafb676 |
1277 | <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0"> |
0fdc2fb6 |
1278 | <tr> |
abafb676 |
1279 | <td align="left"><small><?php echo $paginator_str; ?></small></td> |
1280 | <td align="right"><small><?php echo $msg_cnt_str; ?></small></td> |
0fdc2fb6 |
1281 | </tr> |
abafb676 |
1282 | </table> |
0fdc2fb6 |
1283 | </td> |
abafb676 |
1284 | </tr> |
0fdc2fb6 |
1285 | </table> |
abafb676 |
1286 | </td> |
0fdc2fb6 |
1287 | </tr> |
abafb676 |
1288 | <?php |
0fdc2fb6 |
1289 | } |
6a622b59 |
1290 | /* End of message-list table */ |
a966982b |
1291 | |
6a622b59 |
1292 | do_hook('mailbox_index_after'); |
c7df3f1b |
1293 | echo "</form>\n"; |
a966982b |
1294 | } |
1295 | |
c7df3f1b |
1296 | /** |
8cc8ec79 |
1297 | * Prints the table header for the messages list view |
0fdc2fb6 |
1298 | * |
8cc8ec79 |
1299 | * @param array $aMailbox |
0fdc2fb6 |
1300 | */ |
8cc8ec79 |
1301 | function printHeader($aMailbox) { |
1302 | global $index_order, $internal_date_sort, $color; |
1303 | |
1304 | if ($aMailbox['SORT_METHOD'] != 'THREAD') { |
1305 | $showsort = true; |
1306 | } else { |
1307 | $showsort = false; |
1308 | } |
1309 | |
1b91be0c |
1310 | echo html_tag( 'tr' ,'' , 'center', $color[5] ); |
e4b5f9d1 |
1311 | |
1312 | /* calculate the width of the subject column based on the |
0fdc2fb6 |
1313 | * widths of the other columns */ |
e4b5f9d1 |
1314 | $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5); |
1315 | $subjectwidth = 100; |
1316 | foreach($index_order as $item) { |
4669e892 |
1317 | $subjectwidth -= $widths[$item]; |
e4b5f9d1 |
1318 | } |
1319 | |
1320 | foreach ($index_order as $item) { |
1321 | switch ($item) { |
8008456a |
1322 | case 1: /* checkbox */ |
8cc8ec79 |
1323 | echo html_tag( 'td',get_selectall_link($aMailbox) , '', '', 'width="1%"' ); |
abafb676 |
1324 | break; |
8008456a |
1325 | case 5: /* flags */ |
abafb676 |
1326 | echo html_tag( 'td','' , '', '', 'width="1%"' ); |
8008456a |
1327 | break; |
1328 | case 2: /* from */ |
8cc8ec79 |
1329 | if (handleAsSent($aMailbox['NAME'])) { |
1b91be0c |
1330 | echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' ) |
0fdc2fb6 |
1331 | . '<b>' . _("To") . '</b>'; |
03975a39 |
1332 | if ($showsort) { |
8cc8ec79 |
1333 | ShowSortButton($aMailbox, SQSORT_TO_ASC, SQSORT_TO_DEC); |
03975a39 |
1334 | } |
8008456a |
1335 | } else { |
1b91be0c |
1336 | echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' ) |
0fdc2fb6 |
1337 | . '<b>' . _("From") . '</b>'; |
03975a39 |
1338 | if ($showsort) { |
8cc8ec79 |
1339 | ShowSortButton($aMailbox, SQSORT_FROM_ASC, SQSORT_FROM_DEC); |
03975a39 |
1340 | } |
8008456a |
1341 | } |
1b91be0c |
1342 | echo "</td>\n"; |
8008456a |
1343 | break; |
1344 | case 3: /* date */ |
1b91be0c |
1345 | echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' ) |
0fdc2fb6 |
1346 | . '<b>' . _("Date") . '</b>'; |
a966982b |
1347 | if ($showsort) { |
03975a39 |
1348 | if ($internal_date_sort) { |
8cc8ec79 |
1349 | ShowSortButton($aMailbox, SQSORT_INT_DATE_ASC, SQSORT_INT_DATE_DEC); |
03975a39 |
1350 | } else { |
8cc8ec79 |
1351 | ShowSortButton($aMailbox, SQSORT_DATE_ASC, SQSORT_DATE_DEC); |
03975a39 |
1352 | } |
8008456a |
1353 | } |
1b91be0c |
1354 | echo "</td>\n"; |
8008456a |
1355 | break; |
a966982b |
1356 | case 4: /* subject */ |
e4b5f9d1 |
1357 | echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' ) |
0fdc2fb6 |
1358 | . '<b>' . _("Subject") . '</b>'; |
a966982b |
1359 | if ($showsort) { |
8cc8ec79 |
1360 | ShowSortButton($aMailbox, SQSORT_SUBJ_ASC, SQSORT_SUBJ_DEC); |
8008456a |
1361 | } |
1b91be0c |
1362 | echo "</td>\n"; |
8008456a |
1363 | break; |
1364 | case 6: /* size */ |
03975a39 |
1365 | echo html_tag( 'td', '', 'center','','width="5%" nowrap') |
1366 | . '<b>' . _("Size") . '</b>'; |
1367 | if ($showsort) { |
8cc8ec79 |
1368 | ShowSortButton($aMailbox, SQSORT_SIZE_ASC, SQSORT_SIZE_DEC); |
03975a39 |
1369 | } |
1370 | echo "</td>\n"; |
8008456a |
1371 | break; |
1372 | } |
bdfb67f8 |
1373 | } |
1b91be0c |
1374 | echo "</tr>\n"; |
bdfb67f8 |
1375 | } |
1376 | |
e9e5f0fa |
1377 | |
c7df3f1b |
1378 | /** |
0fdc2fb6 |
1379 | * This function shows the sort button. Isn't this a good comment? |
1380 | * |
8cc8ec79 |
1381 | * @param array $aMailbox |
1382 | * @param integer $Down |
1383 | * @param integer $Up |
0fdc2fb6 |
1384 | */ |
8cc8ec79 |
1385 | function ShowSortButton($aMailbox, $Down, $Up ) { |
6a622b59 |
1386 | global $PHP_SELF; |
94a4ea53 |
1387 | |
6a622b59 |
1388 | /* Figure out which image we want to use. */ |
8cc8ec79 |
1389 | if ($aMailbox['SORT'] != $Up && $aMailbox['SORT'] != $Down) { |
6a622b59 |
1390 | $img = 'sort_none.png'; |
1391 | $which = $Up; |
8cc8ec79 |
1392 | } elseif ($aMailbox['SORT'] == $Up) { |
6a622b59 |
1393 | $img = 'up_pointer.png'; |
1394 | $which = $Down; |
6c930ade |
1395 | } else { |
6a622b59 |
1396 | $img = 'down_pointer.png'; |
03975a39 |
1397 | $which = 0; |
e35045b7 |
1398 | } |
6a622b59 |
1399 | |
1400 | if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) { |
1401 | $source_url = $regs[1]; |
e35045b7 |
1402 | } else { |
6a622b59 |
1403 | $source_url = $PHP_SELF; |
6c930ade |
1404 | } |
6a622b59 |
1405 | |
1406 | /* Now that we have everything figured out, show the actual button. */ |
324ac3c5 |
1407 | echo ' <a href="' . $source_url .'?srt=' . $which |
8cc8ec79 |
1408 | . '&startMessage=1&mailbox=' . urlencode($aMailbox['NAME']) |
0fdc2fb6 |
1409 | . '"><img src="../images/' . $img |
1410 | . '" border="0" width="12" height="10" alt="sort" title="' |
494607f5 |
1411 | . _("Click here to change the sorting of the message list") .'" /></a>'; |
6a622b59 |
1412 | } |
1413 | |
c7df3f1b |
1414 | /** |
0fdc2fb6 |
1415 | * FIXME: Undocumented function |
1416 | * |
8cc8ec79 |
1417 | * @param array $aMailbox |
0fdc2fb6 |
1418 | */ |
8cc8ec79 |
1419 | function get_selectall_link($aMailbox) { |
1420 | global $checkall, $javascript_on; |
324ac3c5 |
1421 | global $PHP_SELF; |
6a622b59 |
1422 | |
1423 | $result = ''; |
1424 | if ($javascript_on) { |
8cc8ec79 |
1425 | $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']); |
3027826a |
1426 | $func_name = "CheckAll" . $safe_name; |
1427 | $form_name = "FormMsgs" . $safe_name; |
6a622b59 |
1428 | $result = '<script language="JavaScript" type="text/javascript">' |
1429 | . "\n<!-- \n" |
3027826a |
1430 | . "function " . $func_name . "() {\n" |
1431 | . " for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n" |
3962deff |
1432 | . " if(document." . $form_name . ".elements[i].type == 'checkbox' && " |
e0b57c62 |
1433 | . " document." . $form_name . ".elements[i].name.substring(0,3) == 'msg'){\n" |
3027826a |
1434 | . " document." . $form_name . ".elements[i].checked = " |
1435 | . " !(document." . $form_name . ".elements[i].checked);\n" |
6a622b59 |
1436 | . " }\n" |
1437 | . " }\n" |
1438 | . "}\n" |
1439 | . "//-->\n" |
abafb676 |
1440 | . '</script>' |
c7df3f1b |
1441 | . '<input type="checkbox" name="toggleAll" title="'._("Toggle All").'" onclick="'.$func_name.'();" />'; |
abafb676 |
1442 | // . <a href="javascript:void(0)" onClick="' . $func_name . '();">' . _("Toggle All") |
1443 | // . "</a>\n"; |
e35045b7 |
1444 | } else { |
6a622b59 |
1445 | if (strpos($PHP_SELF, "?")) { |
8cc8ec79 |
1446 | $result .= "<a href=\"$PHP_SELF&mailbox=" . urlencode($aMailbox['NAME']) |
324ac3c5 |
1447 | . "&startMessage=$aMailbox[PAGEOFFSET]&srt=$aMailbox[SORT]&checkall="; |
6a622b59 |
1448 | } else { |
1449 | $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox) |
324ac3c5 |
1450 | . "&startMessage=$aMailbox[PAGEOFFSET]&srt=$aMailbox[SORT]&checkall="; |
6a622b59 |
1451 | } |
1452 | if (isset($checkall) && $checkall == '1') { |
1453 | $result .= '0'; |
1454 | } else { |
1455 | $result .= '1'; |
1456 | } |
1457 | |
8cc8ec79 |
1458 | if (isset($aMailbox['SEARCH']) && $aMailbox['SEARCH'][0]) { |
1459 | $result .= '&where=' . urlencode($aMailbox['SEARCH'][0]) |
1460 | . '&what=' . urlencode($aMailbox['SEARCH'][1]); |
6a622b59 |
1461 | } |
1462 | $result .= "\">"; |
98a9cc03 |
1463 | $result .= _("All"); |
c7df3f1b |
1464 | $result .= "</a>\n"; |
e35045b7 |
1465 | } |
23d6bd09 |
1466 | |
6a622b59 |
1467 | /* Return our final result. */ |
1468 | return ($result); |
bdfb67f8 |
1469 | } |
1a0e0983 |
1470 | |
c7df3f1b |
1471 | /** |
0fdc2fb6 |
1472 | * This function computes the "Viewing Messages..." string. |
1473 | * |
1474 | * @param integer $start_msg first message number |
1475 | * @param integer $end_msg last message number |
1476 | * @param integer $num_msgs total number of message in folder |
1477 | * @return string |
1478 | */ |
bdfb67f8 |
1479 | function get_msgcnt_str($start_msg, $end_msg, $num_msgs) { |
6a622b59 |
1480 | /* Compute the $msg_cnt_str. */ |
1481 | $result = ''; |
1482 | if ($start_msg < $end_msg) { |
c7df3f1b |
1483 | $result = sprintf(_("Viewing Messages: %s to %s (%s total)"), |
0fdc2fb6 |
1484 | '<b>'.$start_msg.'</b>', '<b>'.$end_msg.'</b>', $num_msgs); |
6a622b59 |
1485 | } else if ($start_msg == $end_msg) { |
c7df3f1b |
1486 | $result = sprintf(_("Viewing Message: %s (1 total)"), '<b>'.$start_msg.'</b>'); |
6a622b59 |
1487 | } else { |
c7df3f1b |
1488 | $result = '<br />'; |
6a622b59 |
1489 | } |
1490 | /* Return our result string. */ |
1491 | return ($result); |
bdfb67f8 |
1492 | } |
1493 | |
c7df3f1b |
1494 | /** |
0fdc2fb6 |
1495 | * Generate a paginator link. |
1496 | * |
8cc8ec79 |
1497 | * @param mixed $box Mailbox name |
1498 | * @param mixed $start_msg Message Offset |
0fdc2fb6 |
1499 | * @param mixed $use |
1500 | * @param string $text text used for paginator link |
1501 | * @return string |
1502 | */ |
324ac3c5 |
1503 | function get_paginator_link($box, $start_msg, $text) { |
1504 | sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER); |
1505 | $result = "<a href=\"$php_self?startMessage=$start_msg&mailbox=$box\" " |
c7df3f1b |
1506 | . ">$text</a>"; |
6a622b59 |
1507 | |
6a622b59 |
1508 | return ($result); |
7b294953 |
1509 | } |
1510 | |
c7df3f1b |
1511 | /** |
0fdc2fb6 |
1512 | * This function computes the paginator string. |
1513 | * |
8cc8ec79 |
1514 | * @param string $box |
1515 | * @param integer $start_msg |
1516 | * @param integer $end_msg |
324ac3c5 |
1517 | * @param integer $num_msgs // total number of messages |
1518 | * @param integer $limit // number of messages to show on one page |
8cc8ec79 |
1519 | * @param integer $sort |
0fdc2fb6 |
1520 | */ |
324ac3c5 |
1521 | //function get_paginator_str($box, $start_msg, $end_msg, $num_msgs, |
1522 | // $show_num, $sort) { |
1523 | function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll) { |
1524 | |
1525 | global $username, $data_dir, $color; |
1526 | sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER); |
6a622b59 |
1527 | |
1528 | /* Initialize paginator string chunks. */ |
1529 | $prv_str = ''; |
1530 | $nxt_str = ''; |
eac26493 |
1531 | $pg_str = ''; |
6a622b59 |
1532 | $all_str = ''; |
6a622b59 |
1533 | |
1534 | $box = urlencode($box); |
324ac3c5 |
1535 | |
6a622b59 |
1536 | /* Create simple strings that will be creating the paginator. */ |
1537 | $spc = ' '; /* This will be used as a space. */ |
1538 | $sep = '|'; /* This will be used as a seperator. */ |
1539 | |
1540 | /* Get some paginator preference values. */ |
1541 | $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON); |
1542 | $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX); |
1543 | |
1544 | /* Make sure that our start message number is not too big. */ |
324ac3c5 |
1545 | $iOffset = min($iOffset, $iTotal); |
6a622b59 |
1546 | |
6a622b59 |
1547 | /* Compute the starting message of the previous and next page group. */ |
324ac3c5 |
1548 | $next_grp = $iOffset + $iLimit; |
1549 | $prev_grp = $iOffset - $iLimit; |
1550 | |
1551 | if (!$bShowAll) { |
1552 | /* Compute the basic previous and next strings. */ |
1553 | if (($next_grp <= $iTotal) && ($prev_grp >= 0)) { |
1554 | $prv_str = get_paginator_link($box, $prev_grp, _("Previous")); |
1555 | $nxt_str = get_paginator_link($box, $next_grp, _("Next")); |
1556 | } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) { |
1557 | $prv_str = get_paginator_link($box, $prev_grp, _("Previous")); |
1558 | $nxt_str = _("Next"); |
1559 | } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) { |
1560 | $prv_str = _("Previous"); |
1561 | $nxt_str = get_paginator_link($box, $next_grp, _("Next")); |
1562 | } |
6a622b59 |
1563 | |
324ac3c5 |
1564 | /* Page selector block. Following code computes page links. */ |
1565 | if ($iLimit != 0 && $pg_sel && ($iTotal > $iLimit)) { |
1566 | /* Most importantly, what is the current page!!! */ |
1567 | $cur_pg = intval($iOffset / $iLimit) + 1; |
6a622b59 |
1568 | |
324ac3c5 |
1569 | /* Compute total # of pages and # of paginator page links. */ |
1570 | $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */ |
1571 | $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */ |
6a622b59 |
1572 | |
324ac3c5 |
1573 | /* Compute the size of the four quarters of the page links. */ |
6a622b59 |
1574 | |
324ac3c5 |
1575 | /* If we can, just show all the pages. */ |
1576 | if (($tot_pgs - 1) <= $pg_max) { |
6a622b59 |
1577 | $q1_pgs = $cur_pg - 1; |
324ac3c5 |
1578 | $q2_pgs = $q3_pgs = 0; |
6a622b59 |
1579 | $q4_pgs = $tot_pgs - $cur_pg; |
324ac3c5 |
1580 | |
1581 | /* Otherwise, compute some magic to choose the four quarters. */ |
1582 | } else { |
1583 | /* |
1584 | * Compute the magic base values. Added together, |
1585 | * these values will always equal to the $pag_pgs. |
1586 | * NOTE: These are DEFAULT values and do not take |
1587 | * the current page into account. That is below. |
1588 | */ |
1589 | $q1_pgs = floor($vis_pgs/4); |
1590 | $q2_pgs = round($vis_pgs/4, 0); |
1591 | $q3_pgs = ceil($vis_pgs/4); |
1592 | $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0); |
1593 | |
1594 | /* Adjust if the first quarter contains the current page. */ |
1595 | if (($cur_pg - $q1_pgs) < 1) { |
1596 | $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs; |
1597 | $q1_pgs = $cur_pg - 1; |
1598 | $q2_pgs = 0; |
1599 | $q3_pgs += ceil($extra_pgs / 2); |
1600 | $q4_pgs += floor($extra_pgs / 2); |
1601 | |
1602 | /* Adjust if the first and second quarters intersect. */ |
1603 | } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) { |
1604 | $extra_pgs = $q2_pgs; |
1605 | $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4); |
1606 | $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4); |
1607 | $q3_pgs += ceil($extra_pgs / 2); |
1608 | $q4_pgs += floor($extra_pgs / 2); |
1609 | |
1610 | /* Adjust if the fourth quarter contains the current page. */ |
1611 | } else if (($cur_pg + $q4_pgs) >= $tot_pgs) { |
1612 | $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs; |
1613 | $q3_pgs = 0; |
1614 | $q4_pgs = $tot_pgs - $cur_pg; |
1615 | $q1_pgs += floor($extra_pgs / 2); |
1616 | $q2_pgs += ceil($extra_pgs / 2); |
1617 | |
1618 | /* Adjust if the third and fourth quarter intersect. */ |
1619 | } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) { |
1620 | $extra_pgs = $q3_pgs; |
1621 | $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4); |
1622 | $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4); |
1623 | $q1_pgs += floor($extra_pgs / 2); |
1624 | $q2_pgs += ceil($extra_pgs / 2); |
1625 | } |
6a622b59 |
1626 | } |
e35045b7 |
1627 | |
324ac3c5 |
1628 | /* |
1629 | * I am leaving this debug code here, commented out, because |
1630 | * it is a really nice way to see what the above code is doing. |
1631 | * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = " |
1632 | * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />'; |
1633 | */ |
6a622b59 |
1634 | |
324ac3c5 |
1635 | /* Print out the page links from the compute page quarters. */ |
6a622b59 |
1636 | |
324ac3c5 |
1637 | /* Start with the first quarter. */ |
1638 | if (($q1_pgs == 0) && ($cur_pg > 1)) { |
6a622b59 |
1639 | $pg_str .= "...$spc"; |
324ac3c5 |
1640 | } else { |
1641 | for ($pg = 1; $pg <= $q1_pgs; ++$pg) { |
1642 | $start = (($pg-1) * $iLimit) + 1; |
1643 | $pg_str .= get_paginator_link($box, $start, $pg) . $spc; |
1644 | } |
1645 | if ($cur_pg - $q2_pgs - $q1_pgs > 1) { |
1646 | $pg_str .= "...$spc"; |
1647 | } |
6a622b59 |
1648 | } |
6a622b59 |
1649 | |
324ac3c5 |
1650 | /* Continue with the second quarter. */ |
1651 | for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) { |
1652 | $start = (($pg-1) * $iLimit) + 1; |
1653 | $pg_str .= get_paginator_link($box, $start, $pg) . $spc; |
1654 | } |
6a622b59 |
1655 | |
324ac3c5 |
1656 | /* Now print the current page. */ |
1657 | $pg_str .= $cur_pg . $spc; |
6a622b59 |
1658 | |
324ac3c5 |
1659 | /* Next comes the third quarter. */ |
1660 | for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) { |
1661 | $start = (($pg-1) * $iLimit) + 1; |
1662 | $pg_str .= get_paginator_link($box, $start, $pg) . $spc; |
1663 | } |
6a622b59 |
1664 | |
324ac3c5 |
1665 | /* And last, print the forth quarter page links. */ |
1666 | if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) { |
6a622b59 |
1667 | $pg_str .= "...$spc"; |
324ac3c5 |
1668 | } else { |
1669 | if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) { |
1670 | $pg_str .= "...$spc"; |
1671 | } |
1672 | for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) { |
1673 | $start = (($pg-1) * $iLimit) + 1; |
1674 | $pg_str .= get_paginator_link($box, $start,$pg) . $spc; |
1675 | } |
6a622b59 |
1676 | } |
1677 | } |
324ac3c5 |
1678 | } else { |
1679 | $pg_str = "<a href=\"$php_self?showall=0" |
1680 | . "&startMessage=1&mailbox=$box\" " |
c7df3f1b |
1681 | . ">" ._("Paginate") . '</a>'; |
6a622b59 |
1682 | } |
1683 | |
6a622b59 |
1684 | /* Put all the pieces of the paginator string together. */ |
1685 | /** |
0fdc2fb6 |
1686 | * Hairy code... But let's leave it like it is since I am not certain |
1687 | * a different approach would be any easier to read. ;) |
1688 | */ |
6a622b59 |
1689 | $result = ''; |
324ac3c5 |
1690 | if ( $prv_str || $nxt_str ) { |
1691 | |
1692 | /* Compute the 'show all' string. */ |
1693 | $all_str = "<a href=\"$php_self?showall=1" |
1694 | . "&startMessage=1&mailbox=$box\" " |
0fdc2fb6 |
1695 | . ">" . _("Show All") . '</a>'; |
324ac3c5 |
1696 | $result .= '['; |
1697 | $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : ''); |
1698 | $result .= ($nxt_str != '' ? $nxt_str : ''); |
1699 | $result .= ']' . $spc ; |
1700 | |
abafb676 |
1701 | } |
eac26493 |
1702 | |
1703 | $result .= ($pg_str != '' ? $spc . '['.$spc.$pg_str.']' . $spc : ''); |
abafb676 |
1704 | $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : ''); |
6a622b59 |
1705 | |
1706 | /* If the resulting string is blank, return a non-breaking space. */ |
1707 | if ($result == '') { |
1708 | $result = ' '; |
1709 | } |
e35045b7 |
1710 | |
6a622b59 |
1711 | /* Return our final magical paginator string. */ |
1712 | return ($result); |
bdfb67f8 |
1713 | } |
1714 | |
c7df3f1b |
1715 | /** |
0fdc2fb6 |
1716 | * FIXME: Undocumented function |
1717 | */ |
eaa4f45f |
1718 | function truncateWithEntities($subject, $trim_at) |
1719 | { |
341aa42f |
1720 | $ent_strlen = strlen($subject); |
1721 | if (($trim_at <= 0) || ($ent_strlen <= $trim_at)) |
6a622b59 |
1722 | return $subject; |
eaa4f45f |
1723 | |
1724 | global $languages, $squirrelmail_language; |
6a622b59 |
1725 | |
6a622b59 |
1726 | /* |
0fdc2fb6 |
1727 | * see if this is entities-encoded string |
1728 | * If so, Iterate through the whole string, find out |
1729 | * the real number of characters, and if more |
1730 | * than $trim_at, substr with an updated trim value. |
1731 | */ |
341aa42f |
1732 | $trim_val = $trim_at; |
1733 | $ent_offset = 0; |
1734 | $ent_loc = 0; |
a1440f89 |
1735 | while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) && |
1736 | (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) { |
1737 | $trim_val += ($ent_loc_end-$ent_loc); |
6a622b59 |
1738 | $ent_offset = $ent_loc_end+1; |
1739 | } |
341aa42f |
1740 | if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val + 6))) { |
a1440f89 |
1741 | $i = strpos($subject,';',$trim_val); |
1742 | if ($i) { |
1743 | $trim_val = strpos($subject,';',$trim_val); |
1744 | } |
46a49f65 |
1745 | } |
341aa42f |
1746 | // only print '...' when we're actually dropping part of the subject |
1747 | if ($ent_strlen <= $trim_val) |
6a622b59 |
1748 | return $subject; |
6a622b59 |
1749 | |
1750 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
1751 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
1752 | return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val); |
1753 | } |
ecaf6352 |
1754 | |
341aa42f |
1755 | return substr_replace($subject, '...', $trim_val); |
bdfb67f8 |
1756 | } |
f93c93b9 |
1757 | |
c7df3f1b |
1758 | /** |
0fdc2fb6 |
1759 | * FIXME: Undocumented function |
1760 | */ |
eaa4f45f |
1761 | function processSubject($subject, $threadlevel = 0) { |
1762 | /* Shouldn't ever happen -- caught too many times in the IMAP functions */ |
1763 | if ($subject == '') { |
1764 | return _("(no subject)"); |
1765 | } |
1766 | |
3fc1a95f |
1767 | global $truncate_subject; /* number of characters for Subject field (<= 0 for unchanged) */ |
1768 | $trim_at = $truncate_subject; |
eaa4f45f |
1769 | |
1770 | /* if this is threaded, subtract two chars per indentlevel */ |
1771 | if (($threadlevel > 0) && ($threadlevel <= 10)) |
1772 | $trim_at -= (2*$threadlevel); |
1773 | |
1774 | return truncateWithEntities($subject, $trim_at); |
1775 | } |
1776 | |
e9e5f0fa |
1777 | |
c7df3f1b |
1778 | /** |
0fdc2fb6 |
1779 | * Creates button |
1780 | * |
1781 | * @deprecated see form functions available in 1.5.1 and 1.4.3. |
1782 | * @param string $type |
1783 | * @param string $name |
1784 | * @param string $value |
1785 | * @param string $js |
1786 | * @param bool $enabled |
1787 | */ |
1a531551 |
1788 | function getButton($type, $name, $value, $js = '', $enabled = TRUE) { |
98a9cc03 |
1789 | $disabled = ( $enabled ? '' : 'disabled ' ); |
1a531551 |
1790 | $js = ( $js ? $js.' ' : '' ); |
c7df3f1b |
1791 | return '<input '.$disabled.$js. |
0fdc2fb6 |
1792 | 'type="'.$type. |
1793 | '" name="'.$name. |
c7df3f1b |
1794 | '" value="'.$value . |
1795 | '" style="padding: 0px; margin: 0px" />'; |
e9e5f0fa |
1796 | } |
1797 | |
c7df3f1b |
1798 | /** |
0fdc2fb6 |
1799 | * Puts string into cell, aligns it and adds <small> tag |
1800 | * |
1801 | * @param string $string string |
1802 | * @param string $align alignment |
1803 | */ |
e9e5f0fa |
1804 | function getSmallStringCell($string, $align) { |
6a622b59 |
1805 | return html_tag('td', |
1806 | '<small>' . $string . ': </small>', |
1807 | $align, |
1808 | '', |
1809 | 'nowrap' ); |
e9e5f0fa |
1810 | } |
1811 | |
c7df3f1b |
1812 | /** |
0fdc2fb6 |
1813 | * This should go in imap_mailbox.php |
1814 | * @param string $mailbox |
1815 | */ |
a3439b27 |
1816 | function handleAsSent($mailbox) { |
6a8e7cae |
1817 | global $handleAsSent_result; |
4669e892 |
1818 | |
6a622b59 |
1819 | /* First check if this is the sent or draft folder. */ |
6a8e7cae |
1820 | $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox); |
a3439b27 |
1821 | |
6a622b59 |
1822 | /* Then check the result of the handleAsSent hook. */ |
1823 | do_hook('check_handleAsSent_result', $mailbox); |
a3439b27 |
1824 | |
6a622b59 |
1825 | /* And return the result. */ |
6a8e7cae |
1826 | return $handleAsSent_result; |
6a622b59 |
1827 | } |
e842b215 |
1828 | |
324ac3c5 |
1829 | /** |
1830 | * Process messages list form and handle the cache gracefully. If $sButton and |
1831 | * $aUid are provided as argument then you can fake a message list submit and |
1832 | * use it i.e. in read_body.php for del move next and update the cache |
1833 | * |
1834 | * @param resource $imapConnection imap connection |
1835 | * @param array $aMailbox (reference) cached mailbox |
1836 | * @param string $sButton fake a submit button |
1837 | * @param array $aUid fake the $msg array |
1838 | * @return string $sError error string in case of an error |
1839 | * @author Marc Groot Koerkamp |
1840 | */ |
1841 | function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) { |
1842 | |
1843 | /* incoming formdata */ |
1844 | $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_POST)) ? 'move' : $sButton; |
1845 | $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_POST)) ? 'expunge' : $sButton; |
1846 | $sButton = (sqgetGlobalVar('attache', $sTmp, SQ_POST)) ? 'attache' : $sButton; |
1847 | $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_POST)) ? 'setDeleted' : $sButton; |
1848 | $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_POST)) ? 'setDeleted' : $sButton; |
1849 | $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_POST)) ? 'setSeen' : $sButton; |
1850 | $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_POST)) ? 'unsetSeen' : $sButton; |
1851 | $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_POST)) ? 'setFlagged' : $sButton; |
1852 | $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_POST)) ? 'unsetFlagged' : $sButton; |
1853 | sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST); |
1854 | sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST); |
1855 | sqgetGlobalVar('msg', $msg, SQ_POST); |
1856 | |
1857 | $sError = ''; |
1858 | $mailbox = $aMailbox['NAME']; |
1859 | |
1860 | /* retrieve the check boxes */ |
1861 | $aUid = (isset($msg) && is_array($msg)) ? array_values($msg) : $aUid; |
1862 | |
1863 | if (count($aUid) && $sButton != 'expunge') { |
1864 | $aUpdatedMsgs = false; |
1865 | $bExpunge = false; |
1866 | switch ($sButton) { |
1867 | case 'setDeleted': |
1868 | // check if id exists in case we come from read_body |
1869 | if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) && |
1870 | !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) { |
1871 | break; |
1872 | } |
1873 | // What kind of hook is this, can it be removed? Disabled for now because it can invalidate the cache |
1874 | //if (!boolean_hook_function('move_messages_button_action', NULL, 1)) { |
1875 | $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash); |
1876 | $bExpunge = true; |
1877 | //} |
1878 | break; |
1879 | case 'unsetDeleted': |
1880 | case 'setSeen': |
1881 | case 'unsetSeen': |
1882 | case 'setFlagged': |
1883 | case 'unsetFlagged': |
1884 | // get flag |
1885 | $sFlag = (substr($sButton,0,3) == 'set') ? '\\'.substr($sButton,3) : '\\'.substr($sButton,5); |
1886 | $bSet = (substr($sButton,0,3) == 'set') ? true : false; |
1887 | $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true); |
1888 | break; |
1889 | case 'move': |
1890 | $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox); |
1891 | sqsession_register($targetMailbox,'lastTargetMailbox'); |
1892 | $bExpunge = true; |
1893 | break; |
1894 | case 'attache': |
1895 | $aMsgHeaders = array(); |
1896 | foreach ($aUid as $iUid) { |
1897 | $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid]; |
1898 | } |
1899 | if (count($aMsgHeaders)) { |
1900 | $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders); |
1901 | // dirty hack, add info to $aMailbox |
1902 | $aMailbox['FORWARD_SESSION'] = $composesession; |
1903 | } |
1904 | break; |
1905 | } |
1906 | /** |
1907 | * Updates messages is an array containing the result of the untagged |
1908 | * fetch responses send by the imap server due to a flag change. That |
1909 | * response is parsed in a array with msg arrays by the parseFetch function |
1910 | */ |
1911 | if ($aUpdatedMsgs) { |
1912 | // Update the message headers cache |
1913 | $aDeleted = array(); |
1914 | foreach ($aUpdatedMsgs as $iUid => $aMsg) { |
1915 | if (isset($aMsg['FLAGS'])) { |
1916 | $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS']; |
1917 | /** |
1918 | * Count the messages with the \Delete flag set so we can determine |
1919 | * if the number of expunged messages equals the number of flagged |
1920 | * messages for deletion. |
1921 | */ |
1922 | if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) { |
1923 | $aDeleted[] = $iUid; |
1924 | } |
1925 | } |
1926 | } |
1927 | if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] && |
1928 | $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true)) |
1929 | { |
1930 | if (count($aDeleted) != $iExpungedMessages) { |
1931 | // there are more messages deleted permanently then we expected |
1932 | // invalidate the cache |
1933 | $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false; |
1934 | $aMailbox['MSG_HEADERS'] = false; |
1935 | } else { |
1936 | // remove expunged messages from cache |
1937 | $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']]; |
1938 | if (is_array($aUidSet)) { |
1939 | // create a UID => array index temp array |
1940 | $aUidSetDummy = array_flip($aUidSet); |
1941 | foreach ($aDeleted as $iUid) { |
1942 | // get the id as well in case of SQM_SORT_NONE |
1943 | if ($aMailbox['SORT'] == SQSORT_NONE) { |
1944 | $aMailbox['ID'] = false; |
1945 | //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID']; |
1946 | //unset($aMailbox['ID'][$iId]); |
1947 | } |
1948 | // unset the UID and message header |
1949 | unset($aUidSetDummy[$iUid]); |
1950 | unset($aMailbox['MSG_HEADERS'][$iUid]); |
1951 | } |
1952 | $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy); |
1953 | // update EXISTS info |
1954 | $aMailbox['EXISTS'] -= $iExpungedMessages; |
1955 | } |
1956 | } |
1957 | // Change the startMessage number if the mailbox was changed |
1958 | if (($aMailbox['PAGEOFFSET']+$iExpungedMessages-1) >= $aMailbox['EXISTS']) { |
1959 | $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ? |
1960 | $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1; |
1961 | } |
1962 | } |
1963 | } |
1964 | } else { |
1965 | if ($sButton == 'expunge') { |
1966 | /** |
1967 | * on expunge we do not know which messages will be deleted |
1968 | * so it's useless to try to sync the cache |
1969 | |
1970 | * Close the mailbox so we do not need to parse the untagged expunge |
1971 | * responses which do not contain uid info. |
1972 | * NB: Closing a mailbox is faster then expunge because the imap |
1973 | * server does not need to generate the untagged expunge responses |
1974 | */ |
1975 | sqimap_run_command($imapConnection,'CLOSE',false,$result,$message); |
1976 | $aMbxResponse = sqimap_mailbox_select($imapConnection,$aMailbox['NAME']); |
1977 | // update the $aMailbox array |
1978 | $aMailbox['EXISTS'] = $aMbxResponse['EXISTS']; |
1979 | $aMailbox['UIDSET'] = false; |
1980 | } else { |
1981 | if ($sButton) { |
1982 | $sError = _("No messages were selected."); |
1983 | } |
1984 | } |
1985 | } |
1986 | return $sError; |
1987 | } |
1988 | |
1989 | function attachSelectedMessages($imapConnection,$aMsgHeaders) { |
1990 | global $username, $attachment_dir, |
1991 | $data_dir, $composesession, |
1992 | $compose_messages; |
1993 | |
1994 | if (!isset($compose_messages)) { |
1995 | $compose_messages = array(); |
1996 | sqsession_register($compose_messages,'compose_messages'); |
1997 | } |
1998 | |
1999 | if (!$composesession) { |
2000 | $composesession = 1; |
2001 | sqsession_register($composesession,'composesession'); |
2002 | } else { |
2003 | $composesession++; |
2004 | sqsession_register($composesession,'composesession'); |
2005 | } |
2006 | |
2007 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
2008 | |
2009 | $composeMessage = new Message(); |
2010 | $rfc822_header = new Rfc822Header(); |
2011 | $composeMessage->rfc822_header = $rfc822_header; |
2012 | $composeMessage->reply_rfc822_header = ''; |
2013 | |
2014 | foreach($aMsgHeaders as $iUid => $aMsgHeader) { |
2015 | /** |
2016 | * Retrieve the full message |
2017 | */ |
2018 | $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE); |
2019 | |
2020 | if ($response == 'OK') { |
2021 | $subject = (isset($aMsgHeader['SUBJECT'])) ? $aMsgHeader['SUBJECT'] : $iUid; |
2022 | |
2023 | array_shift($body_a); |
2024 | array_pop($body_a); |
2025 | $body = implode('', $body_a); |
2026 | $body .= "\r\n"; |
2027 | |
2028 | $localfilename = GenerateRandomString(32, 'FILE', 7); |
2029 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
2030 | |
2031 | $fp = fopen( $full_localfilename, 'wb'); |
2032 | fwrite ($fp, $body); |
2033 | fclose($fp); |
2034 | $composeMessage->initAttachment('message/rfc822',$subject.'.msg', |
2035 | $full_localfilename); |
2036 | } |
2037 | } |
2038 | |
2039 | $compose_messages[$composesession] = $composeMessage; |
2040 | sqsession_register($compose_messages,'compose_messages'); |
2041 | return $composesession; |
2042 | } |
2043 | |
b531f8ea |
2044 | ?> |