]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-21189 custom yui image loading needed for combo yui loader
authorPetr Skoda <skodak@moodle.org>
Fri, 25 Dec 2009 19:51:40 +0000 (19:51 +0000)
committerPetr Skoda <skodak@moodle.org>
Fri, 25 Dec 2009 19:51:40 +0000 (19:51 +0000)
lib/ajax/ajaxlib.php
lib/configonlylib.php
lib/outputlib.php
lib/setup.php
theme/image.php
theme/yui_combo.php [new file with mode: 0644]
theme/yui_image.php [new file with mode: 0644]

index 9f6de99d4b3a390a9d10a7eed7272f451047d659..0332d712d8ac1e170836a5b7374b82e006a83dac 100644 (file)
@@ -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=';
     }
 
     /**
index c07f13e58fb0cb2b004d7eaeacac3a86b5b5d4ae..d90dbaab98a00a93fbf7698df391574781ff9a4f 100644 (file)
@@ -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;
 }
 
index 3c0fcb70bf93f4022e5410a61aa9f4920bafff77..3352595829dd435746495bf13b8a775f87e3ec5c 100644 (file)
@@ -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;
index 0aa0212883536139813ff8e7508437f33bebf70d..6239a189239da82c59cbfc53e0d12a51107fdcbf 100644 (file)
  * @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
index 28ccd214da3dc9ea8acf03b071f395ed089692a0..84f21986999b4e018b83602e1fd8ce7b1dad3700 100644 (file)
@@ -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 (file)
index 0000000..b3d9bbc
--- /dev/null
@@ -0,0 +1 @@
+<?php
diff --git a/theme/yui_image.php b/theme/yui_image.php
new file mode 100644 (file)
index 0000000..b92a010
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+
+// 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
+
+$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.');
+}