Location: PHPKode > projects > LDAPted > ldapted/includes/Tree.php
<?php
/***************************************************************************
 *
 *                                  Tree.php
 *                              -------------------
 *
 *   begin                : Friday, Jul 5, 2002
 *   copyright            : (C) 2002 The Kabramps Team
 *   email                : hide@address.com,
 *                          hide@address.com
 *
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *   See the GNU General Public License for more details.
 *   (http://www.gnu.org/licenses/gpl.html)
 *
 ***************************************************************************/

include("includes/ListEntryFactory.php");
include("includes/XMLlist.php");

class Tree {
  
  var $dnlist;
  var $form; // It's important to know which formular is to be opened
             // when Tree.php creates the links to formular.php
  var $twig;
  var $rowcount;
  var $xml;
  var $listAttributes;
  var $baseDN;
  var $recursive;
  var $folder_rdns = array('ou', 'dc');

	function Tree ($ldapconnection, $baseDN, $filter, $recursive, $form, $twig)
	{
		$config = new Config("config.xml");

		$this->xml       = new XMLlist ( "adapted/lists/".$config->get_host_lists( $_SESSION['host'] ) );
		$this->listAttributes =   $this->xml->get_attributes();
		$this->form      = $form;
		$this->twig      = $twig;
		$this->baseDN    = $baseDN;
		$this->recursive = $recursive;
		$this->dnlist    = $this->build_dnlist( $ldapconnection, $filter );
	}
	
	function get_struct()
	{
		$return = array();
		foreach ( $this->dnlist as $entry)
		{
			if( is_array( $entry ) )
			{
				$dn_parts = explode(',', $entry['dn'] );
				$tmp = $this->get_struct_entry( array_reverse( $dn_parts ) );
				$return = array_merge_recursive( $return, $tmp );
			}
		}

		return $return;
	}

	function get_struct_entry( $dn_parts )
	{
		if( count( $dn_parts ) )
		{
			$dn_part = $dn_parts[0];
			$rdn = explode( '=', $dn_part );
			$rdn = $rdn[0];

			if( !in_array($rdn, $this->folder_rdns) )
			{
				return array( $dn_part => null );
			}
			else
			{
				array_shift( $dn_parts );
				return array( $dn_part => $this->get_struct_entry( $dn_parts) );
			}
		}
		return array();
	}

	// when $basedn is given the algo just gives a subtree
	function get_view()
	{
		$basedn = $this->baseDN; //'ou=france,dc=ez,dc=no';
		$tmpls = new TPLInterface();
		$dnObj = new dnInterpreter( $basedn );
		
		$tree = $this->get_struct();
		if (count($tree) > 0)
		{
			$path = explode( ',', $basedn );
			$path = array_reverse( $path );
			//array_pop( $path );

			$subtree = $tree;
			foreach( $path as $part )
			{
				if( $part )
				{
					$subtree = $subtree[$part];
				}
			}
			if (is_array($subtree))
			{
				return $this->parse_folder( $subtree, $basedn, true );
			}
		}
		else
		{
			return '';
		}
	}
  
	function parse_folder($tree, $subbasedn, $is_root_node = false)
	{
		global $ldap_base;
		global $options;

		$tmpls            = new TPLInterface();
		$listEntryFactory = new ListEntryFactory;

		$assign = array();
		foreach( $tree as $key => $val)
		{
			$rdn = explode('=', $key );
			$rdn = $rdn[0];
			$full_dn = $key.','.$subbasedn;
			
			$entry = $listEntryFactory->factory($this->dnlist[$full_dn],
			                                    $this->listAttributes,
			                                    $this->xml,
                                                $this->form,
                                                $rdn,
                                                $tmpls
                                               );
			$dn = new dnInterpreter( $key );

			if ( in_array( $dn->get_type($dn->get_leaf()), $this->folder_rdns ) )
			{
				if( count( $tree[$key] ) ) // skip empty folders
				{
					$subfolder = $this->parse_folder( $tree[$key], $full_dn );
					$entry->set_subcontent( $subfolder );
				}
			}

			$assign[] = array("TREEENTRY" => $entry->get_view() );
		}

		$tmpls->assign(array('id'           => $subbasedn,
		                     'treerow'      => $assign,
		                     'is_root_node' => $is_root_node
		                     ));

		return $tmpls->fetch('tree.tpl');
	}

	function build_dnlist( $ldapconnection, $filter )
	{
		//in any cases we need the OrgaUnits for a the output
		$filter = "(|(objectclass=organizationalUnit)".$filter.")";

		$result_set = $ldapconnection->search($this->baseDN, $this->xml->get_attributes() , $filter, $this->recursive,  $this->xml->get_sortAttribute() );

		foreach( $result_set as $key => $value )
		{
			$result_set[$value['dn']] = $value;
			unset ( $result_set[$key] );
		}

		// add basedn as part of the result set		
		$result_set[$this->baseDN] = array('dn' => $this->baseDN);

		return $result_set;
	}

}

?>
Return current item: LDAPted