imap_mailbox.php 18 KB

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