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

<html>
<body bgcolor="#999966" text="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">
<title>ANISEED CURATOR - Add Pictures</title>

<br><br>
<center>

<?php
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"])) ) {
	    print "<center><strong>you need to <a href=\"login.php\">login</a> as an authorised curator to proceed.</strong>";
	    print '</center></body></html>';
		return;
	}
?>


<?php

    $id=$_POST["id"];
    //$picfile=$_POST["picfile"];
    $ctrl=$_POST["control"];



	//$t="./tmp";
	$t="/tmp2/aniseed/";
	$file=tempnam($t, "PICTMP");
	


	if (! move_uploaded_file($_FILES['picfile']['tmp_name'], $file)) {
	   print "error: ";
	   print_r($_FILES);
	}
	
/*
	if (is_uploaded_file($picfile)){
	   $file=tempnam ("/tmp", "PICTMP");
	   if (!copy($picfile, $file)) 
	   {
		  // if an error occurs the file could not
		  // be written, read or possibly does not exist
		  print "Error Uploading File.";
		  unset($picfile);
		  exit();
	   }
	
	}else{
	    print "Error: no file uploaded";
	}
*/
	unset($picfile);



	
	list($width, $height, $type, $attr) = getimagesize($file);
	
    if( $type !=2 ){
	    print "<br><center><strong>Error: invalid format.</strong></center>";
		print '</center></body></html>';
		return;
	}
	
    $size = 397;  // new image width
    $sizeTN = 150;  // new image width for the thumbnail
	
	
    if ($type == 2) {    
      $src = imagecreatefromjpeg($file);
	
    }
	
	
    $width = imagesx($src);
    $height = imagesy($src);
    $aspect_ratio = $height/$width;
    

	if ($width <= $size) {
	  $new_w = $width;
	  $new_h = $height;
	} else {
	  $new_w = $size;
	  $new_h = abs($new_w * $aspect_ratio);
	}

	
	$img = imagecreatetruecolor($new_w,$new_h); 
    imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
 

 //redo for the thumbnail
     if ($width <= $sizeTN) {
      $new_w = $width;
      $new_h = $height;
    } else {
      $new_w = $sizeTN;
      $new_h = abs($new_w * $aspect_ratio);
    }

	$imgTN = imagecreatetruecolor($new_w,$new_h); 
    imagecopyresized($imgTN,$src,0,0,0,0,$new_w,$new_h,$width,$height);
 
/*
	 $temp1 = tmpfile();
	
	 Imagejpeg($img,$temp1);
	 $temp2 = tmpfile();
	 Imagejpeg($imgTN,$temp2);
*/
	//$temp1 = tempnam("","");
	$temp1 = tempnam("/tmp2/aniseed/","");
	 Imagejpeg($img,$temp1);;

	//$temp2 = tempnam("","");
	$temp2 = tempnam("/tmp2/aniseed/","");
	 Imagejpeg($imgTN,$temp2);;


$f1=fopen($file,"r");
$text1=fread($f1,filesize($file));
fclose($f1);

$f2=fopen($temp1,"r");
$text2 = fread($f2,filesize($temp1));
fclose($f2);

$f3=fopen($temp2,"r");
$text3 = fread($f3,filesize($temp2));
fclose($f3);


//print $text;


/*Creation de l'objet BLOB pour la creation desd images*/




pg_query($c,"begin"); /*Debut de la transaction*/

						$oid1=pg_lo_create($c); /*Creation de l'oid de la sequence*/
						$oid2=pg_lo_create($c);
						$oid3=pg_lo_create($c);
						
if ($ctrl!="y")
$query="insert into \"PICTURE\" (\"PICTURE_ID\", \"IN_SITU_ID\", \"IMAGE\", \"ANNOTATED_IMAGE\", \"THUMBNAIL\") values(nextval('\"S_PICTURE_ID\"'), $id, '$oid1', '$oid2' ,'$oid3')";
else
$query="insert into \"PICTURE\" (\"PICTURE_ID\", \"IN_SITU_ID\", \"IMAGE\", \"ANNOTATED_IMAGE\", \"THUMBNAIL\",\"CONTROL\") values(nextval('\"S_PICTURE_ID\"'), $id, '$oid1', '$oid2' ,'$oid3','y')";



