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