tree.php 7.9 KB

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