1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
<?php //
//
/**
* adminlib.php - Contains functions that only administrators will ever need to use
*
* @author Martin Dougiamas
* @version $Id: adminlib.php,v 1.16.2.2 2006/09/05 22:38:59 skodak Exp $
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
/**
* Upgrade plugins
*
* @uses $db
* @uses $CFG
* @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
* @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes')
* @param string $return The url to prompt the user to continue to
*/
function upgrade_plugins($type, $dir, $return) {
global $CFG, $db;
if (!$plugs = get_list_of_plugins($dir) ) {
error('No '.$type.' plugins installed!');
}
foreach ($plugs as $plug) {
$fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
unset($plugin);
if ( is_readable($fullplug .'/version.php')) {
include_once($fullplug .'/version.php'); // defines $plugin with version etc
} else {
continue; // Nothing to do.
}
if ( is_readable($fullplug .'/db/'. $CFG->dbtype .'.php')) {
include_once($fullplug .'/db/'. $CFG->dbtype .'.php'); // defines upgrading function
} else {
continue;
}
if (!isset($plugin)) {
continue;
}
if (!empty($plugin->requires)) {
if ($plugin->requires > $CFG->version) {
$info->pluginname = $plug;
$info->pluginversion = $plugin->version;
$info->currentmoodle = $CFG->version;
$info->requiremoodle = $plugin->requires;
notify(get_string('pluginrequirementsnotmet', 'error', $info));
unset($info);
continue;
}
}
$plugin->name = $plug; // The name MUST match the directory
$pluginversion = $type.'_'.$plug.'_version';
if (!isset($CFG->$pluginversion)) {
set_config($pluginversion, 0);
}
if ($CFG->$pluginversion == $plugin->version) {
// do nothing
} else if ($CFG->$pluginversion < $plugin->version) {
if (empty($updated_plugins)) {
$strpluginsetup = get_string('pluginsetup');
print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '', '', false, ' ', ' ');
}
print_heading($plugin->name .' plugin needs upgrading');
if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
$db->debug = true;
@set_time_limit(0); // To allow slow databases to complete the long SQL
if (modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
// OK so far, now update the plugins record
set_config($pluginversion, $plugin->version);
notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
} else {
notify('Installing '. $plugin->name .' FAILED!');
}
$db->debug = false;
} else { // We'll assume no tables are necessary
set_config($pluginversion, $plugin->version);
notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
}
} else { // Upgrade existing install
$upgrade_function = $type.'_'.$plugin->name .'_upgrade';
if (function_exists($upgrade_function)) {
$db->debug=true;
if ($upgrade_function($CFG->$pluginversion)) {
$db->debug=false;
// OK so far, now update the plugins record
set_config($pluginversion, $plugin->version);
notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
} else {
$db->debug=false;
notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
}
}
}
echo '<hr />';
$updated_plugins = true;
} else {
error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
}
}
if (!empty($updated_plugins)) {
print_continue($return);
die;
}
}
/**
* Find and check all modules and load them up or upgrade them if necessary
*
* @uses $db
* @uses $CFG
* @param string $return The url to prompt the user to continue to
* @todo Finish documenting this function
*/
function upgrade_activity_modules($return) {
global $CFG, $db;
if (!$mods = get_list_of_plugins('mod') ) {
error('No modules installed!');
}
foreach ($mods as $mod) {
if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
continue;
}
$fullmod = $CFG->dirroot .'/mod/'. $mod;
unset($module);
if ( is_readable($fullmod .'/version.php')) {
include_once($fullmod .'/version.php'); // defines $module with version etc
} else {
notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
continue;
}
if ( is_readable($fullmod .'/db/'. $CFG->dbtype .'.php')) {
include_once($fullmod .'/db/'. $CFG->dbtype .'.php'); // defines upgrading function
} else {
notify('Module '. $mod .': '. $fullmod .'/db/'. $CFG->dbtype .'.php was not readable');
continue;
}
if (!isset($module)) {
continue;
}
if (!empty($module->requires)) {
if ($module->requires > $CFG->version) {
$info->modulename = $mod;
$info->moduleversion = $module->version;
$info->currentmoodle = $CFG->version;
$info->requiremoodle = $module->requires;
notify(get_string('modulerequirementsnotmet', 'error', $info));
unset($info);
continue;
}
}
$module->name = $mod; // The name MUST match the directory
if ($currmodule = get_record('modules', 'name', $module->name)) {
if ($currmodule->version == $module->version) {
// do nothing
} else if ($currmodule->version < $module->version) {
if (empty($updated_modules)) {
$strmodulesetup = get_string('modulesetup');
print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '', '', false, ' ', ' ');
}
print_heading($module->name .' module needs upgrading');
$upgrade_function = $module->name.'_upgrade';
if (function_exists($upgrade_function)) {
$db->debug=true;
if ($upgrade_function($currmodule->version, $module)) {
$db->debug=false;
// OK so far, now update the modules record
$module->id = $currmodule->id;
if (! update_record('modules', $module)) {
error('Could not update '. $module->name .' record in modules table!');
}
remove_dir($CFG->dataroot . '/cache', true); // flush cache
notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
echo '<hr />';
} else {
$db->debug=false;
notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
}
}
$updated_modules = true;
} else {
error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
}
} else { // module not installed yet, so install it
if (empty($updated_modules)) {
$strmodulesetup = get_string('modulesetup');
print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '', '', false, ' ', ' ');
}
print_heading($module->name);
$updated_modules = true;
$db->debug = true;
@set_time_limit(0); // To allow slow databases to complete the long SQL
if (modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql')) {
$db->debug = false;
if ($module->id = insert_record('modules', $module)) {
notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
echo '<hr />';
} else {
error($module->name .' module could not be added to the module list!');
}
} else {
error($module->name .' tables could NOT be set up successfully!');
}
}
/// Check submodules of this module if necessary
include_once($fullmod.'/lib.php'); // defines upgrading function
$submoduleupgrade = $module->name.'_upgrade_submodules';
if (function_exists($submoduleupgrade)) {
$submoduleupgrade();
}
/// Run any defaults or final code that is necessary for this module
if ( is_readable($fullmod .'/defaults.php')) {
// Insert default values for any important configuration variables
unset($defaults);
include_once($fullmod .'/defaults.php');
if (!empty($defaults)) {
foreach ($defaults as $name => $value) {
if (!isset($CFG->$name)) {
set_config($name, $value);
}
}
}
}
}
if (!empty($updated_modules)) {
print_continue($return);
print_footer();
die;
}
}
/**
* This function will return FALSE if the lock fails to be set (ie, if it's already locked)
*
* @param string $name ?
* @param bool $value ?
* @param int $staleafter ?
* @param bool $clobberstale ?
* @todo Finish documenting this function
*/
function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) {
if (empty($name)) {
mtrace("Tried to get a cron lock for a null fieldname");
return false;
}
if (empty($value)) {
set_config($name,0);
return true;
}
if ($config = get_record('config','name',$name)) {
if (empty($config->value)) {
set_config($name,time());
} else {
// check for stale.
if ((time() - $staleafter) > $config->value) {
mtrace("STALE LOCKFILE FOR $name - was $config->value");
if (!empty($clobberstale)) {
set_config($name,time());
return true;
}
} else {
return false; // was not stale - ie, we're ok to still be running.
}
}
}
else {
set_config($name,time());
}
return true;
}
function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='') {
static $starttime;
static $lasttime;
if (empty($starttime)) {
$starttime = $lasttime = time();
$lasttime = $starttime - $updatetime;
echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
echo '<div id="bar" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
echo '<div id="slider" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
echo '</div>';
echo '<div id="text" align="center" style="width:500px;"></div>';
echo '</td></tr></table>';
echo '</div>';
}
$now = time();
if ($done && (($now - $lasttime) >= $updatetime)) {
$elapsedtime = $now - $starttime;
$projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
$percentage = format_float((float)$done / (float)$total, 2);
$width = (int)(500 * $percentage);
if ($projectedtime > 10) {
$projectedtext = ' Ending: '.format_time($projectedtime);
} else {
$projectedtext = '';
}
echo '<script>';
echo 'document.getElementById("text").innerHTML = "'.addslashes($donetext).' '.$done.' done.'.$projectedtext.'";'."\n";
echo 'document.getElementById("slider").style.width = \''.$width.'px\';'."\n";
echo '</script>';
$lasttime = $now;
sleep($sleeptime);
}
}
/**
* Try to verify that dataroot is not accessible from web.
* It is not 100% correct but might help to reduce number of vulnerable sites.
*
* Protection from httpd.conf and .htaccess is not detected properly.
*/
function is_dataroot_insecure() {
global $CFG;
$siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
$rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
$rp = strrev(trim($rp, '/'));
$rp = explode('/', $rp);
foreach($rp as $r) {
if (strpos($siteroot, '/'.$r.'/') === 0) {
$siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
} else {
break; // probably alias root
}
}
$siteroot = strrev($siteroot);
$dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
if (strpos($dataroot, $siteroot) === 0) {
return true;
}
return false;
}
?>
|