MDL-21124 towards full JS support in themes

This commit is contained in:
Petr Skoda 2009-12-20 23:06:57 +00:00
parent 38aafea249
commit e68c5f36cf
3 changed files with 108 additions and 17 deletions

View file

@ -23,4 +23,85 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
//TODO
// 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
$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
$rev = min_optional_param('rev', 0, 'INT');
if (!file_exists("$CFG->dirroot/theme/$themename/config.php") and !file_exists("$CFG->dataroot/theme/$themename/config.php")) {
header('HTTP/1.0 404 not found');
die('Theme was not found, sorry.');
}
$candidate = "$CFG->dataroot/cache/theme/$themename/javascript.js";
if ($rev > -1 and file_exists($candidate)) {
if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
// we do not actually need to verify the etag value because our files
// never change in cache because we increment the rev parameter
header('HTTP/1.1 304 Not Modified');
die;
}
send_cached_js($candidate, $rev);
}
//=================================================================================
// ok, now we need to start normal moodle script, we need to load all libs and $DB
define('ABORT_AFTER_CONFIG_CANCEL', true);
define('NO_MOODLE_COOKIES', true); // Session not used here
define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
require("$CFG->dirroot/lib/setup.php");
$theme = theme_config::load($themename);
$js = $theme->javascript_content();
if ($rev > -1) {
check_dir_exists(dirname($candidate), true, true);
$fp = fopen($candidate, 'w');
fwrite($fp, $js);
fclose($fp);
send_cached_js($candidate);
} else {
send_uncached_js($js);
}
//=================================================================================
//=== utility functions ==
// we are not using filelib because we need to fine tune all header
// parameters to get the best performance.
function send_cached_js($jspath) {
$lifetime = 60*60*24*20;
header('Content-Disposition: inline; filename="javascripts.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: application/x-javascript');
header('Content-Length: '.filesize($jspath));
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
readfile($jspath);
die;
}
function send_uncached_js($js) {
header('Content-Disposition: inline; filename="javascripts.php"');
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: application/x-javascript');
header('Content-Length: '.strlen($js));
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
echo $js;
die;
}