Merge branch 'wip-mdl-27562' of git://github.com/rajeshtaneja/moodle
[moodle.git] / theme / image.php
blob1ddeb266382c7f31d533f9d020d08eda8238a9dc
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 // image must be last because it may contain "/"
41 list($themename, $component, $rev, $image) = explode('/', $slashargument, 4);
42 $themename = min_clean_param($themename, 'SAFEDIR');
43 $component = min_clean_param($component, 'SAFEDIR');
44 $rev = min_clean_param($rev, 'INT');
45 $image = min_clean_param($image, 'SAFEPATH');
47 } else {
48 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
49 $component = min_optional_param('component', 'core', 'SAFEDIR');
50 $rev = min_optional_param('rev', -1, 'INT');
51 $image = min_optional_param('image', '', 'SAFEPATH');
54 if (empty($component) or $component === 'moodle' or $component === 'core') {
55 $component = 'moodle';
58 if (empty($image)) {
59 image_not_found();
62 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
63 // exists
64 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
65 // exists
66 } else {
67 image_not_found();
70 $candidatelocation = "$CFG->cachedir/theme/$themename/pix/$component";
71 $etag = sha1("$themename/$component/$rev/$image");
73 if ($rev > -1) {
74 if (file_exists("$candidatelocation/$image.error")) {
75 // this is a major speedup if there are multiple missing images,
76 // the only problem is that random requests may pollute our cache.
77 image_not_found();
79 $cacheimage = false;
80 if (file_exists("$candidatelocation/$image.gif")) {
81 $cacheimage = "$candidatelocation/$image.gif";
82 $ext = 'gif';
83 } else if (file_exists("$candidatelocation/$image.png")) {
84 $cacheimage = "$candidatelocation/$image.png";
85 $ext = 'png';
86 } else if (file_exists("$candidatelocation/$image.jpg")) {
87 $cacheimage = "$candidatelocation/$image.jpg";
88 $ext = 'jpg';
89 } else if (file_exists("$candidatelocation/$image.jpeg")) {
90 $cacheimage = "$candidatelocation/$image.jpeg";
91 $ext = 'jpeg';
92 } else if (file_exists("$candidatelocation/$image.ico")) {
93 $cacheimage = "$candidatelocation/$image.ico";
94 $ext = 'ico';
96 if ($cacheimage) {
97 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
98 // we do not actually need to verify the etag value because our files
99 // never change in cache because we increment the rev parameter
100 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
101 $mimetype = get_contenttype_from_ext($ext);
102 header('HTTP/1.1 304 Not Modified');
103 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
104 header('Cache-Control: public, max-age='.$lifetime);
105 header('Content-Type: '.$mimetype);
106 header('Etag: '.$etag);
107 die;
109 send_cached_image($cacheimage, $etag);
113 //=================================================================================
114 // ok, now we need to start normal moodle script, we need to load all libs and $DB
115 define('ABORT_AFTER_CONFIG_CANCEL', true);
117 define('NO_MOODLE_COOKIES', true); // Session not used here
118 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
120 require("$CFG->dirroot/lib/setup.php");
122 $theme = theme_config::load($themename);
123 $imagefile = $theme->resolve_image_location($image, $component);
125 $rev = theme_get_revision();
126 $etag = sha1("$themename/$component/$rev/$image");
128 if (empty($imagefile) or !is_readable($imagefile)) {
129 if ($rev > -1) {
130 if (!file_exists($candidatelocation)) {
131 @mkdir($candidatelocation, $CFG->directorypermissions, true);
133 // make note we can not find this file
134 $cacheimage = "$candidatelocation/$image.error";
135 $fp = fopen($cacheimage, 'w');
136 fclose($fp);
138 image_not_found();
141 if ($rev > -1) {
142 $pathinfo = pathinfo($imagefile);
143 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
145 clearstatcache();
146 if (!file_exists(dirname($cacheimage))) {
147 @mkdir(dirname($cacheimage), $CFG->directorypermissions, true);
150 // Prevent serving of incomplete file from concurrent request,
151 // the rename() should be more atomic than copy().
152 ignore_user_abort(true);
153 if (@copy($imagefile, $cacheimage.'.tmp')) {
154 rename($cacheimage.'.tmp', $cacheimage);
155 @chmod($cacheimage, $CFG->filepermissions);
156 @unlink($cacheimage.'.tmp'); // just in case anything fails
158 ignore_user_abort(false);
159 if (connection_aborted()) {
160 die;
162 // make sure nothing failed
163 clearstatcache();
164 if (file_exists($cacheimage)) {
165 send_cached_image($cacheimage, $etag);
169 send_uncached_image($imagefile);
172 //=================================================================================
173 //=== utility functions ==
174 // we are not using filelib because we need to fine tune all header
175 // parameters to get the best performance.
177 function send_cached_image($imagepath, $etag) {
178 global $CFG;
179 require("$CFG->dirroot/lib/xsendfilelib.php");
181 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
182 $pathinfo = pathinfo($imagepath);
183 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
185 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
187 header('Etag: '.$etag);
188 header('Content-Disposition: inline; filename="'.$imagename.'"');
189 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
190 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
191 header('Pragma: ');
192 header('Cache-Control: public, max-age='.$lifetime);
193 header('Accept-Ranges: none');
194 header('Content-Type: '.$mimetype);
195 header('Content-Length: '.filesize($imagepath));
197 if (xsendfile($imagepath)) {
198 die;
201 // no need to gzip already compressed images ;-)
203 readfile($imagepath);
204 die;
207 function send_uncached_image($imagepath) {
208 $pathinfo = pathinfo($imagepath);
209 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
211 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
213 header('Content-Disposition: inline; filename="'.$imagename.'"');
214 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
215 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
216 header('Pragma: ');
217 header('Accept-Ranges: none');
218 header('Content-Type: '.$mimetype);
219 header('Content-Length: '.filesize($imagepath));
221 readfile($imagepath);
222 die;
225 function image_not_found() {
226 header('HTTP/1.0 404 not found');
227 die('Image was not found, sorry.');
230 function get_contenttype_from_ext($ext) {
231 switch ($ext) {
232 case 'gif':
233 return 'image/gif';
234 case 'png':
235 return 'image/png';
236 case 'jpg':
237 case 'jpeg':
238 return 'image/jpeg';
239 case 'ico':
240 return 'image/vnd.microsoft.icon';
242 return 'document/unknown';