imap_mailbox.php 32 KB

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