MDL-38949 behat: Form field for selectyesno
[moodle.git] / theme / yui_image.php
blob967ed4bc31972180684880377cfaa7e761e26c0c
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);
44 if ($version == 'moodle' && count($parts) >= 3) {
45 if (!defined('ABORT_AFTER_CONFIG_CANCEL')) {
46 define('ABORT_AFTER_CONFIG_CANCEL', true);
47 define('NO_UPGRADE_CHECK', true);
48 define('NO_MOODLE_COOKIES', true);
49 require($CFG->libdir.'/setup.php');
51 $frankenstyle = array_shift($parts);
52 $module = array_shift($parts);
53 $image = array_pop($parts);
54 $subdir = join('/', $parts);
55 $dir = get_component_directory($frankenstyle);
57 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
58 $frankenstylemodulename = join('-', array($version, $frankenstyle, $module));
60 // By default, try and use the /yui/build directory.
61 $imagepath = $dir . '/yui/build/' . $frankenstylemodulename . '/assets/skins/sam/' . $image;
63 // If the shifted versions don't exist, fall back to the non-shifted file.
64 if (!file_exists($imagepath) or !is_file($imagepath)) {
65 $imagepath = $dir . '/yui/' . $module . '/assets/skins/sam/' . $image;
67 } else if ($version == 'gallery' && count($parts)==3) {
68 list($module, $version, $image) = $parts;
69 $imagepath = "$CFG->dirroot/lib/yui/gallery/$module/$version/assets/skins/sam/$image";
70 } else if (count($parts) == 1 && ($version == $CFG->yui3version || $version == $CFG->yui2version)) {
71 list($image) = $parts;
72 if ($version == $CFG->yui3version) {
73 $imagepath = "$CFG->dirroot/lib/yuilib/$CFG->yui3version/build/assets/skins/sam/$image";
74 } else {
75 $imagepath = "$CFG->dirroot/lib/yuilib/2in3/$CFG->yui2version/build/assets/skins/sam/$image";
77 } else {
78 yui_image_not_found();
81 if (!file_exists($imagepath)) {
82 yui_image_not_found();
85 $pathinfo = pathinfo($imagepath);
86 $imagename = $pathinfo['filename'].'.'.$pathinfo['extension'];
88 switch($pathinfo['extension']) {
89 case 'gif' : $mimetype = 'image/gif'; break;
90 case 'png' : $mimetype = 'image/png'; break;
91 case 'jpg' : $mimetype = 'image/jpeg'; break;
92 case 'jpeg' : $mimetype = 'image/jpeg'; break;
93 case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break;
94 default: $mimetype = 'document/unknown';
97 // if they are requesting a revision that's not -1, and they have supplied an
98 // If-Modified-Since header, we can send back a 304 Not Modified since the
99 // content never changes (the rev number is increased any time the content changes)
100 if (strpos($path, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
101 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
102 header('HTTP/1.1 304 Not Modified');
103 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
104 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
105 header('Cache-Control: public, max-age='.$lifetime);
106 header('Content-Type: '.$mimetype);
107 header('Etag: '.$etag);
108 die;
111 yui_image_cached($imagepath, $imagename, $mimetype, $etag);
114 function yui_image_cached($imagepath, $imagename, $mimetype, $etag) {
115 global $CFG;
116 require("$CFG->dirroot/lib/xsendfilelib.php");
118 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
120 header('Content-Disposition: inline; filename="'.$imagename.'"');
121 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT');
122 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
123 header('Pragma: ');
124 header('Cache-Control: public, max-age=315360000');
125 header('Accept-Ranges: none');
126 header('Content-Type: '.$mimetype);
127 header('Content-Length: '.filesize($imagepath));
128 header('Etag: '.$etag);
130 if (xsendfile($imagepath)) {
131 die;
134 // no need to gzip already compressed images ;-)
136 readfile($imagepath);
137 die;
140 function yui_image_not_found() {
141 header('HTTP/1.0 404 not found');
142 die('Image was not found, sorry.');