Merge branch 'wip-MDL-28722-m19' of git://github.com/samhemelryk/moodle into MOODLE_1...
[moodle.git] / filter / tex / pix.php
blob2d9d41975300ba278a20aa569a3ff8606048fe26
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 // disable moodle specific debug messages
20 disable_debugging();
22 require_once($CFG->libdir.'/filelib.php');
23 require_once($CFG->dirroot.'/filter/tex/lib.php');
24 require_once($CFG->dirroot.'/filter/tex/latex.php');
26 $cmd = ''; // Initialise these variables
27 $status = '';
29 $relativepath = get_file_argument('pix.php');
31 $args = explode('/', trim($relativepath, '/'));
33 if (count($args) == 1) {
34 $image = $args[0];
35 $pathname = $CFG->dataroot.'/filter/tex/'.$image;
36 } else {
37 error('No valid arguments supplied');
40 if (!file_exists($pathname)) {
41 $md5 = str_replace(".{$CFG->filter_tex_convertformat}",'',$image);
42 if ($texcache = get_record('cache_filters', 'filter', 'tex', 'md5key', $md5)) {
43 if (!file_exists($CFG->dataroot.'/filter/tex')) {
44 make_upload_directory('filter/tex');
47 // try and render with latex first
48 $latex = new latex();
49 $density = $CFG->filter_tex_density;
50 $background = $CFG->filter_tex_latexbackground;
51 $texexp = html_entity_decode($texcache->rawtext);
52 $latex_path = $latex->render($texexp, $md5, 12, $density, $background);
53 if ($latex_path) {
54 copy($latex_path, $pathname);
55 $latex->clean_up($md5);
57 } else {
58 // failing that, use mimetex
59 $texexp = $texcache->rawtext;
60 $texexp = str_replace('&lt;', '<', $texexp);
61 $texexp = str_replace('&gt;', '>', $texexp);
62 $texexp = preg_replace('!\r\n?!', ' ', $texexp);
63 $texexp = '\Large '.$texexp;
64 $cmd = tex_filter_get_cmd($pathname, $texexp);
65 system($cmd, $status);
70 if (file_exists($pathname)) {
71 send_file($pathname, $image);
72 } else {
73 if (debugging()) {
74 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
75 echo "Image not found!<br />";
76 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a>";
77 } else {
78 echo "Image not found!<br />";
79 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a><br />";
80 echo "Please turn on debug mode in site configuration to see more info here.";