Location: PHPKode > scripts > userManager > usermanager/users.php
<?

/*


	This class written by Mike Wharton, and is intended for PHP beginners.
	
	This class is a simple user class for basic user functionality not requiring a database.
	
	There are multiple versions of functions because php 5 and php 4 use different sets of functions for managing XML files.
	
	Either delete the funcions you dont need or just ignore them.
		
	User Info is stored in a file called users.xml, this file needs to be created by using the createFileDOM/createFilDOMXML functions.
	
	!!!!!!!!!!!!!!REMEMBER TO PROTECT THE XML WITH .HTACCESS!!!!!!!!!!!!!!!!!!	
	
	Enjoy.

*/

class userManager {

	var $m_xmlFileName;
	var $mode;
	var $salt;
	
function __construct(){
	
	$this->salt = "your_pass_salt";
	
	if(func_num_args()>=1){
		$this->m_xmlFileName=func_get_arg(0);
	}
	
	if(func_num_args()>=2){	
		$this->mode=func_get_arg(1);	
	}
	
}	


////////////////////////////GROUP FUNCTIONS//////////////////////////
function AddUserGroupDOMXML($fileName,$groupName,$access){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
	//open document
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	//get root node
	if(!$users=$doc->first_child()){
		die("Error- Could not find document child- <users>");		
	}
	//create new element
	$usergroup = $doc->create_element("usergroup");
	//set attributes
	$usergroup->set_attribute("name",$groupName);	
	$usergroup->set_attribute("access",$access);
	//append to 'users' root node
	$users->append_child($usergroup);
	
	//save out xml file
	$doc->dump_file($fileName);
}

function AddUserGroupDOM($fileName,$groupName,$access){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	//get root node
	if(!$users=$doc->firstChild){
		die("Error- Could not find document child- <users>");		
	}
	//create new element
	$usergroup = $doc->createElement("usergroup");
	//set attributes
	$usergroup->setAttribute("name",$groupName);	
	$usergroup->setAttribute("access",$access);
	//append to 'users' root node
	$users->appendChild($usergroup);
	
	//save out xml file
	$doc->save($fileName);
}
function ChangeGroupAccessDOMXML($fileName,$groupName,$access){//Warning, if more than one group of the same name exist, all will be changed
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}	
	$usergroups = $doc->get_elements_by_tagname( "usergroup" );
	foreach( $usergroups as $usergroup ){
		
		$name=$usergroup->get_attribute("name");
		if($name==$groupName)
		{
			$usergroup->set_attribute("access",$access);			
		}			
	}
	
	if(!$doc->dump_file($fileName))
	return false;	
}

function ChangeGroupAccessDOM($fileName,$groupName,$access){//Warning, if more than one group of the same name exist, all will be changed
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	$usergroups = $doc->getElementsByTagName( "usergroup" );
	foreach( $usergroups as $usergroup ){
		
		$name=$usergroup->getAttribute("name");
		if($name==$groupName)
		{
			$usergroup->setAttribute("access",$access);			
		}			
	}
	
	if(!$doc->save($fileName))
	return false;	
}

function ChangeGroupNameDOMXML($fileName,$groupName,$newname){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}	
	
	$usergroups = $doc->get_elements_by_tagname( "usergroup" );
	foreach( $usergroups as $usergroup ){
		
		$name=$usergroup->get_attribute("name");
		if($name==$groupName)
		{
			$usergroup->set_attribute("name",$newname);			
		}			
	}
	if(!$doc->dump_file($fileName)){
		die("Error- could not write $fileName");	
	}
		
}

function ChangeGroupNameDOM($fileName,$groupName,$newname){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	$usergroups = $doc->getElementsByTagName( "usergroup" );
	foreach( $usergroups as $usergroup ){
		
		$name=$usergroup->getAttribute("name");
		if($name==$groupName)
		{
			$usergroup->setAttribute("name",$newname);			
		}			
	}
	if(!$doc->save($fileName)){
		die("Error- could not write $fileName");	
	}
		
}

function RemoveGroupDOMXML($fileName,$groupName){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	
	//open document
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}	
	
	//get usergroups
	$usergroups = $doc->get_elements_by_tagname( "usergroup" );	
	foreach( $usergroups as $usergroup ){
		//for each group get name
		$name=$usergroup->get_attribute("name");
		if($name==$groupName)
		{	//remove group
			$parent=$usergroup->parent_node();
			$parent->remove_child($usergroup);
		}			
	}
	
	if(!$doc->dump_file($fileName))
	return false;		
}

