MDL-30070 message: Optimised search for users over multiple courses
[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
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.";
38
39 $error_message2 = "Custom mimetex is not executable!<br /><br />";
40
41 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
42 return "$CFG->dirroot/filter/tex/mimetex.exe";
43 }
44
45 $custom_commandpath = "$CFG->dirroot/filter/tex/mimetex";
46 if (file_exists($custom_commandpath)) {
47 if (is_executable($custom_commandpath)) {
48 return $custom_commandpath;
49 } else {
6e7ead3e 50 print_error('mimetexnotexecutable', 'error');
220a90c5 51 }
52 }
53
54 switch (PHP_OS) {
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";
58 }
59
6e7ead3e 60 print_error('mimetexisnotexist', 'error');
220a90c5 61}
62
35716b86 63function filter_tex_sanitize_formula($texexp) {
c94985ef 64 /// Check $texexp against blacklist (whitelisting could be more complete but also harder to maintain)
65 $tex_blacklist = array(
f6a5c4b0 66 'include','command','loop','repeat','open','toks','output',
c94985ef 67 'input','catcode','name','^^',
f6a5c4b0 68 '\def','\edef','\gdef','\xdef',
c94985ef 69 '\every','\errhelp','\errorstopmode','\scrollmode','\nonstopmode',
70 '\batchmode','\read','\write','csname','\newhelp','\uppercase',
71 '\lowercase','\relax','\aftergroup',
f6a5c4b0 72 '\afterassignment','\expandafter','\noexpand','\special',
73 '\let', '\futurelet','\else','\fi','\chardef','\makeatletter','\afterground',
74 '\noexpand','\line','\mathcode','\item','\section','\mbox','\declarerobustcommand'
c94985ef 75 );
76
77 return str_ireplace($tex_blacklist, 'forbiddenkeyword', $texexp);
78}
220a90c5 79
35716b86
PS
80function filter_tex_get_cmd($pathname, $texexp) {
81 $texexp = filter_tex_sanitize_formula($texexp);
220a90c5 82 $texexp = escapeshellarg($texexp);
35716b86 83 $executable = filter_tex_get_executable(false);
220a90c5 84
85 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
7a85562e 86 $executable = str_replace(' ', '^ ', $executable);
87 return "$executable ++ -e \"$pathname\" -- $texexp";
220a90c5 88
89 } else {
90 return "\"$executable\" -e \"$pathname\" -- $texexp";
91 }
92}
93
94/**
95 * Purge all caches when settings changed.
96 */
97function filter_tex_updatedcallback($name) {
8618fd2a 98 global $CFG, $DB;
109e3cb2 99 reset_text_filters_cache();
100
220a90c5 101 if (file_exists("$CFG->dataroot/filter/tex")) {
102 remove_dir("$CFG->dataroot/filter/tex");
103 }
104 if (file_exists("$CFG->dataroot/filter/algebra")) {
105 remove_dir("$CFG->dataroot/filter/algebra");
106 }
7aa06e6d
TL
107 if (file_exists("$CFG->tempdir/latex")) {
108 remove_dir("$CFG->tempdir/latex");
220a90c5 109 }
110
8618fd2a 111 $DB->delete_records('cache_filters', array('filter'=>'tex'));
112 $DB->delete_records('cache_filters', array('filter'=>'algebra'));
35716b86
PS
113
114 if (!isset($CFG->filter_tex_pathlatex)) {
115 // detailed settings not present yet
116 return;
117 }
118
cf4e7548
I
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');
124 }
220a90c5 125}
126
4317f92f 127