Location: PHPKode > projects > LDAP User Management System > lums-0.6/ws/portalinterface.functions.php
<?php

$base_path="/base/path/here";
$log_file = "/logs/web_services/lums.ws.log";
$log = fopen($log_file,'a');
$debug = 1;
#$bind_user = $_SERVER['REMOTE_USER'];
#$bind_pass = $_SERVER['PHP_AUTH_PW'];
$client_ip = $_SERVER['REMOTE_ADDR'];
$F_BASE_DN = 'ou=people,dc=company,dc=com';

	
    include($base_path."/main.php");

function CreateParent(
$StudentUserName, $ParentSurname, $ParentName, $ParentEmail,
$ParentPhone, $ParentUsername, $ParentPassword)
{
	global $debug;
	global $log;
	global $F_BASE_DN;
	global $bind_user;
	global $bind_pass;
	global $client_ip;

	#
	# Basic checks on arguments
	#
	if ($log)
		fwrite($log,date('r') . " sPortal WS: CreateParent(Client=$client_ip): Bind Username='$bind_user',ParentUsername='$ParentUsername'\n");
	#
	if ($debug){
		print "CreateParent(StudentUserName=$StudentUserName,ParentSurname=$ParentSurname,ParentName=$ParentName<br>\n";
		print "ParentEmail=$ParentEmail,ParentPhone=$ParentPhone,ParentUsername=$ParentUsername<br>\n";
		print "ParentPassword=$ParentPassword)<br>\n";
	}
	if ($StudentUserName == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Student username is not available');
	}
	if ($ParentSurname == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Parent surname is not available');
	}
	if ($ParentName == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Parent name is not available');
	}
	if ($ParentEmail == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Parent email is not available');
	}
	if ($ParentPhone == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Parent telephone number is not available');
	}
	if ($ParentUsername == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Parent username is not available');
	}
	if ($ParentPassword == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Parent password is not available');
	}
	$attrs_array = array('umdobject');

	#
	# Check if username already exists
	#
	if ($debug)
		print "CreateParent(): LUMS_ldap_search(,,base=$F_BASE_DN,sub,uid=$ParentUsername)<br>\n";
	$info = LUMS_ldap_search('','',$F_BASE_DN,'sub','uid='.$ParentUsername,$attrs_array);
	if ($info){
		if (is_array($info))
			return array('Status' => '0', 'ErrorMessage' => 'Parent username is already in use');
		else
			return array('Satus' => '0', 'ErrorMessage' => "Search failed: $info");
	}
	
	#
	# Check if student code exists
	#
	if ($debug)
		print "CreateParent(): LUMS_ldap_search(,,base=$F_BASE_DN,sub,uid=$StudentUserName)<br>\n";
	$info = LUMS_ldap_search('','',$F_BASE_DN,'sub','uid='.$StudentUserName,$attrs_array);
	if (!is_array($info)){
		if (!$info)
			return array('Status' => '0', 'ErrorMessage' => 'Student username was not found');
		else
			return array('Satus' => '0', 'ErrorMessage' => "Search failed: $info");
	}
	if ($info[0]['umdobject'][0] != 'student'){
		return array('Status' => '0', 'ErrorMessage' => 'Student username does not correspond to a student');
	}
	$student_dn = $info[0]['dn'];


	#
	# Check Password strength
	#
	# TODO

	#
	# Create Parent entry
	#

	$parent_dn = 'uid=' . $ParentUsername . ',' . $F_BASE_DN;

	$new_info['sn']=$ParentSurname;
	$new_info['givenname']=$ParentName;
	$new_info['uid']=$ParentUsername;
	$new_info['userPassword']=$ParentPassword;
	$new_info['cn']=$ParentName . " " . $ParentSurname;
	$new_info['gsnMyStudents'] = $student_dn;
	$new_info['mail'] = $ParentEmail;
	$new_info['telephonenumber'] = $ParentPhone;

	$r=LUMS_ldap_add_entry('','','parent',$parent_dn,$new_info);

	if (!$r)
		return array('Status' => '1', 'ErrorMessage' => 'User created');
	else
		return array('Status' => '1', 'ErrorMessage' => "User creation failed: $r");

}

function GetPasswordParent(
$ParentUsername)
{
	global $log;
	global $debug;
	global $F_BASE_DN;
	global $bind_user;
	global $bind_pass;
	global $client_ip;

	if ($log)
		fwrite($log,date('r') . " sPortal WS: GetPasswordParent(Client=$client_ip): Bind Username='$bind_user',ParentUsername='$ParentUsername'\n");
	if ($debug)
		print "GetPasswordParent(ParentUsername=$ParentUsername,ParentEmail=$ParentEmail)<br>\n";
	#
	# Basic checks on arguments
	#
	if ($ParentUsername == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Parent username is not available', ParentPassword => '');
	}

	#
	# Check is username exists and is parent
	#
	$attrs_array = array('umdobject');
	if ($debug)
		print "GetPasswordParent(): LUMS_ldap_search(,,base=$F_BASE_DN,sub,uid=$ParentUsername)<br>\n";
	$info = LUMS_ldap_search('','',$F_BASE_DN,'sub','uid='.$ParentUsername,$attrs_array);
	if (!is_array($info)){
		if (!$info)
			return array('Status' => '0', 'ErrorMessage' => 'Parent username was not found', 'ParentPassword' => '');
		else
			return array('Satus' => '0', 'ErrorMessage' => "Search failed: $info", 'ParentPassword' => '');
	}
	if ($debug){
		print "GetPasswordParent(): INFO=<br>\n";
		print_r($info);
	}
	if ($info[0]['umdobject'][0] != 'parent'){
		return array('Status' => '0', 'ErrorMessage' => 'Username does not correspond to a parent object', 'ParentPassword' => '');
	}

	#
	# Reset user password
	#
	$newpass = rand(0,99) . $ParentUsername . rand(0,999);
	if ($debug)
		print "GetPasswordParent(): LUMS_change_password(,,dn=$info[0][dn],newpass=$newpass)<br>\n";

	$err = LUMS_ldap_change_password('','',$info[0]['dn'],$newpass);

	if ($err != ''){
		return array('Status' => '0', 'ErrorMessage' => "Password could not be changed: $err", 'ParentPassword' => '');
	}

	return array('Status' => '1', 'ErrorMessage' => "Password changed", 'ParentPassword' => $newpass);
}

