Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / filter / tex / pix.php
blob457703e54f858ca12beaaad9a1075ed2debfaca9
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 error_reporting(E_ALL);
31 $relativepath = get_file_argument('pix.php');
33 $args = explode('/', trim($relativepath, '/'));
35 if (count($args) == 1) {
36 $image = $args[0];
37 $pathname = $CFG->dataroot.'/filter/tex/'.$image;
38 } else {
39 error('No valid arguments supplied');
42 if (!file_exists($pathname)) {
43 $md5 = str_replace('.gif','',$image);
44 if ($texcache = get_record('cache_filters', 'filter', 'tex', 'md5key', $md5)) {
45 if (!file_exists($CFG->dataroot.'/filter/tex')) {
46 make_upload_directory('filter/tex');
49 // try and render with latex first
50 $latex = new latex();
51 $density = $CFG->filter_tex_density;
52 $background = $CFG->filter_tex_latexbackground;
53 $texexp = html_entity_decode($texcache->rawtext);
54 $latex_path = $latex->render($texexp, $md5, 12, $density, $background);
55 if ($latex_path) {
56 copy($latex_path, $pathname);
57 $latex->clean_up($md5);
59 } else {
60 // failing that, use mimetex
61 $texexp = $texcache->rawtext;
62 $texexp = str_replace('&lt;', '<', $texexp);
63 $texexp = str_replace('&gt;', '>', $texexp);
64 $texexp = preg_replace('!\r\n?!', ' ', $texexp);
65 $texexp = '\Large '.$texexp;
66 $cmd = tex_filter_get_cmd($pathname, $texexp);
67 system($cmd, $status);
72 if (file_exists($pathname)) {
73 send_file($pathname, $image);
74 } else {
75 if (debugging()) {
76 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
77 echo "Image not found!<br />";
78 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a>";
79 } else {
80 echo "Image not found!<br />";
81 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a><br />";
82 echo "Please turn on debug mode in site configuration to see more info here.";