Merge branch 'MDL-38885-master-int' of git://github.com/FMCorz/moodle
[moodle.git] / theme / image.php
blobf3f1c7ab0869783c7872d3b2949bcd3ccdcdb88c
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.
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/>.
17 /**
18 * This file is responsible for serving the one theme and plugin images.
20 * @package core
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // disable moodle specific debug messages and any errors in output,
27 // comment out when debugging or better look into error log!
28 define('NO_DEBUG_DISPLAY', true);
30 // we need just the values from config.php and minlib.php
31 define('ABORT_AFTER_CONFIG', true);
32 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34 if ($slashargument = min_get_slash_argument()) {
35 $slashargument = ltrim($slashargument, '/');
36 if (substr_count($slashargument, '/') < 3) {
37 image_not_found();
39 if (strpos($slashargument, '_s/') === 0) {
40 // Can't use SVG
41 $slashargument = substr($slashargument, 3);
42 $usesvg = false;
43 } else {
44 $usesvg = true;
46 // image must be last because it may contain "/"
47 list($themename, $component, $rev, $image) = explode('/', $slashargument, 4);
48 $themename = min_clean_param($themename, 'SAFEDIR');
49 $component = min_clean_param($component, 'SAFEDIR');
50 $rev = min_clean_param($rev, 'INT');
51 $image = min_clean_param($image, 'SAFEPATH');
53 } else {
54 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
55 $component = min_optional_param('component', 'core', 'SAFEDIR');
56 $rev = min_optional_param('rev', -1, 'INT');
57 $image = min_optional_param('image', '', 'SAFEPATH');
58 $usesvg = (bool)min_optional_param('svg', '1', 'INT');
61 if (empty($component) or $component === 'moodle' or $component === 'core') {
62 $component = 'moodle';
65 if (empty($image)) {
66 image_not_found();
69 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
70 // exists
71 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
72 // exists
73 } else {
74 image_not_found();
77 $candidatelocation = "$CFG->cachedir/theme/$themename/pix/$component";
78 $etag = sha1("$themename/$component/$rev/$image");
80 if ($rev > -1) {
81 if (file_exists("$candidatelocation/$image.error")) {
82 // this is a major speedup if there are multiple missing images,
83 // the only problem is that random requests may pollute our cache.
84 image_not_found();
86 $cacheimage = false;
87 if ($usesvg && file_exists("$candidatelocation/$image.svg")) {
88 $cacheimage = "$candidatelocation/$image.svg";
89 $ext = 'svg';
90 } else if (file_exists("$candidatelocation/$image.png")) {
91 $cacheimage = "$candidatelocation/$image.png";
92 $ext = 'png';
93 } else if (file_exists("$candidatelocation/$image.gif")) {
94 $cacheimage = "$candidatelocation/$image.gif";
95 $ext = 'gif';
96 } else if (file_exists("$candidatelocation/$image.jpg")) {
97 $cacheimage = "$candidatelocation/$image.jpg";
98 $ext = 'jpg';
99 } else if (file_exists("$candidatelocation/$image.jpeg")) {
100 $cacheimage = "$candidatelocation/$image.jpeg";
101 $ext = 'jpeg';
102 } else if (file_exists("$candidatelocation/$image.ico")) {
103 $cacheimage = "$candidatelocation/$image.ico";
104 $ext = 'ico';
106 if ($cacheimage) {
107 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
108 // we do not actually need to verify the etag value because our files
109 // never change in cache because we increment the rev parameter
110 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
111 $mimetype = get_contenttype_from_ext($ext);
112 header('HTTP/1.1 304 Not Modified');
113 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
114 header('Cache-Control: public, max-age='.$lifetime);
115 header('Content-Type: '.$mimetype);
116 header('Etag: '.$etag);
117 die;
119 send_cached_image($cacheimage, $etag);
123 //=================================================================================
124 // ok, now we need to start normal moodle script, we need to load all libs and $DB
125 define('ABORT_AFTER_CONFIG_CANCEL', true);
127 define('NO_MOODLE_COOKIES', true); // Session not used here
128 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
130 require("$CFG->dirroot/lib/setup.php");
132 $theme = theme_config::load($themename);
133 $rev = theme_get_revision();
134 $etag = sha1("$themename/$component/$rev/$image");
136 // We're not using SVG and there is no cached version of this file (in any format).
137 // 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
138 // 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
139 // there is a cached image already of another format.
140 // Remember this only gets run once before any candidate exists, and only if we want a cached revision.
141 if (!$usesvg && $rev > -1) {
142 $imagefile = $theme->resolve_image_location($image, $component, true);
143 if (!empty($imagefile) && is_readable($imagefile)) {
144 $cacheimage = cache_image($image, $imagefile, $candidatelocation);
145 $pathinfo = pathinfo($imagefile);
146 // There is no SVG equivilant, we've just successfully cached an image of another format.
147 if ($pathinfo['extension'] !== 'svg') {
148 // Serve the file as we would in a normal request.
149 if (connection_aborted()) {
150 die;
152 // make sure nothing failed
153 clearstatcache();
154 if (file_exists($cacheimage)) {
155 send_cached_image($cacheimage, $etag);
157 send_uncached_image($imagefile);
158 exit;
163 // Either SVG was requested or we've cached a SVG version and are ready to serve a regular format.
164 $imagefile = $theme->resolve_image_location($image, $component, $usesvg);
165 if (empty($imagefile) or !is_readable($imagefile)) {
166 if ($rev > -1) {
167 if (!file_exists($candidatelocation)) {
168 @mkdir($candidatelocation, $CFG->directorypermissions, true);
170 // make note we can not find this file
171 $cacheimage = "$candidatelocation/$image.error";
172 $fp = fopen($cacheimage, 'w');
173 fclose($fp);
175 image_not_found();
178 if ($rev > -1) {
179 $cacheimage = cache_image($image, $imagefile, $candidatelocation);
180 if (connection_aborted()) {
181 die;
183 // make sure nothing failed
184 clearstatcache();
185 if (file_exists($cacheimage)) {
186 send_cached_image($cacheimage, $etag);
190 send_uncached_image($imagefile);
193 //=================================================================================
194 //=== utility functions ==
195 // we are not using filelib because we need to fine tune all header
196 // parameters to get the best performance.
198 function send_cached_image($imagepath, $etag) {
199 global $CFG;
200 require("$CFG->dirroot/lib/xsendfilelib.php");
202 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
203 $pathinfo = pathinfo($imagepath);
204 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
206 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
208 header('Etag: '.$etag);
209 header('Content-Disposition: inline; filename="'.$imagename.'"');
210 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
211 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
212 header('Pragma: ');
213 header('Cache-Control: public, max-age='.$lifetime);
214 header('Accept-Ranges: none');
215 header('Content-Type: '.$mimetype);
216 header('Content-Length: '.filesize($imagepath));
218 if (xsendfile($imagepath)) {
219 die;
222 // no need to gzip already compressed images ;-)
224 readfile($imagepath);
225 die;
228 function send_uncached_image($imagepath) {
229 $pathinfo = pathinfo($imagepath);
230 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
232 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
234 header('Content-Disposition: inline; filename="'.$imagename.'"');
235 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
236 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
237 header('Pragma: ');
238 header('Accept-Ranges: none');
239 header('Content-Type: '.$mimetype);
240 header('Content-Length: '.filesize($imagepath));
242 readfile($imagepath);
243 die;
246 function image_not_found() {
247 header('HTTP/1.0 404 not found');
248 die('Image was not found, sorry.');
251 function get_contenttype_from_ext($ext) {
252 switch ($ext) {
253 case 'svg':
254 return 'image/svg+xml';
255 case 'png':
256 return 'image/png';
257 case 'gif':
258 return 'image/gif';
259 case 'jpg':
260 case 'jpeg':
261 return 'image/jpeg';
262 case 'ico':
263 return 'image/vnd.microsoft.icon';
265 return 'document/unknown';
269 * Caches a given image file.
271 * @param string $image The name of the image that was requested.
272 * @param string $imagefile The location of the image file we want to cache.
273 * @param string $candidatelocation The location to cache it in.
274 * @return string The path to the cached image.
276 function cache_image($image, $imagefile, $candidatelocation) {
277 global $CFG;
278 $pathinfo = pathinfo($imagefile);
279 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
281 clearstatcache();
282 if (!file_exists(dirname($cacheimage))) {
283 @mkdir(dirname($cacheimage), $CFG->directorypermissions, true);
286 // Prevent serving of incomplete file from concurrent request,
287 // the rename() should be more atomic than copy().
288 ignore_user_abort(true);
289 if (@copy($imagefile, $cacheimage.'.tmp')) {
290 rename($cacheimage.'.tmp', $cacheimage);
291 @chmod($cacheimage, $CFG->filepermissions);
292 @unlink($cacheimage.'.tmp'); // just in case anything fails
294 return $cacheimage;