Source for file tree.php

Documentation is available at tree.php

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

Documentation generated on Sat, 07 Oct 2006 16:33:57 +0300 by phpDocumentor 1.3.0RC6