imap_mailbox.php 30 KB

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