Location: PHPKode > projects > LDAP Navigator > ldapnavigator-0.9.6/functions.php
<?php

/*
 	dump the content of a mixed variable - Debug purpose
*/
function dump( $obj )
{
	 echo "<pre>"; print_r($obj); echo "</pre>";
}

/*!
	compare php version against a given version tag eg. "4.0.1"
	return true if php is more recent then the given version
*/
function minimum_version( $vercheck ) 
{
	$minver = explode(".", $vercheck);
	$curver = explode(".", phpversion());
	if (($curver[0] < $minver[0]) || (($curver[0] = $minver[0])
		&& ($curver[1] < $minver[1]))	|| (($curver[0] = $minver[0])
		&& ($curver[1] = $minver[1])
		&& ($curver[2][0] < $minver[2][0])))
		return false;
	else
		return true;
}

 /*
   LDAP_Open
   connect and optionnaly bind to the LDAP server
   to be called when a LDAP connexion is required
  */

function LDAP_Open()
{
	global $ds, $ldapmanager;
	
	$ds = ldap_connect ( $ldapmanager['host'], $ldapmanager['port'] );
	if( ! $ds ) {
		ErrorPage( "Can not create LDAP connection", "Please check informations and that the server is up" );
	}

	/* Netscape Directory Server will return "Unable to bind to server" if $bindpw is empty */
	if( $ldapmanager['binddn'] !=  "" and $ldapmanager['bindpw'] != "" ) {

		$r = ldap_bind ($ds, $ldapmanager['binddn'], $ldapmanager['bindpw'] );
		if( ! $r ) {
			ErrorPage( "Can authentify", "Please check your logon informations" );
		}
	}
}

/*
	parse_aci
	parse an Netscape ACI string ( an access permission rule)
	decode it into an hash:
		target - the dn protected by this aci
		name - the textual name of the ACI
		version - the ACI version , eg. 3.0 on iPlanet 4
		grant - rule "way", either "allow" or "deny"
		permissions - array of permissions, for ex. array( "read", "compare" ), array( "all" )
		bindingtype - family of binding rule, either "groupdn" or "userdn"
		bindingdn - the dn of the person or group with this ACI, eg "ldap:///

	Note: returned DN fields starts with ldap:/// : bindingdn, target
	CAUTION: the attributefilter is not yet decoded

	@param acitext string textual representation of the ACI, as found in the "aci" attribute
	@param acitype string to specify what family of ACI it is. for now, only Netscape/ iPlanet ACI are supported
	@return hash ACI information
*/
function parse_aci( $acitext, $acitype = "iplanet" )
{
	$aci = array();
/* targetattr ="userpassword || telephonenumber || facsimiletelephonenumber")
	(version 3.0;
	acl "Allow self entry modification";
	allow (write)
	(userdn = "ldap:///self");)
*/

	if( preg_match( "/\(target\s*=\s*\"(.[^\"]*)\"/i", $acitext, $regs ) ) {
		$aci['target'] = $regs[1];
	}

	if( preg_match( "/version (...)\s*;\s*acl\s+\"(.[^\"]*)/i", $acitext, $regs ) ) {
		$aci['version'] = $regs[1];
		$aci['name'] = $regs[2];
	} else {
		// bad aci
		echo "ACI error: name and version not found for $acitext";
		return false;
	}

	// permissions part of ACI
	if( preg_match( "/(allow|deny)\s+\((.[^)]*)\)/i", $acitext, $regs ) ) {
		$aci['grant'] = $regs[1];
		$aci['permissions'] = explode( ",", $regs[2] );
	}
	
	// binding rule part of ACI
	if( preg_match( "/\s*(userdn|groupdn)\s*=\s*\"(.[^\"]*)\"/i", $acitext, $regs ) ) {
		$aci['bindingtype'] = $regs[1];
		$aci['bindingdn'] = $regs[2];
	}
	
	if( preg_match( "/targetattr\s*=\s*\"(.[^\"]*)\"/i", $acitext, $regs ) ) {
		$aci['targetattr'] = explode( " || ", $regs[1] );
	}
	return $aci;
}


function FormStart( $caption= "", $extrastyle="width:300px;" )
{
	echo '<div align=center><table style="border: 1px solid gray;' . $extrastyle . '">';
//	echo "<table class=params style=\"width:96%; padding: 4px;\" >\n";
	echo "<caption>$caption</caption>\n";
}


function FormLine( $cell1, $cell2 = "" )
{
	if( $cell2 == "" ) {
		echo "<tr><td class=form colspan=2>",
			is_object($cell1) ? $cell1->toString() : $cell1,
			"</td></tr>\n";
	} else {
		echo "<tr><td class=form>",
			is_object($cell1) ? $cell1->toString() : $cell1,
			"</td>",
			"<td class=form>",
			is_object($cell2) ? $cell2->toString() : $cell2,
			"</td></tr>\n";
	}
}

