MDL-21193 finally fixed YUI3 laoding - now fully cached and working - yay!
[moodle.git] / theme / yui_image.php
CommitLineData
60f2c866
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 of yui 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
26
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$path = min_optional_param('file', '', 'SAFEPATH');
32
33$parts = explode('/', $path);
34if (count($parts) != 2) {
35 yui_image_not_found();
36}
37list($version, $image) = $parts;
38
39if ($version == $CFG->yui3version) {
aa42314d 40 $imagepath = "$CFG->dirroot/lib/yui/$CFG->yui3version/build/assets/skins/sam/$image";
60f2c866 41} else if ($version == $CFG->yui2version) {
aa42314d 42 $imagepath = "$CFG->dirroot/lib/yui/$CFG->yui2version/build/assets/skins/sam/$image";
60f2c866
PS
43} else {
44 yui_image_not_found();
45}
46
47if (!file_exists($imagepath)) {
48 yui_image_not_found();
49}
50
51yui_image_cached($imagepath);
52
53
54
55function yui_image_cached($imagepath) {
56 $lifetime = 60*60*24*300; // 300 days === forever
57 $pathinfo = pathinfo($imagepath);
58 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
59
60 switch($pathinfo['extension']) {
61 case 'gif' : $mimetype = 'image/gif'; break;
62 case 'png' : $mimetype = 'image/png'; break;
63 case 'jpg' : $mimetype = 'image/jpeg'; break;
64 case 'jpeg' : $mimetype = 'image/jpeg'; break;
65 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
66 default: $mimetype = 'document/unknown';
67 }
68
69 header('Content-Disposition: inline; filename="'.$imagename.'"');
70 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
71 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
72 header('Pragma: ');
945f19f7 73 header('Cache-Control: max-age=315360000');
60f2c866
PS
74 header('Accept-Ranges: none');
75 header('Content-Type: '.$mimetype);
76 header('Content-Length: '.filesize($imagepath));
77
78 while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
79 readfile($imagepath);
80 die;
81}
82
83function yui_image_not_found() {
84 header('HTTP/1.0 404 not found');
85 die('Image was not found, sorry.');
86}