imap_mailbox.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. /**
  3. * imap_mailbox.php
  4. *
  5. * Copyright (c) 1999-2001 The SquirrelMail Development 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. /*************************
  13. ** Expunges a mailbox **
  14. *************************/
  15. function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true)
  16. {
  17. fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
  18. $read = sqimap_read_data($imap_stream, sqimap_session_id(),
  19. $handle_errors, $response, $message);
  20. }
  21. /******************************************************************************
  22. ** Checks whether or not the specified mailbox exists
  23. ******************************************************************************/
  24. function sqimap_mailbox_exists ($imap_stream, $mailbox)
  25. {
  26. if (! isset($mailbox)) {
  27. return false;
  28. }
  29. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
  30. $mbx = sqimap_read_data($imap_stream, sqimap_session_id(),
  31. true, $response, $message);
  32. return isset($mbx[0]);
  33. }
  34. /******************************************************************************
  35. ** Selects a mailbox
  36. ******************************************************************************/
  37. function sqimap_mailbox_select ($imap_stream, $mailbox,
  38. $hide=true, $recent=false)
  39. {
  40. global $auto_expunge;
  41. if ( $mailbox == 'None' ) {
  42. return;
  43. }
  44. fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
  45. $read = sqimap_read_data($imap_stream, sqimap_session_id(),
  46. true, $response, $message);
  47. if ($recent) {
  48. for ($i=0; $i<count($read); $i++) {
  49. if (strpos(strtolower($read[$i]), 'recent')) {
  50. $r = explode(' ', $read[$i]);
  51. }
  52. }
  53. return $r[1];
  54. }
  55. if ($auto_expunge) {
  56. fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
  57. $tmp = sqimap_read_data($imap_stream, sqimap_session_id(),
  58. false, $a, $b);
  59. }
  60. }
  61. /******************************************************************************
  62. ** Creates a folder
  63. ******************************************************************************/
  64. function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
  65. {
  66. global $delimiter;
  67. if (strtolower($type) == 'noselect') {
  68. $mailbox = $mailbox.$delimiter;
  69. }
  70. fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
  71. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  72. true, $response, $message);
  73. sqimap_subscribe ($imap_stream, $mailbox);
  74. }
  75. /******************************************************************************
  76. ** Subscribes to an existing folder
  77. ******************************************************************************/
  78. function sqimap_subscribe ($imap_stream, $mailbox)
  79. {
  80. fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
  81. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  82. true, $response, $message);
  83. }
  84. /******************************************************************************
  85. ** Unsubscribes to an existing folder
  86. ******************************************************************************/
  87. function sqimap_unsubscribe ($imap_stream, $mailbox)
  88. {
  89. global $imap_server_type;
  90. fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
  91. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  92. true, $response, $message);
  93. }
  94. /******************************************************************************
  95. ** This function simply deletes the given folder
  96. ******************************************************************************/
  97. function sqimap_mailbox_delete ($imap_stream, $mailbox)
  98. {
  99. fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
  100. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  101. true, $response, $message);
  102. sqimap_unsubscribe ($imap_stream, $mailbox);
  103. }
  104. /***********************************************************************
  105. ** Determines if the user is subscribed to the folder or not
  106. **********************************************************************/
  107. function sqimap_mailbox_is_subscribed($imap_stream, $folder)
  108. {
  109. $boxes = sqimap_mailbox_list ($imap_stream);
  110. foreach ($boxes as $ref) {
  111. if ($ref['unformatted'] == $folder) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. /******************************************************************************
  118. ** Formats a mailbox into 4 parts for the $boxes array
  119. **
  120. ** The four parts are:
  121. **
  122. ** raw - Raw LIST/LSUB response from the IMAP server
  123. ** formatted - nicely formatted folder name
  124. ** unformatted - unformatted, but with delimiter at end removed
  125. ** unformatted-dm - folder name as it appears in raw response
  126. ** unformatted-disp - unformatted without $folder_prefix
  127. **
  128. ******************************************************************************/
  129. function sqimap_mailbox_parse ($line, $line_lsub)
  130. {
  131. global $folder_prefix, $delimiter;
  132. /* Process each folder line */
  133. for ($g=0; $g < count($line); $g++) {
  134. /* Store the raw IMAP reply */
  135. if (isset($line[$g])) {
  136. $boxes[$g]["raw"] = $line[$g];
  137. }
  138. else {
  139. $boxes[$g]["raw"] = "";
  140. }
  141. /* Count number of delimiters ($delimiter) in folder name */
  142. $mailbox = trim($line_lsub[$g]);
  143. $dm_count = countCharInString($mailbox, $delimiter);
  144. if (substr($mailbox, -1) == $delimiter) {
  145. /* If name ends in delimiter - decrement count by one */
  146. $dm_count--;
  147. }
  148. /* Format folder name, but only if it's a INBOX.* or have */
  149. /* a parent. */
  150. $boxesbyname[$mailbox] = $g;
  151. $parentfolder = readMailboxParent($mailbox, $delimiter);
  152. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  153. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  154. ( isset($boxesbyname[$parentfolder]) &&
  155. (strlen($parentfolder) > 0) ) ) {
  156. $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
  157. if ($indent > 0) {
  158. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
  159. }
  160. else {
  161. $boxes[$g]["formatted"] = '';
  162. }
  163. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
  164. }
  165. else {
  166. $boxes[$g]["formatted"] = $mailbox;
  167. }
  168. $boxes[$g]['unformatted-dm'] = $mailbox;
  169. if (substr($mailbox, -1) == $delimiter) {
  170. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  171. }
  172. $boxes[$g]['unformatted'] = $mailbox;
  173. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  174. $mailbox = substr($mailbox, strlen($folder_prefix));
  175. }
  176. $boxes[$g]['unformatted-disp'] = $mailbox;
  177. $boxes[$g]['id'] = $g;
  178. $boxes[$g]['flags'] = array();
  179. if (isset($line[$g])) {
  180. ereg("\(([^)]*)\)",$line[$g],$regs);
  181. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  182. if ($flags) {
  183. $boxes[$g]['flags'] = explode(' ', $flags);
  184. }
  185. }
  186. }
  187. return $boxes;
  188. }
  189. /* Apparently you must call a user function with usort instead
  190. * of calling a built-in directly. Stupid.
  191. * Patch from dave_michmerhuizen@yahoo.com
  192. * Allows case insensitivity when sorting folders
  193. */
  194. function user_strcasecmp($a, $b)
  195. {
  196. return strcasecmp($a, $b);
  197. }
  198. /******************************************************************************
  199. ** Returns sorted mailbox lists in several different ways.
  200. ** See comment on sqimap_mailbox_parse() for info about the returned array.
  201. ******************************************************************************/
  202. function sqimap_mailbox_list ($imap_stream)
  203. {
  204. global $data_dir, $username, $list_special_folders_first;
  205. global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
  206. global $move_to_trash, $move_to_sent, $save_as_draft;
  207. global $delimiter;
  208. $inbox_in_list = false;
  209. $inbox_subscribed = false;
  210. require_once('../src/load_prefs.php');
  211. require_once('../functions/array.php');
  212. /** LSUB array **/
  213. fputs ($imap_stream, sqimap_session_id() .
  214. " LSUB \"$folder_prefix\" \"*\"\r\n");
  215. $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
  216. true, $response, $message);
  217. /* Section about removing the last element was removed */
  218. /* We don't return "* OK" anymore from sqimap_read_data */
  219. $sorted_lsub_ary = array();
  220. for ($i=0;$i < count($lsub_ary); $i++) {
  221. /* Workaround for EIMS */
  222. /* Doesn't work if the mailbox name is multiple lines */
  223. if (isset($lsub_ary[$i + 1]) &&
  224. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  225. $lsub_ary[$i], $regs)) {
  226. $i ++;
  227. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
  228. '"' . $regs[2];
  229. }
  230. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  231. $sorted_lsub_ary[] = $temp_mailbox_name;
  232. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  233. $inbox_subscribed = true;
  234. }
  235. }
  236. $new_ary = array();
  237. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  238. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  239. $new_ary[] = $sorted_lsub_ary[$i];
  240. }
  241. }
  242. $sorted_lsub_ary = $new_ary;
  243. if (isset($sorted_lsub_ary)) {
  244. usort($sorted_lsub_ary, 'user_strcasecmp');
  245. /*sort($sorted_lsub_ary); */
  246. }
  247. /** LIST array **/
  248. $sorted_list_ary = array();
  249. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  250. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  251. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  252. }
  253. else {
  254. $mbx = $sorted_lsub_ary[$i];
  255. }
  256. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
  257. $read = sqimap_read_data ($imap_stream, sqimap_session_id(),
  258. true, $response, $message);
  259. /* Another workaround for EIMS */
  260. if (isset($read[1]) &&
  261. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  262. $read[0], $regs)) {
  263. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
  264. '"' . $regs[2];
  265. }
  266. if (isset($sorted_list_ary[$i])) {
  267. $sorted_list_ary[$i] = "";
  268. }
  269. if (isset($read[0])) {
  270. $sorted_list_ary[$i] = $read[0];
  271. }
  272. else {
  273. $sorted_list_ary[$i] = "";
  274. }
  275. if (isset($sorted_list_ary[$i]) &&
  276. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") {
  277. $inbox_in_list = true;
  278. }
  279. }
  280. /**
  281. * Just in case they're not subscribed to their inbox,
  282. * we'll get it for them anyway
  283. */
  284. if ($inbox_subscribed == false || $inbox_in_list == false) {
  285. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
  286. $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
  287. true, $response, $message);
  288. /* Another workaround for EIMS */
  289. if (isset($inbox_ary[1]) &&
  290. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  291. $inbox_ary[0], $regs)) {
  292. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  293. '"' . $regs[2];
  294. }
  295. $sorted_list_ary[] = $inbox_ary[0];
  296. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  297. }
  298. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  299. /** Now, lets sort for special folders **/
  300. $boxesnew = Array();
  301. /* Find INBOX */
  302. for ($i = 0; $i < count($boxes); $i++) {
  303. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  304. $boxesnew[] = $boxes[$i];
  305. $used[$i] = true;
  306. $i = count($boxes);
  307. }
  308. }
  309. /* List special folders and their subfolders, if requested. */
  310. if ($list_special_folders_first == true) {
  311. /* First list the trash folder. */
  312. for ($i = 0 ; $i < count($boxes) ; $i++) {
  313. if ($move_to_trash &&
  314. eregi('^' . quotemeta($trash_folder) . '(' .
  315. quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
  316. $boxesnew[] = $boxes[$i];
  317. $used[$i] = true;
  318. }
  319. }
  320. /* Then list the sent folder. */
  321. for ($i = 0 ; $i < count($boxes) ; $i++) {
  322. if ($move_to_sent &&
  323. eregi('^' . quotemeta($sent_folder) . '(' .
  324. quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
  325. $boxesnew[] = $boxes[$i];
  326. $used[$i] = true;
  327. }
  328. }
  329. /* Lastly, list the list the draft folder. */
  330. for ($i = 0 ; $i < count($boxes) ; $i++) {
  331. if ($save_as_draft &&
  332. eregi('^' . quotemeta($draft_folder) . '(' .
  333. quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
  334. $boxesnew[] = $boxes[$i];
  335. $used[$i] = true;
  336. }
  337. }
  338. /* Put INBOX.* folders ahead of the rest. */
  339. for ($i = 0; $i < count($boxes); $i++) {
  340. if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
  341. (!isset($used[$i]) || $used[$i] == false)) {
  342. $boxesnew[] = $boxes[$i];
  343. $used[$i] = true;
  344. }
  345. }
  346. }
  347. /* Rest of the folders */
  348. for ($i = 0; $i < count($boxes); $i++) {
  349. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  350. (!isset($used[$i]) || $used[$i] == false)) {
  351. $boxesnew[] = $boxes[$i];
  352. $used[$i] = true;
  353. }
  354. }
  355. return $boxesnew;
  356. }
  357. /******************************************************************************
  358. ** Returns a list of all folders, subscribed or not
  359. ******************************************************************************/
  360. function sqimap_mailbox_list_all ($imap_stream)
  361. {
  362. global $list_special_folders_first, $folder_prefix;
  363. global $delimiter;
  364. if (!function_exists ("ary_sort")) {
  365. include_once('../functions/array.php');
  366. }
  367. $ssid = sqimap_session_id();
  368. $lsid = strlen( $ssid );
  369. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  370. $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
  371. $g = 0;
  372. $phase = "inbox";
  373. for ($i = 0; $i < count($read_ary); $i++) {
  374. /* Another workaround for EIMS */
  375. if (isset($read_ary[$i + 1]) &&
  376. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  377. $read_ary[$i], $regs)) {
  378. $i ++;
  379. $read_ary[$i] = $regs[1] . '"' .
  380. addslashes(trim($read_ary[$i])) .
  381. '"' . $regs[2];
  382. }
  383. if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
  384. /* Store the raw IMAP reply */
  385. $boxes[$g]["raw"] = $read_ary[$i];
  386. /* Count number of delimiters ($delimiter) in folder name */
  387. $mailbox = find_mailbox_name($read_ary[$i]);
  388. $dm_count = countCharInString($mailbox, $delimiter);
  389. if (substr($mailbox, -1) == $delimiter) {
  390. /* If name ends in delimiter - decrement count by one */
  391. $dm_count--;
  392. }
  393. /* Format folder name, but only if it's a INBOX.* or have */
  394. /* a parent. */
  395. $boxesbyname[$mailbox] = $g;
  396. $parentfolder = readMailboxParent($mailbox, $delimiter);
  397. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  398. (ereg('^'.$folder_prefix, $mailbox)) ||
  399. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  400. if ($dm_count) {
  401. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
  402. }
  403. else {
  404. $boxes[$g]["formatted"] = '';
  405. }
  406. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
  407. }
  408. else {
  409. $boxes[$g]["formatted"] = $mailbox;
  410. }
  411. $boxes[$g]["unformatted-dm"] = $mailbox;
  412. if (substr($mailbox, -1) == $delimiter) {
  413. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  414. }
  415. $boxes[$g]["unformatted"] = $mailbox;
  416. $boxes[$g]["unformatted-disp"] =
  417. ereg_replace('^' . $folder_prefix, '', $mailbox);
  418. $boxes[$g]["id"] = $g;
  419. /** Now lets get the flags for this mailbox **/
  420. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
  421. $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(),
  422. true, $response, $message);
  423. /* Another workaround for EIMS */
  424. if (isset($read_mlbx[1]) &&
  425. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  426. $read_mlbx[0], $regs)) {
  427. $read_mlbx[0] = $regs[1] . '"' .
  428. addslashes(trim($read_mlbx[1])) .
  429. '"' . $regs[2];
  430. }
  431. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  432. $flags = substr($flags, 0, strpos($flags, ")"));
  433. $flags = str_replace('\\', '', $flags);
  434. $flags = trim(strtolower($flags));
  435. if ($flags) {
  436. $boxes[$g]['flags'] = explode(" ", $flags);
  437. }
  438. else {
  439. $boxes[$g]['flags'] = array();
  440. }
  441. }
  442. $g++;
  443. }
  444. if(is_array($boxes)) {
  445. $boxes = ary_sort ($boxes, "unformatted", 1);
  446. }
  447. return $boxes;
  448. }
  449. ?>