3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This file is responsible for serving the one theme and plugin images.
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)) {
40 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
42 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
48 $candidatelocation = "$CFG->dataroot/cache/theme/$themename/pix/$component";
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.
57 if (file_exists("$candidatelocation/$image.gif")) {
58 $cacheimage = "$candidatelocation/$image.gif";
60 } else if (file_exists("$candidatelocation/$image.png")) {
61 $cacheimage = "$candidatelocation/$image.png";
63 } else if (file_exists("$candidatelocation/$image.jpg")) {
64 $cacheimage = "$candidatelocation/$image.jpg";
66 } else if (file_exists("$candidatelocation/$image.jpeg")) {
67 $cacheimage = "$candidatelocation/$image.jpeg";
69 } else if (file_exists("$candidatelocation/$image.ico")) {
70 $cacheimage = "$candidatelocation/$image.ico";
74 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
75 // we do not actually need to verify the etag value because our files
76 // never change in cache because we increment the rev parameter
77 header('HTTP/1.1 304 Not Modified');
79 $lifetime = 60*60*24*30; // 30 days
80 $mimetype = get_contenttype_from_ext($ext);
81 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
82 header('Cache-Control: max-age='.$lifetime);
83 header('Content-Type: '.$mimetype);
86 send_cached_image($cacheimage, $rev);
90 //=================================================================================
91 // ok, now we need to start normal moodle script, we need to load all libs and $DB
92 define('ABORT_AFTER_CONFIG_CANCEL', true);
94 define('NO_MOODLE_COOKIES', true); // Session not used here
95 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
97 require("$CFG->dirroot/lib/setup.php");
99 $theme = theme_config::load($themename);
100 $imagefile = $theme->resolve_image_location($image, $component);
102 $rev = theme_get_revision();
104 if (empty($imagefile) or !is_readable($imagefile)) {
106 // make note we can not find this file
107 $cacheimage = "$candidatelocation/$image.error";
108 $fp = fopen($cacheimage, 'w');
116 $pathinfo = pathinfo($imagefile);
117 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
118 if (!file_exists($cacheimage)) {
119 check_dir_exists(dirname($cacheimage));
120 copy($imagefile, $cacheimage);
122 send_cached_image($cacheimage, $rev);
125 send_uncached_image($imagefile);
129 //=================================================================================
130 //=== utility functions ==
131 // we are not using filelib because we need to fine tune all header
132 // parameters to get the best performance.
134 function send_cached_image($imagepath, $rev) {
135 $lifetime = 60*60*24*30; // 30 days
136 $pathinfo = pathinfo($imagepath);
137 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
139 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
141 header('Etag: '.md5("$rev/$imagepath"));
142 header('Content-Disposition: inline; filename="'.$imagename.'"');
143 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
144 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
146 header('Cache-Control: max-age='.$lifetime);
147 header('Accept-Ranges: none');
148 header('Content-Type: '.$mimetype);
149 header('Content-Length: '.filesize($imagepath));
151 // no need to gzip already compressed images ;-)
153 readfile($imagepath);
157 function send_uncached_image($imagepath) {
158 $pathinfo = pathinfo($imagepath);
159 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
161 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
163 header('Content-Disposition: inline; filename="'.$imagename.'"');
164 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
165 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
167 header('Accept-Ranges: none');
168 header('Content-Type: '.$mimetype);
169 header('Content-Length: '.filesize($imagepath));
171 readfile($imagepath);
175 function image_not_found() {
176 header('HTTP/1.0 404 not found');
177 die('Image was not found, sorry.');
180 function get_contenttype_from_ext($ext) {
190 return 'image/vnd.microsoft.icon';
192 return 'document/unknown';