mailbox_display.php 70 KB

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