Initialise some variables $cmd and $status - thanks Petr
[moodle.git] / filter / algebra / pix.php
blob3be6fa71d3edaddaeb99a29010f5e49c14bb23c1
1 <?PHP // $Id$
2 // This function fetches math. images from the data directory
3 // If not, it obtains the corresponding TeX expression from the cache_tex db table
4 // and uses mimeTeX to create the image file
6 $nomoodlecookie = true; // Because it interferes with caching
8 require_once("../../config.php");
10 $CFG->algebrafilterdir = "filter/algebra";
11 $CFG->texfilterdir = "filter/tex";
12 $CFG->algebraimagedir = "filter/algebra";
14 $cmd = ''; // Initialise these variables
15 $status = '';
17 error_reporting(E_ALL);
19 $lifetime = 86400;
20 if (isset($file)) { // workaround for situations where / syntax doesn't work
21 $pathinfo = '/' . $file;
22 } else {
23 $pathinfo = get_slash_arguments("pix.php");
26 if (! $args = parse_slash_arguments($pathinfo)) {
27 error("No valid arguments supplied");
30 $numargs = count($args);
32 if ($numargs == 1) {
33 $image = $args[0];
34 $pathname = "$CFG->dataroot/$CFG->algebraimagedir/$image";
35 $filetype = "image/gif";
36 } else {
37 error("No valid arguments supplied");
41 if (!file_exists($pathname)) {
42 $md5 = str_replace('.gif','',$image);
43 if ($texcache = get_record("cache_filters", "filter", "algebra", "md5key", $md5)) {
44 if (!file_exists("$CFG->dataroot/$CFG->algebraimagedir")) {
45 make_upload_directory($CFG->algebraimagedir);
48 $texexp = $texcache->rawtext;
49 $texexp = str_replace('&lt;','<',$texexp);
50 $texexp = str_replace('&gt;','>',$texexp);
51 $texexp = preg_replace('!\r\n?!',' ',$texexp);
52 $texexp = '\Large ' . $texexp;
54 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
55 $texexp = str_replace('"','\"',$texexp);
56 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
57 $cmd = str_replace(' ','^ ',$cmd);
58 $cmd .= " ++ -e \"$pathname\" \"$texexp\"";
59 } else if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
61 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname ". escapeshellarg($texexp);
63 } else { /// Auto-detect the right TeX binary
64 switch (PHP_OS) {
66 case "Linux":
67 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" ". escapeshellarg($texexp);
68 break;
70 case "Darwin":
71 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" ". escapeshellarg($texexp);
72 break;
74 default: /// Nothing was found, so tell them how to fix it.
75 echo "Make sure you have an appropriate MimeTeX binary here:\n\n";
76 echo " $CFG->dirroot/$CFG->texfilterdir/mimetex\n\n";
77 echo "and that it has the right permissions set on it as executable program.\n\n";
78 echo "You can get the latest binaries for your ".PHP_OS." platform from: \n\n";
79 echo " http://moodle.org/download/mimetex/";
80 exit;
81 break;
84 system($cmd, $status);
88 if (file_exists($pathname)) {
89 $lastmodified = filemtime($pathname);
90 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
91 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
92 header("Cache-control: max_age = $lifetime"); // a day
93 header("Pragma: ");
94 header("Content-disposition: inline; filename=$image");
95 header("Content-length: ".filesize($pathname));
96 header("Content-type: $filetype");
97 readfile("$pathname");
98 } else {
99 echo "The shell command<br>$cmd<br>returned status = $status<br>\n";
100 echo "Image not found!<br>";
101 echo "Please try the <a href=\"$CFG->wwwroot/filter/algebra/algebradebug.php\">debugging script</a>";
104 exit;