UserManagerClass.php 7.04 KB
<?php

class UserManagerClass
{
	protected $infoXml;
	protected $stderr;
	protected $rootElement;

	function __construct($stderr)
	{
		$this->stderr = $stderr;

		$this->infoXml = new DomDocument("1.0","UTF-8");
		$this->infoXml->preserveWhiteSpace = false;
		$this->infoXml->formatOutput = true;
 
	}

	function GetInfoXmlFilePath()
	{
		return getenv("DDINFO")."/".getenv("AMDA_USERS_INFO");
	}

	function GetGroupsXmlFilePath()
	{
		return getenv("DDINFO")."/".getenv("AMDA_GROUPS_INFO");
	}

	function LoadInfoXmlFile()
	{
		if (file_exists($this->GetInfoXmlFilePath())) 
		{
			$this->infoXml->load($this->GetInfoXmlFilePath());
			$this->rootElement = $this->infoXml->documentElement;
		}
		else
		{
			$this->rootElement = $this->infoXml->createElement("users");
			$this->infoXml->appendChild($this->rootElement);
		}

		return 1;
	}

	function AddUser($login, $pwd_hash, $first_name, $last_name, $email, $news, $groups)
	{
		if (!$this->LoadInfoXmlFile())
		return 0;

		$users = $this->infoXml->getElementsByTagName("user");

		//test if login is already used
		for ($i = 0; $i < $users->length; $i++)
		{
			$crtLogin = $users->item($i)->getAttribute("login");
			if ($crtLogin == $login)
				{
					fprintf($this->stderr,"Login already exist on users info file\n");
					return 0;
				}
		}

		date_default_timezone_set('UTC');
		$user = $this->infoXml->createElement("user");
		$user->setAttribute("login",$login);
		$user->setAttribute("name",$last_name);
		$user->setAttribute("first_name",$first_name);
		$user->setAttribute("group",$groups);
		$user->setAttribute("email",$email);
		$user->setAttribute("date",date('j/m/y'));
		$user->setAttribute("news",$news);

		$this->rootElement->appendChild($user);
  

		exec('DDadmin -a '.$login.' '.$pwd_hash,$output,$return);

		if ($return != 0)
		{
			fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
			return 0;
		}
		else
			$this->infoXml->save($this->GetInfoXmlFilePath());

		return 1;
	}

	function ModifyUserPwd($login, $pwd_hash, $pwd_hash_new)
	{
		exec('DDadmin -m '.$login.' '.$pwd_hash.' '.$pwd_hash_new,$output,$return);

		if ($return != 0)
		{
			fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
			return 0;
		}
		
		return 1;
	}

	function ModifyUserGroup($login,$groups)
	{
		if (!$this->LoadInfoXmlFile())
		return 0;

		$users = $this->infoXml->getElementsByTagName("user");

		for ($i = 0; $i < $users->length; $i++)
		{
			$crtLogin = $users->item($i)->getAttribute("login");
			if ($crtLogin == $login)
				{
					$users->item($i)->setAttribute("group",$groups);
					$this->infoXml->save($this->GetInfoXmlFilePath());
					return 1;
				}
		}

		fprintf($this->stderr,"Cannot found user\n");
		return 0;
	}

	function ResetUserPwd($login)
	{
		exec('DDadmin -r '.$login,$output,$return);

		if ($return != 0)
		{
			fprintf($this->stderr,"DDadmin error : ".$output."\n");
			return 0;
		}
		else
			fprintf($this->stderr,"New password is  : ".$output[0]."\n");
		
		return 1;
	}

	function DeleteUser($login)
	{
		if (!$this->LoadInfoXmlFile())
				return 0;
		
		$users = $this->infoXml->getElementsByTagName("user");

		for ($i = 0; $i < $users->length; $i++)
		{
			$crtLogin = $users->item($i)->getAttribute("login");
			if ($crtLogin == $login)
			{
				$this->rootElement->removeChild($users->item($i));
				break;
			}
		}

		exec('DDadmin -d '.$login,$output,$return);

		if ($return != 0)
		{
			fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
			return 0;
		}

		$this->infoXml->save($this->GetInfoXmlFilePath());
		
		return 1;
	}

