|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 // disable moodle specific debug messages and any errors in output, 00028 // comment out when debugging or better look into error log! 00029 define('NO_DEBUG_DISPLAY', true); 00030 00031 // we need just the values from config.php and minlib.php 00032 define('ABORT_AFTER_CONFIG', true); 00033 require('../config.php'); // this stops immediately at the beginning of lib/setup.php 00034 00035 $themename = min_optional_param('theme', 'standard', 'SAFEDIR'); 00036 $rev = min_optional_param('rev', 0, 'INT'); 00037 $type = min_optional_param('type', 'head', 'RAW'); 00038 00039 if ($type !== 'head' and $type !== 'footer') { 00040 header('HTTP/1.0 404 not found'); 00041 die('Theme was not found, sorry.'); 00042 } 00043 00044 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) { 00045 // exists 00046 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) { 00047 // exists 00048 } else { 00049 header('HTTP/1.0 404 not found'); 00050 die('Theme was not found, sorry.'); 00051 } 00052 00053 $candidate = "$CFG->cachedir/theme/$themename/javascript_$type.js"; 00054 00055 if ($rev > -1 and file_exists($candidate)) { 00056 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { 00057 // we do not actually need to verify the etag value because our files 00058 // never change in cache because we increment the rev parameter 00059 $lifetime = 60*60*24*30; // 30 days 00060 header('HTTP/1.1 304 Not Modified'); 00061 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); 00062 header('Cache-Control: max-age='.$lifetime); 00063 header('Content-Type: application/javascript; charset=utf-8'); 00064 die; 00065 } 00066 send_cached_js($candidate, $rev); 00067 } 00068 00069 //================================================================================= 00070 // ok, now we need to start normal moodle script, we need to load all libs and $DB 00071 define('ABORT_AFTER_CONFIG_CANCEL', true); 00072 00073 define('NO_MOODLE_COOKIES', true); // Session not used here 00074 define('NO_UPGRADE_CHECK', true); // Ignore upgrade check 00075 00076 require("$CFG->dirroot/lib/setup.php"); 00077 // setup include path 00078 set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path()); 00079 require_once('Minify.php'); 00080 00081 $theme = theme_config::load($themename); 00082 00083 if ($rev > -1) { 00084 // note: cache reset might have purged our cache dir structure, 00085 // make sure we do not use stale file stat cache in the next check_dir_exists() 00086 clearstatcache(); 00087 check_dir_exists(dirname($candidate)); 00088 $fp = fopen($candidate, 'w'); 00089 fwrite($fp, minify($theme->javascript_files($type))); 00090 fclose($fp); 00091 send_cached_js($candidate); 00092 } else { 00093 send_uncached_js($theme->javascript_content($type)); 00094 } 00095 00096 //================================================================================= 00097 //=== utility functions == 00098 // we are not using filelib because we need to fine tune all header 00099 // parameters to get the best performance. 00100 00101 function send_cached_js($jspath) { 00102 $lifetime = 60*60*24*30; // 30 days 00103 00104 header('Content-Disposition: inline; filename="javascript.php"'); 00105 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT'); 00106 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); 00107 header('Pragma: '); 00108 header('Cache-Control: max-age='.$lifetime); 00109 header('Accept-Ranges: none'); 00110 header('Content-Type: application/javascript; charset=utf-8'); 00111 if (!min_enable_zlib_compression()) { 00112 header('Content-Length: '.filesize($jspath)); 00113 } 00114 00115 readfile($jspath); 00116 die; 00117 } 00118 00119 function send_uncached_js($js) { 00120 header('Content-Disposition: inline; filename="javascript.php"'); 00121 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT'); 00122 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT'); 00123 header('Pragma: '); 00124 header('Accept-Ranges: none'); 00125 header('Content-Type: application/javascript; charset=utf-8'); 00126 header('Content-Length: '.strlen($js)); 00127 00128 echo $js; 00129 die; 00130 } 00131 00132 function minify($files) { 00133 if (0 === stripos(PHP_OS, 'win')) { 00134 Minify::setDocRoot(); // IIS may need help 00135 } 00136 // disable all caching, we do it in moodle 00137 Minify::setCache(null, false); 00138 00139 $options = array( 00140 'bubbleCssImports' => false, 00141 // Don't gzip content we just want text for storage 00142 'encodeOutput' => false, 00143 // Maximum age to cache, not used but required 00144 'maxAge' => 1800, 00145 // The files to minify 00146 'files' => $files, 00147 // Turn orr URI rewriting 00148 'rewriteCssUris' => false, 00149 // This returns the CSS rather than echoing it for display 00150 'quiet' => true 00151 ); 00152 00153 $result = Minify::serve('Files', $options); 00154 return $result['content']; 00155 }