MDL-10680:
[moodle-linuxchix.git] / filter / tex / pix.php
blob66d39c67516622ddca64ea3f74b2270038196887
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('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
33 $status = '';
35 error_reporting(E_ALL);
37 $relativepath = get_file_argument('pix.php');
39 $args = explode('/', trim($relativepath, '/'));
41 if (count($args) == 1) {
42 $image = $args[0];
43 $pathname = $CFG->dataroot.'/'.$CFG->teximagedir.'/'.$image;
44 } else {
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
56 $latex = new latex();
57 $density = $CFG->filter_tex_density;
58 $background = $CFG->filter_tex_latexbackground;
59 $latex_path = $latex->render( $texcache->rawtext, $md5, 12, $density, $background );
60 if ($latex_path) {
61 copy( $latex_path, $pathname );
62 $latex->clean_up( $md5 );
64 else {
65 // failing that, use mimetex
66 $texexp = $texcache->rawtext;
67 $texexp = str_replace('&lt;','<',$texexp);
68 $texexp = str_replace('&gt;','>',$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
82 switch (PHP_OS) {
84 case "Linux":
85 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" -- $texexp";
86 break;
88 case "Darwin":
89 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" -- $texexp";
90 break;
92 case "FreeBSD":
93 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.freebsd\" -e \"$pathname\" $texexp";
94 break;
96 default: /// Nothing was found, so tell them how to fix it.
97 if (debugging()) {
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/";
103 } else {
104 echo "Mimetex executable was not found,\n";
105 echo "Please turn on debug mode in site configuration to see more info here.";
107 die;
108 break;
111 system($cmd, $status);
116 if (file_exists($pathname)) {
117 send_file($pathname, $image);
118 } else {
119 if (debugging()) {
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>";
123 } else {
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.";