Conversion Results
[insert_php]
//print log file with given path
function print_log($log_file){
echo “
\n";
$file = fopen($log_file,'r');
while(!feof($file))
{
$line = fgets($file);
//avoid printing full server paths
if (preg_match("/Called with (\d+) argument/i", $line, $matches)) {
for($i = 0; $i < $matches[1]; $i++){
fgets($file);
}
}else{
echo $line;
}
}
echo "
\n";
fclose($file);
}
//redirect page to URL after current one is loaded
function js_redirect($url){
echo "\n";
}
[/insert_php]
TBX-Basic file download
$temp_file_name = $_FILES['upload']['tmp_name'];
if ($_FILES["upload"]["error"] > 0)
{
echo "
Error: " . $_FILES["file"]["error"] . "
\n";
}
elseif(valid_encoding($temp_file_name)){
if(valid_file($temp_file_name, $_FILES['upload']['name']))
{
$reroute_stderr = "2>&1";
$perl = "~/.plenv/versions/5.18.0/bin/perl";
$script = "./scripts/mrc2tbx.3.3.pl";
$command = "$perl $script " . escapeshellarg($temp_file_name) . ' ' . $reroute_stderr;
$ret_val = 0;
exec($command, $printed_output, $ret_val);
if($ret_val != 0){
echo "
Error: conversion script exited with non-zero value!
\n";
print_r($printed_output);
}
$tbx = $temp_file_name . '.tbx';
$log = $temp_file_name . '.warnings';
if(file_exists($tbx))
{
$down_file_name = basename($_FILES['upload']['name']) . '.tbx';
echo "
The download of your converted TBX-Basic file should start automatically.
If it doesn't, click the button below to download it:
\n";
echo "
\n";
echo "
Messages logged by the conversion program are shown below.
Some may be error messages, which are helpful for finding any errors in your MRC file.
\n";
//print all logging messages
print_log($log);
js_redirect("/scripts/download.php?tmp_file_name=" . basename($tbx) . "&down_file_name=$down_file_name");
}
else{
echo "
Error: no TBX-Basic file was created!
\n";
}
}
}else{
echo '
Error: your file does not appear to be ascii or utf8 encoded';
}
//check that the given file is either ascii or utf8 encoded
function valid_encoding($file_name)
{
$reroute_stderr = "2>&1";
$perl = "~/.plenv/versions/5.18.0/bin/perl";
$script = "./scripts/get_encoding.pl";
$command = "$perl $script " . escapeshellarg($file_name) . ' ' . $reroute_stderr;
$ret_val = 0;
exec($command, $printed_output, $ret_val);
return preg_match('/.+:\t(?:ascii|utf8)$/',$printed_output[0]);
}
//check that the given file is not malicious code
function valid_file($tmp_name, $file_name)
{
$contents = file_get_contents($file_name);
echo $contents;
$rows = explode("\n", $contents);
foreach ($rows as $row)
{
echo $row;
}
return 1;
}
[/insert_php]