Merge branch 'MDL-38957_master' of git://github.com/dmonllao/moodle
[moodle.git] / theme / image.php
blob75b7c599617bc1edf3741cc72a1eef85378b0e46
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file is responsible for serving the one theme and plugin images.
21 * @package moodlecore
22 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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);
31 // we need just the values from config.php and minlib.php
32 define('ABORT_AFTER_CONFIG', true);
33 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
35 if ($slashargument = min_get_slash_argument()) {
36 $slashargument = ltrim($slashargument, '/');
37 if (substr_count($slashargument, '/') < 3) {
38 image_not_found();
40 if (strpos($slashargument, '_s/') === 0) {
41 // Can't use SVG
42 $slashargument = substr($slashargument, 3);
43 $usesvg = false;
44 } else {
45 $usesvg = true;
47 // image must be last because it may contain "/"
48 list($themename, $component, $rev, $image) = explode('/', $slashargument, 4);
49 $themename = min_clean_param($themename, 'SAFEDIR');
50 $component = min_clean_param($component, 'SAFEDIR');
51 $rev = min_clean_param($rev, 'INT');
52 $image = min_clean_param($image, 'SAFEPATH');
54 } else {
55 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
56 $component = min_optional_param('component', 'core', 'SAFEDIR');
57 $rev = min_optional_param('rev', -1, 'INT');
58 $image = min_optional_param('image', '', 'SAFEPATH');
59 $usesvg = (bool)min_optional_param('svg', '1', 'INT');
62 if (empty($component) or $component === 'moodle' or $component === 'core') {
63 $component = 'moodle';
66 if (empty($image)) {
67 image_not_found();
70 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
71 // exists
72 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
73 // exists
74 } else {
75 image_not_found();
78 $candidatelocation = "$CFG->cachedir/theme/$themename/pix/$component";
79 $etag = sha1("$themename/$component/$rev/$image");
81 if ($rev > -1) {
82 if (file_exists("$candidatelocation/$image.error")) {
83 // this is a major speedup if there are multiple missing images,
84 // the only problem is that random requests may pollute our cache.
85 image_not_found();
87 $cacheimage = false;
88 if ($usesvg && file_exists("$candidatelocation/$image.svg")) {
89 $cacheimage = "$candidatelocation/$image.svg";
90 $ext = 'svg';
91 } else if (file_exists("$candidatelocation/$image.png")) {
92 $cacheimage = "$candidatelocation/$image.png";
93 $ext = 'png';
94 } else if (file_exists("$candidatelocation/$image.gif")) {
95 $cacheimage = "$candidatelocation/$image.gif";
96 $ext = 'gif';
97 } else if (file_exists("$candidatelocation/$image.jpg")) {
98 $cacheimage = "$candidatelocation/$image.jpg";
99 $ext = 'jpg';
100 } else if (file_exists("$candidatelocation/$image.jpeg")) {
101 $cacheimage = "$candidatelocation/$image.jpeg";
102 $ext = 'jpeg';
103 } else if (file_exists("$candidatelocation/$image.ico")) {
104 $cacheimage = "$candidatelocation/$image.ico";
105 $ext = 'ico';
107 if ($cacheimage) {
108 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
109 // we do not actually need to verify the etag value because our files
110 // never change in cache because we increment the rev parameter
111 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
112 $mimetype = get_contenttype_from_ext($ext);
113 header('HTTP/1.1 304 Not Modified');
114 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
115 header('Cache-Control: public, max-age='.$lifetime);
116 header('Content-Type: '.$mimetype);
117 header('Etag: '.$etag);
118 die;
120 send_cached_image($cacheimage, $etag);
124 //=================================================================================
125 // ok, now we need to start normal moodle script, we need to load all libs and $DB
126 define('ABORT_AFTER_CONFIG_CANCEL', true);
128 define('NO_MOODLE_COOKIES', true); // Session not used here
129 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
131 require("$CFG->dirroot/lib/setup.php");
133 $theme = theme_config::load($themename);
134 $rev = theme_get_revision();
135 $etag = sha1("$themename/$component/$rev/$image");
137 // We're not using SVG and there is no cached version of this file (in any format).
138 // As we're going to be caching a format other than svg, and because svg use is conditional we need to ensure that at the same
139 // time we cache a version of the SVG if it exists. If we don't do this other users who ask for SVG would not ever get it as
140 // there is a cached image already of another format.
141 // Remember this only gets run once before any candidate exists, and only if we want a cached revision.
142 if (!$usesvg && $rev > -1) {
143 $imagefile = $theme->resolve_image_location($image, $component, true);
144 if (!empty($imagefile) && is_readable($imagefile)) {
145 $cacheimage = cache_image($image, $imagefile, $candidatelocation);
146 $pathinfo = pathinfo($imagefile);
147 // There is no SVG equivilant, we've just successfully cached an image of another format.
148 if ($pathinfo['extension'] !== 'svg') {
149 // Serve the file as we would in a normal request.
150 if (connection_aborted()) {
151 die;
153 // make sure nothing failed
154 clearstatcache();
155 if (file_exists($cacheimage)) {
156 send_cached_image($cacheimage, $etag);
158 send_uncached_image($imagefile);
159 exit;
164 // Either SVG was requested or we've cached a SVG version and are ready to serve a regular format.
165 $imagefile = $theme->resolve_image_location($image, $component, $usesvg);
166 if (empty($imagefile) or !is_readable($imagefile)) {
167 if ($rev > -1) {
168 if (!file_exists($candidatelocation)) {
169 @mkdir($candidatelocation, $CFG->directorypermissions, true);
171 // make note we can not find this file
172 $cacheimage = "$candidatelocation/$image.error";
173 $fp = fopen($cacheimage, 'w');
174 fclose($fp);
176 image_not_found();
179 if ($rev > -1) {
180 $cacheimage = cache_image($image, $imagefile, $candidatelocation);
181 if (connection_aborted()) {
182 die;
184 // make sure nothing failed
185 clearstatcache();
186 if (file_exists($cacheimage)) {
187 send_cached_image($cacheimage, $etag);
191 send_uncached_image($imagefile);
194 //=================================================================================
195 //=== utility functions ==
196 // we are not using filelib because we need to fine tune all header
197 // parameters to get the best performance.
199 function send_cached_image($imagepath, $etag) {
200 global $CFG;
201 require("$CFG->dirroot/lib/xsendfilelib.php");
203 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
204 $pathinfo = pathinfo($imagepath);
205 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
207 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
209 header('Etag: '.$etag);
210 header('Content-Disposition: inline; filename="'.$imagename.'"');
211 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
212 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
213 header('Pragma: ');
214 header('Cache-Control: public, max-age='.$lifetime);
215 header('Accept-Ranges: none');
216 header('Content-Type: '.$mimetype);
217 header('Content-Length: '.filesize($imagepath));
219 if (xsendfile($imagepath)) {
220 die;
223 // no need to gzip already compressed images ;-)
225 readfile($imagepath);
226 die;
229 function send_uncached_image($imagepath) {
230 $pathinfo = pathinfo($imagepath);
231 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
233 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
235 header('Content-Disposition: inline; filename="'.$imagename.'"');
236 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
237 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
238 header('Pragma: ');
239 header('Accept-Ranges: none');
240 header('Content-Type: '.$mimetype);
241 header('Content-Length: '.filesize($imagepath));
243 readfile($imagepath);
244 die;
247 function image_not_found() {
248 header('HTTP/1.0 404 not found');
249 die('Image was not found, sorry.');
252 function get_contenttype_from_ext($ext) {
253 switch ($ext) {
254 case 'svg':
255 return 'image/svg+xml';
256 case 'png':
257 return 'image/png';
258 case 'gif':
259 return 'image/gif';
260 case 'jpg':
261 case 'jpeg':
262 return 'image/jpeg';
263 case 'ico':
264 return 'image/vnd.microsoft.icon';
266 return 'document/unknown';
270 * Caches a given image file.
272 * @param string $image The name of the image that was requested.
273 * @param string $imagefile The location of the image file we want to cache.
274 * @param string $candidatelocation The location to cache it in.
275 * @return string The path to the cached image.
277 function cache_image($image, $imagefile, $candidatelocation) {
278 global $CFG;
279 $pathinfo = pathinfo($imagefile);
280 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
282 clearstatcache();
283 if (!file_exists(dirname($cacheimage))) {
284 @mkdir(dirname($cacheimage), $CFG->directorypermissions, true);
287 // Prevent serving of incomplete file from concurrent request,
288 // the rename() should be more atomic than copy().
289 ignore_user_abort(true);
290 if (@copy($imagefile, $cacheimage.'.tmp')) {
291 rename($cacheimage.'.tmp', $cacheimage);
292 @chmod($cacheimage, $CFG->filepermissions);
293 @unlink($cacheimage.'.tmp'); // just in case anything fails
295 return $cacheimage;