'^',
'%20' => ' ',
'%3E' => '>',
'%3C' => '<',
'%E4' => 'ä',
'%F6' => 'ö',
'%FC' => 'ü',
'%C4' => 'Ä',
'%D6' => 'Ö',
'%DC' => 'Ü',
'%DF' => 'ß',
'%25' => '\%',
'%22' => '"',
// additional translations for Opera (is this UTF8?)
'%C3%A4' => 'ä',
'%C3%B6' => 'ö',
'%C3%BC' => 'ü',
'%C3%9F' => 'ß',
'%C3%84' => 'Ä',
'%C3%96' => 'Ö',
'%C3%9C' => 'Ü',
'%C2%B' => '^',
'%27' => '\'',
'%5C' => '\\',
// escape any remaining %, we don't want comments
'%' => '\%',
// additional translations for Safari
'%7B' => '{',
'%7D' => '}',
'%7C' => '|',
'%5E' => '^',
'%5D' => ']',
'%5B' => '[',
// html linebreak to newline
'%3Cbr%20/%3E' => '\\\\',
'
' => '\\\\',
'
' => '\\\\',
'
' => '\\\\',
'
' => '\\\\',
);
$new_commands = '
\newcommand{\dd}{\mathrm{d}}
\newcommand{\dx}{\mathrm{d}x}
\newcommand{\Ra}{\Rightarrow}
\newcommand{\longRa}{\Longrightarrow}
\newcommand{\Lra}{\Leftrightarrow}
\newcommand{\longLra}{\Longleftrightarrow}
\newcommand{\ra}{\rightarrow}
\newcommand{\longra}{\longrightarrow}
\newcommand{\nea}{\nearrow}
\newcommand{\sea}{\searrow}
';
function register_error($message) {
global $chars, $errors, $ret;
// save error messages in $errors
$ret = 1;
$errors[] = $message;
if (strlen($message) > $chars)
$chars = strlen($message);
}
function tex_error($line) {
if ($line[0] == '!') {
register_error($line);
return true;
}
return false;
}
function output_errors($errors, $longest_line) {
if(!empty($errors))
{
$height = ImageFontHeight(5);
$width = ImageFontWidth(5);
$strwidth = $width * $longest_line;
$strheight = $height;
$img_width = $strwidth;
$img_height = $strheight * count($errors);
$id = ImageCreate($img_width,$img_height);
$black = ImageColorAllocate($id, 0, 0, 0);
$white = ImageColorAllocate($id, 255, 255, 255);
ImageFill($id, 0,0,$white);
ImageRectangle($id, 0, 0, $img_width-1, $img_height-1, $black);
for ($i=0; $i
";
}
// build md5 of formula
$md5 = md5($f . $mode);
// build md5 dir
$md5dir = $tmpdir.'/'.$md5[0].'/'.$md5[1].'/'.$md5[2].'/'.$md5[3].'/'.$md5[4].'/';
// do latex and dvipng binaries exist?
// not needed here
/*if (strcmp(`which latex`, '') == 0) {
register_error('latex binary nicht gefunden');
}
elseif (strcmp(`which dvipng`, '') == 0) {
register_error('dvipng binary nicht gefunden');
}*/
if ($f == '') { // no LaTeX call needed
register_error('Keine Funktion angegeben'); // print error
}
elseif (!file_exists($md5dir) && (@!mkdir($md5dir,0777,true)))
{
register_error("Konnte Temp-Verzeichnis $md5dir nicht anlegen!");
}
elseif (@!chdir($md5dir)) {
register_error("Konnte nicht ins Temp-Verzeichnis $md5dir wechseln!");
}
else { // pass expression to the LaTeX interpreter
if ($debug) {
echo "MD5: $md5
";
echo "Outputpfad: $tmpdir
";
}
// look for old .gif files which are still in the cache
// disabled for performance and because no gifs are there anymore
/*if (file_exists($md5 . '.gif')) {
if ($debug) {
echo "Datei existiert
";
}
else {
header('Content-Type: image/gif');
header('Expires: '.date('r', time()+60*60*24*7));
readfile($md5 . '.gif');
}
}
else*/
if (file_exists($md5 . '.png')) {
if ($debug) {
echo "Datei existiert
";
}
else {
header('Content-Type: image/png');
header('Expires: '.date('r', time()+60*60*24*7));
readfile($md5 . '.png');
}
}
else { // no .gif or png existing yet
if ($debug) {
echo "Datei existiert noch nicht
";
}
$file = $md5 . '.tex';
$com = '
\documentclass[12pt]{amsart}
\usepackage[latin1]{inputenc}
\usepackage{amsmath,amssymb,color,polynom}
\usepackage[all]{xy}
\pagestyle{empty}
\thispagestyle{empty}
'.$new_commands.'
\begin{document}
\boldmath
';
if ($mode == 'inline')
$com .= '$' . str_replace('&', '', $f) . '$' . "\n";
else if ($mode == 'align') {
$com .= "\\begin{align*}\n";
$com .= "$f\n";
$com .= "\\end{align*}\n";
} else {
$com .= "\\begin{eqnarray*}\n";
$com .= trim($f) . "\n";
$com .= "\\end{eqnarray*}\n";
}
$com .= "\\end{document}";
if ($debug) {
echo '
LaTeX-Output:
' . nl2br($com);
}
$handle = fopen("$md5.tex", "w");
fwrite($handle, $com);
fclose($handle);
// openin_any=p, don't allow any files to be opened which are not in the current path (eg. /etc/passwd)
$process = popen("openin_any=p /usr/bin/latex -halt-on-error $md5.tex", 'r');
do {
$data = fread($process, 8192);
$texlog .= $data;
} while (strlen($data) > 0);
$ret = pclose($process);
if($debug) echo 'Texlog: '.$texlog.'
';
if ($ret == 0) { // latex call worked, now go on with dvips and convert
//$process = popen("dvips -R -E $md5.dvi -o - 2> /dev/null | convert -comment \"powered by Matheboard.de, (c) 2004 Georg Sorst\" -antialias -transparent \"#ffffff\" -quality 100 -density 100x100 - $md5.gif", 'r');
$process = popen("/usr/bin/dvipng --picky -bg transparent -Q 10 -T tight -o $md5.png $md5.dvi", 'r');
$ret = pclose($process);
if ($ret == 0) {
// delete temporary files
if (file_exists($md5 . '.tex')) unlink($md5 . '.tex');
if (file_exists($md5 . '.log')) unlink($md5 . '.log');
if (file_exists($md5 . '.aux')) unlink($md5 . '.aux');
if (file_exists($md5 . '.dvi')) unlink($md5 . '.dvi');
}
}
else { // latex call failed (latex has thrown errors)
$lines = explode ("\n", $texlog);
array_walk($lines, 'tex_error');
} // end of end latex error
} // end of .gif not existing yet
} // end of passing expression
if (!$debug) {
if ($ret == 0) {
header('Content-Type: image/png');
header('Expires: '.date('r', time()+60*60*24*7));
readfile($md5 . '.png');
}
else {
output_errors($errors, $chars);
}
}
else {
echo 'Errors:
';
echo '
'; print_r($errors); ''; } ?>