Merge branch 'm20_MDL-26672_aiccfix' of git://github.com/danmarsden/moodle
[moodle.git] / theme / image.php
blob350db3eaf6cce59d708b8582527f52a9f0377dbc
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 // we need just the values from config.php and minlib.php
28 define('ABORT_AFTER_CONFIG', true);
29 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
31 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
32 $component = min_optional_param('component', 'moodle', 'SAFEDIR');
33 $image = min_optional_param('image', '', 'SAFEPATH');
34 $rev = min_optional_param('rev', -1, 'INT');
36 if (empty($component) or empty($image)) {
37 image_not_found();
40 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
41 // exists
42 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
43 // exists
44 } else {
45 image_not_found();
48 $candidatelocation = "$CFG->dataroot/cache/theme/$themename/pix/$component";
50 if ($rev > -1) {
51 if (file_exists("$candidatelocation/$image.error")) {
52 // this is a major speedup if there are multiple missing images,
53 // the only problem is that random requests may pollute our cache.
54 image_not_found();
56 $cacheimage = false;
57 if (file_exists("$candidatelocation/$image.gif")) {
58 $cacheimage = "$candidatelocation/$image.gif";
59 } else if (file_exists("$candidatelocation/$image.png")) {
60 $cacheimage = "$candidatelocation/$image.png";
61 } else if (file_exists("$candidatelocation/$image.jpg")) {
62 $cacheimage = "$candidatelocation/$image.jpg";
63 } else if (file_exists("$candidatelocation/$image.jpeg")) {
64 $cacheimage = "$candidatelocation/$image.jpeg";
65 } else if (file_exists("$candidatelocation/$image.ico")) {
66 $cacheimage = "$candidatelocation/$image.ico";
68 if ($cacheimage) {
69 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
70 // we do not actually need to verify the etag value because our files
71 // never change in cache because we increment the rev parameter
72 header('HTTP/1.1 304 Not Modified');
73 die;
75 send_cached_image($cacheimage, $rev);
79 //=================================================================================
80 // ok, now we need to start normal moodle script, we need to load all libs and $DB
81 define('ABORT_AFTER_CONFIG_CANCEL', true);
83 define('NO_MOODLE_COOKIES', true); // Session not used here
84 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
86 require("$CFG->dirroot/lib/setup.php");
88 $theme = theme_config::load($themename);
89 $imagefile = $theme->resolve_image_location($image, $component);
91 $rev = theme_get_revision();
93 if (empty($imagefile) or !is_readable($imagefile)) {
94 if ($rev > -1) {
95 // make note we can not find this file
96 $cacheimage = "$candidatelocation/$image.error";
97 $fp = fopen($cacheimage, 'w');
98 fclose($fp);
100 image_not_found();
104 if ($rev > -1) {
105 $pathinfo = pathinfo($imagefile);
106 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
107 if (!file_exists($cacheimage)) {
108 check_dir_exists(dirname($cacheimage));
109 copy($imagefile, $cacheimage);
111 send_cached_image($cacheimage, $rev);
113 } else {
114 send_uncached_image($imagefile);
118 //=================================================================================
119 //=== utility functions ==
120 // we are not using filelib because we need to fine tune all header
121 // parameters to get the best performance.
123 function send_cached_image($imagepath, $rev) {
124 $lifetime = 60*60*24*30; // 30 days
125 $pathinfo = pathinfo($imagepath);
126 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
128 switch($pathinfo['extension']) {
129 case 'gif' : $mimetype = 'image/gif'; break;
130 case 'png' : $mimetype = 'image/png'; break;
131 case 'jpg' : $mimetype = 'image/jpeg'; break;
132 case 'jpeg' : $mimetype = 'image/jpeg'; break;
133 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
134 default: $mimetype = 'document/unknown';
137 header('Etag: '.md5("$rev/$imagepath"));
138 header('Content-Disposition: inline; filename="'.$imagename.'"');
139 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
140 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
141 header('Pragma: ');
142 header('Cache-Control: max-age='.$lifetime);
143 header('Accept-Ranges: none');
144 header('Content-Type: '.$mimetype);
145 header('Content-Length: '.filesize($imagepath));
147 // no need to gzip already compressed images ;-)
149 readfile($imagepath);
150 die;
153 function send_uncached_image($imagepath) {
154 $pathinfo = pathinfo($imagepath);
155 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
157 switch($pathinfo['extension']) {
158 case 'gif' : $mimetype = 'image/gif'; break;
159 case 'png' : $mimetype = 'image/png'; break;
160 case 'jpg' : $mimetype = 'image/jpeg'; break;
161 case 'jpeg' : $mimetype = 'image/jpeg'; break;
162 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
163 default: $mimetype = 'document/unknown';
166 header('Content-Disposition: inline; filename="'.$imagename.'"');
167 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
168 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
169 header('Pragma: ');
170 header('Accept-Ranges: none');
171 header('Content-Type: '.$mimetype);
172 header('Content-Length: '.filesize($imagepath));
174 readfile($imagepath);
175 die;
178 function image_not_found() {
179 header('HTTP/1.0 404 not found');
180 die('Image was not found, sorry.');