tree.php 7.5 KB

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