imap_mailbox.php 18 KB

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