tree.php 7.6 KB

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