Source for file tree.php

Documentation is available at tree.php

  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-2020 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id: tree.php 14840 2020年01月07日 07:42:38Z pdontthink $
  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. if ($tree[$index]['doIHaveChildren']) {
  77. for ($j = 0; $j < count ($tree[$index]['subNodes']); $j++) {
  78. walkTreeInPreOrderEmptyTrash ($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  79. }
  80. if ($tree[$index]['value'] != $trash_folder) {
  81. sqimap_mailbox_delete ($imap_stream, $tree[$index]['value']);
  82. } else {
  83. $mbx_response = sqimap_mailbox_select ($imap_stream, $trash_folder);
  84. if ($mbx_response['EXISTS'] > 0) {
  85. sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
  86. // CLOSE === EXPUNGE and UNSELECT
  87. sqimap_run_command ($imap_stream,'CLOSE',false,$response,$message);
  88. }
  89. }
  90. } else {
  91. if ($tree[$index]['value'] != $trash_folder) {
  92. sqimap_mailbox_delete ($imap_stream, $tree[$index]['value']);
  93. } else {
  94. $mbx_response = sqimap_mailbox_select ($imap_stream, $trash_folder);
  95. if ($mbx_response['EXISTS'] > 0) {
  96. sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
  97. // CLOSE === EXPUNGE and UNSELECT
  98. sqimap_run_command ($imap_stream,'CLOSE',false,$response,$message);
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * Recursively delete a tree of mail folders.
  105. *
  106. * @param int index the place in the tree to start, usually 0
  107. * @param stream imap_stream the IMAP connection to send commands to
  108. * @param array tree the tree to walk
  109. * @return void
  110. */
  111. function walkTreeInPreOrderDeleteFolders ($index, $imap_stream, $tree) {
  112. if ($tree[$index]['doIHaveChildren']) {
  113. for ($j = 0; $j < count ($tree[$index]['subNodes']); $j++) {
  114. walkTreeInPreOrderDeleteFolders ($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  115. }
  116. sqimap_mailbox_delete ($imap_stream, $tree[$index]['value']);
  117. } else {
  118. sqimap_mailbox_delete ($imap_stream, $tree[$index]['value']);
  119. }
  120. }
  121. /**
  122. * Recursively walk a tree of folders to create them under the trash folder.
  123. */
  124. function walkTreeInPostOrderCreatingFoldersUnderTrash ($index, $imap_stream, $tree, $topFolderName) {
  125. global $trash_folder, $delimiter;
  126. $position = strrpos ($topFolderName, $delimiter);
  127. if ($position !== FALSE) {
  128. $position++;
  129. }
  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_msgs_list_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_msgs_list_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. }

Documentation generated on 2020年1月13日 04:25:24 +0100 by phpDocumentor 1.4.3

AltStyle によって変換されたページ (->オリジナル) /