imap_mailbox.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. /*
  15. FIXME. This class should be extracted and placed in a separate file that
  16. can be included before we start the session. That makes caching of the tree
  17. possible. On a refresh mailboxes from left_main.php the only function that
  18. should be called is the sqimap_get_status_mbx_tree. In case of subscribe
  19. / rename / delete / new we have to create methods for adding/changing the
  20. mailbox in the mbx_tree without the need for a refresh.
  21. */
  22. class mailboxes {
  23. var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
  24. $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
  25. $is_trash = false, $is_draft = false, $mbxs = array(),
  26. $unseen = false, $total = false;
  27. function addMbx($mbx, $delimiter, $start, $specialfirst) {
  28. $ary = explode($delimiter, $mbx->mailboxname_full);
  29. $mbx_parent =& $this;
  30. for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
  31. $mbx_childs =& $mbx_parent->mbxs;
  32. $found = false;
  33. if ($mbx_childs) {
  34. foreach ($mbx_childs as $key => $parent) {
  35. if ($parent->mailboxname_sub == $ary[$i]) {
  36. $mbx_parent =& $mbx_parent->mbxs[$key];
  37. $found = true;
  38. break;
  39. }
  40. }
  41. }
  42. if (!$found) {
  43. $no_select_mbx = new mailboxes();
  44. if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
  45. $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
  46. } else {
  47. $no_select_mbx->mailboxname_full = $ary[$i];
  48. }
  49. $no_select_mbx->mailboxname_sub = $ary[$i];
  50. $no_select_mbx->is_noselect = true;
  51. $mbx_parent->mbxs[] = $no_select_mbx;
  52. $i--;
  53. }
  54. }
  55. $mbx_parent->mbxs[] = $mbx;
  56. if ($mbx->is_special && $specialfirst) {
  57. usort($mbx_parent->mbxs, 'sortSpecialMbx');
  58. }
  59. }
  60. }
  61. function sortSpecialMbx($a, $b) {
  62. if ($a->is_inbox) {
  63. $acmp = '0'. $a->mailboxname_full;
  64. } else if ($a->is_special) {
  65. $acmp = '1'. $a->mailboxname_full;
  66. } else {
  67. $acmp = '2' . $a->mailboxname_full;
  68. }
  69. if ($b->is_inbox) {
  70. $bcmp = '0'. $b->mailboxname_full;
  71. }else if ($b->is_special) {
  72. $bcmp = '1' . $b->mailboxname_full;
  73. } else {
  74. $bcmp = '2' . $b->mailboxname_full;
  75. }
  76. if ($acmp == $bcmp) return 0;
  77. return ($acmp > $bcmp) ? 1: -1;
  78. }
  79. function find_mailbox_name ($mailbox) {
  80. if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
  81. return $regs[1];
  82. if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
  83. return $regs[1];
  84. ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
  85. return $regs[1];
  86. }
  87. function check_is_noselect ($lsub_line) {
  88. return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
  89. }
  90. /**
  91. * If $haystack is a full mailbox name, and $needle is the mailbox
  92. * separator character, returns the second last part of the full
  93. * mailbox name (i.e. the mailbox's parent mailbox)
  94. */
  95. function readMailboxParent($haystack, $needle) {
  96. if ($needle == '') {
  97. $ret = '';
  98. } else {
  99. $parts = explode($needle, $haystack);
  100. $elem = array_pop($parts);
  101. while ($elem == '' && count($parts)) {
  102. $elem = array_pop($parts);
  103. }
  104. $ret = join($needle, $parts);
  105. }
  106. return( $ret );
  107. }
  108. /**
  109. * Check if $subbox is below the specified $parentbox
  110. */
  111. function isBoxBelow( $subbox, $parentbox ) {
  112. global $delimiter;
  113. /*
  114. * Eliminate the obvious mismatch, where the
  115. * subfolder path is shorter than that of the potential parent
  116. */
  117. if ( strlen($subbox) < strlen($parentbox) ) {
  118. return false;
  119. }
  120. /* check for delimiter */
  121. if (!substr($parentbox,-1) == $delimiter) {
  122. $parentbox.=$delimiter;
  123. }
  124. if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. /* Defines special mailboxes */
  131. function isSpecialMailbox( $box ) {
  132. global $trash_folder, $sent_folder, $draft_folder,
  133. $move_to_trash, $move_to_sent, $save_as_draft;
  134. $ret = ( (strtolower($box) == 'inbox') ||
  135. isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
  136. if ( !$ret ) {
  137. $ret = do_hook_function( 'special_mailbox', $box );
  138. }
  139. return $ret;
  140. }
  141. function isTrashMailbox ($box) {
  142. global $trash_folder, $move_to_trash;
  143. return $move_to_trash && $trash_folder &&
  144. ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
  145. }
  146. function isSentMailbox($box) {
  147. global $sent_folder, $move_to_sent;
  148. return $move_to_sent && $sent_folder &&
  149. ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
  150. }
  151. function isDraftMailbox($box) {
  152. global $draft_folder, $save_as_draft;
  153. return $save_as_draft &&
  154. ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
  155. }
  156. /* Expunges a mailbox */
  157. function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
  158. global $uid_support;
  159. if ($id) {
  160. if (is_array($id)) {
  161. $id = sqimap_message_list_squisher($id);
  162. }
  163. $id = ' '.$id;
  164. $uid = $uid_support;
  165. } else {
  166. $uid = false;
  167. }
  168. $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
  169. $response, $message, $uid);
  170. $cnt = 0;
  171. if (is_array($read)) {
  172. foreach ($read as $r) {
  173. if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
  174. $cnt++;
  175. }
  176. }
  177. }
  178. return $cnt;
  179. }
  180. /* Checks whether or not the specified mailbox exists */
  181. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  182. if (!isset($mailbox) || empty($mailbox)) {
  183. return false;
  184. }
  185. $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
  186. true, $response, $message);
  187. return isset($mbx[0]);
  188. }
  189. /* Selects a mailbox */
  190. function sqimap_mailbox_select ($imap_stream, $mailbox) {
  191. global $auto_expunge;
  192. if ($mailbox == 'None') {
  193. return;
  194. }
  195. $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
  196. true, $response, $message);
  197. $result = array();
  198. for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
  199. if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
  200. $result[strtoupper($regs[1])] = $regs[2];
  201. } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
  202. $result[strtoupper($regs[2])] = $regs[1];
  203. } else {
  204. if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
  205. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
  206. $result['PERMANENTFLAGS'] = $regs[1];
  207. } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
  208. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
  209. $result['FLAGS'] = $regs[1];
  210. }
  211. }
  212. }
  213. if (preg_match('/^\[(.+)\]/',$message, $regs)) {
  214. $result['RIGHTS']=$regs[1];
  215. }
  216. if ($auto_expunge) {
  217. $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
  218. }
  219. return $result;
  220. }
  221. /* Creates a folder */
  222. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  223. global $delimiter;
  224. if (strtolower($type) == 'noselect') {
  225. $mailbox .= $delimiter;
  226. }
  227. $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
  228. true, $response, $message);
  229. sqimap_subscribe ($imap_stream, $mailbox);
  230. }
  231. /* Subscribes to an existing folder */
  232. function sqimap_subscribe ($imap_stream, $mailbox) {
  233. $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
  234. true, $response, $message);
  235. }
  236. /* Unsubscribes to an existing folder */
  237. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  238. $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
  239. true, $response, $message);
  240. }
  241. /* Deletes the given folder */
  242. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  243. global $data_dir, $username;
  244. $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
  245. true, $response, $message);
  246. sqimap_unsubscribe ($imap_stream, $mailbox);
  247. do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
  248. removePref($data_dir, $username, "thread_$mailbox");
  249. }
  250. /* Determines if the user is subscribed to the folder or not */
  251. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  252. $boxesall = sqimap_mailbox_list ($imap_stream);
  253. foreach ($boxesall as $ref) {
  254. if ($ref['unformatted'] == $folder) {
  255. return true;
  256. }
  257. }
  258. return false;
  259. }
  260. /* Renames a mailbox */
  261. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  262. if ( $old_name != $new_name ) {
  263. global $delimiter, $imap_server_type, $data_dir, $username;
  264. if ( substr( $old_name, -1 ) == $delimiter ) {
  265. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  266. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  267. $postfix = $delimiter;
  268. } else {
  269. $postfix = '';
  270. }
  271. $boxesall = sqimap_mailbox_list($imap_stream);
  272. $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
  273. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  274. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  275. $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
  276. removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
  277. sqimap_subscribe($imap_stream, $new_name.$postfix);
  278. setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
  279. do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
  280. $l = strlen( $old_name ) + 1;
  281. $p = 'unformatted';
  282. foreach ($boxesall as $box) {
  283. if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
  284. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  285. if ($imap_server_type == 'cyrus') {
  286. $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
  287. $data = sqimap_run_command($imap_stream, $cmd, true,
  288. $response, $message);
  289. }
  290. sqimap_unsubscribe($imap_stream, $box[$p]);
  291. $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
  292. removePref($data_dir, $username, 'thread_'.$box[$p]);
  293. sqimap_subscribe($imap_stream, $new_sub);
  294. setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
  295. do_hook_function('rename_or_delete_folder',
  296. $args = array($box[$p], 'rename', $new_sub));
  297. }
  298. }
  299. }
  300. }
  301. /*
  302. * Formats a mailbox into 4 parts for the $boxesall array
  303. *
  304. * The four parts are:
  305. *
  306. * raw - Raw LIST/LSUB response from the IMAP server
  307. * formatted - nicely formatted folder name
  308. * unformatted - unformatted, but with delimiter at end removed
  309. * unformatted-dm - folder name as it appears in raw response
  310. * unformatted-disp - unformatted without $folder_prefix
  311. */
  312. function sqimap_mailbox_parse ($line, $line_lsub) {
  313. global $folder_prefix, $delimiter;
  314. /* Process each folder line */
  315. for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
  316. /* Store the raw IMAP reply */
  317. if (isset($line[$g])) {
  318. $boxesall[$g]['raw'] = $line[$g];
  319. } else {
  320. $boxesall[$g]['raw'] = '';
  321. }
  322. /* Count number of delimiters ($delimiter) in folder name */
  323. $mailbox = trim($line_lsub[$g]);
  324. $dm_count = substr_count($mailbox, $delimiter);
  325. if (substr($mailbox, -1) == $delimiter) {
  326. /* If name ends in delimiter, decrement count by one */
  327. $dm_count--;
  328. }
  329. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  330. $boxesallbyname[$mailbox] = $g;
  331. $parentfolder = readMailboxParent($mailbox, $delimiter);
  332. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  333. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  334. (isset($boxesallbyname[$parentfolder]) &&
  335. (strlen($parentfolder) > 0) ) ) {
  336. $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
  337. if ($indent > 0) {
  338. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  339. } else {
  340. $boxesall[$g]['formatted'] = '';
  341. }
  342. $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  343. } else {
  344. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  345. }
  346. $boxesall[$g]['unformatted-dm'] = $mailbox;
  347. if (substr($mailbox, -1) == $delimiter) {
  348. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  349. }
  350. $boxesall[$g]['unformatted'] = $mailbox;
  351. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  352. $mailbox = substr($mailbox, strlen($folder_prefix));
  353. }
  354. $boxesall[$g]['unformatted-disp'] = $mailbox;
  355. $boxesall[$g]['id'] = $g;
  356. $boxesall[$g]['flags'] = array();
  357. if (isset($line[$g])) {
  358. ereg("\(([^)]*)\)",$line[$g],$regs);
  359. // FIXME Flags do contain the \ character. \NoSelect \NoInferiors
  360. // and $MDNSent <= last one doesn't have the \
  361. // It's better to follow RFC3501 instead of using our own naming.
  362. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  363. if ($flags) {
  364. $boxesall[$g]['flags'] = explode(' ', $flags);
  365. }
  366. }
  367. }
  368. return $boxesall;
  369. }
  370. /*
  371. * Sorting function used to sort mailbox names.
  372. * + Original patch from dave_michmerhuizen@yahoo.com
  373. * + Allows case insensitivity when sorting folders
  374. * + Takes care of the delimiter being sorted to the end, causing
  375. * subfolders to be listed in below folders that are prefixed
  376. * with their parent folders name.
  377. *
  378. * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
  379. * Without special sort function: foobar between foo and foo.bar
  380. * With special sort function: foobar AFTER foo and foo.bar :)
  381. */
  382. function user_strcasecmp($a, $b) {
  383. return strnatcasecmp($a, $b);
  384. }
  385. /*
  386. * Returns list of options (to be echoed into select statement
  387. * based on available mailboxes and separators
  388. * Caller should surround options with <SELECT..> </SELECT> and
  389. * any formatting.
  390. * $imap_stream - $imapConnection to query for mailboxes
  391. * $show_selected - array containing list of mailboxes to pre-select (0 if none)
  392. * $folder_skip - array of folders to keep out of option list (compared in lower)
  393. * $boxes - list of already fetched boxes (for places like folder panel, where
  394. * you know these options will be shown 3 times in a row.. (most often unset).
  395. * $flag - flag to check for in mailbox flags, used to filter out mailboxes.
  396. * 'noselect' by default to remove unselectable mailboxes.
  397. * 'noinferiors' used to filter out folders that can not contain subfolders.
  398. * NULL to avoid flag check entirely.
  399. * NOTE: noselect and noiferiors are used internally. The IMAP representation is
  400. * \NoSelect and \NoInferiors
  401. * $use_long_format - override folder display preference and always show full folder name.
  402. */
  403. function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
  404. $flag = 'noselect', $use_long_format = false ) {
  405. global $username, $data_dir;
  406. $mbox_options = '';
  407. if ( $use_long_format ) {
  408. $shorten_box_names = 0;
  409. } else {
  410. $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
  411. }
  412. if ($boxes == 0) {
  413. $boxes = sqimap_mailbox_list($imap_stream);
  414. }
  415. foreach ($boxes as $boxes_part) {
  416. if ($flag == NULL || !in_array($flag, $boxes_part['flags'])) {
  417. $box = $boxes_part['unformatted'];
  418. if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
  419. continue;
  420. }
  421. $lowerbox = strtolower($box);
  422. // mailboxes are casesensitive => inbox.sent != inbox.Sent
  423. // nevermind, to many dependencies this should be fixed!
  424. if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
  425. $box2 = _("INBOX");
  426. } else {
  427. switch ($shorten_box_names)
  428. {
  429. case 2: /* delimited, style = 2 */
  430. $box2 = str_replace('&nbsp;&nbsp;', '.&nbsp;', $boxes_part['formatted']);
  431. break;
  432. case 1: /* indent, style = 1 */
  433. $box2 = $boxes_part['formatted'];
  434. break;
  435. default: /* default, long names, style = 0 */
  436. $box2 = str_replace(' ', '&nbsp;', imap_utf7_decode_local($boxes_part['unformatted-disp']));
  437. break;
  438. }
  439. }
  440. if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
  441. $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
  442. } else {
  443. $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
  444. }
  445. }
  446. }
  447. return $mbox_options;
  448. }
  449. /*
  450. * Returns sorted mailbox lists in several different ways.
  451. * See comment on sqimap_mailbox_parse() for info about the returned array.
  452. */
  453. function sqimap_mailbox_list($imap_stream) {
  454. global $default_folder_prefix;
  455. if (!isset($boxesnew)) {
  456. global $data_dir, $username, $list_special_folders_first,
  457. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  458. $move_to_trash, $move_to_sent, $save_as_draft,
  459. $delimiter, $noselect_fix_enable;
  460. $inbox_in_list = false;
  461. $inbox_subscribed = false;
  462. require_once(SM_PATH . 'include/load_prefs.php');
  463. if ($noselect_fix_enable) {
  464. $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
  465. } else {
  466. $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
  467. }
  468. /* LSUB array */
  469. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
  470. true, $response, $message);
  471. $sorted_lsub_ary = array();
  472. for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
  473. /*
  474. * Workaround for mailboxes returned as literal
  475. * Doesn't work if the mailbox name is multiple lines
  476. * (larger then fgets buffer)
  477. */
  478. if (isset($lsub_ary[$i + 1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
  479. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  480. $lsub_ary[$i], $regs)) {
  481. $i++;
  482. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  483. }
  484. }
  485. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  486. $sorted_lsub_ary[] = $temp_mailbox_name;
  487. if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
  488. $inbox_subscribed = true;
  489. }
  490. }
  491. /* remove duplicates and ensure array is contiguous, so we don't rely on sort()' side-effect that fails if count()==1 */
  492. $sorted_lsub_ary = array_values(array_unique($sorted_lsub_ary));
  493. /* natural sort mailboxes */
  494. if (isset($sorted_lsub_ary)) {
  495. usort($sorted_lsub_ary, 'user_strcasecmp');
  496. }
  497. /*
  498. * The LSUB response doesn't provide us information about \Noselect
  499. * mail boxes. The LIST response does, that's why we need to do a LIST
  500. * call to retrieve the flags for the mailbox
  501. * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
  502. * in other words, we cannot rely on it.
  503. */
  504. $sorted_list_ary = array();
  505. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  506. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  507. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  508. }
  509. else {
  510. $mbx = $sorted_lsub_ary[$i];
  511. }
  512. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  513. true, $response, $message);
  514. /* Another workaround for literals */
  515. if (isset($read[1]) && substr($read[1],-3) == "}\r\n") {
  516. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  517. $read[0], $regs)) {
  518. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
  519. }
  520. }
  521. if (isset($read[0])) {
  522. $sorted_list_ary[$i] = $read[0];
  523. } else {
  524. $sorted_list_ary[$i] = '';
  525. }
  526. }
  527. /*
  528. * Just in case they're not subscribed to their inbox,
  529. * we'll get it for them anyway
  530. */
  531. if (!$inbox_subscribed) {
  532. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  533. true, $response, $message);
  534. /* Another workaround for literals */
  535. if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
  536. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  537. $inbox_ary[0], $regs)) {
  538. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  539. '"' . $regs[2];
  540. }
  541. }
  542. $sorted_list_ary[] = $inbox_ary[0];
  543. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  544. }
  545. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  546. /* Now, lets sort for special folders */
  547. $boxesnew = $used = array();
  548. /* Find INBOX */
  549. $cnt = count($boxesall);
  550. $used = array_pad($used,$cnt,false);
  551. for($k = 0; $k < $cnt; ++$k) {
  552. if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
  553. $boxesnew[] = $boxesall[$k];
  554. $used[$k] = true;
  555. break;
  556. }
  557. }
  558. /* List special folders and their subfolders, if requested. */
  559. if ($list_special_folders_first) {
  560. for($k = 0; $k < $cnt; ++$k) {
  561. if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
  562. $boxesnew[] = $boxesall[$k];
  563. $used[$k] = true;
  564. }
  565. }
  566. }
  567. /* Rest of the folders */
  568. for($k = 0; $k < $cnt; $k++) {
  569. if (!$used[$k]) {
  570. $boxesnew[] = $boxesall[$k];
  571. }
  572. }
  573. }
  574. return $boxesnew;
  575. }
  576. /*
  577. * Returns a list of all folders, subscribed or not
  578. */
  579. function sqimap_mailbox_list_all($imap_stream) {
  580. global $list_special_folders_first, $folder_prefix, $delimiter;
  581. $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
  582. $g = 0;
  583. $phase = 'inbox';
  584. $fld_pre_length = strlen($folder_prefix);
  585. for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
  586. /* Another workaround for EIMS */
  587. if (isset($read_ary[$i + 1]) &&
  588. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  589. $read_ary[$i], $regs)) {
  590. $i ++;
  591. $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
  592. }
  593. /* Store the raw IMAP reply */
  594. $boxes[$g]['raw'] = $read_ary[$i];
  595. /* Count number of delimiters ($delimiter) in folder name */
  596. $mailbox = find_mailbox_name($read_ary[$i]);
  597. $dm_count = substr_count($mailbox, $delimiter);
  598. if (substr($mailbox, -1) == $delimiter) {
  599. /* If name ends in delimiter - decrement count by one */
  600. $dm_count--;
  601. }
  602. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  603. $boxesallbyname[$mailbox] = $g;
  604. $parentfolder = readMailboxParent($mailbox, $delimiter);
  605. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  606. (ereg('^'.$folder_prefix, $mailbox)) ||
  607. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  608. if ($dm_count) {
  609. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  610. } else {
  611. $boxes[$g]['formatted'] = '';
  612. }
  613. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  614. } else {
  615. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  616. }
  617. $boxes[$g]['unformatted-dm'] = $mailbox;
  618. if (substr($mailbox, -1) == $delimiter) {
  619. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  620. }
  621. $boxes[$g]['unformatted'] = $mailbox;
  622. $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
  623. $boxes[$g]['id'] = $g;
  624. /* Now lets get the flags for this mailbox */
  625. $read_mlbx = $read_ary[$i];
  626. $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
  627. $flags = substr($flags, 0, strpos($flags, ')'));
  628. $flags = str_replace('\\', '', $flags);
  629. $flags = trim(strtolower($flags));
  630. if ($flags) {
  631. $boxes[$g]['flags'] = explode(' ', $flags);
  632. } else {
  633. $boxes[$g]['flags'] = array();
  634. }
  635. $g++;
  636. }
  637. if(is_array($boxes)) {
  638. sort ($boxes);
  639. }
  640. return $boxes;
  641. }
  642. function sqimap_mailbox_tree($imap_stream) {
  643. global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
  644. if (!isset($boxesnew)) {
  645. global $data_dir, $username, $list_special_folders_first,
  646. $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
  647. $imap_server_type;
  648. $inbox_in_list = false;
  649. $inbox_subscribed = false;
  650. $noselect = false;
  651. require_once(SM_PATH . 'include/load_prefs.php');
  652. /* LSUB array */
  653. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  654. true, $response, $message);
  655. /* Check to see if we have an INBOX */
  656. $has_inbox = false;
  657. for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
  658. if (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ary[$i])) {
  659. $has_inbox = true;
  660. break;
  661. }
  662. }
  663. if ($has_inbox == false) {
  664. $lsub_ary[] = '* LSUB () INBOX';
  665. }
  666. /*
  667. * Section about removing the last element was removed
  668. * We don't return "* OK" anymore from sqimap_read_data
  669. */
  670. $sorted_lsub_ary = array();
  671. $cnt = count($lsub_ary);
  672. for ($i = 0; $i < $cnt; $i++) {
  673. /*
  674. * Workaround for EIMS
  675. * Doesn't work if the mailbox name is multiple lines
  676. */
  677. if (isset($lsub_ary[$i + 1]) &&
  678. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  679. $lsub_ary[$i], $regs)) {
  680. $i++;
  681. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  682. }
  683. $mbx = find_mailbox_name($lsub_ary[$i]);
  684. // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
  685. if ($imap_server_type != "uw") {
  686. $noselect = check_is_noselect($lsub_ary[$i]);
  687. }
  688. if (substr($mbx, -1) == $delimiter) {
  689. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  690. }
  691. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
  692. }
  693. // FIX ME this requires a config setting inside conf.pl instead of checking on server type
  694. if ($imap_server_type == "uw") {
  695. $aQuery = array();
  696. $aTag = array();
  697. // prepare an array with queries
  698. foreach ($sorted_lsub_ary as $aMbx) {
  699. $mbx = $aMbx['mbx'];
  700. $query = "LIST \"\" \"$mbx\"";
  701. sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
  702. $aTag[$tag] = $mbx;
  703. }
  704. $sorted_lsub_ary = array();
  705. // execute all the queries at once
  706. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  707. foreach($aTag as $tag => $mbx) {
  708. if ($aServerResponse[$tag] == 'OK') {
  709. $sResponse = implode('', $aResponse[$tag]);
  710. $noselect = check_is_noselect($sResponse);
  711. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
  712. }
  713. }
  714. $cnt = count($sorted_lsub_ary);
  715. }
  716. $sorted_lsub_ary = array_values($sorted_lsub_ary);
  717. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
  718. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
  719. return $boxesnew;
  720. }
  721. }
  722. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
  723. global $data_dir, $username, $list_special_folders_first,
  724. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  725. $move_to_trash, $move_to_sent, $save_as_draft,
  726. $delimiter, $imap_server_type;
  727. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  728. /* create virtual root node */
  729. $mailboxes= new mailboxes();
  730. $mailboxes->is_root = true;
  731. $trail_del = false;
  732. $start = 0;
  733. if (isset($folder_prefix) && ($folder_prefix != '')) {
  734. $start = substr_count($folder_prefix,$delimiter);
  735. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  736. $trail_del = true;
  737. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  738. } else {
  739. $mailboxes->mailboxname_full = $folder_prefix;
  740. $start++;
  741. }
  742. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  743. } else {
  744. $start = 0;
  745. }
  746. $cnt = count($mbx_ary);
  747. for ($i=0; $i < $cnt; $i++) {
  748. if ($mbx_ary[$i]['mbx'] !='' ) {
  749. $mbx = new mailboxes();
  750. $mailbox = $mbx_ary[$i]['mbx'];
  751. switch ($mailbox) {
  752. case 'INBOX':
  753. $mbx->is_inbox = true;
  754. $mbx->is_special = true;
  755. break;
  756. case $trash_folder:
  757. $mbx->is_trash = true;
  758. $mbx->is_special = true;
  759. break;
  760. case $sent_folder:
  761. $mbx->is_sent = true;
  762. $mbx->is_special = true;
  763. break;
  764. case $draft_folder:
  765. $mbx->is_draft = true;
  766. $mbx->is_special = true;
  767. break;
  768. }
  769. if (isset($mbx_ary[$i]['unseen'])) {
  770. $mbx->unseen = $mbx_ary[$i]['unseen'];
  771. }
  772. if (isset($mbx_ary[$i]['nummessages'])) {
  773. $mbx->total = $mbx_ary[$i]['nummessages'];
  774. }
  775. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  776. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  777. if ($r_del_pos) {
  778. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  779. } else { /* mailbox is root folder */
  780. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  781. }
  782. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  783. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  784. }
  785. }
  786. sqimap_utf7_decode_mbx_tree($mailboxes);
  787. sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
  788. return $mailboxes;
  789. }
  790. function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
  791. $mbx_tree->mailboxname_sub=imap_utf7_decode_local($mbx_tree->mailboxname_sub);
  792. if ($mbx_tree->mbxs) {
  793. $iCnt = count($mbx_tree->mbxs);
  794. for ($i=0;$i<$iCnt;++$i) {
  795. $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
  796. }
  797. }
  798. }
  799. function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
  800. if ($mbx_tree)
  801. $aMbxs[] =& $mbx_tree;
  802. if ($mbx_tree->mbxs) {
  803. $iCnt = count($mbx_tree->mbxs);
  804. for ($i=0;$i<$iCnt;++$i) {
  805. sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
  806. }
  807. }
  808. }
  809. /* Define preferences for folder settings. */
  810. /* FIXME, we should load constants.php
  811. unseen_notify
  812. define('SMPREF_UNSEEN_NONE', 1);
  813. define('SMPREF_UNSEEN_INBOX', 2);
  814. define('SMPREF_UNSEEN_ALL', 3);
  815. define('SMPREF_UNSEEN_SPECIAL', 4); // Only special folders
  816. define('SMPREF_UNSEEN_NORMAL', 5); // Only normal folders
  817. unseen_type
  818. define('SMPREF_UNSEEN_ONLY', 1);
  819. define('SMPREF_UNSEEN_TOTAL', 2);
  820. */
  821. function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
  822. global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
  823. $aMbxs = $aQuery = $aTag = array();
  824. sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
  825. // remove the root node
  826. array_shift($aMbxs);
  827. if($unseen_notify == 3) {
  828. $cnt = count($aMbxs);
  829. for($i=0;$i<$cnt;++$i) {
  830. $oMbx =& $aMbxs[$i];
  831. if (!$oMbx->is_noselect) {
  832. $mbx = $oMbx->mailboxname_full;
  833. if ($unseen_type == 2 ||
  834. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  835. $query = "STATUS \"$mbx\" (MESSAGES UNSEEN)";
  836. } else {
  837. $query = "STATUS \"$mbx\" (UNSEEN)";
  838. }
  839. sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
  840. } else {
  841. $oMbx->unseen = $oMbx->total = false;
  842. $tag = false;
  843. }
  844. $oMbx->tag = $tag;
  845. $aMbxs[$i] =& $oMbx;
  846. }
  847. // execute all the queries at once
  848. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  849. $cnt = count($aMbxs);
  850. for($i=0;$i<$cnt;++$i) {
  851. $oMbx =& $aMbxs[$i];
  852. $tag = $oMbx->tag;
  853. if ($tag && $aServerResponse[$tag] == 'OK') {
  854. $sResponse = implode('', $aResponse[$tag]);
  855. if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
  856. $oMbx->unseen = $regs[1];
  857. }
  858. if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
  859. $oMbx->total = $regs[1];
  860. }
  861. }
  862. unset($oMbx->tag);
  863. }
  864. } else if ($unseen_notify == 2) { // INBOX only
  865. $cnt = count($aMbxs);
  866. for($i=0;$i<$cnt;++$i) {
  867. $oMbx =& $aMbxs[$i];
  868. if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
  869. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  870. if ($unseen_type == 2 ||
  871. ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
  872. $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
  873. $oMbx->unseen = $aStatus['UNSEEN'];
  874. $oMbx->total = $aStatus['MESSAGES'];
  875. } else {
  876. $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
  877. }
  878. $aMbxs[$i] =& $oMbx;
  879. if (!$move_to_trash && $trash_folder) {
  880. break;
  881. } else {
  882. // trash comes after INBOX
  883. if ($oMbx->mailboxname_full == $trash_folder) {
  884. break;
  885. }
  886. }
  887. }
  888. }
  889. }
  890. }
  891. ?>