<?php

/* $Id: upload_function.php 51 2012-02-01 09:28:42Z velez $ */

// Test if user is connected
if ( !isset ( $_SERVER['REMOTE_USER'] ) ) {
	header ( 'Location: /pub' );
}

// Global settings
include_once "portal_settings.php";
include_once "portal_functions.php";


// Test if upload is allowed
if ( ! $enable_upload ) {
	header ( "Location: /protected/upload.php?sigtype=".$sigtype."&error=noup" );
}


if ( $debug ) {
	portal_debug ( "INFO: upload_acert_function.php: DB server: $db_server" );
}

// Get user's login name
$login = $_SERVER['REMOTE_USER'];
if ( $debug ) {
	portal_debug ( "INFO: upload_acert_function.php: login: $login" );
	portal_debug ( "INFO: upload_acert_function.php: db_server: $db_server" );
	portal_debug ( "INFO: upload_acert_function.php: db_name: $db_dbname" );
	portal_debug ( "INFO: upload_acert_function.php.php: db_username: $db_username" );
}



// Get user's company name
$company = get_company_name ( $login, $db_server, $db_username, $db_password, $db_dbname, $debug );

// Uploading file's name
$filename_original = $_FILES['thefile']['name'];
$filename_original_base = basename( $filename_original, ".zip" );
$filename_original_ext = pathinfo ( $filename_original, PATHINFO_EXTENSION );
$filename_temp_server = $_FILES['thefile']['tmp_name'];

//PB the 4 following debug messages do not appear in the debug logs !!!
if ( $debug ) {
	portal_debug ( "INFO: upload_function.php: Uploading file s full name: $filename_original" );
	portal_debug ( "INFO: upload_function.php: Uploading file s basename witout extension : $filename_original_base" );
	portal_debug ( "INFO: upload_function.php: Uploading file s extension: $filename_original_ext" );
	portal_debug ( "INFO: upload_function.php: File s temp name on the server: $filename_temp_server" );
}


//LVZ 2014-01-30  created this clean_dir_function to remove the pdf, sha2 , html and the conf report from the company folder. 
// all files are seen a new as soon as the upload is perfomed. it prevents from displaying an old result of signature with the same file.
// it also allows to save spaces. only the TSL.xml are remaining in the folder

clean_dir_content ( $root_dir.$testcases_dir."/".$company, false, $debug );
clean_dir_content ( $root_dir.$extract_dir."/".$company, false, $debug );

$filename = $root_dir.$upload_dir."/".$company."/".$filename_original;
$path_parts = pathinfo($filename);
$file_ext = $path_parts['extension'];


if ( strtolower ( $file_ext != "zip" ) ) {
	if ( $debug ) {
		portal_debug ( "INFO: upload_asicconf function.php: wrong extension: $file_ext" );
	}

	header ( "Location: /protected/upload.php?error=zip" );
}


/*
 // Test if it's a xml file
if ( strtolower ( $filename_original_ext != "cer" ) ) {
header ( "Location: /protected/upload.php?sigtype=".$sigtype."&error=zip" );
}
*/


// Get actual date and time
$dt = datetime();
if ( $debug ) {
	portal_debug ( "INFO: upload_asicconf_function.php: Actual date and time: $dt" );
}
if ( $debug ) {
	portal_debug ( "INFO: Extension is : $file_ext" );
}
// Move the file to the upload dir
// LVZ 2013-12-02  replace filename_original by filename_original_base because .p7m appeared in the foldername
//$filename_store = $dt."_".$login."_".$filename_original;
$filename_store = $dt."_".$login."_".$filename_original_base;
if ( $debug ) {
	portal_debug ( "INFO: upload_asicconf_function.php: Filename in upload dir ie filename_store var: $filename_store" );
}

if ( $debug ) {
	portal_debug ( "INFO: upload_asicconf_function.php: Moving '$filename_temp_server' to '$root_dir$upload_dir/$company/$filename_store'" );
}

if ( ! move_uploaded_file ( $filename_temp_server, $root_dir.$upload_dir."/".$company."/".$filename_original ) ) {
	portal_error ( "upload_asicconf_function.php: Unable to move '$filename_temp_server' to '$root_dir$upload_dir/$company/$filename_store'" );
}



// $newfile_list = "ConfChecker/".$company."/".$filename_original;

chmod ( $root_dir.$upload_dir."/".$company."/".$filename_original, 0666 );

$filename = $root_dir.$upload_dir."/".$company."/".$filename_original;
//$filename_testdir = $root_dir.$testcases_dir."/".$company."/".$filename_original;

