imap_mailbox.php 46 KB

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