Merge branch 'wip-mdl-41843-m24' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / theme / yui_image.php
blob8d3436ec2b45976cf66f744f559b62de804a994d
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 // disable moodle specific debug messages and any errors in output,
28 // comment out when debugging or better look into error log!
29 define('NO_DEBUG_DISPLAY', true);
31 // we need just the values from config.php and minlib.php
32 define('ABORT_AFTER_CONFIG', true);
33 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
35 if ($slashargument = min_get_slash_argument()) {
36 $path = ltrim($slashargument, '/');
37 } else {
38 $path = min_optional_param('file', '', 'SAFEPATH');
41 $etag = sha1($path);
42 $parts = explode('/', $path);
43 $version = array_shift($parts);
45 if ($version == 'moodle' && count($parts) >= 3) {
46 //TODO: this is a ugly hack because we should not load any libs here!
47 define('MOODLE_INTERNAL', true);
48 require_once($CFG->libdir.'/moodlelib.php');
49 $frankenstyle = array_shift($parts);
50 $module = array_shift($parts);
51 $image = array_pop($parts);
52 $subdir = join('/', $parts);
53 $dir = get_component_directory($frankenstyle);
54 $imagepath = $dir.'/yui/'.$module.'/assets/skins/sam/'.$image;
55 } else if ($version == 'gallery' && count($parts)==3) {
56 list($module, $version, $image) = $parts;
57 $imagepath = "$CFG->dirroot/lib/yui/gallery/$module/$version/assets/skins/sam/$image";
58 } else if (count($parts) == 1 && ($version == $CFG->yui3version || $version == $CFG->yui2version)) {
59 list($image) = $parts;
60 if ($version == $CFG->yui3version) {
61 $imagepath = "$CFG->dirroot/lib/yuilib/$CFG->yui3version/build/assets/skins/sam/$image";
62 } else {
63 $imagepath = "$CFG->dirroot/lib/yuilib/2in3/$CFG->yui2version/build/assets/skins/sam/$image";
65 } else {
66 yui_image_not_found();
69 if (!file_exists($imagepath)) {
70 yui_image_not_found();
73 $pathinfo = pathinfo($imagepath);
74 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
76 switch($pathinfo['extension']) {
77 case 'gif' : $mimetype = 'image/gif'; break;
78 case 'png' : $mimetype = 'image/png'; break;
79 case 'jpg' : $mimetype = 'image/jpeg'; break;
80 case 'jpeg' : $mimetype = 'image/jpeg'; break;
81 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
82 default: $mimetype = 'document/unknown';
85 // if they are requesting a revision that's not -1, and they have supplied an
86 // If-Modified-Since header, we can send back a 304 Not Modified since the
87 // content never changes (the rev number is increased any time the content changes)
88 if (strpos($path, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
89 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
90 header('HTTP/1.1 304 Not Modified');
91 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
92 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
93 header('Cache-Control: public, max-age='.$lifetime);
94 header('Content-Type: '.$mimetype);
95 header('Etag: "'.$etag.'"');
96 die;
99 yui_image_cached($imagepath, $imagename, $mimetype, $etag);
102 function yui_image_cached($imagepath, $imagename, $mimetype, $etag) {
103 global $CFG;
104 require("$CFG->dirroot/lib/xsendfilelib.php");
106 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
108 header('Content-Disposition: inline; filename="'.$imagename.'"');
109 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
110 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
111 header('Pragma: ');
112 header('Cache-Control: public, max-age=315360000');
113 header('Accept-Ranges: none');
114 header('Content-Type: '.$mimetype);
115 header('Content-Length: '.filesize($imagepath));
116 header('Etag: "'.$etag.'"');
118 if (xsendfile($imagepath)) {
119 die;
122 // no need to gzip already compressed images ;-)
124 readfile($imagepath);
125 die;
128 function yui_image_not_found() {
129 header('HTTP/1.0 404 not found');
130 die('Image was not found, sorry.');