Match backup version to main version
[moodle.git] / filter / algebra / pix.php
blobffb8cb43ca3c842c7c5aa16ecc09f37f7aa259bd
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 error_reporting(E_ALL);
16 $lifetime = 86400;
17 if (isset($file)) { // workaround for situations where / syntax doesn't work
18 $pathinfo = '/' . $file;
19 } else {
20 $pathinfo = get_slash_arguments("pix.php");
23 if (! $args = parse_slash_arguments($pathinfo)) {
24 error("No valid arguments supplied");
27 $numargs = count($args);
29 if ($numargs == 1) {
30 $image = $args[0];
31 $pathname = "$CFG->dataroot/$CFG->algebraimagedir/$image";
32 $filetype = "image/gif";
33 } else {
34 error("No valid arguments supplied");
38 if (!file_exists($pathname)) {
39 $md5 = str_replace('.gif','',$image);
40 if ($texcache = get_record("cache_filters", "filter", "algebra", "md5key", $md5)) {
41 if (!file_exists("$CFG->dataroot/$CFG->algebraimagedir")) {
42 make_upload_directory($CFG->algebraimagedir);
45 $texexp = $texcache->rawtext;
46 $texexp = str_replace('&lt;','<',$texexp);
47 $texexp = str_replace('&gt;','>',$texexp);
48 $texexp = preg_replace('!\r\n?!',' ',$texexp);
49 $texexp = '\Large ' . $texexp;
50 switch (PHP_OS) {
51 case "Linux":
52 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" ". escapeshellarg($texexp);
53 break;
54 case "WINNT":
55 case "WIN32":
56 case "Windows":
57 $texexp = str_replace('"','\"',$texexp);
58 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
59 $cmd = str_replace(' ','^ ',$cmd);
60 $cmd .= " ++ -e \"$pathname\" \"$texexp\"";
61 break;
62 case "Darwin":
63 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" ". escapeshellarg($texexp);
64 break;
65 default: /// To allow drop-in binaries for other platforms
66 if (!is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) {
67 echo "Make sure you have an appropriate MimeTeX binary here:\n\n";
68 echo " $CFG->dirroot/$CFG->texfilterdir/mimetex\n\n";
69 echo "and that it has the right permissions set on it as executable program.\n\n";
70 echo "You can get the latest binaries for your ".PHP_OS." platform from: \n\n";
71 echo " http://moodle.org/download/mimetex/";
72 exit;
74 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname ". escapeshellarg($texexp);
75 break;
77 system($cmd, $status);
81 if (file_exists($pathname)) {
82 $lastmodified = filemtime($pathname);
83 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
84 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
85 header("Cache-control: max_age = $lifetime"); // a day
86 header("Pragma: ");
87 header("Content-disposition: inline; filename=$image");
88 header("Content-length: ".filesize($pathname));
89 header("Content-type: $filetype");
90 readfile("$pathname");
91 } else {
92 echo "The shell command<br>$cmd<br>returned status = $status<br>\n";
93 echo "Image not found!<br>";
94 echo "Please try the <a href=\"$CFG->wwwroot/filter/algebra/algebradebug.php\">debugging script</a>";
97 exit;