tree.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?
  2. $tree_php = true;
  3. if (!isset($imap_php))
  4. include("../functions/imap.php");
  5. // Recursive function to find the correct parent for a new node
  6. function findParentForChild($value, $treeIndexToStart, $tree) {
  7. // is $value in $tree[$treeIndexToStart]["value"]
  8. if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]["value"]))) {
  9. // do I have children, if not then must be a childnode of the current node
  10. if ($tree[$treeIndexToStart]["doIHaveChildren"]) {
  11. // loop through each subNode checking to see if we are a subNode of one of them
  12. for ($i=0;$i< count($tree[$treeIndexToStart]["subNodes"]);$i++) {
  13. $result = findParentForChild($value, $tree[$treeIndexToStart]["subNodes"][$i], $tree);
  14. if ($result > -1)
  15. return $result;
  16. }
  17. // if we aren't a child of one of the subNodes, must be a child of current node
  18. return $treeIndexToStart;
  19. } else
  20. return $treeIndexToStart;
  21. } else {
  22. // we aren't a child of this node at all
  23. return -1;
  24. }
  25. }
  26. function addChildNodeToTree($comparisonValue, $value, &$tree) {
  27. $parentNode = findParentForChild($comparisonValue, 0, $tree);
  28. // create a new subNode
  29. $newNodeIndex = count($tree);
  30. $tree[$newNodeIndex]["value"] = $value;
  31. $tree[$newNodeIndex]["doIHaveChildren"] = false;
  32. if ($tree[$parentNode]["doIHaveChildren"] == false) {
  33. // make sure the parent knows it has children
  34. $tree[$parentNode]["subNodes"][0] = $newNodeIndex;
  35. $tree[$parentNode]["doIHaveChildren"] = true;
  36. } else {
  37. $nextSubNode = count($tree[$parentNode]["subNodes"]);
  38. // make sure the parent knows it has children
  39. $tree[$parentNode]["subNodes"][$nextSubNode] = $newNodeIndex;
  40. }
  41. }
  42. function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
  43. if ($tree[$index]["doIHaveChildren"]) {
  44. for ($j = 0; $j < count($tree[$index]["subNodes"]); $j++) {
  45. walkTreeInPreOrderDeleteFolders($tree[$index]["subNodes"][$j], $imap_stream, $tree);
  46. }
  47. sqimap_mailbox_delete($imap_stream, $tree[$index]["value"]);
  48. } else {
  49. sqimap_mailbox_delete($imap_stream, $tree[$index]["value"]);
  50. }
  51. }
  52. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $dm, $topFolderName) {
  53. global $trash_folder;
  54. $position = strrpos($topFolderName, $dm) + 1;
  55. $subFolderName = substr($tree[$index]["value"], $position);
  56. if ($tree[$index]["doIHaveChildren"]) {
  57. sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, "");
  58. sqimap_mailbox_select($imap_stream, $tree[$index]["value"]);
  59. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]["value"]);
  60. if ($messageCount > 0)
  61. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName);
  62. for ($j = 0;$j < count($tree[$index]["subNodes"]); $j++)
  63. walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]["subNodes"][$j], $imap_stream, $tree, $dm, $topFolderName);
  64. } else {
  65. sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, "");
  66. sqimap_mailbox_select($imap_stream, $tree[$index]["value"]);
  67. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]["value"]);
  68. if ($messageCount > 0)
  69. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName);
  70. }
  71. }
  72. function simpleWalkTreePre($index, $tree) {
  73. if ($tree[$index]["doIHaveChildren"]) {
  74. for ($j = 0; $j < count($tree[$index]["subNodes"]); $j++) {
  75. simpleWalkTreePre($tree[$index]["subNodes"][$j], $tree);
  76. }
  77. echo $tree[$index]["value"] . "<br>";
  78. } else {
  79. echo $tree[$index]["value"] . "<br>";
  80. }
  81. }
  82. ?>