folder_manip.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. error_box(_("Illegal folder name.") . "<br />\n" .
  27. sprintf(_("The name may not contain any of the following: %s"), '<tt>" \\ '.$delimiter.'</tt>')
  28. . "<br />\n" .
  29. _("Please select a different name.").
  30. '<br /><a href="folders.php">'.
  31. _("Click here to go back") . '</a>.', $color);
  32. sqimap_logout($imapConnection);
  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;
  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. exit;
  74. }
  75. if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
  76. $isfolder = TRUE;
  77. $old = substr($old, 0, strlen($old) - 1);
  78. } else {
  79. $isfolder = FALSE;
  80. }
  81. $old = imap_utf7_decode_local($old);
  82. if (strpos($old, $delimiter)) {
  83. $old_name = substr($old, strrpos($old, $delimiter)+1, strlen($old));
  84. // hide default prefix (INBOX., mail/ or other)
  85. $quoted_prefix=preg_quote($default_folder_prefix,'/');
  86. $prefix_length=(preg_match("/^$quoted_prefix/",$old) ? strlen($default_folder_prefix) : 0);
  87. if ($prefix_length>strrpos($old, $delimiter)) {
  88. $old_parent = '';
  89. } else {
  90. $old_parent = substr($old, $prefix_length, (strrpos($old, $delimiter)-$prefix_length))
  91. . ' ' . $delimiter;
  92. }
  93. } else {
  94. $old_name = $old;
  95. $old_parent = '';
  96. }
  97. echo '<br />' .
  98. html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
  99. html_tag( 'tr',
  100. html_tag( 'td', '<b>' . _("Rename a folder") . '</b>', 'center', $color[0] )
  101. ) .
  102. html_tag( 'tr' ) .
  103. html_tag( 'td', '', 'center', $color[4] ) .
  104. addForm('folders.php').
  105. addHidden('smaction', 'rename').
  106. _("New name:").
  107. '<br /><b>' . htmlspecialchars($old_parent) . '</b>' .
  108. addInput('new_name', $old_name, 25) . '<br /><br />' . "\n";
  109. if ( $isfolder ) {
  110. echo addHidden('isfolder', 'true');
  111. }
  112. echo addHidden('orig', $old).
  113. addHidden('old_name', $old_name).
  114. '<input type="submit" value="'._("Rename")."\" />\n".
  115. '<input type="submit" name="cancelbutton" value="'._("Cancel")."\" />\n".
  116. '</form><br /></td></tr></table>';
  117. echo "\n</td></tr></table>\n</td></tr></table>\n\n</body></html>";
  118. sqimap_logout($imapConnection);
  119. exit;
  120. }
  121. /**
  122. * Given an old and new folder name, renames the folder.
  123. */
  124. function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
  125. {
  126. folders_checkname($imapConnection, $new_name, $delimiter);
  127. $orig = imap_utf7_encode_local($orig);
  128. $old_name = imap_utf7_encode_local($old_name);
  129. $new_name = imap_utf7_encode_local($new_name);
  130. if ($old_name != $new_name) {
  131. if (strpos($orig, $delimiter)) {
  132. $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
  133. } else {
  134. $old_dir = '';
  135. }
  136. if ($old_dir != '') {
  137. $newone = $old_dir . $delimiter . $new_name;
  138. } else {
  139. $newone = $new_name;
  140. }
  141. // Renaming a folder doesn't rename the folder but leaves you unsubscribed
  142. // at least on Cyrus IMAP servers.
  143. if (isset($isfolder)) {
  144. $newone = $newone.$delimiter;
  145. $orig = $orig.$delimiter;
  146. }
  147. sqimap_mailbox_rename( $imapConnection, $orig, $newone );
  148. }
  149. return;
  150. }
  151. /**
  152. * Presents a confirmation dialog to the user asking whether they're
  153. * sure they want to delete this folder.
  154. */
  155. function folders_delete_ask ($imapConnection, $folder_name)
  156. {
  157. global $color,$default_folder_prefix;
  158. if ($folder_name == '') {
  159. plain_error_message(_("You have not selected a folder to delete. Please do so.").
  160. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  161. exit;
  162. }
  163. // hide default folder prefix (INBOX., mail/ or other)
  164. $visible_folder_name = imap_utf7_decode_local($folder_name);
  165. $quoted_prefix = preg_quote($default_folder_prefix,'/');
  166. $prefix_length = (preg_match("/^$quoted_prefix/",$visible_folder_name) ? strlen($default_folder_prefix) : 0);
  167. $visible_folder_name = substr($visible_folder_name,$prefix_length);
  168. echo '<br />' .
  169. html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
  170. html_tag( 'tr',
  171. html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[0] )
  172. ) .
  173. html_tag( 'tr' ) .
  174. html_tag( 'td', '', 'center', $color[4] ) .
  175. sprintf(_("Are you sure you want to delete %s?"),
  176. str_replace(array(' ','<','>'),array('&nbsp;','&lt;','&gt;'),$visible_folder_name)).
  177. addForm('folders.php', 'post')."<p>\n".
  178. addHidden('smaction', 'delete').
  179. addHidden('folder_name', $folder_name).
  180. addSubmit(_("Yes"), 'confirmed').
  181. addSubmit(_("No"), 'cancelbutton').
  182. '</p></form><br /></td></tr></table>';
  183. echo "\n</td></tr></table>\n</td></tr></table>\n\n</body></html>";
  184. sqimap_logout($imapConnection);
  185. exit;
  186. }
  187. /**
  188. * Given a folder, moves it to trash (and all subfolders of it too).
  189. */
  190. function folders_delete_do ($imapConnection, $delimiter, $folder_name)
  191. {
  192. require_once(SM_PATH . 'functions/tree.php');
  193. $boxes = sqimap_mailbox_list ($imapConnection);
  194. global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
  195. if (substr($folder_name, -1) == $delimiter) {
  196. $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
  197. } else {
  198. $folder_name_no_dm = $folder_name;
  199. }
  200. /** lets see if we CAN move folders to the trash.. otherwise,
  201. ** just delete them **/
  202. /* Courier IMAP doesn't like subfolders of Trash
  203. * If global options say we can't move it into Trash
  204. * If it's already a subfolder of trash, we'll have to delete it */
  205. if (strtolower($imap_server_type) == 'courier' ||
  206. (isset($delete_folder) && $delete_folder) ||
  207. eregi('^'.$trash_folder.'.+', $folder_name) )
  208. {
  209. $can_move_to_trash = FALSE;
  210. }
  211. /* Otherwise, check if trash folder exits and support sub-folders */
  212. else {
  213. foreach($boxes as $box) {
  214. if ($box['unformatted'] == $trash_folder) {
  215. $can_move_to_trash = !in_array('noinferiors', $box['flags']);
  216. }
  217. }
  218. }
  219. /** First create the top node in the tree **/
  220. foreach($boxes as $box) {
  221. if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
  222. $foldersTree[0]['value'] = $folder_name;
  223. $foldersTree[0]['doIHaveChildren'] = false;
  224. continue;
  225. }
  226. }
  227. /* Now create the nodes for subfolders of the parent folder
  228. You can tell that it is a subfolder by tacking the mailbox delimiter
  229. on the end of the $folder_name string, and compare to that. */
  230. foreach($boxes as $box) {
  231. if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
  232. addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
  233. }
  234. }
  235. /** Lets start removing the folders and messages **/
  236. if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
  237. walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
  238. walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
  239. } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
  240. walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
  241. }
  242. return;
  243. }
  244. /**
  245. * Given an array of folder_names, subscribes to each of them.
  246. */
  247. function folders_subscribe($imapConnection, $folder_names)
  248. {
  249. global $color;
  250. if (count($folder_names) == 0 || $folder_names[0] == '') {
  251. plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
  252. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  253. sqimap_logout($imapConnection);
  254. exit;
  255. }
  256. global $no_list_for_subscribe, $imap_server_type;
  257. if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
  258. /* Cyrus, atleast, does not typically allow subscription to
  259. * nonexistent folders (this is an optional part of IMAP),
  260. * lets catch it here and report back cleanly. */
  261. if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
  262. plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
  263. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  264. sqimap_logout($imapConnection);
  265. exit;
  266. }
  267. }
  268. foreach ( $folder_names as $folder_name ) {
  269. sqimap_subscribe ($imapConnection, $folder_name);
  270. }
  271. return;
  272. }
  273. /**
  274. * Given a list of folder names, unsubscribes from each of them.
  275. */
  276. function folders_unsubscribe($imapConnection, $folder_names)
  277. {
  278. global $color;
  279. if (count($folder_names) == 0 || $folder_names[0] == '') {
  280. plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
  281. '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
  282. sqimap_logout($imapConnection);
  283. exit;
  284. }
  285. foreach ( $folder_names as $folder_name ) {
  286. sqimap_unsubscribe ($imapConnection, $folder_name);
  287. }
  288. return;
  289. }
  290. ?>