tree.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * tree.php
  4. *
  5. * Copyright (c) 1999-2003 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(SM_PATH . '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. $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
  65. if ($mbx_response['EXISTS'] > 0) {
  66. sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
  67. sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
  68. }
  69. }
  70. } else {
  71. if ($tree[$index]['value'] != $trash_folder) {
  72. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  73. } else {
  74. $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
  75. if ($mbx_response['EXISTS'] > 0) {
  76. sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
  77. sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
  78. }
  79. }
  80. }
  81. }
  82. function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
  83. if ($tree[$index]['doIHaveChildren']) {
  84. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  85. walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  86. }
  87. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  88. } else {
  89. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  90. }
  91. }
  92. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
  93. global $trash_folder, $delimiter;
  94. $position = strrpos($topFolderName, $delimiter) + 1;
  95. $subFolderName = substr($tree[$index]['value'], $position);
  96. if ($tree[$index]['doIHaveChildren']) {
  97. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
  98. $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  99. $messageCount = $mbx_response['EXISTS'];
  100. if ($messageCount > 0)
  101. sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
  102. for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
  103. walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
  104. } else {
  105. sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
  106. $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
  107. $messageCount = $mbx_response['EXISTS'];
  108. if ($messageCount > 0)
  109. sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
  110. }
  111. }
  112. function simpleWalkTreePre($index, $tree) {
  113. if ($tree[$index]['doIHaveChildren']) {
  114. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  115. simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
  116. }
  117. echo $tree[$index]['value'] . '<br>';
  118. } else {
  119. echo $tree[$index]['value'] . '<br>';
  120. }
  121. }
  122. ?>