UserManager.php 3.99 KB
<?php
	require_once(dirname(__FILE__) . '/UserManagerClass.php');

	putenv("LD_LIBRARY_PATH=".getenv("LD_LIBRARY_PATH"));
	putenv("PATH=./:".getenv("DDBASEBIN").":/bin:/usr/bin");

	$stderr = fopen("php://stderr","w");

	$shortopts  = "j:";
	$longopts  = array("json:");
	$options = getopt($shortopts, $longopts);
	$json_file = "";
	if (array_key_exists("json",$options)) {
		$json_file = $options["json"];
	}
	else if (array_key_exists("j",$options)) {
		$json_file = $options["j"];
	}
	
	if (!empty($json_file)) {
		if (!file_exists($json_file)) {
			echo "[ERROR] Cannot find json file: ".$json_file.PHP_EOL;
			exit(1);
		}
		$json_string = file_get_contents($json_file);
		$args = json_decode($json_string);
		if (empty($args)) {
			echo "[ERROR] Cannot decode json data from: ".$json_file.PHP_EOL;
			exit(1);
		}
	}
	else {
		$args = new stdClass();
		$args->action = $argv[1];
		switch ($args->action)
	   {
			case 'delete' :
				$args->login = $argv[2];
			break;
			case 'groups' :
				$args->login = $argv[2];
				$args->groups = $argv[3];
			break;
			case 'add' :
				$args->login = $argv[6];
				$args->pwd = $argv[7];
				$args->first_name= $argv[8];
				$args->last_name= $argv[9];
				$args->email= $argv[10];
				$args->pwd_hashed = $argv[2];
				$args->news= $argv[3];
				$args->groups= $argv[4];
				$args->sendEmail= $argv[5];
			break;
			default :
			fprintf($stderr,"Not implemented action ".$args->action."\n");
			exit;
		}
	}

	if (!isset($args->action))
	{
		fprintf($stderr,"Missing action argument\n");
		exit;
	}

	$userManager = new UserManagerClass($stderr);

	switch ($args->action)
	{
		case 'add' :
			if (!isset($args->login) || !isset($args->pwd) || !isset($args->first_name) || !isset($args->last_name) || 
				!isset($args->email))
			{
				fprintf($stderr,"Missing argument(s) to add a user\n");
				exit;
			}
			
			if (isset($args->pwd_hashed) && !($args->pwd_hashed))
				$pwd_hash = crypt($args->pwd,chr(rand(97,122)).chr(rand(97,122)));
			else
				$pwd_hash = $args->pwd;

			if ($userManager->AddUser($args->login,$pwd_hash,$args->first_name,$args->last_name,$args->email, 
							isset($args->news) ? $args->news : 1, 
							isset($args->groups) ? $args->groups : ''))
			{
				$userManager->GenerateGroupsXmlFile();
				if (isset($args->sendEmail) && $args->sendEmail)
				$userManager->SendRegistrationMail($args->login,$args->pwd,$args->first_name,$args->last_name,$args->email);
					fprintf($stderr,"User ".$args->login." added\n");
			}
		break;
		case 'modify' :
			if (!isset($args->login) || !isset($args->pwd) || !isset($args->pwd_new))
			{
				fprintf($stderr,"Missing argument(s) to modify the user password\n");
				exit;
			}

			if (isset($args->pwd_hashed) && !($args->pwd_hashed))
				$pwd_hash = $userManager->cryptPwd($args->login,$args->pwd);
			else
				$pwd_hash = $args->pwd;

			if (isset($args->pwd_new_hashed) && !($args->pwd_new_hashed))
				$pwd_new_hash = crypt($args->pwd_new,chr(rand(97,122)).chr(rand(97,122)));
			else
				$pwd_new_hash = $args->pwd_new;
		
			if ($userManager->ModifyUserPwd($args->login,$pwd_hash,$pwd_new_hash))
				fprintf($stderr,"Password of".$args->login." modified\n");
		break;
		case 'groups' : 
			if (!isset($args->login) || !isset($args->groups))
			{
				fprintf($stderr,"Missing argument(s) to modify the user groups\n");
				exit;
			}
			if ($userManager->ModifyUserGroup($args->login,$args->groups))
			{
				$userManager->GenerateGroupsXmlFile();
				fprintf($stderr,"Groups of ".$args->login." modified\n");
			}
		break;
		case 'reset' :
			if (!isset($args->login))
			{
				fprintf($stderr,"Missing argument(s) to reset the user password\n");
				exit;
			}
			$userManager->ResetUserPwd($args->login);
		break;
		case 'delete' :
			if (!isset($args->login))
			{
				fprintf($stderr,"Missing argument(s) to delete user\n");
				exit;
			}
			if ($userManager->DeleteUser($args->login))
			{
				$userManager->GenerateGroupsXmlFile();
				fprintf($stderr,"User ".$args->login." deleted\n");
			}
		break;
		case 'check' :
			$userManager->Check();
		break;
	}
?>