function UpdateObject(
$ObjectType, $ObjectUsername, $ObjectSurname, $ObjectName,
$ObjectEmail, $ObjectTelephone)
{
	global $log;
	global $debug;
	global $F_BASE_DN;
	global $bind_user;
	global $bind_pass;
	global $client_ip;

	if ($log)
		fwrite($log,date('r') . " sPortal WS: UpdateObject(Client=$client_ip): Bind Username='$bind_user',ParentUsername='$ObjectUsername'\n");
	if ($debug){
		print "UpdateObject(ObjectType=$ObjectType,ObjectUsername=$ObjectUsername,ObjectSurname=$ObjectSurname<br>\n";
		print "ObjectName=$ObjectName,$ObjectEmail=$ObjectEmail,ObjectTelephone=$ObjectTelephone)<br>\n";
	}
	#
	# Basic checks on arguments
	#
	if ($ObjectType == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object type is not available');
	}
	if ($ObjectUsername == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object username is not available');
	}
	if ($ObjectSurname == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object surname is not available');
	}
	if ($ObjectName == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object name is not available');
	}
	if ($ObjectEmail == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object email is not available');
	}
	if ($ObjectTelephone == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object telephone number is not available');
	}


	#
	# Check that username exists
	#
	$attrs_array = array('umdobject');
	if ($debug)
		print "UpdateObject(): LUMS_ldap_search(,,base=$F_BASE_DN,sub,uid=$ObjectUsername)<br>\n";
	$info = LUMS_ldap_search('','',$F_BASE_DN,'sub','uid='.$ObjectUsername,$attrs_array);
	if (!is_array($info)){
		if (!$info)
			return array('Status' => '0', 'ErrorMessage' => 'Object was not found');
		else
			return array('Satus' => '0', 'ErrorMessage' => "Search failed: $info");	
	}

	if ($debug){
		print "UpdateObject(): INFO=<br>\n";
		print_r($info);
	}

	#
	# We don't allow changes to email/name/surname for anything but 'parent'
	#

	$change_info['telephonenumber'] = $ObjectTelephone;
	if ($ObjectType == 'parent'){
		$change_info['mail'] = $ObjectEmail;
		$change_info['givenname'] = $ObjectName;
		$change_info['sn'] = $ObjectSurname;
	}

	if ($debug){
		print "UpdateObject(): CHANGE_INFO=<br>\n";
		print_r($change_info);
	}

	$r = LUMS_ldap_modify_entry('','',$ObjectType,$info[0]['dn'],$change_info);

	if (!$r)
		return array('Status' => '1', 'ErrorMessage' => 'Object changed');
	else
		return array('Status' => '0', 'ErrorMessage' => "Change failed: $r");
}

function DeleteObject (
$ObjectType, $ObjectUserName)
{
	global $log;
	global $debug;
	global $F_BASE_DN;
	global $bind_user;
	global $bind_pass;
	global $client_ip;

	if ($debug)
		print "DeleteObject(ObjectType = $ObjectType, ObjectUsername = $ObjectUserName)<br>\n";
	if ($log)
		fwrite($log,date('r') . " sPortal WS: DeleteObject(Client=$client_ip): Bind Username='$bind_user',Username='$ObjectUserName'\n");

	#
	# Basic checks on arguments
	#
	if ($ObjectType == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object type is not available');
	}
	if ($ObjectUserName == ''){
		return array('Status' => '0', 'ErrorMessage' => 'Object username is not available');
	}

	#
	# Check that object exists
	#
	$attrs_array = array('uid','umdobject');
	if ($debug)
		print "DeleteObject: LUMS_ldap_search(,,base=$F_BASE_DN,sub,uid=$ObjectUserName)<br>\n";
	$info = LUMS_ldap_search('','',$F_BASE_DN,'sub','uid='.$ObjectUserName,$attrs_array);
	if (!is_array($info)){
		if (!$info)
			return array('Status' => '0', 'ErrorMessage' => 'Object was not found');
		else
			return array('Status' => '0', 'ErrorMessage' => "Search failed: $info");
	}
	if ($info[0]['umdobject'][0] != 'parent'){
		return array('Status' => '0', 'ErrorMessage' => 'Account is not of parent type');
	}
	if ($debug){
		print "DeleteObject: INFO=<br>\n";
		print_r($info);
	}

	if ($debug)
		print "DeleteObject: LUMS_ldap_delete_entry(,,ObjectType=$ObjectType,ObjectUserName=$ObjectUserName)<br>\n";
		
	$r = LUMS_ldap_delete_entry('','',$ObjectType,'uid=' . $ObjectUserName . ',' . $F_BASE_DN);

	if (!$r)
		return array('Status' => '1', 'ErrorMessage' => 'Object deleted');
	else
		return array('Status' => '0', 'ErrorMessage' => "Deletion failed: $r");
}
?>
Return current item: LDAP User Management System