Skip to content

Commit

Permalink
MDL-8897 fixed shell commands in tex and algebra filters; merged from…
Browse files Browse the repository at this point in the history
… MOODLE_17_STABLE
  • Loading branch information
skodak committed Mar 14, 2007
1 parent 69945f8 commit 10ccb7a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 96 deletions.
127 changes: 63 additions & 64 deletions filter/algebra/algebradebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
}

function algebra2tex($algebra) {
Global $CFG;
global $CFG;
$algebra = str_replace('&lt;','<',$algebra);
$algebra = str_replace('&gt;','>',$algebra);
$algebra = str_replace('<>','#',$algebra);
Expand All @@ -99,9 +99,9 @@ function algebra2tex($algebra) {
$algebra = str_replace('epsilon','zepslon',$algebra);
$algebra = str_replace('upsilon','zupslon',$algebra);
$algebra = preg_replace('!\r\n?!',' ',$algebra);
$algebra = escapeshellarg($algebra);

if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
$algebra = "\"". str_replace('"','\"',$algebra) . "\"";
$cmd = "cd $CFG->dirroot\\$CFG->algebrafilterdirwin & algebra2tex.pl x/2";
$test = `$cmd`;
if ($test != '\frac{x}{2}') {
Expand All @@ -117,7 +117,6 @@ function algebra2tex($algebra) {
}
$cmd = "cd $CFG->dirroot\\$CFG->algebrafilterdirwin & algebra2tex.pl $algebra";
} else {
$algebra = escapeshellarg($algebra);
$cmd = "cd $CFG->dirroot/$CFG->algebrafilterdir; ./algebra2tex.pl x/2";
$test = `$cmd`;
if ($test != '\frac{x}{2}') {
Expand Down Expand Up @@ -206,66 +205,66 @@ function outputText($texexp) {
}

function tex2image($texexp, $md5, $return=false) {
global $CFG;
$error_message1 = "Your system is not configured to run mimeTeX. ";
$error_message1 .= "You need to download the appropriate<br /> executable ";
$error_message1 .= "from <a href=\"http://moodle.org/download/mimetex/\">";
$error_message1 .= "http://moodle.org/download/mimetex/</a>, or obtain the ";
$error_message1 .= "C source<br /> from <a href=\"http://www.forkosh.com/mimetex.zip\">";
$error_message1 .= "http://www.forkosh.com/mimetex.zip</a>, compile it and ";
$error_message1 .= "put the executable into your<br /> moodle/filter/tex/ directory. ";
$error_message1 .= "You also need to edit your moodle/filter/algebra/pix.php file<br />";
$error_message1 .= ' by adding the line<br /><pre> case "' . PHP_OS . "\":\n";
$error_message1 .= " \$cmd = \"\\\\\"\$CFG->dirroot/\$CFG->texfilterdir/";
$error_message1 .= 'mimetex.' . strtolower(PHP_OS) . "\\\\\" -e \\\\\"\$pathname\\\\\" \". escapeshellarg(\$texexp);";
$error_message1 .= "</pre>You also need to add this to your algebradebug.php file.";
global $CFG;
$error_message1 = "Your system is not configured to run mimeTeX. ";
$error_message1 .= "You need to download the appropriate<br /> executable ";
$error_message1 .= "from <a href=\"http://moodle.org/download/mimetex/\">";
$error_message1 .= "http://moodle.org/download/mimetex/</a>, or obtain the ";
$error_message1 .= "C source<br /> from <a href=\"http://www.forkosh.com/mimetex.zip\">";
$error_message1 .= "http://www.forkosh.com/mimetex.zip</a>, compile it and ";
$error_message1 .= "put the executable into your<br /> moodle/filter/tex/ directory. ";
$error_message1 .= "You also need to edit your moodle/filter/algebra/pix.php file<br />";
$error_message1 .= ' by adding the line<br /><pre> case "' . PHP_OS . "\":\n";
$error_message1 .= " \$cmd = \"\\\\\"\$CFG->dirroot/\$CFG->texfilterdir/";
$error_message1 .= 'mimetex.' . strtolower(PHP_OS) . "\\\\\" -e \\\\\"\$pathname\\\\\" \". escapeshellarg(\$texexp);";
$error_message1 .= "</pre>You also need to add this to your algebradebug.php file.";

if ($texexp) {
$texexp = '\Large ' . $texexp;
$lifetime = 86400;
$image = $md5 . ".gif";
$filetype = 'image/gif';
if (!file_exists("$CFG->dataroot/$CFG->algebraimagedir")) {
make_upload_directory($CFG->algebraimagedir);
}
$pathname = "$CFG->dataroot/$CFG->algebraimagedir/$image";
if (file_exists($pathname)) {
unlink($pathname);
}
$commandpath = "";
$cmd = "";
switch (PHP_OS) {
case "Linux":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.linux";
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" ". escapeshellarg($texexp);
break;
case "WINNT":
case "WIN32":
case "Windows":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
$texexp = str_replace('"','\"',$texexp);
$cmd = str_replace(' ','^ ',$commandpath);
$cmd .= " ++ -e \"$pathname\" \"$texexp\"";
break;
case "Darwin":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin";
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" ". escapeshellarg($texexp);
break;
}
if (!$cmd) {
if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex";
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname ". escapeshellarg($texexp);
} else {
error($error_message1);
}
}
system($cmd, $status);
}
if ($return) {
return $image;
}
if ($texexp && file_exists($pathname)) {
if ($texexp) {
$texexp = '\Large ' . $texexp;
$lifetime = 86400;
$image = $md5 . ".gif";
$filetype = 'image/gif';
if (!file_exists("$CFG->dataroot/$CFG->algebraimagedir")) {
make_upload_directory($CFG->algebraimagedir);
}
$pathname = "$CFG->dataroot/$CFG->algebraimagedir/$image";
if (file_exists($pathname)) {
unlink($pathname);
}
$commandpath = "";
$cmd = "";
$texexp = escapeshellarg($texexp);
switch (PHP_OS) {
case "Linux":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.linux";
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" $texexp";
break;
case "WINNT":
case "WIN32":
case "Windows":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
$cmd = str_replace(' ','^ ',$commandpath);
$cmd .= " ++ -e \"$pathname\" $texexp";
break;
case "Darwin":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin";
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" $texexp";
break;
}
if (!$cmd) {
if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex";
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname $texexp";
} else {
error($error_message1);
}
}
system($cmd, $status);
}
if ($return) {
return $image;
}
if ($texexp && file_exists($pathname)) {
$lastmodified = filemtime($pathname);
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
Expand All @@ -275,7 +274,7 @@ function tex2image($texexp, $md5, $return=false) {
header("Content-length: ".filesize($pathname));
header("Content-type: $filetype");
readfile("$pathname");
} else {
} else {
$ecmd = "$cmd 2>&1";
echo `$ecmd` . "<br />\n";
echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
Expand All @@ -301,7 +300,7 @@ function tex2image($texexp, $md5, $return=false) {
echo "mimetex executable $commandpath not found!<br />";
}
echo "Image not found!";
}
}
}

function slasharguments($texexp, $md5) {
Expand Down
3 changes: 1 addition & 2 deletions filter/algebra/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ function algebra_filter ($courseid, $text) {
$algebra = str_replace('epsilon','zepslon',$algebra);
$algebra = str_replace('upsilon','zupslon',$algebra);
$algebra = preg_replace('!\r\n?!',' ',$algebra);
$algebra = escapeshellarg($algebra);
if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
$algebra = "\"". str_replace('"','\"',$algebra) . "\"";
$cmd = "cd $CFG->dirroot\\$CFG->algebrafilterdirwin & algebra2tex.pl $algebra";
} else {
$algebra = escapeshellarg($algebra);
$cmd = "cd $CFG->dirroot/$CFG->algebrafilterdir; ./algebra2tex.pl $algebra";
}
$texexp = `$cmd`;
Expand Down
10 changes: 5 additions & 5 deletions filter/algebra/pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@
$texexp = str_replace('&gt;','>',$texexp);
$texexp = preg_replace('!\r\n?!',' ',$texexp);
$texexp = '\Large ' . $texexp;
$texexp = escapeshellarg($texexp);

if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
$texexp = str_replace('"','\"',$texexp);
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
$cmd = str_replace(' ','^ ',$cmd);
$cmd .= " ++ -e \"$pathname\" -- \"$texexp\"";
$cmd .= " ++ -e \"$pathname\" -- $texexp";
} else if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary

$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname -- ". escapeshellarg($texexp);
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname -- $texexp";

} else { /// Auto-detect the right TeX binary
switch (PHP_OS) {

case "Linux":
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" -- ". escapeshellarg($texexp);
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" -- $texexp";
break;

case "Darwin":
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" -- ". escapeshellarg($texexp);
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" -- $texexp";
break;

default: /// Nothing was found, so tell them how to fix it.
Expand Down
12 changes: 6 additions & 6 deletions filter/tex/pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,29 @@
$texexp = str_replace('&gt;','>',$texexp);
$texexp = preg_replace('!\r\n?!',' ',$texexp);
$texexp = '\Large ' . $texexp;
$texexp = escapeshellarg($texexp);

if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
$texexp = str_replace('"','\"',$texexp);
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
$cmd = str_replace(' ','^ ',$cmd);
$cmd .= " ++ -e \"$pathname\" -- \"$texexp\"";
$cmd .= " ++ -e \"$pathname\" -- $texexp";
} else if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary

$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname -- ". escapeshellarg($texexp);
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname -- $texexp";

} else { /// Auto-detect the right TeX binary
switch (PHP_OS) {

case "Linux":
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" -- ". escapeshellarg($texexp);
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" -- $texexp";
break;

case "Darwin":
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" -- ". escapeshellarg($texexp);
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" -- $texexp";
break;

case "FreeBSD":
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.freebsd\" -e \"$pathname\" ". escapeshellarg($texexp);
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.freebsd\" -e \"$pathname\" $texexp";
break;

default: /// Nothing was found, so tell them how to fix it.
Expand Down
26 changes: 13 additions & 13 deletions filter/tex/texdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,31 @@ function tex2image($texexp, $return=false) {
}
$commandpath = "";
$cmd = "";
$texexp = escapeshellarg($texexp);
switch (PHP_OS) {
case "Linux":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.linux";
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" ". escapeshellarg($texexp);
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" $texexp";
break;
case "WINNT":
case "WIN32":
case "Windows":
case "WIN32":
case "Windows":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
$texexp = str_replace('"','\"',$texexp);
$cmd = str_replace(' ','^ ',$commandpath);
$cmd .= " ++ -e \"$pathname\" \"$texexp\"";
$cmd = str_replace(' ','^ ',$commandpath);
$cmd .= " ++ -e \"$pathname\" $texexp";
break;
case "Darwin":
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin";
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" ". escapeshellarg($texexp);
$cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" $texexp";
break;
}
if (!$cmd) {
if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex";
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname ". escapeshellarg($texexp);
} else {
error($error_message1);
}
if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
$commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex";
$cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname $texexp";
} else {
error($error_message1);
}
}
system($cmd, $status);
}
Expand Down
13 changes: 7 additions & 6 deletions filter/tex/texed.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@
make_upload_directory($CFG->teximagedir);
}
$pathname = "$CFG->dataroot/$CFG->teximagedir/$image";
$texexp = escapeshellarg($texexp);

switch (PHP_OS) {
case "Linux":
system("$CFG->dirroot/$CFG->texfilterdir/mimetex.linux -e $pathname -- ". escapeshellarg($texexp) );
system("$CFG->dirroot/$CFG->texfilterdir/mimetex.linux -e $pathname -- $texexp" );
break;
case "WINNT":
case "WIN32":
case "Windows":
$texexp = str_replace('"','\"',$texexp);
system("$CFG->dirroot/$CFG->texfilterdir/mimetex.exe -e $pathname -- \"$texexp\"");
case "WIN32":
case "Windows":
system("$CFG->dirroot/$CFG->texfilterdir/mimetex.exe -e $pathname -- $texexp");
break;
case "Darwin":
system("$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin -e $pathname -- ". escapeshellarg($texexp) );
system("$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin -e $pathname -- $texexp" );
break;
}
if (file_exists($pathname)) {
Expand Down

0 comments on commit 10ccb7a

Please sign in to comment.