imap_mailbox.php 15 KB

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