function RemoveGroupDOM($fileName,$groupName){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	//get usergroups
	$usergroups = $doc->getElementsByTagName( "usergroup" );	
	foreach( $usergroups as $usergroup ){
		//for each group get name
		$name=$usergroup->getAttribute("name");
		if($name==$groupName)
		{	//remove group
			$usergroup->parentNode->removeChild($usergroup);
		}			
	}
	
	if(!$doc->save($fileName))
	return false;		
}


////////////////////////////FILE FUNCTIONS///////////////////////////
function CreateFileDOMXML($path,$fileName){

	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	else
		$fileName = $path.$fileName;		
	
	// Create a new document
	$doc = domxml_new_doc( "1.0" );
	
	// Create a root element
	$root_node = $doc->create_element( "users" );
	
	// Append it to the document
	$doc->append_child( $root_node );
	
	// Dump the document to the file
	$doc->dump_file( $fileName ); 		
}

function CreateFileDOM($path,$fileName){

	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	else
		$fileName = $path.$fileName;		
	
	// Create a new document
	$doc = new DOMDocument('1.0', 'iso-8859-1');
	
	$doc->formatOutput = true;
	
	// Create a root element
	$root_node = $doc->createElement( "users" );
	
	// Append it to the document
	$doc->appendChild( $root_node );
	
	// Dump the document to the file
	$doc->save( $fileName ); 		
}

function SetXmlFileName($fileName){
		$this->m_xmlFileName = $fileName;	
}

////////////////////////////USER FUNCTIONS///////////////////////////

function AddUserDOMXML($fileName,$groupName,$username,$password,$email,$access){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	//open document
	if(!$doc = domxml_open_file($fileName)) {
	   	return false;
	}
	
	
	//first, find if any users with the same name exists
	
	$users=$doc->get_elements_by_tagname( "user" );
	foreach( $users as $user ){
		$name=$user->get_attribute("username");
		if($name==$username){
			return "FALSE_USER_EXISTS";			
		}
	}
	
	$parent_node=false;
	//if we have usergroups
	if($groupName){
		$usergroups = $doc->get_elements_by_tagname( "usergroup" );
		foreach( $usergroups as $usergroup ){
				$name=$usergroup->get_attribute("name");
				if($name==$groupName)
				{	//this is our parent node
					$parent_node=$usergroup;
				}
		}
	}else//if we dont have usergroups
	{	//then the parent node is the document root node 'users'
		if(!$parent_node=$doc->first_child())
			return false;
	}
	
	if(!$parent_node){
		$parent_node=$doc->first_child();		
	}
	
	//create user node
	$newUser = $doc->create_element("user");
	$newUser->set_attribute("username",$username);
	$newUser->set_attribute("password",md5($password+$this->salt));
	$newUser->set_attribute("access",$access);
	$newUser->set_attribute("email",$email);
	
	//add to parent node
	if(!$parent_node->append_child($newUser))
		return false;
	
	if(!$doc->dump_file($fileName))
		return false;
	
	return true;	
}

function AddUserDOM($fileName,$groupName,$username,$password,$email,$access){

	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	//first, find if any users with the same name exists
	
	$users=$doc->getElementsByTagName( "user" );
	foreach( $users as $user ){
		$name=$user->getAttribute("username");
		if($name==$username){
			return "FALSE_USER_EXISTS";			
		}
	}
	
	$parent_node=false;
	//if we have usergroups
	if($groupName){
		$usergroups = $doc->getElementsByTagName( "usergroup" );
		foreach( $usergroups as $usergroup ){
				$name=$usergroup->getAttribute("name");
				if($name==$groupName)
				{	//this is our parent node
					$parent_node=$usergroup;
				}
		}
	}else//if we dont have usergroups
	{	//then the parent node is the document root node 'users'
		if(!$parent_node=$doc->firstChild)
			return false;
	}
	
	if(!$parent_node){
		$parent_node=$doc->first_child;		
	}
	
	//create user node
	$newUser = $doc->createElement("user");
	$newUser->setAttribute("username",$username);
	$newUser->setAttribute("password",md5($password+$this->salt));
	$newUser->setAttribute("access",$access);
	$newUser->setAttribute("email",$email);
	
	//add to parent node
	if(!$parent_node->appendChild($newUser))
		return false;
	
	if(!$doc->save($fileName))
		return false;
	
	return true;	
}




