mailbox_display.php 56 KB

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