<?php
    include ("connection.php");
    include("frames_header.php");
?>

<?php
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"]))or(!isset($_SESSION["curationauthor"])) ) {
	    print "<center><strong>you need to <a href=\"login.php\">login</a> as an authorised administrator to proceed.</strong></center>";
	    include("frames_footer.php");
		return;
	}else{
	    $name = $_POST["name"];
		$surname = $_POST["surname"];
		$log = strtoupper($_POST["log"]);
		$pwd = $_POST["pwd"];
		$pwd2 = $_POST["pwd2"];
		$mail = $_POST["mail"];
		$team = $_POST["team"];
		$tel = $_POST["tel"];
		$addresse = $_POST["addresse"];
	    $fax = $_POST["fax"];
		//$annotator = $_POST["annotator"];
		$annotator="y";  //constante de maniere a tj voir les utilisateurs dans le manager
		$curator = $_POST["curator"];
		
		if($pwd!=$pwd2){
		    print "<center>ERROR: wrong password</center>";
			include("frames_footer.php");
			return;
		}
		
		if(! eregi("^.+@.+\..+$", $mail)){
		    print "<center>ERROR: invalid email address</center>";
			include("frames_footer.php");
			return;
		}
		
		$requete="select \"AUTHOR_ID\" from \"AUTHOR\" where \"MAIL\"='$mail'";
		$resultset = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		if($row=pg_fetch_array($resultset)){
		    print "<center>ERROR: duplicate email entry</center>";
			include("frames_footer.php");
			return;
		}
		
/*Contrainte future du serveur de base de données, a activer dans le cas ou plusieurs bases de données tournent sur le meme serveur,
par convention les utilisateurs d'ANISEED auront un login du type ANISEED_User: par exemple: ANISEED_FABRICE.*/

/*Pour cela, il faut désactiver ce commentaire, pour la modification automatique du login*/
/*		$log="ANISEED_".$log; */

		
		$requete="select \"usename\" from \"pg_shadow\" where \"usename\"='$log'";
		$resultset = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		
		
		if( (strlen($name)>0)and(strlen($surname)>0)and(strlen($log)>0)and(strlen($pass)>0)and(strlen($mail)>0)and(strlen($team)>0)  ){
			//creation de l'utilisateur dans oracle
			
			/*$requete="CREATE USER \"".$log."\" PROFILE \"DEFAULT\" IDENTIFIED BY \"".$pwd."\" DEFAULT TABLESPACE \"USERS\" ACCOUNT UNLOCK";*/






			$requete="CREATE USER \"".$log."\" WITH PASSWORD '".$pwd."' NOCREATEDB NOCREATEUSER";
			pg_query($c,"begin");			
			$resultset = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			//print $requete;
			
			
			// Validation de la transaction
			
		
			// Verification de la validation : si une erreur est survenue, afficher le message d'erreur
			
			
			//privileges
			if($annotator=="y"){
            /*    $requete="GRANT \"ANISEED_ANNOTATOR\" TO \"$log\"";
				$resultset = OCIParse($connection, $requete);
				//OCIExecute($resultset,OCI_DEFAULT);
		*/
			}
		$requete="ALTER GROUP \"ANNOTATOR_ANISEED\" ADD USER \"$log\"";
		pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

			if($curator=="y"){
                /*$requete="GRANT \"ANISEED_CURATOR\" TO \"$log\"";
				$resultset = OCIParse($connection, $requete);
				//OCIExecute($resultset,OCI_DEFAULT);*/
		$requete="ALTER GROUP \"CURATOR_ANISEED\" ADD USER \"$log\"";
		pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	
			}
/*
		$requete="GRANT \"CONNECT\" TO \"$log\"";
			$resultset = OCIParse($connection, $requete);
			//OCIExecute($resultset,OCI_DEFAULT);*/
	
			
			//enr des parametres de l'utilisateur dans ANISEED
			$requete="insert into \"AUTHOR\"(\"AUTHOR_ID\", \"NAME\", \"SURNAME\", \"LOGIN\", \"MAIL\", \"TEAM\", \"TEL\", \"LAB_ADDRESS\", \"FAX\", \"IS_CURATOR\" ) values(nextval('\"S_AUTHOR_ID\"'), '$name', '$surname', '$log', '$mail', '$team', '$tel', '$address', '$fax', '$curator')";
			
			$resultset = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			pg_query($c,"commit");
			
		}else{
			print "<center><strong>Every fields marked whith a * are required.</strong></center>";
			include("frames_footer.php");
			return;
		}

		
		
		// Validation de la transaction
		//$committed = OCICommit($connection);
	
		// Verification de la validation : si une erreur est survenue, afficher le message d'erreur
		
			print '<center><H1>User creation done</H1></center><br>';			
		
        print '<center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';

	}
?>

<?php
    include("frames_footer.php");
?> 