<?php

/* $Id: upload_function.php 51 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_function.php: DB server: $db_server" );
}

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

// 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'];
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" );
}

// LVZ 2011-12-02  : the test of the zip has been removed as it seems to cause some upload error
// The error code is sent back even if the upload file is zip.
/*
// Test if it's a zip file
if ( strtolower ( $filename_original_ext != "zip" ) ) {
  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" );
}

// 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/$filename_store'" );
}

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

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

// Create extract dir
$extract_dir = $temp_dir."/".$filename_original_base."_".$login."_".$dt;
if ( $debug ) {
  portal_debug ( "INFO: upload_function.php: Extract dir: $extract_dir" );
}

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

// Unzip uploaded file
$cmd = "/usr/bin/unzip -o $root_dir$upload_dir/$filename_store -d $root_dir$extract_dir";
if ( $debug ) {
  portal_debug ( "INFO: upload_function.php: Unzipping $root_dir$upload_dir/$filename_store into $root_dir$extract_dir " );
  portal_debug ( "INFO: upload_function.php: Unzip command: $cmd" );
}
exec ( $cmd, $output, $retval );
if ( $retval > 2 ) {
  delete_dir_content ( $root_dir.$extract_dir, true, $debug );
  portal_error ( "upload_function.php: Couldn't extract '$root_dir$upload_dir/$filename_store' to '$root_dir$extract_dir'" );
}

// list temp dir
if ( $debug ) {
  $cmd = "ls -laR $root_dir$temp_dir";
  $output = shell_exec ( $cmd );
  portal_debug ( "INFO: upload_function.php: shell command: $cmd" );
  portal_debug ( "INFO: upload_function.php: \n$output" );
}

// Select testcase dirs in uploaded file
$list_newdir = select_dir ( $root_dir.$extract_dir, $testcase_dir_prefix, $debug );
$list_newdir_size = count ( $list_newdir );
if ( $debug ) {
  portal_debug ( "INFO: upload_function.php: Number of testcase dirs in uploaded file: $list_newdir_size" );
}

// Copy files to testcases dirs
$newfile_list = "";
$oldfile_list = "";

if ( $list_newdir_size > 0 ) {
  foreach ( $list_newdir as $newdir ) {
    if ( check_dir ( $root_dir.$testcases_dir, $root_dir.$extract_dir."/".$newdir, $testcase_dir_prefix, $company, $debug ) ) {
      if ( $debug ) {
        portal_debug ( "INFO: upload_function.php: Directory $newdir/$company can be found in $root_dir$testcases_dir" );
      }

      if ( ! compare_and_copy_files ( $root_dir.$extract_dir."/".$newdir."/".$company, $root_dir.$testcases_dir."/".$newdir."/".$company, $newfile_list, $oldfile_list, $testcase_dir_prefix, $debug ) ) {
        delete_dir ( $root_dir.$extract_dir, $debug );
        portal_error ( "upload_function.php: Unable to copy files from $root_dir$extract_dir/$newdir/$company to $root_dir$testcases_dir/$newdir/$company" );
      }
    } else {
      if ( $debug ) {
        portal_debug ( "INFO: upload_function.php: Directory $newdir doesn't exist in $root_dir$testcases_dir" );
      }
    }
  }
}

// Delete extract dir
delete_dir_content ( $root_dir.$extract_dir, true, $debug );

// Create new download file
$dt = datetime();
$zipfile_name = $zipfile_prefix.$dt;

if ( $debug ) {
        portal_debug ( "INFO: upload_function.php:$dt " );
		   portal_debug ( "INFO: upload_function.php:$zipfile_prefix " );
		      portal_debug ( "INFO: upload_function.php: $zipfile_name" );
}
$zipfile_exclude_mask = '/var/www/html/xadesweb/protected/ESIG/TestCases/MS/';
$zipfile_include_mask = '/var/www/html/xadesweb/protected/ESIG/TestCases/ESIG*/';

if ( ! create_zipfile ( $zipfile_name, $root_dir.$testcases_dir , $root_dir.$download_dir, $zipfile_include_mask, $zipfile_exclude_mask, $debug ) ){
  portal_error ( "upload_function.php: Couldn't create zip file '$zipfile_name' in '$root_dir$download_dir'" );
}

//LVZ 2011-11-30 in response of Frank Cornelis ' request to get always the latest zip file
 $cmd = "cp $root_dir$download_dir/$zipfile_name.zip $root_dir$download_dir/plugtest_latest.zip";
  exec ( $cmd, $output, $retval );



// 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$download_dir/$zipfile_name.zip\n\n";

if ( strlen ( $newfile_list ) > 0 ) {
  $body2 = "The following files are handled as new:\n".$newfile_list."\n";
} else {
  $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 = "The content of the uploaded file is:
".$output."
BR,
Electronic Signature Remote Plugtest Team";

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

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

// Send notification mail
//$recipient = "laurent.velez@etsi.org,luigi.rizzo@infocert.it,konrad.lanz@iaik.tugraz.at,cruellas@ac.upc.edu";
$recipient = "laurent.velez@etsi.org";

$subject = "New Verification report was uploaded by ".$login;
$headers = "From: Plugtests Portal <xades3_admin@list.etsi.org>";


if ( $login == 'velez' ){ $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" );
}


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

mail ( $recipient, $subject, $body, $headers );

if ( ! chdir ( $root_dir.$doc_dir) ) {
  portal_error ( "upload_function.php: Unable to change to $root_dir$doc_dir" );
}

$cmd = "./build.sh &";
exec ( $cmd, $output, $retval );

if ( $debug ) {
  portal_debug ( "INFO: upload_function.php: End of normal operation" );
}

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


?>