tree.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * tree.php
  4. *
  5. * Copyright (c) 1999-2001 The SquirrelMail Development 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. * $Id$
  12. */
  13. /*****************************************************************/
  14. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  15. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  16. /*** + Base level indent should begin at left margin, as ***/
  17. /*** the require_once below. ***/
  18. /*** + All identation should consist of four space blocks ***/
  19. /*** + Tab characters are evil. ***/
  20. /*** + all comments should use "slash-star ... star-slash" ***/
  21. /*** style -- no pound characters, no slash-slash style ***/
  22. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  23. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  24. /*** + Please use ' instead of ", when possible. Note " ***/
  25. /*** should always be used in _( ) function calls. ***/
  26. /*** Thank you for your help making the SM code more readable. ***/
  27. /*****************************************************************/
  28. require_once('../functions/imap.php');
  29. /**
  30. * findParentForChild
  31. *
  32. * Recursive function to find the correct parent for a new node
  33. */
  34. function findParentForChild($value, $treeIndexToStart, $tree) {
  35. // is $value in $tree[$treeIndexToStart]['value']
  36. if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
  37. // do I have children, if not then must be a childnode of the current node
  38. if ($tree[$treeIndexToStart]['doIHaveChildren']) {
  39. // loop through each subNode checking to see if we are a subNode of one of them
  40. for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
  41. $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
  42. if ($result > -1)
  43. return $result;
  44. }
  45. // if we aren't a child of one of the subNodes, must be a child of current node
  46. return $treeIndexToStart;
  47. } else
  48. return $treeIndexToStart;
  49. } else {
  50. // we aren't a child of this node at all
  51. return -1;
  52. }
  53. }
  54. function addChildNodeToTree($comparisonValue, $value, &$tree) {
  55. $parentNode = findParentForChild($comparisonValue, 0, $tree);
  56. // create a new subNode
  57. $newNodeIndex = count($tree);
  58. $tree[$newNodeIndex]['value'] = $value;
  59. $tree[$newNodeIndex]['doIHaveChildren'] = false;
  60. if ($tree[$parentNode]['doIHaveChildren'] == false) {
  61. // make sure the parent knows it has children
  62. $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
  63. $tree[$parentNode]['doIHaveChildren'] = true;
  64. } else {
  65. $nextSubNode = count($tree[$parentNode]['subNodes']);
  66. // make sure the parent knows it has children
  67. $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
  68. }
  69. }
  70. function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
  71. global $trash_folder;
  72. if ($tree[$index]['doIHaveChildren']) {
  73. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  74. walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  75. }
  76. if ($tree[$index]['value'] != $trash_folder) {
  77. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  78. } else {
  79. $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
  80. if ($numMessages > 0) {
  81. sqimap_mailbox_select($imap_stream, $trash_folder);
  82. sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
  83. sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
  84. }
  85. }
  86. } else {
  87. if ($tree[$index]['value'] != $trash_folder) {
  88. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  89. } else {
  90. $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
  91. if ($numMessages > 0) {
  92. sqimap_mailbox_select($imap_stream, $trash_folder);
  93. sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
  94. sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
  95. }
  96. }
  97. }
  98. }
  99. function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
  100. if ($tree[$index]['doIHaveChildren']) {
  101. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  102. walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  103. }
  104. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  105. } else {
  106. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  107. }
  108. }
  109. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
  110. global $trash_folder, $delimiter;
  111. $position = strrpos($topFolderName, $delimiter) + 1;
  112. $subFolderName = substr($tree[$index]['value'], $position);
  113. if ($tree[$index]['doIHaveChildren']) {
  114. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
  115. sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  116. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
  117. if ($messageCount > 0)
  118. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
  119. for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
  120. walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
  121. } else {
  122. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
  123. sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  124. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
  125. if ($messageCount > 0)
  126. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
  127. }
  128. }
  129. function simpleWalkTreePre($index, $tree) {
  130. if ($tree[$index]['doIHaveChildren']) {
  131. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  132. simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
  133. }
  134. echo $tree[$index]['value'] . '<br>';
  135. } else {
  136. echo $tree[$index]['value'] . '<br>';
  137. }
  138. }
  139. ?>