Location: PHPKode > projects > LDAPted > ldapted/formular.php
<?php
/***************************************************************************
 *
 *                                  formular.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/session.php");
include("includes/TPLInterface.php");
include("includes/dnInterpreter.php");
include("includes/XMLinterface.php");
include("includes/Config.php");
include("includes/LDAPinterface.php");
include("includes/ControlFactory.php");
#include("includes/Notify.php");

$formular = $_REQUEST['form'];
$dn       = $_REQUEST['dn'];

$status  = gettext("READY.");

$config  = new Config("config.xml");
$options = $config->get_options();
$path    = $options["forms"].$config->get_host_form_path($host); //unbedingt hier?

$ldap    = new LDAPinterface($host,$bind_dn,$pw);
$tmpls   = new TPLInterface();
$xml =     new XMLinterface($path.$config->get_host_form_file($host,$formular));

$attributes    = $xml->get_attributes();
$layout_options= $xml->get_layout_options();

#if ( $xml->notify() ) $notify = new Notify($xml,$ldap,$dn);

$controls = array();
$controlFactory = new ControlFactory;
foreach ($attributes as $attribute )
{
	$controls[$attribute] = $controlFactory->factory( $xml->get_type( $attribute ) );
	$controls[$attribute]->init( $dn, $attribute, $xml, $language );
}

$action      = $_REQUEST['edit'] ? 'edit' : 'save';
$errorOnSave = false;

if ($_REQUEST['save'])
{
	foreach ($attributes as $attribute )
	{
		$controls[$attribute]->set_value( $controls[$attribute]->html2LDAPvalue( $HTTP_POST_VARS[$attribute], null ) );
	}
	
	$input_ok = check_input( &$controls, $dn );

	if( !$input_ok )
	{
		// handle input
		$errorOnSave = true;
	}
	else
	{
		//modifytimestamp added to recognize write conflicts
		$modify['modifytimestamp'] = array($HTTP_POST_VARS['modifytimestamp']);

		$success = save_entry( $controls, $dn );
		if( !$success )
		{
			// something went wrong while saving
			$errorOnSave = true;
		}
		$ldapEntry = $modify; // changes in the form won't get lost
	}
}

if( $action == 'edit' || $errorOnSave || !$dn )
{
	$action = 'save';
}
else
{
	$action = 'edit';	
}

if( !$errorOnSave && $dn )
{
	// read ldap entry
	$result = $ldap->search( $dn, $attributes, null, 1);
	$ldapEntry = $result[0];
	
	// and set the values to the controls
	foreach( $attributes as $attribute )
	{
		$controls[$attribute]->set_value( $ldapEntry[ $attribute ] );
	}
}
 
if ( $layout_options['template'] != "" )
{
	$layouttemplate = $options['forms'].$config->get_host_form_path($host).$layout_options['template'];
}
else
{
	$layouttemplate = "formularbody.tpl";
}

$tmpls->assign( array('controls' => $controls,
                      'action'   => $action,
                     ));
$formbody = $tmpls->fetch($layouttemplate);

$jsfiles = get_jsfiles( $controls );


$tmpls->assign(array('jsfiles'          =>  $jsfiles,
                     "FORMULARBODY"    => $formbody,
                     "WIDTH"           => $layout_options['width'],
                     "HEIGHT"          => $layout_options['height'],
                     "PAGETITLE"       => "LDAPted: Details for ".$dn,
                     'STATUS'          => $status,
                     "MODE"            => $mode,
//		     "CAPTION"         => $dnInterp->get_label($dnInterp->get_leaf()),
                     "ERRORMESSAGE"    => $errormessage,
                     "action_label"    => $action,
                     "RESET"           => gettext("reset"),
                     "BACKTOBROWSER"   => gettext("close window"),
                     "DN"              => $dn,
                     "MODIFYTIMESTAMP" => $modifytimestamp,
                     'skin_folder'     => $tmpls->template_dir
             ));

$tmpls->display('formular.tpl');



function save_entry( $controls, &$dn )
{	
	global $xml;
	global $ldap;

	$ldap_entry = array();
	$ldap_entry['objectclass'] = $xml->get_objectClasses();
	
	foreach( $controls as $control )
	{
		$key    = $control->get_attribute();
		$values = $control->get_value();
		if( $values )
		{
			if( $values[0] == null || ( isset( $values[0] ) && $values[0] == '' ) )
			{
				$values = array();			
			}
			$ldap_entry[ $key ] = $values;
		}
	}

	if( $dn )
	{
		$success = $ldap->modify( $dn, $ldap_entry );
	}
	else
	{
		$basedn = $_REQUEST['basedn'];		
		$from_options = $xml->get_options();
		$rdn = $from_options['rdn'];
		$rdn_control = $controls[ $rdn ];
		
		if( !$rdn_control )
		{
			die('could not get a control object to construct dn');
		}
		$rdn_value = $rdn_control->get_value();
		$rdn_value = $rdn_value[0];
		
		if( !$rdn_value )
		{
			die('empty rdn value -> cannot construct dn');
		}
		$dn = $rdn.'='.$rdn_value.','.$basedn;

		$success = $ldap->add( $dn, $ldap_entry );
	}
	
	if( !$success )
	{
		//for ($i=0; $i < count($attributes); $i++)
		{
			//$control[$attributes[$i]]->add_error_messages($ldap->get_error_attribs($attributes[$i]));
		}
		//$errormessage = $ldap->get_unresolvable_attrib_errors($attributes);
		print_r( $ldap );
	}
	
	return $success;
}

function get_jsfiles( $controls )
{

	$jsfiles = array( 'templates/std/formular.js' );
	
	foreach( $controls as $control )
	{
		if( is_array( $control_jsfiles = $control->get_jsfiles() ) )
		{
			foreach( $control_jsfiles as $file )
			{
				if( !in_array( $control_jsfiles, $jsfiles ) )
				{ 
					$jsfiles[] = $file;
				}
			}
		}
	}
	return $jsfiles;
}

function check_input( $controls )
{
	$result = true;
	
	foreach( $controls as $control )
	{

		if( $control->required  )
		{
			$value = $control->get_value();
			if( !$value || ( count($value) == 1 && !$value[0] ) ) // kinda strange
			{
				$result = false;
				break;
			}
		}
	}
	
	return $result;
}
?>
Return current item: LDAPted