Warning removal.
[squirrelmail.git] / functions / tree.php
CommitLineData
59177427 1<?php
245a6892 2
2d367c68 3 /**
2ba13803 4 ** tree.php
5 **
6 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 **
9 ** This code provides various string manipulation functions that are
10 ** used by the rest of the Squirrelmail code.
11 **
12 ** $Id$
2d367c68 13 */
a4351446 14
0fc2aca0 15 require_once('../functions/imap.php');
a4351446 16
41fd4ed4 17 // Recursive function to find the correct parent for a new node
a4351446 18 function findParentForChild($value, $treeIndexToStart, $tree) {
8a549df2 19 // is $value in $tree[$treeIndexToStart]['value']
20 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
41fd4ed4 21 // do I have children, if not then must be a childnode of the current node
8a549df2 22 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
41fd4ed4 23 // loop through each subNode checking to see if we are a subNode of one of them
8a549df2 24 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
25 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
8703e429 26 if ($result > -1)
a4351446 27 return $result;
28 }
41fd4ed4 29 // if we aren't a child of one of the subNodes, must be a child of current node
a4351446 30 return $treeIndexToStart;
31 } else
32 return $treeIndexToStart;
33 } else {
41fd4ed4 34 // we aren't a child of this node at all
a4351446 35 return -1;
36 }
37 }
38
abddc974 39 function addChildNodeToTree($comparisonValue, $value, &$tree) {
40 $parentNode = findParentForChild($comparisonValue, 0, $tree);
a4351446 41
42 // create a new subNode
abddc974 43 $newNodeIndex = count($tree);
8a549df2 44 $tree[$newNodeIndex]['value'] = $value;
45 $tree[$newNodeIndex]['doIHaveChildren'] = false;
a4351446 46
8a549df2 47 if ($tree[$parentNode]['doIHaveChildren'] == false) {
a4351446 48 // make sure the parent knows it has children
8a549df2 49 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
50 $tree[$parentNode]['doIHaveChildren'] = true;
a4351446 51 } else {
8a549df2 52 $nextSubNode = count($tree[$parentNode]['subNodes']);
a4351446 53 // make sure the parent knows it has children
8a549df2 54 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
a4351446 55 }
56 }
57
294bf31a 58 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
59 global $trash_folder;
8a549df2 60 if ($tree[$index]['doIHaveChildren']) {
61 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
62 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
294bf31a 63 }
8a549df2 64 if ($tree[$index]['value'] != $trash_folder) {
65 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
294bf31a 66 } else {
67 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
68 if ($numMessages > 0) {
69 sqimap_mailbox_select($imap_stream, $trash_folder);
8a549df2 70 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
8cf653ad 71 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
294bf31a 72 }
73 }
74 } else {
8a549df2 75 if ($tree[$index]['value'] != $trash_folder) {
76 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
294bf31a 77 } else {
78 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
79 if ($numMessages > 0) {
80 sqimap_mailbox_select($imap_stream, $trash_folder);
8a549df2 81 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
8cf653ad 82 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
294bf31a 83 }
84 }
85 }
86 }
87
a4351446 88 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
8a549df2 89 if ($tree[$index]['doIHaveChildren']) {
90 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
91 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
a4351446 92 }
8a549df2 93 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
a4351446 94 } else {
8a549df2 95 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
a4351446 96 }
97 }
98
525b7ae6 99 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
100 global $trash_folder, $delimiter;
a4351446 101
525b7ae6 102 $position = strrpos($topFolderName, $delimiter) + 1;
8a549df2 103 $subFolderName = substr($tree[$index]['value'], $position);
a4351446 104
8a549df2 105 if ($tree[$index]['doIHaveChildren']) {
525b7ae6 106 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
8a549df2 107 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
a4351446 108
8a549df2 109 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
a4351446 110 if ($messageCount > 0)
525b7ae6 111 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
a4351446 112
8a549df2 113 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
525b7ae6 114 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
a4351446 115 } else {
525b7ae6 116 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
8a549df2 117 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
a4351446 118
8a549df2 119 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
a4351446 120 if ($messageCount > 0)
525b7ae6 121 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
a4351446 122 }
123 }
abddc974 124
125 function simpleWalkTreePre($index, $tree) {
8a549df2 126 if ($tree[$index]['doIHaveChildren']) {
127 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
128 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
abddc974 129 }
8a549df2 130 echo $tree[$index]['value'] . '<br>';
abddc974 131 } else {
8a549df2 132 echo $tree[$index]['value'] . '<br>';
abddc974 133 }
134 }
525b7ae6 135?>