Merge branch 'wip-MDL-25454-master' of git://github.com/marinaglancy/moodle
[moodle.git] / theme / image.php
blobe00f5592f7f69f0210c2bb097a72b7a0254c57be
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->cachedir/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 $ext = 'gif';
60 } else if (file_exists("$candidatelocation/$image.png")) {
61 $cacheimage = "$candidatelocation/$image.png";
62 $ext = 'png';
63 } else if (file_exists("$candidatelocation/$image.jpg")) {
64 $cacheimage = "$candidatelocation/$image.jpg";
65 $ext = 'jpg';
66 } else if (file_exists("$candidatelocation/$image.jpeg")) {
67 $cacheimage = "$candidatelocation/$image.jpeg";
68 $ext = 'jpeg';
69 } else if (file_exists("$candidatelocation/$image.ico")) {
70 $cacheimage = "$candidatelocation/$image.ico";
71 $ext = 'ico';
73 if ($cacheimage) {
74 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
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 $lifetime = 60*60*24*30; // 30 days
78 $mimetype = get_contenttype_from_ext($ext);
79 header('HTTP/1.1 304 Not Modified');
80 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
81 header('Cache-Control: max-age='.$lifetime);
82 header('Content-Type: '.$mimetype);
83 die;
85 send_cached_image($cacheimage, $rev);
89 //=================================================================================
90 // ok, now we need to start normal moodle script, we need to load all libs and $DB
91 define('ABORT_AFTER_CONFIG_CANCEL', true);
93 define('NO_MOODLE_COOKIES', true); // Session not used here
94 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
96 require("$CFG->dirroot/lib/setup.php");
98 $theme = theme_config::load($themename);
99 $imagefile = $theme->resolve_image_location($image, $component);
101 $rev = theme_get_revision();
103 if (empty($imagefile) or !is_readable($imagefile)) {
104 if ($rev > -1) {
105 // make note we can not find this file
106 $cacheimage = "$candidatelocation/$image.error";
107 $fp = fopen($cacheimage, 'w');
108 fclose($fp);
110 image_not_found();
114 if ($rev > -1) {
115 $pathinfo = pathinfo($imagefile);
116 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
117 if (!file_exists($cacheimage)) {
118 check_dir_exists(dirname($cacheimage));
119 copy($imagefile, $cacheimage);
121 send_cached_image($cacheimage, $rev);
123 } else {
124 send_uncached_image($imagefile);
128 //=================================================================================
129 //=== utility functions ==
130 // we are not using filelib because we need to fine tune all header
131 // parameters to get the best performance.
133 function send_cached_image($imagepath, $rev) {
134 $lifetime = 60*60*24*30; // 30 days
135 $pathinfo = pathinfo($imagepath);
136 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
138 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
140 header('Etag: '.md5("$rev/$imagepath"));
141 header('Content-Disposition: inline; filename="'.$imagename.'"');
142 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
143 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
144 header('Pragma: ');
145 header('Cache-Control: max-age='.$lifetime);
146 header('Accept-Ranges: none');
147 header('Content-Type: '.$mimetype);
148 header('Content-Length: '.filesize($imagepath));
150 // no need to gzip already compressed images ;-)
152 readfile($imagepath);
153 die;
156 function send_uncached_image($imagepath) {
157 $pathinfo = pathinfo($imagepath);
158 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
160 $mimetype = get_contenttype_from_ext($pathinfo['extension']);
162 header('Content-Disposition: inline; filename="'.$imagename.'"');
163 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
164 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
165 header('Pragma: ');
166 header('Accept-Ranges: none');
167 header('Content-Type: '.$mimetype);
168 header('Content-Length: '.filesize($imagepath));
170 readfile($imagepath);
171 die;
174 function image_not_found() {
175 header('HTTP/1.0 404 not found');
176 die('Image was not found, sorry.');
179 function get_contenttype_from_ext($ext) {
180 switch ($ext) {
181 case 'gif':
182 return 'image/gif';
183 case 'png':
184 return 'image/png';
185 case 'jpg':
186 case 'jpeg':
187 return 'image/jpeg';
188 case 'ico':
189 return 'image/vnd.microsoft.icon';
191 return 'document/unknown';