yui_combo.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file is responsible for serving of yui Javascript and CSS
  18. *
  19. * @package core
  20. * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. // disable moodle specific debug messages and any errors in output,
  24. // comment out when debugging or better look into error log!
  25. define('NO_DEBUG_DISPLAY', true);
  26. // we need just the values from config.php and minlib.php
  27. define('ABORT_AFTER_CONFIG', true);
  28. require('../config.php'); // this stops immediately at the beginning of lib/setup.php
  29. // get special url parameters
  30. list($parts, $slasharguments) = combo_params();
  31. if (!$parts) {
  32. combo_not_found();
  33. }
  34. $etag = sha1($parts);
  35. $parts = trim($parts, '&');
  36. // find out what we are serving - only one type per request
  37. $content = '';
  38. if (substr($parts, -3) === '.js') {
  39. $mimetype = 'application/javascript';
  40. } else if (substr($parts, -4) === '.css') {
  41. $mimetype = 'text/css';
  42. } else {
  43. combo_not_found();
  44. }
  45. // if they are requesting a revision that's not -1, and they have supplied an
  46. // If-Modified-Since header, we can send back a 304 Not Modified since the
  47. // content never changes (the rev number is increased any time the content changes)
  48. if (strpos($parts, '/-1/') === false and (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))) {
  49. $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
  50. header('HTTP/1.1 304 Not Modified');
  51. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
  52. header('Cache-Control: public, max-age='.$lifetime);
  53. header('Content-Type: '.$mimetype);
  54. header('Etag: "'.$etag.'"');
  55. die;
  56. }
  57. $parts = explode('&', $parts);
  58. $cache = true;
  59. $lastmodified = 0;
  60. while (count($parts)) {
  61. $part = array_shift($parts);
  62. if (empty($part)) {
  63. continue;
  64. }
  65. $filecontent = '';
  66. $part = min_clean_param($part, 'SAFEPATH');
  67. $bits = explode('/', $part);
  68. if (count($bits) < 2) {
  69. $content .= "\n// Wrong combo resource $part!\n";
  70. continue;
  71. }
  72. $version = array_shift($bits);
  73. if ($version === 'rollup') {
  74. $yuipatchedversion = explode('_', array_shift($bits));
  75. $revision = $yuipatchedversion[0];
  76. $rollupname = array_shift($bits);
  77. if (strpos($rollupname, 'yui-moodlesimple') !== false) {
  78. if (substr($rollupname, -3) === '.js') {
  79. // Determine which version of this rollup should be used.
  80. $filesuffix = '.js';
  81. preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
  82. if (isset($matches[1])) {
  83. $filesuffix = $matches[0];
  84. }
  85. $type = 'js';
  86. } else if (substr($rollupname, -4) === '.css') {
  87. $type = 'css';
  88. } else {
  89. continue;
  90. }
  91. // Allow support for revisions on YUI between official releases.
  92. // We can just discard the subrevision since it is only used to invalidate the browser cache.
  93. $yuipatchedversion = explode('_', $revision);
  94. $yuiversion = $yuipatchedversion[0];
  95. $yuimodules = array(
  96. 'yui',
  97. 'oop',
  98. 'event-custom-base',
  99. 'dom-core',
  100. 'dom-base',
  101. 'color-base',
  102. 'dom-style',
  103. 'selector-native',
  104. 'selector',
  105. 'node-core',
  106. 'node-base',
  107. 'event-base',
  108. 'event-base-ie',
  109. 'pluginhost-base',
  110. 'pluginhost-config',
  111. 'event-delegate',
  112. 'node-event-delegate',
  113. 'node-pluginhost',
  114. 'dom-screen',
  115. 'node-screen',
  116. 'node-style',
  117. 'querystring-stringify-simple',
  118. 'io-base',
  119. 'json-parse',
  120. 'transition',
  121. 'selector-css2',
  122. 'selector-css3',
  123. 'dom-style-ie',
  124. // Some extras we use everywhere.
  125. 'escape',
  126. 'attribute-core',
  127. 'event-custom-complex',
  128. 'base-core',
  129. 'attribute-base',
  130. 'attribute-extras',
  131. 'attribute-observable',
  132. 'base-observable',
  133. 'base-base',
  134. 'base-pluginhost',
  135. 'base-build',
  136. 'event-synthetic',
  137. 'attribute-complex',
  138. 'event-mouseenter',
  139. 'event-key',
  140. 'event-outside',
  141. 'event-focus',
  142. 'classnamemanager',
  143. 'widget-base',
  144. 'widget-htmlparser',
  145. 'widget-skin',
  146. 'widget-uievents',
  147. 'widget-stdmod',
  148. 'widget-position',
  149. 'widget-position-align',
  150. 'widget-stack',
  151. 'widget-position-constrain',
  152. 'overlay',
  153. 'widget-autohide',
  154. 'button-core',
  155. 'button-plugin',
  156. 'widget-buttons',
  157. 'widget-modality',
  158. 'panel',
  159. 'yui-throttle',
  160. 'dd-ddm-base',
  161. 'dd-drag',
  162. 'dd-plugin',
  163. // Cache is used by moodle-core-tooltip which we include everywhere.
  164. 'cache-base',
  165. );
  166. // We need to add these new parts to the beginning of the $parts list, not the end.
  167. if ($type === 'js') {
  168. $newparts = array();
  169. foreach ($yuimodules as $module) {
  170. $newparts[] = $yuiversion . '/' . $module . '/' . $module . $filesuffix;
  171. }
  172. $newparts[] = 'yuiuseall/yuiuseall';
  173. $parts = array_merge($newparts, $parts);
  174. } else {
  175. $newparts = array();
  176. foreach ($yuimodules as $module) {
  177. $candidate = $yuiversion . '/' . $module . '/assets/skins/sam/' . $module . '.css';
  178. if (!file_exists("$CFG->libdir/yuilib/$candidate")) {
  179. continue;
  180. }
  181. $newparts[] = $candidate;
  182. }
  183. if ($newparts) {
  184. $parts = array_merge($newparts, $parts);
  185. }
  186. }
  187. }
  188. // Handle the mcore rollup.
  189. if (strpos($rollupname, 'mcore') !== false) {
  190. $yuimodules = array(
  191. 'core/tooltip/tooltip',
  192. 'core/popuphelp/popuphelp',
  193. 'core/widget-focusafterclose/widget-focusafterclose',
  194. 'core/dock/dock-loader',
  195. 'core/notification/notification-dialogue',
  196. );
  197. // Determine which version of this rollup should be used.
  198. $filesuffix = '.js';
  199. preg_match('/(-(debug|min))?\.js/', $rollupname, $matches);
  200. if (isset($matches[1])) {
  201. $filesuffix = $matches[0];
  202. }
  203. // We need to add these new parts to the beginning of the $parts list, not the end.
  204. $newparts = array();
  205. foreach ($yuimodules as $module) {
  206. $newparts[] = 'm/' . $revision . '/' . $module . $filesuffix;
  207. }
  208. $parts = array_merge($newparts, $parts);
  209. }
  210. continue;
  211. }
  212. if ($version === 'm') {
  213. $version = 'moodle';
  214. }
  215. if ($version === 'moodle') {
  216. if (count($bits) <= 3) {
  217. // This is an invalid module load attempt.
  218. $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
  219. continue;
  220. }
  221. $revision = (int)array_shift($bits);
  222. if ($revision === -1) {
  223. // Revision -1 says please don't cache the JS
  224. $cache = false;
  225. }
  226. $frankenstyle = array_shift($bits);
  227. $filename = array_pop($bits);
  228. $modulename = $bits[0];
  229. $dir = core_component::get_component_directory($frankenstyle);
  230. // For shifted YUI modules, we need the YUI module name in frankenstyle format.
  231. $frankenstylemodulename = join('-', array($version, $frankenstyle, $modulename));
  232. $frankenstylefilename = preg_replace('/' . $modulename . '/', $frankenstylemodulename, $filename);
  233. // Submodules are stored in a directory with the full submodule name.
  234. // We need to remove the -debug.js, -min.js, and .js from the file name to calculate that directory name.
  235. $frankenstyledirectoryname = str_replace(array('-min.js', '-debug.js', '.js', '.css'), '', $frankenstylefilename);
  236. // By default, try and use the /yui/build directory.
  237. $contentfile = $dir . '/yui/build/' . $frankenstyledirectoryname;
  238. if ($mimetype == 'text/css') {
  239. // CSS assets are in a slightly different place to the JS.
  240. $contentfile = $contentfile . '/assets/skins/sam/' . $frankenstylefilename;
  241. // Add the path to the bits to handle fallback for non-shifted assets.
  242. $bits[] = 'assets';
  243. $bits[] = 'skins';
  244. $bits[] = 'sam';
  245. } else {
  246. $contentfile = $contentfile . '/' . $frankenstylefilename;
  247. }
  248. // If the shifted versions don't exist, fall back to the non-shifted file.
  249. if (!file_exists($contentfile) or !is_file($contentfile)) {
  250. // We have to revert to the non-minified and non-debug versions.
  251. $filename = preg_replace('/-(min|debug)\./', '.', $filename);
  252. $contentfile = $dir . '/yui/' . join('/', $bits) . '/' . $filename;
  253. }
  254. } else if ($version === '2in3') {
  255. $contentfile = "$CFG->libdir/yuilib/$part";
  256. } else if ($version == 'gallery') {
  257. if (count($bits) <= 2) {
  258. // This is an invalid module load attempt.
  259. $content .= "\n// Incorrect moodle module inclusion. Not enough component information in {$part}.\n";
  260. continue;
  261. }
  262. $revision = (int)array_shift($bits);
  263. if ($revision === -1) {
  264. // Revision -1 says please don't cache the JS
  265. $cache = false;
  266. }
  267. $contentfile = "$CFG->libdir/yuilib/gallery/" . join('/', $bits);
  268. } else if ($version == 'yuiuseall') {
  269. // Create global Y that is available in global scope,
  270. // this is the trick behind original SimpleYUI.
  271. $filecontent = "var Y = YUI().use('*');";
  272. } else {
  273. // Allow support for revisions on YUI between official releases.
  274. // We can just discard the subrevision since it is only used to invalidate the browser cache.
  275. $yuipatchedversion = explode('_', $version);
  276. $yuiversion = $yuipatchedversion[0];
  277. if ($yuiversion != $CFG->yui3version) {
  278. $content .= "\n// Wrong yui version $part!\n";
  279. continue;
  280. }
  281. $newpart = explode('/', $part);
  282. $newpart[0] = $yuiversion;
  283. $part = implode('/', $newpart);
  284. $contentfile = "$CFG->libdir/yuilib/$part";
  285. }
  286. if (!file_exists($contentfile) or !is_file($contentfile)) {
  287. $location = '$CFG->dirroot'.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $contentfile);
  288. $content .= "\n// Combo resource $part ($location) not found!\n";
  289. continue;
  290. }
  291. if (empty($filecontent)) {
  292. $filecontent = file_get_contents($contentfile);
  293. }
  294. $fmodified = filemtime($contentfile);
  295. if ($fmodified > $lastmodified) {
  296. $lastmodified = $fmodified;
  297. }
  298. $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
  299. $sep = ($slasharguments ? '/' : '?file=');
  300. if ($mimetype === 'text/css') {
  301. if ($version == 'moodle') {
  302. // Search for all images in the file and replace with an appropriate link to the yui_image.php script
  303. $imagebits = array(
  304. $sep . $version,
  305. $frankenstyle,
  306. $modulename,
  307. array_shift($bits),
  308. '$1.$2'
  309. );
  310. $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot . '/theme/yui_image.php' . implode('/', $imagebits), $filecontent);
  311. } else if ($version == '2in3') {
  312. // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
  313. // I've added this as a separate regex so it can be easily removed once
  314. // YUI standardise there CSS methods
  315. $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
  316. // search for all images in yui2 CSS and serve them through the yui_image.php script
  317. $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$CFG->yui2version.'/$1.$2', $filecontent);
  318. } else if ($version == 'gallery') {
  319. // Replace any references to the CDN with a relative link.
  320. $filecontent = preg_replace('#(' . preg_quote('http://yui.yahooapis.com/') . '(gallery-[^/]*/))#', '../../../../', $filecontent);
  321. // Replace all relative image links with the a link to yui_image.php.
  322. $filecontent = preg_replace('#(' . preg_quote('../../../../') . ')(gallery-[^/]*/assets/skins/sam/[a-z0-9_-]+)\.(png|gif)#',
  323. $relroot . '/theme/yui_image.php' . $sep . '/gallery/' . $revision . '/$2.$3', $filecontent);
  324. } else {
  325. // First we need to remove relative paths to images. These are used by YUI modules to make use of global assets.
  326. // I've added this as a separate regex so it can be easily removed once
  327. // YUI standardise there CSS methods
  328. $filecontent = preg_replace('#(\.\./\.\./\.\./\.\./assets/skins/sam/)?([a-z0-9_-]+)\.(png|gif)#', '$2.$3', $filecontent);
  329. // search for all images in yui2 CSS and serve them through the yui_image.php script
  330. $filecontent = preg_replace('/([a-z0-9_-]+)\.(png|gif)/', $relroot.'/theme/yui_image.php'.$sep.$version.'/$1.$2', $filecontent);
  331. }
  332. }
  333. $content .= $filecontent;
  334. }
  335. if ($lastmodified == 0) {
  336. $lastmodified = time();
  337. }
  338. if ($cache) {
  339. combo_send_cached($content, $mimetype, $etag, $lastmodified);
  340. } else {
  341. combo_send_uncached($content, $mimetype);
  342. }
  343. /**
  344. * Send the JavaScript cached
  345. * @param string $content
  346. * @param string $mimetype
  347. * @param string $etag
  348. * @param int $lastmodified
  349. */
  350. function combo_send_cached($content, $mimetype, $etag, $lastmodified) {
  351. $lifetime = 60*60*24*360; // 1 year, we do not change YUI versions often, there are a few custom yui modules
  352. header('Content-Disposition: inline; filename="combo"');
  353. header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
  354. header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
  355. header('Pragma: ');
  356. header('Cache-Control: public, max-age='.$lifetime);
  357. header('Accept-Ranges: none');
  358. header('Content-Type: '.$mimetype);
  359. header('Etag: "'.$etag.'"');
  360. if (!min_enable_zlib_compression()) {
  361. header('Content-Length: '.strlen($content));
  362. }
  363. echo $content;
  364. die;
  365. }
  366. /**
  367. * Send the JavaScript uncached
  368. * @param string $content
  369. * @param string $mimetype
  370. */
  371. function combo_send_uncached($content, $mimetype) {
  372. header('Content-Disposition: inline; filename="combo"');
  373. header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
  374. header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
  375. header('Pragma: ');
  376. header('Accept-Ranges: none');
  377. header('Content-Type: '.$mimetype);
  378. if (!min_enable_zlib_compression()) {
  379. header('Content-Length: '.strlen($content));
  380. }
  381. echo $content;
  382. die;
  383. }
  384. function combo_not_found($message = '') {
  385. header('HTTP/1.0 404 not found');
  386. if ($message) {
  387. echo $message;
  388. } else {
  389. echo 'Combo resource not found, sorry.';
  390. }
  391. die;
  392. }
  393. function combo_params() {
  394. if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], 'file=/') === 0) {
  395. // url rewriting
  396. $slashargument = substr($_SERVER['QUERY_STRING'], 6);
  397. return array($slashargument, true);
  398. } else if (isset($_SERVER['REQUEST_URI']) and strpos($_SERVER['REQUEST_URI'], '?') !== false) {
  399. $parts = explode('?', $_SERVER['REQUEST_URI'], 2);
  400. return array($parts[1], false);
  401. } else if (isset($_SERVER['QUERY_STRING']) and strpos($_SERVER['QUERY_STRING'], '?') !== false) {
  402. // note: buggy or misconfigured IIS does return the query string in REQUEST_URI
  403. return array($_SERVER['QUERY_STRING'], false);
  404. } else if ($slashargument = min_get_slash_argument(false)) {
  405. $slashargument = ltrim($slashargument, '/');
  406. return array($slashargument, true);
  407. } else {
  408. // unsupported server, sorry!
  409. combo_not_found('Unsupported server - query string can not be determined, try disabling YUI combo loading in admin settings.');
  410. }
  411. }