mailbox_display.php 62 KB

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