imap_mailbox.php 30 KB

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