Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / filter / tex / texdebug.php
blob97fe91ad4e2a7e4c13b5a076169d22d72e731e2a
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 if (empty($CFG->textfilters)) {
11 error ('Filter not enabled!');
12 } else {
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);
25 if ($query) {
26 $output = $query;
27 $splitpos = strpos($query,'&')-4;
28 $texexp = substr($query,4,$splitpos);
29 $md5 = md5($texexp);
30 if (strpos($query,'ShowDB') || strpos($query,'DeleteDB')) {
31 $texcache = get_record("cache_filters","filter","tex", "md5key", $md5);
33 if (strpos($query,'ShowDB')) {
34 if ($texcache) {
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";
42 } else {
43 $output = "DB cache_filters entry for $texexp not found\n";
46 if (strpos($query,'DeleteDB')) {
47 if ($texcache) {
48 $output = "Deleting DB cache_filters entry for $texexp\n";
49 $result = delete_records("cache_filters","id",$texcache->id);
50 if ($result) {
51 $result = 1;
52 } else {
53 $result = 0;
55 $output .= "Number of records deleted = $result\n";
56 } else {
57 $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n";
60 if (strpos($query,'ShowImage')) {
61 tex2image($texexp);
62 } else if (strpos($query,'SlashArguments')) {
63 slasharguments($texexp);
64 } else {
65 outputText($output);
67 exit;
71 function outputText($texexp) {
72 header("Content-type: text/html");
73 echo "<html><body><pre>\n";
74 if ($texexp) {
75 $texexp = str_replace('<','&lt;',$texexp);
76 $texexp = str_replace('>','&gt;',$texexp);
77 $texexp = str_replace('"','&quot;',$texexp);
78 echo "$texexp\n\n";
79 } else {
80 echo "No text output available\n\n";
82 echo "</pre></body></html>\n";
85 function tex2image($texexp, $return=false) {
86 global $CFG;
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.";
100 if ($texexp) {
101 $texexp = '\Large ' . $texexp;
102 $lifetime = 86400;
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)) {
110 unlink($pathname);
112 $commandpath = "";
113 $cmd = "";
114 $texexp = escapeshellarg($texexp);
115 switch (PHP_OS) {
116 case "Linux":
117 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.linux";
118 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" $texexp";
119 break;
120 case "WINNT":
121 case "WIN32":
122 case "Windows":
123 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
124 $cmd = str_replace(' ','^ ',$commandpath);
125 $cmd .= " ++ -e \"$pathname\" $texexp";
126 break;
127 case "Darwin":
128 $commandpath="$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin";
129 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" $texexp";
130 break;
132 if (!$cmd) {
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";
136 } else {
137 error($error_message1);
140 system($cmd, $status);
142 if ($return) {
143 return $image;
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
150 header("Pragma: ");
151 header("Content-disposition: inline; filename=$image");
152 header("Content-length: ".filesize($pathname));
153 header("Content-type: $filetype");
154 readfile("$pathname");
155 } else {
156 $ecmd = "$cmd 2>&1";
157 echo `$ecmd` . "<br />\n";
158 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
159 if ($status == 4) {
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 />";
171 } else {
172 $handle = fopen($commandpath,"rb");
173 $contents = fread($handle,16384);
174 fclose($handle);
175 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
177 } else {
178 echo "mimetex executable $commandpath not found!<br />";
180 echo "Image not found!";
184 function slasharguments($texexp) {
185 global $CFG;
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>";
203 <html>
204 <head><title>TeX Filter Debugger</title></head>
205 <body>
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">
210 <center>
211 <input type="text" name="tex" size="50"
212 value="f(x)=\Bigint_{-\infty}^x~e^{-t^2}dt" />
213 </center>
214 <ol>
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>
223 </ol>
224 </form> <br /> <br />
225 <center>
226 <iframe name="inlineframe" align="middle" width="80%" height="200">
227 &lt;p&gt;Something is wrong...&lt;/p&gt;
228 </iframe>
229 </center> <br />
230 <hr />
231 <a name="help">
232 <h2>Debugging Help</h2>
233 </a>
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 &lt;img src=&quot;.../filter/tex/pix.php...&quot;&gt;
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>
242 <ol>
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 &quot;Delete DB Entry&quot;</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 &quot;" . PHP_OS . "&quot;:" ;?><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>
264 </ol>
265 </body>
266 </html>