Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Moodle - Filter for converting simple calculator-type algebraic
  20   * expressions to cached gif images
  21   *
  22   * @package    filter
  23   * @subpackage algebra
  24   * @copyright  2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu
  25   *             Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  //-------------------------------------------------------------------------
  32  // NOTE: This Moodle text filter converts algebraic expressions delimited
  33  // by either @@...@@ or by <algebra...>...</algebra> tags
  34  // first converts it to TeX using WeBWorK algebra parser Perl library
  35  // AlgParser.pm, part of the WeBWorK distribution obtained from
  36  // http://webhost.math.rochester.edu/downloadwebwork/
  37  // then converts the TeX to gif images using
  38  // mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by
  39  // John Forkosh john@forkosh.com. The mimetex.cgi ELF binary compiled for Linux i386
  40  // as well as AlgParser.pm are included with this distribution.
  41  // Note that there may be patent restrictions on the production of gif images
  42  // in Canada and some parts of Western Europe and Japan until July 2004.
  43  //-------------------------------------------------------------------------
  44  // You will then need to edit your moodle/config.php to invoke mathml_filter.php
  45  //-------------------------------------------------------------------------
  46  
  47  function filter_algebra_image($imagefile, $tex= "", $height="", $width="", $align="middle") {
  48    // Given the path to a picture file in a course, or a URL,
  49    // this function includes the picture in the page.
  50    global $CFG, $OUTPUT;
  51  
  52    $output = "";
  53    $style = 'style="border:0px; vertical-align:'.$align.';';
  54    $title = '';
  55    if ($tex) {
  56      $tex = html_entity_decode($tex, ENT_QUOTES, 'UTF-8');
  57      $title = 'title="'.s($tex).'"';
  58    }
  59    if ($height) {
  60      $style .= " height:{$height}px;";
  61    }
  62    if ($width) {
  63      $style .= " width:{$width}px;";
  64    }
  65    $style .= '"';
  66    $anchorcontents = '';
  67    if ($imagefile) {
  68      $anchorcontents .= "<img $title alt=\"".s($tex)."\" src=\"";
  69      if ($CFG->slasharguments) {        // Use this method if possible for better caching
  70        $anchorcontents .= "$CFG->wwwroot/filter/algebra/pix.php/$imagefile";
  71      } else {
  72        $anchorcontents .= "$CFG->wwwroot/filter/algebra/pix.php?file=$imagefile";
  73      }
  74      $anchorcontents .= "\" $style />";
  75  
  76      if (!file_exists("$CFG->dataroot/filter/algebra/$imagefile") && has_capability('moodle/site:config', context_system::instance())) {
  77          $link = '/filter/algebra/algebradebug.php';
  78          $action = null;
  79      } else {
  80          $link = new moodle_url('/filter/tex/displaytex.php', array('texexp'=>$tex));
  81          $action = new popup_action('click', $link, 'popup', array('width'=>320,'height'=>240)); //TODO: the popups do not work when text caching is enabled!!
  82      }
  83      $output .= $OUTPUT->action_link($link, $anchorcontents, $action, array('title'=>'TeX'));
  84  
  85    } else {
  86      $output .= "Error: must pass URL or course";
  87    }
  88    return $output;
  89  }
  90  
  91  class filter_algebra extends moodle_text_filter {
  92      public function filter($text, array $options = array()){
  93          global $CFG, $DB;
  94  
  95          /// Do a quick check using stripos to avoid unnecessary wor
  96          if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) {
  97              return $text;
  98          }
  99  
 100  //restrict filtering to forum 130 (Maths Tools on moodle.org)
 101  #    $scriptname = $_SERVER['SCRIPT_NAME'];
 102  #    if (!strstr($scriptname,'/forum/')) {
 103  #        return $text;
 104  #    }
 105  #    if (strstr($scriptname,'post.php')) {
 106  #        $parent = forum_get_post_full($_GET['reply']);
 107  #        $discussion = $DB->get_record("forum_discussions",array("id"=>$parent->discussion));
 108  #    } else if (strstr($scriptname,'discuss.php')) {
 109  #        $discussion = $DB->get_record("forum_discussions",array("id"=>$_GET['d']));
 110  #    } else {
 111  #        return $text;
 112  #    }
 113  #    if ($discussion->forum != 130) {
 114  #        return $text;
 115  #    }
 116  
 117          preg_match_all('/@(@@+)([^@])/',$text,$matches);
 118          for ($i=0;$i<count($matches[0]);$i++) {
 119              $replacement = str_replace('@','&#x00040;',$matches[1][$i]).$matches[2][$i];
 120              $text = str_replace($matches[0][$i],$replacement,$text);
 121          }
 122  
 123          // <algebra> some algebraic input expression </algebra>
 124          // or @@ some algebraic input expression @@
 125  
 126          preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
 127          for ($i=0; $i<count($matches[0]); $i++) {
 128              $algebra = $matches[1][$i] . $matches[2][$i];
 129  
 130              // Look for some common false positives, and skip processing them.
 131              if ($algebra == 'PLUGINFILE' || $algebra == 'DRAFTFILE') {
 132                  // Raw pluginfile URL.
 133                  continue;
 134              }
 135              if (preg_match('/^ -\d+(,\d+)? \+\d+(,\d+)? $/', $algebra)) {
 136                  // Part of a unified diff.
 137                  continue;
 138              }
 139  
 140              $algebra = str_replace('<nolink>','',$algebra);
 141              $algebra = str_replace('</nolink>','',$algebra);
 142              $algebra = str_replace('<span class="nolink">','',$algebra);
 143              $algebra = str_replace('</span>','',$algebra);
 144              $align = "middle";
 145              if (preg_match('/^align=bottom /',$algebra)) {
 146                $align = "text-bottom";
 147                $algebra = preg_replace('/^align=bottom /','',$algebra);
 148              } else if (preg_match('/^align=top /',$algebra)) {
 149                $align = "text-top";
 150                $algebra = preg_replace('/^align=top /','',$algebra);
 151              }
 152              $md5 =  md5($algebra);
 153              $filename =  $md5  . ".gif";
 154              if (! $texcache = $DB->get_record("cache_filters",array("filter"=>"algebra", "md5key"=>$md5))) {
 155                 $algebra = str_replace('&lt;','<',$algebra);
 156                 $algebra = str_replace('&gt;','>',$algebra);
 157                 $algebra = str_replace('<>','#',$algebra);
 158                 $algebra = str_replace('<=','%',$algebra);
 159                 $algebra = str_replace('>=','!',$algebra);
 160                 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
 161                 $algebra = str_replace('delta','zdelta',$algebra);
 162                 $algebra = str_replace('beta','bita',$algebra);
 163                 $algebra = str_replace('theta','thita',$algebra);
 164                 $algebra = str_replace('zeta','zita',$algebra);
 165                 $algebra = str_replace('eta','xeta',$algebra);
 166                 $algebra = str_replace('epsilon','zepslon',$algebra);
 167                 $algebra = str_replace('upsilon','zupslon',$algebra);
 168                 $algebra = preg_replace('!\r\n?!',' ',$algebra);
 169                 $algebra = escapeshellarg($algebra);
 170                 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
 171                    $cmd  = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
 172                 } else {
 173                    $cmd  = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
 174                 }
 175                 $texexp = `$cmd`;
 176                 if (preg_match('/parsehilight/',$texexp)) {
 177                   $text = str_replace( $matches[0][$i],"<b>Syntax error:</b> " . $texexp,$text);
 178                 } else if ($texexp) {
 179                    $texexp = str_replace('zeroplace','',$texexp);
 180                    $texexp = str_replace('#','\not= ',$texexp);
 181                    $texexp = str_replace('%','\leq ',$texexp);
 182                    $texexp = str_replace('!','\geq ',$texexp);
 183                    $texexp = str_replace('\left{','{',$texexp);
 184                    $texexp = str_replace('\right}','}',$texexp);
 185                    $texexp = str_replace('\fun',' ',$texexp);
 186                    $texexp = str_replace('infty','\infty',$texexp);
 187                    $texexp = str_replace('alpha','\alpha',$texexp);
 188                    $texexp = str_replace('gamma','\gamma',$texexp);
 189                    $texexp = str_replace('iota','\iota',$texexp);
 190                    $texexp = str_replace('kappa','\kappa',$texexp);
 191                    $texexp = str_replace('lambda','\lambda',$texexp);
 192                    $texexp = str_replace('mu','\mu',$texexp);
 193                    $texexp = str_replace('nu','\nu',$texexp);
 194                    $texexp = str_replace('xi','\xi',$texexp);
 195                    $texexp = str_replace('rho','\rho',$texexp);
 196                    $texexp = str_replace('sigma','\sigma',$texexp);
 197                    $texexp = str_replace('tau','\tau',$texexp);
 198                    $texexp = str_replace('phi','\phi',$texexp);
 199                    $texexp = str_replace('chi','\chi',$texexp);
 200                    $texexp = str_replace('psi','\psi',$texexp);
 201                    $texexp = str_replace('omega','\omega',$texexp);
 202                    $texexp = str_replace('zdelta','\delta',$texexp);
 203                    $texexp = str_replace('bita','\beta',$texexp);
 204                    $texexp = str_replace('thita','\theta',$texexp);
 205                    $texexp = str_replace('zita','\zeta',$texexp);
 206                    $texexp = str_replace('xeta','\eta',$texexp);
 207                    $texexp = str_replace('zepslon','\epsilon',$texexp);
 208                    $texexp = str_replace('zupslon','\upsilon',$texexp);
 209                    $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
 210                    $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
 211                    $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
 212                    $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
 213                    $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
 214                    $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
 215                    $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
 216                    $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
 217                    $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
 218                    $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
 219                    $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
 220                    $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
 221                    $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
 222                    //$texexp = preg_replace('/\\\frac{(.+?)}{\\\left\((.+?)\\\right\)}/s','\frac{'."\$1}{\$2}",$texexp);
 223                    $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
 224                    $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
 225                    $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
 226                    $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
 227                    $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
 228                    $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
 229                    $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
 230                    // Remove a forbidden keyword.
 231                    $texexp = str_replace('\mbox', '', $texexp);
 232                    $texcache = new stdClass();
 233                    $texcache->filter = 'algebra';
 234                    $texcache->version = 1;
 235                    $texcache->md5key = $md5;
 236                    $texcache->rawtext = $texexp;
 237                    $texcache->timemodified = time();
 238                    $DB->insert_record("cache_filters", $texcache, false);
 239                    $text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texexp, '', '', $align), $text);
 240                 } else {
 241                    $text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
 242                 }
 243              } else {
 244                 $text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texcache->rawtext), $text);
 245              }
 246          }
 247          return $text;
 248      }
 249  }
 250  
 251