From 60f2c8666a4441369c98e3c88dbf48b7865f3b6d Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Fri, 25 Dec 2009 19:51:40 +0000 Subject: [PATCH] MDL-21189 custom yui image loading needed for combo yui loader --- lib/ajax/ajaxlib.php | 6 +-- lib/configonlylib.php | 14 +++++-- lib/outputlib.php | 11 +----- lib/setup.php | 30 +++++++-------- theme/image.php | 2 +- theme/yui_combo.php | 1 + theme/yui_image.php | 85 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 118 insertions(+), 31 deletions(-) create mode 100644 theme/yui_combo.php create mode 100644 theme/yui_image.php diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index 9f6de99d4b3..0332d712d8a 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -141,9 +141,9 @@ class page_requirements_manager { $this->yui2loader->base = $CFG->httpswwwroot . '/lib/yui/'. $CFG->yui2version . '/'; } - // This file helps to minimise number of http requests - //$this->yui3loader->comboBase = $CFG->httpswwwroot . '/theme/yuicomboloader.php?'; - //$this->yui2loader->comboBase = $CFG->httpswwwroot . '/theme/yuicomboloader.php?'; + // This file helps to minimise number of http requests and implements proper caching + //$this->yui3loader->comboBase = $CFG->httpswwwroot . '/theme/yui_combo.php?file='; + //$this->yui2loader->comboBase = $CFG->httpswwwroot . '/theme/yui_combo.php?file='; } /** diff --git a/lib/configonlylib.php b/lib/configonlylib.php index c07f13e58fb..d90dbaab98a 100644 --- a/lib/configonlylib.php +++ b/lib/configonlylib.php @@ -44,12 +44,20 @@ function min_optional_param($name, $default, $type) { // very, very, very ugly hack, unforunately $OUTPUT->pix_url() is not used properly in javascript code :-( $value = $_GET['amp;'.$name]; } + switch($type) { - case 'INT': $value = (int)$value; break; - case 'SAFEDIR': $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); break; - case 'SAFEPATH': $value = preg_replace('/[^a-zA-Z0-9\/_-]/', '', $value); break; + case 'RAW': break; + case 'INT': $value = (int)$value; + break; + case 'SAFEDIR': $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); + break; + case 'SAFEPATH': $value = preg_replace('/[^a-zA-Z0-9\/\._-]/', '', $value); + $value = preg_replace('/\.+/', '.', $value); + $value = preg_replace('#/+#', '/', $value); + break; default: die("Coding error: incorrent parameter type specified ($type)."); } + return $value; } diff --git a/lib/outputlib.php b/lib/outputlib.php index 3c0fcb70bf9..3352595829d 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -632,7 +632,8 @@ class theme_config { unset($item); unset($items); - $yui2_sheets = preg_replace('/([a-z-]+)\.(png|gif)/', '[[pix:yui2|$1]]', $yui2_sheets); + // search for all images in yui2 CSS and serve them through the yui_image.php script + $yui2_sheets = preg_replace('/([a-z-]+)\.(png|gif)/', 'yui_image.php?file='.$CFG->yui2version.'/$1.$2', $yui2_sheets); $css['yui2'][] = $this->post_process($yui2_sheets); // get all plugin sheets @@ -877,14 +878,6 @@ class theme_config { } return null; - } else if ($component === 'yui2') { - // YUI2 is here for legacy reasons only, YUI3 images are not handled here - // yui images need to be overriden with CSS using proper selectors - if ($imagefile = $this->image_exists("$CFG->libdir/yui/$CFG->yui2version/assets/skins/sam/$image")) { - return $imagefile; - } - return null; - } else { if (strpos($component, '_') === false) { $component = 'mod_'.$component; diff --git a/lib/setup.php b/lib/setup.php index 0aa02128835..6239a189239 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -26,14 +26,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -// special support for highly optimised scripts that do not need libraries and DB connection -if (defined('ABORT_AFTER_CONFIG')) { - if (!defined('ABORT_AFTER_CONFIG_CANCEL')) { - require_once("$CFG->dirroot/lib/configonlylib.php"); - return; - } -} - /** * Holds the core settings that affect how Moodle works. Some of its fields * are set in config.php, and the rest are loaded from the config table. @@ -48,6 +40,21 @@ if (defined('ABORT_AFTER_CONFIG')) { */ global $CFG; +// Set up some paths. +$CFG->libdir = $CFG->dirroot .'/lib'; + +// exact version of currently used yui2 and 3 library +$CFG->yui2version = '2.8.0r4'; +$CFG->yui3version = '3.0.0'; + +// special support for highly optimised scripts that do not need libraries and DB connection +if (defined('ABORT_AFTER_CONFIG')) { + if (!defined('ABORT_AFTER_CONFIG_CANCEL')) { + require_once("$CFG->dirroot/lib/configonlylib.php"); + return; + } +} + /** * Database connection. Used for all access to the database. * @global moodle_database $DB @@ -201,9 +208,6 @@ if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) { // Store settings from config.php in array in $CFG - we can use it later to detect problems and overrides $CFG->config_php_settings = (array)$CFG; -// Set up some paths. -$CFG->libdir = $CFG->dirroot .'/lib'; - // Set httpswwwroot default value (this variable will replace $CFG->wwwroot // inside some URLs used in HTTPSPAGEREQUIRED pages. $CFG->httpswwwroot = $CFG->wwwroot; @@ -239,10 +243,6 @@ if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php $CFG->prefix = ''; } -// exact version of currently used yui2 and 3 library -$CFG->yui2version = '2.8.0r4'; -$CFG->yui3version = '3.0.0'; - // Load up standard libraries require_once($CFG->libdir .'/textlib.class.php'); // Functions to handle multibyte strings require_once($CFG->libdir .'/filterlib.php'); // Functions for filtering test as it is output diff --git a/theme/image.php b/theme/image.php index 28ccd214da3..84f21986999 100644 --- a/theme/image.php +++ b/theme/image.php @@ -121,7 +121,7 @@ if ($rev > -1) { // parameters to get the best performance. function send_cached_image($imagepath, $rev) { - $lifetime = 60*60*24*3; + $lifetime = 60*60*24*3; // 3 days $pathinfo = pathinfo($imagepath); $imagename = $pathinfo['filename'].'.'.$pathinfo['extension']; diff --git a/theme/yui_combo.php b/theme/yui_combo.php new file mode 100644 index 00000000000..b3d9bbc7f37 --- /dev/null +++ b/theme/yui_combo.php @@ -0,0 +1 @@ +. + +/** + * 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 + +$path = min_optional_param('file', '', 'SAFEPATH'); + +$parts = explode('/', $path); +if (count($parts) != 2) { + yui_image_not_found(); +} +list($version, $image) = $parts; + +if ($version == $CFG->yui3version) { + $imagepath = "$CFG->dirroot/lib/yui/$CFG->yui3version/assets/skins/sam/$image"; +} else if ($version == $CFG->yui2version) { + $imagepath = "$CFG->dirroot/lib/yui/$CFG->yui2version/assets/skins/sam/$image"; +} else { + yui_image_not_found(); +} + +if (!file_exists($imagepath)) { + yui_image_not_found(); +} + +yui_image_cached($imagepath); + + + +function yui_image_cached($imagepath) { + $lifetime = 60*60*24*300; // 300 days === forever + $pathinfo = pathinfo($imagepath); + $imagename = $pathinfo['filename'].'.'.$pathinfo['extension']; + + switch($pathinfo['extension']) { + case 'gif' : $mimetype = 'image/gif'; break; + case 'png' : $mimetype = 'image/png'; break; + case 'jpg' : $mimetype = 'image/jpeg'; break; + case 'jpeg' : $mimetype = 'image/jpeg'; break; + case 'ico' : $mimetype = 'image/vnd.microsoft.icon'; break; + default: $mimetype = 'document/unknown'; + } + + header('Content-Disposition: inline; filename="'.$imagename.'"'); + header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($imagepath)) .' GMT'); + header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); + header('Pragma: '); + header('Accept-Ranges: none'); + header('Content-Type: '.$mimetype); + header('Content-Length: '.filesize($imagepath)); + + while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite + readfile($imagepath); + die; +} + +function yui_image_not_found() { + header('HTTP/1.0 404 not found'); + die('Image was not found, sorry.'); +}