font.php 7.9 KB

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