tree.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * tree.php
  4. *
  5. * This file provides functions to walk trees of folders, for
  6. * instance to delete a whole tree.
  7. *
  8. * @copyright 1999-2025 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. require_once(SM_PATH . 'functions/imap.php');
  14. /**
  15. * Recursive function to find the correct parent for a new node.
  16. *
  17. * @param mixed value the value to find a parent for
  18. * @param int treeIndexToStart where to start the search, usually the root node (0)
  19. * @param array tree the tree to search
  20. * @return int the index of the parent
  21. */
  22. function findParentForChild($value, $treeIndexToStart, $tree) {
  23. // is $value in $tree[$treeIndexToStart]['value']
  24. if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
  25. // do I have children, if not then must be a childnode of the current node
  26. if ($tree[$treeIndexToStart]['doIHaveChildren']) {
  27. // loop through each subNode checking to see if we are a subNode of one of them
  28. for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
  29. $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
  30. if ($result > -1)
  31. return $result;
  32. }
  33. // if we aren't a child of one of the subNodes, must be a child of current node
  34. return $treeIndexToStart;
  35. } else
  36. return $treeIndexToStart;
  37. } else {
  38. // we aren't a child of this node at all
  39. return -1;
  40. }
  41. }
  42. /**
  43. * Will insert a new value into the tree, based on a given comparison value.
  44. *
  45. * @param mixed comparisonValue the value to determine where the new element should be placed.
  46. * @param mixed value the new node to insert
  47. * @param array tree the tree to insert the node in, by ref
  48. */
  49. function addChildNodeToTree($comparisonValue, $value, &$tree) {
  50. $parentNode = findParentForChild($comparisonValue, 0, $tree);
  51. // create a new subNode
  52. $newNodeIndex = count($tree);
  53. $tree[$newNodeIndex]['value'] = $value;
  54. $tree[$newNodeIndex]['doIHaveChildren'] = false;
  55. if ($tree[$parentNode]['doIHaveChildren'] == false) {
  56. // make sure the parent knows it has children
  57. $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
  58. $tree[$parentNode]['doIHaveChildren'] = true;
  59. } else {
  60. $nextSubNode = count($tree[$parentNode]['subNodes']);
  61. // make sure the parent knows it has children
  62. $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
  63. }
  64. }
  65. /**
  66. * Recursively walk the tree of trash mailboxes and delete all folders and messages
  67. *
  68. * @param int index the place in the tree to start, usually 0
  69. * @param stream imap_stream the IMAP connection to send commands to
  70. * @param array tree the tree to walk
  71. * @return void
  72. */
  73. function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
  74. global $trash_folder;
  75. walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
  76. }
  77. /**
  78. * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
  79. *
  80. * @param int index the place in the tree to start, usually 0
  81. * @param stream imap_stream the IMAP connection to send commands to
  82. * @param array tree the tree to walk
  83. * @param mailbox the name of the root folder to empty
  84. * @return void
  85. */
  86. function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) {
  87. if ($tree[$index]['doIHaveChildren']) {
  88. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  89. walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  90. }
  91. if ($tree[$index]['value'] != $mailbox) {
  92. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  93. } else {
  94. $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
  95. if ($mbx_response['EXISTS'] > 0) {
  96. sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
  97. // CLOSE === EXPUNGE and UNSELECT
  98. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  99. }
  100. }
  101. } else {
  102. if ($tree[$index]['value'] != $mailbox) {
  103. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  104. } else {
  105. $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
  106. if ($mbx_response['EXISTS'] > 0) {
  107. sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
  108. // CLOSE === EXPUNGE and UNSELECT
  109. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * Recursively delete a tree of mail folders.
  116. *
  117. * @param int index the place in the tree to start, usually 0
  118. * @param stream imap_stream the IMAP connection to send commands to
  119. * @param array tree the tree to walk
  120. * @return void
  121. */
  122. function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
  123. if ($tree[$index]['doIHaveChildren']) {
  124. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  125. walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  126. }
  127. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  128. } else {
  129. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  130. }
  131. }
  132. /**
  133. * Recursively walk a tree of folders to create them under the trash folder.
  134. */
  135. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
  136. global $trash_folder, $delimiter;
  137. $position = strrpos($topFolderName, $delimiter);
  138. if ($position !== FALSE) {
  139. $position++;
  140. }
  141. $subFolderName = substr($tree[$index]['value'], $position);
  142. if ($tree[$index]['doIHaveChildren']) {
  143. // create new trash subfolder only if it does not exist.
  144. if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
  145. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
  146. $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  147. $messageCount = $mbx_response['EXISTS'];
  148. if ($messageCount > 0) {
  149. sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
  150. }
  151. // after copy close the mailbox to get in unselected state
  152. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  153. for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
  154. walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
  155. } else {
  156. if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
  157. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
  158. $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  159. $messageCount = $mbx_response['EXISTS'];
  160. if ($messageCount > 0) {
  161. sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
  162. }
  163. // after copy close the mailbox to get in unselected state
  164. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  165. }
  166. }
  167. /**
  168. * Recursive function that outputs a tree In-Pre-Order.
  169. * @param int index the node to start (usually 0)
  170. * @param array tree the tree to walk
  171. * @return void
  172. */
  173. function simpleWalkTreePre($index, $tree) {
  174. if ($tree[$index]['doIHaveChildren']) {
  175. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  176. simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
  177. }
  178. echo $tree[$index]['value'] . '<br />';
  179. } else {
  180. echo $tree[$index]['value'] . '<br />';
  181. }
  182. }