Skip to content

Commit

Permalink
MDL-38121 tell admins when plugin installed into wrong dir
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Feb 23, 2013
1 parent f29e62c commit bdbcb6d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions lang/en/plugin.php
Expand Up @@ -29,6 +29,7 @@
$string['availability'] = 'Availability';
$string['checkforupdates'] = 'Check for available updates';
$string['checkforupdateslast'] = 'Last check done on {$a}';
$string['detectedmisplacedplugin'] = 'Plugin "{$a->component}" is installed in incorrect location "{$a->current}", expected location is "{$a->expected}"';
$string['displayname'] = 'Plugin name';
$string['err_response_curl'] = 'Unable to fetch available updates data - unexpected cURL error.';
$string['err_response_format_version'] = 'Unexpected version of the response format. Please try to re-check for available updates.';
Expand Down
4 changes: 4 additions & 0 deletions lib/moodlelib.php
Expand Up @@ -9134,10 +9134,14 @@ function moodle_needs_upgrading() {
continue;
}
$module = new stdClass();
$plugin = new stdClass();
if (!is_readable($fullmod.'/version.php')) {
continue;
}
include($fullmod.'/version.php'); // defines $module with version etc
if (!isset($module->version) and isset($plugin->version)) {
$module = $plugin;
}
if (empty($installed[$mod])) {
return true;
} else if ($module->version > $installed[$mod]->version) {
Expand Down
4 changes: 4 additions & 0 deletions lib/pluginlib.php
Expand Up @@ -2857,9 +2857,13 @@ protected function load_version_php() {
$versionfile = $this->full_path('version.php');

$module = new stdClass();
$plugin = new stdClass();
if (is_readable($versionfile)) {
include($versionfile);
}
if (!isset($module->version) and isset($plugin->version)) {
$module = $plugin;
}
return $module;
}

Expand Down
43 changes: 40 additions & 3 deletions lib/upgradelib.php
Expand Up @@ -97,6 +97,23 @@ function __construct($plugin, $details) {
}
}

/**
* @package core
* @subpackage upgrade
* @copyright 2009 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_misplaced_exception extends moodle_exception {
function __construct($component, $expected, $current) {
global $CFG;
$a = new stdClass();
$a->component = $component;
$a->expected = $expected;
$a->current = $current;
parent::__construct('detectedmisplacedplugin', 'core_plugin', "$CFG->wwwroot/$CFG->admin/index.php", $a);
}
}

/**
* Sets maximum expected time needed for upgrade task.
* Please always make sure that upgrade will not run longer!
Expand Down Expand Up @@ -381,12 +398,19 @@ function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
}

$plugin = new stdClass();
$module = new stdClass(); // Prevent some notices when plugin placed in wrong directory.
require($fullplug.'/version.php'); // defines $plugin with version etc

if (!isset($plugin->version) and isset($module->version)) {
$plugin = $module;
}

// if plugin tells us it's full name we may check the location
if (isset($plugin->component)) {
if ($plugin->component !== $component) {
throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
$current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullplug);
$expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($plugin->component));
throw new plugin_misplaced_exception($component, $expected, $current);
}
}

Expand Down Expand Up @@ -532,12 +556,19 @@ function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
}

$module = new stdClass();
$plugin = new stdClass(); // Prevent some notices when plugin placed in wrong directory.
require($fullmod .'/version.php'); // defines $module with version etc

if (!isset($module->version) and isset($plugin->version)) {
$module = $plugin;
}

// if plugin tells us it's full name we may check the location
if (isset($module->component)) {
if ($module->component !== $component) {
throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
$current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullmod);
$expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($module->component));
throw new plugin_misplaced_exception($component, $expected, $current);
}
}

Expand Down Expand Up @@ -703,15 +734,21 @@ function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
}
$plugin = new stdClass();
$module = new stdClass(); // Prevent some notices when module placed in wrong directory.
$plugin->version = NULL;
$plugin->cron = 0;
include($fullblock.'/version.php');
if (!isset($plugin->version) and isset($module->version)) {
$plugin = $module;
}
$block = $plugin;

// if plugin tells us it's full name we may check the location
if (isset($block->component)) {
if ($block->component !== $component) {
throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
$current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullblock);
$expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($block->component));
throw new plugin_misplaced_exception($component, $expected, $current);
}
}

Expand Down

0 comments on commit bdbcb6d

Please sign in to comment.