MDL-30070 message: Optimised search for users over multiple courses
[moodle.git] / filter / tex / pix.php
CommitLineData
2e9b772f 1<?PHP
9d49b430 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
5
2e9b772f
PS
6// disable moodle specific debug messages and any errors in output
7define('NO_DEBUG_DISPLAY', true);
6800d78e 8define('NO_MOODLE_COOKIES', true); // Because it interferes with caching
9d49b430 9
e7f927a0 10 require_once('../../config.php');
33851b5c 11
0662bd67 12 if (!filter_is_enabled('tex')) {
e25b7ded 13 print_error('filternotenabled');
33851b5c 14 }
15
f1e0649c 16 require_once($CFG->libdir.'/filelib.php');
220a90c5 17 require_once($CFG->dirroot.'/filter/tex/lib.php');
18 require_once($CFG->dirroot.'/filter/tex/latex.php');
e7f927a0 19
20 $cmd = ''; // Initialise these variables
65087454 21 $status = '';
22
11e7b506 23 $relativepath = get_file_argument();
9d49b430 24
e7f927a0 25 $args = explode('/', trim($relativepath, '/'));
9d49b430 26
e7f927a0 27 if (count($args) == 1) {
28 $image = $args[0];
220a90c5 29 $pathname = $CFG->dataroot.'/filter/tex/'.$image;
9d49b430 30 } else {
c85fac1f 31 print_error('invalidarguments', 'error');
9d49b430 32 }
33
9d49b430 34 if (!file_exists($pathname)) {
cf4e7548 35 $md5 = str_replace(".{$CFG->filter_tex_convertformat}",'',$image);
b402e134 36 if ($texcache = $DB->get_record('cache_filters', array('filter'=>'tex', 'md5key'=>$md5))) {
220a90c5 37 if (!file_exists($CFG->dataroot.'/filter/tex')) {
38 make_upload_directory('filter/tex');
9d49b430 39 }
40
18a2407e 41 // try and render with latex first
42 $latex = new latex();
5b540fa9 43 $density = $CFG->filter_tex_density;
44 $background = $CFG->filter_tex_latexbackground;
f7f0909c 45 $texexp = $texcache->rawtext; // the entities are now decoded before inserting to DB
220a90c5 46 $latex_path = $latex->render($texexp, $md5, 12, $density, $background);
47 if ($latex_path) {
48 copy($latex_path, $pathname);
49 $latex->clean_up($md5);
50
51 } else {
18a2407e 52 // failing that, use mimetex
53 $texexp = $texcache->rawtext;
220a90c5 54 $texexp = str_replace('&lt;', '<', $texexp);
55 $texexp = str_replace('&gt;', '>', $texexp);
56 $texexp = preg_replace('!\r\n?!', ' ', $texexp);
57 $texexp = '\Large '.$texexp;
35716b86 58 $cmd = filter_tex_get_cmd($pathname, $texexp);
18a2407e 59 system($cmd, $status);
aaad4891 60 }
9d49b430 61 }
62 }
63
64 if (file_exists($pathname)) {
e7f927a0 65 send_file($pathname, $image);
9d49b430 66 } else {
779ef7fc 67 if (debugging()) {
e7f927a0 68 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
69 echo "Image not found!<br />";
220a90c5 70 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a>";
e7f927a0 71 } else {
72 echo "Image not found!<br />";
220a90c5 73 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a><br />";
e7f927a0 74 echo "Please turn on debug mode in site configuration to see more info here.";
75 }
9d49b430 76 }