1 |
efrain |
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
|
|
|
5 |
|
|
|
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
|
|
|
9 |
|
|
|
10 |
require_once('../../config.php');
|
|
|
11 |
|
|
|
12 |
if (!filter_is_enabled('tex')) {
|
|
|
13 |
throw new \moodle_exception('filternotenabled');
|
|
|
14 |
}
|
|
|
15 |
|
|
|
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');
|
|
|
19 |
|
|
|
20 |
$cmd = ''; // Initialise these variables
|
|
|
21 |
$status = '';
|
|
|
22 |
|
|
|
23 |
$relativepath = get_file_argument();
|
|
|
24 |
|
|
|
25 |
$args = explode('/', trim($relativepath, '/'));
|
|
|
26 |
|
|
|
27 |
if (count($args) == 1) {
|
|
|
28 |
$image = $args[0];
|
|
|
29 |
$pathname = $CFG->dataroot.'/filter/tex/'.$image;
|
|
|
30 |
} else {
|
|
|
31 |
throw new \moodle_exception('invalidarguments', 'error');
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
if (!file_exists($pathname)) {
|
|
|
35 |
$convertformat = get_config('filter_tex', 'convertformat');
|
|
|
36 |
if (strpos($image, '.png')) {
|
|
|
37 |
$convertformat = 'png';
|
|
|
38 |
}
|
|
|
39 |
$md5 = str_replace(".{$convertformat}", '', $image);
|
|
|
40 |
if ($texcache = $DB->get_record('cache_filters', array('filter'=>'tex', 'md5key'=>$md5))) {
|
|
|
41 |
if (!file_exists($CFG->dataroot.'/filter/tex')) {
|
|
|
42 |
make_upload_directory('filter/tex');
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// try and render with latex first
|
|
|
46 |
$latex = new latex();
|
|
|
47 |
$density = get_config('filter_tex', 'density');
|
|
|
48 |
$background = get_config('filter_tex', 'latexbackground');
|
|
|
49 |
$texexp = $texcache->rawtext; // the entities are now decoded before inserting to DB
|
|
|
50 |
$lateximage = $latex->render($texexp, $image, 12, $density, $background);
|
|
|
51 |
if ($lateximage) {
|
|
|
52 |
copy($lateximage, $pathname);
|
|
|
53 |
} else {
|
|
|
54 |
// failing that, use mimetex
|
|
|
55 |
$texexp = $texcache->rawtext;
|
|
|
56 |
$texexp = str_replace('<', '<', $texexp);
|
|
|
57 |
$texexp = str_replace('>', '>', $texexp);
|
|
|
58 |
$texexp = preg_replace('!\r\n?!', ' ', $texexp);
|
|
|
59 |
$texexp = '\Large '.$texexp;
|
|
|
60 |
$cmd = filter_tex_get_cmd($pathname, $texexp);
|
|
|
61 |
system($cmd, $status);
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if (file_exists($pathname)) {
|
|
|
67 |
send_file($pathname, $image, YEARSECS, 0, false, false, '', false, [
|
|
|
68 |
'cacheability' => 'public',
|
|
|
69 |
'immutable' => true,
|
|
|
70 |
]);
|
|
|
71 |
} else {
|
|
|
72 |
if (debugging()) {
|
|
|
73 |
echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
|
|
|
74 |
echo "Image not found!<br />";
|
|
|
75 |
echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a>";
|
|
|
76 |
} else {
|
|
|
77 |
echo "Image not found!<br />";
|
|
|
78 |
echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a><br />";
|
|
|
79 |
echo "Please turn on debug mode in site configuration to see more info here.";
|
|
|
80 |
}
|
|
|
81 |
}
|