MDL-48846 filter: Stop reffering to invalid moodle.org/download/mimetex
[moodle.git] / filter / tex / lib.php
CommitLineData
4317f92f 1<?php
f7f0909c
PS
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 * TeX filter library functions.
19 *
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
25 */
220a90c5 26
35716b86
PS
27defined('MOODLE_INTERNAL') || die();
28
29function filter_tex_get_executable($debug=false) {
220a90c5 30 global $CFG;
31
220a90c5 32 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
33 return "$CFG->dirroot/filter/tex/mimetex.exe";
34 }
35
7a372321
TG
36 if ($pathmimetex = get_config('filter_tex', 'pathmimetex')) {
37 if (is_executable($pathmimetex)) {
38 return $pathmimetex;
39 } else {
40 print_error('mimetexnotexecutable', 'error');
41 }
42 }
43
220a90c5 44 $custom_commandpath = "$CFG->dirroot/filter/tex/mimetex";
45 if (file_exists($custom_commandpath)) {
46 if (is_executable($custom_commandpath)) {
47 return $custom_commandpath;
48 } else {
6e7ead3e 49 print_error('mimetexnotexecutable', 'error');
220a90c5 50 }
51 }
52
53 switch (PHP_OS) {
54 case "Linux": return "$CFG->dirroot/filter/tex/mimetex.linux";
55 case "Darwin": return "$CFG->dirroot/filter/tex/mimetex.darwin";
56 case "FreeBSD": return "$CFG->dirroot/filter/tex/mimetex.freebsd";
57 }
58
6e7ead3e 59 print_error('mimetexisnotexist', 'error');
220a90c5 60}
61
35716b86 62function filter_tex_sanitize_formula($texexp) {
c94985ef 63 /// Check $texexp against blacklist (whitelisting could be more complete but also harder to maintain)
64 $tex_blacklist = array(
f6a5c4b0 65 'include','command','loop','repeat','open','toks','output',
c94985ef 66 'input','catcode','name','^^',
f6a5c4b0 67 '\def','\edef','\gdef','\xdef',
c94985ef 68 '\every','\errhelp','\errorstopmode','\scrollmode','\nonstopmode',
69 '\batchmode','\read','\write','csname','\newhelp','\uppercase',
70 '\lowercase','\relax','\aftergroup',
f6a5c4b0 71 '\afterassignment','\expandafter','\noexpand','\special',
72 '\let', '\futurelet','\else','\fi','\chardef','\makeatletter','\afterground',
73 '\noexpand','\line','\mathcode','\item','\section','\mbox','\declarerobustcommand'
c94985ef 74 );
75
76 return str_ireplace($tex_blacklist, 'forbiddenkeyword', $texexp);
77}
220a90c5 78
35716b86
PS
79function filter_tex_get_cmd($pathname, $texexp) {
80 $texexp = filter_tex_sanitize_formula($texexp);
220a90c5 81 $texexp = escapeshellarg($texexp);
35716b86 82 $executable = filter_tex_get_executable(false);
220a90c5 83
84 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
7a85562e 85 $executable = str_replace(' ', '^ ', $executable);
86 return "$executable ++ -e \"$pathname\" -- $texexp";
220a90c5 87
88 } else {
89 return "\"$executable\" -e \"$pathname\" -- $texexp";
90 }
91}
92
93/**
94 * Purge all caches when settings changed.
95 */
96function filter_tex_updatedcallback($name) {
8618fd2a 97 global $CFG, $DB;
109e3cb2 98 reset_text_filters_cache();
99
220a90c5 100 if (file_exists("$CFG->dataroot/filter/tex")) {
101 remove_dir("$CFG->dataroot/filter/tex");
102 }
103 if (file_exists("$CFG->dataroot/filter/algebra")) {
104 remove_dir("$CFG->dataroot/filter/algebra");
105 }
7aa06e6d
TL
106 if (file_exists("$CFG->tempdir/latex")) {
107 remove_dir("$CFG->tempdir/latex");
220a90c5 108 }
109
8618fd2a 110 $DB->delete_records('cache_filters', array('filter'=>'tex'));
111 $DB->delete_records('cache_filters', array('filter'=>'algebra'));
35716b86 112
7a372321
TG
113 $pathlatex = get_config('filter_tex', 'pathlatex');
114 if ($pathlatex === false) {
35716b86
PS
115 // detailed settings not present yet
116 return;
117 }
118
a275c52f
DT
119 $pathlatex = trim($pathlatex, " '\"");
120 $pathdvips = trim(get_config('filter_tex', 'pathdvips'), " '\"");
121 $pathconvert = trim(get_config('filter_tex', 'pathconvert'), " '\"");
122 $pathdvisvgm = trim(get_config('filter_tex', 'pathdvisvgm'), " '\"");
7a372321 123
a57a336f
DT
124 $supportedformats = array('gif');
125 if ((is_file($pathlatex) && is_executable($pathlatex)) &&
126 (is_file($pathdvips) && is_executable($pathdvips))) {
127 if (is_file($pathconvert) && is_executable($pathconvert)) {
128 $supportedformats[] = 'png';
129 }
130 if (is_file($pathdvisvgm) && is_executable($pathdvisvgm)) {
131 $supportedformats[] = 'svg';
132 }
133 }
134 if (!in_array(get_config('filter_tex', 'convertformat'), $supportedformats)) {
135 set_config('convertformat', array_pop($supportedformats), 'filter_tex');
cf4e7548 136 }
220a90c5 137
a57a336f 138}
4317f92f 139