textfilters)) { error ('Filter not enabled!'); } else { $filters = explode(',', $CFG->textfilters); if (array_search('filter/tex', $filters) === FALSE) { error ('Filter not enabled!'); } } $CFG->texfilterdir = "filter/tex"; $CFG->teximagedir = "filter/tex"; $query = urldecode($_SERVER['QUERY_STRING']); error_reporting(E_ALL); if ($query) { $output = $query; $splitpos = strpos($query,'&')-4; $texexp = substr($query,4,$splitpos); $md5 = md5($texexp); if (strpos($query,'ShowDB') || strpos($query,'DeleteDB')) { $texcache = get_record("cache_filters","filter","tex", "md5key", $md5); } if (strpos($query,'ShowDB')) { if ($texcache) { $output = "DB cache_filters entry for $texexp\n"; $output .= "id = $texcache->id\n"; $output .= "filter = $texcache->filter\n"; $output .= "version = $texcache->version\n"; $output .= "md5key = $texcache->md5key\n"; $output .= "rawtext = $texcache->rawtext\n"; $output .= "timemodified = $texcache->timemodified\n"; } else { $output = "DB cache_filters entry for $texexp not found\n"; } } if (strpos($query,'DeleteDB')) { if ($texcache) { $output = "Deleting DB cache_filters entry for $texexp\n"; $result = delete_records("cache_filters","id",$texcache->id); if ($result) { $result = 1; } else { $result = 0; } $output .= "Number of records deleted = $result\n"; } else { $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n"; } } if (strpos($query,'ShowImage')) { tex2image($texexp); } else if (strpos($query,'SlashArguments')) { slasharguments($texexp); } else { outputText($output); } exit; } function outputText($texexp) { header("Content-type: text/html"); echo "
\n";
if ($texexp) {
$texexp = str_replace('<','<',$texexp);
$texexp = str_replace('>','>',$texexp);
$texexp = str_replace('"','"',$texexp);
echo "$texexp\n\n";
} else {
echo "No text output available\n\n";
}
echo "\n";
}
function tex2image($texexp, $return=false) {
global $CFG;
$error_message1 = "Your system is not configured to run mimeTeX. ";
$error_message1 .= "You need to download the appropriate case "' . PHP_OS . "\":\n";
$error_message1 .= " \$cmd = \"\\\\\"\$CFG->dirroot/\$CFG->texfilterdir/";
$error_message1 .= 'mimetex.' . strtolower(PHP_OS) . "\\\\\" -e \\\\\"\$pathname\\\\\" \". escapeshellarg(\$texexp);";
$error_message1 .= "You also need to add this to your texdebug.php file.";
if ($texexp) {
$texexp = '\Large ' . $texexp;
$lifetime = 86400;
$image = md5($texexp) . ".gif";
$filetype = 'image/gif';
if (!file_exists("$CFG->dataroot/$CFG->teximagedir")) {
make_upload_directory($CFG->teximagedir);
}
$pathname = "$CFG->dataroot/$CFG->teximagedir/$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");
header("Cache-control: max_age = $lifetime"); // a day
header("Pragma: ");
header("Content-disposition: inline; filename=$image");
header("Content-length: ".filesize($pathname));
header("Content-type: $filetype");
readfile("$pathname");
} else {
$ecmd = "$cmd 2>&1";
echo `$ecmd` . "If the following image displays correctly, set your ";
echo "Administration->Configuration->Variables ";
echo "setting for slasharguments to file.php/1/pic.jpg: ";
echo "
Otherwise set it to file.php?file=/1/pic.jpg ";
echo "It should display correctly as ";
echo "
If neither equation image displays correctly, please seek "; echo "further help at moodle.org at the "; echo ""; echo "Mathematics Tools Forum
"; } ?>Please enter an algebraic expression without any surrounding $$ into the text box below. (Click here for help.)
First a brief overview of how the TeX filter works. The TeX filter first searches the database cache_filters table to see if this TeX expression had been processed before. If not, it adds a DB entry for that expression. It then replaces the TeX expression by an <img src=".../filter/tex/pix.php..."> tag. The filter/tex/pix.php script then searches the database to find an appropriate gif image file for that expression and to create one if it doesn't exist. Here are a few common things that can go wrong and some suggestions on how you might try to fix them.