| 1 | efrain | 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.
 | 
        
           |  |  | 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 |  */
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | function filter_tex_get_executable($debug=false) {
 | 
        
           |  |  | 30 |     global $CFG;
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |     if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
 | 
        
           |  |  | 33 |         return "$CFG->dirroot/filter/tex/mimetex.exe";
 | 
        
           |  |  | 34 |     }
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |     if ($pathmimetex = get_config('filter_tex', 'pathmimetex')) {
 | 
        
           |  |  | 37 |         if (is_executable($pathmimetex)) {
 | 
        
           |  |  | 38 |             return $pathmimetex;
 | 
        
           |  |  | 39 |         } else {
 | 
        
           |  |  | 40 |             throw new \moodle_exception('mimetexnotexecutable', 'error');
 | 
        
           |  |  | 41 |         }
 | 
        
           |  |  | 42 |     }
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 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 {
 | 
        
           |  |  | 49 |             throw new \moodle_exception('mimetexnotexecutable', 'error');
 | 
        
           |  |  | 50 |         }
 | 
        
           |  |  | 51 |     }
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |     switch (PHP_OS) {
 | 
        
           |  |  | 54 |         case "Darwin":  return "$CFG->dirroot/filter/tex/mimetex.darwin";
 | 
        
           |  |  | 55 |         case "FreeBSD": return "$CFG->dirroot/filter/tex/mimetex.freebsd";
 | 
        
           |  |  | 56 |         case "Linux":
 | 
        
           |  |  | 57 |             if (php_uname('m') == 'aarch64') {
 | 
        
           |  |  | 58 |                 return "$CFG->dirroot/filter/tex/mimetex.linux.aarch64";
 | 
        
           |  |  | 59 |             }
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |             return "$CFG->dirroot/filter/tex/mimetex.linux";
 | 
        
           |  |  | 62 |     }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |     throw new \moodle_exception('mimetexisnotexist', 'error');
 | 
        
           |  |  | 65 | }
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 | /**
 | 
        
           |  |  | 68 |  * Check the formula expression against the list of denied keywords.
 | 
        
           |  |  | 69 |  *
 | 
        
           |  |  | 70 |  * List of allowed could be more complete but also harder to maintain.
 | 
        
           |  |  | 71 |  *
 | 
        
           |  |  | 72 |  * @param string $texexp Formula expression to check.
 | 
        
           |  |  | 73 |  * @return string Formula expression with denied keywords replaced with 'forbiddenkeyword'.
 | 
        
           |  |  | 74 |  */
 | 
        
           |  |  | 75 | function filter_tex_sanitize_formula(string $texexp): string {
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |     $denylist = [
 | 
        
           |  |  | 78 |         'include', 'command', 'loop', 'repeat', 'open', 'toks', 'output',
 | 
        
           |  |  | 79 |         'input', 'catcode', 'name', '^^',
 | 
        
           |  |  | 80 |         '\def', '\edef', '\gdef', '\xdef',
 | 
        
           |  |  | 81 |         '\every', '\errhelp', '\errorstopmode', '\scrollmode', '\nonstopmode',
 | 
        
           |  |  | 82 |         '\batchmode', '\read', '\write', 'csname', '\newhelp', '\uppercase',
 | 
        
           |  |  | 83 |         '\lowercase', '\relax', '\aftergroup',
 | 
        
           |  |  | 84 |         '\afterassignment', '\expandafter', '\noexpand', '\special',
 | 
        
           |  |  | 85 |         '\let', '\futurelet', '\else', '\fi', '\chardef', '\makeatletter', '\afterground',
 | 
        
           |  |  | 86 |         '\noexpand', '\line', '\mathcode', '\item', '\section', '\mbox', '\declarerobustcommand',
 | 
        
           | 1441 | ariadna | 87 |         '\ExplSyntaxOn', '\pdffiledump', '\mathtex',
 | 
        
           | 1 | efrain | 88 |     ];
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |     $allowlist = ['inputenc'];
 | 
        
           |  |  | 91 |   | 
        
           | 1441 | ariadna | 92 |     // Add encoded backslash (\) versions of backslashed items to deny list.
 | 
        
           |  |  | 93 |     $encodedslashdenylist = array_map(function($value) {
 | 
        
           |  |  | 94 |         $encoded = str_replace('\\', '\', $value);
 | 
        
           |  |  | 95 |         // Return an encoded slash version if a slash is found, otherwise null so we can filter it off.
 | 
        
           |  |  | 96 |         return $encoded != $value ? $encoded : null;
 | 
        
           |  |  | 97 |     }, $denylist);
 | 
        
           |  |  | 98 |     $encodedslashdenylist = array_filter($encodedslashdenylist);
 | 
        
           |  |  | 99 |     $denylist = array_merge($denylist, $encodedslashdenylist);
 | 
        
           |  |  | 100 |   | 
        
           | 1 | efrain | 101 |     // Prepare the denylist for regular expression.
 | 
        
           |  |  | 102 |     $denylist = array_map(function($value){
 | 
        
           |  |  | 103 |         return '/' . preg_quote($value, '/') . '/i';
 | 
        
           |  |  | 104 |     }, $denylist);
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |     // Prepare the allowlist for regular expression.
 | 
        
           |  |  | 107 |     $allowlist = array_map(function($value){
 | 
        
           |  |  | 108 |         return '/\bforbiddenkeyword_(' . preg_quote($value, '/') . ')\b/i';
 | 
        
           |  |  | 109 |     }, $allowlist);
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |     // First, mangle all denied words.
 | 
        
           |  |  | 112 |     $texexp = preg_replace_callback($denylist,
 | 
        
           |  |  | 113 |         function($matches) {
 | 
        
           | 1441 | ariadna | 114 |             // Remove backslashes to make commands impotent.
 | 
        
           |  |  | 115 |             $noslashes = str_replace('\\', '', $matches[0]);
 | 
        
           |  |  | 116 |             return 'forbiddenkeyword_' . $noslashes;
 | 
        
           | 1 | efrain | 117 |         },
 | 
        
           |  |  | 118 |         $texexp
 | 
        
           |  |  | 119 |     );
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 |     // Then, change back the allowed words.
 | 
        
           |  |  | 122 |     $texexp = preg_replace_callback($allowlist,
 | 
        
           |  |  | 123 |         function($matches) {
 | 
        
           |  |  | 124 |             return $matches[1];
 | 
        
           |  |  | 125 |         },
 | 
        
           |  |  | 126 |         $texexp
 | 
        
           |  |  | 127 |     );
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 |     return $texexp;
 | 
        
           |  |  | 130 | }
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 | function filter_tex_get_cmd($pathname, $texexp) {
 | 
        
           |  |  | 133 |     $texexp = filter_tex_sanitize_formula($texexp);
 | 
        
           |  |  | 134 |     $texexp = escapeshellarg($texexp);
 | 
        
           |  |  | 135 |     $executable = filter_tex_get_executable(false);
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |     if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
 | 
        
           |  |  | 138 |         $executable = str_replace(' ', '^ ', $executable);
 | 
        
           |  |  | 139 |         return "$executable ++ -e  \"$pathname\" -- $texexp";
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 |     } else {
 | 
        
           |  |  | 142 |         return "\"$executable\" -e \"$pathname\" -- $texexp";
 | 
        
           |  |  | 143 |     }
 | 
        
           |  |  | 144 | }
 | 
        
           |  |  | 145 |   | 
        
           |  |  | 146 | /**
 | 
        
           |  |  | 147 |  * Purge all caches when settings changed.
 | 
        
           |  |  | 148 |  */
 | 
        
           |  |  | 149 | function filter_tex_updatedcallback($name) {
 | 
        
           |  |  | 150 |     global $CFG, $DB;
 | 
        
           |  |  | 151 |     reset_text_filters_cache();
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 |     if (file_exists("$CFG->dataroot/filter/tex")) {
 | 
        
           |  |  | 154 |         remove_dir("$CFG->dataroot/filter/tex");
 | 
        
           |  |  | 155 |     }
 | 
        
           |  |  | 156 |     if (file_exists("$CFG->dataroot/filter/algebra")) {
 | 
        
           |  |  | 157 |         remove_dir("$CFG->dataroot/filter/algebra");
 | 
        
           |  |  | 158 |     }
 | 
        
           |  |  | 159 |     if (file_exists("$CFG->tempdir/latex")) {
 | 
        
           |  |  | 160 |         remove_dir("$CFG->tempdir/latex");
 | 
        
           |  |  | 161 |     }
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |     $DB->delete_records('cache_filters', array('filter'=>'tex'));
 | 
        
           |  |  | 164 |     $DB->delete_records('cache_filters', array('filter'=>'algebra'));
 | 
        
           |  |  | 165 |   | 
        
           |  |  | 166 |     $pathlatex = get_config('filter_tex', 'pathlatex');
 | 
        
           |  |  | 167 |     if ($pathlatex === false) {
 | 
        
           |  |  | 168 |         // detailed settings not present yet
 | 
        
           |  |  | 169 |         return;
 | 
        
           |  |  | 170 |     }
 | 
        
           |  |  | 171 |   | 
        
           |  |  | 172 |     $pathlatex = trim($pathlatex, " '\"");
 | 
        
           |  |  | 173 |     $pathdvips = trim(get_config('filter_tex', 'pathdvips'), " '\"");
 | 
        
           |  |  | 174 |     $pathconvert = trim(get_config('filter_tex', 'pathconvert'), " '\"");
 | 
        
           |  |  | 175 |     $pathdvisvgm = trim(get_config('filter_tex', 'pathdvisvgm'), " '\"");
 | 
        
           |  |  | 176 |   | 
        
           |  |  | 177 |     $supportedformats = array('gif');
 | 
        
           |  |  | 178 |     if ((is_file($pathlatex) && is_executable($pathlatex)) &&
 | 
        
           |  |  | 179 |             (is_file($pathdvips) && is_executable($pathdvips))) {
 | 
        
           |  |  | 180 |         if (is_file($pathconvert) && is_executable($pathconvert)) {
 | 
        
           |  |  | 181 |              $supportedformats[] = 'png';
 | 
        
           |  |  | 182 |         }
 | 
        
           |  |  | 183 |         if (is_file($pathdvisvgm) && is_executable($pathdvisvgm)) {
 | 
        
           |  |  | 184 |              $supportedformats[] = 'svg';
 | 
        
           |  |  | 185 |         }
 | 
        
           |  |  | 186 |     }
 | 
        
           |  |  | 187 |     if (!in_array(get_config('filter_tex', 'convertformat'), $supportedformats)) {
 | 
        
           |  |  | 188 |         set_config('convertformat', array_pop($supportedformats), 'filter_tex');
 | 
        
           |  |  | 189 |     }
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 | }
 | 
        
           |  |  | 192 |   |