Revert " MDL-45044 filter_tex: properly escape excutable pathnames for Windows"
[moodle.git] / filter / tex / latex.php
bloba79a33e31cd9a42419abea381ac6eeb20d382f76
1 <?php
2 // latex.php
3 // render TeX stuff using latex - this will not work on all platforms
4 // or configurations. Only works on Linux and Mac with appropriate
5 // software installed.
6 // Much of this inspired/copied from Benjamin Zeiss' work
8 class latex {
10 var $temp_dir;
11 var $error;
13 /**
14 * Constructor - create temporary directories and build paths to
15 * external 'helper' binaries.
16 * Other platforms could/should be added
18 function latex() {
19 global $CFG;
21 // construct directory structure
22 $this->temp_dir = $CFG->tempdir . "/latex";
23 make_temp_directory('latex');
26 /**
27 * Accessor function for support_platform field.
28 * @return boolean value of supported_platform
30 function supported() {
31 return $this->supported_platform;
34 /**
35 * Turn the bit of TeX into a valid latex document
36 * @param string $forumula the TeX formula
37 * @param int $fontsize the font size
38 * @return string the latex document
40 function construct_latex_document( $formula, $fontsize=12 ) {
41 global $CFG;
43 $formula = filter_tex_sanitize_formula($formula);
45 // $fontsize don't affects to formula's size. $density can change size
46 $doc = "\\documentclass[{$fontsize}pt]{article}\n";
47 $doc .= get_config('filter_tex', 'latexpreamble');
48 $doc .= "\\pagestyle{empty}\n";
49 $doc .= "\\begin{document}\n";
50 //dlnsk $doc .= "$ {$formula} $\n";
51 if (preg_match("/^[[:space:]]*\\\\begin\\{(gather|align|alignat|multline).?\\}/i",$formula)) {
52 $doc .= "$formula\n";
53 } else {
54 $doc .= "$ {$formula} $\n";
56 $doc .= "\\end{document}\n";
57 return $doc;
60 /**
61 * execute an external command, with optional logging
62 * @param string $command command to execute
63 * @param file $log valid open file handle - log info will be written to this file
64 * @return return code from execution of command
66 function execute( $command, $log=null ) {
67 $output = array();
68 exec( $command, $output, $return_code );
69 if ($log) {
70 fwrite( $log, "COMMAND: $command \n" );
71 $outputs = implode( "\n", $output );
72 fwrite( $log, "OUTPUT: $outputs \n" );
73 fwrite( $log, "RETURN_CODE: $return_code\n " );
75 return $return_code;
78 /**
79 * Render TeX string into gif/png
80 * @param string $formula TeX formula
81 * @param string $filename base of filename for output (no extension)
82 * @param int $fontsize font size
83 * @param int $density density value for .ps to .gif/.png conversion
84 * @param string $background background color (e.g, #FFFFFF).
85 * @param file $log valid open file handle for optional logging (debugging only)
86 * @return bool true if successful
88 function render( $formula, $filename, $fontsize=12, $density=240, $background='', $log=null ) {
90 global $CFG;
92 // quick check - will this work?
93 $pathlatex = get_config('filter_tex', 'pathlatex');
94 if (empty($pathlatex)) {
95 return false;
98 $doc = $this->construct_latex_document( $formula, $fontsize );
100 // construct some file paths
101 $tex = "{$this->temp_dir}/$filename.tex";
102 $dvi = "{$this->temp_dir}/$filename.dvi";
103 $ps = "{$this->temp_dir}/$filename.ps";
104 $convertformat = get_config('filter_tex', 'convertformat');
105 $img = "{$this->temp_dir}/$filename.{$convertformat}";
107 // turn the latex doc into a .tex file in the temp area
108 $fh = fopen( $tex, 'w' );
109 fputs( $fh, $doc );
110 fclose( $fh );
112 // run latex on document
113 $command = "{$pathlatex} --interaction=nonstopmode --halt-on-error $tex";
114 chdir( $this->temp_dir );
115 if ($this->execute($command, $log)) { // It allways False on Windows
116 // return false;
119 // run dvips (.dvi to .ps)
120 $pathdvips = get_config('filter_tex', 'pathdvips');
121 $command = "{$pathdvips} -E $dvi -o $ps";
122 if ($this->execute($command, $log )) {
123 return false;
126 // run convert on document (.ps to .gif/.png)
127 if ($background) {
128 $bg_opt = "-transparent \"$background\""; // Makes transparent background
129 } else {
130 $bg_opt = "";
132 $pathconvert = get_config('filter_tex', 'pathconvert');
133 $command = "{$pathconvert} -density $density -trim $bg_opt $ps $img";
134 if ($this->execute($command, $log )) {
135 return false;
138 return $img;
142 * Delete files created in temporary area
143 * Don't forget to copy the final gif/png before calling this
144 * @param string $filename file base (no extension)
146 function clean_up( $filename ) {
147 global $CFG;
149 unlink( "{$this->temp_dir}/$filename.tex" );
150 unlink( "{$this->temp_dir}/$filename.dvi" );
151 unlink( "{$this->temp_dir}/$filename.ps" );
152 $convertformat = get_config('filter_tex', 'convertformat');
153 unlink( "{$this->temp_dir}/$filename.{$convertformat}" );
154 unlink( "{$this->temp_dir}/$filename.aux" );
155 unlink( "{$this->temp_dir}/$filename.log" );
156 return;