function tex_filter_get_cmd($pathname, $texexp) { $texexp = tex_sanitize_formula($texexp); $texexp = escapeshellarg($texexp); $executable = tex_filter_get_executable(false); if (PHP_OS == "WINNT" || PHP_OS == "WIN32" || PHP_OS == "Windows") { $executable = str_replace(' ', '^ ', $executable); return "{$executable} ++ -e \"{$pathname}\" -- {$texexp}"; } else { return "\"{$executable}\" -e \"{$pathname}\" -- {$texexp}"; } }
/** * Turn the bit of TeX into a valid latex document * @param string $forumula the TeX formula * @param int $fontsize the font size * @return string the latex document */ function construct_latex_document($formula, $fontsize = 12) { global $CFG; $formula = tex_sanitize_formula($formula); // $fontsize don't affects to formula's size. $density can change size $doc = "\\documentclass[{$fontsize}pt]{article}\n"; $doc .= $CFG->filter_tex_latexpreamble; $doc .= "\\pagestyle{empty}\n"; $doc .= "\\begin{document}\n"; //dlnsk $doc .= "$ {$formula} $\n"; if (preg_match("/^[[:space:]]*\\\\begin\\{(gather|align|alignat|multline).?\\}/i", $formula)) { $doc .= "{$formula}\n"; } else { $doc .= "\$ {$formula} \$\n"; } $doc .= "\\end{document}\n"; return $doc; }
} $CFG->texfilterdir = "filter/tex"; $CFG->teximagedir = "filter/tex"; error_reporting(E_ALL); $texexp = urldecode($_SERVER['QUERY_STRING']); $texexp = str_replace('formdata=', '', $texexp); if ($texexp) { //$texexp = stripslashes($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}"; $texexp = tex_sanitize_formula($texexp); $texexp = escapeshellarg($texexp); switch (PHP_OS) { case "Linux": system("{$CFG->dirroot}/{$CFG->texfilterdir}/mimetex.linux -e {$pathname} -- {$texexp}"); break; case "WINNT": 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} -- {$texexp}"); break; } if (file_exists($pathname)) {
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<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/tex/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 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 = tex_sanitize_formula($texexp); $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}` . "<br />\n"; echo "The shell command<br />{$cmd}<br />returned status = {$status}<br />\n"; if ($status == 4) { echo "Status corresponds to illegal instruction<br />\n"; } else { if ($status == 11) { echo "Status corresponds to bus error<br />\n"; } else { if ($status == 22) { echo "Status corresponds to abnormal termination<br />\n"; } } } if (file_exists($commandpath)) { echo "File size of mimetex executable {$commandpath} is " . filesize($commandpath) . "<br />"; echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />"; if (function_exists("md5_file")) { echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />"; } else { $handle = fopen($commandpath, "rb"); $contents = fread($handle, 16384); fclose($handle); echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />"; } } else { echo "mimetex executable {$commandpath} not found!<br />"; } echo "Image not found!"; } }