MDL-62202 privacy: fix behat tests to point to new location under users
[moodle.git] / filter / tex / lib.php
blob4aad3ec543f2bde853554522adeba254566ee2a0
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.
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/>.
17 /**
18 * TeX filter library functions.
20 * @package filter
21 * @subpackage tex
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) {
30 global $CFG;
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 if ($pathmimetex = get_config('filter_tex', 'pathmimetex')) {
46 if (is_executable($pathmimetex)) {
47 return $pathmimetex;
48 } else {
49 print_error('mimetexnotexecutable', 'error');
53 $custom_commandpath = "$CFG->dirroot/filter/tex/mimetex";
54 if (file_exists($custom_commandpath)) {
55 if (is_executable($custom_commandpath)) {
56 return $custom_commandpath;
57 } else {
58 print_error('mimetexnotexecutable', 'error');
62 switch (PHP_OS) {
63 case "Linux": return "$CFG->dirroot/filter/tex/mimetex.linux";
64 case "Darwin": return "$CFG->dirroot/filter/tex/mimetex.darwin";
65 case "FreeBSD": return "$CFG->dirroot/filter/tex/mimetex.freebsd";
68 print_error('mimetexisnotexist', 'error');
71 function filter_tex_sanitize_formula($texexp) {
72 /// Check $texexp against blacklist (whitelisting could be more complete but also harder to maintain)
73 $tex_blacklist = array(
74 'include','command','loop','repeat','open','toks','output',
75 'input','catcode','name','^^',
76 '\def','\edef','\gdef','\xdef',
77 '\every','\errhelp','\errorstopmode','\scrollmode','\nonstopmode',
78 '\batchmode','\read','\write','csname','\newhelp','\uppercase',
79 '\lowercase','\relax','\aftergroup',
80 '\afterassignment','\expandafter','\noexpand','\special',
81 '\let', '\futurelet','\else','\fi','\chardef','\makeatletter','\afterground',
82 '\noexpand','\line','\mathcode','\item','\section','\mbox','\declarerobustcommand'
85 return str_ireplace($tex_blacklist, 'forbiddenkeyword', $texexp);
88 function filter_tex_get_cmd($pathname, $texexp) {
89 $texexp = filter_tex_sanitize_formula($texexp);
90 $texexp = escapeshellarg($texexp);
91 $executable = filter_tex_get_executable(false);
93 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
94 $executable = str_replace(' ', '^ ', $executable);
95 return "$executable ++ -e \"$pathname\" -- $texexp";
97 } else {
98 return "\"$executable\" -e \"$pathname\" -- $texexp";
103 * Purge all caches when settings changed.
105 function filter_tex_updatedcallback($name) {
106 global $CFG, $DB;
107 reset_text_filters_cache();
109 if (file_exists("$CFG->dataroot/filter/tex")) {
110 remove_dir("$CFG->dataroot/filter/tex");
112 if (file_exists("$CFG->dataroot/filter/algebra")) {
113 remove_dir("$CFG->dataroot/filter/algebra");
115 if (file_exists("$CFG->tempdir/latex")) {
116 remove_dir("$CFG->tempdir/latex");
119 $DB->delete_records('cache_filters', array('filter'=>'tex'));
120 $DB->delete_records('cache_filters', array('filter'=>'algebra'));
122 $pathlatex = get_config('filter_tex', 'pathlatex');
123 if ($pathlatex === false) {
124 // detailed settings not present yet
125 return;
128 $pathlatex = trim($pathlatex, " '\"");
129 $pathdvips = trim(get_config('filter_tex', 'pathdvips'), " '\"");
130 $pathconvert = trim(get_config('filter_tex', 'pathconvert'), " '\"");
131 $pathdvisvgm = trim(get_config('filter_tex', 'pathdvisvgm'), " '\"");
133 $supportedformats = array('gif');
134 if ((is_file($pathlatex) && is_executable($pathlatex)) &&
135 (is_file($pathdvips) && is_executable($pathdvips))) {
136 if (is_file($pathconvert) && is_executable($pathconvert)) {
137 $supportedformats[] = 'png';
139 if (is_file($pathdvisvgm) && is_executable($pathdvisvgm)) {
140 $supportedformats[] = 'svg';
143 if (!in_array(get_config('filter_tex', 'convertformat'), $supportedformats)) {
144 set_config('convertformat', array_pop($supportedformats), 'filter_tex');