MDL-26028 send_file performance improvements
[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
TL
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);
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)) {
e164a0df
PS
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();
c426ef3a 125 check_dir_exists(dirname($cacheimage));
78946b9b
PS
126 copy($imagefile, $cacheimage);
127 }
128 send_cached_image($cacheimage, $rev);
129
130} else {
131 send_uncached_image($imagefile);
132}
133
134
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.
139
140function send_cached_image($imagepath, $rev) {
13ea159e 141 $lifetime = 60*60*24*30; // 30 days
78946b9b
PS
142 $pathinfo = pathinfo($imagepath);
143 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
144
ccc0fff9 145 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
78946b9b
PS
146
147 header('Etag: '.md5("$rev/$imagepath"));
148 header('Content-Disposition: inline; filename="'.$imagename.'"');
13ea159e 149 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
78946b9b
PS
150 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
151 header('Pragma: ');
13ea159e 152 header('Cache-Control: max-age='.$lifetime);
78946b9b
PS
153 header('Accept-Ranges: none');
154 header('Content-Type: '.$mimetype);
155 header('Content-Length: '.filesize($imagepath));
156
7c986f04
PS
157 // no need to gzip already compressed images ;-)
158
78946b9b
PS
159 readfile($imagepath);
160 die;
161}
162
163function send_uncached_image($imagepath) {
164 $pathinfo = pathinfo($imagepath);
165 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
166
ccc0fff9 167 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
78946b9b
PS
168
169 header('Content-Disposition: inline; filename="'.$imagename.'"');
170 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
13ea159e 171 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
78946b9b
PS
172 header('Pragma: ');
173 header('Accept-Ranges: none');
174 header('Content-Type: '.$mimetype);
175 header('Content-Length: '.filesize($imagepath));
176
78946b9b
PS
177 readfile($imagepath);
178 die;
179}
180
181function image_not_found() {
182 header('HTTP/1.0 404 not found');
183 die('Image was not found, sorry.');
ccc0fff9
TL
184}
185
186function 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';
197 }
198 return 'document/unknown';
199}