weekly release 2.2dev
[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
78946b9b
PS
27// we need just the values from config.php and minlib.php
28define('ABORT_AFTER_CONFIG', true);
29require('../config.php'); // this stops immediately at the beginning of lib/setup.php
30
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');
35
36if (empty($component) or empty($image)) {
37 image_not_found();
38}
39
73e504bc
PS
40if (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 {
78946b9b
PS
45 image_not_found();
46}
47
48$candidatelocation = "$CFG->dataroot/cache/theme/$themename/pix/$component";
49
50if ($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();
55 }
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";
67 }
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;
74 }
75 send_cached_image($cacheimage, $rev);
76 }
77}
78
79//=================================================================================
80// ok, now we need to start normal moodle script, we need to load all libs and $DB
81define('ABORT_AFTER_CONFIG_CANCEL', true);
82
83define('NO_MOODLE_COOKIES', true); // Session not used here
84define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
85
86require("$CFG->dirroot/lib/setup.php");
87
88$theme = theme_config::load($themename);
89$imagefile = $theme->resolve_image_location($image, $component);
90
91$rev = theme_get_revision();
92
93if (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);
99 }
100 image_not_found();
101}
102
103
104if ($rev > -1) {
105 $pathinfo = pathinfo($imagefile);
106 $cacheimage = "$candidatelocation/$image.".$pathinfo['extension'];
107 if (!file_exists($cacheimage)) {
c426ef3a 108 check_dir_exists(dirname($cacheimage));
78946b9b
PS
109 copy($imagefile, $cacheimage);
110 }
111 send_cached_image($cacheimage, $rev);
112
113} else {
114 send_uncached_image($imagefile);
115}
116
117
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.
122
123function send_cached_image($imagepath, $rev) {
13ea159e 124 $lifetime = 60*60*24*30; // 30 days
78946b9b
PS
125 $pathinfo = pathinfo($imagepath);
126 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
127
128 switch($pathinfo['extension']) {
13ea159e
PS
129 case 'gif' : $mimetype = 'image/gif'; break;
130 case 'png' : $mimetype = 'image/png'; break;
131 case 'jpg' : $mimetype = 'image/jpeg'; break;
78946b9b 132 case 'jpeg' : $mimetype = 'image/jpeg'; break;
13ea159e 133 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
78946b9b
PS
134 default: $mimetype = 'document/unknown';
135 }
136
137 header('Etag: '.md5("$rev/$imagepath"));
138 header('Content-Disposition: inline; filename="'.$imagename.'"');
13ea159e 139 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
78946b9b
PS
140 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
141 header('Pragma: ');
13ea159e 142 header('Cache-Control: max-age='.$lifetime);
78946b9b
PS
143 header('Accept-Ranges: none');
144 header('Content-Type: '.$mimetype);
145 header('Content-Length: '.filesize($imagepath));
146
7c986f04
PS
147 // no need to gzip already compressed images ;-)
148
78946b9b
PS
149 readfile($imagepath);
150 die;
151}
152
153function send_uncached_image($imagepath) {
154 $pathinfo = pathinfo($imagepath);
155 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
156
157 switch($pathinfo['extension']) {
13ea159e
PS
158 case 'gif' : $mimetype = 'image/gif'; break;
159 case 'png' : $mimetype = 'image/png'; break;
160 case 'jpg' : $mimetype = 'image/jpeg'; break;
78946b9b 161 case 'jpeg' : $mimetype = 'image/jpeg'; break;
13ea159e 162 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
78946b9b
PS
163 default: $mimetype = 'document/unknown';
164 }
165
166 header('Content-Disposition: inline; filename="'.$imagename.'"');
167 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
13ea159e 168 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 15) .' GMT');
78946b9b
PS
169 header('Pragma: ');
170 header('Accept-Ranges: none');
171 header('Content-Type: '.$mimetype);
172 header('Content-Length: '.filesize($imagepath));
173
78946b9b
PS
174 readfile($imagepath);
175 die;
176}
177
178function image_not_found() {
179 header('HTTP/1.0 404 not found');
180 die('Image was not found, sorry.');
181}