Add "Security:" with relevant changelog items and add CVE id's.
[squirrelmail.git] / functions / tree.php
CommitLineData
59177427 1<?php
245a6892 2
35586184 3/**
4 * tree.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 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 *
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
35586184 14 */
a4351446 15
d6c32258 16/** Clearly, this needs the IMAP functions.. */
b68edc75 17require_once(SM_PATH . 'functions/imap.php');
35586184 18
19/**
8b096f0a 20 * Recursive function to find the correct parent for a new node.
21 *
22 * @param mixed value the value to find a parent for
23 * @param int treeIndexToStart where to start the search, usually the root node (0)
24 * @param array tree the tree to search
25 * @return int the index of the parent
35586184 26 */
65c3ec94 27function findParentForChild($value, $treeIndexToStart, $tree) {
28 // is $value in $tree[$treeIndexToStart]['value']
29 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
30 // do I have children, if not then must be a childnode of the current node
31 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
41fd4ed4 32 // loop through each subNode checking to see if we are a subNode of one of them
8a549df2 33 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
65c3ec94 34 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
35 if ($result > -1)
36 return $result;
a4351446 37 }
41fd4ed4 38 // if we aren't a child of one of the subNodes, must be a child of current node
a4351446 39 return $treeIndexToStart;
65c3ec94 40 } else
a4351446 41 return $treeIndexToStart;
65c3ec94 42 } else {
43 // we aren't a child of this node at all
44 return -1;
45 }
46}
47
8b096f0a 48/**
49 * Will insert a new value into the tree, based on a given comparison value.
50 *
51 * @param mixed comparisonValue the value to determine where the new element should be placed.
52 * @param mixed value the new node to insert
53 * @param array tree the tree to insert the node in, by ref
54 */
65c3ec94 55function addChildNodeToTree($comparisonValue, $value, &$tree) {
56 $parentNode = findParentForChild($comparisonValue, 0, $tree);
57
58 // create a new subNode
59 $newNodeIndex = count($tree);
60 $tree[$newNodeIndex]['value'] = $value;
61 $tree[$newNodeIndex]['doIHaveChildren'] = false;
62
63 if ($tree[$parentNode]['doIHaveChildren'] == false) {
64 // make sure the parent knows it has children
65 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
66 $tree[$parentNode]['doIHaveChildren'] = true;
67 } else {
68 $nextSubNode = count($tree[$parentNode]['subNodes']);
69 // make sure the parent knows it has children
70 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
71 }
72}
73
8b096f0a 74/**
75 * Recursively walk the tree of trash mailboxes and delete all folders and messages
76 *
77 * @param int index the place in the tree to start, usually 0
78 * @param stream imap_stream the IMAP connection to send commands to
79 * @param array tree the tree to walk
80 * @return void
81 */
65c3ec94 82function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
83 global $trash_folder;
84 if ($tree[$index]['doIHaveChildren']) {
85 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 86 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
65c3ec94 87 }
88 if ($tree[$index]['value'] != $trash_folder) {
8a549df2 89 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
65c3ec94 90 } else {
b44ee3f0 91 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
eaf1a312 92 if ($mbx_response['EXISTS'] > 0) {
b44ee3f0 93 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
eaf1a312 94 // CLOSE === EXPUNGE and UNSELECT
95 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
294bf31a 96 }
65c3ec94 97 }
98 } else {
99 if ($tree[$index]['value'] != $trash_folder) {
8a549df2 100 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
65c3ec94 101 } else {
eaf1a312 102 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
103 if ($mbx_response['EXISTS'] > 0) {
104 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
105 // CLOSE === EXPUNGE and UNSELECT
106 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
294bf31a 107 }
65c3ec94 108 }
109 }
110}
111
b44ee3f0 112
8b096f0a 113/**
114 * Recursively delete a tree of mail folders.
115 *
116 * @param int index the place in the tree to start, usually 0
117 * @param stream imap_stream the IMAP connection to send commands to
118 * @param array tree the tree to walk
119 * @return void
120 */
65c3ec94 121function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
122 if ($tree[$index]['doIHaveChildren']) {
123 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 124 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
65c3ec94 125 }
126 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
127 } else {
128 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
129 }
130}
131
8b096f0a 132/**
133 * Recursively walk a tree of folders to create them under the trash folder.
134 */
65c3ec94 135function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
136 global $trash_folder, $delimiter;
137
4bce71a5 138 $position = strrpos($topFolderName, $delimiter);
e521a833 139 if ($position !== FALSE) {
4bce71a5 140 $position++;
e521a833 141 }
65c3ec94 142 $subFolderName = substr($tree[$index]['value'], $position);
143
144 if ($tree[$index]['doIHaveChildren']) {
145 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
62f7daa5 146 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
eaf1a312 147 $messageCount = $mbx_response['EXISTS'];
148 if ($messageCount > 0) {
b44ee3f0 149 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
eaf1a312 150 }
151 // after copy close the mailbox to get in unselected state
152 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
65c3ec94 153 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
525b7ae6 154 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
65c3ec94 155 } else {
156 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
b44ee3f0 157 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
158 $messageCount = $mbx_response['EXISTS'];
eaf1a312 159 if ($messageCount > 0) {
b44ee3f0 160 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
eaf1a312 161 }
162 // after copy close the mailbox to get in unselected state
163 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
65c3ec94 164 }
165}
abddc974 166
8b096f0a 167/**
168 * Recursive function that outputs a tree In-Pre-Order.
169 * @param int index the node to start (usually 0)
170 * @param array tree the tree to walk
171 * @return void
172 */
65c3ec94 173function simpleWalkTreePre($index, $tree) {
174 if ($tree[$index]['doIHaveChildren']) {
175 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 176 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
65c3ec94 177 }
6fd95361 178 echo $tree[$index]['value'] . '<br />';
65c3ec94 179 } else {
6fd95361 180 echo $tree[$index]['value'] . '<br />';
65c3ec94 181 }
182}
e521a833 183?>