MDL-24471, FILEMANAGER change <button> to <input type="button" />, moodle form may...
[moodle.git] / filter / tex / pix.php
blob45b1b79b6731f957985fef42e9bd81714f98177b
1 <?PHP
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 // disable moodle specific debug messages and any errors in output
7 define('NO_DEBUG_DISPLAY', true);
8 define('NO_MOODLE_COOKIES', true); // Because it interferes with caching
10 require_once('../../config.php');
12 if (!filter_is_enabled('filter/tex')) {
13 print_error('filternotenabled');
16 require_once($CFG->libdir.'/filelib.php');
17 require_once($CFG->dirroot.'/filter/tex/lib.php');
18 require_once($CFG->dirroot.'/filter/tex/latex.php');
20 $cmd = ''; // Initialise these variables
21 $status = '';
23 $relativepath = get_file_argument();
25 $args = explode('/', trim($relativepath, '/'));
27 if (count($args) == 1) {
28 $image = $args[0];
29 $pathname = $CFG->dataroot.'/filter/tex/'.$image;
30 } else {
31 print_error('invalidarguments', 'error');
34 if (!file_exists($pathname)) {
35 $md5 = str_replace(".{$CFG->filter_tex_convertformat}",'',$image);
36 if ($texcache = $DB->get_record('cache_filters', array('filter'=>'tex', 'md5key'=>$md5))) {
37 if (!file_exists($CFG->dataroot.'/filter/tex')) {
38 make_upload_directory('filter/tex');
41 // try and render with latex first
42 $latex = new latex();
43 $density = $CFG->filter_tex_density;
44 $background = $CFG->filter_tex_latexbackground;
45 $texexp = html_entity_decode($texcache->rawtext);
46 $latex_path = $latex->render($texexp, $md5, 12, $density, $background);
47 if ($latex_path) {
48 copy($latex_path, $pathname);
49 $latex->clean_up($md5);
51 } else {
52 // failing that, use mimetex
53 $texexp = $texcache->rawtext;
54 $texexp = str_replace('&lt;', '<', $texexp);
55 $texexp = str_replace('&gt;', '>', $texexp);
56 $texexp = preg_replace('!\r\n?!', ' ', $texexp);
57 $texexp = '\Large '.$texexp;
58 $cmd = filter_tex_get_cmd($pathname, $texexp);
59 system($cmd, $status);
64 if (file_exists($pathname)) {
65 send_file($pathname, $image);
66 } else {
67 if (debugging()) {
68 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
69 echo "Image not found!<br />";
70 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a>";
71 } else {
72 echo "Image not found!<br />";
73 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a><br />";
74 echo "Please turn on debug mode in site configuration to see more info here.";