if ( $debug ) {
	portal_debug ( "INFO: upload_asicconf_function.php: Filename in upload dir: $filename_original" );
	portal_debug ( "INFO: upload_asicconf_function.php: Full path in upload dir: $filename" );
}

// Create extract dir
// $extract_dir = $temp_dir."/".$filename_original_base."_".$login."_".$dt;



if ( $debug ) {
  portal_debug ( "INFO: upload_asicconf_function.php: Extract dir: $extract_dir" );
}

// LVZ
/*
if ( ! mkdir ( $root_dir.$extract_dir, 0777 ) ) {
  portal_error ( "upload_asicconf_function.php: Unable to create dir: $root_dir$extract_dir" );
}
*/

// Unzip uploaded file
// $cmd = "/usr/bin/unzip -o $filename -d $extract_dir";




$asicconf2 = basename( $filename, ".zip" );
$asicconffolder = $extract_dir."/".$company."/".$asicconf2;
$comp_dir = $root_dir.$testcases_dir."/".$company;

$extract_comp_dir = $root_dir.$extract_dir."/".$company;

/////////////////////////////////////////////////////////////////////////////////
// LVZ unzipping function , no more needed as the asicconfchecker will perform the unzip
/*
// Unzip uploaded file
$cmd = "/usr/bin/unzip -o $filename -d $asicconffolder";


if ( $debug ) {
  portal_debug ( "INFO: upload_asicconf_function.php: Unzipping $root_dir$upload_dir/$filename_store into $root_dir$extract_dir " );
  portal_debug ( "INFO: upload_asicconf_function.php: Unzip command: $cmd" );
}
exec ( $cmd, $output, $retval );

if ( $retval > 2 ) {
  delete_dir_content ( $root_dir.$extract_dir, true, $debug );
  portal_error ( "upload_asicconf_function.php: Couldn't extract '$root_dir$upload_dir/$filename_store' to '$root_dir$extract_dir'" );
}
*/

// LVZ end of unzipping function
///////////////////////////////////////////////////////////////////////////////////



//$file = basename ($path,".php")
//$acerfile = basename ( $filename, ".cer" );


// $meta_filename = $asicconffolder."/META-INF/*.*";


chmod ( $root_dir.$upload_dir."/".$company."/".$filename_original, 0666 );

if ( $debug ) {
	portal_debug ( "INFO: asicconf2 ".$asicconf2);
	portal_debug ( "INFO: asicconffolder ".$asicconffolder);
	portal_debug ( "INFO: comp_dir output folder ".$comp_dir);
	portal_debug ( "INFO: extract_comp_dir temp folder ".$extract_comp_dir);
}

// $cmd_newFiles = 'java -jar /var/www/html/xadesweb/protected/CAdESConf/tools/CAdESConformanceChecker.jar -in '.$meta_filename.' -rootoutFolder '.$asicconffolder;

//$cmd_newFiles = 'java -jar /var/www/html/xadesweb/protected/ASiCConf/tools/ASiC_Conf_Checker/ASiCConformanceChecker.jar -in '.$filename.' -out '.$comp_dir.'/ -temp '.$extract_comp_dir.'/';


$cmd_newFiles = 'java -jar /var/www/html/xadesweb/protected/ASiCConf/tools/ASiCConformanceChecker.jar -in /var/www/html/xadesweb/protected/ASiCConf/Upload/ETSI/test1.zip -out /var/www/html/xadesweb/protected/ASiCConf/TestCases/ETSI/ -temp /var/www/html/xadesweb/protected/ASiCConf/Extract/ETSI/ 2>&1';

$cmd_secondFiles = 'java -jar /var/www/html/xadesweb/protected/ASiCConf/tools/XAdESConformanceChecker.jar -in /var/www/html/xadesweb/protected/ASiCConf/Extract/ETSI/META-INF/signatures.xml -outFolder /var/www/html/xadesweb/protected/ASiCConf/TestCases/ETSI/ -inASiC';
//$cmd_newFiles = '/usr/java/jdk1.6.0_06/bin/java -jar /var/www/html/xadesweb/protected/ASiCConf/tools/ASiCConformanceChecker.jar -in /var/www/html/xadesweb/protected/ASiCConf/Upload/ETSI/test1.zip -out /var/www/html/xadesweb/protected/ASiCConf/TestCases/ETSI/ -temp /var/www/html/xadesweb/protected/ASiCConf/Extract/ETSI/ 2>&1';

//$cmd_newFiles = '. /var/www/html/xadesweb/protected/ASiCConf/tools/asic.sh';





if ( $debug ) {
	portal_debug ( "INFO: executing ".$cmd_newFiles);
}
 
