imap_mailbox.php 34 KB

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