imap_mailbox.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. *
  296. * @param stream $imap_stream imap connection resource
  297. * @param string $mailbox mailbox name
  298. * @param array $mailboxlist (since 1.5.1) optional array of mailboxes from
  299. * sqimap_get_mailboxes() (to avoid having to talk to imap server)
  300. * @return boolean
  301. * @since 1.0 or older
  302. */
  303. function sqimap_mailbox_exists ($imap_stream, $mailbox, $mailboxlist=null) {
  304. if (!isset($mailbox) || empty($mailbox)) {
  305. return false;
  306. }
  307. if (is_array($mailboxlist)) {
  308. // use previously retrieved mailbox list
  309. foreach ($mailboxlist as $mbox) {
  310. if ($mbox['unformatted-dm'] == $mailbox) { return true; }
  311. }
  312. return false;
  313. } else {
  314. // go to imap server
  315. $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
  316. true, $response, $message);
  317. return isset($mbx[0]);
  318. }
  319. }
  320. /**
  321. * Selects a mailbox
  322. * Before 1.3.0 used more arguments and returned data depended on those argumements.
  323. * @param stream $imap_stream imap connection resource
  324. * @param string $mailbox mailbox name
  325. * @return array results of select command (on success - permanentflags, flags and rights)
  326. * @since 1.0 or older
  327. */
  328. function sqimap_mailbox_select ($imap_stream, $mailbox) {
  329. if ($mailbox == 'None') {
  330. return;
  331. }
  332. $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
  333. true, $response, $message);
  334. $result = array();
  335. for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
  336. if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
  337. $result[strtoupper($regs[1])] = $regs[2];
  338. } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
  339. $result[strtoupper($regs[2])] = $regs[1];
  340. } else {
  341. if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
  342. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
  343. $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
  344. } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
  345. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
  346. $result['FLAGS'] = explode(' ',strtolower($regs[1]));
  347. }
  348. }
  349. }
  350. if (!isset($result['PERMANENTFLAGS'])) {
  351. $result['PERMANENTFLAGS'] = $result['FLAGS'];
  352. }
  353. if (preg_match('/^\[(.+)\]/',$message, $regs)) {
  354. $result['RIGHTS']=strtoupper($regs[1]);
  355. }
  356. return $result;
  357. }
  358. /**
  359. * Creates a folder.
  360. *
  361. * Mailbox is automatically subscribed.
  362. *
  363. * Set $type to string that does not match 'noselect' (case insensitive),
  364. * if you don't want to prepend delimiter to mailbox name. Please note
  365. * that 'noinferiors' might be used someday as keyword for folders
  366. * that store only messages.
  367. * @param stream $imap_steam imap connection resource
  368. * @param string $mailbox mailbox name
  369. * @param string $type folder type.
  370. * @since 1.0 or older
  371. */
  372. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  373. global $delimiter;
  374. if (strtolower($type) == 'noselect') {
  375. $mailbox .= $delimiter;
  376. }
  377. $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
  378. sqimap_encode_mailbox_name($mailbox),
  379. true, $response, $message);
  380. sqimap_subscribe ($imap_stream, $mailbox);
  381. }
  382. /**
  383. * Subscribes to an existing folder.
  384. * @param stream $imap_stream imap connection resource
  385. * @param string $mailbox mailbox name
  386. * @param boolean $debug (since 1.5.1)
  387. * @since 1.0 or older
  388. */
  389. function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
  390. $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
  391. sqimap_encode_mailbox_name($mailbox),
  392. $debug, $response, $message);
  393. }
  394. /**
  395. * Unsubscribes from an existing folder
  396. * @param stream $imap_stream imap connection resource
  397. * @param string $mailbox mailbox name
  398. * @since 1.0 or older
  399. */
  400. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  401. $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
  402. sqimap_encode_mailbox_name($mailbox),
  403. false, $response, $message);
  404. }
  405. /**
  406. * Deletes the given folder
  407. * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
  408. * @param stream $imap_stream imap connection resource
  409. * @param string $mailbox mailbox name
  410. * @since 1.0 or older
  411. */
  412. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  413. global $data_dir, $username;
  414. sqimap_unsubscribe ($imap_stream, $mailbox);
  415. if (sqimap_mailbox_exists($imap_stream, $mailbox)) {
  416. $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
  417. sqimap_encode_mailbox_name($mailbox),
  418. true, $response, $message);
  419. if ($response !== 'OK') {
  420. // subscribe again
  421. sqimap_subscribe ($imap_stream, $mailbox);
  422. } else {
  423. do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
  424. removePref($data_dir, $username, "thread_$mailbox");
  425. removePref($data_dir, $username, "collapse_folder_$mailbox");
  426. }
  427. }
  428. }
  429. /**
  430. * Determines if the user is subscribed to the folder or not
  431. * @param stream $imap_stream imap connection resource
  432. * @param string $mailbox mailbox name
  433. * @return boolean
  434. * @since 1.2.0
  435. */
  436. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  437. $boxesall = sqimap_mailbox_list ($imap_stream);
  438. foreach ($boxesall as $ref) {
  439. if ($ref['unformatted'] == $folder) {
  440. return true;
  441. }
  442. }
  443. return false;
  444. }
  445. /**
  446. * Renames a mailbox.
  447. * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
  448. * @param stream $imap_stream imap connection resource
  449. * @param string $old_name mailbox name
  450. * @param string $new_name new mailbox name
  451. * @since 1.2.3
  452. */
  453. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  454. if ( $old_name != $new_name ) {
  455. global $delimiter, $imap_server_type, $data_dir, $username;
  456. if ( substr( $old_name, -1 ) == $delimiter ) {
  457. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  458. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  459. $postfix = $delimiter;
  460. } else {
  461. $postfix = '';
  462. }
  463. $boxesall = sqimap_mailbox_list_all($imap_stream);
  464. $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
  465. ' ' . sqimap_encode_mailbox_name($new_name);
  466. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  467. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  468. $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
  469. $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
  470. removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
  471. removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
  472. sqimap_subscribe($imap_stream, $new_name.$postfix);
  473. setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
  474. setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
  475. do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
  476. $l = strlen( $old_name ) + 1;
  477. $p = 'unformatted';
  478. foreach ($boxesall as $box) {
  479. if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
  480. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  481. /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
  482. if ($imap_server_type == 'cyrus') {
  483. $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
  484. $data = sqimap_run_command($imap_stream, $cmd, false,
  485. $response, $message);
  486. }
  487. $was_subscribed = sqimap_mailbox_is_subscribed($imap_stream, $box[$p]);
  488. if ( $was_subscribed ) {
  489. sqimap_unsubscribe($imap_stream, $box[$p]);
  490. }
  491. $oldpref_thread = getPref($data_dir, $username, 'thread_'.$box[$p]);
  492. $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$box[$p]);
  493. removePref($data_dir, $username, 'thread_'.$box[$p]);
  494. removePref($data_dir, $username, 'collapse_folder_'.$box[$p]);
  495. if ( $was_subscribed ) {
  496. sqimap_subscribe($imap_stream, $new_sub);
  497. }
  498. setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref_thread);
  499. setPref($data_dir, $username, 'collapse_folder_'.$new_sub, $oldpref_collapse);
  500. do_hook_function('rename_or_delete_folder',
  501. $args = array($box[$p], 'rename', $new_sub));
  502. }
  503. }
  504. }
  505. }
  506. /**
  507. * Formats a mailbox into parts for the $boxesall array
  508. *
  509. * The parts are:
  510. * <ul>
  511. * <li>raw - Raw LIST/LSUB response from the IMAP server
  512. * <li>formatted - nicely formatted folder name
  513. * <li>unformatted - unformatted, but with delimiter at end removed
  514. * <li>unformatted-dm - folder name as it appears in raw response
  515. * <li>unformatted-disp - unformatted without $folder_prefix
  516. * <li>id - TODO: document me
  517. * <li>flags - TODO: document me
  518. * </ul>
  519. * Before 1.2.0 used third argument for delimiter.
  520. *
  521. * Before 1.5.1 used second argument for lsub line. Argument was removed in order to use
  522. * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags
  523. * array (for example, "\NoSelect" in addition to "noselect")
  524. * @param array $line
  525. * @return array
  526. * @since 1.0 or older
  527. * @todo document id and flags keys in boxes array and function arguments.
  528. */
  529. function sqimap_mailbox_parse ($line) {
  530. global $folder_prefix, $delimiter;
  531. /* Process each folder line */
  532. for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
  533. /* Store the raw IMAP reply */
  534. if (isset($line[$g])) {
  535. $boxesall[$g]['raw'] = $line[$g];
  536. } else {
  537. $boxesall[$g]['raw'] = '';
  538. }
  539. /* Count number of delimiters ($delimiter) in folder name */
  540. $mailbox = find_mailbox_name($line[$g]);
  541. $dm_count = substr_count($mailbox, $delimiter);
  542. if (substr($mailbox, -1) == $delimiter) {
  543. /* If name ends in delimiter, decrement count by one */
  544. $dm_count--;
  545. }
  546. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  547. $boxesallbyname[$mailbox] = $g;
  548. $parentfolder = readMailboxParent($mailbox, $delimiter);
  549. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  550. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  551. (isset($boxesallbyname[$parentfolder]) &&
  552. (strlen($parentfolder) > 0) ) ) {
  553. $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
  554. if ($indent > 0) {
  555. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  556. } else {
  557. $boxesall[$g]['formatted'] = '';
  558. }
  559. $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  560. } else {
  561. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  562. }
  563. $boxesall[$g]['unformatted-dm'] = $mailbox;
  564. if (substr($mailbox, -1) == $delimiter) {
  565. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  566. }
  567. $boxesall[$g]['unformatted'] = $mailbox;
  568. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  569. $mailbox = substr($mailbox, strlen($folder_prefix));
  570. }
  571. $boxesall[$g]['unformatted-disp'] = $mailbox;
  572. $boxesall[$g]['id'] = $g;
  573. $boxesall[$g]['flags'] = array();
  574. if (isset($line[$g])) {
  575. ereg("\(([^)]*)\)",$line[$g],$regs);
  576. /**
  577. * Since 1.5.1 flags are stored with RFC3501 naming
  578. * and also the old way for backwards compatibility
  579. * so for example "\NoSelect" and "noselect"
  580. */
  581. $flags = trim($regs[1]);
  582. if ($flags) {
  583. $flagsarr = explode(' ',$flags);
  584. $flagsarrnew=$flagsarr;
  585. // add old type
  586. foreach ($flagsarr as $flag) {
  587. $flagsarrnew[]=strtolower(str_replace('\\', '',$flag));
  588. }
  589. $boxesall[$g]['flags']=$flagsarrnew;
  590. }
  591. }
  592. }
  593. return $boxesall;
  594. }
  595. /**
  596. * Returns list of options (to be echoed into select statement
  597. * based on available mailboxes and separators
  598. * Caller should surround options with <select ...> </select> and
  599. * any formatting.
  600. * @param stream $imap_stream imap connection resource to query for mailboxes
  601. * @param array $show_selected array containing list of mailboxes to pre-select (0 if none)
  602. * @param array $folder_skip array of folders to keep out of option list (compared in lower)
  603. * @param $boxes list of already fetched boxes (for places like folder panel, where
  604. * you know these options will be shown 3 times in a row.. (most often unset).
  605. * @param string $flag (since 1.4.1) flag to check for in mailbox flags, used to filter out mailboxes.
  606. * 'noselect' by default to remove unselectable mailboxes.
  607. * 'noinferiors' used to filter out folders that can not contain subfolders.
  608. * NULL to avoid flag check entirely.
  609. * NOTE: noselect and noiferiors are used internally. The IMAP representation is
  610. * \NoSelect and \NoInferiors
  611. * @param boolean $use_long_format (since 1.4.1) override folder display preference and always show full folder name.
  612. * @return string html formated mailbox selection options
  613. * @since 1.3.2
  614. */
  615. function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
  616. $flag = 'noselect', $use_long_format = false ) {
  617. global $username, $data_dir;
  618. $mbox_options = '';
  619. if ( $use_long_format ) {
  620. $shorten_box_names = 0;
  621. } else {
  622. $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
  623. }
  624. if ($boxes == 0) {
  625. $boxes = sqimap_mailbox_list($imap_stream);
  626. }
  627. foreach ($boxes as $boxes_part) {
  628. if ($flag == NULL || (is_array($boxes_part['flags'])
  629. && !in_array($flag, $boxes_part['flags']))) {
  630. $box = $boxes_part['unformatted'];
  631. if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
  632. continue;
  633. }
  634. $lowerbox = strtolower($box);
  635. // mailboxes are casesensitive => inbox.sent != inbox.Sent
  636. // nevermind, to many dependencies this should be fixed!
  637. if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
  638. $box2 = _("INBOX");
  639. } else {
  640. switch ($shorten_box_names)
  641. {
  642. case 2: /* delimited, style = 2 */
  643. $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '.&nbsp;', htmlspecialchars($boxes_part['formatted']));
  644. break;
  645. case 1: /* indent, style = 1 */
  646. $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '&nbsp;&nbsp;', htmlspecialchars($boxes_part['formatted']));
  647. break;
  648. default: /* default, long names, style = 0 */
  649. $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
  650. break;
  651. }
  652. }
  653. if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
  654. $mbox_options .= '<option value="' . htmlspecialchars($box) .'" selected="selected">'.$box2.'</option>' . "\n";
  655. } else {
  656. $mbox_options .= '<option value="' . htmlspecialchars($box) .'">'.$box2.'</option>' . "\n";
  657. }
  658. }
  659. }
  660. return $mbox_options;
  661. }
  662. /**
  663. * Returns sorted mailbox lists in several different ways.
  664. *
  665. * Since 1.5.1 most of the functionality has been moved to new function sqimap_get_mailboxes
  666. *
  667. * See comment on sqimap_mailbox_parse() for info about the returned array.
  668. * @param resource $imap_stream imap connection resource
  669. * @param boolean $force force update of mailbox listing. available since 1.4.2 and 1.5.0
  670. * @return array list of mailboxes
  671. * @since 1.0 or older
  672. */
  673. function sqimap_mailbox_list($imap_stream, $force=false) {
  674. global $boxesnew,$show_only_subscribed_folders;
  675. if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
  676. $boxesnew=sqimap_get_mailboxes($imap_stream,$force,$show_only_subscribed_folders);
  677. }
  678. return $boxesnew;
  679. }
  680. /**
  681. * Returns a list of all folders, subscribed or not
  682. *
  683. * Since 1.5.1 code moved to sqimap_get_mailboxes()
  684. *
  685. * @param stream $imap_stream imap connection resource
  686. * @return array see sqimap_mailbox_parse()
  687. * @since 1.0 or older
  688. */
  689. function sqimap_mailbox_list_all($imap_stream) {
  690. global $show_only_subscribed_folders;
  691. // fourth argument prevents registration of retrieved list of mailboxes in session
  692. $boxes=sqimap_get_mailboxes($imap_stream,true,false,false);
  693. return $boxes;
  694. }
  695. /**
  696. * Gets the list of mailboxes for sqimap_maolbox_tree and sqimap_mailbox_list
  697. *
  698. * This is because both of those functions had duplicated logic, but with slightly different
  699. * implementations. This will make both use the same implementation, which should make it
  700. * easier to maintain and easier to modify in the future
  701. * @param stream $imap_stream imap connection resource
  702. * @param bool $force force a reload and ignore cache
  703. * @param bool $show_only_subscribed controls listing of visible or all folders
  704. * @param bool $session_register controls registration of retrieved data in session.
  705. * @return object boxesnew - array of mailboxes and their attributes
  706. * @since 1.5.1
  707. */
  708. function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=true,$session_register=true) {
  709. global $show_only_subscribed_folders,$noselect_fix_enable,$folder_prefix,
  710. $list_special_folders_first,$imap_server_type;
  711. $inbox_subscribed = false;
  712. $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
  713. if ($show_only_subscribed) { $show_only_subscribed=$show_only_subscribed_folders; }
  714. require_once(SM_PATH . 'include/load_prefs.php');
  715. /**
  716. * There are three main listing commands we can use in IMAP:
  717. * LSUB shows just the list of subscribed folders
  718. * may include flags, but these are not necessarily accurate or authoratative
  719. * \NoSelect has special meaning: the folder does not exist -OR- it means this
  720. * folder is not subscribed but children may be
  721. * [RFC-2060]
  722. * LIST this shows every mailbox on the system
  723. * flags are always included and are accurate and authoratative
  724. * \NoSelect means folder should not be selected
  725. * [RFC-2060]
  726. * LIST (SUBSCRIBED) implemented with LIST-SUBSCRIBED extension
  727. * this is like list but returns only subscribed folders
  728. * flag meanings are like LIST, not LSUB
  729. * \NonExistent means mailbox doesn't exist
  730. * \PlaceHolder means parent is not valid (selectable), but one or more children are
  731. * \NoSelect indeed means that the folder should not be selected
  732. * IMAPEXT-LIST-EXTENSIONS-04 August 2003 B. Leiba
  733. */
  734. if (!$show_only_subscribed) {
  735. $lsub = 'LIST';
  736. $sub_cache_name='list_cache';
  737. } elseif ($listsubscribed) {
  738. $lsub = 'LIST (SUBSCRIBED)';
  739. $sub_cache_name='listsub_cache';
  740. } else {
  741. $lsub = 'LSUB';
  742. $sub_cache_name='lsub_cache';
  743. }
  744. // Some IMAP servers allow subfolders to exist even if the parent folders do not
  745. // This fixes some problems with the folder list when this is the case, causing the
  746. // NoSelect folders to be displayed
  747. if ($noselect_fix_enable) {
  748. $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
  749. $list_args = "LIST \"$folder_prefix\" \"*%\"";
  750. } else {
  751. $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
  752. $list_args = "LIST \"$folder_prefix\" \"*\"";
  753. }
  754. // get subscribed mailbox list from cache (session)
  755. // if not there, then get it from the imap server and store in cache
  756. sqsession_is_active();
  757. if (!$force) {
  758. sqgetGlobalVar($sub_cache_name,$lsub_cache,SQ_SESSION);
  759. }
  760. if (!empty($lsub_cache)) {
  761. $lsub_assoc_ary=$lsub_cache;
  762. } else {
  763. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args, true, $response, $message);
  764. $lsub_ary = compact_mailboxes_response($lsub_ary);
  765. if (!empty($lsub_ary)) {
  766. foreach ($lsub_ary as $rawline) {
  767. $temp_mailbox_name=find_mailbox_name($rawline);
  768. $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
  769. }
  770. unset($lsub_ary);
  771. sqsession_register($lsub_assoc_ary,$sub_cache_name);
  772. }
  773. }
  774. // Now to get the mailbox flags
  775. // The LSUB response may return \NoSelect flags, etc. but it is optional
  776. // according to RFC3501, and even when returned it may not be accurate
  777. // or authoratative. LIST will always return accurate results.
  778. if (($lsub == 'LIST') || ($lsub == 'LIST (SUBSCRIBED)')) {
  779. // we've already done a LIST or LIST (SUBSCRIBED)
  780. // and NOT a LSUB, so no need to do it again
  781. $list_assoc_ary = $lsub_assoc_ary;
  782. } else {
  783. // we did a LSUB so now we need to do a LIST
  784. // first see if it is in cache
  785. $list_cache_name='list_cache';
  786. if (!$force) {
  787. sqgetGlobalVar($list_cache_name,$list_cache,SQ_SESSION);
  788. }
  789. if (!empty($list_cache)) {
  790. $list_assoc_ary=$list_cache;
  791. // we could store this in list_cache_name but not necessary
  792. } else {
  793. // not in cache so we need to go get it from the imap server
  794. $list_assoc_ary = array();
  795. $list_ary = sqimap_run_command($imap_stream, $list_args,
  796. true, $response, $message);
  797. $list_ary = compact_mailboxes_response($list_ary);
  798. if (!empty($list_ary)) {
  799. foreach ($list_ary as $rawline) {
  800. $temp_mailbox_name=find_mailbox_name($rawline);
  801. $list_assoc_ary[$temp_mailbox_name]=$rawline;
  802. }
  803. unset($list_ary);
  804. sqsession_register($list_assoc_ary,$list_cache_name);
  805. }
  806. }
  807. }
  808. // If they aren't subscribed to the inbox, then add it anyway (if its in LIST)
  809. $inbox_subscribed=false;
  810. if (!empty($lsub_assoc_ary)) {
  811. foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
  812. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  813. $inbox_subscribed=true;
  814. }
  815. }
  816. }
  817. if (!$inbox_subscribed) {
  818. if (!empty($list_assoc_ary)) {
  819. foreach ($list_assoc_ary as $temp_mailbox_name=>$rawline) {
  820. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  821. $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
  822. }
  823. }
  824. }
  825. }
  826. // Now we have the raw output, we need to create an array of mailbox names we will return
  827. if (!$show_only_subscribed) {
  828. $final_folders_assoc_ary=$list_assoc_ary;
  829. } else {
  830. /**
  831. * only show subscribed folders
  832. * we need to merge the folders here... we can't trust the flags, etc. from the lsub_assoc_array
  833. * so we use the lsub_assoc_array as the list of folders and the values come from list_assoc_array
  834. */
  835. if (!empty($lsub_assoc_ary)) {
  836. foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
  837. if (!empty($list_assoc_ary[$temp_mailbox_name])) {
  838. $final_folders_assoc_ary[$temp_mailbox_name]=$list_assoc_ary[$temp_mailbox_name];
  839. }
  840. }
  841. }
  842. }
  843. // Now produce a flat, sorted list
  844. if (!empty($final_folders_assoc_ary)) {
  845. uksort($final_folders_assoc_ary,'strnatcasecmp');
  846. foreach ($final_folders_assoc_ary as $temp_mailbox_name=>$rawline) {
  847. $final_folders_ary[]=$rawline;
  848. }
  849. }
  850. // this will put it into an array we can use later
  851. // containing:
  852. // raw - Raw LIST/LSUB response from the IMAP server
  853. // formatted - formatted folder name
  854. // unformatted - unformatted, but with the delimiter at the end removed
  855. // unformated-dm - folder name as it appears in raw response
  856. // unformatted-disp - unformatted without $folder_prefix
  857. // id - the array element number (0, 1, 2, etc.)
  858. // flags - mailbox flags
  859. if (!empty($final_folders_ary)) {
  860. $boxesall = sqimap_mailbox_parse($final_folders_ary);
  861. } else {
  862. // they have no mailboxes
  863. $boxesall=array();
  864. }
  865. /* Now, lets sort for special folders */
  866. $boxesnew = $used = array();
  867. /* Find INBOX */
  868. $cnt = count($boxesall);
  869. $used = array_pad($used,$cnt,false);
  870. $has_inbox = false;
  871. for($k = 0; $k < $cnt; ++$k) {
  872. if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
  873. $boxesnew[] = $boxesall[$k];
  874. $used[$k] = true;
  875. $has_inbox = true;
  876. break;
  877. }
  878. }
  879. if ($has_inbox == false) {
  880. // do a list request for inbox because we should always show
  881. // inbox even if the user isn't subscribed to it.
  882. $inbox_ary = sqimap_run_command($imap_stream, 'LIST "" "INBOX"',
  883. true, $response, $message);
  884. $inbox_ary = compact_mailboxes_response($inbox_ary);
  885. if (count($inbox_ary)) {
  886. $inbox_entry = sqimap_mailbox_parse($inbox_ary);
  887. // add it on top of the list
  888. if (!empty($boxesnew)) {
  889. array_unshift($boxesnew,$inbox_entry[0]);
  890. } else {
  891. $boxesnew[]=$inbox_entry[0];
  892. }
  893. /* array_unshift($used,true); */
  894. }
  895. }
  896. /* List special folders and their subfolders, if requested. */
  897. if ($list_special_folders_first) {
  898. for($k = 0; $k < $cnt; ++$k) {
  899. if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
  900. $boxesnew[] = $boxesall[$k];
  901. $used[$k] = true;
  902. }
  903. }
  904. }
  905. /* Find INBOX's children */
  906. for($k = 0; $k < $cnt; ++$k) {
  907. $isboxbelow=isBoxBelow(strtoupper($boxesall[$k]['unformatted']),'INBOX');
  908. if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
  909. $is_inbox=1;
  910. } else {
  911. $is_inbox=0;
  912. }
  913. if (!$used[$k] && $isboxbelow && $is_inbox) {
  914. $boxesnew[] = $boxesall[$k];
  915. $used[$k] = true;
  916. }
  917. }
  918. /* Rest of the folders */
  919. for($k = 0; $k < $cnt; $k++) {
  920. if (!$used[$k]) {
  921. $boxesnew[] = $boxesall[$k];
  922. }
  923. }
  924. /**
  925. * Don't register boxes in session, if $session_register is set to false
  926. * Prevents registration of sqimap_mailbox_list_all() results.
  927. */
  928. if ($session_register) sqsession_register($boxesnew,'boxesnew');
  929. return $boxesnew;
  930. }
  931. /**
  932. * Fills mailbox object
  933. *
  934. * this is passed the mailbox array by left_main.php
  935. * who has previously obtained it from sqimap_get_mailboxes
  936. * that way, the raw mailbox list is available in left_main to other
  937. * things besides just sqimap_mailbox_tree
  938. * imap_stream is just used now to get status info
  939. *
  940. * most of the functionality is moved to sqimap_get_mailboxes
  941. * also takes care of TODO items:
  942. * caching mailbox tree
  943. * config setting for UW imap section (not needed now)
  944. *
  945. * Some code fragments are present in 1.3.0 - 1.4.4.
  946. * @param stream $imap_stream imap connection resource
  947. * @param array $lsub_ary output array from sqimap_get_mailboxes (contains mailboxes and flags)
  948. * @return object see mailboxes class.
  949. * @since 1.5.0
  950. */
  951. function sqimap_mailbox_tree($imap_stream,$lsub_ary) {
  952. $sorted_lsub_ary = array();
  953. $cnt = count($lsub_ary);
  954. for ($i = 0; $i < $cnt; $i++) {
  955. $mbx=$lsub_ary[$i]['unformatted'];
  956. $flags=$lsub_ary[$i]['flags'];
  957. $noinferiors=0;
  958. if (in_array('\Noinferiors',$flags)) { $noinferiors=1; }
  959. if (in_array('\NoInferiors',$flags)) { $noinferiors=1; }
  960. if (in_array('\HasNoChildren',$flags)) { $noinferiors=1; }
  961. $noselect=0;
  962. if (in_array('\NoSelect',$flags)) { $noselect=1; }
  963. /**
  964. * LIST (SUBSCRIBED) has two new flags, \NonExistent which means the mailbox is subscribed to
  965. * but doesn't exist, and \PlaceHolder which is similar (but not the same) as \NoSelect
  966. * For right now, we'll treat these the same as \NoSelect and this behavior can be changed
  967. * later if needed
  968. */
  969. if (in_array('\NonExistent',$flags)) { $noselect=1; }
  970. if (in_array('\PlaceHolder',$flags)) { $noselect=1; }
  971. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  972. }
  973. $sorted_lsub_ary = array_values($sorted_lsub_ary);
  974. usort($sorted_lsub_ary, 'mbxSort');
  975. $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
  976. return $boxestree;
  977. }
  978. /**
  979. * Callback function used for sorting mailboxes in sqimap_mailbox_tree
  980. * @param string $a
  981. * @param string $b
  982. * @return integer see php strnatcasecmp()
  983. * @since 1.5.1
  984. */
  985. function mbxSort($a, $b) {
  986. return strnatcasecmp($a['mbx'], $b['mbx']);
  987. }
  988. /**
  989. * Fills mailbox object
  990. *
  991. * Some code fragments are present in 1.3.0 - 1.4.4.
  992. * @param array $mbx_ary
  993. * @param $mbxs
  994. * @param stream $imap_stream imap connection resource
  995. * @return object see mailboxes class
  996. * @since 1.5.0
  997. */
  998. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
  999. global $data_dir, $username, $list_special_folders_first,
  1000. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  1001. $move_to_trash, $move_to_sent, $save_as_draft,
  1002. $delimiter, $imap_server_type;
  1003. // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  1004. /* create virtual root node */
  1005. $mailboxes= new mailboxes();
  1006. $mailboxes->is_root = true;
  1007. $trail_del = false;
  1008. $start = 0;
  1009. if (isset($folder_prefix) && ($folder_prefix != '')) {
  1010. $start = substr_count($folder_prefix,$delimiter);
  1011. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  1012. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  1013. } else {
  1014. $mailboxes->mailboxname_full = $folder_prefix;
  1015. $start++;
  1016. }
  1017. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  1018. } else {
  1019. $start = 0;
  1020. }
  1021. $cnt = count($mbx_ary);
  1022. for ($i=0; $i < $cnt; $i++) {
  1023. if ($mbx_ary[$i]['mbx'] !='' ) {
  1024. $mbx = new mailboxes();
  1025. $mailbox = $mbx_ary[$i]['mbx'];
  1026. /*
  1027. * Set the is_special flag if it concerned a special mailbox.
  1028. * Used for displaying the special folders on top in the mailbox
  1029. * tree displaying code.
  1030. */
  1031. $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
  1032. $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
  1033. $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
  1034. $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
  1035. if (!$mbx->is_special)
  1036. $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
  1037. if (isset($mbx_ary[$i]['unseen'])) {
  1038. $mbx->unseen = $mbx_ary[$i]['unseen'];
  1039. }
  1040. if (isset($mbx_ary[$i]['nummessages'])) {
  1041. $mbx->total = $mbx_ary[$i]['nummessages'];
  1042. }
  1043. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  1044. $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
  1045. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  1046. if ($r_del_pos) {
  1047. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  1048. } else { /* mailbox is root folder */
  1049. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  1050. }
  1051. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  1052. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  1053. }
  1054. }
  1055. sqimap_utf7_decode_mbx_tree($mailboxes);
  1056. sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
  1057. return $mailboxes;
  1058. }
  1059. /**
  1060. * @param object $mbx_tree
  1061. * @since 1.5.0
  1062. */
  1063. function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
  1064. if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
  1065. $mbx_tree->mailboxname_sub = _("INBOX");
  1066. else
  1067. $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
  1068. if ($mbx_tree->mbxs) {
  1069. $iCnt = count($mbx_tree->mbxs);
  1070. for ($i=0;$i<$iCnt;++$i) {
  1071. sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
  1072. }
  1073. }
  1074. }
  1075. /**
  1076. * @param object $mbx_tree
  1077. * @param array $aMbxs
  1078. * @since 1.5.0
  1079. */
  1080. function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
  1081. if ($mbx_tree)
  1082. $aMbxs[] =& $mbx_tree;
  1083. if ($mbx_tree->mbxs) {
  1084. $iCnt = count($mbx_tree->mbxs);
  1085. for ($i=0;$i<$iCnt;++$i) {
  1086. sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
  1087. }
  1088. }
  1089. }
  1090. /**
  1091. * @param stream $imap_stream imap connection resource
  1092. * @param object $mbx_tree
  1093. * @since since 1.5.0
  1094. */
  1095. function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
  1096. global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
  1097. $aMbxs = $aQuery = array();
  1098. sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
  1099. // remove the root node
  1100. array_shift($aMbxs);
  1101. if($unseen_notify == 3) {
  1102. $cnt = count($aMbxs);
  1103. for($i=0;$i<$cnt;++$i) {
  1104. $oMbx =& $aMbxs[$i];
  1105. if (!$oMbx->is_noselect) {
  1106. $mbx = $oMbx->mailboxname_full;
  1107. if ($unseen_type == 2 ||
  1108. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  1109. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
  1110. } else {
  1111. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
  1112. }
  1113. sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
  1114. } else {
  1115. $oMbx->unseen = $oMbx->total = false;
  1116. $tag = false;
  1117. }
  1118. $oMbx->tag = $tag;
  1119. $aMbxs[$i] =& $oMbx;
  1120. }
  1121. // execute all the queries at once
  1122. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  1123. $cnt = count($aMbxs);
  1124. for($i=0;$i<$cnt;++$i) {
  1125. $oMbx =& $aMbxs[$i];
  1126. $tag = $oMbx->tag;
  1127. if ($tag && $aServerResponse[$tag] == 'OK') {
  1128. $sResponse = implode('', $aResponse[$tag]);
  1129. if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
  1130. $oMbx->unseen = $regs[1];
  1131. }
  1132. if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
  1133. $oMbx->total = $regs[1];
  1134. }
  1135. }
  1136. unset($oMbx->tag);
  1137. }
  1138. } else if ($unseen_notify == 2) { // INBOX only
  1139. $cnt = count($aMbxs);
  1140. for($i=0;$i<$cnt;++$i) {
  1141. $oMbx =& $aMbxs[$i];
  1142. if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
  1143. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  1144. if ($unseen_type == 2 ||
  1145. ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
  1146. $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
  1147. $oMbx->unseen = $aStatus['UNSEEN'];
  1148. $oMbx->total = $aStatus['MESSAGES'];
  1149. } else {
  1150. $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
  1151. }
  1152. $aMbxs[$i] =& $oMbx;
  1153. if (!$move_to_trash && $trash_folder) {
  1154. break;
  1155. } else {
  1156. // trash comes after INBOX
  1157. if ($oMbx->mailboxname_full == $trash_folder) {
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. /**
  1166. * Checks if folder is noselect (can't store messages)
  1167. *
  1168. * Function does not check if folder subscribed.
  1169. * @param stream $oImapStream imap connection resource
  1170. * @param string $sImapFolder imap folder name
  1171. * @param object $oBoxes mailboxes class object.
  1172. * @return boolean true, when folder has noselect flag. false in any other case.
  1173. * @since 1.5.1
  1174. */
  1175. function sqimap_mailbox_is_noselect($oImapStream,$sImapFolder,&$oBoxes) {
  1176. // build mailbox object if it is not available
  1177. if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
  1178. foreach($oBoxes as $box) {
  1179. if ($box['unformatted']==$sImapFolder) {
  1180. return (bool) check_is_noselect($box['raw']);
  1181. }
  1182. }
  1183. return false;
  1184. }
  1185. /**
  1186. * Checks if folder is noinferiors (can't store other folders)
  1187. *
  1188. * Function does not check if folder subscribed.
  1189. * @param stream $oImapStream imap connection resource
  1190. * @param string $sImapFolder imap folder name
  1191. * @param object $oBoxes mailboxes class object.
  1192. * @return boolean true, when folder has noinferiors flag. false in any other case.
  1193. * @since 1.5.1
  1194. */
  1195. function sqimap_mailbox_is_noinferiors($oImapStream,$sImapFolder,&$oBoxes) {
  1196. // build mailbox object if it is not available
  1197. if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
  1198. foreach($oBoxes as $box) {
  1199. if ($box['unformatted']==$sImapFolder) {
  1200. return (bool) check_is_noinferiors($box['raw']);
  1201. }
  1202. }
  1203. return false;
  1204. }
  1205. ?>