imap_mailbox.php 30 KB

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