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.
/theme/ -> font.php (source)

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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   * This file is responsible for serving the fonts used in CSS.
  19   *
  20   * Note: it is recommended to use only WOFF2 (Web Open Font Format v2) fonts.
  21   *
  22   * @package   core
  23   * @copyright 2013 Petr Skoda (skodak)  {@link http://skodak.org}
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  // Disable moodle specific debug messages and any errors in output,
  28  // comment out when debugging or better look into error log!
  29  define('NO_DEBUG_DISPLAY', true);
  30  
  31  define('ABORT_AFTER_CONFIG', true);
  32  require('../config.php');
  33  
  34  if ($slashargument = min_get_slash_argument()) {
  35      $slashargument = ltrim($slashargument, '/');
  36      if (substr_count($slashargument, '/') < 3) {
  37          font_not_found();
  38      }
  39      list($themename, $component, $rev, $font) = explode('/', $slashargument, 4);
  40      $themename = min_clean_param($themename, 'SAFEDIR');
  41      $component = min_clean_param($component, 'SAFEDIR');
  42      $rev       = min_clean_param($rev, 'INT');
  43      $font      = min_clean_param($font, 'RAW');
  44  
  45  } else {
  46      $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
  47      $component = min_optional_param('component', 'core', 'SAFEDIR');
  48      $rev       = min_optional_param('rev', -1, 'INT');
  49      $font      = min_optional_param('font', '', 'RAW');
  50  }
  51  
  52  if (!min_is_revision_valid_and_current($rev)) {
  53      // If the rev is invalid, normalise it to -1 to disable all caching.
  54      $rev = -1;
  55  }
  56  
  57  if (!$font) {
  58      font_not_found();
  59  }
  60  
  61  if ($to = strpos($font, '?')) {
  62      $font = substr($font, 0, $to);
  63  }
  64  
  65  if (empty($component) or $component === 'moodle' or $component === 'core') {
  66      $component = 'core';
  67  }
  68  
  69  if (preg_match('/^[a-z0-9_-]+\.woff2$/i', $font, $matches)) {
  70      $font = $matches[0];
  71      $mimetype = 'font/woff2';
  72  
  73  } else if (preg_match('/^[a-z0-9_-]+\.woff$/i', $font, $matches)) {
  74      // This is the real standard!
  75      $font = $matches[0];
  76      $mimetype = 'font/woff';
  77  
  78  } else if (preg_match('/^[a-z0-9_-]+\.ttf$/i', $font, $matches)) {
  79      $font = $matches[0];
  80      $mimetype = 'font/ttf';
  81  
  82  } else if (preg_match('/^[a-z0-9_-]+\.otf$/i', $font, $matches)) {
  83      $font = $matches[0];
  84      $mimetype = 'font/otf';
  85  
  86  } else if (preg_match('/^[a-z0-9_-]+\.eot$/i', $font, $matches)) {
  87      // IE8 must die!!!
  88      $font = $matches[0];
  89      $mimetype = 'application/vnd.ms-fontobject';
  90  } else if (preg_match('/^[a-z0-9_-]+\.svg$/i', $font, $matches)) {
  91      $font = $matches[0];
  92      $mimetype = 'image/svg+xml';
  93  
  94  } else {
  95      font_not_found();
  96  }
  97  
  98  if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
  99      // Normal theme exists.
 100  } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
 101      // Theme exists in alternative location.
 102  } else {
 103      font_not_found();
 104  }
 105  
 106  $candidatelocation = "$CFG->localcachedir/theme/$rev/$themename/fonts/$component";
 107  $etag = sha1("$rev/$themename/$component/$font");
 108  
 109  if ($rev > 0) {
 110      if (file_exists("$candidatelocation/$font.error")) {
 111          font_not_found();
 112      }
 113  
 114      if (file_exists("$candidatelocation/$font")) {
 115          if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
 116              // We do not actually need to verify the etag value because our files
 117              // never change in cache because we increment the rev parameter.
 118              // 90 days only - based on Moodle point release cadence being every 3 months.
 119              $lifetime = 60 * 60 * 24 * 90;
 120              header('HTTP/1.1 304 Not Modified');
 121              header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
 122              header('Cache-Control: public, max-age='.$lifetime);
 123              header('Content-Type: '.$mimetype);
 124              header('Etag: "'.$etag.'"');
 125              die;
 126          }
 127          send_cached_font("$candidatelocation/$font", $etag, $font, $mimetype);
 128      }
 129  }
 130  
 131  // Ok, now we need to start normal moodle script, we need to load all libs and $DB.
 132  define('ABORT_AFTER_CONFIG_CANCEL', true);
 133  
 134  define('NO_MOODLE_COOKIES', true); // Session not used here.
 135  define('NO_UPGRADE_CHECK', true);  // Ignore upgrade check.
 136  
 137  require("$CFG->dirroot/lib/setup.php");
 138  
 139  $theme = theme_config::load($themename);
 140  $themerev = theme_get_revision();
 141  
 142  $fontfile = $theme->resolve_font_location($font, $component);
 143  
 144  if ($themerev <= 0 or $rev != $themerev) {
 145      // Do not send caching headers if they do not request current revision,
 146      // we do not want to pollute browser caches with outdated fonts.
 147      if (empty($fontfile) or !is_readable($fontfile)) {
 148          font_not_found();
 149      }
 150      send_uncached_font($fontfile, $font, $mimetype);
 151  }
 152  
 153  make_localcache_directory('theme', false);
 154  
 155  if (empty($fontfile) or !is_readable($fontfile)) {
 156      if (!file_exists($candidatelocation)) {
 157          @mkdir($candidatelocation, $CFG->directorypermissions, true);
 158      }
 159      // Make note we can not find this file.
 160      $cachefont = "$candidatelocation/$font.error";
 161      $fp = fopen($cachefont, 'w');
 162      fclose($fp);
 163      font_not_found();
 164  }
 165  
 166  $cachefont = cache_font($font, $fontfile, $candidatelocation);
 167  if (connection_aborted()) {
 168      die;
 169  }
 170  // Make sure nothing failed.
 171  clearstatcache();
 172  if (file_exists($cachefont)) {
 173      send_cached_font($cachefont, $etag, $font, $mimetype);
 174  }
 175  
 176  send_uncached_font($fontfile, $font, $mimetype);
 177  
 178  
 179  
 180  // Utility functions.
 181  
 182  function send_cached_font($fontpath, $etag, $font, $mimetype) {
 183      global $CFG;
 184      require("$CFG->dirroot/lib/xsendfilelib.php");
 185  
 186      // 90 days only - based on Moodle point release cadence being every 3 months.
 187      $lifetime = 60 * 60 * 24 * 90;
 188  
 189      header('Etag: "'.$etag.'"');
 190      header('Content-Disposition: inline; filename="'.$font.'"');
 191      header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($fontpath)) .' GMT');
 192      header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
 193      header('Pragma: ');
 194      header('Cache-Control: public, max-age='.$lifetime.', immutable');
 195      header('Accept-Ranges: none');
 196      header('Content-Type: '.$mimetype);
 197      header('Content-Length: '.filesize($fontpath));
 198  
 199      if (xsendfile($fontpath)) {
 200          die;
 201      }
 202  
 203      // No need to gzip already compressed fonts.
 204  
 205      readfile($fontpath);
 206      die;
 207  }
 208  
 209  function send_uncached_font($fontpath, $font, $mimetype) {
 210      header('Content-Disposition: inline; filename="'.$font.'"');
 211      header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
 212      header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
 213      header('Pragma: ');
 214      header('Accept-Ranges: none');
 215      header('Content-Type: '.$mimetype);
 216      header('Content-Length: '.filesize($fontpath));
 217  
 218      readfile($fontpath);
 219      die;
 220  }
 221  
 222  function font_not_found() {
 223      header('HTTP/1.0 404 not found');
 224      die('font was not found, sorry.');
 225  }
 226  
 227  /**
 228   * Caches a given font file.
 229   *
 230   * @param string $font The name of the font that was requested.
 231   * @param string $fontfile The location of the font file we want to cache.
 232   * @param string $candidatelocation The location to cache it in.
 233   * @return string The path to the cached font.
 234   */
 235  function cache_font($font, $fontfile, $candidatelocation) {
 236      global $CFG;
 237      $cachefont = "$candidatelocation/$font";
 238  
 239      clearstatcache();
 240      if (!file_exists($candidatelocation)) {
 241          @mkdir($candidatelocation, $CFG->directorypermissions, true);
 242      }
 243  
 244      // Prevent serving of incomplete file from concurrent request,
 245      // the rename() should be more atomic than copy().
 246      ignore_user_abort(true);
 247      if (@copy($fontfile, $cachefont.'.tmp')) {
 248          rename($cachefont.'.tmp', $cachefont);
 249          @chmod($cachefont, $CFG->filepermissions);
 250          @unlink($cachefont.'.tmp'); // Just in case anything fails.
 251      }
 252      return $cachefont;
 253  }