} $jsfiles[] = $jsfile; } if (!$jsfiles) { // bad luck - no valid files die; } $etag = sha1($rev . implode(',', $jsfiles)); $candidate = $CFG->cachedir . '/js/' . $etag; if ($rev > -1) { if (file_exists($candidate)) { if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { // we do not actually need to verify the etag value because our files // never change in cache because we increment the rev parameter js_send_unmodified(filemtime($candidate), $etag); } js_send_cached($candidate, $etag); } else { js_write_cache_file_content($candidate, js_minify($jsfiles)); // verify nothing failed in cache file creation clearstatcache(); if (file_exists($candidate)) { js_send_cached($candidate, $etag); } } } $content = ''; foreach ($jsfiles as $jsfile) { $content .= file_get_contents($jsfile) . "\n"; } js_send_uncached($content, $etag);
/** * Compile a Javascript file. * * @param ID_TEXT Name of the JS file * @param PATH Full path to the JS file * @param boolean Whether to also do minification */ function js_compile($j, $js_cache_path, $minify = true) { require_lang('javascript'); global $KEEP_MARKERS, $SHOW_EDIT_LINKS; $temp_keep_markers = $KEEP_MARKERS; $temp_show_edit_links = $SHOW_EDIT_LINKS; $KEEP_MARKERS = false; $SHOW_EDIT_LINKS = false; $tpl_params = array(); if ($j == 'javascript_staff') { $url_patterns = array(); $cma_hooks = find_all_hooks('systems', 'content_meta_aware'); $award_hooks = find_all_hooks('systems', 'awards'); $common = array_intersect(array_keys($cma_hooks), array_keys($award_hooks)); foreach ($common as $hook) { require_code('hooks/systems/content_meta_aware/' . $hook); $hook_ob = object_factory('Hook_content_meta_aware_' . $hook); $info = $hook_ob->info(); list($zone, $attributes, ) = page_link_decode($info['view_pagelink_pattern']); $url = build_url($attributes, $zone, NULL, false, false, true); $url_patterns[$url->evaluate()] = array('PATTERN' => $url->evaluate(), 'HOOK' => $hook); list($zone, $attributes, ) = page_link_decode($info['edit_pagelink_pattern']); $url = build_url($attributes, $zone, NULL, false, false, true); $url_patterns[$url->evaluate()] = array('PATTERN' => $url->evaluate(), 'HOOK' => $hook); } $tpl_params['URL_PATTERNS'] = array_values($url_patterns); } $js = do_template(strtoupper($j), $tpl_params); $KEEP_MARKERS = $temp_keep_markers; $SHOW_EDIT_LINKS = $temp_show_edit_links; global $ATTACHED_MESSAGES_RAW; $num_msgs_before = count($ATTACHED_MESSAGES_RAW); $out = $js->evaluate(); $num_msgs_after = count($ATTACHED_MESSAGES_RAW); $success_status = $num_msgs_before == $num_msgs_after; if ($minify) { $out = js_minify($out); } if ($out == '') { $contents = $out; } else { $contents = '/* DO NOT EDIT. THIS IS A CACHE FILE AND WILL GET OVERWRITTEN RANDOMLY.' . chr(10) . 'INSTEAD EDIT THE TEMPLATE FROM WITHIN THE ADMIN ZONE, OR BY MANUALLY EDITING A TEMPLATES_CUSTOM OVERRIDE. */' . chr(10) . chr(10) . $out; } $js_file = @fopen($js_cache_path . '.tmp', 'wt'); if ($js_file === false) { intelligent_write_error($js_cache_path . '.tmp'); } if (fwrite($js_file, $contents) < strlen($contents)) { warn_exit(do_lang_tempcode('COULD_NOT_SAVE_FILE')); } fclose($js_file); fix_permissions($js_cache_path . '.tmp'); @rename($js_cache_path . '.tmp', $js_cache_path); sync_file($js_cache_path); if (!$success_status) { touch($js_cache_path, 0); // Fudge it so it's going to auto expire. We do have to write the file as it's referenced, but we want it to expire instantly so that any errors will reshow. } }
// If-Modified-Since requests so there is no need to handle them specially. header('Expires: ' . date('r', time() + 365 * 24 * 3600)); header('Cache-Control: max-age=' . 365 * 24 * 3600); // Pragma is set to no-cache by default so must be overridden. header('Pragma:'); } // Get the right MIME type. $mimetype = mimeinfo('type', $file); // For JS files, these can be minified and stored in cache. if ($mimetype === 'application/x-javascript' && $allowcache) { // The cached file is stored without version number etc. This is okay // because $CFG->cachedir is cleared each time there is a plugin update, // such as a new version of a tinymce plugin. // Flatten filename and include cache location. $cache = $CFG->cachedir . '/editor_tinymce/pluginjs'; $cachefile = $cache . '/' . $tinymceplugin . str_replace('/', '_', $innerpath); // If it doesn't exist, minify it and save to that location. if (!file_exists($cachefile)) { $content = js_minify(array($file)); js_write_cache_file_content($cachefile, $content); } $file = $cachefile; } else { if ($mimetype === 'text/html') { header('X-UA-Compatible: IE=edge'); } } // Serve file. header('Content-Length: ' . filesize($file)); header('Content-Type: ' . $mimetype); readfile($file);
// never change in cache because we increment the rev parameter js_send_unmodified(filemtime($candidate), $etag); } js_send_cached($candidate, $etag); } //================================================================================= // 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); $rev = theme_get_revision(); $etag = sha1("$themename/$rev/$type"); if ($rev > -1) { js_write_cache_file_content($candidate, js_minify($theme->javascript_files($type))); // verify nothing failed in cache file creation clearstatcache(); if (file_exists($candidate)) { js_send_cached($candidate, $etag); } } js_send_uncached($theme->javascript_content($type));