folder_manip.php 12 KB

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