	//This function will be deprecated for AMDA-NG
	function GenerateGroupsXmlFile()
	{
		if (!$this->LoadInfoXmlFile())
			return 0;

		$users = $this->infoXml->getElementsByTagName("user");

		$groupsXml = new DomDocument("1.0","UTF-8");
		$groupsXml->preserveWhiteSpace = false;
		$groupsXml->formatOutput = true;
		$rootGroupsElement = $groupsXml->createElement("AMDA_USERS");
		$groupsXml->appendChild($rootGroupsElement);
			
		for ($i = 0; $i < $users->length; $i++)
		{
			if (strcmp($users->item($i)->getAttribute("group"),"") == 0)
					continue;
			$u = $groupsXml->createElement("user",$users->item($i)->getAttribute("login"));
			$u->setAttribute("group",$users->item($i)->getAttribute("group"));
			$rootGroupsElement->appendChild($u);
		}

		$groupsXml->save($this->GetGroupsXmlFilePath());

		return 1;
	}

	function SendRegistrationMail($login,$pwd,$first_name, $last_name, $email)
	{
		$subject = 'AMDA registration';

		$msg  = "Dear $first_name $last_name, \r\n\r\n";
		$msg .= "Thanks for your interest in our interactive space physics data manipulation tool and database AMDA\r\n\r\n";
		$msg .= "Your login: $login \r\n";
		$msg .= "  password: $pwd \r\n\r\n";
		$msg .= "at http://amda.cdpp.eu \r\n\r\n";
		$msg .= "Please contact us in case of any problems or questions.\r\n\r\n";
                $msg .= "For acknowledgment and data use policy please follow the rules of the road available at http://amda.cdpp.eu/help/policy.html.\r\n\r\n";
$msg .= "We also invite you to connect to our data archive (https://cdpp-archive.cnes.fr) and use our other tools for 3D visualization of spacecraft and science data (3DView, http://3dview.cdpp.eu), for solar wind propagation (Propagation Tool, http://propagationtool.cdpp.eu and Heliopropa, http://heliopropa.cdpp.eu), or for coordinate and time transformation (TREPS, http://treps.cdpp.eu).\r\n\r\n";
                $msg .= "Feedback on CDPP tools is always welcome and greatly appreciated (amda at irap.omp.eu)!\r\n\r\n";
		$msg .= "Best regards,\r\n\r\n";
		$msg .= "CDPP-AMDA Team";
	
		$headers = "From: amda@irap.omp.eu " . "\r\n".
						"Reply-To: amda@irap.omp.eu " . "\r\n".
						"Cc: amda@irap.omp.eu" . "\r\n".
						"Content-type: text/plain; charset=utf-8\r\n";
		mail($email, $subject, $msg, $headers);

	}

	function Check()
	{
		if (!$this->LoadInfoXmlFile())
			return 0;

		$users = $this->infoXml->getElementsByTagName("user");

		exec('DDadmin -l',$output,$return);

		if ($return != 0)
			{
			fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
			return 0;
			}
			
		$ddAdminUsers = explode(',',$output[0]);

		for ($i = 0; $i < $users->length; $i++)
		{
			$found = false;
			for ($j = 0; $j < count($ddAdminUsers); $j++)
				if (strcmp($users->item($i)->getAttribute("login"),$ddAdminUsers[$j]) == 0)
				{ 
					$found = true;
					break;
				}
			if (!$found)
				fprintf($this->stderr,"User ".$users->item($i)->getAttribute("login")." not found on nc file\n");
		}

		for ($i = 0; $i < count($ddAdminUsers); $i++)
		{
			$found = false;
			for ($j = 0; $j < $users->length; $j++)
				if (strcmp($users->item($j)->getAttribute("login"),$ddAdminUsers[$i]) == 0)
				{    
					$found = true;
					break;
				}
			if (!$found)
				fprintf($this->stderr,"User ".$ddAdminUsers[$i]." not found on info file\n");
		}

		return 1;
	}

	//crypt the password with the salt corresponding to the login
	function cryptPwd($login,$pwd)
	{
		exec('DDadmin -s '.$login,$output,$return);

		if ($return != 0)
		{
			fprintf($this->stderr,"DDadmin error : ".$output."\n");
			return '';
		}

		$salt = $output[0];

		return crypt($pwd,$salt);
        }
}

?>