# $cmd_newFiles = '. '.$root_dir.$upload_dir.'/attributeCert.sh';



shell_exec($cmd_newFiles);


if ( $debug ) {
	portal_debug ( "INFO: executing ".$cmd_secondFiles);
}
 




shell_exec($cmd_secondFiles);

/*
 if (file_exists($cmd_newFiles)){

//Complete the command for running the conformance checker
$attrcertfiles_array = explode("\n",$newfile_list);
$attrcertfiles = implode ( " ",$attrcertfiles_array);
if ( $debug ) {
portal_debug ( "INFO: TSL files to be tested ".$attrcertfiles);
}

$cmd_newFiles .= ' '.$attrcertfiles;
if ( $debug ) {
portal_debug ( "INFO: executing ".$cmd_newFiles);
}
 
$ccreport = shell_exec($cmd_newFiles);
portal_debug("Output: $ccreport");

*/
# $ccreport = shell_exec($cmd_newFiles);
$code='&error=ok';

//$cadesconfreport = basename($acertfile);
//$acertreport = $testcases_dir."/".$company."/".$acertreport;


//$cadesconfreport = $cadesconffolder."/".$cadesconf2."/index.html";
// $asicconfreport = $testcases_dir."/".$company."/".$asicconf2."/index.html";


$asicconfreport = $testcases_dir."/".$company."/index.html";

if ( $debug ) {
	portal_debug ( "INFO: asicconfreport ".$asicconfreport);
}

// Compile notification mail body
$body1 = "Dear Participants,

		A new file has been uploaded to the portal by the user '".$login."'. You can download the updated files from:
		$portal_address$asicconfreport\n\n";


if ( strlen ( $newfile_list ) > 0 ) {
	$body2 = "The following files are handled as new:\n".$newfile_list."\n";

	$body2 = "";
}

if ( strlen ( $oldfile_list ) > 0 ) {
	$body3 = "The following files have been deleted:\n".$oldfile_list."\n";
} else {
	$body3 = "";
}

// $output = shell_exec ( "/usr/bin/unzip -lq $root_dir$upload_dir/$filename_store 2>&1" );
$body4 = "
		Best Regards,
		ETSI Team";

//$body4 = "The content of the uploaded file is:
// ".$output."
//BR,
//XAdES Remote Plugtest Team";

$body = $body1.$body2.$body3.$body4;

if ( $debug ) {
	portal_debug ( "INFO: upload_asicconf_function.php: \n$body" );
}

// Send notification mail

if ( $debug ) {
	portal_debug ( "INFO: upload_asicconf_function.php: Sigtype: $sigtype" );
}
//if($sigtype=="confchecker") {
//   $recipient = "TSL_INTEROP@list.etsi.org";
//}
portal_debug ( "INFO: upload_asicconf_function.php: mail recipient: $recipient" );
$subject = "New ASiC Conformance Report has been generated by ".$login;

$headers = "From: ASiC Conformance Checker<laurent.velez@etsi.org>";

if ( $login == 'velez' ){
	$recipient = "laurent.velez@etsi.org";
}

$recipient = "laurent.velez@etsi.org";

if ( $debug ) {
	portal_debug ( "INFO: upload_asicconf_function.php: mail recipient: $recipient" );
	portal_debug ( "INFO: upload_asicconf_function.php: mail subject: $subject" );
	portal_debug ( "INFO: upload_asicconf_function.php: mail headers: $headers" );
}

//2013-12-02 LVZ  Disable the mail sending for performance and also as it is useless.
// mail ( $recipient, $subject, $body, $headers );


//LVZ 21-11-2010
// Create a file for each upload in the company folder. used later for /protected/TSLPlugtest/stat_number_of_signatures.php
$filename_stat = "statreport_".$dt."_".$company."_".$login.".txt";
$filepath = $root_dir.$asicstats_dir."/".$company."/".$filename_stat;
$fp = fopen($filepath,"a+");
fputs($fp, $filename_stat);
fclose($fp);


//Conformance checker
//header ( "Location: /protected/upload.php?sigtype=".$sigtype."&error=ok&tslfile=".$tslfile."&confcheckerreport=".$ccreport."&confcheckerpretty=".$ccpretty);

//header ( "Location: /protected/upload.php?sigtype=".$sigtype."&error=ok&confcheckerpretty=$ccpretty&confcheckerreport=$ccreport");

//LVZ
//header ("Location: /protected/upload.php?sigtype=".$sigtype.$code."&confcheckerpretty=$ccpretty&confcheckerreport=$ccreport");

header ("Location: /protected/upload.php?sigtype=".$sigtype.$code."&asicconfreport=$asicconfreport");

?>
