imap_mailbox.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. // TODO: implement mailbox tree caching. maybe store object in session?
  835. $noselect = false;
  836. $noinferiors = false;
  837. require_once(SM_PATH . 'include/load_prefs.php');
  838. if ($show_only_subscribed_folders) {
  839. $lsub_cmd = 'LSUB';
  840. } else {
  841. $lsub_cmd = 'LIST';
  842. }
  843. /* LSUB array */
  844. $lsub_ary = sqimap_run_command ($imap_stream, "$lsub_cmd \"$folder_prefix\" \"*\"",
  845. true, $response, $message);
  846. $lsub_ary = compact_mailboxes_response($lsub_ary);
  847. /* Check to see if we have an INBOX */
  848. $has_inbox = false;
  849. for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
  850. if (preg_match("/^\*\s+$lsub_cmd.*\s\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
  851. $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
  852. // in case of an unsubscribed inbox an imap server can
  853. // return the inbox in the lsub results with a \NoSelect
  854. // flag.
  855. if (!preg_match("/\*\s+$lsub_cmd\s+\(.*\\\\NoSelect.*\).*/i",$lsub_ary[$i])) {
  856. $has_inbox = true;
  857. } else {
  858. // remove the result and request it again with a list
  859. // response at a later stage.
  860. unset($lsub_ary[$i]);
  861. // re-index the array otherwise the addition of the LIST
  862. // response will fail in PHP 4.1.2 and probably other older versions
  863. $lsub_ary = array_values($lsub_ary);
  864. }
  865. break;
  866. }
  867. }
  868. if ($has_inbox == false) {
  869. // do a list request for inbox because we should always show
  870. // inbox even if the user isn't subscribed to it.
  871. $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
  872. true, $response, $message);
  873. $inbox_ary = compact_mailboxes_response($inbox_ary);
  874. if (count($inbox_ary)) {
  875. $lsub_ary[] = $inbox_ary[0];
  876. }
  877. }
  878. /*
  879. * Section about removing the last element was removed
  880. * We don't return "* OK" anymore from sqimap_read_data
  881. */
  882. $sorted_lsub_ary = array();
  883. $cnt = count($lsub_ary);
  884. for ($i = 0; $i < $cnt; $i++) {
  885. $mbx = find_mailbox_name($lsub_ary[$i]);
  886. // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
  887. if ($imap_server_type != "uw") {
  888. $noselect = check_is_noselect($lsub_ary[$i]);
  889. $noinferiors = check_is_noinferiors($lsub_ary[$i]);
  890. }
  891. if (substr($mbx, -1) == $delimiter) {
  892. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  893. }
  894. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  895. }
  896. // FIX ME this requires a config setting inside conf.pl instead of checking on server type
  897. if ($imap_server_type == "uw") {
  898. $aQuery = array();
  899. $aTag = array();
  900. // prepare an array with queries
  901. foreach ($sorted_lsub_ary as $aMbx) {
  902. $mbx = stripslashes($aMbx['mbx']);
  903. sqimap_prepare_pipelined_query('LIST "" ' . sqimap_encode_mailbox_name($mbx), $tag, $aQuery, false);
  904. $aTag[$tag] = $mbx;
  905. }
  906. $sorted_lsub_ary = array();
  907. // execute all the queries at once
  908. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  909. foreach($aTag as $tag => $mbx) {
  910. if ($aServerResponse[$tag] == 'OK') {
  911. $sResponse = implode('', $aResponse[$tag]);
  912. $noselect = check_is_noselect($sResponse);
  913. $noinferiors = check_is_noinferiors($sResponse);
  914. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  915. }
  916. }
  917. $cnt = count($sorted_lsub_ary);
  918. }
  919. $sorted_lsub_ary = array_values($sorted_lsub_ary);
  920. usort($sorted_lsub_ary, 'mbxSort');
  921. $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
  922. return $boxestree;
  923. }
  924. /**
  925. * Callback function used for sorting mailboxes in sqimap_mailbox_tree
  926. * @param string $a
  927. * @param string $b
  928. * @return integer see php strnatcasecmp()
  929. * @since 1.5.1
  930. */
  931. function mbxSort($a, $b) {
  932. return strnatcasecmp($a['mbx'], $b['mbx']);
  933. }
  934. /**
  935. * Fills mailbox object
  936. *
  937. * Some code fragments are present in 1.3.0 - 1.4.4.
  938. * @param array $mbx_ary
  939. * @param $mbxs
  940. * @param stream $imap_stream imap connection resource
  941. * @return object see mailboxes class
  942. * @since 1.5.0
  943. */
  944. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
  945. global $data_dir, $username, $list_special_folders_first,
  946. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  947. $move_to_trash, $move_to_sent, $save_as_draft,
  948. $delimiter, $imap_server_type;
  949. // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  950. /* create virtual root node */
  951. $mailboxes= new mailboxes();
  952. $mailboxes->is_root = true;
  953. $trail_del = false;
  954. $start = 0;
  955. if (isset($folder_prefix) && ($folder_prefix != '')) {
  956. $start = substr_count($folder_prefix,$delimiter);
  957. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  958. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  959. } else {
  960. $mailboxes->mailboxname_full = $folder_prefix;
  961. $start++;
  962. }
  963. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  964. } else {
  965. $start = 0;
  966. }
  967. $cnt = count($mbx_ary);
  968. for ($i=0; $i < $cnt; $i++) {
  969. if ($mbx_ary[$i]['mbx'] !='' ) {
  970. $mbx = new mailboxes();
  971. $mailbox = $mbx_ary[$i]['mbx'];
  972. /*
  973. * Set the is_special flag if it concerned a special mailbox.
  974. * Used for displaying the special folders on top in the mailbox
  975. * tree displaying code.
  976. */
  977. $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
  978. $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
  979. $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
  980. $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
  981. if (!$mbx->is_special)
  982. $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
  983. if (isset($mbx_ary[$i]['unseen'])) {
  984. $mbx->unseen = $mbx_ary[$i]['unseen'];
  985. }
  986. if (isset($mbx_ary[$i]['nummessages'])) {
  987. $mbx->total = $mbx_ary[$i]['nummessages'];
  988. }
  989. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  990. $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
  991. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  992. if ($r_del_pos) {
  993. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  994. } else { /* mailbox is root folder */
  995. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  996. }
  997. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  998. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  999. }
  1000. }
  1001. sqimap_utf7_decode_mbx_tree($mailboxes);
  1002. sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
  1003. return $mailboxes;
  1004. }
  1005. /**
  1006. * @param object $mbx_tree
  1007. * @since 1.5.0
  1008. */
  1009. function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
  1010. if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
  1011. $mbx_tree->mailboxname_sub = _("INBOX");
  1012. else
  1013. $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
  1014. if ($mbx_tree->mbxs) {
  1015. $iCnt = count($mbx_tree->mbxs);
  1016. for ($i=0;$i<$iCnt;++$i) {
  1017. $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
  1018. }
  1019. }
  1020. }
  1021. /**
  1022. * @param object $mbx_tree
  1023. * @param array $aMbxs
  1024. * @since 1.5.0
  1025. */
  1026. function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
  1027. if ($mbx_tree)
  1028. $aMbxs[] =& $mbx_tree;
  1029. if ($mbx_tree->mbxs) {
  1030. $iCnt = count($mbx_tree->mbxs);
  1031. for ($i=0;$i<$iCnt;++$i) {
  1032. sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
  1033. }
  1034. }
  1035. }
  1036. /**
  1037. * @param stream $imap_stream imap connection resource
  1038. * @param object $mbx_tree
  1039. * @since since 1.5.0
  1040. */
  1041. function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
  1042. global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
  1043. $aMbxs = $aQuery = array();
  1044. sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
  1045. // remove the root node
  1046. array_shift($aMbxs);
  1047. if($unseen_notify == 3) {
  1048. $cnt = count($aMbxs);
  1049. for($i=0;$i<$cnt;++$i) {
  1050. $oMbx =& $aMbxs[$i];
  1051. if (!$oMbx->is_noselect) {
  1052. $mbx = $oMbx->mailboxname_full;
  1053. if ($unseen_type == 2 ||
  1054. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  1055. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
  1056. } else {
  1057. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
  1058. }
  1059. sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
  1060. } else {
  1061. $oMbx->unseen = $oMbx->total = false;
  1062. $tag = false;
  1063. }
  1064. $oMbx->tag = $tag;
  1065. $aMbxs[$i] =& $oMbx;
  1066. }
  1067. // execute all the queries at once
  1068. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  1069. $cnt = count($aMbxs);
  1070. for($i=0;$i<$cnt;++$i) {
  1071. $oMbx =& $aMbxs[$i];
  1072. $tag = $oMbx->tag;
  1073. if ($tag && $aServerResponse[$tag] == 'OK') {
  1074. $sResponse = implode('', $aResponse[$tag]);
  1075. if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
  1076. $oMbx->unseen = $regs[1];
  1077. }
  1078. if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
  1079. $oMbx->total = $regs[1];
  1080. }
  1081. }
  1082. unset($oMbx->tag);
  1083. }
  1084. } else if ($unseen_notify == 2) { // INBOX only
  1085. $cnt = count($aMbxs);
  1086. for($i=0;$i<$cnt;++$i) {
  1087. $oMbx =& $aMbxs[$i];
  1088. if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
  1089. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  1090. if ($unseen_type == 2 ||
  1091. ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
  1092. $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
  1093. $oMbx->unseen = $aStatus['UNSEEN'];
  1094. $oMbx->total = $aStatus['MESSAGES'];
  1095. } else {
  1096. $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
  1097. }
  1098. $aMbxs[$i] =& $oMbx;
  1099. if (!$move_to_trash && $trash_folder) {
  1100. break;
  1101. } else {
  1102. // trash comes after INBOX
  1103. if ($oMbx->mailboxname_full == $trash_folder) {
  1104. break;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. }
  1111. ?>