<?php

/* $Id: upload_function.php 51 2009-02-19 09:28:42Z kremer $ */

// 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:: DB server: $db_server" );
}

// Get user's login name
$login = $_SERVER['REMOTE_USER'];
if ( $debug ) {
  portal_debug ( "INFO:: 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, ".cer" );
$filename_original_ext = pathinfo ( $filename_original, PATHINFO_EXTENSION );
$filename_temp_server = $_FILES['thefile']['tmp_name'];
if ( $debug ) {
  portal_debug ( "INFO:: Uploading file's full name: $filename_original" );
  portal_debug ( "INFO:: Uploading file's name: $filename_original_base" );
  portal_debug ( "INFO:: Uploading file's extension: $filename_original_ext" );
  portal_debug ( "INFO:: File's temp name on the server: $filename_temp_server" );
}

// Test if it's a zip 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:: Actual date and time: $dt" );
}

// Move the file to the upload dir
$filename_store = $dt."_".$login."_".$filename_original;
if ( $debug ) {
  portal_debug ( "INFO:: Filename in upload dir: $filename_store" );
}

if ( $debug ) {
  portal_debug ( "INFO:: 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_cert_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:: Extract dir: $extract_dir" );
}

if ( ! mkdir ( $root_dir.$extract_dir, 0777 ) ) {
  portal_error ( "upload_cert_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:: Unzipping $root_dir$upload_dir/$filename_store into $root_dir$extract_dir " );
  portal_debug ( "INFO:: Unzip command: $cmd" );
}
exec ( $cmd, $output, $retval );
if ( $retval > 2 ) {
  delete_dir_content ( $root_dir.$extract_dir, true, $debug );
  portal_error ( "upload_cert_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:: shell command: $cmd" );
  portal_debug ( "INFO:: \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:: 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:: Directory $newdir/$company can be found in $root_dir$testcases_dir" );
      }
*/
	  
	  
      if ( ! copy_cert ( $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_cert_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:: 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 ( ! 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'" );
//}


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


    if ( ! chdir ( $root_dir.$doc_dir) ) {
      portal_error ( "upload_cert_function.php: Unable to change to $root_dir$doc_dir" );
    } else {
      portal_debug ( "INFO:: Changed to directory $root_dir$doc_dir");
    }

    //$cmd_newFiles = './build_newfile_list.sh';
	//$cmd_newFiles = './build_check_cert.sh';
$cmd_newFiles = './attributeCert.sh';
	 
	 
    if ( $debug ) {
      portal_debug ( "INFO: trying to execute ".$cmd_newFiles);
    }

    if (file_exists($cmd_newFiles)){
	
      //Complete the command for running the test cases
      $certfiles_array = explode("\n",$newfile_list);
      $certfiles = implode ( " ",$certfiles_array);
      if ( $debug ) {
        portal_debug ( "INFO: CERT files to be tested ".$certfiles);
      }
    
      $cmd_newFiles .= ' '.$certfiles;
      if ( $debug ) {
        portal_debug ( "INFO: executing ".$cmd_newFiles); 
      }
   
      $output = shell_exec($cmd_newFiles);
	//portal_error ( "Output: $output" );
	
	
      if ( $debug ) {	
        portal_debug ( "INFO: name  ".$output);   
      }

    } else {
      portal_error ( "ERROR: could not find ".$cmd_newFiles);
    }
  
  
} 

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


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

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

if ( $debug ) {
  portal_debug ( "INFO:: End of normal operation" );
}
//$output = urlencode($output);
header ( "Location: /protected/upload.php?sigtype=".$sigtype."&error=ok&certreport=$output" );


?>
