Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * This function fetches math. images from the data directory 19 * If not, it obtains the corresponding TeX expression from the cache_tex db table 20 * and uses mimeTeX to create the image file 21 * 22 * @package filter 23 * @subpackage tex 24 * @copyright 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu 25 * Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 29 require_once("../../config.php"); 30 31 if (!filter_is_enabled('tex')) { 32 print_error('filternotenabled'); 33 } 34 35 require_once($CFG->libdir.'/filelib.php'); 36 require_once($CFG->dirroot.'/filter/tex/lib.php'); 37 require_once($CFG->dirroot.'/filter/tex/latex.php'); 38 39 $action = optional_param('action', '', PARAM_ALPHA); 40 $texexp = optional_param('tex', '', PARAM_RAW); 41 42 require_login(); 43 require_capability('moodle/site:config', context_system::instance(), $USER->id); /// Required cap to run this. MDL-18552 44 if ($action || $texexp) { 45 require_sesskey(); 46 } 47 48 $output = ''; 49 50 // look up in cache if required 51 if ($action=='ShowDB' or $action=='DeleteDB') { 52 $md5 = md5($texexp); 53 $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5)); 54 } 55 56 // Action: Show DB Entry 57 if ($action=='ShowDB') { 58 if ($texcache) { 59 $output = "DB cache_filters entry for $texexp\n"; 60 $output .= "id = $texcache->id\n"; 61 $output .= "filter = $texcache->filter\n"; 62 $output .= "version = $texcache->version\n"; 63 $output .= "md5key = $texcache->md5key\n"; 64 $output .= "rawtext = $texcache->rawtext\n"; 65 $output .= "timemodified = $texcache->timemodified\n"; 66 } else { 67 $output = "DB cache_filters entry for $texexp not found\n"; 68 } 69 } 70 71 // Action: Delete DB Entry 72 if ($action=='DeleteDB') { 73 if ($texcache) { 74 $output = "Deleting DB cache_filters entry for $texexp\n"; 75 $result = $DB->delete_records("cache_filters", array("id"=>$texcache->id)); 76 if ($result) { 77 $result = 1; 78 } else { 79 $result = 0; 80 } 81 $output .= "Number of records deleted = $result\n"; 82 } else { 83 $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n"; 84 } 85 } 86 87 // Action: Show Image 88 if ($action=='ShowImageMimetex') { 89 tex2image($texexp); 90 } 91 92 // Action: Check Slasharguments 93 if ($action=='SlashArguments') { 94 slasharguments($texexp); 95 exit; 96 } 97 98 // Action: Show Tex command line output 99 if ($action=='ShowImageTex') { 100 TexOutput($texexp, true); 101 exit; 102 } 103 104 // Action: Show Tex command line output 105 if ($action=='ShowOutputTex') { 106 if (debugging()) { 107 TexOutput($texexp); 108 } else { 109 echo "Can not output detailed information due to security concerns, please turn on debug mode first."; 110 } 111 exit; 112 } 113 114 if (!empty($action)) { 115 outputText($output); 116 } 117 118 // nothing more to do if there was any action 119 if (!empty($action)) { 120 exit; 121 } 122 123 124 function outputText($texexp) { 125 header("Content-type: text/html; charset=utf-8"); 126 echo "<html><body><pre>\n"; 127 if ($texexp) { 128 echo s($texexp)."\n\n"; 129 } else { 130 echo "No text output available\n\n"; 131 } 132 echo "</pre></body></html>\n"; 133 } 134 135 function tex2image($texexp, $return=false) { 136 global $CFG; 137 138 if (!$texexp) { 139 echo 'No tex expresion specified'; 140 return; 141 } 142 143 $image = md5($texexp) . ".gif"; 144 $filetype = 'image/gif'; 145 if (!file_exists("$CFG->dataroot/filter/tex")) { 146 make_upload_directory("filter/tex"); 147 } 148 $pathname = "$CFG->dataroot/filter/tex/$image"; 149 if (file_exists($pathname)) { 150 unlink($pathname); 151 } 152 153 $texexp = '\Large '.$texexp; 154 $commandpath = filter_tex_get_executable(true); 155 $cmd = filter_tex_get_cmd($pathname, $texexp); 156 system($cmd, $status); 157 158 if ($return) { 159 return $image; 160 } 161 162 if (file_exists($pathname)) { 163 send_file($pathname, $image); 164 165 } else if (debugging()) { 166 $ecmd = "$cmd 2>&1"; 167 echo `$ecmd` . "<br />\n"; 168 echo "The shell command<br />$cmd<br />returned status = $status<br />\n"; 169 if ($status == 4) { 170 echo "Status corresponds to illegal instruction<br />\n"; 171 } else if ($status == 11) { 172 echo "Status corresponds to bus error<br />\n"; 173 } else if ($status == 22) { 174 echo "Status corresponds to abnormal termination<br />\n"; 175 } 176 if (file_exists($commandpath)) { 177 echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />"; 178 echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />"; 179 if (function_exists("md5_file")) { 180 echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />"; 181 } else { 182 $handle = fopen($commandpath,"rb"); 183 $contents = fread($handle,16384); 184 fclose($handle); 185 echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />"; 186 } 187 } else { 188 echo "mimetex executable $commandpath not found!<br />"; 189 } 190 echo "Image not found!"; 191 } else { 192 echo "Can not output detailed information due to security concerns, please turn on debug mode first."; 193 } 194 } 195 196 197 // test Tex/Ghostscript output - command execution only 198 function TexOutput($expression, $graphic=false) { 199 global $CFG; 200 $output = ''; 201 202 $latex = new latex(); 203 204 // first check if it is likely to work at all 205 $output .= "<h3>Checking executables</h3>\n"; 206 $executablesexist = true; 207 $pathlatex = trim(get_config('filter_tex', 'pathlatex'), " '\""); 208 if (is_file($pathlatex)) { 209 $output .= "latex executable ($pathlatex) is readable<br />\n"; 210 } else { 211 $executablesexist = false; 212 $output .= "<b>Error:</b> latex executable ($pathlatex) is not readable<br />\n"; 213 } 214 $pathdvips = trim(get_config('filter_tex', 'pathdvips'), " '\""); 215 if (is_file($pathdvips)) { 216 $output .= "dvips executable ($pathdvips) is readable<br />\n"; 217 } else { 218 $executablesexist = false; 219 $output .= "<b>Error:</b> dvips executable ($pathdvips) is not readable<br />\n"; 220 } 221 $pathconvert = trim(get_config('filter_tex', 'pathconvert'), " '\""); 222 if (is_file($pathconvert)) { 223 $output .= "convert executable ($pathconvert) is readable<br />\n"; 224 } else { 225 $executablesexist = false; 226 $output .= "<b>Error:</b> convert executable ($pathconvert) is not readable<br />\n"; 227 } 228 $pathdvisvgm = trim(get_config('filter_tex', 'pathdvisvgm'), " '\""); 229 if (is_file($pathdvisvgm)) { 230 $output .= "dvisvgm executable ($pathdvisvgm) is readable<br />\n"; 231 } else { 232 $executablesexist = false; 233 $output .= "<b>Error:</b> dvisvgm executable ($pathdvisvgm) is not readable<br />\n"; 234 } 235 236 // knowing that it might work.. 237 $md5 = md5($expression); 238 $output .= "<p>base filename for expression is '$md5'</p>\n"; 239 240 // temporary paths 241 $tex = "$md5.tex"; // Absolute paths won't work with openin_any = p setting. 242 $dvi = "$latex->temp_dir/$md5.dvi"; 243 $ps = "$latex->temp_dir/$md5.ps"; 244 $convertformat = get_config('filter_tex', 'convertformat'); 245 $img = "$latex->temp_dir/$md5.{$convertformat}"; 246 247 // Change directory to temp dir so that we can work with relative paths. 248 chdir($latex->temp_dir); 249 250 // put the expression as a file into the temp area 251 $expression = html_entity_decode($expression); 252 $output .= "<p>Processing TeX expression:</p><pre>$expression</pre>\n"; 253 $doc = $latex->construct_latex_document($expression); 254 $fh = fopen($tex, 'w'); 255 fputs($fh, $doc); 256 fclose($fh); 257 258 // step 1: latex command 259 $pathlatex = escapeshellarg($pathlatex); 260 $cmd = "$pathlatex --interaction=nonstopmode --halt-on-error $tex"; 261 $output .= execute($cmd); 262 263 // step 2: dvips command 264 $pathdvips = escapeshellarg($pathdvips); 265 $cmd = "$pathdvips -E $dvi -o $ps"; 266 $output .= execute($cmd); 267 268 // Step 3: Set convert or dvisvgm command. 269 if ($convertformat == 'svg') { 270 $pathdvisvgm = escapeshellarg($pathdvisvgm); 271 $cmd = "$pathdvisvgm -E $ps -o $img"; 272 } else { 273 $pathconvert = escapeshellarg($pathconvert); 274 $cmd = "$pathconvert -density 240 -trim $ps $img "; 275 } 276 $output .= execute($cmd); 277 278 if (!$graphic) { 279 echo $output; 280 } else if (file_exists($img)) { 281 send_file($img, "$md5.{$convertformat}"); 282 } else { 283 echo "Error creating image, see command execution output for more details."; 284 } 285 } 286 287 function execute($cmd) { 288 exec($cmd, $result, $code); 289 $output = "<pre>$ $cmd\n"; 290 $lines = implode("\n", $result); 291 $output .= "OUTPUT: $lines\n"; 292 $output .= "RETURN CODE: $code\n</pre>\n"; 293 return $output; 294 } 295 296 function slasharguments($texexp) { 297 global $CFG; 298 $admin = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=http'; 299 $image = tex2image($texexp,true); 300 echo "<p>If the following image displays correctly, set your "; 301 echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> "; 302 echo "setting for slasharguments to file.php/1/pic.jpg: "; 303 echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php/$image\" align=\"absmiddle\"></p>\n"; 304 echo "<p>Otherwise set it to file.php?file=/1/pic.jpg "; 305 echo "It should display correctly as "; 306 echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php?file=$image\" align=\"absmiddle\"></p>\n"; 307 echo "<p>If neither equation image displays correctly, please seek "; 308 echo "further help at moodle.org at the "; 309 echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">"; 310 echo "Mathematics Tools Forum</a></p>"; 311 } 312 313 ?> 314 315 <html> 316 <head><title>TeX Filter Debugger</title></head> 317 <body> 318 <p>Please enter an algebraic expression <b>without</b> any surrounding $$ into 319 the text box below. (Click <a href="#help">here for help.</a>) 320 <form action="texdebug.php" method="get" 321 target="inlineframe"> 322 <center> 323 <input type="text" name="tex" size="50" 324 value="f(x)=\int_{-\infty}^x~e^{-t^2}dt" /> 325 </center> 326 <p>The following tests are available:</p> 327 <ol> 328 <li><input type="radio" name="action" value="ShowDB" id="ShowDB" /> 329 <label for="ShowDB">See the cache_filters database entry for this expression (if any).</label></li> 330 <li><input type="radio" name="action" value="DeleteDB" id="DeleteDB" /> 331 <label for="DeleteDB">Delete the cache_filters database entry for this expression (if any).</label></li> 332 <li><input type="radio" name="action" value="ShowImageMimetex" id="ShowImageMimetex" checked="checked" /> 333 <label for="ShowImageMimetex">Show a graphic image of the algebraic expression rendered with mimetex.</label></li> 334 <li><input type="radio" name="action" value="ShowImageTex" id="ShowImageTex" /> 335 <label for="ShowImageTex">Show a graphic image of the algebraic expression rendered with Tex/Ghostscript.</label></li> 336 <li><input type="radio" name="action" value="ShowOutputTex" id="ShowOutputTex" /> 337 <label for="ShowOutputTex">Show command execution output from the algebraic expression rendered with Tex/Ghostscript.</label></li> 338 <li><input type="radio" name="action" value="SlashArguments" id="SlashArguments" /> 339 <label for="SlashArguments">Check slasharguments setting.</label></li> 340 </ol> 341 <input type="submit" value="Do it!" /> 342 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" /> 343 </form> <br /> <br /> 344 <center> 345 <iframe name="inlineframe" align="middle" width="80%" height="200"> 346 <p>Something is wrong...</p> 347 </iframe> 348 </center> <br /> 349 <hr /> 350 <a name="help"> 351 <h2>Debugging Help</h2> 352 </a> 353 <p>First a brief overview of how the TeX filter works. The TeX filter first 354 searches the database cache_filters table to see if this TeX expression had been 355 processed before. If not, it adds a DB entry for that expression. It then 356 replaces the TeX expression by an <img src=".../filter/tex/pix.php..."> 357 tag. The filter/tex/pix.php script then searches the database to find an 358 appropriate gif/png/svg image file for that expression and to create one if it doesn't exist. 359 It will then use either the LaTex/Ghostscript renderer (using external executables 360 on your system) or the bundled Mimetex executable. The full Latex/Ghostscript 361 renderer produces better results and is tried first. 362 Here are a few common things that can go wrong and some suggestions on how 363 you might try to fix them.</p> 364 <ol> 365 <li>Something had gone wrong on a previous occasion when the filter tried to 366 process this expression. Then the database entry for that expression contains 367 a bad TeX expression in the rawtext field (usually blank). You can fix this 368 by clicking on "Delete DB Entry"</li> 369 <li>The TeX to gif/png/svg image conversion process does not work. 370 If paths are specified in the filter configuation screen for the three 371 executables these will be tried first. Note that they still must be correctly 372 installed and have the correct permissions. In particular make sure that you 373 have all the packages installed (e.g., on Debian/Ubuntu you need to install 374 the 'tetex-extra' package). Running the 'show command execution' test should 375 give a big clue. 376 If this fails or is not available, the Mimetex executable is tried. If this 377 fails a likely cause is that the mimetex binary you are using is 378 incompatible with your operating system. You can try compiling it from the 379 C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip"> 380 http://www.forkosh.com/mimetex.zip</a>. 381 Another possible problem which may affect 382 both Unix and Windows servers is that the web server doesn't have execute permission 383 on the mimetex binary. In that case change permissions accordingly</li> 384 </ol> 385 </body> 386 </html>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body