imap_mailbox.php 15 KB

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