folder_manip.php 11 KB

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