imap_mailbox.php 45 KB

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