folder_manip.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * folder_manip.php
  4. *
  5. * Copyright (c) 1999-2005 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * Functions for IMAP folder manipulation:
  9. * (un)subscribe, create, rename, delete.
  10. *
  11. * @version $Id$
  12. * @package squirrelmail
  13. * @see folders.php
  14. * @author Thijs Kinkhorst - kink@squirrelmail.org
  15. */
  16. /**
  17. * Helper function for the functions below; checks if the user entered
  18. * folder name is valid according to the IMAP standard. If not, it
  19. * bails out with an error and cleanly terminates the IMAP connection.
  20. */
  21. function folders_checkname($imapConnection, $folder_name, $delimiter)
  22. {
  23. if (substr_count($folder_name, '"') || substr_count($folder_name, "\\") ||
  24. substr_count($folder_name, $delimiter) || ($folder_name == '')) {
  25. global $color;
  26. plain_error_message(_("Illegal folder name. Please select a different name.").
  27. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  28. sqimap_logout($imapConnection);
  29. exit;
  30. }
  31. }
  32. /**
  33. * Called from folders.php to create a new folder.
  34. */
  35. function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs)
  36. {
  37. folders_checkname($imapConnection, $folder_name, $delimiter);
  38. global $folder_prefix;
  39. $folder_name = imap_utf7_encode_local($folder_name);
  40. if ( ! empty($contain_subs) ) {
  41. $folder_name = $folder_name . $delimiter;
  42. }
  43. if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) {
  44. $folder_prefix = $folder_prefix . $delimiter;
  45. }
  46. if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) {
  47. $subfolder_orig = $subfolder;
  48. $subfolder = $folder_prefix . $subfolder;
  49. } else {
  50. $subfolder_orig = $subfolder;
  51. }
  52. if (trim($subfolder_orig) == '') {
  53. sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, '');
  54. } else {
  55. sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, '');
  56. }
  57. return;
  58. }
  59. /**
  60. * Called from folders.php, given a folder name, ask the user what this
  61. * folder should be renamed to.
  62. */
  63. function folders_rename_getname ($imapConnection, $delimiter, $old) {
  64. global $color;
  65. if ( $old == '' ) {
  66. plain_error_message(_("You have not selected a folder to rename. Please do so.").
  67. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  68. sqimap_logout($imapConnection);
  69. exit;
  70. }
  71. if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
  72. $isfolder = TRUE;
  73. $old = substr($old, 0, strlen($old) - 1);
  74. } else {
  75. $isfolder = FALSE;
  76. }
  77. $old = imap_utf7_decode_local($old);
  78. if (strpos($old, $delimiter)) {
  79. $old_name = substr($old, strrpos($old, $delimiter)+1, strlen($old));
  80. $old_parent = substr($old, 0, strrpos($old, $delimiter));
  81. } else {
  82. $old_name = $old;
  83. $old_parent = '';
  84. }
  85. echo '<br />' .
  86. html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
  87. html_tag( 'tr',
  88. html_tag( 'td', '<b>' . _("Rename a folder") . '</b>', 'center', $color[0] )
  89. ) .
  90. html_tag( 'tr' ) .
  91. html_tag( 'td', '', 'center', $color[4] ) .
  92. addForm('folders.php').
  93. addHidden('smaction', 'rename').
  94. _("New name:").
  95. '<br /><b>' . htmlspecialchars($old_parent) . ' ' . htmlspecialchars($delimiter) . '</b>' .
  96. addInput('new_name', $old_name, 25) . '<br /><br />' . "\n";
  97. if ( $isfolder ) {
  98. echo addHidden('isfolder', 'true');
  99. }
  100. echo addHidden('orig', $old).
  101. addHidden('old_name', $old_name).
  102. '<input type="submit" value="'._("Rename")."\" />\n".
  103. '<input type="submit" name="cancelbutton" value="'._("Cancel")."\" />\n".
  104. '</form><br /></td></tr></table>';
  105. echo "\n\n</body></html>";
  106. sqimap_logout($imapConnection);
  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;
  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. exit;
  150. }
  151. echo '<br />' .
  152. html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
  153. html_tag( 'tr',
  154. html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[0] )
  155. ) .
  156. html_tag( 'tr' ) .
  157. html_tag( 'td', '', 'center', $color[4] ) .
  158. sprintf(_("Are you sure you want to delete %s?"),
  159. str_replace(array(' ','<','>'),array('&nbsp;','&lt;','&gt;'),
  160. imap_utf7_decode_local($folder_name))).
  161. addForm('folders.php', 'post')."<p>\n".
  162. addHidden('smaction', 'delete').
  163. addHidden('folder_name', $folder_name).
  164. addSubmit(_("Yes"), 'confirmed').
  165. addSubmit(_("No"), 'cancelbutton').
  166. '</p></form><br /></td></tr></table>';
  167. echo "\n\n</body></html>";
  168. sqimap_logout($imapConnection);
  169. exit;
  170. }
  171. /**
  172. * Given a folder, moves it to trash (and all subfolders of it too).
  173. */
  174. function folders_delete_do ($imapConnection, $delimiter, $folder_name)
  175. {
  176. require_once(SM_PATH . 'functions/tree.php');
  177. $boxes = sqimap_mailbox_list ($imapConnection);
  178. global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
  179. if (substr($folder_name, -1) == $delimiter) {
  180. $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
  181. } else {
  182. $folder_name_no_dm = $folder_name;
  183. }
  184. /** lets see if we CAN move folders to the trash.. otherwise,
  185. ** just delete them **/
  186. /* Courier IMAP doesn't like subfolders of Trash
  187. * If global options say we can't move it into Trash
  188. * If it's already a subfolder of trash, we'll have to delete it */
  189. if (strtolower($imap_server_type) == 'courier' ||
  190. (isset($delete_folder) && $delete_folder) ||
  191. eregi('^'.$trash_folder.'.+', $folder_name) )
  192. {
  193. $can_move_to_trash = FALSE;
  194. }
  195. /* Otherwise, check if trash folder exits and support sub-folders */
  196. else {
  197. foreach($boxes as $box) {
  198. if ($box['unformatted'] == $trash_folder) {
  199. $can_move_to_trash = !in_array('noinferiors', $box['flags']);
  200. }
  201. }
  202. }
  203. /** First create the top node in the tree **/
  204. foreach($boxes as $box) {
  205. if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
  206. $foldersTree[0]['value'] = $folder_name;
  207. $foldersTree[0]['doIHaveChildren'] = false;
  208. continue;
  209. }
  210. }
  211. /* Now create the nodes for subfolders of the parent folder
  212. You can tell that it is a subfolder by tacking the mailbox delimiter
  213. on the end of the $folder_name string, and compare to that. */
  214. foreach($boxes as $box) {
  215. if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
  216. addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
  217. }
  218. }
  219. /** Lets start removing the folders and messages **/
  220. if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
  221. walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
  222. walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
  223. } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
  224. walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
  225. }
  226. return;
  227. }
  228. /**
  229. * Given an array of folder_names, subscribes to each of them.
  230. */
  231. function folders_subscribe($imapConnection, $folder_names)
  232. {
  233. global $color;
  234. if (count($folder_names) == 0 || $folder_names[0] == '') {
  235. plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
  236. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  237. sqimap_logout($imapConnection);
  238. exit;
  239. }
  240. global $no_list_for_subscribe, $imap_server_type;
  241. if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
  242. /* Cyrus, atleast, does not typically allow subscription to
  243. * nonexistent folders (this is an optional part of IMAP),
  244. * lets catch it here and report back cleanly. */
  245. if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
  246. plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
  247. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  248. sqimap_logout($imapConnection);
  249. exit;
  250. }
  251. }
  252. foreach ( $folder_names as $folder_name ) {
  253. sqimap_subscribe ($imapConnection, $folder_name);
  254. }
  255. return;
  256. }
  257. /**
  258. * Given a list of folder names, unsubscribes from each of them.
  259. */
  260. function folders_unsubscribe($imapConnection, $folder_names)
  261. {
  262. global $color;
  263. if (count($folder_names) == 0 || $folder_names[0] == '') {
  264. plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
  265. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  266. sqimap_logout($imapConnection);
  267. exit;
  268. }
  269. foreach ( $folder_names as $folder_name ) {
  270. sqimap_unsubscribe ($imapConnection, $folder_name);
  271. }
  272. return;
  273. }
  274. ?>