MDL-11515:
[moodle-linuxchix.git] / filter / tex / pix.php
blob64bb46c2e3ee7764d9ea220cdfefedff30c259c4
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 $texexp = html_entity_decode( $texcache->rawtext );
60 $latex_path = $latex->render( $texexp, $md5, 12, $density, $background );
61 if ($latex_path) {
62 copy( $latex_path, $pathname );
63 $latex->clean_up( $md5 );
65 else {
66 // failing that, use mimetex
67 $texexp = $texcache->rawtext;
68 $texexp = str_replace('&lt;','<',$texexp);
69 $texexp = str_replace('&gt;','>',$texexp);
70 $texexp = preg_replace('!\r\n?!',' ',$texexp);
71 $texexp = '\Large ' . $texexp;
72 $texexp = escapeshellarg($texexp);
74 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
75 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex.exe";
76 $cmd = str_replace(' ','^ ',$cmd);
77 $cmd .= " ++ -e \"$pathname\" -- $texexp";
78 } else if (is_executable("$CFG->dirroot/$CFG->texfilterdir/mimetex")) { /// Use the custom binary
80 $cmd = "$CFG->dirroot/$CFG->texfilterdir/mimetex -e $pathname -- $texexp";
82 } else { /// Auto-detect the right TeX binary
83 switch (PHP_OS) {
85 case "Linux":
86 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.linux\" -e \"$pathname\" -- $texexp";
87 break;
89 case "Darwin":
90 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.darwin\" -e \"$pathname\" -- $texexp";
91 break;
93 case "FreeBSD":
94 $cmd = "\"$CFG->dirroot/$CFG->texfilterdir/mimetex.freebsd\" -e \"$pathname\" $texexp";
95 break;
97 default: /// Nothing was found, so tell them how to fix it.
98 if (debugging()) {
99 echo "Make sure you have an appropriate MimeTeX binary here:\n\n";
100 echo " $CFG->dirroot/$CFG->texfilterdir/mimetex\n\n";
101 echo "and that it has the right permissions set on it as executable program.\n\n";
102 echo "You can get the latest binaries for your ".PHP_OS." platform from: \n\n";
103 echo " http://moodle.org/download/mimetex/";
104 } else {
105 echo "Mimetex executable was not found,\n";
106 echo "Please turn on debug mode in site configuration to see more info here.";
108 die;
109 break;
112 system($cmd, $status);
117 if (file_exists($pathname)) {
118 send_file($pathname, $image);
119 } else {
120 if (debugging()) {
121 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
122 echo "Image not found!<br />";
123 echo "Please try the <a href=\"$CFG->wwwroot/$CFG->texfilterdir/texdebug.php\">debugging script</a>";
124 } else {
125 echo "Image not found!<br />";
126 echo "Please try the <a href=\"$CFG->wwwroot/$CFG->texfilterdir/texdebug.php\">debugging script</a><br />";
127 echo "Please turn on debug mode in site configuration to see more info here.";