Skip to content
Newer
Older
100644 234 lines (203 sloc) 8.3 KB
60f2c86 @skodak MDL-21189 custom yui image loading needed for combo yui loader
skodak authored
1 <?php
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
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
ee891da @skodak MDL-32612 disable error displaying in theme files and javascript
skodak authored
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);
30
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
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
34
35 // get special url parameters
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
36
37 list($parts, $slasharguments) = combo_params();
38 if (!$parts) {
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
39 combo_not_found();
40 }
41
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
42 $etag = sha1($parts);
945f19f @skodak MDL-21193 finally fixed YUI3 laoding - now fully cached and working -…
skodak authored
43 $parts = trim($parts, '&');
44
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
45 // find out what we are serving - only one type per request
46 $content = '';
47 if (substr($parts, -3) === '.js') {
a6338a1 @samhemelryk themes MDL-22826 Some minor changes to improve the IE user experience
samhemelryk authored
48 $mimetype = 'application/javascript';
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
49 } else if (substr($parts, -4) === '.css') {
50 $mimetype = 'text/css';
51 } else {
52 combo_not_found();
53 }
54
ccc0fff @tlevi MDL-28709: Performance: Send cache/contenttype headers with 304 respo…
tlevi authored
55 // if they are requesting a revision that's not -1, and they have supplied an
56 // If-Modified-Since header, we can send back a 304 Not Modified since the
57 // content never changes (the rev number is increased any time the content changes)
14b1579 @skodak MDL-29686 obey caching setting for moodle yui modules
skodak authored
58 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
59 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
aa603b1 @skodak MDL-28709 adding HTTP_IF_MODIFIED_SINCE and minor style tweaks
skodak authored
60 header('HTTP/1.1 304 Not Modified');
ccc0fff @tlevi MDL-28709: Performance: Send cache/contenttype headers with 304 respo…
tlevi authored
61 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
8c672cf @skodak MDL-32683 explicitly allow more caching of static content
skodak authored
62 header('Cache-Control: public, max-age='.$lifetime);
ccc0fff @tlevi MDL-28709: Performance: Send cache/contenttype headers with 304 respo…
tlevi authored
63 header('Content-Type: '.$mimetype);
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
64 header('Etag: '.$etag);
ccc0fff @tlevi MDL-28709: Performance: Send cache/contenttype headers with 304 respo…
tlevi authored
65 die;
66 }
67
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
68 $parts = explode('&', $parts);
7738729 @samhemelryk javascript MDL-23430 YUI moodle modules are no longer cached if CFG->…
samhemelryk authored
69 $cache = true;
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
70 $lastmodified = 0;
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
71
72 foreach ($parts as $part) {
945f19f @skodak MDL-21193 finally fixed YUI3 laoding - now fully cached and working -…
skodak authored
73 if (empty($part)) {
74 continue;
75 }
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
76 $part = min_clean_param($part, 'SAFEPATH');
77 $bits = explode('/', $part);
78 if (count($bits) < 2) {
a4738eb @skodak MDL-21193 improved error messages in combo loader
skodak authored
79 $content .= "\n// Wrong combo resource $part!\n";
80 continue;
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
81 }
2b722f8 @samhemelryk javascript MDL-22920 Added functionality to allow inclusion of Moodle…
samhemelryk authored
82 //debug($bits);
83 $version = array_shift($bits);
84 if ($version == 'moodle') {
78bfb56 @skodak MDL-21249 improved php docs and adding direct access prevention in co…
skodak authored
85 //TODO: this is a ugly hack because we should not load any libs here!
2f76f10 @andrewnicols MDL-31242 Prevent redefinition of MOODLE_INTERNAL when loading multip…
andrewnicols authored
86 if (!defined('MOODLE_INTERNAL')) {
87 define('MOODLE_INTERNAL', true);
88 require_once($CFG->libdir.'/moodlelib.php');
89 }
7738729 @samhemelryk javascript MDL-23430 YUI moodle modules are no longer cached if CFG->…
samhemelryk authored
90 $revision = (int)array_shift($bits);
91 if ($revision === -1) {
92 // Revision -1 says please don't cache the JS
93 $cache = false;
94 }
2b722f8 @samhemelryk javascript MDL-22920 Added functionality to allow inclusion of Moodle…
samhemelryk authored
95 $frankenstyle = array_shift($bits);
3b17690 @samhemelryk javascript MDL-22920 Added support for CSS images within the new modu…
samhemelryk authored
96 $filename = array_pop($bits);
2b722f8 @samhemelryk javascript MDL-22920 Added functionality to allow inclusion of Moodle…
samhemelryk authored
97 $dir = get_component_directory($frankenstyle);
3b17690 @samhemelryk javascript MDL-22920 Added support for CSS images within the new modu…
samhemelryk authored
98 if ($mimetype == 'text/css') {
99 $bits[] = 'assets';
100 $bits[] = 'skins';
101 $bits[] = 'sam';
102 }
103 $contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
2b722f8 @samhemelryk javascript MDL-22920 Added functionality to allow inclusion of Moodle…
samhemelryk authored
104 } else {
105 if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
106 $content .= "\n// Wrong yui version $part!\n";
107 continue;
108 }
109 $contentfile = "$CFG->libdir/yui/$part";
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
110 }
111 if (!file_exists($contentfile) or !is_file($contentfile)) {
5683815 MDL-31263 Rewrite block drag/drop using YUI3
Ruslan Kabalin authored
112 $content .= "\n// Combo resource $part ($contentfile) not found!\n";
a4738eb @skodak MDL-21193 improved error messages in combo loader
skodak authored
113 continue;
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
114 }
115 $filecontent = file_get_contents($contentfile);
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
116 $fmodified = filemtime($contentfile);
117 if ($fmodified > $lastmodified) {
118 $lastmodified = $fmodified;
119 }
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
120
6e7b460 @skodak MDL-32683 use slashargument urls for yui combo loader
skodak authored
121 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
122 $sep = ($slasharguments ? '/' : '?file=');
6e7b460 @skodak MDL-32683 use slashargument urls for yui combo loader
skodak authored
123
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
124 if ($mimetype === 'text/css') {
3b17690 @samhemelryk javascript MDL-22920 Added support for CSS images within the new modu…
samhemelryk authored
125 if ($version == 'moodle') {
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
126 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
3b17690 @samhemelryk javascript MDL-22920 Added support for CSS images within the new modu…
samhemelryk authored
127 } else if ($version == 'gallery') {
2a102b9 @samhemelryk block-community MDL-19314 Fixed gallery lightbox implementation and a…
samhemelryk authored
128 // search for all images in gallery module CSS and serve them through the yui_image.php script
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
129 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
2a102b9 @samhemelryk block-community MDL-19314 Fixed gallery lightbox implementation and a…
samhemelryk authored
130 } else {
4d90912 @samhemelryk yui combo MDLSITE-1032 Fixed relative paths to assets
samhemelryk authored
131 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
132 // I've added this as a separate regex so it can be easily removed once
133 // YUI standardise there CSS methods
6e6034e @skodak MDL-26153 fix yui combo loading for images with numbers in file name
skodak authored
134 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
4d90912 @samhemelryk yui combo MDLSITE-1032 Fixed relative paths to assets
samhemelryk authored
135
2a102b9 @samhemelryk block-community MDL-19314 Fixed gallery lightbox implementation and a…
samhemelryk authored
136 // search for all images in yui2 CSS and serve them through the yui_image.php script
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
137 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
2a102b9 @samhemelryk block-community MDL-19314 Fixed gallery lightbox implementation and a…
samhemelryk authored
138 }
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
139 }
140
141 $content .= $filecontent;
142 }
143
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
144 if ($lastmodified == 0) {
145 $lastmodified = time();
146 }
147
7738729 @samhemelryk javascript MDL-23430 YUI moodle modules are no longer cached if CFG->…
samhemelryk authored
148 if ($cache) {
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
149 combo_send_cached($content, $mimetype, $etag, $lastmodified);
7738729 @samhemelryk javascript MDL-23430 YUI moodle modules are no longer cached if CFG->…
samhemelryk authored
150 } else {
151 combo_send_uncached($content, $mimetype);
152 }
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
153
154
7738729 @samhemelryk javascript MDL-23430 YUI moodle modules are no longer cached if CFG->…
samhemelryk authored
155 /**
156 * Send the JavaScript cached
157 * @param string $content
158 * @param string $mimetype
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
159 * @param string $etag
160 * @param int $lastmodified
7738729 @samhemelryk javascript MDL-23430 YUI moodle modules are no longer cached if CFG->…
samhemelryk authored
161 */
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
162 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
163 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
164
165 header('Content-Disposition: inline; filename="combo"');
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
166 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
167 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
168 header('Pragma: ');
84e1e04 @skodak MDL-33636 add more yui caching goodness
skodak authored
169 header('Cache-Control: public, max-age='.$lifetime);
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
170 header('Accept-Ranges: none');
171 header('Content-Type: '.$mimetype);
dbe14f3 @skodak MDL-32683 fine tune yui resource caching
skodak authored
172 header('Etag: '.$etag);
7c986f0 @skodak MDL-21241 theme CSS and JS gzip compression + some more fixes
skodak authored
173 if (!min_enable_zlib_compression()) {
174 header('Content-Length: '.strlen($content));
175 }
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
176
177 echo $content;
178 die;
179 }
180
7738729 @samhemelryk javascript MDL-23430 YUI moodle modules are no longer cached if CFG->…
samhemelryk authored
181 /**
182 * Send the JavaScript uncached
183 * @param string $content
184 * @param string $mimetype
185 */
186 function combo_send_uncached($content, $mimetype) {
187 header('Content-Disposition: inline; filename="combo"');
188 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
189 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
190 header('Pragma: ');
191 header('Accept-Ranges: none');
192 header('Content-Type: '.$mimetype);
193 if (!min_enable_zlib_compression()) {
194 header('Content-Length: '.strlen($content));
195 }
196
197 echo $content;
198 die;
199 }
200
37c8259 @skodak MDL-28382 work around or detect REQUEST_URI IIS bug
skodak authored
201 function combo_not_found($message = '') {
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
202 header('HTTP/1.0 404 not found');
37c8259 @skodak MDL-28382 work around or detect REQUEST_URI IIS bug
skodak authored
203 if ($message) {
204 echo $message;
205 } else {
206 echo 'Combo resource not found, sorry.';
207 }
208 die;
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
209 }
210
211 function combo_params() {
18ac11b @skodak MDL-32849 add support for url rewriting in min_get_slash_argument() a…
skodak authored
212 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
213 // url rewriting
214 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
215 return array($slashargument, true);
216
217 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
37c8259 @skodak MDL-28382 work around or detect REQUEST_URI IIS bug
skodak authored
218 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
219 return array($parts[1], false);
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
220
6e7b460 @skodak MDL-32683 use slashargument urls for yui combo loader
skodak authored
221 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
18ac11b @skodak MDL-32849 add support for url rewriting in min_get_slash_argument() a…
skodak authored
222 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
223 return array($_SERVER['QUERY_STRING'], false);
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
224
6e7b460 @skodak MDL-32683 use slashargument urls for yui combo loader
skodak authored
225 } else if ($slashargument = min_get_slash_argument()) {
226 $slashargument = ltrim($slashargument, '/');
d5222fa @skodak MDL-32683 use slashargument urls for yui images
skodak authored
227 return array($slashargument, true);
6e7b460 @skodak MDL-32683 use slashargument urls for yui combo loader
skodak authored
228
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
229 } else {
37c8259 @skodak MDL-28382 work around or detect REQUEST_URI IIS bug
skodak authored
230 // unsupported server, sorry!
231 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
aa42314 @skodak MDL-21189 yui2/3 combo loader implemented - yay! admin settings in th…
skodak authored
232 }
233 }
Something went wrong with that request. Please try again.