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!');
13 $filters = explode(',', $CFG->textfilters
);
14 if (array_search('filter/tex', $filters) === FALSE) {
15 error ('Filter not enabled!');
19 // disable moodle specific debug messages
22 require_once($CFG->libdir
.'/filelib.php');
23 require_once('defaultsettings.php' );
24 require_once('latex.php');
26 $CFG->texfilterdir
= 'filter/tex';
27 $CFG->teximagedir
= 'filter/tex';
29 // check/initialise default configuration for filter (in defaultsettings.php)
30 tex_defaultsettings();
32 $cmd = ''; // Initialise these variables
35 error_reporting(E_ALL
);
37 $relativepath = get_file_argument('pix.php');
39 $args = explode('/', trim($relativepath, '/'));
41 if (count($args) == 1) {
43 $pathname = $CFG->dataroot
.'/'.$CFG->teximagedir
.'/'.$image;
45 error('No valid arguments supplied');
48 if (!file_exists($pathname)) {
49 $md5 = str_replace('.gif','',$image);
50 if ($texcache = get_record('cache_filters', 'filter', 'tex', 'md5key', $md5)) {
51 if (!file_exists($CFG->dataroot
.'/'.$CFG->teximagedir
)) {
52 make_upload_directory($CFG->teximagedir
);
55 // try and render with latex first
57 $density = $CFG->filter_tex_density
;
58 $background = $CFG->filter_tex_latexbackground
;
59 $latex_path = $latex->render( $texcache->rawtext
, $md5, 12, $density, $background );
61 copy( $latex_path, $pathname );
62 $latex->clean_up( $md5 );
65 // failing that, use mimetex
66 $texexp = $texcache->rawtext
;
67 $texexp = str_replace('<','<',$texexp);
68 $texexp = str_replace('>','>',$texexp);
69 $texexp = preg_replace('!\r\n?!',' ',$texexp);
70 $texexp = '\Large ' . $texexp;
71 $texexp = escapeshellarg($texexp);
73 if ((PHP_OS
== "WINNT") ||
(PHP_OS
== "WIN32") ||
(PHP_OS
== "Windows")) {
74 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
75 $cmd = str_replace(' ','^ ',$cmd);
76 $cmd .= " ++ -e \"$pathname\" -- $texexp";
77 } else if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
79 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname -- $texexp";
81 } else { /// Auto-detect the right TeX binary
85 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" -- $texexp";
89 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" -- $texexp";
93 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.freebsd\" -e \"$pathname\" $texexp";
96 default: /// Nothing was found, so tell them how to fix it.
98 echo "Make sure you have an appropriate MimeTeX binary here:\n\n";
99 echo " $CFG->dirroot/$CFG->texfilterdir/mimetex\n\n";
100 echo "and that it has the right permissions set on it as executable program.\n\n";
101 echo "You can get the latest binaries for your ".PHP_OS
." platform from: \n\n";
102 echo " http://moodle.org/download/mimetex/";
104 echo "Mimetex executable was not found,\n";
105 echo "Please turn on debug mode in site configuration to see more info here.";
111 system($cmd, $status);
116 if (file_exists($pathname)) {
117 send_file($pathname, $image);
120 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
121 echo "Image not found!<br />";
122 echo "Please try the <a href=\"$CFG->wwwroot/$CFG->texfilterdir/texdebug.php\">debugging script</a>";
124 echo "Image not found!<br />";
125 echo "Please try the <a href=\"$CFG->wwwroot/$CFG->texfilterdir/texdebug.php\">debugging script</a><br />";
126 echo "Please turn on debug mode in site configuration to see more info here.";