2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * TeX filter library functions.
22 * @copyright 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu
23 * Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
29 function filter_tex_get_executable($debug=false) {
32 $error_message1 = "Your system is not configured to run mimeTeX. You need to download the appropriate<br />"
33 ."executable for you ".PHP_OS
." platform from <a href=\"http://moodle.org/download/mimetex/\">"
34 ."http://moodle.org/download/mimetex/</a>, or obtain the C source<br /> "
35 ."from <a href=\"http://www.forkosh.com/mimetex.zip\">"
36 ."http://www.forkosh.com/mimetex.zip</a>, compile it and "
37 ."put the executable into your<br /> moodle/filter/tex/ directory.";
39 $error_message2 = "Custom mimetex is not executable!<br /><br />";
41 if ((PHP_OS
== "WINNT") ||
(PHP_OS
== "WIN32") ||
(PHP_OS
== "Windows")) {
42 return "$CFG->dirroot/filter/tex/mimetex.exe";
45 $custom_commandpath = "$CFG->dirroot/filter/tex/mimetex";
46 if (file_exists($custom_commandpath)) {
47 if (is_executable($custom_commandpath)) {
48 return $custom_commandpath;
50 print_error('mimetexnotexecutable', 'error');
55 case "Linux": return "$CFG->dirroot/filter/tex/mimetex.linux";
56 case "Darwin": return "$CFG->dirroot/filter/tex/mimetex.darwin";
57 case "FreeBSD": return "$CFG->dirroot/filter/tex/mimetex.freebsd";
60 print_error('mimetexisnotexist', 'error');
63 function filter_tex_sanitize_formula($texexp) {
64 /// Check $texexp against blacklist (whitelisting could be more complete but also harder to maintain)
65 $tex_blacklist = array(
66 'include','command','loop','repeat','open','toks','output',
67 'input','catcode','name','^^',
68 '\def','\edef','\gdef','\xdef',
69 '\every','\errhelp','\errorstopmode','\scrollmode','\nonstopmode',
70 '\batchmode','\read','\write','csname','\newhelp','\uppercase',
71 '\lowercase','\relax','\aftergroup',
72 '\afterassignment','\expandafter','\noexpand','\special',
73 '\let', '\futurelet','\else','\fi','\chardef','\makeatletter','\afterground',
74 '\noexpand','\line','\mathcode','\item','\section','\mbox','\declarerobustcommand'
77 return str_ireplace($tex_blacklist, 'forbiddenkeyword', $texexp);
80 function filter_tex_get_cmd($pathname, $texexp) {
81 $texexp = filter_tex_sanitize_formula($texexp);
82 $texexp = escapeshellarg($texexp);
83 $executable = filter_tex_get_executable(false);
85 if ((PHP_OS
== "WINNT") ||
(PHP_OS
== "WIN32") ||
(PHP_OS
== "Windows")) {
86 $executable = str_replace(' ', '^ ', $executable);
87 return "$executable ++ -e \"$pathname\" -- $texexp";
90 return "\"$executable\" -e \"$pathname\" -- $texexp";
95 * Purge all caches when settings changed.
97 function filter_tex_updatedcallback($name) {
99 reset_text_filters_cache();
101 if (file_exists("$CFG->dataroot/filter/tex")) {
102 remove_dir("$CFG->dataroot/filter/tex");
104 if (file_exists("$CFG->dataroot/filter/algebra")) {
105 remove_dir("$CFG->dataroot/filter/algebra");
107 if (file_exists("$CFG->tempdir/latex")) {
108 remove_dir("$CFG->tempdir/latex");
111 $DB->delete_records('cache_filters', array('filter'=>'tex'));
112 $DB->delete_records('cache_filters', array('filter'=>'algebra'));
114 if (!isset($CFG->filter_tex_pathlatex
)) {
115 // detailed settings not present yet
119 if (!(is_file($CFG->filter_tex_pathlatex
) && is_executable($CFG->filter_tex_pathlatex
) &&
120 is_file($CFG->filter_tex_pathdvips
) && is_executable($CFG->filter_tex_pathdvips
) &&
121 is_file($CFG->filter_tex_pathconvert
) && is_executable($CFG->filter_tex_pathconvert
))) {
122 // LaTeX, dvips or convert are not available, and mimetex can only produce GIFs so...
123 set_config('filter_tex_convertformat', 'gif');