TEST: myfile, {'. $_POST['myfile']. '}
'; echo 'TEST: mat, {'. $_POST['mat']. '}
'; echo 'TEST: keywei, {'. $_POST['keywei']. '}
'; echo 'TEST: keynum, {'. $_POST['keynum']. '}
'; echo 'TEST: trials, {'. $_POST['trials']. '}
'; echo 'TEST: iseed, {'. $_POST['iseed']. '}
'; echo 'TEST: conf, {'. $_POST['conf']. '}
'; echo 'TEST: ishow, {'. $_POST['ishow']. '}
'; } define('DIR_PATH', $base_path. 'web/cgi-bin/'); define('TMP_DIR', $base_path. 'web/tmp/'); if ($asp) define('TMP_DIR', $base_path. 'temp/'); define('TMP_URL', 'http://web.tecnico.ulisboa.pt/ist11038/tmp/'); define('EXEC_KEY', 'executable'); // not yet used ? define('OUTPUT_ERROR_FILE', 'error-output.txt'); global $_POST; $file_content = ''; $file_arg_name; $temp; $param = ''; $work_dir = ''; error_reporting(E_ALL); function undo_magic_quotes_on_post(&$post_array) //............function {if (get_magic_quotes_gpc( )) {foreach ($post_array as $key => $value) {$post_array[$key] = stripslashes($value); } // end-foreach } // end-if } // ......................................................end function function uploadfile($filename, $filesize, $test) //............function {try { // Undefined | Multiple Files | $_FILES Corruption Attack if (!isset($_FILES[$filename]['error']) || is_array($_FILES[$filename]['error'])) {throw new RuntimeException('Invalid parameters.');} // Check $_FILES['myfile']['error'] value. switch ($_FILES[$filename]['error']) {case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: throw new RuntimeException('No file sent.'); case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: throw new RuntimeException('Exceeded filesize limit.'); default: throw new RuntimeException('Unknown errors.'); } // end-switch if ($_FILES[$filename]['size'] > $filesize) {throw new RuntimeException('Exceeded filesize limit.');} if ($test) echo 'TEST: File is uploaded successfully: {'. $_FILES[$filename]['tmp_name']. '}
'; return 1; } catch (RuntimeException $e) { if ($test) echo 'TEST: RuntimeException ?
'; echo $e->getMessage(); } // end-catch return 0; } // .....................................................end function //include('python_top.php'); undo_magic_quotes_on_post($_POST); foreach ($_POST as $key => $value) {if ($key == "mat") {$file_content .= $value . "\n"; if ($test) echo "TEST: $key : $value
"; } } // end-if key & end foreach $filename = 'myfile'; // ........................................upload $up = uploadfile($filename, 100000, $test); if ($test) echo 'TEST: myfile, {'. $_POST['myfile']. '}
'; if ($test) echo "TEST: filename $filename {". $_POST[$filename]. '}
'; //Create temporary file with data if (!$up){ $temp = tmpfile(); fwrite($temp, $file_content); $file_arg_name = stream_get_meta_data($temp)['uri']; echo "FILE NAME" . $file_arg_name . "
"; } else $file_arg_name = $_FILES['myfile']['tmp_name']; // $param = $_FILES['myfile']['tmp_name'].'\n'; $work_name = microtime(true) * 10000; // Cria pasta de trabalho if ($test) echo 'TEST: work_name: {'. $work_name. '}
'; $work_dir = TMP_DIR. $work_name; umask(0000); // remover permissoes de defeito if ($test) echo 'TEST: work_dir: {'. $work_dir. '}
'; if (mkdir($work_dir, 0777)) { $descriptorspec = array(0 => array("pipe", "r"), // stdin, pipe the child reads from 1 => array("pipe", "w"), // stdout, pipe the child writes to 2 => array("file", $work_dir.'/'.OUTPUT_ERROR_FILE, "a")); // $exec_string = '/usr/bin/python3 BagsFromExcel.py '. DIR_PATH; $exec_string = '/usr/bin/python3 BagsFromExcel.py '. $file_arg_name; $process = proc_open($exec_string, $descriptorspec, $pipes); // $param = $_FILES['myfile']['tmp_name'].'\n'; $param .= $up. "\n"; $param .= "Peso\nNum\n5000\n0\n95\n0\n"; $param .= $work_dir."/\n"; if ($test) echo "TEST: PARAMS: $param
"; if (is_resource($process)) { /* Escrever para o stdin do executavel os parametros */ fwrite($pipes[0], $param); if ($test) echo 'TEST --- Params: {'. $param. '}
'; fclose($pipes[0]); } // end-if is_resource // Admitimos que o executavel nao gera linhas com + de 4095 caracteres while (!feof($pipes[1])){ $buffer = fgets($pipes[1], 4096); // if ($test) echo $buffer; echo htmlentities($buffer, ENT_NOQUOTES, "ISO8859-15"); } //Delete temporary file if(!$up) fclose($temp); //Apagar ficheiro de erro se estiver vazio if (file_exists($work_dir. '/'. OUTPUT_ERROR_FILE)){ $size = filesize($work_dir. '/'. OUTPUT_ERROR_FILE); if ($size == 0) { unlink($work_dir. '/'. OUTPUT_ERROR_FILE); } } if ($test) echo 'TEST: TMP_DIR {'. TMP_DIR. '}
'; if ($test) echo '

TEST: src {'. TMP_DIR. $work_name. '}

'; echo 'Graph'; fclose($pipes[1]); } include 'python_bot.php'; ?>