/
lib.php
148 lines (125 loc) · 5.61 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* TeX filter library functions.
*
* @package filter
* @subpackage tex
* @copyright 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu
* Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
function filter_texwjax_get_executable($debug=false) {
global $CFG;
$error_message1 = "Your system is not configured to run mimeTeX. You need to download the appropriate<br />"
."executable for you ".PHP_OS." platform from <a href=\"http://moodle.org/download/mimetex/\">"
."http://moodle.org/download/mimetex/</a>, or obtain the C source<br /> "
."from <a href=\"http://www.forkosh.com/mimetex.zip\">"
."http://www.forkosh.com/mimetex.zip</a>, compile it and "
."put the executable into your<br /> moodle/filter/texwjax/ directory.";
$error_message2 = "Custom mimetex is not executable!<br /><br />";
if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
return "$CFG->dirroot/filter/texwjax/mimetex.exe";
}
if ($pathmimetex = get_config('filter_texwjax', 'pathmimetex')) {
if (is_executable($pathmimetex)) {
return $pathmimetex;
} else {
print_error('mimetexnotexecutable', 'error');
}
}
$custom_commandpath = "$CFG->dirroot/filter/texwjax/mimetex";
if (file_exists($custom_commandpath)) {
if (is_executable($custom_commandpath)) {
return $custom_commandpath;
} else {
print_error('mimetexnotexecutable', 'error');
}
}
switch (PHP_OS) {
case "Linux": return "$CFG->dirroot/filter/texwjax/mimetex.linux";
case "Darwin": return "$CFG->dirroot/filter/texwjax/mimetex.darwin";
case "FreeBSD": return "$CFG->dirroot/filter/texwjax/mimetex.freebsd";
}
print_error('mimetexisnotexist', 'error');
}
function filter_texwjax_sanitize_formula($texexp) {
/// Check $texexp against blacklist (whitelisting could be more complete but also harder to maintain)
$tex_blacklist = array(
'include','command','loop','repeat','open','toks','output',
'input','catcode','name','^^',
'\def','\edef','\gdef','\xdef',
'\every','\errhelp','\errorstopmode','\scrollmode','\nonstopmode',
'\batchmode','\read','\write','csname','\newhelp','\uppercase',
'\lowercase','\relax','\aftergroup',
'\afterassignment','\expandafter','\noexpand','\special',
'\let', '\futurelet','\else','\fi','\chardef','\makeatletter','\afterground',
'\noexpand','\line','\mathcode','\item','\section','\mbox','\declarerobustcommand'
);
return str_ireplace($tex_blacklist, 'forbiddenkeyword', $texexp);
}
function filter_texwjax_get_cmd($pathname, $texexp) {
$texexp = filter_texwjax_sanitize_formula($texexp);
$texexp = escapeshellarg($texexp);
$executable = filter_texwjax_get_executable(false);
if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
$executable = str_replace(' ', '^ ', $executable);
return "$executable ++ -e \"$pathname\" -- $texexp";
} else {
return "\"$executable\" -e \"$pathname\" -- $texexp";
}
}
/**
* Purge all caches when settings changed.
*/
function filter_texwjax_updatedcallback($name) {
global $CFG, $DB;
reset_text_filters_cache();
if (file_exists("$CFG->dataroot/filter/texwjax")) {
remove_dir("$CFG->dataroot/filter/texwjax");
}
if (file_exists("$CFG->dataroot/filter/algebra")) {
remove_dir("$CFG->dataroot/filter/algebra");
}
if (file_exists("$CFG->tempdir/latex")) {
remove_dir("$CFG->tempdir/latex");
}
$DB->delete_records('cache_filters', array('filter'=>'texwjax'));
$DB->delete_records('cache_filters', array('filter'=>'algebra'));
$pathlatex = get_config('filter_texwjax', 'pathlatex');
if ($pathlatex === false) {
// detailed settings not present yet
return;
}
$pathlatex = trim($pathlatex, " '\"");
$pathdvips = trim(get_config('filter_texwjax', 'pathdvips'), " '\"");
$pathconvert = trim(get_config('filter_texwjax', 'pathconvert'), " '\"");
$pathdvisvgm = trim(get_config('filter_texwjax', 'pathdvisvgm'), " '\"");
$supportedformats = array('gif');
if ((is_file($pathlatex) && is_executable($pathlatex)) &&
(is_file($pathdvips) && is_executable($pathdvips))) {
if (is_file($pathconvert) && is_executable($pathconvert)) {
$supportedformats[] = 'png';
}
if (is_file($pathdvisvgm) && is_executable($pathdvisvgm)) {
$supportedformats[] = 'svg';
}
}
if (!in_array(get_config('filter_texwjax', 'convertformat'), $supportedformats)) {
set_config('convertformat', array_pop($supportedformats), 'filter_texwjax');
}
}