function ChangeUserPasswordDOMXML($fileName,$username,$password){ 
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	//open document
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	//get all users
	$users = $doc->get_elements_by_tagname( "user" );
	
	$success=false;
	
	//find user 
	foreach( $users as $user ){
		$name=$user->get_attribute("username");
		if($name==$username)
		{
			//change password
			$user->set_attribute("password",md5($password+$this->salt));			
			$success=true;
		}	
	}

	if(!$doc->dump_file($fileName))
		return false;	
	return $success;	
}

function ChangeUserPasswordDOM($fileName,$username,$password){ 
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	//get all users
	$users = $doc->getElementsByTagName( "user" );
	
	$success=false;
	
	//find user 
	foreach( $users as $user ){
		$name=$user->getAttribute("username");
		if($name==$username)
		{
			//change password
			$user->setAttribute("password",md5($password+$this->salt));			
			$success=true;
		}	
	}

	if(!$doc->save($fileName))
		return false;	
	return $success;	
}

function RemoveUserDOMXML($fileName,$username){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	//open document
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	$success=false;
	
	//get all users
	$users = $doc->get_elements_by_tagname( "user" );
	
	//find user 
	foreach( $users as $user ){
		$name=$user->get_attribute("username");
		if($name==$username)
		{
			$parent=$user->parent_node();
			if($parent->remove_child($user)) {
				$success=true;
			} else {
				exit("error removing node");
			}
		}	
	}	
	
	if(!$doc->dump_file($fileName))
		return false;
		
	return $success;	
		
}

function RemoveUserDOM($fileName,$username){
	
	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	$success=false;
	
	//get all users
	$users = $doc->getElementsByTagName( "user" );
	
	//find user 
	foreach( $users as $user ){
		$name=$user->getAttribute("username");
		if($name==$username)
		{
			
			$user->parentNode->removeChild($user);
			$success=true;
		}	
	}	
	
	if(!$doc->save($fileName))
		return false;
		
	return $success;	
		
}

function LoginDOMXML($fileName,$username,$password){

	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	//open document
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	$users = $doc->get_elements_by_tagname( "user" );
	
	//find user 
	foreach( $users as $user ){
		$name=$user->get_attribute("username");
		if($name==$username)
		{	
			$pass=$user->get_attribute("password");
			if(md5($password+$this->salt)==$pass){				
				//we have a login
				return true;
				
			}
		}	
			
	}	

	return false;	

}

function LoginDOM($fileName,$username,$password){

	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	$users = $doc->getElementsByTagname( "user" );
	
	//find user 
	foreach( $users as $user ){
		$name=$user->getAttribute("username");
		if($name==$username)
		{	
			$pass=$user->getAttribute("password");
			if(md5($password+$this->salt)==$pass){				
				//we have a login
				return true;
				
			}
		}	
			
	}	

	return false;	

}


function GetUserAccessDOMXML($fileName,$username){


	if(!$fileName)
		$fileName=$this->m_xmlFileName;
	//open document
	if(!$doc = domxml_open_file($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	$users = $doc->get_elements_by_tagname( "user" );
	
	//find user 
	foreach( $users as $user ){
		$name=$user->get_attribute("username");
		if($name==$username)
		{	
			$access=$user->get_attribute("access");
			return $access;			
		}	
			
	}	

	return false;	
}

function GetUserAccessDOM($fileName,$username){


	if(!$fileName)
		$fileName=$this->m_xmlFileName;
		
		$doc = new DOMDocument();		
	//open document
	if(!$doc->load($fileName)) {
	   	die("Error- could not access $fileName or error in file");
	}
	
	$users = $doc->getElementsByTagname( "user" );
	
	//find user 
	foreach( $users as $user ){
		$name=$user->getAttribute("username");
		if($name==$username)
		{	
			$access=$user->getAttribute("access");
			return $access;			
		}	
			
	}	

	return false;	
}


}

?>
Return current item: userManager