Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/filter/tex/ -> pix.php (source)

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

   1  <?php
   2        // This function fetches math. images from the data directory
   3        // If not, it obtains the corresponding TeX expression from the cache_tex db table
   4        // and uses mimeTeX to create the image file
   5  
   6  // disable moodle specific debug messages and any errors in output
   7  define('NO_DEBUG_DISPLAY', true);
   8  define('NO_MOODLE_COOKIES', true); // Because it interferes with caching
   9  
  10      require_once('../../config.php');
  11  
  12      if (!filter_is_enabled('tex')) {
  13          throw new \moodle_exception('filternotenabled');
  14      }
  15  
  16      require_once($CFG->libdir.'/filelib.php');
  17      require_once($CFG->dirroot.'/filter/tex/lib.php');
  18      require_once($CFG->dirroot.'/filter/tex/latex.php');
  19  
  20      $cmd    = '';               // Initialise these variables
  21      $status = '';
  22  
  23      $relativepath = get_file_argument();
  24  
  25      $args = explode('/', trim($relativepath, '/'));
  26  
  27      if (count($args) == 1) {
  28          $image    = $args[0];
  29          $pathname = $CFG->dataroot.'/filter/tex/'.$image;
  30      } else {
  31          throw new \moodle_exception('invalidarguments', 'error');
  32      }
  33  
  34      if (!file_exists($pathname)) {
  35          $convertformat = get_config('filter_tex', 'convertformat');
  36          if (strpos($image, '.png')) {
  37              $convertformat = 'png';
  38          }
  39          $md5 = str_replace(".{$convertformat}", '', $image);
  40          if ($texcache = $DB->get_record('cache_filters', array('filter'=>'tex', 'md5key'=>$md5))) {
  41              if (!file_exists($CFG->dataroot.'/filter/tex')) {
  42                  make_upload_directory('filter/tex');
  43              }
  44  
  45              // try and render with latex first
  46              $latex = new latex();
  47              $density = get_config('filter_tex', 'density');
  48              $background = get_config('filter_tex', 'latexbackground');
  49              $texexp = $texcache->rawtext; // the entities are now decoded before inserting to DB
  50              $lateximage = $latex->render($texexp, $image, 12, $density, $background);
  51              if ($lateximage) {
  52                  copy($lateximage, $pathname);
  53              } else {
  54                  // failing that, use mimetex
  55                  $texexp = $texcache->rawtext;
  56                  $texexp = str_replace('&lt;', '<', $texexp);
  57                  $texexp = str_replace('&gt;', '>', $texexp);
  58                  $texexp = preg_replace('!\r\n?!', ' ', $texexp);
  59                  $texexp = '\Large '.$texexp;
  60                  $cmd = filter_tex_get_cmd($pathname, $texexp);
  61                  system($cmd, $status);
  62              }
  63          }
  64      }
  65  
  66      if (file_exists($pathname)) {
  67          send_file($pathname, $image);
  68      } else {
  69          if (debugging()) {
  70              echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
  71              echo "Image not found!<br />";
  72              echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a>";
  73          } else {
  74              echo "Image not found!<br />";
  75              echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a><br />";
  76              echo "Please turn on debug mode in site configuration to see more info here.";
  77          }
  78      }