<?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" );
}


//$_POST

//uploading the checkbox status
// Test if checkbox is ticked

/*

if ( $_POST['reflow'] == 'yes' ) {
$enable_reflow = true;
if ( $debug ) {

portal_debug ( "INFO: enable_reflow is true :: Checkbox= $enable_reflow" );
}
}
else {
$enable_reflow = false;
if ( $debug ) {

portal_debug ( "INFO: enable_reflow is false :: Checkbox= $enable_reflow" );
}
}



if ( $_POST['highcontrast'] == 'yes' ) {
$enable_highcontrast= true;
if ( $debug ) {

portal_debug ( "INFO: enable_reflow is true :: Checkbox= $enable_highcontrast" );
}
}
else {
$enable_highcontrast = false;
if ( $debug ) {

portal_debug ( "INFO: enable_reflow is false :: Checkbox= $enable_highcontrast" );
}
}
*/

// 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, ".pdf" );
$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 name: $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" );
}

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


if ( strtolower ( $file_ext != "pdf" ) ) {
	if ( $debug ) {
		portal_debug ( "INFO: upload_padesconf 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_function.php: Actual date and time: $dt" );
}
if ( $debug ) {
	portal_debug ( "INFO: Extension is : $file_ext" );
}
// Move the file to the upload dir
$filename_store = $dt."_".$login."_".$filename_original;
if ( $debug ) {
	portal_debug ( "INFO: upload_function.php: Filename in upload dir: $filename_store" );
}

if ( $debug ) {
	portal_debug ( "INFO: upload_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_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;

if ( $debug ) {
	portal_debug ( "INFO: upload_padesconf_function.php: Filename in upload dir: $filename_original" );
}


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




$padesconf2 = basename( $filename, ".pdf" );
$padesconffolder = $root_dir.$testcases_dir."/".$company;


if ( $debug ) {
	portal_debug ( "INFO: padesconf2 ".$padesconf2);
		portal_debug ( "INFO: filename ".$filename);
	portal_debug ( "INFO: padesconffolder ".$padesconffolder);
}

$cmd_folder = 'cd /var/www/html/xadesweb/protected/PAdESConf/tools/ ; ';
$cmd_newFiles = 'java -jar /var/www/html/xadesweb/protected/PAdESConf/tools/AdESConformanceChecker.jar -in '.$filename.' -outFolder '.$padesconffolder.'/'.$padesconf2.' -testSpec BaselineProfile';


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



if ( $debug ) {
	portal_debug ( "INFO: executing ".$cmd_folder.$cmd_newFiles);
}
shell_exec($cmd_folder.$cmd_newFiles);
/*
 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';

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


//$xadesconfreport = $xadesconffolder."/".$xadesconf2."/index.html";
$padesconfreport = $testcases_dir."/".$company."/".$padesconf2."/index.html";


// 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$padesconfreport\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_function.php: \n$body" );
}

// Send notification mail

if ( $debug ) {
	portal_debug ( "INFO: upload_function.php: Sigtype: $sigtype" );
}
//if($sigtype=="confchecker") {
//   $recipient = "TSL_INTEROP@list.etsi.org";
//}
portal_debug ( "INFO: upload_padesconf_function.php: mail recipient: $recipient" );
$subject = "New PAdES 2015 Conformance upload has been performed by ".$login;

$headers = "From: PAdES Baseline Profile Conformance <laurent.velez@etsi.org>";

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

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

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


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.$testcases_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."&padesconfreport=$padesconfreport");

?>
