imap_mailbox.php 18 KB

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