[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 WOFF (Web Open Font Format) 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 (!$font) { 53 font_not_found(); 54 } 55 56 if (empty($component) or $component === 'moodle' or $component === 'core') { 57 $component = 'core'; 58 } 59 60 if (preg_match('/^[a-z0-9_-]+\.woff2$/i', $font, $matches)) { 61 $font = $matches[0]; 62 $mimetype = 'application/font-woff2'; 63 64 } else if (preg_match('/^[a-z0-9_-]+\.woff$/i', $font, $matches)) { 65 // This is the real standard! 66 $font = $matches[0]; 67 $mimetype = 'application/font-woff'; 68 69 } else if (preg_match('/^[a-z0-9_-]+\.ttf$/i', $font, $matches)) { 70 $font = $matches[0]; 71 $mimetype = 'application/x-font-ttf'; 72 73 } else if (preg_match('/^[a-z0-9_-]+\.otf$/i', $font, $matches)) { 74 $font = $matches[0]; 75 $mimetype = 'application/x-font-opentype'; 76 77 } else if (preg_match('/^[a-z0-9_-]+\.eot$/i', $font, $matches)) { 78 // IE8 must die!!! 79 $font = $matches[0]; 80 $mimetype = 'application/vnd.ms-fontobject'; 81 } else if (preg_match('/^[a-z0-9_-]+\.svg$/i', $font, $matches)) { 82 $font = $matches[0]; 83 $mimetype = 'image/svg+xml'; 84 85 } else { 86 font_not_found(); 87 } 88 89 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) { 90 // Normal theme exists. 91 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) { 92 // Theme exists in alternative location. 93 } else { 94 font_not_found(); 95 } 96 97 $candidatelocation = "$CFG->localcachedir/theme/$rev/$themename/fonts/$component"; 98 $etag = sha1("$rev/$themename/$component/$font"); 99 100 if ($rev > 0) { 101 if (file_exists("$candidatelocation/$font.error")) { 102 font_not_found(); 103 } 104 105 if (file_exists("$candidatelocation/$font")) { 106 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { 107 // We do not actually need to verify the etag value because our files 108 // never change in cache because we increment the rev parameter. 109 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often. 110 header('HTTP/1.1 304 Not Modified'); 111 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); 112 header('Cache-Control: public, max-age='.$lifetime); 113 header('Content-Type: '.$mimetype); 114 header('Etag: "'.$etag.'"'); 115 die; 116 } 117 send_cached_font("$candidatelocation/$font", $etag, $font, $mimetype); 118 } 119 } 120 121 // Ok, now we need to start normal moodle script, we need to load all libs and $DB. 122 define('ABORT_AFTER_CONFIG_CANCEL', true); 123 124 define('NO_MOODLE_COOKIES', true); // Session not used here. 125 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check. 126 127 require("$CFG->dirroot/lib/setup.php"); 128 129 $theme = theme_config::load($themename); 130 $themerev = theme_get_revision(); 131 132 $fontfile = $theme->resolve_font_location($font, $component); 133 134 if ($themerev <= 0 or $rev != $themerev) { 135 // Do not send caching headers if they do not request current revision, 136 // we do not want to pollute browser caches with outdated fonts. 137 if (empty($fontfile) or !is_readable($fontfile)) { 138 font_not_found(); 139 } 140 send_uncached_font($fontfile, $font, $mimetype); 141 } 142 143 make_localcache_directory('theme', false); 144 145 if (empty($fontfile) or !is_readable($fontfile)) { 146 if (!file_exists($candidatelocation)) { 147 @mkdir($candidatelocation, $CFG->directorypermissions, true); 148 } 149 // Make note we can not find this file. 150 $cachefont = "$candidatelocation/$font.error"; 151 $fp = fopen($cachefont, 'w'); 152 fclose($fp); 153 font_not_found(); 154 } 155 156 $cachefont = cache_font($font, $fontfile, $candidatelocation); 157 if (connection_aborted()) { 158 die; 159 } 160 // Make sure nothing failed. 161 clearstatcache(); 162 if (file_exists($cachefont)) { 163 send_cached_font($cachefont, $etag, $font, $mimetype); 164 } 165 166 send_uncached_font($fontfile, $font, $mimetype); 167 168 169 170 // Utility functions. 171 172 function send_cached_font($fontpath, $etag, $font, $mimetype) { 173 global $CFG; 174 require("$CFG->dirroot/lib/xsendfilelib.php"); 175 176 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often. 177 178 header('Etag: "'.$etag.'"'); 179 header('Content-Disposition: inline; filename="'.$font.'"'); 180 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($fontpath)) .' GMT'); 181 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); 182 header('Pragma: '); 183 header('Cache-Control: public, max-age='.$lifetime); 184 header('Accept-Ranges: none'); 185 header('Content-Type: '.$mimetype); 186 header('Content-Length: '.filesize($fontpath)); 187 188 if (xsendfile($fontpath)) { 189 die; 190 } 191 192 // No need to gzip already compressed fonts. 193 194 readfile($fontpath); 195 die; 196 } 197 198 function send_uncached_font($fontpath, $font, $mimetype) { 199 header('Content-Disposition: inline; filename="'.$font.'"'); 200 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT'); 201 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT'); 202 header('Pragma: '); 203 header('Accept-Ranges: none'); 204 header('Content-Type: '.$mimetype); 205 header('Content-Length: '.filesize($fontpath)); 206 207 readfile($fontpath); 208 die; 209 } 210 211 function font_not_found() { 212 header('HTTP/1.0 404 not found'); 213 die('font was not found, sorry.'); 214 } 215 216 /** 217 * Caches a given font file. 218 * 219 * @param string $font The name of the font that was requested. 220 * @param string $fontfile The location of the font file we want to cache. 221 * @param string $candidatelocation The location to cache it in. 222 * @return string The path to the cached font. 223 */ 224 function cache_font($font, $fontfile, $candidatelocation) { 225 global $CFG; 226 $cachefont = "$candidatelocation/$font"; 227 228 clearstatcache(); 229 if (!file_exists($candidatelocation)) { 230 @mkdir($candidatelocation, $CFG->directorypermissions, true); 231 } 232 233 // Prevent serving of incomplete file from concurrent request, 234 // the rename() should be more atomic than copy(). 235 ignore_user_abort(true); 236 if (@copy($fontfile, $cachefont.'.tmp')) { 237 rename($cachefont.'.tmp', $cachefont); 238 @chmod($cachefont, $CFG->filepermissions); 239 @unlink($cachefont.'.tmp'); // Just in case anything fails. 240 } 241 return $cachefont; 242 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |