MDL-20830 workshop: better deadlines defaults
[moodle.git] / filter / tex / lib.php
blob3253ab9bc24557363e157306823baba4131c2863
1 <?php //$Id$
3 function tex_filter_get_executable($debug=false) {
4 global $CFG;
6 $error_message1 = "Your system is not configured to run mimeTeX. You need to download the appropriate<br />"
7 ."executable for you ".PHP_OS." platform from <a href=\"http://moodle.org/download/mimetex/\">"
8 ."http://moodle.org/download/mimetex/</a>, or obtain the C source<br /> "
9 ."from <a href=\"http://www.forkosh.com/mimetex.zip\">"
10 ."http://www.forkosh.com/mimetex.zip</a>, compile it and "
11 ."put the executable into your<br /> moodle/filter/tex/ directory.";
13 $error_message2 = "Custom mimetex is not executable!<br /><br />";
15 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
16 return "$CFG->dirroot/filter/tex/mimetex.exe";
19 $custom_commandpath = "$CFG->dirroot/filter/tex/mimetex";
20 if (file_exists($custom_commandpath)) {
21 if (is_executable($custom_commandpath)) {
22 return $custom_commandpath;
23 } else {
24 error($error_message2.$error_message1);
28 switch (PHP_OS) {
29 case "Linux": return "$CFG->dirroot/filter/tex/mimetex.linux";
30 case "Darwin": return "$CFG->dirroot/filter/tex/mimetex.darwin";
31 case "FreeBSD": return "$CFG->dirroot/filter/tex/mimetex.freebsd";
34 error($error_message1);
37 function tex_sanitize_formula($texexp) {
38 /// Check $texexp against blacklist (whitelisting could be more complete but also harder to maintain)
39 $tex_blacklist = array(
40 'include','command','loop','repeat','open','toks','output',
41 'input','catcode','name','^^',
42 '\def','\edef','\gdef','\xdef',
43 '\every','\errhelp','\errorstopmode','\scrollmode','\nonstopmode',
44 '\batchmode','\read','\write','csname','\newhelp','\uppercase',
45 '\lowercase','\relax','\aftergroup',
46 '\afterassignment','\expandafter','\noexpand','\special',
47 '\let', '\futurelet','\else','\fi','\chardef','\makeatletter','\afterground',
48 '\noexpand','\line','\mathcode','\item','\section','\mbox','\declarerobustcommand'
51 return str_ireplace($tex_blacklist, 'forbiddenkeyword', $texexp);
54 function tex_filter_get_cmd($pathname, $texexp) {
55 $texexp = tex_sanitize_formula($texexp);
56 $texexp = escapeshellarg($texexp);
57 $executable = tex_filter_get_executable(false);
59 if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
60 $executable = str_replace(' ', '^ ', $executable);
61 return "$executable ++ -e \"$pathname\" -- $texexp";
63 } else {
64 return "\"$executable\" -e \"$pathname\" -- $texexp";
68 /**
69 * Purge all caches when settings changed.
71 function filter_tex_updatedcallback($name) {
72 global $CFG;
73 reset_text_filters_cache();
75 if (file_exists("$CFG->dataroot/filter/tex")) {
76 remove_dir("$CFG->dataroot/filter/tex");
78 if (file_exists("$CFG->dataroot/filter/algebra")) {
79 remove_dir("$CFG->dataroot/filter/algebra");
81 if (file_exists("$CFG->dataroot/temp/latex")) {
82 remove_dir("$CFG->dataroot/temp/latex");
85 delete_records('cache_filters', 'filter', 'tex');
86 delete_records('cache_filters', 'filter', 'algebra');