imap_mailbox.php 15 KB

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