imap_mailbox.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. <?php
  2. /**
  3. * imap_mailbox.php
  4. *
  5. * Copyright (c) 1999-2005 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This implements all functions that manipulate mailboxes
  9. *
  10. * @version $Id$
  11. * @package squirrelmail
  12. * @subpackage imap
  13. */
  14. /** @ignore */
  15. if (! defined('SM_PATH')) define('SM_PATH','../');
  16. /** UTF7 support */
  17. require_once(SM_PATH . 'functions/imap_utf7_local.php');
  18. global $boxesnew;
  19. /**
  20. * Mailboxes class
  21. *
  22. * FIXME. This class should be extracted and placed in a separate file that
  23. * can be included before we start the session. That makes caching of the tree
  24. * possible. On a refresh mailboxes from left_main.php the only function that
  25. * should be called is the sqimap_get_status_mbx_tree. In case of subscribe
  26. * / rename / delete / new we have to create methods for adding/changing the
  27. * mailbox in the mbx_tree without the need for a refresh.
  28. *
  29. * Some code fragments are present in 1.3.0 - 1.4.4.
  30. * @package squirrelmail
  31. * @subpackage imap
  32. * @since 1.5.0
  33. */
  34. class mailboxes {
  35. var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
  36. $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
  37. $is_trash = false, $is_draft = false, $mbxs = array(),
  38. $unseen = false, $total = false;
  39. function addMbx($mbx, $delimiter, $start, $specialfirst) {
  40. $ary = explode($delimiter, $mbx->mailboxname_full);
  41. $mbx_parent =& $this;
  42. for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
  43. $mbx_childs =& $mbx_parent->mbxs;
  44. $found = false;
  45. if ($mbx_childs) {
  46. foreach ($mbx_childs as $key => $parent) {
  47. if ($parent->mailboxname_sub == $ary[$i]) {
  48. $mbx_parent =& $mbx_parent->mbxs[$key];
  49. $found = true;
  50. break;
  51. }
  52. }
  53. }
  54. if (!$found) {
  55. $no_select_mbx = new mailboxes();
  56. if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
  57. $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
  58. } else {
  59. $no_select_mbx->mailboxname_full = $ary[$i];
  60. }
  61. $no_select_mbx->mailboxname_sub = $ary[$i];
  62. $no_select_mbx->is_noselect = true;
  63. $mbx_parent->mbxs[] = $no_select_mbx;
  64. $i--;
  65. }
  66. }
  67. $mbx_parent->mbxs[] = $mbx;
  68. if ($mbx->is_special && $specialfirst) {
  69. usort($mbx_parent->mbxs, 'sortSpecialMbx');
  70. }
  71. }
  72. }
  73. /**
  74. * array callback used for sorting in mailboxes class
  75. * @param object $a
  76. * @param object $b
  77. * @return integer see php strnatcasecmp()
  78. * @since 1.3.0
  79. */
  80. function sortSpecialMbx($a, $b) {
  81. if ($a->is_inbox) {
  82. $acmp = '0'. $a->mailboxname_full;
  83. } else if ($a->is_special) {
  84. $acmp = '1'. $a->mailboxname_full;
  85. } else {
  86. $acmp = '2' . $a->mailboxname_full;
  87. }
  88. if ($b->is_inbox) {
  89. $bcmp = '0'. $b->mailboxname_full;
  90. }else if ($b->is_special) {
  91. $bcmp = '1' . $b->mailboxname_full;
  92. } else {
  93. $bcmp = '2' . $b->mailboxname_full;
  94. }
  95. return strnatcasecmp($acmp, $bcmp);
  96. }
  97. /**
  98. * @param array $ary
  99. * @return array
  100. * @since 1.5.0
  101. */
  102. function compact_mailboxes_response($ary) {
  103. /*
  104. * Workaround for mailboxes returned as literal
  105. * FIXME : Doesn't work if the mailbox name is multiple lines
  106. * (larger then fgets buffer)
  107. */
  108. for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
  109. if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
  110. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  111. $ary[$i], $regs)) {
  112. $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
  113. array_splice($ary, $i+1, 2);
  114. }
  115. }
  116. }
  117. /* remove duplicates and ensure array is contiguous */
  118. return array_values(array_unique($ary));
  119. }
  120. /**
  121. * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
  122. * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
  123. * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
  124. *
  125. * Originally stored in functions/strings.php. Since 1.2.6 stored in
  126. * functions/imap_mailbox.php
  127. * @param string $line imap LIST/LSUB response line
  128. * @return string mailbox name
  129. */
  130. function find_mailbox_name($line) {
  131. if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
  132. if (substr($regs[1], 0, 1) == '"')
  133. return stripslashes(substr($regs[1], 1, -1));
  134. return $regs[1];
  135. }
  136. return '';
  137. }
  138. /**
  139. * Detects if mailbox has noselect flag (can't store messages)
  140. * In versions older than 1.4.5 function checks only LSUB responses
  141. * and can produce pcre warnings.
  142. * @param string $lsub_line mailbox line from untagged LIST or LSUB response
  143. * @return bool whether this is a Noselect mailbox.
  144. * @since 1.3.2
  145. */
  146. function check_is_noselect ($lsub_line) {
  147. return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
  148. }
  149. /**
  150. * Detects if mailbox has noinferiors flag (can't store subfolders)
  151. * @param string $lsub_line mailbox line from untagged LIST or LSUB response
  152. * @return bool whether this is a Noinferiors mailbox.
  153. * @since 1.5.0
  154. */
  155. function check_is_noinferiors ($lsub_line) {
  156. return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
  157. }
  158. /**
  159. * Detects mailbox's parent folder
  160. *
  161. * If $haystack is a full mailbox name, and $needle is the mailbox
  162. * separator character, returns the second last part of the full
  163. * mailbox name (i.e. the mailbox's parent mailbox)
  164. *
  165. * Originally stored in functions/strings.php. Since 1.2.6 stored in
  166. * functions/imap_mailbox.php
  167. * @param string $haystack full mailbox name
  168. * @param string $needle delimiter
  169. * @return string parent mailbox
  170. */
  171. function readMailboxParent($haystack, $needle) {
  172. if ($needle == '') {
  173. $ret = '';
  174. } else {
  175. $parts = explode($needle, $haystack);
  176. $elem = array_pop($parts);
  177. while ($elem == '' && count($parts)) {
  178. $elem = array_pop($parts);
  179. }
  180. $ret = join($needle, $parts);
  181. }
  182. return( $ret );
  183. }
  184. /**
  185. * Check if $subbox is below the specified $parentbox
  186. * @param string $subbox potential sub folder
  187. * @param string $parentbox potential parent
  188. * @return boolean
  189. * @since 1.2.3
  190. */
  191. function isBoxBelow( $subbox, $parentbox ) {
  192. global $delimiter;
  193. /*
  194. * Eliminate the obvious mismatch, where the
  195. * subfolder path is shorter than that of the potential parent
  196. */
  197. if ( strlen($subbox) < strlen($parentbox) ) {
  198. return false;
  199. }
  200. /* check for delimiter */
  201. if (substr($parentbox,-1) != $delimiter) {
  202. $parentbox .= $delimiter;
  203. }
  204. return (substr($subbox,0,strlen($parentbox)) == $parentbox);
  205. }
  206. /**
  207. * Defines special mailboxes: given a mailbox name, it checks if this is a
  208. * "special" one: INBOX, Trash, Sent or Draft.
  209. *
  210. * Since 1.2.5 function includes special_mailbox hook.<br>
  211. * Since 1.4.3 hook supports more than one plugin.
  212. * @param string $box mailbox name
  213. * @return boolean
  214. * @since 1.2.3
  215. */
  216. function isSpecialMailbox( $box ) {
  217. $ret = ( (strtolower($box) == 'inbox') ||
  218. isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
  219. if ( !$ret ) {
  220. $ret = boolean_hook_function('special_mailbox',$box,1);
  221. }
  222. return $ret;
  223. }
  224. /**
  225. * Detects if mailbox is a Trash folder or subfolder of Trash
  226. * @param string $box mailbox name
  227. * @return bool whether this is a Trash folder
  228. * @since 1.4.0
  229. */
  230. function isTrashMailbox ($box) {
  231. global $trash_folder, $move_to_trash;
  232. return $move_to_trash && $trash_folder &&
  233. ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
  234. }
  235. /**
  236. * Detects if mailbox is a Sent folder or subfolder of Sent
  237. * @param string $box mailbox name
  238. * @return bool whether this is a Sent folder
  239. * @since 1.4.0
  240. */
  241. function isSentMailbox($box) {
  242. global $sent_folder, $move_to_sent;
  243. return $move_to_sent && $sent_folder &&
  244. ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
  245. }
  246. /**
  247. * Detects if mailbox is a Drafts folder or subfolder of Drafts
  248. * @param string $box mailbox name
  249. * @return bool whether this is a Draft folder
  250. * @since 1.4.0
  251. */
  252. function isDraftMailbox($box) {
  253. global $draft_folder, $save_as_draft;
  254. return $save_as_draft &&
  255. ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
  256. }
  257. /**
  258. * Expunges a mailbox
  259. *
  260. * WARNING: Select mailbox before calling this function.
  261. *
  262. * permanently removes all messages that have the \Deleted flag
  263. * set from the selected mailbox. See EXPUNGE command chapter in
  264. * IMAP RFC.
  265. * @param stream $imap_stream imap connection resource
  266. * @param string $mailbox mailbox name (unused since 1.1.3).
  267. * @param boolean $handle_errors error handling control (displays error_box on error).
  268. * @param mixed $id (since 1.3.0) integer message id or array with integer ids
  269. * @return integer number of expunged messages
  270. * @since 1.0 or older
  271. */
  272. function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
  273. if ($id) {
  274. if (is_array($id)) {
  275. $id = sqimap_message_list_squisher($id);
  276. }
  277. $id = ' '.$id;
  278. $uid = TRUE;
  279. } else {
  280. $uid = false;
  281. }
  282. $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
  283. $response, $message, $uid);
  284. $cnt = 0;
  285. if (is_array($read)) {
  286. foreach ($read as $r) {
  287. if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
  288. $cnt++;
  289. }
  290. }
  291. }
  292. return $cnt;
  293. }
  294. /**
  295. * Checks whether or not the specified mailbox exists
  296. * @param stream $imap_stream imap connection resource
  297. * @param string $mailbox mailbox name
  298. * @return boolean
  299. * @since 1.0 or older
  300. */
  301. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  302. if (!isset($mailbox) || empty($mailbox)) {
  303. return false;
  304. }
  305. $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
  306. true, $response, $message);
  307. return isset($mbx[0]);
  308. }
  309. /**
  310. * Selects a mailbox
  311. * Before 1.3.0 used more arguments and returned data depended on those argumements.
  312. * @param stream $imap_stream imap connection resource
  313. * @param string $mailbox mailbox name
  314. * @return array results of select command (on success - permanentflags, flags and rights)
  315. * @since 1.0 or older
  316. */
  317. function sqimap_mailbox_select ($imap_stream, $mailbox) {
  318. if ($mailbox == 'None') {
  319. return;
  320. }
  321. $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
  322. true, $response, $message);
  323. $result = array();
  324. for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
  325. if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
  326. $result[strtoupper($regs[1])] = $regs[2];
  327. } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
  328. $result[strtoupper($regs[2])] = $regs[1];
  329. } else {
  330. if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
  331. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
  332. $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
  333. } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
  334. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
  335. $result['FLAGS'] = explode(' ',strtolower($regs[1]));
  336. }
  337. }
  338. }
  339. if (!isset($result['PERMANENTFLAGS'])) {
  340. $result['PERMANENTFLAGS'] = $result['FLAGS'];
  341. }
  342. if (preg_match('/^\[(.+)\]/',$message, $regs)) {
  343. $result['RIGHTS']=strtoupper($regs[1]);
  344. }
  345. return $result;
  346. }
  347. /**
  348. * Creates a folder.
  349. *
  350. * Mailbox is automatically subscribed.
  351. *
  352. * Set $type to string that does not match 'noselect' (case insensitive),
  353. * if you don't want to prepend delimiter to mailbox name. Please note
  354. * that 'noinferiors' might be used someday as keyword for folders
  355. * that store only messages.
  356. * @param stream $imap_steam imap connection resource
  357. * @param string $mailbox mailbox name
  358. * @param string $type folder type.
  359. * @since 1.0 or older
  360. */
  361. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  362. global $delimiter;
  363. if (strtolower($type) == 'noselect') {
  364. $mailbox .= $delimiter;
  365. }
  366. $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
  367. sqimap_encode_mailbox_name($mailbox),
  368. true, $response, $message);
  369. sqimap_subscribe ($imap_stream, $mailbox);
  370. }
  371. /**
  372. * Subscribes to an existing folder.
  373. * @param stream $imap_stream imap connection resource
  374. * @param string $mailbox mailbox name
  375. * @param boolean $debug (since 1.5.1)
  376. * @since 1.0 or older
  377. */
  378. function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
  379. $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
  380. sqimap_encode_mailbox_name($mailbox),
  381. $debug, $response, $message);
  382. }
  383. /**
  384. * Unsubscribes from an existing folder
  385. * @param stream $imap_stream imap connection resource
  386. * @param string $mailbox mailbox name
  387. * @since 1.0 or older
  388. */
  389. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  390. $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
  391. sqimap_encode_mailbox_name($mailbox),
  392. false, $response, $message);
  393. }
  394. /**
  395. * Deletes the given folder
  396. * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
  397. * @param stream $imap_stream imap connection resource
  398. * @param string $mailbox mailbox name
  399. * @since 1.0 or older
  400. */
  401. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  402. global $data_dir, $username;
  403. sqimap_unsubscribe ($imap_stream, $mailbox);
  404. $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
  405. sqimap_encode_mailbox_name($mailbox),
  406. true, $response, $message);
  407. if ($response !== 'OK') {
  408. // subscribe again
  409. sqimap_subscribe ($imap_stream, $mailbox);
  410. } else {
  411. do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
  412. removePref($data_dir, $username, "thread_$mailbox");
  413. removePref($data_dir, $username, "collapse_folder_$mailbox");
  414. }
  415. }
  416. /**
  417. * Determines if the user is subscribed to the folder or not
  418. * @param stream $imap_stream imap connection resource
  419. * @param string $mailbox mailbox name
  420. * @return boolean
  421. * @since 1.2.0
  422. */
  423. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  424. $boxesall = sqimap_mailbox_list ($imap_stream);
  425. foreach ($boxesall as $ref) {
  426. if ($ref['unformatted'] == $folder) {
  427. return true;
  428. }
  429. }
  430. return false;
  431. }
  432. /**
  433. * Renames a mailbox.
  434. * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
  435. * @param stream $imap_stream imap connection resource
  436. * @param string $old_name mailbox name
  437. * @param string $new_name new mailbox name
  438. * @since 1.2.3
  439. */
  440. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  441. if ( $old_name != $new_name ) {
  442. global $delimiter, $imap_server_type, $data_dir, $username;
  443. if ( substr( $old_name, -1 ) == $delimiter ) {
  444. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  445. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  446. $postfix = $delimiter;
  447. } else {
  448. $postfix = '';
  449. }
  450. $boxesall = sqimap_mailbox_list_all($imap_stream);
  451. $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
  452. ' ' . sqimap_encode_mailbox_name($new_name);
  453. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  454. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  455. $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
  456. $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
  457. removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
  458. removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
  459. sqimap_subscribe($imap_stream, $new_name.$postfix);
  460. setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
  461. setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
  462. do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
  463. $l = strlen( $old_name ) + 1;
  464. $p = 'unformatted';
  465. foreach ($boxesall as $box) {
  466. if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
  467. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  468. /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
  469. if ($imap_server_type == 'cyrus') {
  470. $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
  471. $data = sqimap_run_command($imap_stream, $cmd, false,
  472. $response, $message);
  473. }
  474. $was_subscribed = sqimap_mailbox_is_subscribed($imap_stream, $box[$p]);
  475. if ( $was_subscribed ) {
  476. sqimap_unsubscribe($imap_stream, $box[$p]);
  477. }
  478. $oldpref_thread = getPref($data_dir, $username, 'thread_'.$box[$p]);
  479. $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$box[$p]);
  480. removePref($data_dir, $username, 'thread_'.$box[$p]);
  481. removePref($data_dir, $username, 'collapse_folder_'.$box[$p]);
  482. if ( $was_subscribed ) {
  483. sqimap_subscribe($imap_stream, $new_sub);
  484. }
  485. setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref_thread);
  486. setPref($data_dir, $username, 'collapse_folder_'.$new_sub, $oldpref_collapse);
  487. do_hook_function('rename_or_delete_folder',
  488. $args = array($box[$p], 'rename', $new_sub));
  489. }
  490. }
  491. }
  492. }
  493. /**
  494. * Formats a mailbox into parts for the $boxesall array
  495. *
  496. * The parts are:
  497. * <ul>
  498. * <li>raw - Raw LIST/LSUB response from the IMAP server
  499. * <li>formatted - nicely formatted folder name
  500. * <li>unformatted - unformatted, but with delimiter at end removed
  501. * <li>unformatted-dm - folder name as it appears in raw response
  502. * <li>unformatted-disp - unformatted without $folder_prefix
  503. * <li>id - TODO: document me
  504. * <li>flags - TODO: document me
  505. * </ul>
  506. * Before 1.2.0 used third argument for delimiter.
  507. * @param $line
  508. * @param $line_lsub
  509. * @return array
  510. * @since 1.0 or older
  511. * @todo document id and flags keys in boxes array and function arguments.
  512. */
  513. function sqimap_mailbox_parse ($line, $line_lsub) {
  514. global $folder_prefix, $delimiter;
  515. /* Process each folder line */
  516. for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
  517. /* Store the raw IMAP reply */
  518. if (isset($line[$g])) {
  519. $boxesall[$g]['raw'] = $line[$g];
  520. } else {
  521. $boxesall[$g]['raw'] = '';
  522. }
  523. /* Count number of delimiters ($delimiter) in folder name */
  524. $mailbox = /*trim(*/$line_lsub[$g]/*)*/;
  525. $dm_count = substr_count($mailbox, $delimiter);
  526. if (substr($mailbox, -1) == $delimiter) {
  527. /* If name ends in delimiter, decrement count by one */
  528. $dm_count--;
  529. }
  530. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  531. $boxesallbyname[$mailbox] = $g;
  532. $parentfolder = readMailboxParent($mailbox, $delimiter);
  533. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  534. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  535. (isset($boxesallbyname[$parentfolder]) &&
  536. (strlen($parentfolder) > 0) ) ) {
  537. $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
  538. if ($indent > 0) {
  539. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  540. } else {
  541. $boxesall[$g]['formatted'] = '';
  542. }
  543. $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  544. } else {
  545. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  546. }
  547. $boxesall[$g]['unformatted-dm'] = $mailbox;
  548. if (substr($mailbox, -1) == $delimiter) {
  549. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  550. }
  551. $boxesall[$g]['unformatted'] = $mailbox;
  552. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  553. $mailbox = substr($mailbox, strlen($folder_prefix));
  554. }
  555. $boxesall[$g]['unformatted-disp'] = $mailbox;
  556. $boxesall[$g]['id'] = $g;
  557. $boxesall[$g]['flags'] = array();
  558. if (isset($line[$g])) {
  559. ereg("\(([^)]*)\)",$line[$g],$regs);
  560. // FIXME Flags do contain the \ character. \NoSelect \NoInferiors
  561. // and $MDNSent <= last one doesn't have the \
  562. // It's better to follow RFC3501 instead of using our own naming.
  563. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  564. if ($flags) {
  565. $boxesall[$g]['flags'] = explode(' ', $flags);
  566. }
  567. }
  568. }
  569. return $boxesall;
  570. }
  571. /**
  572. * Returns list of options (to be echoed into select statement
  573. * based on available mailboxes and separators
  574. * Caller should surround options with <select ...> </select> and
  575. * any formatting.
  576. * @param stream $imap_stream imap connection resource to query for mailboxes
  577. * @param array $show_selected array containing list of mailboxes to pre-select (0 if none)
  578. * @param array $folder_skip array of folders to keep out of option list (compared in lower)
  579. * @param $boxes list of already fetched boxes (for places like folder panel, where
  580. * you know these options will be shown 3 times in a row.. (most often unset).
  581. * @param string $flag (since 1.4.1) flag to check for in mailbox flags, used to filter out mailboxes.
  582. * 'noselect' by default to remove unselectable mailboxes.
  583. * 'noinferiors' used to filter out folders that can not contain subfolders.
  584. * NULL to avoid flag check entirely.
  585. * NOTE: noselect and noiferiors are used internally. The IMAP representation is
  586. * \NoSelect and \NoInferiors
  587. * @param boolean $use_long_format (since 1.4.1) override folder display preference and always show full folder name.
  588. * @return string html formated mailbox selection options
  589. * @since 1.3.2
  590. */
  591. function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
  592. $flag = 'noselect', $use_long_format = false ) {
  593. global $username, $data_dir;
  594. $mbox_options = '';
  595. if ( $use_long_format ) {
  596. $shorten_box_names = 0;
  597. } else {
  598. $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
  599. }
  600. if ($boxes == 0) {
  601. $boxes = sqimap_mailbox_list($imap_stream);
  602. }
  603. foreach ($boxes as $boxes_part) {
  604. if ($flag == NULL || (is_array($boxes_part['flags'])
  605. && !in_array($flag, $boxes_part['flags']))) {
  606. $box = $boxes_part['unformatted'];
  607. if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
  608. continue;
  609. }
  610. $lowerbox = strtolower($box);
  611. // mailboxes are casesensitive => inbox.sent != inbox.Sent
  612. // nevermind, to many dependencies this should be fixed!
  613. if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
  614. $box2 = _("INBOX");
  615. } else {
  616. switch ($shorten_box_names)
  617. {
  618. case 2: /* delimited, style = 2 */
  619. $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '.&nbsp;', htmlspecialchars($boxes_part['formatted']));
  620. break;
  621. case 1: /* indent, style = 1 */
  622. $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '&nbsp;&nbsp;', htmlspecialchars($boxes_part['formatted']));
  623. break;
  624. default: /* default, long names, style = 0 */
  625. $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
  626. break;
  627. }
  628. }
  629. if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
  630. $mbox_options .= '<option value="' . htmlspecialchars($box) .'" selected="selected">'.$box2.'</option>' . "\n";
  631. } else {
  632. $mbox_options .= '<option value="' . htmlspecialchars($box) .'">'.$box2.'</option>' . "\n";
  633. }
  634. }
  635. }
  636. return $mbox_options;
  637. }
  638. /**
  639. * Returns sorted mailbox lists in several different ways.
  640. * See comment on sqimap_mailbox_parse() for info about the returned array.
  641. * @param resource $imap_stream imap connection resource
  642. * @param boolean $force force update of mailbox listing. available since 1.4.2 and 1.5.0
  643. * @return array list of mailboxes
  644. * @since 1.0 or older
  645. */
  646. function sqimap_mailbox_list($imap_stream, $force=false) {
  647. if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
  648. global $data_dir, $username, $list_special_folders_first,
  649. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  650. $move_to_trash, $move_to_sent, $save_as_draft,
  651. $delimiter, $noselect_fix_enable, $imap_server_type,
  652. $show_only_subscribed_folders;
  653. $inbox_subscribed = false;
  654. $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
  655. require_once(SM_PATH . 'include/load_prefs.php');
  656. if (!$show_only_subscribed_folders) {
  657. $lsub = 'LIST';
  658. } elseif ($listsubscribed) {
  659. $lsub = 'LIST (SUBSCRIBED)';
  660. } else {
  661. $lsub = 'LSUB';
  662. }
  663. if ($noselect_fix_enable) {
  664. $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
  665. } else {
  666. $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
  667. }
  668. /* LSUB array */
  669. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
  670. true, $response, $message);
  671. $lsub_ary = compact_mailboxes_response($lsub_ary);
  672. $sorted_lsub_ary = array();
  673. for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
  674. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  675. $sorted_lsub_ary[] = $temp_mailbox_name;
  676. if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
  677. $inbox_subscribed = true;
  678. }
  679. }
  680. /* natural sort mailboxes */
  681. if (isset($sorted_lsub_ary)) {
  682. usort($sorted_lsub_ary, 'strnatcasecmp');
  683. }
  684. /*
  685. * The LSUB response doesn't provide us information about \Noselect
  686. * mail boxes. The LIST response does, that's why we need to do a LIST
  687. * call to retrieve the flags for the mailbox
  688. * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
  689. * in other words, we cannot rely on it.
  690. */
  691. $sorted_list_ary = array();
  692. // if (!$listsubscribed) {
  693. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  694. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  695. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  696. }
  697. else {
  698. $mbx = $sorted_lsub_ary[$i];
  699. }
  700. $read = sqimap_run_command ($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mbx),
  701. true, $response, $message);
  702. $read = compact_mailboxes_response($read);
  703. if (isset($read[0])) {
  704. $sorted_list_ary[$i] = $read[0];
  705. } else {
  706. $sorted_list_ary[$i] = '';
  707. }
  708. }
  709. // }
  710. /*
  711. * Just in case they're not subscribed to their inbox,
  712. * we'll get it for them anyway
  713. */
  714. if (!$inbox_subscribed) {
  715. $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
  716. true, $response, $message);
  717. $sorted_list_ary[] = implode('',compact_mailboxes_response($inbox_ary));
  718. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  719. }
  720. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  721. /* Now, lets sort for special folders */
  722. $boxesnew = $used = array();
  723. /* Find INBOX */
  724. $cnt = count($boxesall);
  725. $used = array_pad($used,$cnt,false);
  726. for($k = 0; $k < $cnt; ++$k) {
  727. if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
  728. $boxesnew[] = $boxesall[$k];
  729. $used[$k] = true;
  730. break;
  731. }
  732. }
  733. /* List special folders and their subfolders, if requested. */
  734. if ($list_special_folders_first) {
  735. for($k = 0; $k < $cnt; ++$k) {
  736. if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
  737. $boxesnew[] = $boxesall[$k];
  738. $used[$k] = true;
  739. }
  740. }
  741. }
  742. /* Find INBOX's children */
  743. for($k = 0; $k < $cnt; ++$k) {
  744. if (!$used[$k] && isBoxBelow(strtolower($boxesall[$k]['unformatted']), 'inbox') &&
  745. strtolower($boxesall[$k]['unformatted']) != 'inbox') {
  746. $boxesnew[] = $boxesall[$k];
  747. $used[$k] = true;
  748. }
  749. }
  750. /* Rest of the folders */
  751. for($k = 0; $k < $cnt; $k++) {
  752. if (!$used[$k]) {
  753. $boxesnew[] = $boxesall[$k];
  754. }
  755. }
  756. sqsession_register($boxesnew,'boxesnew');
  757. }
  758. return $boxesnew;
  759. }
  760. /**
  761. * Returns a list of all folders, subscribed or not
  762. * @param stream $imap_stream imap connection resource
  763. * @return array see sqimap_mailbox_parse()
  764. * @since 1.0 or older
  765. */
  766. function sqimap_mailbox_list_all($imap_stream) {
  767. global $list_special_folders_first, $folder_prefix, $delimiter;
  768. $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
  769. $read_ary = compact_mailboxes_response($read_ary);
  770. $g = 0;
  771. $fld_pre_length = strlen($folder_prefix);
  772. for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
  773. /* Store the raw IMAP reply */
  774. $boxes[$g]['raw'] = $read_ary[$i];
  775. /* Count number of delimiters ($delimiter) in folder name */
  776. $mailbox = find_mailbox_name($read_ary[$i]);
  777. $dm_count = substr_count($mailbox, $delimiter);
  778. if (substr($mailbox, -1) == $delimiter) {
  779. /* If name ends in delimiter - decrement count by one */
  780. $dm_count--;
  781. }
  782. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  783. $boxesallbyname[$mailbox] = $g;
  784. $parentfolder = readMailboxParent($mailbox, $delimiter);
  785. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  786. (ereg('^'.$folder_prefix, $mailbox)) ||
  787. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  788. if ($dm_count) {
  789. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  790. } else {
  791. $boxes[$g]['formatted'] = '';
  792. }
  793. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  794. } else {
  795. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  796. }
  797. $boxes[$g]['unformatted-dm'] = $mailbox;
  798. if (substr($mailbox, -1) == $delimiter) {
  799. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  800. }
  801. $boxes[$g]['unformatted'] = $mailbox;
  802. $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
  803. $boxes[$g]['id'] = $g;
  804. /* Now lets get the flags for this mailbox */
  805. $read_mlbx = $read_ary[$i];
  806. $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
  807. $flags = substr($flags, 0, strpos($flags, ')'));
  808. $flags = str_replace('\\', '', $flags);
  809. $flags = trim(strtolower($flags));
  810. if ($flags) {
  811. $boxes[$g]['flags'] = explode(' ', $flags);
  812. } else {
  813. $boxes[$g]['flags'] = array();
  814. }
  815. $g++;
  816. }
  817. if(is_array($boxes)) {
  818. sort ($boxes);
  819. }
  820. return $boxes;
  821. }
  822. /**
  823. * Fills mailbox object
  824. *
  825. * Some code fragments are present in 1.3.0 - 1.4.4.
  826. * @param stream $imap_stream imap connection resource
  827. * @return object see mailboxes class.
  828. * @since 1.5.0
  829. */
  830. function sqimap_mailbox_tree($imap_stream) {
  831. global $default_folder_prefix, $data_dir, $username, $list_special_folders_first,
  832. $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
  833. $imap_server_type, $show_only_subscribed_folders;
  834. $noselect = false;
  835. $noinferiors = false;
  836. require_once(SM_PATH . 'include/load_prefs.php');
  837. if ($show_only_subscribed_folders) {
  838. $lsub_cmd = 'LSUB';
  839. } else {
  840. $lsub_cmd = 'LIST';
  841. }
  842. /* LSUB array */
  843. $lsub_ary = sqimap_run_command ($imap_stream, "$lsub_cmd \"$folder_prefix\" \"*\"",
  844. true, $response, $message);
  845. $lsub_ary = compact_mailboxes_response($lsub_ary);
  846. /* Check to see if we have an INBOX */
  847. $has_inbox = false;
  848. for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
  849. if (preg_match("/^\*\s+$lsub_cmd.*\s\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
  850. $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
  851. // in case of an unsubscribed inbox an imap server can
  852. // return the inbox in the lsub results with a \NoSelect
  853. // flag.
  854. if (!preg_match("/\*\s+$lsub_cmd\s+\(.*\\\\NoSelect.*\).*/i",$lsub_ary[$i])) {
  855. $has_inbox = true;
  856. } else {
  857. // remove the result and request it again with a list
  858. // response at a later stage.
  859. unset($lsub_ary[$i]);
  860. // re-index the array otherwise the addition of the LIST
  861. // response will fail in PHP 4.1.2 and probably other older versions
  862. $lsub_ary = array_values($lsub_ary);
  863. }
  864. break;
  865. }
  866. }
  867. if ($has_inbox == false) {
  868. // do a list request for inbox because we should always show
  869. // inbox even if the user isn't subscribed to it.
  870. $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
  871. true, $response, $message);
  872. $inbox_ary = compact_mailboxes_response($inbox_ary);
  873. if (count($inbox_ary)) {
  874. $lsub_ary[] = $inbox_ary[0];
  875. }
  876. }
  877. /*
  878. * Section about removing the last element was removed
  879. * We don't return "* OK" anymore from sqimap_read_data
  880. */
  881. $sorted_lsub_ary = array();
  882. $cnt = count($lsub_ary);
  883. for ($i = 0; $i < $cnt; $i++) {
  884. $mbx = find_mailbox_name($lsub_ary[$i]);
  885. // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
  886. if ($imap_server_type != "uw") {
  887. $noselect = check_is_noselect($lsub_ary[$i]);
  888. $noinferiors = check_is_noinferiors($lsub_ary[$i]);
  889. }
  890. if (substr($mbx, -1) == $delimiter) {
  891. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  892. }
  893. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  894. }
  895. // FIX ME this requires a config setting inside conf.pl instead of checking on server type
  896. if ($imap_server_type == "uw") {
  897. $aQuery = array();
  898. $aTag = array();
  899. // prepare an array with queries
  900. foreach ($sorted_lsub_ary as $aMbx) {
  901. $mbx = stripslashes($aMbx['mbx']);
  902. sqimap_prepare_pipelined_query('LIST "" ' . sqimap_encode_mailbox_name($mbx), $tag, $aQuery, false);
  903. $aTag[$tag] = $mbx;
  904. }
  905. $sorted_lsub_ary = array();
  906. // execute all the queries at once
  907. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  908. foreach($aTag as $tag => $mbx) {
  909. if ($aServerResponse[$tag] == 'OK') {
  910. $sResponse = implode('', $aResponse[$tag]);
  911. $noselect = check_is_noselect($sResponse);
  912. $noinferiors = check_is_noinferiors($sResponse);
  913. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  914. }
  915. }
  916. $cnt = count($sorted_lsub_ary);
  917. }
  918. $sorted_lsub_ary = array_values($sorted_lsub_ary);
  919. usort($sorted_lsub_ary, 'mbxSort');
  920. $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
  921. return $boxestree;
  922. }
  923. /**
  924. * Callback function used for sorting mailboxes in sqimap_mailbox_tree
  925. * @param string $a
  926. * @param string $b
  927. * @return integer see php strnatcasecmp()
  928. * @since 1.5.1
  929. */
  930. function mbxSort($a, $b) {
  931. return strnatcasecmp($a['mbx'], $b['mbx']);
  932. }
  933. /**
  934. * Fills mailbox object
  935. *
  936. * Some code fragments are present in 1.3.0 - 1.4.4.
  937. * @param array $mbx_ary
  938. * @param $mbxs
  939. * @param stream $imap_stream imap connection resource
  940. * @return object see mailboxes class
  941. * @since 1.5.0
  942. */
  943. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
  944. global $data_dir, $username, $list_special_folders_first,
  945. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  946. $move_to_trash, $move_to_sent, $save_as_draft,
  947. $delimiter, $imap_server_type;
  948. // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  949. /* create virtual root node */
  950. $mailboxes= new mailboxes();
  951. $mailboxes->is_root = true;
  952. $trail_del = false;
  953. $start = 0;
  954. if (isset($folder_prefix) && ($folder_prefix != '')) {
  955. $start = substr_count($folder_prefix,$delimiter);
  956. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  957. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  958. } else {
  959. $mailboxes->mailboxname_full = $folder_prefix;
  960. $start++;
  961. }
  962. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  963. } else {
  964. $start = 0;
  965. }
  966. $cnt = count($mbx_ary);
  967. for ($i=0; $i < $cnt; $i++) {
  968. if ($mbx_ary[$i]['mbx'] !='' ) {
  969. $mbx = new mailboxes();
  970. $mailbox = $mbx_ary[$i]['mbx'];
  971. /*
  972. * Set the is_special flag if it concerned a special mailbox.
  973. * Used for displaying the special folders on top in the mailbox
  974. * tree displaying code.
  975. */
  976. $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
  977. $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
  978. $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
  979. $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
  980. if (!$mbx->is_special)
  981. $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
  982. if (isset($mbx_ary[$i]['unseen'])) {
  983. $mbx->unseen = $mbx_ary[$i]['unseen'];
  984. }
  985. if (isset($mbx_ary[$i]['nummessages'])) {
  986. $mbx->total = $mbx_ary[$i]['nummessages'];
  987. }
  988. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  989. $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
  990. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  991. if ($r_del_pos) {
  992. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  993. } else { /* mailbox is root folder */
  994. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  995. }
  996. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  997. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  998. }
  999. }
  1000. sqimap_utf7_decode_mbx_tree($mailboxes);
  1001. sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
  1002. return $mailboxes;
  1003. }
  1004. /**
  1005. * @param object $mbx_tree
  1006. * @since 1.5.0
  1007. */
  1008. function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
  1009. if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
  1010. $mbx_tree->mailboxname_sub = _("INBOX");
  1011. else
  1012. $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
  1013. if ($mbx_tree->mbxs) {
  1014. $iCnt = count($mbx_tree->mbxs);
  1015. for ($i=0;$i<$iCnt;++$i) {
  1016. $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
  1017. }
  1018. }
  1019. }
  1020. /**
  1021. * @param object $mbx_tree
  1022. * @param array $aMbxs
  1023. * @since 1.5.0
  1024. */
  1025. function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
  1026. if ($mbx_tree)
  1027. $aMbxs[] =& $mbx_tree;
  1028. if ($mbx_tree->mbxs) {
  1029. $iCnt = count($mbx_tree->mbxs);
  1030. for ($i=0;$i<$iCnt;++$i) {
  1031. sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
  1032. }
  1033. }
  1034. }
  1035. /**
  1036. * @param stream $imap_stream imap connection resource
  1037. * @param object $mbx_tree
  1038. * @since since 1.5.0
  1039. */
  1040. function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
  1041. global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
  1042. $aMbxs = $aQuery = array();
  1043. sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
  1044. // remove the root node
  1045. array_shift($aMbxs);
  1046. if($unseen_notify == 3) {
  1047. $cnt = count($aMbxs);
  1048. for($i=0;$i<$cnt;++$i) {
  1049. $oMbx =& $aMbxs[$i];
  1050. if (!$oMbx->is_noselect) {
  1051. $mbx = $oMbx->mailboxname_full;
  1052. if ($unseen_type == 2 ||
  1053. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  1054. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
  1055. } else {
  1056. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
  1057. }
  1058. sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
  1059. } else {
  1060. $oMbx->unseen = $oMbx->total = false;
  1061. $tag = false;
  1062. }
  1063. $oMbx->tag = $tag;
  1064. $aMbxs[$i] =& $oMbx;
  1065. }
  1066. // execute all the queries at once
  1067. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  1068. $cnt = count($aMbxs);
  1069. for($i=0;$i<$cnt;++$i) {
  1070. $oMbx =& $aMbxs[$i];
  1071. $tag = $oMbx->tag;
  1072. if ($tag && $aServerResponse[$tag] == 'OK') {
  1073. $sResponse = implode('', $aResponse[$tag]);
  1074. if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
  1075. $oMbx->unseen = $regs[1];
  1076. }
  1077. if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
  1078. $oMbx->total = $regs[1];
  1079. }
  1080. }
  1081. unset($oMbx->tag);
  1082. }
  1083. } else if ($unseen_notify == 2) { // INBOX only
  1084. $cnt = count($aMbxs);
  1085. for($i=0;$i<$cnt;++$i) {
  1086. $oMbx =& $aMbxs[$i];
  1087. if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
  1088. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  1089. if ($unseen_type == 2 ||
  1090. ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
  1091. $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
  1092. $oMbx->unseen = $aStatus['UNSEEN'];
  1093. $oMbx->total = $aStatus['MESSAGES'];
  1094. } else {
  1095. $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
  1096. }
  1097. $aMbxs[$i] =& $oMbx;
  1098. if (!$move_to_trash && $trash_folder) {
  1099. break;
  1100. } else {
  1101. // trash comes after INBOX
  1102. if ($oMbx->mailboxname_full == $trash_folder) {
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. ?>