mailbox_display.php 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. <?php
  2. /**
  3. * mailbox_display.php
  4. *
  5. * This contains functions that display mailbox information, such as the
  6. * table row that has sender, date, subject, etc...
  7. *
  8. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /**
  14. * Selects a mailbox for header retrieval.
  15. * Cache control for message headers is embedded.
  16. *
  17. * @param resource $imapConnection imap socket handle
  18. * @param string $mailbox mailbox to select and retrieve message headers from
  19. * @param array $aConfig array with system config settings and incoming vars
  20. * @param array $aProps mailbox specific properties
  21. *
  22. * @return array $aMailbox mailbox array with all relevant information
  23. *
  24. * @since 1.5.1
  25. * @author Marc Groot Koerkamp
  26. */
  27. function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aProps) {
  28. /**
  29. * NB: retrieve this from the session before accessing this function
  30. * and make sure you write it back at the end of the script after
  31. * the aMailbox var is added so that the headers are added to the cache
  32. */
  33. global $mailbox_cache;
  34. $aDefaultConfigProps = array(
  35. // 'charset' => 'US-ASCII',
  36. 'user' => false, /* no pref storage if false */
  37. 'setindex' => 0,
  38. // 'search' => 'ALL',
  39. 'max_cache_size' => SQM_MAX_MBX_IN_CACHE
  40. );
  41. $aConfig = array_merge($aDefaultConfigProps,$aConfig);
  42. $iSetIndx = $aConfig['setindex'];
  43. $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
  44. if ($mailbox_cache) {
  45. if (isset($mailbox_cache[$account.'_'.$mailbox])) {
  46. $aCachedMailbox = $mailbox_cache[$account.'_'.$mailbox];
  47. } else {
  48. $aCachedMailbox = false;
  49. }
  50. /* cleanup cache */
  51. if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
  52. $aTime = array();
  53. foreach($mailbox_cache as $cachedmailbox => $aVal) {
  54. $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
  55. }
  56. if (ksort($aTime,SORT_NUMERIC)) {
  57. for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++$i) {
  58. $sOldestMbx = array_shift($aTime);
  59. /**
  60. * Remove only the UIDSET and MSG_HEADERS from cache because those can
  61. * contain large amounts of data.
  62. */
  63. if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
  64. $mailbox_cache[$sOldestMbx]['UIDSET']= false;
  65. }
  66. if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
  67. $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
  68. }
  69. }
  70. }
  71. }
  72. } else {
  73. $aCachedMailbox = false;
  74. }
  75. /**
  76. * Deal with imap servers that do not return the required UIDNEXT or
  77. * UIDVALIDITY response
  78. * from a SELECT call (since rfc 3501 it's required).
  79. */
  80. if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) {
  81. $aStatus = sqimap_status_messages($imapConnection,$mailbox,
  82. array('UIDNEXT','UIDVALIDITY'));
  83. $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
  84. $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
  85. }
  86. $aMailbox['ACCOUNT'] = $account;
  87. $aMailbox['UIDSET'][$iSetIndx] = false;
  88. $aMailbox['ID'] = false;
  89. $aMailbox['SETINDEX'] = $iSetIndx;
  90. $aMailbox['MSG_HEADERS'] = false;
  91. if ($aCachedMailbox) {
  92. /**
  93. * Validate integrity of cached data
  94. */
  95. if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
  96. $aMbxResponse['EXISTS'] &&
  97. $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
  98. $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] &&
  99. isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
  100. (!isset($aConfig['search']) || /* always set search from the searchpage */
  101. $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
  102. if (isset($aCachedMailbox['MSG_HEADERS'])) {
  103. $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
  104. }
  105. $aMailbox['ID'] = $aCachedMailbox['ID'];
  106. if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
  107. if (isset($aProps[MBX_PREF_SORT]) && $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT'] ) {
  108. $newsort = $aProps[MBX_PREF_SORT];
  109. $oldsort = $aCachedMailbox['SORT'];
  110. /**
  111. * If it concerns a reverse sort we do not need to invalidate
  112. * the cached sorted UIDSET, a reverse is sufficient.
  113. */
  114. if ((($newsort % 2) && ($newsort + 1 == $oldsort)) ||
  115. (!($newsort % 2) && ($newsort - 1 == $oldsort))) {
  116. $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
  117. } else {
  118. $server_sort_array = false;
  119. $aMailbox['MSG_HEADERS'] = false;
  120. $aMailbox['ID'] = false;
  121. }
  122. // store the new sort value in the mailbox pref
  123. if ($aConfig['user']) {
  124. // FIXME, in ideal situation, we write back the
  125. // prefs at the end of the script
  126. setUserPref($aConfig['user'],'pref_'.$account.'_'.$mailbox,serialize($aProps));
  127. }
  128. } else {
  129. $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Restore the offset in the paginator if no new offset is provided.
  136. */
  137. if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
  138. $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET'];
  139. $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET'];
  140. } else {
  141. $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] -1 : 0;
  142. $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] : 1;
  143. }
  144. /**
  145. * Restore the number of messages in the result set
  146. */
  147. if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) {
  148. $aMailbox['TOTAL'][$iSetIndx] = $aCachedMailbox['TOTAL'][$iSetIndx];
  149. }
  150. /**
  151. * Restore the showall value no new showall value is provided.
  152. */
  153. if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
  154. isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
  155. $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx];
  156. } else {
  157. $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ? 1 : 0;
  158. }
  159. /**
  160. * Restore the sort order if no new sort order is provided.
  161. */
  162. if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) {
  163. $aMailbox['SORT'] = $aCachedMailbox['SORT'];
  164. } else {
  165. $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT])) ? $aProps[MBX_PREF_SORT] : 0;
  166. }
  167. /**
  168. * Restore the number of message to show per page when no new limit is provided
  169. */
  170. if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) {
  171. $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
  172. } else {
  173. $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT])) ? $aProps[MBX_PREF_LIMIT] : 15;
  174. }
  175. /**
  176. * Restore the ordered columns to show when no new ordered columns are provided
  177. */
  178. if (!isset($aProps[MBX_PREF_COLUMNS]) && isset($aCachedMailbox['COLUMNS'])) {
  179. $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS'];
  180. } else {
  181. $aMailbox['COLUMNS'] = (isset($aProps[MBX_PREF_COLUMNS])) ? $aProps[MBX_PREF_COLUMNS] :
  182. array(SQM_COL_FLAGS,SQM_COL_FROM, SQM_COL_SUBJ, SQM_COL_FLAGS);
  183. }
  184. /**
  185. * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
  186. */
  187. $aMailbox['FETCHHEADERS'] = (isset($aCachedMailbox['FETCHHEADERS'])) ? $aCachedMailbox['FETCHHEADERS'] : null;
  188. if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
  189. $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
  190. } else {
  191. $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE])) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false;
  192. }
  193. if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
  194. $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
  195. } else if (isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
  196. $aConfig['search'] != $aCachedMailbox['SEARCH'][$iSetIndx]) {
  197. // reset the pageindex
  198. $aMailbox['SEARCH'][$iSetIndx] = $aConfig['search'];
  199. $aMailbox['OFFSET'] = 0;
  200. $aMailbox['PAGEOFFSET'] = 1;
  201. } else {
  202. $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ? $aConfig['search'] : 'ALL';
  203. }
  204. if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) {
  205. $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx];
  206. } else {
  207. $aMailbox['CHARSET'][$iSetIndx] = (isset($aConfig['charset'])) ? $aConfig['charset'] : 'US-ASCII';
  208. }
  209. $aMailbox['NAME'] = $mailbox;
  210. $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
  211. $aMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS'];
  212. $aMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ? $aMbxResponse['RECENT'] : 0;
  213. $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
  214. $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
  215. $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS'];
  216. $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS'];
  217. /* decide if we are thread sorting or not */
  218. if ($aMailbox['SORT'] & SQSORT_THREAD) {
  219. if (!sqimap_capability($imapConnection,'THREAD')) {
  220. $aMailbox['SORT'] ^= SQSORT_THREAD;
  221. } else {
  222. $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
  223. }
  224. } else {
  225. $aMailbox['THREAD_INDENT'] = false;
  226. }
  227. /* set a timestamp for cachecontrol */
  228. $aMailbox['TIMESTAMP'] = time();
  229. return $aMailbox;
  230. }
  231. /**
  232. * Fetch the message headers for a mailbox. Settings are part of the aMailbox
  233. * array. Dependent of the mailbox settings it deals with sort, thread and search
  234. * If server sort is supported then SORT is also used for retrieving sorted search results
  235. *
  236. * @param resource $imapConnection imap socket handle
  237. * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
  238. * @return error $error error number
  239. * @since 1.5.1
  240. * @author Marc Groot Koerkamp
  241. */
  242. function fetchMessageHeaders($imapConnection, &$aMailbox) {
  243. /* FIX ME, this function is kind of big, maybe we can split it up in
  244. a couple of functions. Make sure the functions are private and starts with _
  245. Also make sure that the error codes are propagated */
  246. /**
  247. * Retrieve the UIDSET.
  248. * Setindex is used to be able to store multiple uid sets. That will make it
  249. * possible to display the mailbox multiple times in different sort order
  250. * or to store serach results separate from normal mailbox view.
  251. */
  252. $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
  253. $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
  254. /**
  255. * Adjust the start_msg
  256. */
  257. $start_msg = $aMailbox['PAGEOFFSET'];
  258. if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
  259. $start_msg -= $aMailbox['LIMIT'];
  260. if($start_msg < 1) {
  261. $start_msg = 1;
  262. }
  263. }
  264. if (is_array($aMailbox['UIDSET'])) {
  265. $aUid =& $aMailbox['UIDSET'][$iSetIndx];
  266. } else {
  267. $aUid = false;
  268. }
  269. $aFetchHeaders = $aMailbox['FETCHHEADERS'];
  270. $iError = 0;
  271. $aFetchItems = $aHeaderItems = array();
  272. // initialize the fields we want to retrieve:
  273. $aHeaderFields = array();
  274. foreach ($aFetchHeaders as $v) {
  275. switch ($v) {
  276. case SQM_COL_DATE: $aHeaderFields[] = 'Date'; break;
  277. case SQM_COL_TO: $aHeaderFields[] = 'To'; break;
  278. case SQM_COL_CC: $aHeaderFields[] = 'Cc'; break;
  279. case SQM_COL_FROM: $aHeaderFields[] = 'From'; break;
  280. case SQM_COL_SUBJ: $aHeaderFields[] = 'Subject'; break;
  281. case SQM_COL_PRIO: $aHeaderFields[] = 'X-Priority'; break;
  282. case SQM_COL_ATTACHMENT: $aHeaderFields[] = 'Content-Type'; break;
  283. case SQM_COL_INT_DATE: $aFetchItems[] = 'INTERNALDATE'; break;
  284. case SQM_COL_FLAGS: $aFetchItems[] = 'FLAGS'; break;
  285. case SQM_COL_SIZE: $aFetchItems[] = 'RFC822.SIZE'; break;
  286. default: break;
  287. }
  288. }
  289. /**
  290. * A uidset with sorted uid's is available. We can use the cache
  291. */
  292. if (isset($aUid) && $aUid ) {
  293. // limit the cache to SQM_MAX_PAGES_IN_CACHE
  294. if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
  295. $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
  296. $iCacheSize = count($aMailbox['MSG_HEADERS']);
  297. if ($iCacheSize > $iMaxMsgs) {
  298. $iReduce = $iCacheSize - $iMaxMsgs;
  299. foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
  300. if ($iReduce) {
  301. unset($aMailbox['MSG_HEADERS'][$iUid]);
  302. } else {
  303. break;
  304. }
  305. --$iReduce;
  306. }
  307. }
  308. }
  309. $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
  310. /* do some funky cache checks */
  311. if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
  312. // temp code, read_body del / next links fo not update fields.
  313. foreach ($aMailbox['MSG_HEADERS'] as $iUid => $aValue) {
  314. if (!isset($aValue['UID'])) {
  315. unset($aMailbox['MSG_HEADERS'][$iUid]);
  316. }
  317. }
  318. $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
  319. } else {
  320. $aMailbox['MSG_HEADERS'] = array();
  321. $aUidCached = array();
  322. }
  323. $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
  324. /**
  325. * $aUidNotCached contains an array with UID's which need to be fetched to
  326. * complete the needed message headers.
  327. */
  328. if (count($aUidNotCached)) {
  329. $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
  330. $aHeaderFields,$aFetchItems);
  331. // append the msgs to the existend headers
  332. $aMailbox['MSG_HEADERS'] += $aMsgs;
  333. }
  334. } else {
  335. /**
  336. * Initialize the sorted UID list or initiate a UID list with search
  337. * results and fetch the visible message headers
  338. */
  339. if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
  340. if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
  341. $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
  342. } else {
  343. $iError = 0;
  344. $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
  345. $aUid = $aMailbox['UIDSET'][$iSetIndx];
  346. }
  347. if (!$iError) {
  348. /**
  349. * Number of messages is the resultset
  350. */
  351. $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
  352. $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
  353. if (count($id_slice)) {
  354. $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
  355. $aHeaderFields,$aFetchItems);
  356. } else {
  357. $iError = 1; // FIX ME, define an error code
  358. }
  359. }
  360. } else { //
  361. $iError = 0;
  362. $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
  363. $aUid = $aMailbox['UIDSET'][$iSetIndx];
  364. if (!$iError) {
  365. /**
  366. * Number of messages is the resultset
  367. */
  368. $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
  369. $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
  370. if (count($id_slice)) {
  371. $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
  372. $aHeaderFields,$aFetchItems);
  373. } else {
  374. $iError = 1; // FIX ME, define an error code
  375. }
  376. }
  377. }
  378. }
  379. return $iError;
  380. }
  381. /**
  382. * Prepares the message headers for display inside a template. The links are calculated,
  383. * color for row highlighting is calculated and optionally the strings are truncated.
  384. *
  385. * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
  386. * @param array $aProps properties
  387. * @return array $aFormattedMessages array with message headers and format info
  388. * @since 1.5.1
  389. * @author Marc Groot Koerkamp
  390. */
  391. function prepareMessageList(&$aMailbox, $aProps) {
  392. /* Globalize link attributes so plugins can share in modifying them */
  393. global $link, $title, $target, $onclick, $link_extra;
  394. /* retrieve the properties */
  395. $my_email_address = (isset($aProps['email'])) ? $aProps['email'] : false;
  396. $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  397. $aColumnDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  398. $aExtraColumns = (isset($aProps['extra_columns'])) ? $aProps['extra_columns'] : array();
  399. $iAccount = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
  400. $sMailbox = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
  401. $sTargetModule = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
  402. /*
  403. * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
  404. * $aFormattedMessages[$iUid]['match_identity'] = true
  405. * The template can show some image if there is a match.
  406. * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
  407. */
  408. /**
  409. * Only retrieve values for displayable columns
  410. */
  411. foreach ($aColumnDesc as $k => $v) {
  412. switch ($k) {
  413. case SQM_COL_FROM: $aCol[SQM_COL_FROM] = 'from'; break;
  414. case SQM_COL_DATE: $aCol[SQM_COL_DATE] = 'date'; break;
  415. case SQM_COL_SUBJ: $aCol[SQM_COL_SUBJ] = 'subject'; break;
  416. case SQM_COL_FLAGS: $aCol[SQM_COL_FLAGS] = 'FLAGS'; break;
  417. case SQM_COL_SIZE: $aCol[SQM_COL_SIZE] = 'SIZE'; break;
  418. case SQM_COL_PRIO: $aCol[SQM_COL_PRIO] = 'x-priority'; break;
  419. case SQM_COL_ATTACHMENT: $aCol[SQM_COL_ATTACHMENT] = 'content-type'; break;
  420. case SQM_COL_INT_DATE: $aCol[SQM_COL_INT_DATE] = 'INTERNALDATE'; break;
  421. case SQM_COL_TO: $aCol[SQM_COL_TO] = 'to'; break;
  422. case SQM_COL_CC: $aCol[SQM_COL_CC] = 'cc'; break;
  423. case SQM_COL_BCC: $aCol[SQM_COL_BCC] = 'bcc'; break;
  424. default: break;
  425. }
  426. }
  427. $aExtraHighLightColumns = array();
  428. foreach ($aExtraColumns as $v) {
  429. switch ($v) {
  430. case SQM_COL_FROM: $aExtraHighLightColumns[] = 'from'; break;
  431. case SQM_COL_SUBJ: $aExtraHighLightColumns[] = 'subject'; break;
  432. case SQM_COL_TO: $aExtraHighLightColumns[] = 'to'; break;
  433. case SQM_COL_CC: $aExtraHighLightColumns[] = 'cc'; break;
  434. case SQM_COL_BCC: $aExtraHighLightColumns[] = 'bcc'; break;
  435. default: break;
  436. }
  437. }
  438. $aFormattedMessages = array();
  439. $iSetIndx = $aMailbox['SETINDEX'];
  440. $aId = $aMailbox['UIDSET'][$iSetIndx];
  441. $aHeaders =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
  442. MSG_HEADERS can contain large amounts of data */
  443. $iOffset = $aMailbox['OFFSET'];
  444. $sort = $aMailbox['SORT'];
  445. $iPageOffset = $aMailbox['PAGEOFFSET'];
  446. $sMailbox = $aMailbox['NAME'];
  447. $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
  448. $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ? $aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
  449. $aSearch = ($sSearch) ? array('search.php',$aMailbox['SETINDEX']) : null;
  450. /* avoid improper usage */
  451. if ($sMailbox && isset($iAccount) && $sTargetModule) {
  452. $aInitQuery = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
  453. } else {
  454. $aInitQuery = false;
  455. }
  456. if ($aMailbox['SORT'] & SQSORT_THREAD) {
  457. $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
  458. $bThread = true;
  459. } else {
  460. $bThread = false;
  461. }
  462. /*
  463. * Retrieve value for checkbox column
  464. */
  465. if (!sqgetGlobalVar('checkall',$checkall,SQ_GET)) {
  466. $checkall = false;
  467. }
  468. /*
  469. * Loop through and display the info for each message.
  470. */
  471. $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $iOffset + $aMailbox['LIMIT'];
  472. for ($i=$iOffset,$t=0;$i<$iEnd;++$i) {
  473. if (isset($aId[$i])) {
  474. $bHighLight = false;
  475. $value = $title = $link = $target = $onclick = $link_extra = '';
  476. $aQuery = ($aInitQuery !== false) ? $aInitQuery : false;
  477. $aMsg = $aHeaders[$aId[$i]];
  478. if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
  479. $aQuery[] = "where=". $aSearch[0];
  480. $aQuery[] = "what=" . $aSearch[1];
  481. }
  482. $iUid = (isset($aMsg['UID'])) ? $aMsg['UID'] : $aId[$i];
  483. if ($aQuery) {
  484. $aQuery[] = "passed_id=$aId[$i]";
  485. $aQuery[] = "startMessage=$iPageOffset";
  486. }
  487. foreach ($aCol as $k => $v) {
  488. $title = $link = $target = $onclick = $link_extra = '';
  489. $aColumns[$k] = array();
  490. $value = (isset($aMsg[$v])) ? $aMsg[$v] : '';
  491. $sUnknown = _("Unknown recipient");
  492. switch ($k) {
  493. case SQM_COL_FROM:
  494. $sUnknown = _("Unknown sender");
  495. case SQM_COL_TO:
  496. case SQM_COL_CC:
  497. case SQM_COL_BCC:
  498. $sTmp = false;
  499. if ($value) {
  500. if ($highlight_list && !$bHighLight) {
  501. $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
  502. }
  503. $aAddressList = parseRFC822Address($value);
  504. $sTmp = getAddressString($aAddressList,array('best' => true));
  505. $title = $title_maybe = '';
  506. foreach ($aAddressList as $aAddr) {
  507. $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL])) ? $aAddr[SQM_ADDR_PERSONAL] : '';
  508. $sMailbox = (isset($aAddr[SQM_ADDR_MAILBOX])) ? $aAddr[SQM_ADDR_MAILBOX] : '';
  509. $sHost = (isset($aAddr[SQM_ADDR_HOST])) ? $aAddr[SQM_ADDR_HOST] : '';
  510. if ($sPersonal) {
  511. $title .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
  512. } else {
  513. // if $value gets truncated we need to add the addresses with no
  514. // personal name as well
  515. $title_maybe .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
  516. }
  517. }
  518. if ($title) {
  519. $title = substr($title,0,-2); // strip ', ';
  520. }
  521. $sTmp = decodeHeader($sTmp);
  522. if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
  523. $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
  524. if ($sTrunc != $sTmp) {
  525. if (!$title) {
  526. $title = htmlspecialchars($sTmp);
  527. } else if ($title_maybe) {
  528. $title = $title .', '.$title_maybe;
  529. $title = substr($title,0,-2); // strip ', ';
  530. }
  531. }
  532. $sTmp = $sTrunc;
  533. }
  534. }
  535. $value = ($sTmp) ? $sTmp : $sUnknown;
  536. break;
  537. case SQM_COL_SUBJ:
  538. // subject is mime encoded, decode it.
  539. // value is sanitized in decoding function.
  540. // TODO, verify if it should be done before or after the highlighting
  541. $value=decodeHeader($value);
  542. if ($highlight_list && !$bHighLight) {
  543. $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
  544. }
  545. $iIndent = (isset($aIndentArray[$aId[$i]])) ? $aIndentArray[$aId[$i]] : 0;
  546. // FIXME: don't break 8bit symbols and html entities during truncation
  547. if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
  548. $sTmp = truncateWithEntities($value, $aColumnDesc[$k]['truncate']-$iIndent);
  549. // drop any double spaces since these will be displayed in the title
  550. $title = ($sTmp != $value) ? preg_replace('/\s{2,}/', ' ', $value) : '';
  551. $value = $sTmp;
  552. }
  553. /* generate the link to the message */
  554. if ($aQuery) {
  555. // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
  556. $link = $sTargetModule.'.php?' . implode('&amp;',$aQuery);
  557. // see top of this function for which attributes are available
  558. // in the global scope for plugin use (like $link, $target,
  559. // $onclick, $link_extra, $title, and so forth)
  560. // plugins are responsible for sharing nicely (such as for
  561. // setting the target, etc)
  562. do_hook('subject_link', $temp=array(&$iPageOffset, &$sSearch, &$aSearch));
  563. }
  564. $value = (trim($value)) ? $value : _("(no subject)");
  565. /* add thread indentation */
  566. $aColumns[$k]['indent'] = $iIndent;
  567. break;
  568. case SQM_COL_SIZE:
  569. $value = show_readable_size($value);
  570. break;
  571. case SQM_COL_DATE:
  572. case SQM_COL_INT_DATE:
  573. $value = getDateString(getTimeStamp(explode(' ',trim($value))));
  574. break;
  575. case SQM_COL_FLAGS:
  576. $aFlagColumn = array('seen' => false,
  577. 'deleted'=>false,
  578. 'answered'=>false,
  579. 'flagged' => false,
  580. 'draft' => false);
  581. if(!is_array($value)) $value = array();
  582. foreach ($value as $sFlag => $value) {
  583. switch ($sFlag) {
  584. case '\\seen' : $aFlagColumn['seen'] = true; break;
  585. case '\\deleted' : $aFlagColumn['deleted'] = true; break;
  586. case '\\answered': $aFlagColumn['answered'] = true; break;
  587. case '\\flagged' : $aFlagColumn['flagged'] = true; break;
  588. case '\\draft' : $aFlagColumn['draft'] = true; break;
  589. default: break;
  590. }
  591. }
  592. $value = $aFlagColumn;
  593. break;
  594. case SQM_COL_PRIO:
  595. $value = ($value) ? (int) $value : 3;
  596. break;
  597. case SQM_COL_ATTACHMENT:
  598. $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ? true : false;
  599. break;
  600. case SQM_COL_CHECK:
  601. $value = $checkall;
  602. break;
  603. default : break;
  604. }
  605. if ($title) { $aColumns[$k]['title'] = $title; }
  606. if ($link) { $aColumns[$k]['link'] = $link; }
  607. if ($link_extra) { $aColumns[$k]['link_extra'] = $link_extra; }
  608. if ($onclick) { $aColumns[$k]['onclick'] = $onclick; }
  609. if ($target) { $aColumns[$k]['target'] = $target; }
  610. $aColumns[$k]['value'] = $value;
  611. }
  612. /* columns which will not be displayed but should be inspected
  613. because the highlight list contains rules with those columns */
  614. foreach ($aExtraHighLightColumns as $v) {
  615. if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
  616. $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
  617. }
  618. }
  619. $aFormattedMessages[$iUid]['columns'] = $aColumns;
  620. } else {
  621. break;
  622. }
  623. }
  624. return $aFormattedMessages;
  625. }
  626. /**
  627. * Sets the row color if the provided column value pair matches a hightlight rule
  628. *
  629. * @param string $sCol column name
  630. * @param string $sVal column value
  631. * @param array $highlight_list highlight rules
  632. * @param array $aFormat (reference) array where row color info is stored
  633. * @return bool match found
  634. * @since 1.5.1
  635. * @author Marc Groot Koerkamp
  636. */
  637. function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
  638. if (!is_array($highlight_list) && count($highlight_list) == 0) {
  639. return false;
  640. }
  641. $hlt_color = false;
  642. $sCol = strtoupper($sCol);
  643. foreach ($highlight_list as $highlight_list_part) {
  644. if (trim($highlight_list_part['value'])) {
  645. $high_val = strtolower($highlight_list_part['value']);
  646. $match_type = strtoupper($highlight_list_part['match_type']);
  647. if($match_type == 'TO_CC') {
  648. if ($sCol == 'TO' || $sCol == 'CC') {
  649. $match_type = $sCol;
  650. } else {
  651. continue;
  652. }
  653. } else {
  654. if ($match_type != $sCol) {
  655. continue;
  656. }
  657. }
  658. if (strpos(strtolower($sVal),$high_val) !== false) {
  659. $hlt_color = $highlight_list_part['color'];
  660. break;
  661. }
  662. }
  663. }
  664. if ($hlt_color) {
  665. // Bug in highlight color???
  666. if ($hlt_color{0} != '#') {
  667. $hlt_color = '#'. $hlt_color;
  668. }
  669. $aFormat['row']['color'] = $hlt_color;
  670. return true;
  671. } else {
  672. return false;
  673. }
  674. }
  675. function setUserPref($username, $pref, $value) {
  676. global $data_dir;
  677. setPref($data_dir,$username,$pref,$value);
  678. }
  679. /**
  680. * Execute the sorting for a mailbox
  681. *
  682. * @param resource $imapConnection Imap connection
  683. * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
  684. * @return int $error (reference) Error number
  685. * @private
  686. * @since 1.5.1
  687. * @author Marc Groot Koerkamp
  688. */
  689. function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
  690. $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
  691. $bDirection = !($aMailbox['SORT'] % 2);
  692. $error = 0;
  693. if (!$aMailbox['SEARCH'][$iSetIndx]) {
  694. $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
  695. }
  696. if (($aMailbox['SORT'] & SQSORT_THREAD) && sqimap_capability($imapConnection,'THREAD')) {
  697. $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
  698. if ($aRes === false) {
  699. $aMailbox['SORT'] -= SQSORT_THREAD;
  700. $error = 1; // fix me, define an error code;
  701. } else {
  702. $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
  703. $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
  704. }
  705. } else if ($aMailbox['SORT'] === SQSORT_NONE) {
  706. $id = sqimap_run_search($imapConnection, 'ALL' , '');
  707. if ($id === false) {
  708. $error = 1; // fix me, define an error code
  709. } else {
  710. $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
  711. $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
  712. }
  713. } else {
  714. if (sqimap_capability($imapConnection,'SORT')) {
  715. $sSortField = _getSortField($aMailbox['SORT'],true);
  716. $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
  717. if ($id === false) {
  718. $error = 1; // fix me, define an error code
  719. } else {
  720. $aMailbox['UIDSET'][$iSetIndx] = $id;
  721. }
  722. } else {
  723. $id = NULL;
  724. if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
  725. $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
  726. }
  727. $sSortField = _getSortField($aMailbox['SORT'],false);
  728. $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
  729. }
  730. }
  731. return $error;
  732. }
  733. /**
  734. * Does the $srt $_GET var to field mapping
  735. *
  736. * @param int $srt Field to sort on
  737. * @param bool $bServerSort Server sorting is true
  738. * @return string $sSortField Field to sort on
  739. * @since 1.5.1
  740. * @private
  741. */
  742. function _getSortField($sort,$bServerSort) {
  743. switch($sort) {
  744. case SQSORT_NONE:
  745. $sSortField = 'UID';
  746. break;
  747. case SQSORT_DATE_ASC:
  748. case SQSORT_DATE_DESC:
  749. $sSortField = 'DATE';
  750. break;
  751. case SQSORT_FROM_ASC:
  752. case SQSORT_FROM_DESC:
  753. $sSortField = 'FROM';
  754. break;
  755. case SQSORT_SUBJ_ASC:
  756. case SQSORT_SUBJ_DESC:
  757. $sSortField = 'SUBJECT';
  758. break;
  759. case SQSORT_SIZE_ASC:
  760. case SQSORT_SIZE_DESC:
  761. $sSortField = ($bServerSort) ? 'SIZE' : 'RFC822.SIZE';
  762. break;
  763. case SQSORT_TO_ASC:
  764. case SQSORT_TO_DESC:
  765. $sSortField = 'TO';
  766. break;
  767. case SQSORT_CC_ASC:
  768. case SQSORT_CC_DESC:
  769. $sSortField = 'CC';
  770. break;
  771. case SQSORT_INT_DATE_ASC:
  772. case SQSORT_INT_DATE_DESC:
  773. $sSortField = ($bServerSort) ? 'ARRIVAL' : 'INTERNALDATE';
  774. break;
  775. case SQSORT_THREAD:
  776. break;
  777. default: $sSortField = 'UID';
  778. break;
  779. }
  780. return $sSortField;
  781. }
  782. /**
  783. * This function is a utility function for setting which headers should be
  784. * fetched. It takes into account the highlight list which requires extra
  785. * headers to be fetch in order to make those rules work. It's called before
  786. * the headers are fetched which happens in showMessagesForMailbox and when
  787. * the next and prev links in read_body.php are used.
  788. *
  789. * @param array $aMailbox associative array with mailbox related vars
  790. * @param array $aProps
  791. * @return void
  792. * @since 1.5.1
  793. */
  794. function calcFetchColumns(&$aMailbox, &$aProps) {
  795. $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  796. $aColumnsDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  797. $aFetchColumns = $aColumnsDesc;
  798. if (isset($aFetchColumns[SQM_COL_CHECK])) {
  799. unset($aFetchColumns[SQM_COL_CHECK]);
  800. }
  801. /*
  802. * Before we fetch the message headers, check if we need to fetch extra columns
  803. * to make the message highlighting work
  804. */
  805. if (is_array($highlight_list) && count($highlight_list)) {
  806. $aHighlightColumns = array();
  807. foreach ($highlight_list as $highlight_list_part) {
  808. if (trim($highlight_list_part['value'])) {
  809. $match_type = strtoupper($highlight_list_part['match_type']);
  810. switch ($match_type) {
  811. case 'TO_CC':
  812. $aHighlightColumns[SQM_COL_TO] = true;
  813. $aHighlightColumns[SQM_COL_CC] = true;
  814. break;
  815. case 'TO': $aHighlightColumns[SQM_COL_TO] = true; break;
  816. case 'CC': $aHighlightColumns[SQM_COL_CC] = true; break;
  817. case 'FROM': $aHighlightColumns[SQM_COL_FROM] = true; break;
  818. case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ] = true; break;
  819. }
  820. }
  821. }
  822. $aExtraColumns = array();
  823. foreach ($aHighlightColumns as $k => $v) {
  824. if (!isset($aFetchColumns[$k])) {
  825. $aExtraColumns[] = $k;
  826. $aFetchColumns[$k] = true;
  827. }
  828. }
  829. if (count($aExtraColumns)) {
  830. $aProps['extra_columns'] = $aExtraColumns;
  831. }
  832. }
  833. $aMailbox['FETCHHEADERS'] = array_keys($aFetchColumns);
  834. }
  835. /**
  836. * This function loops through a group of messages in the mailbox
  837. * and shows them to the user.
  838. *
  839. * @param resource $imapConnection
  840. * @param array $aMailbox associative array with mailbox related vars
  841. * @param array $aProps
  842. * @param int $iError error code, 0 is no error
  843. */
  844. function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
  845. global $PHP_SELF;
  846. global $boxes, $show_copy_buttons;
  847. $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  848. $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ? $aProps['config']['fancy_index_highlite'] : true;
  849. $aColumnsDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  850. $iAccount = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
  851. $sMailbox = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
  852. $sTargetModule = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
  853. $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ? $aProps['config']['show_flag_buttons'] : true;
  854. /* allows to control copy button in function call. If array key is not set, code follows user preferences */
  855. if (isset($aProps['config']['show_copy_buttons']))
  856. $show_copy_buttons = $aProps['config']['show_copy_buttons'];
  857. $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ? $aProps['config']['lastTargetMailbox'] : '';
  858. $aOrder = array_keys($aProps['columns']);
  859. $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
  860. ? $aProps['config']['trash_folder'] : false;
  861. $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
  862. ? $aProps['config']['sent_folder'] : false;
  863. $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
  864. ? $aProps['config']['draft_folder'] : false;
  865. $page_selector = (isset($aProps['config']['page_selector'])) ? $aProps['config']['page_selector'] : false;
  866. $page_selector_max = (isset($aProps['config']['page_selector_max'])) ? $aProps['config']['page_selector_max'] : 10;
  867. $color = $aProps['config']['color'];
  868. /*
  869. * Form ID
  870. */
  871. static $iFormId;
  872. if (!isset($iFormId)) {
  873. $iFormId=1;
  874. } else {
  875. ++$iFormId;
  876. }
  877. // store the columns to fetch so we can pick them up in read_body
  878. // where we validate the cache.
  879. calcFetchColumns($aMailbox ,$aProps);
  880. $iError = fetchMessageHeaders($imapConnection, $aMailbox);
  881. if ($iError) {
  882. return array();
  883. } else {
  884. $aMessages = prepareMessageList($aMailbox, $aProps);
  885. }
  886. $iSetIndx = $aMailbox['SETINDEX'];
  887. $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
  888. $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
  889. $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
  890. $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
  891. $iEnd = min ( $iEnd, $iNumberOfMessages );
  892. $php_self = $PHP_SELF;
  893. $urlMailbox = urlencode($aMailbox['NAME']);
  894. if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
  895. $source_url = $regs[1];
  896. } else {
  897. $source_url = $php_self;
  898. }
  899. $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&amp;account='.$aMailbox['ACCOUNT'];
  900. $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
  901. $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
  902. $baseurl .= '&amp;where=' . $where . '&amp;what=' . $what;
  903. /* build thread sorting links */
  904. $newsort = $aMailbox['SORT'];
  905. if (sqimap_capability($imapConnection,'THREAD')) {
  906. if ($aMailbox['SORT'] & SQSORT_THREAD) {
  907. $newsort -= SQSORT_THREAD;
  908. $thread_name = _("Unthread View");
  909. } else {
  910. $thread_name = _("Thread View");
  911. $newsort = $aMailbox['SORT'] + SQSORT_THREAD;
  912. }
  913. $thread_link_uri = $baseurl . '&amp;srt=' . $newsort
  914. . '&amp;startMessage=1';
  915. } else {
  916. $thread_link_uri ='';
  917. $thread_name = '';
  918. }
  919. $sort = $aMailbox['SORT'];
  920. /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
  921. /* future admin control over displayable buttons */
  922. $aAdminControl = array(
  923. 'markUnflagged' => 1,
  924. 'markFlagged' => 1,
  925. 'markRead' => 1,
  926. 'markUnread' => 1,
  927. 'forward' => 1,
  928. 'delete' => 1,
  929. 'undeleteButton'=> 1,
  930. 'bypass_trash' => 1,
  931. 'expungeButton' => 1,
  932. 'moveButton' => 1,
  933. 'copyButton' => 1
  934. );
  935. /* user prefs control */
  936. $aUserControl = array (
  937. 'markUnflagged' => $show_flag_buttons,
  938. 'markFlagged' => $show_flag_buttons,
  939. 'markRead' => 1,
  940. 'markUnread' => 1,
  941. 'forward' => 1,
  942. 'delete' => 1,
  943. 'undeleteButton'=> 1,
  944. 'bypass_trash' => 1,
  945. 'expungeButton' => 1,
  946. 'moveButton' => 1,
  947. 'copyButton' => $show_copy_buttons
  948. );
  949. $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
  950. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
  951. $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  952. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
  953. $trash_folder) ? true : false; //
  954. $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  955. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ? true : false;
  956. $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ? true : false;
  957. $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  958. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
  959. /* Button options that depend on IMAP server and selected folder */
  960. $aImapControl = array (
  961. 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
  962. 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
  963. 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
  964. 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
  965. 'forward' => 1,
  966. 'delete' => $showDelete,
  967. 'undeleteButton'=> $showUndelete,
  968. 'bypass_trash' => $showByPassTrash,
  969. 'expungeButton' => $showExpunge,
  970. 'moveButton' => $showMove,
  971. 'copyButton' => 1
  972. );
  973. /* Button strings */
  974. $aButtonStrings = array(
  975. 'markUnflagged' => _("Unflag"),
  976. 'markFlagged' => _("Flag"),
  977. 'markRead' => _("Read"),
  978. 'markUnread' => _("Unread"),
  979. 'forward' => _("Forward"),
  980. 'delete' => _("Delete"),
  981. 'undeleteButton' => _("Undelete"),
  982. 'bypass_trash' => _("Bypass Trash"),
  983. 'expungeButton' => _("Expunge"),
  984. 'moveButton' => _("Move"),
  985. 'copyButton' => _("Copy")
  986. );
  987. /**
  988. * Register buttons in order to an array
  989. * The key is the "name", the first element of the value array is the "value", second argument is the type.
  990. */
  991. $aFormElements = array();
  992. foreach($aAdminControl as $k => $v) {
  993. if ($v & $aUserControl[$k] & $aImapControl[$k]) {
  994. switch ($k) {
  995. case 'markUnflagged':
  996. case 'markFlagged':
  997. case 'markRead':
  998. case 'markUnread':
  999. case 'delete':
  1000. case 'undeleteButton':
  1001. case 'expungeButton':
  1002. case 'forward':
  1003. $aFormElements[$k]
  1004. = array('value' => $aButtonStrings[$k], 'type' => 'submit');
  1005. break;
  1006. case 'bypass_trash':
  1007. $aFormElements[$k]
  1008. = array('value' => $aButtonStrings[$k], 'type' => 'checkbox');
  1009. break;
  1010. case 'moveButton':
  1011. case 'copyButton':
  1012. $aFormElements['targetMailbox']
  1013. = array('options_list' => sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),
  1014. 'type' => 'select');
  1015. $aFormElements['mailbox']
  1016. = array('value' => $aMailbox['NAME'], 'type' => 'hidden');
  1017. $aFormElements['startMessage']
  1018. = array('value' => $aMailbox['PAGEOFFSET'], 'type' => 'hidden');
  1019. $aFormElements[$k]
  1020. = array('value' => $aButtonStrings[$k], 'type' => 'submit');
  1021. break;
  1022. }
  1023. }
  1024. $aFormElements['account'] = array('value' => $iAccount,'type' => 'hidden');
  1025. }
  1026. do_hook('message_list_controls', $aFormElements);
  1027. /*
  1028. * This is the beginning of the message list table.
  1029. * It wraps around all messages
  1030. */
  1031. $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
  1032. $form_name = "FormMsgs" . $safe_name;
  1033. //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
  1034. $align = array('left' => 'left', 'right' => 'right');
  1035. //}
  1036. //sm_print_r($align);
  1037. /* finally set the template vars */
  1038. // FIXME, before we support multiple templates we must review the names of the vars
  1039. // BUMP!
  1040. $aTemplate['color'] = $color;
  1041. $aTemplate['form_name'] = "FormMsgs" . $safe_name;
  1042. $aTemplate['form_id'] = 'mbx_'.$iFormId;
  1043. $aTemplate['page_selector'] = $page_selector;
  1044. $aTemplate['page_selector_max'] = $page_selector_max;
  1045. $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
  1046. $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
  1047. $aTemplate['end_msg'] = $iEnd;
  1048. $aTemplate['align'] = $align;
  1049. $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
  1050. $aTemplate['aOrder'] = $aOrder;
  1051. $aTemplate['aFormElements'] = $aFormElements;
  1052. $aTemplate['sort'] = $sort;
  1053. $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
  1054. $aTemplate['baseurl'] = $baseurl;
  1055. $aTemplate['aMessages'] =& $aMessages;
  1056. $aTemplate['trash_folder'] = $trash_folder;
  1057. $aTemplate['sent_folder'] = $sent_folder;
  1058. $aTemplate['draft_folder'] = $draft_folder;
  1059. $aTemplate['thread_link_uri'] = $thread_link_uri;
  1060. $aTemplate['thread_name'] = $thread_name;
  1061. $aTemplate['php_self'] = str_replace('&','&amp;',$php_self);
  1062. $aTemplate['mailbox'] = $sMailbox;
  1063. //FIXME: javascript_on is always assigned to the template object in places like init.php; is there some reason to reassign it here? is there some chance that it was changed? if not, please remove this line!
  1064. $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ? $aProps['config']['javascript_on'] : false;
  1065. $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false;
  1066. $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ? $aProps['config']['icon_theme'] : false;
  1067. $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ? $aProps['config']['use_icons'] : false;
  1068. $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ? $aProps['config']['alt_index_colors'] : false;
  1069. $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
  1070. /**
  1071. * Set up sort possibilities; one could argue that this is best
  1072. * placed in the template, but most template authors won't understand
  1073. * or need to understand it, so some advanced templates can override
  1074. * it if they do something different.
  1075. */
  1076. if (!($aTemplate['sort'] & SQSORT_THREAD) && $aTemplate['enablesort']) {
  1077. $aTemplate['aSortSupported']
  1078. = array(SQM_COL_SUBJ => array(SQSORT_SUBJ_ASC , SQSORT_SUBJ_DESC),
  1079. SQM_COL_DATE => array(SQSORT_DATE_DESC , SQSORT_DATE_ASC),
  1080. SQM_COL_INT_DATE => array(SQSORT_INT_DATE_DESC, SQSORT_INT_DATE_ASC),
  1081. SQM_COL_FROM => array(SQSORT_FROM_ASC , SQSORT_FROM_DESC),
  1082. SQM_COL_TO => array(SQSORT_TO_ASC , SQSORT_TO_DESC),
  1083. SQM_COL_CC => array(SQSORT_CC_ASC , SQSORT_CC_DESC),
  1084. SQM_COL_SIZE => array(SQSORT_SIZE_ASC , SQSORT_SIZE_DESC));
  1085. } else {
  1086. $aTemplate['aSortSupported'] = array();
  1087. }
  1088. /**
  1089. * Figure out which columns should serve as labels for checkbox:
  1090. * we try to grab the two columns before and after the checkbox,
  1091. * except the subject column, since it is the link that opens
  1092. * the message view
  1093. *
  1094. * if $javascript_on is set, then the highlighting code takes
  1095. * care of this; just skip it
  1096. *
  1097. * This code also might be more appropriate in a template file, but
  1098. * we are moving this complex stuff out of the way of template
  1099. * authors; advanced template sets are always free to override
  1100. * the resultant values.
  1101. *
  1102. */
  1103. $show_label_columns = array();
  1104. $index_order_part = array();
  1105. if (!($aTemplate['javascript_on'] && $aTemplate['fancy_index_highlite'])) {
  1106. $get_next_two = 0;
  1107. $last_order_part = 0;
  1108. $last_last_order_part = 0;
  1109. foreach ($aTemplate['aOrder'] as $index_order_part) {
  1110. if ($index_order_part == SQM_COL_CHECK) {
  1111. $get_next_two = 1;
  1112. if ($last_last_order_part != SQM_COL_SUBJ)
  1113. $show_label_columns[] = $last_last_order_part;
  1114. if ($last_order_part != SQM_COL_SUBJ)
  1115. $show_label_columns[] = $last_order_part;
  1116. } else if ($get_next_two > 0 && $get_next_two < 3 && $index_order_part != SQM_COL_SUBJ) {
  1117. $show_label_columns[] = $index_order_part;
  1118. $get_next_two++;
  1119. }
  1120. $last_last_order_part = $last_order_part;
  1121. $last_order_part = $index_order_part;
  1122. }
  1123. }
  1124. $aTemplate['show_label_columns'] = $show_label_columns;
  1125. return $aTemplate;
  1126. }
  1127. /**
  1128. * Truncates a string and take care of html encoded characters
  1129. *
  1130. * @param string $s string to truncate
  1131. * @param int $iTrimAt Trim at nn characters
  1132. * @return string Trimmed string
  1133. */
  1134. function truncateWithEntities($s, $iTrimAt) {
  1135. global $languages, $squirrelmail_language;
  1136. $ent_strlen = strlen($s);
  1137. if (($iTrimAt <= 0) || ($ent_strlen <= $iTrimAt))
  1138. return $s;
  1139. if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  1140. function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
  1141. return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $s, $iTrimAt);
  1142. } else {
  1143. /*
  1144. * see if this is entities-encoded string
  1145. * If so, Iterate through the whole string, find out
  1146. * the real number of characters, and if more
  1147. * than $iTrimAt, substr with an updated trim value.
  1148. */
  1149. $trim_val = $iTrimAt;
  1150. $ent_offset = 0;
  1151. $ent_loc = 0;
  1152. while ( $ent_loc < $trim_val && (($ent_loc = strpos($s, '&', $ent_offset)) !== false) &&
  1153. (($ent_loc_end = strpos($s, ';', $ent_loc+3)) !== false) ) {
  1154. $trim_val += ($ent_loc_end-$ent_loc);
  1155. $ent_offset = $ent_loc_end+1;
  1156. }
  1157. if (($trim_val > $iTrimAt) && ($ent_strlen > $trim_val) && (strpos($s,';',$trim_val) < ($trim_val + 6))) {
  1158. $i = strpos($s,';',$trim_val);
  1159. if ($i !== false) {
  1160. $trim_val = strpos($s,';',$trim_val)+1;
  1161. }
  1162. }
  1163. // only print '...' when we're actually dropping part of the subject
  1164. if ($ent_strlen <= $trim_val)
  1165. return $s;
  1166. }
  1167. return substr_replace($s, '...', $trim_val);
  1168. }
  1169. /**
  1170. * This should go in imap_mailbox.php
  1171. * @param string $mailbox
  1172. */
  1173. function handleAsSent($mailbox) {
  1174. global $handleAsSent_result;
  1175. /* First check if this is the sent or draft folder. */
  1176. $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
  1177. /* Then check the result of the handleAsSent hook. */
  1178. do_hook('check_handleAsSent_result', $mailbox);
  1179. /* And return the result. */
  1180. return $handleAsSent_result;
  1181. }
  1182. /**
  1183. * Process messages list form and handle the cache gracefully. If $sButton and
  1184. * $aUid are provided as argument then you can fake a message list submit and
  1185. * use it i.e. in read_body.php for del move next and update the cache
  1186. *
  1187. * @param resource $imapConnection imap connection
  1188. * @param array $aMailbox (reference) cached mailbox
  1189. * @param string $sButton fake a submit button
  1190. * @param array $aUid fake the $msg array
  1191. * @return string $sError error string in case of an error
  1192. * @since 1.5.1
  1193. * @author Marc Groot Koerkamp
  1194. */
  1195. function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
  1196. /* incoming formdata */
  1197. $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_POST)) ? 'move' : $sButton;
  1198. $sButton = (sqgetGlobalVar('copyButton', $sTmp, SQ_POST)) ? 'copy' : $sButton;
  1199. $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_POST)) ? 'expunge' : $sButton;
  1200. $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_POST)) ? 'forward' : $sButton;
  1201. $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_POST)) ? 'setDeleted' : $sButton;
  1202. $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_POST)) ? 'unsetDeleted' : $sButton;
  1203. $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_POST)) ? 'setSeen' : $sButton;
  1204. $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_POST)) ? 'unsetSeen' : $sButton;
  1205. $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_POST)) ? 'setFlagged' : $sButton;
  1206. $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_POST)) ? 'unsetFlagged' : $sButton;
  1207. sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
  1208. sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST);
  1209. sqgetGlobalVar('msg', $msg, SQ_POST);
  1210. if (sqgetGlobalVar('account', $iAccount, SQ_POST) === false) {
  1211. $iAccount = 0;
  1212. }
  1213. $sError = '';
  1214. $mailbox = $aMailbox['NAME'];
  1215. /* retrieve the check boxes */
  1216. $aUid = (isset($msg) && is_array($msg)) ? array_values($msg) : $aUid;
  1217. if (count($aUid) && $sButton != 'expunge') {
  1218. $aUpdatedMsgs = false;
  1219. $bExpunge = false;
  1220. switch ($sButton) {
  1221. case 'setDeleted':
  1222. // check if id exists in case we come from read_body
  1223. if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
  1224. !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
  1225. break;
  1226. }
  1227. $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
  1228. $bExpunge = true;
  1229. //}
  1230. break;
  1231. case 'unsetDeleted':
  1232. case 'setSeen':
  1233. case 'unsetSeen':
  1234. case 'setFlagged':
  1235. case 'unsetFlagged':
  1236. // get flag
  1237. $sFlag = (substr($sButton,0,3) == 'set') ? '\\'.substr($sButton,3) : '\\'.substr($sButton,5);
  1238. $bSet = (substr($sButton,0,3) == 'set') ? true : false;
  1239. $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
  1240. break;
  1241. case 'move':
  1242. $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox,true,$mailbox);
  1243. sqsession_register($targetMailbox,'lastTargetMailbox');
  1244. $bExpunge = true;
  1245. break;
  1246. case 'copy':
  1247. // sqimap_msgs_list_copy returns true or false.
  1248. // If error happens - fourth argument handles it inside function.
  1249. sqimap_msgs_list_copy($imapConnection,$aUid,$targetMailbox,true);
  1250. sqsession_register($targetMailbox,'lastTargetMailbox');
  1251. break;
  1252. case 'forward':
  1253. $aMsgHeaders = array();
  1254. foreach ($aUid as $iUid) {
  1255. $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
  1256. }
  1257. if (count($aMsgHeaders)) {
  1258. $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
  1259. // dirty hack, add info to $aMailbox
  1260. $aMailbox['FORWARD_SESSION'] = $composesession;
  1261. }
  1262. break;
  1263. default:
  1264. // Hook for plugin buttons
  1265. do_hook('mailbox_display_button_action', $aUid);
  1266. break;
  1267. }
  1268. /**
  1269. * Updates messages is an array containing the result of the untagged
  1270. * fetch responses send by the imap server due to a flag change. That
  1271. * response is parsed in a array with msg arrays by the parseFetch function
  1272. */
  1273. if ($aUpdatedMsgs) {
  1274. // Update the message headers cache
  1275. $aDeleted = array();
  1276. foreach ($aUpdatedMsgs as $iUid => $aMsg) {
  1277. if (isset($aMsg['FLAGS'])) {
  1278. /**
  1279. * Only update the cached headers if the header is
  1280. * cached.
  1281. */
  1282. if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
  1283. $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
  1284. }
  1285. /**
  1286. * Count the messages with the \Delete flag set so we can determine
  1287. * if the number of expunged messages equals the number of flagged
  1288. * messages for deletion.
  1289. */
  1290. if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
  1291. $aDeleted[] = $iUid;
  1292. }
  1293. }
  1294. }
  1295. if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
  1296. $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
  1297. {
  1298. if (count($aDeleted) != $iExpungedMessages) {
  1299. // there are more messages deleted permanently then we expected
  1300. // invalidate the cache
  1301. $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
  1302. $aMailbox['MSG_HEADERS'] = false;
  1303. } else {
  1304. // remove expunged messages from cache
  1305. $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
  1306. if (is_array($aUidSet)) {
  1307. // create a UID => array index temp array
  1308. $aUidSetDummy = array_flip($aUidSet);
  1309. foreach ($aDeleted as $iUid) {
  1310. // get the id as well in case of SQM_SORT_NONE
  1311. if ($aMailbox['SORT'] == SQSORT_NONE) {
  1312. $aMailbox['ID'] = false;
  1313. //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
  1314. //unset($aMailbox['ID'][$iId]);
  1315. }
  1316. // unset the UID and message header
  1317. unset($aUidSetDummy[$iUid]);
  1318. unset($aMailbox['MSG_HEADERS'][$iUid]);
  1319. }
  1320. $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
  1321. }
  1322. }
  1323. // update EXISTS info
  1324. if ($iExpungedMessages) {
  1325. $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
  1326. $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
  1327. }
  1328. if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
  1329. $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
  1330. $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
  1331. $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
  1332. }
  1333. }
  1334. }
  1335. } else {
  1336. if ($sButton == 'expunge') {
  1337. /**
  1338. * on expunge we do not know which messages will be deleted
  1339. * so it's useless to try to sync the cache
  1340. *
  1341. * Close the mailbox so we do not need to parse the untagged expunge
  1342. * responses which do not contain uid info.
  1343. * NB: Closing a mailbox is faster then expunge because the imap
  1344. * server does not need to generate the untagged expunge responses
  1345. */
  1346. sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
  1347. $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
  1348. } else {
  1349. if ($sButton) {
  1350. $sError = _("No messages were selected.");
  1351. }
  1352. }
  1353. }
  1354. return $sError;
  1355. }
  1356. /**
  1357. * Attach messages to a compose session
  1358. *
  1359. * @param resource $imapConnection imap connection
  1360. * @param array $aMsgHeaders
  1361. * @return int $composesession unique compose_session_id where the attached messages belong to
  1362. * @author Marc Groot Koerkamp
  1363. */
  1364. function attachSelectedMessages($imapConnection,$aMsgHeaders) {
  1365. sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
  1366. sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
  1367. if (!isset($compose_messages)|| is_null($compose_messages)) {
  1368. $compose_messages = array();
  1369. sqsession_register($compose_messages,'compose_messages');
  1370. }
  1371. if (!$composesession) {
  1372. $composesession = 1;
  1373. sqsession_register($composesession,'composesession');
  1374. } else {
  1375. $composesession++;
  1376. sqsession_register($composesession,'composesession');
  1377. }
  1378. $composeMessage = new Message();
  1379. $rfc822_header = new Rfc822Header();
  1380. $composeMessage->rfc822_header = $rfc822_header;
  1381. $composeMessage->reply_rfc822_header = '';
  1382. foreach($aMsgHeaders as $iUid => $aMsgHeader) {
  1383. /**
  1384. * Retrieve the full message
  1385. */
  1386. $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
  1387. if ($response == 'OK') {
  1388. $subject = (isset($aMsgHeader['subject'])) ? $aMsgHeader['subject'] : $iUid;
  1389. array_shift($body_a);
  1390. array_pop($body_a);
  1391. $body = implode('', $body_a);
  1392. $body .= "\r\n";
  1393. $filename = sq_get_attach_tempfile();
  1394. $fp = fopen($filename, 'wb');
  1395. fwrite ($fp, $body);
  1396. fclose($fp);
  1397. $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
  1398. $filename);
  1399. }
  1400. }
  1401. $compose_messages[$composesession] = $composeMessage;
  1402. sqsession_register($compose_messages,'compose_messages');
  1403. return $composesession;
  1404. }