mailbox_display.php 83 KB

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