Merge branch 'MDL-25801' of git://github.com/timhunt/moodle
[moodle.git] / theme / image.php
blob63acfaf0053e1bb6e6c568bd6c96c85d69869fb9
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 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
36 $component = min_optional_param('component', 'moodle', 'SAFEDIR');
37 $image = min_optional_param('image', '', 'SAFEPATH');
38 $rev = min_optional_param('rev', -1, 'INT');
40 if (empty($component) or empty($image)) {
41 image_not_found();
44 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
45 // exists
46 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
47 // exists
48 } else {
49 image_not_found();
52 $candidatelocation = "$CFG->cachedir/theme/$themename/pix/$component";
54 if ($rev > -1) {
55 if (file_exists("$candidatelocation/$image.error")) {
56 // this is a major speedup if there are multiple missing images,
57 // the only problem is that random requests may pollute our cache.
58 image_not_found();
60 $cacheimage = false;
61 if (file_exists("$candidatelocation/$image.gif")) {
62 $cacheimage = "$candidatelocation/$image.gif";
63 $ext = 'gif';
64 } else if (file_exists("$candidatelocation/$image.png")) {
65 $cacheimage = "$candidatelocation/$image.png";
66 $ext = 'png';
67 } else if (file_exists("$candidatelocation/$image.jpg")) {
68 $cacheimage = "$candidatelocation/$image.jpg";
69 $ext = 'jpg';
70 } else if (file_exists("$candidatelocation/$image.jpeg")) {
71 $cacheimage = "$candidatelocation/$image.jpeg";
72 $ext = 'jpeg';
73 } else if (file_exists("$candidatelocation/$image.ico")) {
74 $cacheimage = "$candidatelocation/$image.ico";
75 $ext = 'ico';
77 if ($cacheimage) {
78 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
79 // we do not actually need to verify the etag value because our files
80 // never change in cache because we increment the rev parameter
81 $lifetime = 60*60*24*30; // 30 days
82 $mimetype = get_contenttype_from_ext($ext);
83 header('HTTP/1.1 304 Not Modified');
84 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
85 header('Cache-Control: max-age='.$lifetime);
86 header('Content-Type: '.$mimetype);
87 die;
89 send_cached_image($cacheimage, $rev);
93 //=================================================================================
94 // ok, now we need to start normal moodle script, we need to load all libs and $DB
95 define('ABORT_AFTER_CONFIG_CANCEL', true);
97 define('NO_MOODLE_COOKIES', true); // Session not used here
98 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
100 require("$CFG->dirroot/lib/setup.php");
102 $theme = theme_config::load($themename);
103 $imagefile = $theme->resolve_image_location($image, $component);
105 $rev = theme_get_revision();
107 if (empty($imagefile) or !is_readable($imagefile)) {
108 if ($rev > -1) {
109 // make note we can not find this file
110 $cacheimage = "$candidatelocation/$image.error";
111 $fp = fopen($cacheimage, 'w');
112 fclose($fp);
114 image_not_found();
118 if ($rev > -1) {
119 $pathinfo = pathinfo($imagefile);
120 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
121 if (!file_exists($cacheimage)) {
122 // note: cache reset might have purged our cache dir structure,
123 // make sure we do not use stale file stat cache in the next check_dir_exists()
124 clearstatcache();
125 check_dir_exists(dirname($cacheimage));
126 copy($imagefile, $cacheimage);
128 send_cached_image($cacheimage, $rev);
130 } else {
131 send_uncached_image($imagefile);
135 //=================================================================================
136 //=== utility functions ==
137 // we are not using filelib because we need to fine tune all header
138 // parameters to get the best performance.
140 function send_cached_image($imagepath, $rev) {
141 $lifetime = 60*60*24*30; // 30 days
142 $pathinfo = pathinfo($imagepath);
143 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
145 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
147 header('Etag: '.md5("$rev/$imagepath"));
148 header('Content-Disposition: inline; filename="'.$imagename.'"');
149 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
150 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
151 header('Pragma: ');
152 header('Cache-Control: max-age='.$lifetime);
153 header('Accept-Ranges: none');
154 header('Content-Type: '.$mimetype);
155 header('Content-Length: '.filesize($imagepath));
157 // no need to gzip already compressed images ;-)
159 readfile($imagepath);
160 die;
163 function send_uncached_image($imagepath) {
164 $pathinfo = pathinfo($imagepath);
165 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
167 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
169 header('Content-Disposition: inline; filename="'.$imagename.'"');
170 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
171 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
172 header('Pragma: ');
173 header('Accept-Ranges: none');
174 header('Content-Type: '.$mimetype);
175 header('Content-Length: '.filesize($imagepath));
177 readfile($imagepath);
178 die;
181 function image_not_found() {
182 header('HTTP/1.0 404 not found');
183 die('Image was not found, sorry.');
186 function get_contenttype_from_ext($ext) {
187 switch ($ext) {
188 case 'gif':
189 return 'image/gif';
190 case 'png':
191 return 'image/png';
192 case 'jpg':
193 case 'jpeg':
194 return 'image/jpeg';
195 case 'ico':
196 return 'image/vnd.microsoft.icon';
198 return 'document/unknown';