2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This function fetches math. images from the data directory
19 * If not, it obtains the corresponding TeX expression from the cache_tex db table
20 * and uses mimeTeX to create the image file
24 * @copyright 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu
25 * Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once("../../config.php");
31 if (!filter_is_enabled('tex')) {
32 print_error('filternotenabled');
35 require_once($CFG->libdir
.'/filelib.php');
36 require_once($CFG->dirroot
.'/filter/tex/lib.php');
37 require_once($CFG->dirroot
.'/filter/tex/latex.php');
39 $action = optional_param('action', '', PARAM_ALPHA
);
40 $texexp = optional_param('tex', '', PARAM_RAW
);
43 require_capability('moodle/site:config', context_system
::instance(), $USER->id
); /// Required cap to run this. MDL-18552
44 if ($action ||
$texexp) {
50 // look up in cache if required
51 if ($action=='ShowDB' or $action=='DeleteDB') {
53 $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5));
56 // Action: Show DB Entry
57 if ($action=='ShowDB') {
59 $output = "DB cache_filters entry for $texexp\n";
60 $output .= "id = $texcache->id\n";
61 $output .= "filter = $texcache->filter\n";
62 $output .= "version = $texcache->version\n";
63 $output .= "md5key = $texcache->md5key\n";
64 $output .= "rawtext = $texcache->rawtext\n";
65 $output .= "timemodified = $texcache->timemodified\n";
67 $output = "DB cache_filters entry for $texexp not found\n";
71 // Action: Delete DB Entry
72 if ($action=='DeleteDB') {
74 $output = "Deleting DB cache_filters entry for $texexp\n";
75 $result = $DB->delete_records("cache_filters", array("id"=>$texcache->id
));
81 $output .= "Number of records deleted = $result\n";
83 $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n";
88 if ($action=='ShowImageMimetex') {
92 // Action: Check Slasharguments
93 if ($action=='SlashArguments') {
94 slasharguments($texexp);
98 // Action: Show Tex command line output
99 if ($action=='ShowImageTex') {
100 TexOutput($texexp, true);
104 // Action: Show Tex command line output
105 if ($action=='ShowOutputTex') {
109 echo "Can not output detailed information due to security concerns, please turn on debug mode first.";
114 if (!empty($action)) {
118 // nothing more to do if there was any action
119 if (!empty($action)) {
124 function outputText($texexp) {
125 header("Content-type: text/html; charset=utf-8");
126 echo "<html><body><pre>\n";
128 echo s($texexp)."\n\n";
130 echo "No text output available\n\n";
132 echo "</pre></body></html>\n";
135 function tex2image($texexp, $return=false) {
139 echo 'No tex expresion specified';
143 $image = md5($texexp) . ".gif";
144 $filetype = 'image/gif';
145 if (!file_exists("$CFG->dataroot/filter/tex")) {
146 make_upload_directory("filter/tex");
148 $pathname = "$CFG->dataroot/filter/tex/$image";
149 if (file_exists($pathname)) {
153 $texexp = '\Large '.$texexp;
154 $commandpath = filter_tex_get_executable(true);
155 $cmd = filter_tex_get_cmd($pathname, $texexp);
156 system($cmd, $status);
162 if (file_exists($pathname)) {
163 send_file($pathname, $image);
165 } else if (debugging()) {
167 echo `
$ecmd`
. "<br />\n";
168 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
170 echo "Status corresponds to illegal instruction<br />\n";
171 } else if ($status == 11) {
172 echo "Status corresponds to bus error<br />\n";
173 } else if ($status == 22) {
174 echo "Status corresponds to abnormal termination<br />\n";
176 if (file_exists($commandpath)) {
177 echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />";
178 echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
179 if (function_exists("md5_file")) {
180 echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
182 $handle = fopen($commandpath,"rb");
183 $contents = fread($handle,16384);
185 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
188 echo "mimetex executable $commandpath not found!<br />";
190 echo "Image not found!";
192 echo "Can not output detailed information due to security concerns, please turn on debug mode first.";
197 // test Tex/Ghostscript output - command execution only
198 function TexOutput($expression, $graphic=false) {
202 $latex = new latex();
204 // first check if it is likely to work at all
205 $output .= "<h3>Checking executables</h3>\n";
206 $executablesexist = true;
207 $pathlatex = trim(get_config('filter_tex', 'pathlatex'), " '\"");
208 if (is_file($pathlatex)) {
209 $output .= "latex executable ($pathlatex) is readable<br />\n";
211 $executablesexist = false;
212 $output .= "<b>Error:</b> latex executable ($pathlatex) is not readable<br />\n";
214 $pathdvips = trim(get_config('filter_tex', 'pathdvips'), " '\"");
215 if (is_file($pathdvips)) {
216 $output .= "dvips executable ($pathdvips) is readable<br />\n";
218 $executablesexist = false;
219 $output .= "<b>Error:</b> dvips executable ($pathdvips) is not readable<br />\n";
221 $pathconvert = trim(get_config('filter_tex', 'pathconvert'), " '\"");
222 if (is_file($pathconvert)) {
223 $output .= "convert executable ($pathconvert) is readable<br />\n";
225 $executablesexist = false;
226 $output .= "<b>Error:</b> convert executable ($pathconvert) is not readable<br />\n";
228 $pathdvisvgm = trim(get_config('filter_tex', 'pathdvisvgm'), " '\"");
229 if (is_file($pathdvisvgm)) {
230 $output .= "dvisvgm executable ($pathdvisvgm) is readable<br />\n";
232 $executablesexist = false;
233 $output .= "<b>Error:</b> dvisvgm executable ($pathdvisvgm) is not readable<br />\n";
236 // knowing that it might work..
237 $md5 = md5($expression);
238 $output .= "<p>base filename for expression is '$md5'</p>\n";
241 $tex = "$md5.tex"; // Absolute paths won't work with openin_any = p setting.
242 $dvi = "$latex->temp_dir/$md5.dvi";
243 $ps = "$latex->temp_dir/$md5.ps";
244 $convertformat = get_config('filter_tex', 'convertformat');
245 $img = "$latex->temp_dir/$md5.{$convertformat}";
247 // Change directory to temp dir so that we can work with relative paths.
248 chdir($latex->temp_dir
);
250 // put the expression as a file into the temp area
251 $expression = html_entity_decode($expression);
252 $output .= "<p>Processing TeX expression:</p><pre>$expression</pre>\n";
253 $doc = $latex->construct_latex_document($expression);
254 $fh = fopen($tex, 'w');
258 // step 1: latex command
259 $pathlatex = escapeshellarg($pathlatex);
260 $cmd = "$pathlatex --interaction=nonstopmode --halt-on-error $tex";
261 $output .= execute($cmd);
263 // step 2: dvips command
264 $pathdvips = escapeshellarg($pathdvips);
265 $cmd = "$pathdvips -E $dvi -o $ps";
266 $output .= execute($cmd);
268 // Step 3: Set convert or dvisvgm command.
269 if ($convertformat == 'svg') {
270 $pathdvisvgm = escapeshellarg($pathdvisvgm);
271 $cmd = "$pathdvisvgm -E $ps -o $img";
273 $pathconvert = escapeshellarg($pathconvert);
274 $cmd = "$pathconvert -density 240 -trim $ps $img ";
276 $output .= execute($cmd);
280 } else if (file_exists($img)) {
281 send_file($img, "$md5.{$convertformat}");
283 echo "Error creating image, see command execution output for more details.";
287 function execute($cmd) {
288 exec($cmd, $result, $code);
289 $output = "<pre>$ $cmd\n";
290 $lines = implode("\n", $result);
291 $output .= "OUTPUT: $lines\n";
292 $output .= "RETURN CODE: $code\n</pre>\n";
296 function slasharguments($texexp) {
298 $admin = $CFG->wwwroot
.'/'.$CFG->admin
.'/settings.php?section=http';
299 $image = tex2image($texexp,true);
300 echo "<p>If the following image displays correctly, set your ";
301 echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> ";
302 echo "setting for slasharguments to file.php/1/pic.jpg: ";
303 echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php/$image\" align=\"absmiddle\"></p>\n";
304 echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
305 echo "It should display correctly as ";
306 echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php?file=$image\" align=\"absmiddle\"></p>\n";
307 echo "<p>If neither equation image displays correctly, please seek ";
308 echo "further help at moodle.org at the ";
309 echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">";
310 echo "Mathematics Tools Forum</a></p>";
316 <head
><title
>TeX Filter Debugger
</title
></head
>
318 <p
>Please enter an algebraic expression
<b
>without
</b
> any surrounding $$ into
319 the text box below
. (Click
<a href
="#help">here
for help
.</a
>)
320 <form action
="texdebug.php" method
="get"
321 target
="inlineframe">
323 <input type
="text" name
="tex" size
="50"
324 value
="f(x)=\int_{-\infty}^x~e^{-t^2}dt" />
326 <p
>The following tests are available
:</p
>
328 <li
><input type
="radio" name
="action" value
="ShowDB" id
="ShowDB" />
329 <label
for="ShowDB">See the cache_filters database entry
for this
expression (if any
).</label
></li
>
330 <li
><input type
="radio" name
="action" value
="DeleteDB" id
="DeleteDB" />
331 <label
for="DeleteDB">Delete the cache_filters database entry
for this
expression (if any
).</label
></li
>
332 <li
><input type
="radio" name
="action" value
="ShowImageMimetex" id
="ShowImageMimetex" checked
="checked" />
333 <label
for="ShowImageMimetex">Show a graphic image of the algebraic expression rendered with mimetex
.</label
></li
>
334 <li
><input type
="radio" name
="action" value
="ShowImageTex" id
="ShowImageTex" />
335 <label
for="ShowImageTex">Show a graphic image of the algebraic expression rendered with Tex
/Ghostscript
.</label
></li
>
336 <li
><input type
="radio" name
="action" value
="ShowOutputTex" id
="ShowOutputTex" />
337 <label
for="ShowOutputTex">Show command execution output from the algebraic expression rendered with Tex
/Ghostscript
.</label
></li
>
338 <li
><input type
="radio" name
="action" value
="SlashArguments" id
="SlashArguments" />
339 <label
for="SlashArguments">Check slasharguments setting
.</label
></li
>
341 <input type
="submit" value
="Do it!" />
342 <input type
="hidden" name
="sesskey" value
="<?php echo sesskey(); ?>" />
343 </form
> <br
/> <br
/>
345 <iframe name
="inlineframe" align
="middle" width
="80%" height
="200">
346 <
;p
>
;Something is wrong
...<
;/p
>
;
351 <h2
>Debugging Help
</h2
>
353 <p
>First a brief overview of how the TeX filter works
. The TeX filter first
354 searches the database cache_filters table to see
if this TeX expression had been
355 processed before
. If not
, it adds a DB entry
for that expression
. It then
356 replaces the TeX expression by an
<
;img src
="
;.../filter
/tex
/pix
.php
..."
;>
;
357 tag
. The filter
/tex
/pix
.php script then searches the database to find an
358 appropriate gif
/png
/svg image file
for that expression
and to create one
if it doesn
't exist.
359 It will then use either the LaTex/Ghostscript renderer (using external executables
360 on your system) or the bundled Mimetex executable. The full Latex/Ghostscript
361 renderer produces better results and is tried first.
362 Here are a few common things that can go wrong and some suggestions on how
363 you might try to fix them.</p>
365 <li>Something had gone wrong on a previous occasion when the filter tried to
366 process this expression. Then the database entry for that expression contains
367 a bad TeX expression in the rawtext field (usually blank). You can fix this
368 by clicking on "Delete DB Entry"</li>
369 <li>The TeX to gif/png/svg image conversion process does not work.
370 If paths are specified in the filter configuation screen for the three
371 executables these will be tried first. Note that they still must be correctly
372 installed and have the correct permissions. In particular make sure that you
373 have all the packages installed (e.g., on Debian/Ubuntu you need to install
374 the 'tetex
-extra
' package). Running the 'show command execution
' test should
376 If this fails or is not available, the Mimetex executable is tried. If this
377 fails a likely cause is that the mimetex binary you are using is
378 incompatible with your operating system. You can try compiling it from the
379 C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
380 http://www.forkosh.com/mimetex.zip</a>.
381 Another possible problem which may affect
382 both Unix and Windows servers is that the web server doesn't have execute permission
383 on the mimetex binary
. In that
case change permissions accordingly
</li
>