/*
	try to find icon that represents the entry, given its objectclasses
	@return string the icon file (without the path to it)
*/
function smartIcon( $objectclasses )
{
	global $config;

	if( ! is_array($objectclasses) )
		return $config->objectclassIcons['_default'];
	// we iter in reverse order as usually, most significant objectclass is in last position
	end($objectclasses);
	while( $oc = strtolower(current($objectclasses)) ) {
		prev($objectclasses);
		if( isset($config->objectclassIcons[$oc]) )
			return $config->objectclassIcons[$oc];
	}
	// not found, return the default icon
	return $config->objectclassIcons['_default'];
}


function ConfirmationPage( $title, $text, $nextUrl, $cancelUrl )
{
	include_once( "lib/html.php" );
	include( "./page.header.php" );
	
	echo '<form><div align=center><table cellspacing=0 style="padding: 8px; border: 1px solid gray;">';
	echo '<caption>', $title, "</caption>\n";
	FormLine( '<img src="img/warning.gif" style=\"pagdding: 8px;\" border=0>',  $text );
	$btnNext = new Button( "Continue", "document.location='$nextUrl'", "" );
	$btnCancel = new Button( "Cancel", "document.location='$cancelUrl'", "" );
	$html = $btnNext->toString() . $btnCancel->toString();
	FormLine( '&nbsp;',  $html );
	echo "</table></div></form>";
	include( "./page.footer.php" );
	exit;
}


function ErrorPage( $title, $text, $cancelUrl= "history.go(-1)" )
{
	include_once( "lib/html.php" );
	include( "./page.header.php" );

	
	if( preg_match( "/^(window|history)/i", $cancelUrl ) ) {
		$fullUrl =  $cancelUrl;
//		echo "javascript URL";
	}else {
		$fullUrl =  "document.location='$cancelUrl'";
	}

	FormStart( $title );
	FormLine( 
		'<img src="img/warning.gif" border=0>',  
		$text );	
	FormLine( 
		'&nbsp;',  
		new Button( "Back", $fullUrl, "" ) );

	echo "</table>";

//	echo "<pre>"; print_r($GLOBALS);
	
	include( "./page.footer.php" );
	exit;
}

function InfoPage( $title, $text, $backUrl = "history.go(-1)" )
{
	include_once( "lib/html.php" );
	include( "./page.header.php" );
	FormStart( $title );
	FormLine(
		'<img src="img/info.gif" border=0>',
		$text );
	FormLine( 
		'&nbsp;',  
		new Button( "Back", $backUrl, "" ) );
	 
	echo "</table>";
	include( "./page.footer.php" );
	exit;
}

function isAscii( $value )
{
	for ($k = 0; $k < strlen ($value); $k++) {
			$chstr = substr ($value, $k, 1);
			/* if there is non-print character */
			if ((ord ($chstr) < 20) or (ord ($chstr) > 126)) {
					return false;
			}
	}
	return true;
}

function Debug( $obj )
{
	global $DEBUG;

	if( ! isset($DEBUG) or $DEBUG == 0 )
		return;

	switch( gettype($obj))
	{
	case "string":
		echo "[DEBUG] $obj\n<br>"; break;
	default:
		echo "[DEBUG] <pre>"; print_r($obj ); echo "</pre>"; break;
	}
}

/**
 * build a query string from a hash
 * dont handle complex values ( hash,array)
 * @param hash array of key=>values to transform into a query string
 * @return the query string without the leading "?"
 **/
function makeQueryString( $hash )
{
	$qs = "";
	foreach( $hash as $k => $v ) {
		$qs .= urlencode($k) . "=" . urlencode($v) . "&";
	}
	return substr($qs,0,-1);

}

function HashToForm( $hash )
{
	foreach( $hash as $k => $v ) 
	{
		html::hidden( $k, $v );
	}
}

function RowN()
{
	$nargs = func_num_args();	
	echo '<tr>';

	for( $i=0; $i< $nargs; $i++ ) {
		
		echo '<td class="form">';
		MixedVarDisplay( func_get_arg($i) );
		echo "</td>\n";		
	}
	echo "</tr>\n";
	
}

function MixedVarDisplay( $var )
{
	switch( gettype($var) ) 
	{

	case "object" :
		if( method_exists( $var, "toString" ) )
			echo $var->toString();
		break;

	case "array" :
		for( $i=0; $i< count($var); $i++ ) {
			MixedVarDisplay( $var[$i] );
		}
		break;

	default :
		echo $var; 
	}
}

?>
Return current item: LDAP Navigator