/** * Used to include JavaScript libraries. * * When the $lib parameter is given, the function will ensure that the * named library or libraries is loaded onto the page - either in the * HTML <head>, just after the header, or at an arbitrary later point in * the page, depending on where this function is called. * * Libraries will not be included more than once, so this works like * require_once in PHP. * * There are two special-case calls to this function from print_header which are * internal to weblib and use the second $extracthtml parameter: * $extracthtml = 1: this is used before printing the header. * It returns the script tag code that should go inside the <head>. * $extracthtml = 2: this is used after printing the header and handles any * require_js calls that occurred within the header itself. * * @param mixed $lib The library or libraries to load (a string or array of strings) * There are three way to specify the library: * 1. a shorname like 'yui_yahoo'. The list of recognised values is in lib/ajax/ajaxlib.php * 2. the path to the library relative to wwwroot, for example 'lib/javascript-static.js' * 3. (legacy) a full URL like $CFG->wwwroot . '/lib/javascript-static.js'. * @param int $extracthtml Private. For internal weblib use only. * @return mixed No return value (except when doing the internal $extracthtml * calls, when it returns html code). */ function require_js($lib, $extracthtml = 0) { global $CFG; static $loadlibs = array(); static $state = REQUIREJS_BEFOREHEADER; static $latecode = ''; if (!empty($lib)) { // Add the lib to the list of libs to be loaded, if it isn't already // in the list. if (is_array($lib)) { foreach ($lib as $singlelib) { require_js($singlelib); } } else { $libpath = ajax_get_lib($lib); if (array_search($libpath, $loadlibs) === false) { $loadlibs[] = $libpath; // For state other than 0 we need to take action as well as just // adding it to loadlibs if ($state != REQUIREJS_BEFOREHEADER) { // Get the script statement for this library $scriptstatement = get_require_js_code(array($libpath)); if ($state == REQUIREJS_AFTERHEADER) { // After the header, print it immediately print $scriptstatement; } else { // Haven't finished the header yet. Add it after the // header $latecode .= $scriptstatement; } } } } } else { if ($extracthtml == 1) { if ($state !== REQUIREJS_BEFOREHEADER) { debugging('Incorrect state in require_js (expected BEFOREHEADER): be careful not to call with empty $lib (except in print_header)'); } else { $state = REQUIREJS_INHEADER; } return get_require_js_code($loadlibs); } else { if ($extracthtml == 2) { if ($state !== REQUIREJS_INHEADER) { debugging('Incorrect state in require_js (expected INHEADER): be careful not to call with empty $lib (except in print_header)'); return ''; } else { $state = REQUIREJS_AFTERHEADER; return $latecode; } } else { debugging('Unexpected value for $extracthtml'); } } } }
/** * add includes (js and css) into uploaded files * before returning them, useful for themes and utf.js includes * @param string text - text to search and replace * @return string - text with added head includes */ function file_modify_html_header($text) { // first look for <head> tag global $CFG; $stylesheetshtml = ''; foreach ($CFG->stylesheets as $stylesheet) { $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '" />' . "\n"; } $ufo = ''; if (filter_is_enabled('filter/mediaplugin')) { // this script is needed by most media filter plugins. $ufo = get_require_js_code(array($CFG->wwwroot . '/lib/ufo.js')); } preg_match('/\\<head\\>|\\<HEAD\\>/', $text, $matches); if ($matches) { $replacement = '<head>' . $ufo . $stylesheetshtml; $text = preg_replace('/\\<head\\>|\\<HEAD\\>/', $replacement, $text, 1); return $text; } // if not, look for <html> tag, and stick <head> right after preg_match('/\\<html\\>|\\<HTML\\>/', $text, $matches); if ($matches) { // replace <html> tag with <html><head>includes</head> $replacement = '<html>' . "\n" . '<head>' . $ufo . $stylesheetshtml . '</head>'; $text = preg_replace('/\\<html\\>|\\<HTML\\>/', $replacement, $text, 1); return $text; } // if not, look for <body> tag, and stick <head> before body preg_match('/\\<body\\>|\\<BODY\\>/', $text, $matches); if ($matches) { $replacement = '<head>' . $ufo . $stylesheetshtml . '</head>' . "\n" . '<body>'; $text = preg_replace('/\\<body\\>|\\<BODY\\>/', $replacement, $text, 1); return $text; } // if not, just stick a <head> tag at the beginning $text = '<head>' . $ufo . $stylesheetshtml . '</head>' . "\n" . $text; return $text; }