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 if (empty($CFG->textfilters
)) {
11 error ('Filter not enabled!');
13 $filters = explode(',', $CFG->textfilters
);
14 if (array_search('filter/tex', $filters) === FALSE) {
15 error ('Filter not enabled!');
19 $CFG->texfilterdir
= "filter/tex";
20 $CFG->teximagedir
= "filter/tex";
22 $query = urldecode($_SERVER['QUERY_STRING']);
23 error_reporting(E_ALL
);
27 $splitpos = strpos($query,'&')-4;
28 $texexp = substr($query,4,$splitpos);
30 if (strpos($query,'ShowDB') ||
strpos($query,'DeleteDB')) {
31 $texcache = get_record("cache_filters","filter","tex", "md5key", $md5);
33 if (strpos($query,'ShowDB')) {
35 $output = "DB cache_filters entry for $texexp\n";
36 $output .= "id = $texcache->id\n";
37 $output .= "filter = $texcache->filter\n";
38 $output .= "version = $texcache->version\n";
39 $output .= "md5key = $texcache->md5key\n";
40 $output .= "rawtext = $texcache->rawtext\n";
41 $output .= "timemodified = $texcache->timemodified\n";
43 $output = "DB cache_filters entry for $texexp not found\n";
46 if (strpos($query,'DeleteDB')) {
48 $output = "Deleting DB cache_filters entry for $texexp\n";
49 $result = delete_records("cache_filters","id",$texcache->id
);
55 $output .= "Number of records deleted = $result\n";
57 $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n";
60 if (strpos($query,'ShowImage')) {
62 } else if (strpos($query,'SlashArguments')) {
63 slasharguments($texexp);
71 function outputText($texexp) {
72 header("Content-type: text/html");
73 echo "<html><body><pre>\n";
75 $texexp = str_replace('<','<',$texexp);
76 $texexp = str_replace('>','>',$texexp);
77 $texexp = str_replace('"','"',$texexp);
80 echo "No text output available\n\n";
82 echo "</pre></body></html>\n";
85 function tex2image($texexp, $return=false) {
87 $error_message1 = "Your system is not configured to run mimeTeX. ";
88 $error_message1 .= "You need to download the appropriate<br /> executable ";
89 $error_message1 .= "from <a href=\"http://moodle.org/download/mimetex/\">";
90 $error_message1 .= "http://moodle.org/download/mimetex/</a>, or obtain the ";
91 $error_message1 .= "C source<br /> from <a href=\"http://www.forkosh.com/mimetex.zip\">";
92 $error_message1 .= "http://www.forkosh.com/mimetex.zip</a>, compile it and ";
93 $error_message1 .= "put the executable into your<br /> moodle/filter/tex/ directory. ";
94 $error_message1 .= "You also need to edit your moodle/filter/tex/pix.php file<br />";
95 $error_message1 .= ' by adding the line<br /><pre> case "' . PHP_OS
. "\":\n";
96 $error_message1 .= " \$cmd = \"\\\\\"\$CFG->dirroot/\$CFG->texfilterdir/";
97 $error_message1 .= 'mimetex.' . strtolower(PHP_OS
) . "\\\\\" -e \\\\\"\$pathname\\\\\" \". escapeshellarg(\$texexp);";
98 $error_message1 .= "</pre>You also need to add this to your texdebug.php file.";
101 $texexp = '\Large ' . $texexp;
103 $image = md5($texexp) . ".gif";
104 $filetype = 'image/gif';
105 if (!file_exists("$CFG->dataroot/$CFG->teximagedir")) {
106 make_upload_directory($CFG->teximagedir
);
108 $pathname = "$CFG->dataroot/$CFG->teximagedir/$image";
109 if (file_exists($pathname)) {
114 $texexp = escapeshellarg($texexp);
117 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.linux";
118 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" $texexp";
123 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
124 $cmd = str_replace(' ','^ ',$commandpath);
125 $cmd .= " ++ -e \"$pathname\" $texexp";
128 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin";
129 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" $texexp";
133 if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
134 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex";
135 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname $texexp";
137 error($error_message1);
140 system($cmd, $status);
145 if ($texexp && file_exists($pathname)) {
146 $lastmodified = filemtime($pathname);
147 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
148 header("Expires: " . gmdate("D, d M Y H:i:s", time() +
$lifetime) . " GMT");
149 header("Cache-control: max_age = $lifetime"); // a day
151 header("Content-disposition: inline; filename=$image");
152 header("Content-length: ".filesize($pathname));
153 header("Content-type: $filetype");
154 readfile("$pathname");
157 echo `
$ecmd`
. "<br />\n";
158 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
160 echo "Status corresponds to illegal instruction<br />\n";
161 } else if ($status == 11) {
162 echo "Status corresponds to bus error<br />\n";
163 } else if ($status == 22) {
164 echo "Status corresponds to abnormal termination<br />\n";
166 if (file_exists($commandpath)) {
167 echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />";
168 echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
169 if (function_exists("md5_file")) {
170 echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
172 $handle = fopen($commandpath,"rb");
173 $contents = fread($handle,16384);
175 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
178 echo "mimetex executable $commandpath not found!<br />";
180 echo "Image not found!";
184 function slasharguments($texexp) {
186 $admin = $CFG->wwwroot
. '/' . $CFG->admin
. '/config.php';
187 $image = tex2image($texexp,true);
188 echo "<p>If the following image displays correctly, set your ";
189 echo "<a href=\"$admin\" target=\"_blank\">Administration->Configuration->Variables</a> ";
190 echo "setting for slasharguments to file.php/1/pic.jpg: ";
191 echo "<img src=\"pix.php/$image\" align=\"absmiddle\"></p>\n";
192 echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
193 echo "It should display correctly as ";
194 echo "<img src=\"pix.php?file=$image\" align=\"absmiddle\"></p>\n";
195 echo "<p>If neither equation image displays correctly, please seek ";
196 echo "further help at moodle.org at the ";
197 echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&username=guest\" target=\"_blank\">";
198 echo "Mathematics Tools Forum</a></p>";
204 <head
><title
>TeX Filter Debugger
</title
></head
>
206 <p
>Please enter an algebraic expression
<b
>without
</b
> any surrounding $$ into
207 the text box below
. (Click
<a href
="#help">here
for help
.</a
>)
208 <form action
="texdebug.php" method
="get"
209 target
="inlineframe">
211 <input type
="text" name
="tex" size
="50"
212 value
="f(x)=\Bigint_{-\infty}^x~e^{-t^2}dt" />
215 <li
>First click on this button
<input type
="submit" name
="ShowDB" value
="Show DB Entry" />
216 to see the cache_filters database entry
for this expression
.</li
>
217 <li
>If the database entry looks corrupt
, click on this button to delete it
:
218 <input type
="submit" name
="DeleteDB" value
="Delete DB Entry" /></li
>
219 <li
>Then click on this button
<input type
="submit" name
="ShowImage" value
="Show Image" />
220 to show a graphic image of the algebraic expression
.</li
>
221 <li
>Finally check your slash arguments setting
222 <input type
="submit" name
="SlashArguments" value
="Check Slash Arguments" /></li
>
224 </form
> <br
/> <br
/>
226 <iframe name
="inlineframe" align
="middle" width
="80%" height
="200">
227 <
;p
>
;Something is wrong
...<
;/p
>
;
232 <h2
>Debugging Help
</h2
>
234 <p
>First a brief overview of how the TeX filter works
. The TeX filter first
235 searches the database cache_filters table to see
if this TeX expression had been
236 processed before
. If not
, it adds a DB entry
for that expression
. It then
237 replaces the TeX expression by an
<
;img src
="
;.../filter
/tex
/pix
.php
..."
;>
;
238 tag
. The filter
/tex
/pix
.php script then searches the database to find an
239 appropriate gif image file
for that expression
and to create one
if it doesn
't exist.
240 Here are a few common things that can go wrong and some suggestions on how
241 you might try to fix them.</p>
243 <li>Something had gone wrong on a previous occasion when the filter tried to
244 process this expression. Then the database entry for that expression contains
245 a bad TeX expression in the rawtext field (usually blank). You can fix this
246 by clicking on "Delete DB Entry"</li>
247 <li>The TeX to gif image conversion process does not work. If your server is
248 running Unix, a likely cause is that the mimetex binary you are using is
249 incompatible with your operating system. You can try compiling it from the
250 C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
251 http://www.forkosh.com/mimetex.zip</a>, or looking for an appropriate
252 binary at <a href="http://moodle.org/download/mimetex/">
253 http://moodle.org/download/mimetex/</a>. You may then also need to
254 edit your moodle/filter/tex/pix.php file to add
255 <br /><?PHP echo "case "" . PHP_OS . "":" ;?><br ?> to the list of operating systems
256 in the switch (PHP_OS) statement. Windows users may have a problem properly
257 unzipping mimetex.exe. Make sure that mimetex.exe is is <b>PRECISELY</b>
258 433152 bytes in size. If not, download a fresh copy from
259 <a href="http://moodle.org/download/mimetex/windows/mimetex.exe">
260 http://moodle.org/download/mimetex/windows/mimetex.exe</a>.
261 Another possible problem which may affect
262 both Unix and Windows servers is that the web server doesn't have execute permission
263 on the mimetex binary
. In that
case change permissions accordingly
</li
>