MDL-42081 EditPDF: Add default stamp images
[moodle.git] / theme / yui_combo.php
blobd98865200b10c09659f713a12425516faeeaaba3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file is responsible for serving of yui Javascript and CSS
20 * @package core
21 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // disable moodle specific debug messages and any errors in output,
27 // comment out when debugging or better look into error log!
28 define('NO_DEBUG_DISPLAY', true);
30 // we need just the values from config.php and minlib.php
31 define('ABORT_AFTER_CONFIG', true);
32 require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34 // get special url parameters
36 list($parts, $slasharguments) = combo_params();
37 if (!$parts) {
38 combo_not_found();
41 $etag = sha1($parts);
42 $parts = trim($parts, '&');
44 // find out what we are serving - only one type per request
45 $content = '';
46 if (substr($parts, -3) === '.js') {
47 $mimetype = 'application/javascript';
48 } else if (substr($parts, -4) === '.css') {
49 $mimetype = 'text/css';
50 } else {
51 combo_not_found();
54 // if they are requesting a revision that's not -1, and they have supplied an
55 // If-Modified-Since header, we can send back a 304 Not Modified since the
56 // content never changes (the rev number is increased any time the content changes)
57 if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
58 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
59 header('HTTP/1.1 304 Not Modified');
60 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
61 header('Cache-Control: public, max-age='.$lifetime);
62 header('Content-Type: '.$mimetype);
63 header('Etag: "'.$etag.'"');
64 die;
67 $parts = explode('&', $parts);
68 $cache = true;
69 $lastmodified = 0;
71 while (count($parts)) {
72 $part = array_shift($parts);
73 if (empty($part)) {
74 continue;
76 $filecontent = '';
77 $part = min_clean_param($part, 'SAFEPATH');
78 $bits = explode('/', $part);
79 if (count($bits) < 2) {
80 $content .= "\n// Wrong combo resource $part!\n";
81 continue;
84 $version = array_shift($bits);
85 if ($version === 'rollup') {
86 $revision = array_shift($bits);
87 $rollupname = array_shift($bits);
89 if (strpos($rollupname, 'yui-moodlesimple') !== false) {
90 if (substr($rollupname, -3) === '.js') {
91 // Determine whether we should minify this rollup.
92 preg_match('/(-min)?\.js/', $rollupname, $matches);
93 $filesuffix = '.js';
94 if (isset($matches[1])) {
95 $filesuffix = '-min.js';
97 $type = 'js';
98 } else if (substr($rollupname, -4) === '.css') {
99 $type = 'css';
100 } else {
101 continue;
104 $yuimodules = array(
105 // Include everything from original SimpleYUI,
106 // this list can be built using http://yuilibrary.com/yui/configurator/ by selecting all modules
107 // listed in https://github.com/yui/yui3/blob/v3.12.0/build/simpleyui/simpleyui.js#L21327
108 'yui',
109 'yui-base',
110 'get',
111 'features',
112 'loader-base',
113 'loader-rollup',
114 'loader-yui3',
115 'oop',
116 'event-custom-base',
117 'dom-core',
118 'dom-base',
119 'color-base',
120 'dom-style',
121 'selector-native',
122 'selector',
123 'node-core',
124 'node-base',
125 'event-base',
126 'event-base-ie',
127 'pluginhost-base',
128 'pluginhost-config',
129 'event-delegate',
130 'node-event-delegate',
131 'node-pluginhost',
132 'dom-screen',
133 'node-screen',
134 'node-style',
135 'querystring-stringify-simple',
136 'io-base',
137 'json-parse',
138 'transition',
139 'selector-css2',
140 'selector-css3',
141 'dom-style-ie',
143 // Some extras we use everywhere.
144 'escape',
146 'attribute-core',
147 'event-custom-complex',
148 'base-core',
149 'attribute-base',
150 'attribute-extras',
151 'attribute-observable',
152 'base-observable',
153 'base-base',
154 'base-pluginhost',
155 'base-build',
156 'event-synthetic',
158 'attribute-complex',
159 'event-mouseenter',
160 'event-key',
161 'event-outside',
162 'event-autohide',
163 'event-focus',
164 'classnamemanager',
165 'widget-base',
166 'widget-htmlparser',
167 'widget-skin',
168 'widget-uievents',
169 'widget-stdmod',
170 'widget-position',
171 'widget-position-align',
172 'widget-stack',
173 'widget-position-constrain',
174 'overlay',
176 'widget-autohide',
177 'button-core',
178 'button-plugin',
179 'widget-buttons',
180 'widget-modality',
181 'panel',
182 'yui-throttle',
183 'dd-ddm-base',
184 'dd-drag',
185 'dd-plugin',
188 // We need to add these new parts to the beginning of the $parts list, not the end.
189 if ($type === 'js') {
190 $newparts = array();
191 foreach ($yuimodules as $module) {
192 $newparts[] = $revision . '/' . $module . '/' . $module . $filesuffix;
194 $newparts[] = 'yuiuseall/yuiuseall';
195 $parts = array_merge($newparts, $parts);
196 } else {
197 $newparts = array();
198 foreach ($yuimodules as $module) {
199 $candidate = $revision . '/' . $module . '/assets/skins/sam/' . $module . '.css';
200 if (!file_exists("$CFG->libdir/yuilib/$candidate")) {
201 continue;
203 $newparts[] = $candidate;
205 if ($newparts) {
206 $parts = array_merge($newparts, $parts);
211 // Handle the mcore rollup.
212 if (strpos($rollupname, 'mcore') !== false) {
213 $yuimodules = array(
214 'core/tooltip/tooltip',
215 'core/dock/dock-loader',
216 'core/notification/notification-dialogue',
219 $filesuffix = '.js';
220 if (isset($matches[1])) {
221 $filesuffix = '-min.js';
224 // We need to add these new parts to the beginning of the $parts list, not the end.
225 $newparts = array();
226 foreach ($yuimodules as $module) {
227 $newparts[] = 'm/' . $revision . '/' . $module . $filesuffix;
229 $parts = array_merge($newparts, $parts);
231 continue;
233 if ($version === 'm') {
234 $version = 'moodle';
236 if ($version === 'moodle') {
237 if (count($bits) <= 3) {
238 // This is an invalid module load attempt.
239 $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
240 continue;
242 $revision = (int)array_shift($bits);
243 if ($revision === -1) {
244 // Revision -1 says please don't cache the JS
245 $cache = false;
247 $frankenstyle = array_shift($bits);
248 $filename = array_pop($bits);
249 $modulename = $bits[0];
250 $dir = core_component::get_component_directory($frankenstyle);
252 // For shifted YUI modules, we need the YUI module name in frankenstyle format.
253 $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
254 $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
256 // Submodules are stored in a directory with the full submodule name.
257 // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
258 $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js', '.css'), '', $frankenstylefilename);
260 // By default, try and use the /yui/build directory.
261 $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
262 if ($mimetype == 'text/css') {
263 // CSS assets are in a slightly different place to the JS.
264 $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
266 // Add the path to the bits to handle fallback for non-shifted assets.
267 $bits[] = 'assets';
268 $bits[] = 'skins';
269 $bits[] = 'sam';
270 } else {
271 $contentfile = $contentfile . '/' . $frankenstylefilename;
274 // If the shifted versions don't exist, fall back to the non-shifted file.
275 if (!file_exists($contentfile) or !is_file($contentfile)) {
276 // We have to revert to the non-minified and non-debug versions.
277 $filename = preg_replace('/-(min|debug)\./', '.', $filename);
278 $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
280 } else if ($version === '2in3') {
281 $contentfile = "$CFG->libdir/yuilib/$part";
283 } else if ($version == 'gallery') {
284 $contentfile = "$CFG->libdir/yui/$part";
286 } else if ($version == 'yuiuseall') {
287 // Create global Y that is available in global scope,
288 // this is the trick behind original SimpleYUI.
289 $filecontent = "var Y = YUI().use('*');";
291 } else {
292 if ($version != $CFG->yui3version) {
293 $content .= "\n// Wrong yui version $part!\n";
294 continue;
296 $contentfile = "$CFG->libdir/yuilib/$part";
298 if (!file_exists($contentfile) or !is_file($contentfile)) {
299 $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
300 $content .= "\n// Combo resource $part ($location) not found!\n";
301 continue;
304 if (empty($filecontent)) {
305 $filecontent = file_get_contents($contentfile);
307 $fmodified = filemtime($contentfile);
308 if ($fmodified > $lastmodified) {
309 $lastmodified = $fmodified;
312 $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
313 $sep = ($slasharguments ? '/' : '?file=');
315 if ($mimetype === 'text/css') {
316 if ($version == 'moodle') {
317 // Search for all images in the file and replace with an appropriate link to the yui_image.php script
318 $imagebits = array(
319 $sep . $version,
320 $frankenstyle,
321 $modulename,
322 array_shift($bits),
323 '$1.$2'
326 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
327 } else if ($version == '2in3') {
328 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
329 // I've added this as a separate regex so it can be easily removed once
330 // YUI standardise there CSS methods
331 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
333 // search for all images in yui2 CSS and serve them through the yui_image.php script
334 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
336 } else if ($version == 'gallery') {
337 // search for all images in gallery module CSS and serve them through the yui_image.php script
338 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
340 } else {
341 // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
342 // I've added this as a separate regex so it can be easily removed once
343 // YUI standardise there CSS methods
344 $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
346 // search for all images in yui2 CSS and serve them through the yui_image.php script
347 $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
351 $content .= $filecontent;
354 if ($lastmodified == 0) {
355 $lastmodified = time();
358 if ($cache) {
359 combo_send_cached($content, $mimetype, $etag, $lastmodified);
360 } else {
361 combo_send_uncached($content, $mimetype);
366 * Send the JavaScript cached
367 * @param string $content
368 * @param string $mimetype
369 * @param string $etag
370 * @param int $lastmodified
372 function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
373 $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
375 header('Content-Disposition: inline; filename="combo"');
376 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
377 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
378 header('Pragma: ');
379 header('Cache-Control: public, max-age='.$lifetime);
380 header('Accept-Ranges: none');
381 header('Content-Type: '.$mimetype);
382 header('Etag: "'.$etag.'"');
383 if (!min_enable_zlib_compression()) {
384 header('Content-Length: '.strlen($content));
387 echo $content;
388 die;
392 * Send the JavaScript uncached
393 * @param string $content
394 * @param string $mimetype
396 function combo_send_uncached($content, $mimetype) {
397 header('Content-Disposition: inline; filename="combo"');
398 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
399 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
400 header('Pragma: ');
401 header('Accept-Ranges: none');
402 header('Content-Type: '.$mimetype);
403 if (!min_enable_zlib_compression()) {
404 header('Content-Length: '.strlen($content));
407 echo $content;
408 die;
411 function combo_not_found($message = '') {
412 header('HTTP/1.0 404 not found');
413 if ($message) {
414 echo $message;
415 } else {
416 echo 'Combo resource not found, sorry.';
418 die;
421 function combo_params() {
422 if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
423 // url rewriting
424 $slashargument = substr($_SERVER['QUERY_STRING'], 6);
425 return array($slashargument, true);
427 } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
428 $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
429 return array($parts[1], false);
431 } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
432 // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
433 return array($_SERVER['QUERY_STRING'], false);
435 } else if ($slashargument = min_get_slash_argument()) {
436 $slashargument = ltrim($slashargument, '/');
437 return array($slashargument, true);
439 } else {
440 // unsupported server, sorry!
441 combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');