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