MDL-41878 JavaScript: Add an alias for Moodle in the YUI loader
[moodle.git] / theme / yui_image.php
CommitLineData
60f2c866 1<?php
60f2c866
PS
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * This file is responsible for serving of yui images
19 *
574909ef 20 * @package core
60f2c866
PS
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
ee891dac
PS
26// disable moodle specific debug messages and any errors in output,
27// comment out when debugging or better look into error log!
28define('NO_DEBUG_DISPLAY', true);
29
60f2c866
PS
30// we need just the values from config.php and minlib.php
31define('ABORT_AFTER_CONFIG', true);
32require('../config.php'); // this stops immediately at the beginning of lib/setup.php
33
d5222fae
PS
34if ($slashargument = min_get_slash_argument()) {
35 $path = ltrim($slashargument, '/');
36} else {
37 $path = min_optional_param('file', '', 'SAFEPATH');
38}
60f2c866 39
1c6d3af5 40$etag = sha1($path);
60f2c866 41$parts = explode('/', $path);
2a102b90 42$version = array_shift($parts);
03e36e70
AN
43if ($version === 'm') {
44 $version = 'moodle';
45}
3b17690c 46if ($version == 'moodle' && count($parts) >= 3) {
3b17690c
SH
47 $frankenstyle = array_shift($parts);
48 $module = array_shift($parts);
49 $image = array_pop($parts);
50 $subdir = join('/', $parts);
cba94cec 51 $dir = core_component::get_component_directory($frankenstyle);
a45e8fd3
ARN
52
53 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
54 $frankenstylemodulename = join('-', array($version, $frankenstyle, $module));
55
56 // By default, try and use the /yui/build directory.
57 $imagepath = $dir . '/yui/build/' . $frankenstylemodulename . '/assets/skins/sam/' . $image;
58
59 // If the shifted versions don't exist, fall back to the non-shifted file.
60 if (!file_exists($imagepath) or !is_file($imagepath)) {
61 $imagepath = $dir . '/yui/' . $module . '/assets/skins/sam/' . $image;
62 }
3b17690c 63} else if ($version == 'gallery' && count($parts)==3) {
2a102b90
SH
64 list($module, $version, $image) = $parts;
65 $imagepath = "$CFG->dirroot/lib/yui/gallery/$module/$version/assets/skins/sam/$image";
66} else if (count($parts) == 1 && ($version == $CFG->yui3version || $version == $CFG->yui2version)) {
67 list($image) = $parts;
68 if ($version == $CFG->yui3version) {
aaee690b 69 $imagepath = "$CFG->dirroot/lib/yuilib/$CFG->yui3version/assets/skins/sam/$image";
2a102b90 70 } else {
1c76d55a 71 $imagepath = "$CFG->dirroot/lib/yuilib/2in3/$CFG->yui2version/build/assets/skins/sam/$image";
2a102b90 72 }
60f2c866
PS
73} else {
74 yui_image_not_found();
75}
76
77if (!file_exists($imagepath)) {
78 yui_image_not_found();
79}
80
dbe14f39
PS
81$pathinfo = pathinfo($imagepath);
82$imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
83
84switch($pathinfo['extension']) {
85 case 'gif' : $mimetype = 'image/gif'; break;
86 case 'png' : $mimetype = 'image/png'; break;
87 case 'jpg' : $mimetype = 'image/jpeg'; break;
88 case 'jpeg' : $mimetype = 'image/jpeg'; break;
89 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
90 default: $mimetype = 'document/unknown';
91}
92
93// if they are requesting a revision that's not -1, and they have supplied an
94// If-Modified-Since header, we can send back a 304 Not Modified since the
95// content never changes (the rev number is increased any time the content changes)
1c6d3af5 96if (strpos($path, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
dbe14f39
PS
97 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
98 header('HTTP/1.1 304 Not Modified');
99 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
100 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
101 header('Cache-Control: public, max-age='.$lifetime);
102 header('Content-Type: '.$mimetype);
06eca486 103 header('Etag: "'.$etag.'"');
dbe14f39
PS
104 die;
105}
60f2c866 106
dbe14f39 107yui_image_cached($imagepath, $imagename, $mimetype, $etag);
60f2c866
PS
108
109
dbe14f39 110function yui_image_cached($imagepath, $imagename, $mimetype, $etag) {
7e9f1b63
PS
111 global $CFG;
112 require("$CFG->dirroot/lib/xsendfilelib.php");
113
dbe14f39 114 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
60f2c866
PS
115
116 header('Content-Disposition: inline; filename="'.$imagename.'"');
117 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
118 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
119 header('Pragma: ');
8c672cf9 120 header('Cache-Control: public, max-age=315360000');
60f2c866
PS
121 header('Accept-Ranges: none');
122 header('Content-Type: '.$mimetype);
123 header('Content-Length: '.filesize($imagepath));
06eca486 124 header('Etag: "'.$etag.'"');
60f2c866 125
7e9f1b63
PS
126 if (xsendfile($imagepath)) {
127 die;
128 }
129
7c986f04
PS
130 // no need to gzip already compressed images ;-)
131
60f2c866
PS
132 readfile($imagepath);
133 die;
134}
135
136function yui_image_not_found() {
137 header('HTTP/1.0 404 not found');
138 die('Image was not found, sorry.');
139}