folder_manip.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * folder_manip.php
  4. *
  5. * Functions for IMAP folder manipulation:
  6. * (un)subscribe, create, rename, delete.
  7. *
  8. * @author Thijs Kinkhorst <kink at squirrelmail.org>
  9. * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id$
  12. * @package squirrelmail
  13. * @see folders.php
  14. */
  15. /**
  16. * Helper function for the functions below; checks if the user entered
  17. * folder name is valid according to the IMAP standard. If not, it
  18. * bails out with an error and cleanly terminates the IMAP connection.
  19. */
  20. function folders_checkname($imapConnection, $folder_name, $delimiter)
  21. {
  22. if (substr_count($folder_name, '"') || substr_count($folder_name, "\\") ||
  23. substr_count($folder_name, $delimiter) || ($folder_name == '')) {
  24. global $color, $oTemplate;
  25. error_box(_("Illegal folder name.") . "<br />\n" .
  26. sprintf(_("The name may not contain any of the following: %s"), '<tt>" \\ '.$delimiter.'</tt>')
  27. . "<br />\n" .
  28. _("Please select a different name.").
  29. '<br /><a href="folders.php">'.
  30. _("Click here to go back") . '</a>.');
  31. sqimap_logout($imapConnection);
  32. $oTemplate->display('footer.tpl');
  33. exit;
  34. }
  35. }
  36. /**
  37. * Called from folders.php to create a new folder.
  38. */
  39. function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs)
  40. {
  41. folders_checkname($imapConnection, $folder_name, $delimiter);
  42. global $folder_prefix;
  43. $folder_name = imap_utf7_encode_local($folder_name);
  44. if ( ! empty($contain_subs) ) {
  45. $folder_name = $folder_name . $delimiter;
  46. }
  47. if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) {
  48. $folder_prefix = $folder_prefix . $delimiter;
  49. }
  50. if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) {
  51. $subfolder_orig = $subfolder;
  52. $subfolder = $folder_prefix . $subfolder;
  53. } else {
  54. $subfolder_orig = $subfolder;
  55. }
  56. if (trim($subfolder_orig) == '') {
  57. sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, '');
  58. } else {
  59. sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, '');
  60. }
  61. return;
  62. }
  63. /**
  64. * Called from folders.php, given a folder name, ask the user what this
  65. * folder should be renamed to.
  66. */
  67. function folders_rename_getname ($imapConnection, $delimiter, $old) {
  68. global $color, $default_folder_prefix, $oTemplate;
  69. if ( $old == '' ) {
  70. plain_error_message(_("You have not selected a folder to rename. Please do so.").
  71. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  72. sqimap_logout($imapConnection);
  73. $oTemplate->display('footer.tpl');
  74. exit;
  75. }
  76. if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
  77. $isfolder = TRUE;
  78. $old = substr($old, 0, strlen($old) - 1);
  79. } else {
  80. $isfolder = FALSE;
  81. }
  82. $old = imap_utf7_decode_local($old);
  83. if (strpos($old, $delimiter)) {
  84. $old_name = substr($old, strrpos($old, $delimiter)+1, strlen($old));
  85. // hide default prefix (INBOX., mail/ or other)
  86. $quoted_prefix=preg_quote($default_folder_prefix,'/');
  87. $prefix_length=(preg_match("/^$quoted_prefix/",$old) ? strlen($default_folder_prefix) : 0);
  88. if ($prefix_length>strrpos($old, $delimiter)) {
  89. $old_parent = '';
  90. } else {
  91. $old_parent = substr($old, $prefix_length, (strrpos($old, $delimiter)-$prefix_length))
  92. . ' ' . $delimiter;
  93. }
  94. } else {
  95. $old_name = $old;
  96. $old_parent = '';
  97. }
  98. sqimap_logout($imapConnection);
  99. $oTemplate->assign('dialog_type', 'rename');
  100. $oTemplate->assign('color', $color);
  101. $oTemplate->assign('old_parent', htmlspecialchars($old_parent));
  102. $oTemplate->assign('old', htmlspecialchars($old));
  103. $oTemplate->assign('old_name', htmlspecialchars($old_name));
  104. $oTemplate->assign('isfolder', $isfolder);
  105. $oTemplate->display('folder_manip_dialog.tpl');
  106. $oTemplate->display('footer.tpl');
  107. exit;
  108. }
  109. /**
  110. * Given an old and new folder name, renames the folder.
  111. */
  112. function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
  113. {
  114. folders_checkname($imapConnection, $new_name, $delimiter);
  115. $orig = imap_utf7_encode_local($orig);
  116. $old_name = imap_utf7_encode_local($old_name);
  117. $new_name = imap_utf7_encode_local($new_name);
  118. if ($old_name != $new_name) {
  119. if (strpos($orig, $delimiter)) {
  120. $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
  121. } else {
  122. $old_dir = '';
  123. }
  124. if ($old_dir != '') {
  125. $newone = $old_dir . $delimiter . $new_name;
  126. } else {
  127. $newone = $new_name;
  128. }
  129. // Renaming a folder doesn't rename the folder but leaves you unsubscribed
  130. // at least on Cyrus IMAP servers.
  131. if (isset($isfolder)) {
  132. $newone = $newone.$delimiter;
  133. $orig = $orig.$delimiter;
  134. }
  135. sqimap_mailbox_rename( $imapConnection, $orig, $newone );
  136. }
  137. return;
  138. }
  139. /**
  140. * Presents a confirmation dialog to the user asking whether they're
  141. * sure they want to delete this folder.
  142. */
  143. function folders_delete_ask ($imapConnection, $folder_name)
  144. {
  145. global $color, $default_folder_prefix, $oTemplate;
  146. if ($folder_name == '') {
  147. plain_error_message(_("You have not selected a folder to delete. Please do so.").
  148. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  149. sqimap_logout($imapConnection);
  150. $oTemplate->display('footer.tpl');
  151. exit;
  152. }
  153. // hide default folder prefix (INBOX., mail/ or other)
  154. $visible_folder_name = imap_utf7_decode_local($folder_name);
  155. $quoted_prefix = preg_quote($default_folder_prefix,'/');
  156. $prefix_length = (preg_match("/^$quoted_prefix/",$visible_folder_name) ? strlen($default_folder_prefix) : 0);
  157. $visible_folder_name = substr($visible_folder_name,$prefix_length);
  158. sqimap_logout($imapConnection);
  159. $oTemplate->assign('dialog_type', 'delete');
  160. $oTemplate->assign('color', $color);
  161. $oTemplate->assign('folder_name', htmlspecialchars($folder_name));
  162. $oTemplate->assign('visible_folder_name', htmlspecialchars($visible_folder_name));
  163. $oTemplate->display('folder_manip_dialog.tpl');
  164. $oTemplate->display('footer.tpl');
  165. exit;
  166. }
  167. /**
  168. * Given a folder, moves it to trash (and all subfolders of it too).
  169. */
  170. function folders_delete_do ($imapConnection, $delimiter, $folder_name)
  171. {
  172. include(SM_PATH . 'functions/tree.php');
  173. $boxes = sqimap_mailbox_list ($imapConnection);
  174. global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
  175. if (substr($folder_name, -1) == $delimiter) {
  176. $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
  177. } else {
  178. $folder_name_no_dm = $folder_name;
  179. }
  180. /** lets see if we CAN move folders to the trash.. otherwise,
  181. ** just delete them **/
  182. if ($delete_folder || eregi('^'.$trash_folder.'.+', $folder_name) ) {
  183. $can_move_to_trash = FALSE;
  184. } else {
  185. /* Otherwise, check if trash folder exits and support sub-folders */
  186. foreach($boxes as $box) {
  187. if ($box['unformatted'] == $trash_folder) {
  188. $can_move_to_trash = !in_array('noinferiors', $box['flags']);
  189. }
  190. }
  191. }
  192. /** First create the top node in the tree **/
  193. foreach($boxes as $box) {
  194. if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
  195. $foldersTree[0]['value'] = $folder_name;
  196. $foldersTree[0]['doIHaveChildren'] = false;
  197. continue;
  198. }
  199. }
  200. /* Now create the nodes for subfolders of the parent folder
  201. You can tell that it is a subfolder by tacking the mailbox delimiter
  202. on the end of the $folder_name string, and compare to that. */
  203. foreach($boxes as $box) {
  204. if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
  205. addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
  206. }
  207. }
  208. /** Lets start removing the folders and messages **/
  209. if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
  210. walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
  211. walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
  212. } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
  213. walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
  214. }
  215. return;
  216. }
  217. /**
  218. * Given an array of folder_names, subscribes to each of them.
  219. */
  220. function folders_subscribe($imapConnection, $folder_names)
  221. {
  222. global $color, $oTemplate;
  223. if (count($folder_names) == 0 || $folder_names[0] == '') {
  224. plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
  225. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  226. sqimap_logout($imapConnection);
  227. $oTemplate->display('footer.tpl');
  228. exit;
  229. }
  230. global $no_list_for_subscribe, $imap_server_type;
  231. if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
  232. /* Cyrus, atleast, does not typically allow subscription to
  233. * nonexistent folders (this is an optional part of IMAP),
  234. * lets catch it here and report back cleanly. */
  235. if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
  236. plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
  237. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  238. sqimap_logout($imapConnection);
  239. exit;
  240. }
  241. }
  242. foreach ( $folder_names as $folder_name ) {
  243. sqimap_subscribe ($imapConnection, $folder_name);
  244. }
  245. return;
  246. }
  247. /**
  248. * Given a list of folder names, unsubscribes from each of them.
  249. */
  250. function folders_unsubscribe($imapConnection, $folder_names)
  251. {
  252. global $color, $oTemplate;
  253. if (count($folder_names) == 0 || $folder_names[0] == '') {
  254. plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
  255. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  256. sqimap_logout($imapConnection);
  257. $oTemplate->display('footer.tpl');
  258. exit;
  259. }
  260. foreach ( $folder_names as $folder_name ) {
  261. sqimap_unsubscribe ($imapConnection, $folder_name);
  262. }
  263. return;
  264. }