MDL-27588 Fixed up several bugs with the formal_white theme
[moodle.git] / theme / yui_image.php
blob23ac987ff291e961a89d5e9ecfaf985ee76ef067
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 of yui 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 $path = min_optional_param('file', '', 'SAFEPATH');
33 $parts = explode('/', $path);
34 $version = array_shift($parts);
36 if ($version == 'moodle' && count($parts) >= 3) {
37 //TODO: this is a ugly hack because we should not load any libs here!
38 define('MOODLE_INTERNAL', true);
39 require_once($CFG->libdir.'/moodlelib.php');
40 $frankenstyle = array_shift($parts);
41 $module = array_shift($parts);
42 $image = array_pop($parts);
43 $subdir = join('/', $parts);
44 $dir = get_component_directory($frankenstyle);
45 $imagepath = $dir.'/yui/'.$module.'/assets/skins/sam/'.$image;
46 } else if ($version == 'gallery' && count($parts)==3) {
47 list($module, $version, $image) = $parts;
48 $imagepath = "$CFG->dirroot/lib/yui/gallery/$module/$version/assets/skins/sam/$image";
49 } else if (count($parts) == 1 && ($version == $CFG->yui3version || $version == $CFG->yui2version)) {
50 list($image) = $parts;
51 if ($version == $CFG->yui3version) {
52 $imagepath = "$CFG->dirroot/lib/yui/$CFG->yui3version/build/assets/skins/sam/$image";
53 } else {
54 $imagepath = "$CFG->dirroot/lib/yui/$CFG->yui2version/build/assets/skins/sam/$image";
56 } else {
57 yui_image_not_found();
60 if (!file_exists($imagepath)) {
61 yui_image_not_found();
64 yui_image_cached($imagepath);
68 function yui_image_cached($imagepath) {
69 $lifetime = 60*60*24*300; // 300 days === forever
70 $pathinfo = pathinfo($imagepath);
71 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
73 switch($pathinfo['extension']) {
74 case 'gif' : $mimetype = 'image/gif'; break;
75 case 'png' : $mimetype = 'image/png'; break;
76 case 'jpg' : $mimetype = 'image/jpeg'; break;
77 case 'jpeg' : $mimetype = 'image/jpeg'; break;
78 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
79 default: $mimetype = 'document/unknown';
82 header('Content-Disposition: inline; filename="'.$imagename.'"');
83 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
84 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
85 header('Pragma: ');
86 header('Cache-Control: max-age=315360000');
87 header('Accept-Ranges: none');
88 header('Content-Type: '.$mimetype);
89 header('Content-Length: '.filesize($imagepath));
91 // no need to gzip already compressed images ;-)
93 readfile($imagepath);
94 die;
97 function yui_image_not_found() {
98 header('HTTP/1.0 404 not found');
99 die('Image was not found, sorry.');