mailbox_display.php 69 KB

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