mailbox_display.php 62 KB

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