[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/filter/tex/ -> lib.php (source)

   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      $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      if ($pathmimetex = get_config('filter_tex', 'pathmimetex')) {
  46          if (is_executable($pathmimetex)) {
  47              return $pathmimetex;
  48          } else {
  49              print_error('mimetexnotexecutable', 'error');
  50          }
  51      }
  52  
  53      $custom_commandpath = "$CFG->dirroot/filter/tex/mimetex";
  54      if (file_exists($custom_commandpath)) {
  55          if (is_executable($custom_commandpath)) {
  56              return $custom_commandpath;
  57          } else {
  58              print_error('mimetexnotexecutable', 'error');
  59          }
  60      }
  61  
  62      switch (PHP_OS) {
  63          case "Linux":   return "$CFG->dirroot/filter/tex/mimetex.linux";
  64          case "Darwin":  return "$CFG->dirroot/filter/tex/mimetex.darwin";
  65          case "FreeBSD": return "$CFG->dirroot/filter/tex/mimetex.freebsd";
  66      }
  67  
  68      print_error('mimetexisnotexist', 'error');
  69  }
  70  
  71  function filter_tex_sanitize_formula($texexp) {
  72      /// Check $texexp against blacklist (whitelisting could be more complete but also harder to maintain)
  73      $tex_blacklist = array(
  74          'include','command','loop','repeat','open','toks','output',
  75          'input','catcode','name','^^',
  76          '\def','\edef','\gdef','\xdef',
  77          '\every','\errhelp','\errorstopmode','\scrollmode','\nonstopmode',
  78          '\batchmode','\read','\write','csname','\newhelp','\uppercase',
  79          '\lowercase','\relax','\aftergroup',
  80          '\afterassignment','\expandafter','\noexpand','\special',
  81          '\let', '\futurelet','\else','\fi','\chardef','\makeatletter','\afterground',
  82          '\noexpand','\line','\mathcode','\item','\section','\mbox','\declarerobustcommand'
  83      );
  84  
  85      return  str_ireplace($tex_blacklist, 'forbiddenkeyword', $texexp);
  86  }
  87  
  88  function filter_tex_get_cmd($pathname, $texexp) {
  89      $texexp = filter_tex_sanitize_formula($texexp);
  90      $texexp = escapeshellarg($texexp);
  91      $executable = filter_tex_get_executable(false);
  92  
  93      if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
  94          $executable = str_replace(' ', '^ ', $executable);
  95          return "$executable ++ -e  \"$pathname\" -- $texexp";
  96  
  97      } else {
  98          return "\"$executable\" -e \"$pathname\" -- $texexp";
  99      }
 100  }
 101  
 102  /**
 103   * Purge all caches when settings changed.
 104   */
 105  function filter_tex_updatedcallback($name) {
 106      global $CFG, $DB;
 107      reset_text_filters_cache();
 108  
 109      if (file_exists("$CFG->dataroot/filter/tex")) {
 110          remove_dir("$CFG->dataroot/filter/tex");
 111      }
 112      if (file_exists("$CFG->dataroot/filter/algebra")) {
 113          remove_dir("$CFG->dataroot/filter/algebra");
 114      }
 115      if (file_exists("$CFG->tempdir/latex")) {
 116          remove_dir("$CFG->tempdir/latex");
 117      }
 118  
 119      $DB->delete_records('cache_filters', array('filter'=>'tex'));
 120      $DB->delete_records('cache_filters', array('filter'=>'algebra'));
 121  
 122      $pathlatex = get_config('filter_tex', 'pathlatex');
 123      if ($pathlatex === false) {
 124          // detailed settings not present yet
 125          return;
 126      }
 127  
 128      $pathlatex = trim($pathlatex, " '\"");
 129      $pathdvips = trim(get_config('filter_tex', 'pathdvips'), " '\"");
 130      $pathconvert = trim(get_config('filter_tex', 'pathconvert'), " '\"");
 131      $pathdvisvgm = trim(get_config('filter_tex', 'pathdvisvgm'), " '\"");
 132  
 133      $supportedformats = array('gif');
 134      if ((is_file($pathlatex) && is_executable($pathlatex)) &&
 135              (is_file($pathdvips) && is_executable($pathdvips))) {
 136          if (is_file($pathconvert) && is_executable($pathconvert)) {
 137               $supportedformats[] = 'png';
 138          }
 139          if (is_file($pathdvisvgm) && is_executable($pathdvisvgm)) {
 140               $supportedformats[] = 'svg';
 141          }
 142      }
 143      if (!in_array(get_config('filter_tex', 'convertformat'), $supportedformats)) {
 144          set_config('convertformat', array_pop($supportedformats), 'filter_tex');
 145      }
 146  
 147  }
 148  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1