2009-12-25 19:51:40 +00:00
|
|
|
<?php
|
2009-12-25 22:37:04 +00:00
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is responsible for serving of yui images
|
|
|
|
*
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// we need just the values from config.php and minlib.php
|
|
|
|
define('ABORT_AFTER_CONFIG', true);
|
|
|
|
require('../config.php'); // this stops immediately at the beginning of lib/setup.php
|
|
|
|
|
|
|
|
// get special url parameters
|
|
|
|
if (!$parts = combo_params()) {
|
|
|
|
combo_not_found();
|
|
|
|
}
|
|
|
|
|
2009-12-28 23:08:55 +00:00
|
|
|
$parts = trim($parts, '&');
|
|
|
|
|
2009-12-25 22:37:04 +00:00
|
|
|
// find out what we are serving - only one type per request
|
|
|
|
$content = '';
|
|
|
|
if (substr($parts, -3) === '.js') {
|
2010-07-07 09:10:10 +00:00
|
|
|
$mimetype = 'application/javascript';
|
2009-12-25 22:37:04 +00:00
|
|
|
} else if (substr($parts, -4) === '.css') {
|
|
|
|
$mimetype = 'text/css';
|
|
|
|
} else {
|
|
|
|
combo_not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = explode('&', $parts);
|
2010-08-05 04:05:28 +00:00
|
|
|
$cache = true;
|
2009-12-25 22:37:04 +00:00
|
|
|
|
|
|
|
foreach ($parts as $part) {
|
2009-12-28 23:08:55 +00:00
|
|
|
if (empty($part)) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-12-25 22:37:04 +00:00
|
|
|
$part = min_clean_param($part, 'SAFEPATH');
|
|
|
|
$bits = explode('/', $part);
|
|
|
|
if (count($bits) < 2) {
|
2009-12-29 12:38:50 +00:00
|
|
|
$content .= "\n// Wrong combo resource $part!\n";
|
|
|
|
continue;
|
2009-12-25 22:37:04 +00:00
|
|
|
}
|
2010-07-01 02:26:21 +00:00
|
|
|
//debug($bits);
|
|
|
|
$version = array_shift($bits);
|
|
|
|
if ($version == 'moodle') {
|
2010-07-25 13:35:05 +00:00
|
|
|
//TODO: this is a ugly hack because we should not load any libs here!
|
|
|
|
define('MOODLE_INTERNAL', true);
|
2010-07-02 05:39:59 +00:00
|
|
|
require_once($CFG->libdir.'/moodlelib.php');
|
2010-08-05 04:05:28 +00:00
|
|
|
$revision = (int)array_shift($bits);
|
|
|
|
if ($revision === -1) {
|
|
|
|
// Revision -1 says please don't cache the JS
|
|
|
|
$cache = false;
|
|
|
|
}
|
2010-07-01 02:26:21 +00:00
|
|
|
$frankenstyle = array_shift($bits);
|
2010-07-02 05:39:59 +00:00
|
|
|
$filename = array_pop($bits);
|
2010-07-01 02:26:21 +00:00
|
|
|
$dir = get_component_directory($frankenstyle);
|
2010-07-02 05:39:59 +00:00
|
|
|
if ($mimetype == 'text/css') {
|
|
|
|
$bits[] = 'assets';
|
|
|
|
$bits[] = 'skins';
|
|
|
|
$bits[] = 'sam';
|
|
|
|
}
|
|
|
|
$contentfile = $dir.'/yui/'.join('/', $bits).'/'.$filename;
|
2010-07-01 02:26:21 +00:00
|
|
|
} else {
|
|
|
|
if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
|
|
|
|
$content .= "\n// Wrong yui version $part!\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$contentfile = "$CFG->libdir/yui/$part";
|
2009-12-25 22:37:04 +00:00
|
|
|
}
|
|
|
|
if (!file_exists($contentfile) or !is_file($contentfile)) {
|
2009-12-29 12:38:50 +00:00
|
|
|
$content .= "\n// Combo resource $part not found!\n";
|
|
|
|
continue;
|
2009-12-25 22:37:04 +00:00
|
|
|
}
|
|
|
|
$filecontent = file_get_contents($contentfile);
|
|
|
|
|
|
|
|
if ($mimetype === 'text/css') {
|
2010-07-02 05:39:59 +00:00
|
|
|
if ($version == 'moodle') {
|
|
|
|
$filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$frankenstyle.'/'.array_shift($bits).'/$1.$2', $filecontent);
|
|
|
|
} else if ($version == 'gallery') {
|
2010-06-04 01:49:53 +00:00
|
|
|
// search for all images in gallery module CSS and serve them through the yui_image.php script
|
2010-07-01 02:26:21 +00:00
|
|
|
$filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
|
2010-06-04 01:49:53 +00:00
|
|
|
} else {
|
2010-10-07 08:25:56 +00:00
|
|
|
// First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
|
|
|
|
// I've added this as a separate regex so it can be easily removed once
|
|
|
|
// YUI standardise there CSS methods
|
|
|
|
$filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z_-]+)\.(png|gif)#', '$2.$3', $filecontent);
|
|
|
|
|
2010-06-04 01:49:53 +00:00
|
|
|
// search for all images in yui2 CSS and serve them through the yui_image.php script
|
|
|
|
$filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
|
|
|
|
}
|
2009-12-25 22:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$content .= $filecontent;
|
|
|
|
}
|
|
|
|
|
2010-08-05 04:05:28 +00:00
|
|
|
if ($cache) {
|
|
|
|
combo_send_cached($content, $mimetype);
|
|
|
|
} else {
|
|
|
|
combo_send_uncached($content, $mimetype);
|
|
|
|
}
|
2009-12-25 22:37:04 +00:00
|
|
|
|
|
|
|
|
2010-08-05 04:05:28 +00:00
|
|
|
/**
|
|
|
|
* Send the JavaScript cached
|
|
|
|
* @param string $content
|
|
|
|
* @param string $mimetype
|
|
|
|
*/
|
2009-12-25 22:37:04 +00:00
|
|
|
function combo_send_cached($content, $mimetype) {
|
|
|
|
$lifetime = 60*60*24*300; // 300 days === forever
|
|
|
|
|
|
|
|
header('Content-Disposition: inline; filename="combo"');
|
|
|
|
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
|
|
|
|
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
|
|
|
header('Pragma: ');
|
2009-12-28 23:08:55 +00:00
|
|
|
header('Cache-Control: max-age=315360000');
|
2009-12-25 22:37:04 +00:00
|
|
|
header('Accept-Ranges: none');
|
|
|
|
header('Content-Type: '.$mimetype);
|
2010-01-05 20:18:15 +00:00
|
|
|
if (!min_enable_zlib_compression()) {
|
|
|
|
header('Content-Length: '.strlen($content));
|
|
|
|
}
|
2009-12-25 22:37:04 +00:00
|
|
|
|
|
|
|
echo $content;
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2010-08-05 04:05:28 +00:00
|
|
|
/**
|
|
|
|
* Send the JavaScript uncached
|
|
|
|
* @param string $content
|
|
|
|
* @param string $mimetype
|
|
|
|
*/
|
|
|
|
function combo_send_uncached($content, $mimetype) {
|
|
|
|
header('Content-Disposition: inline; filename="combo"');
|
|
|
|
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
|
|
|
|
header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
|
|
|
|
header('Pragma: ');
|
|
|
|
header('Accept-Ranges: none');
|
|
|
|
header('Content-Type: '.$mimetype);
|
|
|
|
if (!min_enable_zlib_compression()) {
|
|
|
|
header('Content-Length: '.strlen($content));
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $content;
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2009-12-25 22:37:04 +00:00
|
|
|
function combo_not_found() {
|
|
|
|
header('HTTP/1.0 404 not found');
|
|
|
|
die('Combo resource not found, sorry.');
|
|
|
|
}
|
|
|
|
|
|
|
|
function combo_params() {
|
|
|
|
if (!empty($_SERVER['REQUEST_URI'])) {
|
|
|
|
$parts = explode('?', $_SERVER['REQUEST_URI']);
|
|
|
|
if (count($parts) != 2) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $parts[1];
|
|
|
|
|
|
|
|
} else if (!empty($_SERVER['QUERY_STRING'])) {
|
|
|
|
return $_SERVER['QUERY_STRING'];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|