mailbox_display.php 70 KB

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