tree.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * tree.php
  4. *
  5. * Copyright (c) 1999-2002 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. * $Id$
  12. */
  13. require_once('../functions/imap.php');
  14. /**
  15. * findParentForChild
  16. *
  17. * Recursive function to find the correct parent for a new node
  18. */
  19. function findParentForChild($value, $treeIndexToStart, $tree) {
  20. // is $value in $tree[$treeIndexToStart]['value']
  21. if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
  22. // do I have children, if not then must be a childnode of the current node
  23. if ($tree[$treeIndexToStart]['doIHaveChildren']) {
  24. // loop through each subNode checking to see if we are a subNode of one of them
  25. for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
  26. $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
  27. if ($result > -1)
  28. return $result;
  29. }
  30. // if we aren't a child of one of the subNodes, must be a child of current node
  31. return $treeIndexToStart;
  32. } else
  33. return $treeIndexToStart;
  34. } else {
  35. // we aren't a child of this node at all
  36. return -1;
  37. }
  38. }
  39. function addChildNodeToTree($comparisonValue, $value, &$tree) {
  40. $parentNode = findParentForChild($comparisonValue, 0, $tree);
  41. // create a new subNode
  42. $newNodeIndex = count($tree);
  43. $tree[$newNodeIndex]['value'] = $value;
  44. $tree[$newNodeIndex]['doIHaveChildren'] = false;
  45. if ($tree[$parentNode]['doIHaveChildren'] == false) {
  46. // make sure the parent knows it has children
  47. $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
  48. $tree[$parentNode]['doIHaveChildren'] = true;
  49. } else {
  50. $nextSubNode = count($tree[$parentNode]['subNodes']);
  51. // make sure the parent knows it has children
  52. $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
  53. }
  54. }
  55. function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
  56. global $trash_folder;
  57. if ($tree[$index]['doIHaveChildren']) {
  58. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  59. walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  60. }
  61. if ($tree[$index]['value'] != $trash_folder) {
  62. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  63. } else {
  64. $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
  65. if ($numMessages > 0) {
  66. sqimap_mailbox_select($imap_stream, $trash_folder);
  67. sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
  68. sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
  69. }
  70. }
  71. } else {
  72. if ($tree[$index]['value'] != $trash_folder) {
  73. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  74. } else {
  75. $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
  76. if ($numMessages > 0) {
  77. sqimap_mailbox_select($imap_stream, $trash_folder);
  78. sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
  79. sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
  80. }
  81. }
  82. }
  83. }
  84. function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
  85. if ($tree[$index]['doIHaveChildren']) {
  86. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  87. walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  88. }
  89. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  90. } else {
  91. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  92. }
  93. }
  94. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
  95. global $trash_folder, $delimiter;
  96. $position = strrpos($topFolderName, $delimiter) + 1;
  97. $subFolderName = substr($tree[$index]['value'], $position);
  98. if ($tree[$index]['doIHaveChildren']) {
  99. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
  100. sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  101. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
  102. if ($messageCount > 0)
  103. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
  104. for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
  105. walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
  106. } else {
  107. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
  108. sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  109. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
  110. if ($messageCount > 0)
  111. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
  112. }
  113. }
  114. function simpleWalkTreePre($index, $tree) {
  115. if ($tree[$index]['doIHaveChildren']) {
  116. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  117. simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
  118. }
  119. echo $tree[$index]['value'] . '<br>';
  120. } else {
  121. echo $tree[$index]['value'] . '<br>';
  122. }
  123. }
  124. ?>