imap_mailbox.php 44 KB

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