2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file is responsible for serving of yui images
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // disable moodle specific debug messages and any errors in output,
27 // comment out when debugging or better look into error log!
28 define('NO_DEBUG_DISPLAY', true);
30 // we need just the values from config.php and minlib.php
31 define('ABORT_AFTER_CONFIG', true);
32 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34 if ($slashargument = min_get_slash_argument()) {
35 $path = ltrim($slashargument, '/');
37 $path = min_optional_param('file', '', 'SAFEPATH');
41 $parts = explode('/', $path);
42 $version = array_shift($parts);
43 if ($version == 'moodle' && count($parts) >= 3) {
44 if (!defined('ABORT_AFTER_CONFIG_CANCEL')) {
45 define('ABORT_AFTER_CONFIG_CANCEL', true);
46 define('NO_UPGRADE_CHECK', true);
47 define('NO_MOODLE_COOKIES', true);
48 require($CFG->libdir
.'/setup.php');
50 $frankenstyle = array_shift($parts);
51 $module = array_shift($parts);
52 $image = array_pop($parts);
53 $subdir = join('/', $parts);
54 $dir = get_component_directory($frankenstyle);
56 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
57 $frankenstylemodulename = join('-', array($version, $frankenstyle, $module));
59 // By default, try and use the /yui/build directory.
60 $imagepath = $dir . '/yui/build/' . $frankenstylemodulename . '/assets/skins/sam/' . $image;
62 // If the shifted versions don't exist, fall back to the non-shifted file.
63 if (!file_exists($imagepath) or !is_file($imagepath)) {
64 $imagepath = $dir . '/yui/' . $module . '/assets/skins/sam/' . $image;
66 } else if ($version == 'gallery' && count($parts)==3) {
67 list($module, $version, $image) = $parts;
68 $imagepath = "$CFG->dirroot/lib/yui/gallery/$module/$version/assets/skins/sam/$image";
69 } else if (count($parts) == 1 && ($version == $CFG->yui3version ||
$version == $CFG->yui2version
)) {
70 list($image) = $parts;
71 if ($version == $CFG->yui3version
) {
72 $imagepath = "$CFG->dirroot/lib/yuilib/$CFG->yui3version/build/assets/skins/sam/$image";
74 $imagepath = "$CFG->dirroot/lib/yuilib/2in3/$CFG->yui2version/build/assets/skins/sam/$image";
77 yui_image_not_found();
80 if (!file_exists($imagepath)) {
81 yui_image_not_found();
84 $pathinfo = pathinfo($imagepath);
85 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
87 switch($pathinfo['extension']) {
88 case 'gif' : $mimetype = 'image/gif'; break;
89 case 'png' : $mimetype = 'image/png'; break;
90 case 'jpg' : $mimetype = 'image/jpeg'; break;
91 case 'jpeg' : $mimetype = 'image/jpeg'; break;
92 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
93 default: $mimetype = 'document/unknown';
96 // if they are requesting a revision that's not -1, and they have supplied an
97 // If-Modified-Since header, we can send back a 304 Not Modified since the
98 // content never changes (the rev number is increased any time the content changes)
99 if (strpos($path, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) ||
!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
100 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
101 header('HTTP/1.1 304 Not Modified');
102 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
103 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
104 header('Cache-Control: public, max-age='.$lifetime);
105 header('Content-Type: '.$mimetype);
106 header('Etag: "'.$etag.'"');
110 yui_image_cached($imagepath, $imagename, $mimetype, $etag);
113 function yui_image_cached($imagepath, $imagename, $mimetype, $etag) {
115 require("$CFG->dirroot/lib/xsendfilelib.php");
117 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
119 header('Content-Disposition: inline; filename="'.$imagename.'"');
120 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
121 header('Expires: '. gmdate('D, d M Y H:i:s', time() +
$lifetime) .' GMT');
123 header('Cache-Control: public, max-age=315360000');
124 header('Accept-Ranges: none');
125 header('Content-Type: '.$mimetype);
126 header('Content-Length: '.filesize($imagepath));
127 header('Etag: "'.$etag.'"');
129 if (xsendfile($imagepath)) {
133 // no need to gzip already compressed images ;-)
135 readfile($imagepath);
139 function yui_image_not_found() {
140 header('HTTP/1.0 404 not found');
141 die('Image was not found, sorry.');