mailbox_display.php 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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 1999-2017 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, $preselected;
  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 .= sm_encode_html_special_chars($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 .= sm_encode_html_special_chars($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 = sm_truncate_string($sTmp, $aColumnDesc[$k]['truncate'], '...', TRUE);
  524. if ($sTrunc != $sTmp) {
  525. if (!$title) {
  526. $title = $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) ? (substr($sTmp, 0, 6) == '&quot;' && substr($sTmp, -6) == '&quot;' ? substr(substr($sTmp, 0, -6), 6) : $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 = sm_truncate_string($value, $aColumnDesc[$k]['truncate']-$iIndent, '...', TRUE);
  549. // drop any double spaces since these will be displayed in the title
  550. // Nah, it's nice to always have a roll-over
  551. //$title = ($sTmp != $value) ? preg_replace('/\s{2,}/', ' ', $value) : '';
  552. $title = preg_replace('/\s{2,}/', ' ', $value);
  553. $value = $sTmp;
  554. }
  555. /* generate the link to the message */
  556. if ($aQuery) {
  557. // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
  558. $link = $sTargetModule.'.php?' . implode('&amp;',$aQuery);
  559. // see top of this function for which attributes are available
  560. // in the global scope for plugin use (like $link, $target,
  561. // $onclick, $link_extra, $title, and so forth)
  562. // plugins are responsible for sharing nicely (such as for
  563. // setting the target, etc)
  564. $temp = array(&$iPageOffset, &$sSearch, &$aSearch, $aMsg);
  565. do_hook('subject_link', $temp);
  566. }
  567. $value = (trim($value)) ? $value : _("(no subject)");
  568. /* add thread indentation */
  569. $aColumns[$k]['indent'] = $iIndent;
  570. break;
  571. case SQM_COL_SIZE:
  572. $value = show_readable_size($value);
  573. break;
  574. case SQM_COL_DATE:
  575. case SQM_COL_INT_DATE:
  576. $value = getTimeStamp(explode(' ',trim($value)));
  577. $title = getDateString($value, TRUE);
  578. $value = getDateString($value);
  579. break;
  580. case SQM_COL_FLAGS:
  581. $aFlagColumn = array('seen' => false,
  582. 'deleted'=>false,
  583. 'answered'=>false,
  584. 'forwarded'=>false,
  585. 'flagged' => false,
  586. 'draft' => false);
  587. if(!is_array($value)) $value = array();
  588. foreach ($value as $sFlag => $v) {
  589. switch ($sFlag) {
  590. case '\\seen' : $aFlagColumn['seen'] = true; break;
  591. case '\\deleted' : $aFlagColumn['deleted'] = true; break;
  592. case '\\answered': $aFlagColumn['answered'] = true; break;
  593. case '$forwarded': $aFlagColumn['forwarded'] = true; break;
  594. case '\\flagged' : $aFlagColumn['flagged'] = true; break;
  595. case '\\draft' : $aFlagColumn['draft'] = true; break;
  596. default: break;
  597. }
  598. }
  599. $value = $aFlagColumn;
  600. break;
  601. case SQM_COL_PRIO:
  602. $value = ($value) ? (int) $value : 3;
  603. break;
  604. case SQM_COL_ATTACHMENT:
  605. $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ? true : false;
  606. break;
  607. case SQM_COL_CHECK:
  608. $value = ($checkall || in_array($iUid, $preselected));
  609. break;
  610. default : break;
  611. }
  612. if ($title) { $aColumns[$k]['title'] = $title; }
  613. if ($link) { $aColumns[$k]['link'] = $link; }
  614. if ($link_extra) { $aColumns[$k]['link_extra'] = $link_extra; }
  615. if ($onclick) { $aColumns[$k]['onclick'] = $onclick; }
  616. if ($target) { $aColumns[$k]['target'] = $target; }
  617. $aColumns[$k]['value'] = $value;
  618. }
  619. /* columns which will not be displayed but should be inspected
  620. because the highlight list contains rules with those columns */
  621. foreach ($aExtraHighLightColumns as $v) {
  622. if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
  623. $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
  624. }
  625. }
  626. $aFormattedMessages[$iUid]['columns'] = $aColumns;
  627. } else {
  628. break;
  629. }
  630. }
  631. return $aFormattedMessages;
  632. }
  633. /**
  634. * Sets the row color if the provided column value pair matches a hightlight rule
  635. *
  636. * @param string $sCol column name
  637. * @param string $sVal column value
  638. * @param array $highlight_list highlight rules
  639. * @param array $aFormat (reference) array where row color info is stored
  640. * @return bool match found
  641. * @since 1.5.1
  642. * @author Marc Groot Koerkamp
  643. */
  644. function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
  645. if (!is_array($highlight_list) && count($highlight_list) == 0) {
  646. return false;
  647. }
  648. $hlt_color = false;
  649. $sCol = strtoupper($sCol);
  650. foreach ($highlight_list as $highlight_list_part) {
  651. if (trim($highlight_list_part['value'])) {
  652. $high_val = strtolower($highlight_list_part['value']);
  653. $match_type = strtoupper($highlight_list_part['match_type']);
  654. if($match_type == 'TO_CC') {
  655. if ($sCol == 'TO' || $sCol == 'CC') {
  656. $match_type = $sCol;
  657. } else {
  658. continue;
  659. }
  660. } else {
  661. if ($match_type != $sCol) {
  662. continue;
  663. }
  664. }
  665. if (strpos(strtolower($sVal),$high_val) !== false) {
  666. $hlt_color = $highlight_list_part['color'];
  667. break;
  668. }
  669. }
  670. }
  671. if ($hlt_color) {
  672. // Bug in highlight color???
  673. if ($hlt_color{0} != '#') {
  674. $hlt_color = '#'. $hlt_color;
  675. }
  676. $aFormat['row']['color'] = $hlt_color;
  677. return true;
  678. } else {
  679. return false;
  680. }
  681. }
  682. function setUserPref($username, $pref, $value) {
  683. global $data_dir;
  684. setPref($data_dir,$username,$pref,$value);
  685. }
  686. /**
  687. * Execute the sorting for a mailbox
  688. *
  689. * @param resource $imapConnection Imap connection
  690. * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
  691. * @return int $error (reference) Error number
  692. * @private
  693. * @since 1.5.1
  694. * @author Marc Groot Koerkamp
  695. */
  696. function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
  697. $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
  698. $bDirection = !($aMailbox['SORT'] % 2);
  699. $error = 0;
  700. if (!$aMailbox['SEARCH'][$iSetIndx]) {
  701. $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
  702. }
  703. if (($aMailbox['SORT'] & SQSORT_THREAD) && sqimap_capability($imapConnection,'THREAD')) {
  704. $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
  705. if ($aRes === false) {
  706. $aMailbox['SORT'] -= SQSORT_THREAD;
  707. $error = 1; // fix me, define an error code;
  708. } else {
  709. $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
  710. $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
  711. }
  712. } else if ($aMailbox['SORT'] === SQSORT_NONE) {
  713. $id = sqimap_run_search($imapConnection, 'ALL' , '');
  714. if ($id === false) {
  715. $error = 1; // fix me, define an error code
  716. } else {
  717. $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
  718. $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
  719. }
  720. } else {
  721. if (sqimap_capability($imapConnection,'SORT')) {
  722. $sSortField = _getSortField($aMailbox['SORT'],true);
  723. $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
  724. if ($id === false) {
  725. $error = 1; // fix me, define an error code
  726. } else {
  727. $aMailbox['UIDSET'][$iSetIndx] = $id;
  728. }
  729. } else {
  730. $id = NULL;
  731. if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
  732. $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
  733. }
  734. $sSortField = _getSortField($aMailbox['SORT'],false);
  735. $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
  736. }
  737. }
  738. return $error;
  739. }
  740. /**
  741. * Does the $srt $_GET var to field mapping
  742. *
  743. * @param int $srt Field to sort on
  744. * @param bool $bServerSort Server sorting is true
  745. * @return string $sSortField Field to sort on
  746. * @since 1.5.1
  747. * @private
  748. */
  749. function _getSortField($sort,$bServerSort) {
  750. switch($sort) {
  751. case SQSORT_NONE:
  752. $sSortField = 'UID';
  753. break;
  754. case SQSORT_DATE_ASC:
  755. case SQSORT_DATE_DESC:
  756. $sSortField = 'DATE';
  757. break;
  758. case SQSORT_FROM_ASC:
  759. case SQSORT_FROM_DESC:
  760. $sSortField = 'FROM';
  761. break;
  762. case SQSORT_SUBJ_ASC:
  763. case SQSORT_SUBJ_DESC:
  764. $sSortField = 'SUBJECT';
  765. break;
  766. case SQSORT_SIZE_ASC:
  767. case SQSORT_SIZE_DESC:
  768. $sSortField = ($bServerSort) ? 'SIZE' : 'RFC822.SIZE';
  769. break;
  770. case SQSORT_TO_ASC:
  771. case SQSORT_TO_DESC:
  772. $sSortField = 'TO';
  773. break;
  774. case SQSORT_CC_ASC:
  775. case SQSORT_CC_DESC:
  776. $sSortField = 'CC';
  777. break;
  778. case SQSORT_INT_DATE_ASC:
  779. case SQSORT_INT_DATE_DESC:
  780. $sSortField = ($bServerSort) ? 'ARRIVAL' : 'INTERNALDATE';
  781. break;
  782. case SQSORT_THREAD:
  783. break;
  784. default: $sSortField = 'UID';
  785. break;
  786. }
  787. return $sSortField;
  788. }
  789. /**
  790. * This function is a utility function for setting which headers should be
  791. * fetched. It takes into account the highlight list which requires extra
  792. * headers to be fetch in order to make those rules work. It's called before
  793. * the headers are fetched which happens in showMessagesForMailbox and when
  794. * the next and prev links in read_body.php are used.
  795. *
  796. * @param array $aMailbox associative array with mailbox related vars
  797. * @param array $aProps
  798. * @return void
  799. * @since 1.5.1
  800. */
  801. function calcFetchColumns(&$aMailbox, &$aProps) {
  802. $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  803. $aColumnsDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  804. $aFetchColumns = $aColumnsDesc;
  805. if (isset($aFetchColumns[SQM_COL_CHECK])) {
  806. unset($aFetchColumns[SQM_COL_CHECK]);
  807. }
  808. /*
  809. * Before we fetch the message headers, check if we need to fetch extra columns
  810. * to make the message highlighting work
  811. */
  812. if (is_array($highlight_list) && count($highlight_list)) {
  813. $aHighlightColumns = array();
  814. foreach ($highlight_list as $highlight_list_part) {
  815. if (trim($highlight_list_part['value'])) {
  816. $match_type = strtoupper($highlight_list_part['match_type']);
  817. switch ($match_type) {
  818. case 'TO_CC':
  819. $aHighlightColumns[SQM_COL_TO] = true;
  820. $aHighlightColumns[SQM_COL_CC] = true;
  821. break;
  822. case 'TO': $aHighlightColumns[SQM_COL_TO] = true; break;
  823. case 'CC': $aHighlightColumns[SQM_COL_CC] = true; break;
  824. case 'FROM': $aHighlightColumns[SQM_COL_FROM] = true; break;
  825. case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ] = true; break;
  826. }
  827. }
  828. }
  829. $aExtraColumns = array();
  830. foreach ($aHighlightColumns as $k => $v) {
  831. if (!isset($aFetchColumns[$k])) {
  832. $aExtraColumns[] = $k;
  833. $aFetchColumns[$k] = true;
  834. }
  835. }
  836. if (count($aExtraColumns)) {
  837. $aProps['extra_columns'] = $aExtraColumns;
  838. }
  839. }
  840. $aMailbox['FETCHHEADERS'] = array_keys($aFetchColumns);
  841. }
  842. /**
  843. * This function loops through a group of messages in the mailbox
  844. * and shows them to the user.
  845. *
  846. * @param resource $imapConnection
  847. * @param array $aMailbox associative array with mailbox related vars
  848. * @param array $aProps
  849. * @param int $iError error code, 0 is no error
  850. */
  851. function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
  852. global $PHP_SELF;
  853. global $boxes, $show_copy_buttons;
  854. $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  855. $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ? $aProps['config']['fancy_index_highlite'] : true;
  856. $aColumnsDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  857. $iAccount = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
  858. $sMailbox = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
  859. $sTargetModule = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
  860. $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ? $aProps['config']['show_flag_buttons'] : true;
  861. /* allows to control copy button in function call. If array key is not set, code follows user preferences */
  862. if (isset($aProps['config']['show_copy_buttons']))
  863. $show_copy_buttons = $aProps['config']['show_copy_buttons'];
  864. $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ? $aProps['config']['lastTargetMailbox'] : '';
  865. $aOrder = array_keys($aProps['columns']);
  866. $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
  867. ? $aProps['config']['trash_folder'] : false;
  868. $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
  869. ? $aProps['config']['sent_folder'] : false;
  870. $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
  871. ? $aProps['config']['draft_folder'] : false;
  872. $page_selector = (isset($aProps['config']['page_selector'])) ? $aProps['config']['page_selector'] : false;
  873. $page_selector_max = (isset($aProps['config']['page_selector_max'])) ? $aProps['config']['page_selector_max'] : 10;
  874. $color = $aProps['config']['color'];
  875. /*
  876. * Form ID
  877. */
  878. static $iFormId;
  879. if (!isset($iFormId)) {
  880. $iFormId=1;
  881. } else {
  882. ++$iFormId;
  883. }
  884. // store the columns to fetch so we can pick them up in read_body
  885. // where we validate the cache.
  886. calcFetchColumns($aMailbox ,$aProps);
  887. $iError = fetchMessageHeaders($imapConnection, $aMailbox);
  888. if ($iError) {
  889. return array();
  890. } else {
  891. $aMessages = prepareMessageList($aMailbox, $aProps);
  892. }
  893. $iSetIndx = $aMailbox['SETINDEX'];
  894. $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
  895. $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
  896. $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
  897. $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
  898. $iEnd = min ( $iEnd, $iNumberOfMessages );
  899. $php_self = $PHP_SELF;
  900. $urlMailbox = urlencode($aMailbox['NAME']);
  901. if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
  902. $source_url = $regs[1];
  903. } else {
  904. $source_url = $php_self;
  905. }
  906. $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&amp;account='.$aMailbox['ACCOUNT'] . (strpos($source_url, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : '');
  907. $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
  908. $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
  909. $baseurl .= '&amp;where=' . $where . '&amp;what=' . $what;
  910. /* build thread sorting links */
  911. $newsort = $aMailbox['SORT'];
  912. if (sqimap_capability($imapConnection,'THREAD')) {
  913. if ($aMailbox['SORT'] & SQSORT_THREAD) {
  914. $newsort -= SQSORT_THREAD;
  915. $thread_name = _("Unthread View");
  916. } else {
  917. $thread_name = _("Thread View");
  918. $newsort = $aMailbox['SORT'] + SQSORT_THREAD;
  919. }
  920. $thread_link_uri = $baseurl . '&amp;srt=' . $newsort
  921. . '&amp;startMessage=1';
  922. } else {
  923. $thread_link_uri ='';
  924. $thread_name = '';
  925. }
  926. $sort = $aMailbox['SORT'];
  927. /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
  928. /* future admin control over displayable buttons */
  929. $aAdminControl = array(
  930. 'markFlagged' => 1,
  931. 'markUnflagged' => 1,
  932. 'markRead' => 1,
  933. 'markUnread' => 1,
  934. 'forward' => 1,
  935. 'delete' => 1,
  936. 'undeleteButton'=> 1,
  937. 'bypass_trash' => 1,
  938. 'expungeButton' => 1,
  939. 'moveButton' => 1,
  940. 'copyButton' => 1
  941. );
  942. /* user prefs control */
  943. $aUserControl = array (
  944. 'markFlagged' => $show_flag_buttons,
  945. 'markUnflagged' => $show_flag_buttons,
  946. 'markRead' => 1,
  947. 'markUnread' => 1,
  948. 'forward' => 1,
  949. 'delete' => 1,
  950. 'undeleteButton'=> 1,
  951. 'bypass_trash' => 1,
  952. 'expungeButton' => 1,
  953. 'moveButton' => 1,
  954. 'copyButton' => $show_copy_buttons
  955. );
  956. $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
  957. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
  958. $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  959. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
  960. $trash_folder) ? true : false; //
  961. $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  962. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) /* trash folder unrelated methinks: && !$trash_folder*/) ? true : false;
  963. $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ? true : false;
  964. $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  965. in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
  966. /* Button options that depend on IMAP server and selected folder */
  967. $aImapControl = array (
  968. 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
  969. 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
  970. 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
  971. 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
  972. 'forward' => 1,
  973. 'delete' => $showDelete,
  974. 'undeleteButton'=> $showUndelete,
  975. 'bypass_trash' => $showByPassTrash,
  976. 'expungeButton' => $showExpunge,
  977. 'moveButton' => $showMove,
  978. 'copyButton' => 1
  979. );
  980. /* Button strings */
  981. $aButtonStrings = array(
  982. 'markFlagged' => _("Flag"),
  983. 'markUnflagged' => _("Unflag"),
  984. 'markRead' => _("Read"),
  985. 'markUnread' => _("Unread"),
  986. 'forward' => _("Forward"),
  987. 'delete' => _("Delete"),
  988. 'undeleteButton' => _("Undelete"),
  989. 'bypass_trash' => _("Bypass Trash"),
  990. 'expungeButton' => _("Expunge"),
  991. 'moveButton' => _("Move"),
  992. 'copyButton' => _("Copy")
  993. );
  994. /* Button access keys */
  995. global $accesskey_mailbox_flag, $accesskey_mailbox_unflag,
  996. $accesskey_mailbox_read, $accesskey_mailbox_unread,
  997. $accesskey_mailbox_forward, $accesskey_mailbox_delete,
  998. $accesskey_mailbox_undelete, $accesskey_mailbox_bypass_trash,
  999. $accesskey_mailbox_expunge, $accesskey_mailbox_move,
  1000. $accesskey_mailbox_copy, $accesskey_mailbox_move_to;
  1001. $aButtonAccessKeys = array(
  1002. 'markFlagged' => $accesskey_mailbox_flag,
  1003. 'markUnflagged' => $accesskey_mailbox_unflag,
  1004. 'markRead' => $accesskey_mailbox_read,
  1005. 'markUnread' => $accesskey_mailbox_unread,
  1006. 'forward' => $accesskey_mailbox_forward,
  1007. 'delete' => $accesskey_mailbox_delete,
  1008. 'undeleteButton' => $accesskey_mailbox_undelete,
  1009. 'bypass_trash' => $accesskey_mailbox_bypass_trash,
  1010. 'expungeButton' => $accesskey_mailbox_expunge,
  1011. 'moveButton' => $accesskey_mailbox_move,
  1012. 'copyButton' => $accesskey_mailbox_copy,
  1013. );
  1014. /**
  1015. * Register buttons in order to an array
  1016. * The key is the "name", the first element of the value array is the "value", second argument is the type.
  1017. */
  1018. $aFormElements = array();
  1019. foreach($aAdminControl as $k => $v) {
  1020. if ($v & $aUserControl[$k] & $aImapControl[$k]) {
  1021. switch ($k) {
  1022. case 'markFlagged':
  1023. case 'markUnflagged':
  1024. case 'markRead':
  1025. case 'markUnread':
  1026. case 'delete':
  1027. case 'undeleteButton':
  1028. case 'expungeButton':
  1029. case 'forward':
  1030. $aFormElements[$k]
  1031. = array('value' => $aButtonStrings[$k], 'type' => 'submit', 'accesskey' => (isset($aButtonAccessKeys[$k]) ? $aButtonAccessKeys[$k] : 'NONE'));
  1032. break;
  1033. case 'bypass_trash':
  1034. $aFormElements[$k]
  1035. = array('value' => $aButtonStrings[$k], 'type' => 'checkbox', 'accesskey' => (isset($aButtonAccessKeys[$k]) ? $aButtonAccessKeys[$k] : 'NONE'));
  1036. break;
  1037. case 'moveButton':
  1038. case 'copyButton':
  1039. $aFormElements['targetMailbox']
  1040. = array('options_list' => sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),
  1041. 'type' => 'select',
  1042. 'accesskey' => $accesskey_mailbox_move_to);
  1043. $aFormElements['mailbox']
  1044. = array('value' => $aMailbox['NAME'], 'type' => 'hidden');
  1045. $aFormElements['startMessage']
  1046. = array('value' => $aMailbox['PAGEOFFSET'], 'type' => 'hidden');
  1047. $aFormElements[$k]
  1048. = array('value' => $aButtonStrings[$k], 'type' => 'submit', 'accesskey' => (isset($aButtonAccessKeys[$k]) ? $aButtonAccessKeys[$k] : 'NONE'));
  1049. break;
  1050. }
  1051. }
  1052. $aFormElements['account'] = array('value' => $iAccount,'type' => 'hidden');
  1053. }
  1054. do_hook('message_list_controls', $aFormElements);
  1055. /*
  1056. * This is the beginning of the message list table.
  1057. * It wraps around all messages
  1058. */
  1059. $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
  1060. $form_name = "FormMsgs" . $safe_name;
  1061. //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
  1062. $align = array('left' => 'left', 'right' => 'right');
  1063. //}
  1064. //sm_print_r($align);
  1065. /* finally set the template vars */
  1066. // FIXME, before we support multiple templates we must review the names of the vars
  1067. // BUMP!
  1068. $aTemplate['color'] = $color;
  1069. $aTemplate['form_name'] = "FormMsgs" . $safe_name;
  1070. $aTemplate['form_id'] = 'mbx_'.$iFormId;
  1071. $aTemplate['page_selector'] = $page_selector;
  1072. $aTemplate['page_selector_max'] = $page_selector_max;
  1073. $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
  1074. $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
  1075. $aTemplate['end_msg'] = $iEnd;
  1076. $aTemplate['align'] = $align;
  1077. $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
  1078. $aTemplate['aOrder'] = $aOrder;
  1079. $aTemplate['aFormElements'] = $aFormElements;
  1080. $aTemplate['sort'] = $sort;
  1081. $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
  1082. $aTemplate['baseurl'] = $baseurl;
  1083. $aTemplate['aMessages'] =& $aMessages;
  1084. $aTemplate['trash_folder'] = $trash_folder;
  1085. $aTemplate['sent_folder'] = $sent_folder;
  1086. $aTemplate['draft_folder'] = $draft_folder;
  1087. $aTemplate['thread_link_uri'] = $thread_link_uri;
  1088. $aTemplate['thread_name'] = $thread_name;
  1089. $aTemplate['php_self'] = str_replace('&','&amp;',$php_self);
  1090. $aTemplate['mailbox'] = $sMailbox;
  1091. //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!
  1092. $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ? $aProps['config']['javascript_on'] : false;
  1093. $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false;
  1094. $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ? $aProps['config']['icon_theme'] : false;
  1095. $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ? $aProps['config']['use_icons'] : false;
  1096. $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ? $aProps['config']['alt_index_colors'] : false;
  1097. $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
  1098. /**
  1099. * Set up sort possibilities; one could argue that this is best
  1100. * placed in the template, but most template authors won't understand
  1101. * or need to understand it, so some advanced templates can override
  1102. * it if they do something different.
  1103. */
  1104. if (!($aTemplate['sort'] & SQSORT_THREAD) && $aTemplate['enablesort']) {
  1105. $aTemplate['aSortSupported']
  1106. = array(SQM_COL_SUBJ => array(SQSORT_SUBJ_ASC , SQSORT_SUBJ_DESC),
  1107. SQM_COL_DATE => array(SQSORT_DATE_DESC , SQSORT_DATE_ASC),
  1108. SQM_COL_INT_DATE => array(SQSORT_INT_DATE_DESC, SQSORT_INT_DATE_ASC),
  1109. SQM_COL_FROM => array(SQSORT_FROM_ASC , SQSORT_FROM_DESC),
  1110. SQM_COL_TO => array(SQSORT_TO_ASC , SQSORT_TO_DESC),
  1111. SQM_COL_CC => array(SQSORT_CC_ASC , SQSORT_CC_DESC),
  1112. SQM_COL_SIZE => array(SQSORT_SIZE_ASC , SQSORT_SIZE_DESC));
  1113. } else {
  1114. $aTemplate['aSortSupported'] = array();
  1115. }
  1116. /**
  1117. * Figure out which columns should serve as labels for checkbox:
  1118. * we try to grab the two columns before and after the checkbox,
  1119. * except the subject column, since it is the link that opens
  1120. * the message view
  1121. *
  1122. * if $javascript_on is set, then the highlighting code takes
  1123. * care of this; just skip it
  1124. *
  1125. * This code also might be more appropriate in a template file, but
  1126. * we are moving this complex stuff out of the way of template
  1127. * authors; advanced template sets are always free to override
  1128. * the resultant values.
  1129. *
  1130. */
  1131. $show_label_columns = array();
  1132. $index_order_part = array();
  1133. if (!($aTemplate['javascript_on'] && $aTemplate['fancy_index_highlite'])) {
  1134. $get_next_two = 0;
  1135. $last_order_part = 0;
  1136. $last_last_order_part = 0;
  1137. foreach ($aTemplate['aOrder'] as $index_order_part) {
  1138. if ($index_order_part == SQM_COL_CHECK) {
  1139. $get_next_two = 1;
  1140. if ($last_last_order_part != SQM_COL_SUBJ)
  1141. $show_label_columns[] = $last_last_order_part;
  1142. if ($last_order_part != SQM_COL_SUBJ)
  1143. $show_label_columns[] = $last_order_part;
  1144. } else if ($get_next_two > 0 && $get_next_two < 3 && $index_order_part != SQM_COL_SUBJ) {
  1145. $show_label_columns[] = $index_order_part;
  1146. $get_next_two++;
  1147. }
  1148. $last_last_order_part = $last_order_part;
  1149. $last_order_part = $index_order_part;
  1150. }
  1151. }
  1152. $aTemplate['show_label_columns'] = $show_label_columns;
  1153. return $aTemplate;
  1154. }
  1155. /**
  1156. * Process messages list form and handle the cache gracefully. If $sButton and
  1157. * $aUid are provided as argument then you can fake a message list submit and
  1158. * use it i.e. in read_body.php for del move next and update the cache
  1159. *
  1160. * @param resource $imapConnection imap connection
  1161. * @param array $aMailbox (reference) cached mailbox
  1162. * @param string $sButton fake a submit button
  1163. * @param array $aUid fake the $msg array
  1164. * @param string $targetMailbox fake the target mailbox for move operations
  1165. * @param boolean $bypass_trash fake the bypass trash checkbox for delete operations
  1166. * @return string $sError error string in case of an error
  1167. * @since 1.5.1
  1168. * @author Marc Groot Koerkamp
  1169. */
  1170. function handleMessageListForm($imapConnection, &$aMailbox, $sButton='',
  1171. $aUid = array(), $targetMailbox='', $bypass_trash=NULL) {
  1172. /* incoming formdata */
  1173. $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_FORM)) ? 'move' : $sButton;
  1174. $sButton = (sqgetGlobalVar('copyButton', $sTmp, SQ_FORM)) ? 'copy' : $sButton;
  1175. $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_FORM)) ? 'expunge' : $sButton;
  1176. $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_FORM)) ? 'forward' : $sButton;
  1177. $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_FORM)) ? 'setDeleted' : $sButton;
  1178. $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_FORM)) ? 'unsetDeleted' : $sButton;
  1179. $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_FORM)) ? 'setSeen' : $sButton;
  1180. $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_FORM)) ? 'unsetSeen' : $sButton;
  1181. $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_FORM)) ? 'setFlagged' : $sButton;
  1182. $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_FORM)) ? 'unsetFlagged' : $sButton;
  1183. if (empty($targetMailbox)) sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_FORM);
  1184. if (is_null($bypass_trash)) sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_FORM);
  1185. sqgetGlobalVar('msg', $msg, SQ_FORM);
  1186. if (sqgetGlobalVar('account', $iAccount, SQ_FORM) === false) {
  1187. $iAccount = 0;
  1188. }
  1189. $sError = '';
  1190. $mailbox = $aMailbox['NAME'];
  1191. /* retrieve the check boxes */
  1192. $aUid = (isset($msg) && is_array($msg)) ? array_values($msg) : $aUid;
  1193. if (count($aUid) && $sButton != 'expunge') {
  1194. // don't do anything to any messages until we have done security check
  1195. // FIXME: not sure this code really belongs here, but there's nowhere else to put it with this architecture
  1196. sqgetGlobalVar('smtoken', $submitted_token, SQ_FORM, '');
  1197. sm_validate_security_token($submitted_token, -1, TRUE);
  1198. // make sure message UIDs are sanitized (BIGINT)
  1199. foreach ($aUid as $i => $uid)
  1200. $aUid[$i] = (preg_match('/^[0-9]+$/', $uid) ? $uid : '0');
  1201. $aUpdatedMsgs = false;
  1202. $bExpunge = false;
  1203. switch ($sButton) {
  1204. case 'setDeleted':
  1205. // check if id exists in case we come from read_body
  1206. if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
  1207. !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
  1208. break;
  1209. }
  1210. $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
  1211. $bExpunge = true;
  1212. //}
  1213. break;
  1214. case 'unsetDeleted':
  1215. case 'setSeen':
  1216. case 'unsetSeen':
  1217. case 'setFlagged':
  1218. case 'unsetFlagged':
  1219. // get flag
  1220. $sFlag = (substr($sButton,0,3) == 'set') ? '\\'.substr($sButton,3) : '\\'.substr($sButton,5);
  1221. $bSet = (substr($sButton,0,3) == 'set') ? true : false;
  1222. $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
  1223. break;
  1224. case 'move':
  1225. $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox,true,$mailbox);
  1226. sqsession_register($targetMailbox,'lastTargetMailbox');
  1227. $bExpunge = true;
  1228. break;
  1229. case 'copy':
  1230. // sqimap_msgs_list_copy returns true or false.
  1231. // If error happens - fourth argument handles it inside function.
  1232. sqimap_msgs_list_copy($imapConnection,$aUid,$targetMailbox,true);
  1233. sqsession_register($targetMailbox,'lastTargetMailbox');
  1234. break;
  1235. case 'forward':
  1236. $aMsgHeaders = array();
  1237. foreach ($aUid as $iUid) {
  1238. $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
  1239. }
  1240. if (count($aMsgHeaders)) {
  1241. $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
  1242. // dirty hack, add info to $aMailbox
  1243. $aMailbox['FORWARD_SESSION']['SESSION_NUMBER'] = $composesession;
  1244. $aMailbox['FORWARD_SESSION']['UIDS'] = $aUid;
  1245. }
  1246. break;
  1247. default:
  1248. // Hook for plugin buttons
  1249. $temp = array(&$sButton, &$aMailbox, $iAccount, $aMailbox['NAME'], &$aUid);
  1250. do_hook('mailbox_display_button_action', $temp);
  1251. break;
  1252. }
  1253. /**
  1254. * $aUpdatedMsgs is an array containing the result of the untagged
  1255. * fetch responses send by the imap server due to a flag change. That
  1256. * response is parsed in an array with msg arrays by the parseFetch function
  1257. */
  1258. if ($aUpdatedMsgs) {
  1259. // Update the message headers cache
  1260. $aDeleted = array();
  1261. foreach ($aUpdatedMsgs as $iUid => $aMsg) {
  1262. if (isset($aMsg['FLAGS'])) {
  1263. /**
  1264. * Only update the cached headers if the header is
  1265. * cached.
  1266. */
  1267. if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
  1268. $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
  1269. }
  1270. /**
  1271. * Also update flags in message object
  1272. */
  1273. //FIXME: WHY are we keeping flags in TWO places?!? This is error-prone and some core code uses the is_xxxx message object values while other code uses the flags array above. That's a mess.
  1274. if (isset($aMailbox['MSG_HEADERS'][$iUid]['MESSAGE_OBJECT'])) {
  1275. $message = $aMailbox['MSG_HEADERS'][$iUid]['MESSAGE_OBJECT'];
  1276. $message->is_seen = false;
  1277. $message->is_answered = false;
  1278. $message->is_forwarded = false;
  1279. $message->is_deleted = false;
  1280. $message->is_flagged = false;
  1281. $message->is_mdnsent = false;
  1282. foreach ($aMsg['FLAGS'] as $flag => $value) {
  1283. if (strtolower($flag) == '\\seen' && $value)
  1284. $message->is_seen = true;
  1285. else if (strtolower($flag) == '\\answered' && $value)
  1286. $message->is_answered = true;
  1287. else if (strtolower($flag) == '$forwarded' && $value)
  1288. $message->is_forwarded = true;
  1289. else if (strtolower($flag) == '\\deleted' && $value)
  1290. $message->is_deleted = true;
  1291. else if (strtolower($flag) == '\\flagged' && $value)
  1292. $message->is_flagged = true;
  1293. else if (strtolower($flag) == '$mdnsent' && $value)
  1294. $message->is_mdnsent = true;
  1295. }
  1296. $aMailbox['MSG_HEADERS'][$iUid]['MESSAGE_OBJECT'] = $message;
  1297. }
  1298. /**
  1299. * Count the messages with the \Delete flag set so we can determine
  1300. * if the number of expunged messages equals the number of flagged
  1301. * messages for deletion.
  1302. */
  1303. if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
  1304. $aDeleted[] = $iUid;
  1305. }
  1306. }
  1307. }
  1308. if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
  1309. $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
  1310. {
  1311. if (count($aDeleted) != $iExpungedMessages) {
  1312. // there are more messages deleted permanently then we expected
  1313. // invalidate the cache
  1314. $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
  1315. $aMailbox['MSG_HEADERS'] = false;
  1316. } else {
  1317. // remove expunged messages from cache
  1318. $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
  1319. if (is_array($aUidSet)) {
  1320. // create a UID => array index temp array
  1321. $aUidSetDummy = array_flip($aUidSet);
  1322. foreach ($aDeleted as $iUid) {
  1323. // get the id as well in case of SQM_SORT_NONE
  1324. if ($aMailbox['SORT'] == SQSORT_NONE) {
  1325. $aMailbox['ID'] = false;
  1326. //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
  1327. //unset($aMailbox['ID'][$iId]);
  1328. }
  1329. // unset the UID and message header
  1330. unset($aUidSetDummy[$iUid]);
  1331. unset($aMailbox['MSG_HEADERS'][$iUid]);
  1332. }
  1333. $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
  1334. }
  1335. }
  1336. // update EXISTS info
  1337. if ($iExpungedMessages) {
  1338. $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
  1339. $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
  1340. }
  1341. if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
  1342. $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
  1343. $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
  1344. $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
  1345. }
  1346. }
  1347. }
  1348. } else {
  1349. if ($sButton == 'expunge') {
  1350. /**
  1351. * on expunge we do not know which messages will be deleted
  1352. * so it's useless to try to sync the cache
  1353. *
  1354. * Close the mailbox so we do not need to parse the untagged expunge
  1355. * responses which do not contain uid info.
  1356. * NB: Closing a mailbox is faster then expunge because the imap
  1357. * server does not need to generate the untagged expunge responses
  1358. */
  1359. sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
  1360. $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
  1361. } else {
  1362. // this is the same hook as above, but here it is called in the
  1363. // context of not having had any messages selected and if any
  1364. // plugin handles the situation, it should return TRUE so we
  1365. // know this was not an erroneous user action
  1366. //
  1367. global $null;
  1368. $temp = array(&$sButton, &$aMailbox, $iAccount, $aMailbox['NAME'], $null);
  1369. if (!boolean_hook_function('mailbox_display_button_action', $temp, 1)
  1370. && $sButton) {
  1371. $sError = _("No messages were selected.");
  1372. }
  1373. }
  1374. }
  1375. return $sError;
  1376. }
  1377. /**
  1378. * Attach messages to a compose session
  1379. *
  1380. * @param resource $imapConnection imap connection
  1381. * @param array $aMsgHeaders
  1382. * @return int $composesession unique compose_session_id where the attached messages belong to
  1383. * @author Marc Groot Koerkamp
  1384. */
  1385. function attachSelectedMessages($imapConnection,$aMsgHeaders) {
  1386. sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
  1387. sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
  1388. if (!isset($compose_messages)|| is_null($compose_messages)) {
  1389. $compose_messages = array();
  1390. sqsession_register($compose_messages,'compose_messages');
  1391. }
  1392. if (!$composesession) {
  1393. $composesession = 1;
  1394. sqsession_register($composesession,'composesession');
  1395. } else {
  1396. $composesession++;
  1397. sqsession_register($composesession,'composesession');
  1398. }
  1399. $composeMessage = new Message();
  1400. $rfc822_header = new Rfc822Header();
  1401. $composeMessage->rfc822_header = $rfc822_header;
  1402. $composeMessage->reply_rfc822_header = '';
  1403. foreach($aMsgHeaders as $iUid => $aMsgHeader) {
  1404. /**
  1405. * Retrieve the full message
  1406. */
  1407. $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
  1408. if ($response == 'OK') {
  1409. $subject = (isset($aMsgHeader['subject'])) ? $aMsgHeader['subject'] : $iUid;
  1410. array_shift($body_a);
  1411. array_pop($body_a);
  1412. $body = implode('', $body_a);
  1413. $body .= "\r\n";
  1414. global $username, $attachment_dir;
  1415. $filename = sq_get_attach_tempfile();
  1416. $fullpath = getHashedDir($username, $attachment_dir) . '/' . $filename;
  1417. $fp = fopen($fullpath, 'wb');
  1418. fwrite ($fp, $body);
  1419. fclose($fp);
  1420. $composeMessage->initAttachment('message/rfc822', $subject . '.eml', $filename);
  1421. // create subject for new message
  1422. //
  1423. $subject = decodeHeader($subject,false,false,true);
  1424. $subject = str_replace('"', "'", $subject);
  1425. $subject = trim($subject);
  1426. if (substr(strtolower($subject), 0, 4) != 'fwd:') {
  1427. $subject = 'Fwd: ' . $subject;
  1428. }
  1429. $composeMessage->rfc822_header->subject = $subject;
  1430. }
  1431. }
  1432. $compose_messages[$composesession] = $composeMessage;
  1433. sqsession_register($compose_messages,'compose_messages');
  1434. return $composesession;
  1435. }