tree.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * tree.php
  4. *
  5. * Copyright (c) 1999-2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This code provides various string manipulation functions that are
  9. * used by the rest of the Squirrelmail code.
  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. if ($tree[$index]['doIHaveChildren']) {
  78. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  79. walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  80. }
  81. if ($tree[$index]['value'] != $trash_folder) {
  82. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  83. } else {
  84. $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
  85. if ($mbx_response['EXISTS'] > 0) {
  86. sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
  87. // CLOSE === EXPUNGE and UNSELECT
  88. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  89. }
  90. }
  91. } else {
  92. if ($tree[$index]['value'] != $trash_folder) {
  93. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  94. } else {
  95. $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
  96. if ($mbx_response['EXISTS'] > 0) {
  97. sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
  98. // CLOSE === EXPUNGE and UNSELECT
  99. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Recursively delete a tree of mail folders.
  106. *
  107. * @param int index the place in the tree to start, usually 0
  108. * @param stream imap_stream the IMAP connection to send commands to
  109. * @param array tree the tree to walk
  110. * @return void
  111. */
  112. function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
  113. if ($tree[$index]['doIHaveChildren']) {
  114. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  115. walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  116. }
  117. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  118. } else {
  119. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  120. }
  121. }
  122. /**
  123. * Recursively walk a tree of folders to create them under the trash folder.
  124. */
  125. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
  126. global $trash_folder, $delimiter;
  127. $position = strrpos($topFolderName, $delimiter);
  128. if ($position !== FALSE)
  129. $position++;
  130. $subFolderName = substr($tree[$index]['value'], $position);
  131. if ($tree[$index]['doIHaveChildren']) {
  132. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
  133. $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  134. $messageCount = $mbx_response['EXISTS'];
  135. if ($messageCount > 0) {
  136. sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
  137. }
  138. // after copy close the mailbox to get in unselected state
  139. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  140. for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
  141. walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
  142. } else {
  143. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
  144. $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  145. $messageCount = $mbx_response['EXISTS'];
  146. if ($messageCount > 0) {
  147. sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
  148. }
  149. // after copy close the mailbox to get in unselected state
  150. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  151. }
  152. }
  153. /**
  154. * Recursive function that outputs a tree In-Pre-Order.
  155. * @param int index the node to start (usually 0)
  156. * @param array tree the tree to walk
  157. * @return void
  158. */
  159. function simpleWalkTreePre($index, $tree) {
  160. if ($tree[$index]['doIHaveChildren']) {
  161. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  162. simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
  163. }
  164. echo $tree[$index]['value'] . '<br>';
  165. } else {
  166. echo $tree[$index]['value'] . '<br>';
  167. }
  168. }
  169. ?>