imap_mailbox.php 30 KB

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