MDL-32683 explicitly allow more caching of static content
[moodle.git] / theme / image.php
CommitLineData
78946b9b
PS
1<?php
2
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.
14//
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/>.
17
18/**
19 * This file is responsible for serving the one theme and plugin images.
20 *
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
24 */
25
73e504bc 26
c198390a
PS
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
78946b9b
PS
31// we need just the values from config.php and minlib.php
32define('ABORT_AFTER_CONFIG', true);
33require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34
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');
39
40if (empty($component) or empty($image)) {
41 image_not_found();
42}
43
73e504bc
PS
44if (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 {
78946b9b
PS
49 image_not_found();
50}
51
365bec4c 52$candidatelocation = "$CFG->cachedir/theme/$themename/pix/$component";
78946b9b
PS
53
54if ($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();
59 }
60 $cacheimage = false;
61 if (file_exists("$candidatelocation/$image.gif")) {
62 $cacheimage = "$candidatelocation/$image.gif";
ccc0fff9 63 $ext = 'gif';
78946b9b
PS
64 } else if (file_exists("$candidatelocation/$image.png")) {
65 $cacheimage = "$candidatelocation/$image.png";
ccc0fff9 66 $ext = 'png';
78946b9b
PS
67 } else if (file_exists("$candidatelocation/$image.jpg")) {
68 $cacheimage = "$candidatelocation/$image.jpg";
ccc0fff9 69 $ext = 'jpg';
78946b9b
PS
70 } else if (file_exists("$candidatelocation/$image.jpeg")) {
71 $cacheimage = "$candidatelocation/$image.jpeg";
ccc0fff9 72 $ext = 'jpeg';
78946b9b
PS
73 } else if (file_exists("$candidatelocation/$image.ico")) {
74 $cacheimage = "$candidatelocation/$image.ico";
ccc0fff9 75 $ext = 'ico';
78946b9b
PS
76 }
77 if ($cacheimage) {
aa603b14 78 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
78946b9b
PS
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
ccc0fff9
TL
81 $lifetime = 60*60*24*30; // 30 days
82 $mimetype = get_contenttype_from_ext($ext);
aa603b14 83 header('HTTP/1.1 304 Not Modified');
ccc0fff9 84 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
8c672cf9 85 header('Cache-Control: public, max-age='.$lifetime);
ccc0fff9 86 header('Content-Type: '.$mimetype);
78946b9b
PS
87 die;
88 }
89 send_cached_image($cacheimage, $rev);
90 }
91}
92
93//=================================================================================
94// ok, now we need to start normal moodle script, we need to load all libs and $DB
95define('ABORT_AFTER_CONFIG_CANCEL', true);
96
97define('NO_MOODLE_COOKIES', true); // Session not used here
98define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
99
100require("$CFG->dirroot/lib/setup.php");
101
102$theme = theme_config::load($themename);
103$imagefile = $theme->resolve_image_location($image, $component);
104
105$rev = theme_get_revision();
106
107if (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);
113 }
114 image_not_found();
115}
116
117
118if ($rev > -1) {
119 $pathinfo = pathinfo($imagefile);
120 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
121 if (!file_exists($cacheimage)) {
5d234a34
PS
122 if (!file_exists(dirname($cacheimage))) {
123 // Sometimes there was a race condition in check_dir_exists(),
124 // so let the next copy() log errors instead.
125 @mkdir(dirname($cacheimage), $CFG->directorypermissions, true);
126 }
78946b9b
PS
127 copy($imagefile, $cacheimage);
128 }
129 send_cached_image($cacheimage, $rev);
130
131} else {
132 send_uncached_image($imagefile);
133}
134
135
136//=================================================================================
137//=== utility functions ==
138// we are not using filelib because we need to fine tune all header
139// parameters to get the best performance.
140
141function send_cached_image($imagepath, $rev) {
7e9f1b63
PS
142 global $CFG;
143 require("$CFG->dirroot/lib/xsendfilelib.php");
144
13ea159e 145 $lifetime = 60*60*24*30; // 30 days
78946b9b
PS
146 $pathinfo = pathinfo($imagepath);
147 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
148
ccc0fff9 149 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
78946b9b
PS
150
151 header('Etag: '.md5("$rev/$imagepath"));
152 header('Content-Disposition: inline; filename="'.$imagename.'"');
13ea159e 153 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
78946b9b
PS
154 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
155 header('Pragma: ');
8c672cf9 156 header('Cache-Control: public, max-age='.$lifetime);
78946b9b
PS
157 header('Accept-Ranges: none');
158 header('Content-Type: '.$mimetype);
159 header('Content-Length: '.filesize($imagepath));
160
7e9f1b63
PS
161 if (xsendfile($imagepath)) {
162 die;
163 }
164
7c986f04
PS
165 // no need to gzip already compressed images ;-)
166
78946b9b
PS
167 readfile($imagepath);
168 die;
169}
170
171function send_uncached_image($imagepath) {
172 $pathinfo = pathinfo($imagepath);
173 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
174
ccc0fff9 175 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
78946b9b
PS
176
177 header('Content-Disposition: inline; filename="'.$imagename.'"');
178 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
13ea159e 179 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
78946b9b
PS
180 header('Pragma: ');
181 header('Accept-Ranges: none');
182 header('Content-Type: '.$mimetype);
183 header('Content-Length: '.filesize($imagepath));
184
78946b9b
PS
185 readfile($imagepath);
186 die;
187}
188
189function image_not_found() {
190 header('HTTP/1.0 404 not found');
191 die('Image was not found, sorry.');
ccc0fff9
TL
192}
193
194function get_contenttype_from_ext($ext) {
195 switch ($ext) {
196 case 'gif':
197 return 'image/gif';
198 case 'png':
199 return 'image/png';
200 case 'jpg':
201 case 'jpeg':
202 return 'image/jpeg';
203 case 'ico':
204 return 'image/vnd.microsoft.icon';
205 }
206 return 'document/unknown';
207}