$stmt = pg_query($c,$query) or die ("<strong> ERROR </strong>".pg_last_error($c));

	$handler=pg_lo_open($c,$oid1,"w");
	pg_lo_write($handler,$text1);
	pg_lo_close($handler);

$handler=pg_lo_open($c,$oid2,"w");
	pg_lo_write($handler,$text2);
	pg_lo_close($handler);

$handler=pg_lo_open($c,$oid3,"w");
	pg_lo_write($handler,$text3);
	pg_lo_close($handler);

/*
     $lob = OCINewDescriptor($connection, OCI_D_LOB);
	 $lobTN = OCINewDescriptor($connection, OCI_D_LOB);
	 $lobANOT = OCINewDescriptor($connection, OCI_D_LOB);
	 
     $stmt = OCIParse($connection,"insert into ciona_design.picture (PICTURE_ID, IN_SITU_ID, IMAGE, ANNOTATED_IMAGE, THUMBNAIL) values(CIONA_DESIGN.S_PICTURE_ID.nextval, $id, empty_blob(), empty_blob(), empty_blob()) returning IMAGE,ANNOTATED_IMAGE,THUMBNAIL into :IMG, :IMGANOT, :IMGTN");
	 
     OCIBindByName($stmt, ':IMG', &$lob, -1, OCI_B_BLOB);
	 OCIBindByName($stmt, ':IMGANOT', &$lobANOT, -1, OCI_B_BLOB);
	 OCIBindByName($stmt, ':IMGTN', &$lobTN, -1, OCI_B_BLOB);
	 
	 
     OCIExecute($stmt, OCI_DEFAULT);
     
	 if(  ($lob->savefile($file))and($lobANOT->savefile($temp1))and($lobTN->savefile($temp2))  ){
        OCICommit($connection);

	    print '<center><H1>Validation done</H1>';
	    print '<input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';

     }else{
        echo "Couldn't upload Blob\n";
     }
     OCIFreeDesc($lob);
     OCIFreeStatement($stmt);
*/


	$requete = "update \"ENTRY_ORIGIN\" set \"ANNOTATOR_ID\"=".$_SESSION['curationauthor'].", \"ANNOTATION_DATE\"=current_date where \"BIO_ID\"=$id";
	$resultset = pg_query($c,$requete) or die("<strong> ERROR </strong>".pg_last_error($c));
    





pg_query($c,"commit"); /*Valisation de la transaction*/


 print '<center><H1>Validation done</H1>';
	    print '<input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';


/*
    //enregistrement dans la base
    include("connection.php");
    $requete = "insert into CIONA_DESIGN.PICTURE (PICTURE_ID, IN_SITU_ID, IMAGE, ANNOTATED_IMAGE, THUMBNAIL) ".
                        "values(CIONA_DESIGN.S_PICTURE_ID.nextval, $id, ".
                        "empty_blob(),".
                        "empty_blob(),".
                        "empty_blob()".")"
                        ;
	$resultset = OCIParse($connection, $requete);
	OCIExecute($resultset);


    $requete = "select CIONA_DESIGN.S_PICTURE_ID.currval AS key from DUAL";
	$resultset = OCIParse($connection, $requete);
	OCIExecute($resultset);
	if(OCIFetch($resultset)){
	    $key=OCIresult($resultset,"KEY");
	}
	
	$requete = "update CIONA_DESIGN.PICTURE set THUMBNAIL='".$imgTN."', ANNOTATED_IMAGE='".$src."', IMAGE='".$img."' where picture_id=$key";
	
    // Validation de la transaction
    $committed = OCICommit($connection);

    imagedestroy($img);
	imagedestroy($imgTN);
	
    print '<table width="100%" border="0">';
	
    // V�ification de la validation : si une erreur est survenue, afficher le message d'erreur
    if(!$committed) {
        $error = OCIError($connection);
        echo 'ERROR:'.$error['message'];
    }else{
	    print '<tr><td align="center"><H1>Validation done</H1></td></tr>';
	    print '<tr><td align="center"><center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center></td></tr>';
    }
*/

    unlink($file);
	
	
	
	
	
?>


</center>
<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>

</body>
</html>