imap_mailbox.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. <?php
  2. /**
  3. * imap_mailbox.php
  4. *
  5. * Copyright (c) 1999-2003 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. * $Id$
  11. */
  12. require_once(SM_PATH . 'functions/imap_utf7_local.php');
  13. global $boxesnew;
  14. class mailboxes {
  15. var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
  16. $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
  17. $is_trash = false, $is_draft = false, $mbxs = array(),
  18. $unseen = false, $total = false;
  19. function addMbx($mbx, $delimiter, $start, $specialfirst) {
  20. $ary = explode($delimiter, $mbx->mailboxname_full);
  21. $mbx_parent = &$this;
  22. for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
  23. $mbx_childs = &$mbx_parent->mbxs;
  24. $found = false;
  25. if ($mbx_childs) {
  26. foreach ($mbx_childs as $key => $parent) {
  27. if ($parent->mailboxname_sub == $ary[$i]) {
  28. $mbx_parent = &$mbx_parent->mbxs[$key];
  29. $found = true;
  30. }
  31. }
  32. }
  33. if (!$found) {
  34. $no_select_mbx = new mailboxes();
  35. if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
  36. $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
  37. } else {
  38. $no_select_mbx->mailboxname_full = $ary[$i];
  39. }
  40. $no_select_mbx->mailboxname_sub = $ary[$i];
  41. $no_select_mbx->is_noselect = true;
  42. $mbx_parent->mbxs[] = $no_select_mbx;
  43. $i--;
  44. }
  45. }
  46. $mbx_parent->mbxs[] = $mbx;
  47. if ($mbx->is_special && $specialfirst) {
  48. usort($mbx_parent->mbxs, 'sortSpecialMbx');
  49. }
  50. }
  51. }
  52. function sortSpecialMbx($a, $b) {
  53. if ($a->is_inbox) {
  54. $acmp = '0'. $a->mailboxname_full;
  55. } else if ($a->is_special) {
  56. $acmp = '1'. $a->mailboxname_full;
  57. } else {
  58. $acmp = '2' . $a->mailboxname_full;
  59. }
  60. if ($b->is_inbox) {
  61. $bcmp = '0'. $b->mailboxname_full;
  62. }else if ($b->is_special) {
  63. $bcmp = '1' . $b->mailboxname_full;
  64. } else {
  65. $bcmp = '2' . $b->mailboxname_full;
  66. }
  67. if ($acmp == $bcmp) return 0;
  68. return ($acmp > $bcmp) ? 1: -1;
  69. }
  70. function find_mailbox_name ($mailbox) {
  71. if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
  72. return $regs[1];
  73. if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
  74. return $regs[1];
  75. ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
  76. return $regs[1];
  77. }
  78. function check_is_noselect ($lsub_line) {
  79. return preg_match("/^\* LSUB \([^\)]*\\Noselect[^\)]*\)/i", $lsub_line);
  80. }
  81. /**
  82. * If $haystack is a full mailbox name, and $needle is the mailbox
  83. * separator character, returns the second last part of the full
  84. * mailbox name (i.e. the mailbox's parent mailbox)
  85. */
  86. function readMailboxParent($haystack, $needle) {
  87. if ($needle == '') {
  88. $ret = '';
  89. } else {
  90. $parts = explode($needle, $haystack);
  91. $elem = array_pop($parts);
  92. while ($elem == '' && count($parts)) {
  93. $elem = array_pop($parts);
  94. }
  95. $ret = join($needle, $parts);
  96. }
  97. return( $ret );
  98. }
  99. /**
  100. * Check if $subbox is below the specified $parentbox
  101. */
  102. function isBoxBelow( $subbox, $parentbox ) {
  103. global $delimiter;
  104. /*
  105. * Eliminate the obvious mismatch, where the
  106. * subfolder path is shorter than that of the potential parent
  107. */
  108. if ( strlen($subbox) < strlen($parentbox) ) {
  109. return false;
  110. }
  111. /* check for delimiter */
  112. if (!substr($parentbox,-1) == $delimiter) {
  113. $parentbox.=$delimiter;
  114. }
  115. if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. /* Defines special mailboxes */
  122. function isSpecialMailbox( $box ) {
  123. global $trash_folder, $sent_folder, $draft_folder,
  124. $move_to_trash, $move_to_sent, $save_as_draft;
  125. $ret = ( (strtolower($box) == 'inbox') ||
  126. isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
  127. if ( !$ret ) {
  128. $ret = do_hook_function( 'special_mailbox', $box );
  129. }
  130. return $ret;
  131. }
  132. function isTrashMailbox ($box) {
  133. global $trash_folder, $move_to_trash;
  134. return $move_to_trash && $trash_folder &&
  135. ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
  136. }
  137. function isSentMailbox($box) {
  138. global $sent_folder, $move_to_sent;
  139. return $move_to_sent && $sent_folder &&
  140. ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
  141. }
  142. function isDraftMailbox($box) {
  143. global $draft_folder, $save_as_draft;
  144. return $save_as_draft &&
  145. ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
  146. }
  147. /* Expunges a mailbox */
  148. function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
  149. global $uid_support;
  150. if ($id) {
  151. if (is_array($id)) {
  152. $id = sqimap_message_list_squisher($id);
  153. }
  154. $id = ' '.$id;
  155. $uid = $uid_support;
  156. } else {
  157. $uid = false;
  158. }
  159. $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
  160. $response, $message, $uid);
  161. $cnt = 0;
  162. if (is_array($read)) {
  163. foreach ($read as $r) {
  164. if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
  165. $cnt++;
  166. }
  167. }
  168. }
  169. return $cnt;
  170. }
  171. /* Checks whether or not the specified mailbox exists */
  172. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  173. if (!isset($mailbox)) {
  174. return false;
  175. }
  176. $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
  177. true, $response, $message);
  178. return isset($mbx[0]);
  179. }
  180. /* Selects a mailbox */
  181. function sqimap_mailbox_select ($imap_stream, $mailbox) {
  182. global $auto_expunge;
  183. if ($mailbox == 'None') {
  184. return;
  185. }
  186. $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
  187. true, $response, $message);
  188. $result = array();
  189. for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
  190. if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
  191. $result[strtoupper($regs[1])] = $regs[2];
  192. } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
  193. $result[strtoupper($regs[2])] = $regs[1];
  194. } else {
  195. if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
  196. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
  197. $result['PERMANENTFLAGS'] = $regs[1];
  198. } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
  199. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
  200. $result['FLAGS'] = $regs[1];
  201. }
  202. }
  203. }
  204. if (preg_match('/^\[(.+)\]/',$message, $regs)) {
  205. $result['RIGHTS']=$regs[1];
  206. }
  207. if ($auto_expunge) {
  208. $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
  209. }
  210. return $result;
  211. }
  212. /* Creates a folder */
  213. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  214. global $delimiter;
  215. if (strtolower($type) == 'noselect') {
  216. $mailbox .= $delimiter;
  217. }
  218. $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
  219. true, $response, $message);
  220. sqimap_subscribe ($imap_stream, $mailbox);
  221. }
  222. /* Subscribes to an existing folder */
  223. function sqimap_subscribe ($imap_stream, $mailbox) {
  224. $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
  225. true, $response, $message);
  226. }
  227. /* Unsubscribes to an existing folder */
  228. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  229. $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
  230. true, $response, $message);
  231. }
  232. /* Deletes the given folder */
  233. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  234. global $data_dir, $username;
  235. $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
  236. true, $response, $message);
  237. sqimap_unsubscribe ($imap_stream, $mailbox);
  238. do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
  239. removePref($data_dir, $username, "thread_$mailbox");
  240. }
  241. /* Determines if the user is subscribed to the folder or not */
  242. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  243. $boxesall = sqimap_mailbox_list ($imap_stream);
  244. foreach ($boxesall as $ref) {
  245. if ($ref['unformatted'] == $folder) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. /* Renames a mailbox */
  252. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  253. if ( $old_name != $new_name ) {
  254. global $delimiter, $imap_server_type, $data_dir, $username;
  255. if ( substr( $old_name, -1 ) == $delimiter ) {
  256. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  257. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  258. $postfix = $delimiter;
  259. } else {
  260. $postfix = '';
  261. }
  262. $boxesall = sqimap_mailbox_list($imap_stream);
  263. $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
  264. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  265. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  266. $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
  267. removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
  268. sqimap_subscribe($imap_stream, $new_name.$postfix);
  269. setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
  270. do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
  271. $l = strlen( $old_name ) + 1;
  272. $p = 'unformatted';
  273. foreach ($boxesall as $box) {
  274. if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
  275. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  276. if ($imap_server_type == 'cyrus') {
  277. $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
  278. $data = sqimap_run_command($imap_stream, $cmd, true,
  279. $response, $message);
  280. }
  281. sqimap_unsubscribe($imap_stream, $box[$p]);
  282. $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
  283. removePref($data_dir, $username, 'thread_'.$box[$p]);
  284. sqimap_subscribe($imap_stream, $new_sub);
  285. setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
  286. do_hook_function('rename_or_delete_folder',
  287. $args = array($box[$p], 'rename', $new_sub));
  288. }
  289. }
  290. }
  291. }
  292. /*
  293. * Formats a mailbox into 4 parts for the $boxesall array
  294. *
  295. * The four parts are:
  296. *
  297. * raw - Raw LIST/LSUB response from the IMAP server
  298. * formatted - nicely formatted folder name
  299. * unformatted - unformatted, but with delimiter at end removed
  300. * unformatted-dm - folder name as it appears in raw response
  301. * unformatted-disp - unformatted without $folder_prefix
  302. */
  303. function sqimap_mailbox_parse ($line, $line_lsub) {
  304. global $folder_prefix, $delimiter;
  305. /* Process each folder line */
  306. for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
  307. /* Store the raw IMAP reply */
  308. if (isset($line[$g])) {
  309. $boxesall[$g]['raw'] = $line[$g];
  310. } else {
  311. $boxesall[$g]['raw'] = '';
  312. }
  313. /* Count number of delimiters ($delimiter) in folder name */
  314. $mailbox = trim($line_lsub[$g]);
  315. $dm_count = substr_count($mailbox, $delimiter);
  316. if (substr($mailbox, -1) == $delimiter) {
  317. /* If name ends in delimiter, decrement count by one */
  318. $dm_count--;
  319. }
  320. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  321. $boxesallbyname[$mailbox] = $g;
  322. $parentfolder = readMailboxParent($mailbox, $delimiter);
  323. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  324. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  325. (isset($boxesallbyname[$parentfolder]) &&
  326. (strlen($parentfolder) > 0) ) ) {
  327. $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
  328. if ($indent > 0) {
  329. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  330. } else {
  331. $boxesall[$g]['formatted'] = '';
  332. }
  333. $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  334. } else {
  335. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  336. }
  337. $boxesall[$g]['unformatted-dm'] = $mailbox;
  338. if (substr($mailbox, -1) == $delimiter) {
  339. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  340. }
  341. $boxesall[$g]['unformatted'] = $mailbox;
  342. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  343. $mailbox = substr($mailbox, strlen($folder_prefix));
  344. }
  345. $boxesall[$g]['unformatted-disp'] = $mailbox;
  346. $boxesall[$g]['id'] = $g;
  347. $boxesall[$g]['flags'] = array();
  348. if (isset($line[$g])) {
  349. ereg("\(([^)]*)\)",$line[$g],$regs);
  350. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  351. if ($flags) {
  352. $boxesall[$g]['flags'] = explode(' ', $flags);
  353. }
  354. }
  355. }
  356. return $boxesall;
  357. }
  358. /*
  359. * Sorting function used to sort mailbox names.
  360. * + Original patch from dave_michmerhuizen@yahoo.com
  361. * + Allows case insensitivity when sorting folders
  362. * + Takes care of the delimiter being sorted to the end, causing
  363. * subfolders to be listed in below folders that are prefixed
  364. * with their parent folders name.
  365. *
  366. * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
  367. * Without special sort function: foobar between foo and foo.bar
  368. * With special sort function: foobar AFTER foo and foo.bar :)
  369. */
  370. function user_strcasecmp($a, $b) {
  371. return strnatcasecmp($a, $b);
  372. }
  373. /*
  374. * Returns list of options (to be echoed into select statement
  375. * based on available mailboxes and separators
  376. * Caller should surround options with <SELECT..> </SELECT> and
  377. * any formatting.
  378. * $imap_stream - $imapConnection to query for mailboxes
  379. * $show_selected - array containing list of mailboxes to pre-select (0 if none)
  380. * $folder_skip - array of folders to keep out of option list (compared in lower)
  381. * $boxes - list of already fetched boxes (for places like folder panel, where
  382. * you know these options will be shown 3 times in a row.. (most often unset).
  383. */
  384. function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0 ) {
  385. global $username, $data_dir;
  386. $mbox_options = '';
  387. $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
  388. if ($boxes == 0) {
  389. $boxes = sqimap_mailbox_list($imap_stream);
  390. }
  391. foreach ($boxes as $boxes_part) {
  392. if (!in_array('noselect', $boxes_part['flags'])) {
  393. $box = $boxes_part['unformatted'];
  394. $lowerbox = strtolower($box);
  395. if ($folder_skip != 0 && in_array($lowerbox, $folder_skip) ) {
  396. continue;
  397. }
  398. if ($lowerbox == 'inbox'){
  399. $box2 = _("INBOX");
  400. } else if ( $shorten_box_names == 2 ) { /* delimited, style = 2 */
  401. $box2 = str_replace('&nbsp;&nbsp;', '.&nbsp;', $boxes_part['formatted']);
  402. } else if ( $shorten_box_names == 1 ) { /* indent, style = 1 */
  403. $box2 = $boxes_part['formatted'];
  404. } else { /* default, long names, style = 0 */
  405. $box2 = str_replace(' ', '&nbsp;', imap_utf7_decode_local($boxes_part['unformatted-disp']));
  406. }
  407. if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
  408. $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
  409. } else {
  410. $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
  411. }
  412. }
  413. }
  414. return $mbox_options;
  415. }
  416. /*
  417. * Returns sorted mailbox lists in several different ways.
  418. * See comment on sqimap_mailbox_parse() for info about the returned array.
  419. */
  420. function sqimap_mailbox_list($imap_stream) {
  421. global $default_folder_prefix;
  422. if (!isset($boxesnew)) {
  423. global $data_dir, $username, $list_special_folders_first,
  424. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  425. $move_to_trash, $move_to_sent, $save_as_draft,
  426. $delimiter, $noselect_fix_enable;
  427. $inbox_in_list = false;
  428. $inbox_subscribed = false;
  429. require_once(SM_PATH . 'include/load_prefs.php');
  430. if ($noselect_fix_enable) {
  431. $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
  432. } else {
  433. $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
  434. }
  435. /* LSUB array */
  436. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
  437. true, $response, $message);
  438. $sorted_lsub_ary = array();
  439. for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
  440. /*
  441. * Workaround for mailboxes returned as literal
  442. * Doesn't work if the mailbox name is multiple lines
  443. * (larger then fgets buffer)
  444. */
  445. if (isset($lsub_ary[$i + 1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
  446. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  447. $lsub_ary[$i], $regs)) {
  448. $i++;
  449. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  450. }
  451. }
  452. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  453. $sorted_lsub_ary[] = $temp_mailbox_name;
  454. if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
  455. $inbox_subscribed = true;
  456. }
  457. }
  458. /* remove duplicates */
  459. $sorted_lsub_ary = array_unique($sorted_lsub_ary);
  460. /* natural sort mailboxes */
  461. if (isset($sorted_lsub_ary)) {
  462. usort($sorted_lsub_ary, 'user_strcasecmp');
  463. }
  464. /*
  465. * The LSUB response doesn't provide us information about \Noselect
  466. * mail boxes. The LIST response does, that's why we need to do a LIST
  467. * call to retrieve the flags for the mailbox
  468. * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
  469. * in other words, we cannot rely on it.
  470. */
  471. $sorted_list_ary = array();
  472. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  473. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  474. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  475. }
  476. else {
  477. $mbx = $sorted_lsub_ary[$i];
  478. }
  479. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  480. true, $response, $message);
  481. /* Another workaround for literals */
  482. if (isset($read[1]) && substr($read[1],-3) == "}\r\n") {
  483. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  484. $read[0], $regs)) {
  485. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
  486. }
  487. }
  488. if (isset($read[0])) {
  489. $sorted_list_ary[$i] = $read[0];
  490. } else {
  491. $sorted_list_ary[$i] = '';
  492. }
  493. }
  494. /*
  495. * Just in case they're not subscribed to their inbox,
  496. * we'll get it for them anyway
  497. */
  498. if (!$inbox_subscribed) {
  499. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  500. true, $response, $message);
  501. /* Another workaround for literals */
  502. if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
  503. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  504. $inbox_ary[0], $regs)) {
  505. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  506. '"' . $regs[2];
  507. }
  508. }
  509. $sorted_list_ary[] = $inbox_ary[0];
  510. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  511. }
  512. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  513. /* Now, lets sort for special folders */
  514. $boxesnew = $used = array();
  515. /* Find INBOX */
  516. $cnt = count($boxesall);
  517. $used = array_pad($used,$cnt,false);
  518. for($k = 0; $k < $cnt; ++$k) {
  519. if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
  520. $boxesnew[] = $boxesall[$k];
  521. $used[$k] = true;
  522. break;
  523. }
  524. }
  525. /* List special folders and their subfolders, if requested. */
  526. if ($list_special_folders_first) {
  527. for($k = 0; $k < $cnt; ++$k) {
  528. if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
  529. $boxesnew[] = $boxesall[$k];
  530. $used[$k] = true;
  531. }
  532. }
  533. }
  534. /* Rest of the folders */
  535. for($k = 0; $k < $cnt; $k++) {
  536. if (!$used[$k]) {
  537. $boxesnew[] = $boxesall[$k];
  538. }
  539. }
  540. }
  541. return $boxesnew;
  542. }
  543. /*
  544. * Returns a list of all folders, subscribed or not
  545. */
  546. function sqimap_mailbox_list_all($imap_stream) {
  547. global $list_special_folders_first, $folder_prefix, $delimiter;
  548. $ssid = sqimap_session_id();
  549. $lsid = strlen( $ssid );
  550. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  551. $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
  552. $g = 0;
  553. $phase = 'inbox';
  554. $fld_pre_length = strlen($folder_prefix);
  555. for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
  556. /* Another workaround for EIMS */
  557. if (isset($read_ary[$i + 1]) &&
  558. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  559. $read_ary[$i], $regs)) {
  560. $i ++;
  561. $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
  562. }
  563. if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
  564. /* Store the raw IMAP reply */
  565. $boxes[$g]['raw'] = $read_ary[$i];
  566. /* Count number of delimiters ($delimiter) in folder name */
  567. $mailbox = find_mailbox_name($read_ary[$i]);
  568. $dm_count = substr_count($mailbox, $delimiter);
  569. if (substr($mailbox, -1) == $delimiter) {
  570. /* If name ends in delimiter - decrement count by one */
  571. $dm_count--;
  572. }
  573. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  574. $boxesallbyname[$mailbox] = $g;
  575. $parentfolder = readMailboxParent($mailbox, $delimiter);
  576. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  577. (ereg('^'.$folder_prefix, $mailbox)) ||
  578. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  579. if ($dm_count) {
  580. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  581. } else {
  582. $boxes[$g]['formatted'] = '';
  583. }
  584. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  585. } else {
  586. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  587. }
  588. $boxes[$g]['unformatted-dm'] = $mailbox;
  589. if (substr($mailbox, -1) == $delimiter) {
  590. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  591. }
  592. $boxes[$g]['unformatted'] = $mailbox;
  593. $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
  594. $boxes[$g]['id'] = $g;
  595. /* Now lets get the flags for this mailbox */
  596. $read_mlbx = $read_ary[$i];
  597. // $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
  598. // true, $response, $message);
  599. /* Another workaround for EIMS */
  600. // if (isset($read_mlbx[1]) &&
  601. // ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
  602. // $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
  603. // }
  604. // echo $read_mlbx[0] .' raw 2 <br>';
  605. $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
  606. $flags = substr($flags, 0, strpos($flags, ')'));
  607. $flags = str_replace('\\', '', $flags);
  608. $flags = trim(strtolower($flags));
  609. if ($flags) {
  610. $boxes[$g]['flags'] = explode(' ', $flags);
  611. } else {
  612. $boxes[$g]['flags'] = array();
  613. }
  614. }
  615. $g++;
  616. }
  617. if(is_array($boxes)) {
  618. sort ($boxes);
  619. }
  620. return $boxes;
  621. }
  622. function sqimap_mailbox_tree($imap_stream) {
  623. global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
  624. if (!isset($boxesnew)) {
  625. global $data_dir, $username, $list_special_folders_first,
  626. $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
  627. $inbox_in_list = false;
  628. $inbox_subscribed = false;
  629. require_once(SM_PATH . 'include/load_prefs.php');
  630. /* LSUB array */
  631. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  632. true, $response, $message);
  633. /*
  634. * Section about removing the last element was removed
  635. * We don't return "* OK" anymore from sqimap_read_data
  636. */
  637. $sorted_lsub_ary = array();
  638. $cnt = count($lsub_ary);
  639. for ($i = 0; $i < $cnt; $i++) {
  640. /*
  641. * Workaround for EIMS
  642. * Doesn't work if the mailbox name is multiple lines
  643. */
  644. if (isset($lsub_ary[$i + 1]) &&
  645. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  646. $lsub_ary[$i], $regs)) {
  647. $i++;
  648. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  649. }
  650. /*
  651. if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
  652. $flag = $regs[1];
  653. $mbx = trim($regs[3]);
  654. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  655. }
  656. */
  657. $mbx = find_mailbox_name($lsub_ary[$i]);
  658. $noselect = check_is_noselect($lsub_ary[$i]);
  659. if (substr($mbx, -1) == $delimiter) {
  660. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  661. }
  662. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
  663. }
  664. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
  665. for($i = 0; $i < $cnt; $i++) {
  666. if ($sorted_lsub_ary[$i]['mbx'] == 'INBOX') {
  667. $inbox_in_list = true;
  668. break;
  669. }
  670. }
  671. /*
  672. * Just in case they're not subscribed to their inbox,
  673. * we'll get it for them anyway
  674. */
  675. if (!$inbox_in_list) {
  676. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  677. true, $response, $message);
  678. /* Another workaround for EIMS */
  679. if (isset($inbox_ary[1]) &&
  680. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  681. $inbox_ary[0], $regs)) {
  682. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  683. '"' . $regs[2];
  684. }
  685. $mbx = find_mailbox_name($inbox_ary[0]);
  686. if (substr($mbx, -1) == $delimiter) {
  687. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  688. }
  689. if ($mbx == 'INBOX') {
  690. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
  691. sqimap_subscribe($imap_stream, 'INBOX');
  692. $cnt++;
  693. }
  694. /*
  695. if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
  696. $flag = $regs[1];
  697. $mbx = trim($regs[3]);
  698. if (substr($mbx, -1) == $delimiter) {
  699. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  700. }
  701. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  702. }
  703. */
  704. }
  705. for ($i = 0 ; $i < $cnt; $i++) {
  706. $mbx = $sorted_lsub_ary[$i]['mbx'];
  707. if (($unseen_notify == 2 && $mbx == 'INBOX') ||
  708. ($unseen_notify == 3) ||
  709. ($move_to_trash && ($mbx == $trash_folder))) {
  710. if($sorted_lsub_ary[$i]['noselect']) {
  711. $sorted_lsub_ary[$i]['unseen'] = 0;
  712. } else {
  713. $sorted_lsub_ary[$i]['unseen'] =
  714. sqimap_unseen_messages($imap_stream, $mbx);
  715. }
  716. if (($unseen_type == 2) ||
  717. ($move_to_trash && ($mbx == $trash_folder)) ||
  718. ($mbx == $trash_folder)) {
  719. if($sorted_lsub_ary[$i]['noselect']) {
  720. $sorted_lsub_ary[$i]['nummessages'] = 0;
  721. } else {
  722. $sorted_lsub_ary[$i]['nummessages'] =
  723. sqimap_get_num_messages($imap_stream, $mbx);
  724. }
  725. }
  726. }
  727. }
  728. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
  729. return $boxesnew;
  730. }
  731. }
  732. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
  733. global $data_dir, $username, $list_special_folders_first,
  734. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  735. $move_to_trash, $move_to_sent, $save_as_draft,
  736. $delimiter;
  737. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  738. /* create virtual root node */
  739. $mailboxes= new mailboxes();
  740. $mailboxes->is_root = true;
  741. $trail_del = false;
  742. if (isset($folder_prefix) && $folder_prefix != '') {
  743. $start = substr_count($folder_prefix,$delimiter);
  744. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  745. $trail_del = true;
  746. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  747. } else {
  748. $mailboxes->mailboxname_full = $folder_prefix;
  749. $start++;
  750. }
  751. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  752. } else {
  753. $start = 0;
  754. }
  755. $cnt = count($mbx_ary);
  756. for ($i=0; $i < $cnt; $i++) {
  757. if ($mbx_ary[$i]['mbx'] !='' ) {
  758. $mbx = new mailboxes();
  759. $mailbox = $mbx_ary[$i]['mbx'];
  760. switch ($mailbox) {
  761. case 'INBOX':
  762. $mbx->is_inbox = true;
  763. $mbx->is_special = true;
  764. break;
  765. case $trash_folder:
  766. $mbx->is_trash = true;
  767. $mbx->is_special = true;
  768. break;
  769. case $sent_folder:
  770. $mbx->is_sent = true;
  771. $mbx->is_special = true;
  772. break;
  773. case $draft_folder:
  774. $mbx->is_draft = true;
  775. $mbx->is_special = true;
  776. break;
  777. }
  778. if (isset($mbx_ary[$i]['unseen'])) {
  779. $mbx->unseen = $mbx_ary[$i]['unseen'];
  780. }
  781. if (isset($mbx_ary[$i]['nummessages'])) {
  782. $mbx->total = $mbx_ary[$i]['nummessages'];
  783. }
  784. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  785. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  786. if ($r_del_pos) {
  787. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  788. } else { /* mailbox is root folder */
  789. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  790. }
  791. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  792. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  793. }
  794. }
  795. return $mailboxes;
  796. }
  797. ?>