imap_mailbox.php 16 KB

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