This is no longer required as a call to checkForPrefs was added to
[squirrelmail.git] / functions / tree.php
CommitLineData
59177427 1<?php
245a6892 2
35586184 3/**
4 * tree.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 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 *
12 * $Id$
13 */
a4351446 14
35586184 15/*****************************************************************/
16/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
17/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
18/*** + Base level indent should begin at left margin, as ***/
19/*** the require_once below. ***/
20/*** + All identation should consist of four space blocks ***/
21/*** + Tab characters are evil. ***/
22/*** + all comments should use "slash-star ... star-slash" ***/
23/*** style -- no pound characters, no slash-slash style ***/
24/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
25/*** ALWAYS USE { AND } CHARACTERS!!! ***/
26/*** + Please use ' instead of ", when possible. Note " ***/
27/*** should always be used in _( ) function calls. ***/
28/*** Thank you for your help making the SM code more readable. ***/
29/*****************************************************************/
a4351446 30
35586184 31require_once('../functions/imap.php');
32
33/**
34 * findParentForChild
35 *
36 * Recursive function to find the correct parent for a new node
37 */
7350889b 38
a4351446 39 function findParentForChild($value, $treeIndexToStart, $tree) {
8a549df2 40 // is $value in $tree[$treeIndexToStart]['value']
41 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
41fd4ed4 42 // do I have children, if not then must be a childnode of the current node
8a549df2 43 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
41fd4ed4 44 // loop through each subNode checking to see if we are a subNode of one of them
8a549df2 45 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
46 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
8703e429 47 if ($result > -1)
a4351446 48 return $result;
49 }
41fd4ed4 50 // if we aren't a child of one of the subNodes, must be a child of current node
a4351446 51 return $treeIndexToStart;
52 } else
53 return $treeIndexToStart;
54 } else {
41fd4ed4 55 // we aren't a child of this node at all
a4351446 56 return -1;
57 }
58 }
59
abddc974 60 function addChildNodeToTree($comparisonValue, $value, &$tree) {
61 $parentNode = findParentForChild($comparisonValue, 0, $tree);
a4351446 62
63 // create a new subNode
abddc974 64 $newNodeIndex = count($tree);
8a549df2 65 $tree[$newNodeIndex]['value'] = $value;
66 $tree[$newNodeIndex]['doIHaveChildren'] = false;
a4351446 67
8a549df2 68 if ($tree[$parentNode]['doIHaveChildren'] == false) {
a4351446 69 // make sure the parent knows it has children
8a549df2 70 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
71 $tree[$parentNode]['doIHaveChildren'] = true;
a4351446 72 } else {
8a549df2 73 $nextSubNode = count($tree[$parentNode]['subNodes']);
a4351446 74 // make sure the parent knows it has children
8a549df2 75 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
a4351446 76 }
77 }
78
294bf31a 79 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
80 global $trash_folder;
8a549df2 81 if ($tree[$index]['doIHaveChildren']) {
82 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
83 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
294bf31a 84 }
8a549df2 85 if ($tree[$index]['value'] != $trash_folder) {
86 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
294bf31a 87 } else {
88 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
89 if ($numMessages > 0) {
90 sqimap_mailbox_select($imap_stream, $trash_folder);
8a549df2 91 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
8cf653ad 92 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
294bf31a 93 }
94 }
95 } else {
8a549df2 96 if ($tree[$index]['value'] != $trash_folder) {
97 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
294bf31a 98 } else {
99 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
100 if ($numMessages > 0) {
101 sqimap_mailbox_select($imap_stream, $trash_folder);
8a549df2 102 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
8cf653ad 103 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
294bf31a 104 }
105 }
106 }
107 }
108
a4351446 109 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
8a549df2 110 if ($tree[$index]['doIHaveChildren']) {
111 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
112 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
a4351446 113 }
8a549df2 114 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
a4351446 115 } else {
8a549df2 116 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
a4351446 117 }
118 }
119
525b7ae6 120 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
121 global $trash_folder, $delimiter;
a4351446 122
525b7ae6 123 $position = strrpos($topFolderName, $delimiter) + 1;
8a549df2 124 $subFolderName = substr($tree[$index]['value'], $position);
a4351446 125
8a549df2 126 if ($tree[$index]['doIHaveChildren']) {
525b7ae6 127 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
8a549df2 128 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
a4351446 129
8a549df2 130 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
a4351446 131 if ($messageCount > 0)
525b7ae6 132 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
a4351446 133
8a549df2 134 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
525b7ae6 135 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
a4351446 136 } else {
525b7ae6 137 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
8a549df2 138 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
a4351446 139
8a549df2 140 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
a4351446 141 if ($messageCount > 0)
525b7ae6 142 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
a4351446 143 }
144 }
abddc974 145
146 function simpleWalkTreePre($index, $tree) {
8a549df2 147 if ($tree[$index]['doIHaveChildren']) {
148 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
149 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
abddc974 150 }
8a549df2 151 echo $tree[$index]['value'] . '<br>';
abddc974 152 } else {
8a549df2 153 echo $tree[$index]['value'] . '<br>';
abddc974 154 }
155 }
525b7ae6 156?>