validate plugin ID and shutdown if it's missing

- DEV_MODE needed to keep running without proper ID
This commit is contained in:
Steffen Schröder 2014-08-12 16:40:43 +02:00
parent 300d817409
commit 699c5951d9

View File

@ -9,6 +9,7 @@ use ManiaControl\Files\FileUtil;
use ManiaControl\Logger; use ManiaControl\Logger;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Utils\ClassUtil;
/** /**
* Class managing Plugins * Class managing Plugins
@ -108,7 +109,7 @@ class PluginManager {
* @return bool * @return bool
*/ */
public static function isPluginClass($pluginClass) { public static function isPluginClass($pluginClass) {
$pluginClass = self::getClass($pluginClass); $pluginClass = ClassUtil::getClass($pluginClass);
if (!class_exists($pluginClass, false)) { if (!class_exists($pluginClass, false)) {
return false; return false;
} }
@ -122,19 +123,6 @@ class PluginManager {
return true; return true;
} }
/**
* Get the Class of the Object
*
* @param mixed $object
* @return string
*/
private static function getClass($object) {
if (is_object($object)) {
return get_class($object);
}
return (string)$object;
}
/** /**
* Deactivate the Plugin with the given Class * Deactivate the Plugin with the given Class
* *
@ -190,9 +178,9 @@ class PluginManager {
* @return string * @return string
*/ */
public static function getPluginClass($pluginClass) { public static function getPluginClass($pluginClass) {
$pluginClass = self::getClass($pluginClass); $pluginClass = ClassUtil::getClass($pluginClass);
if (!self::isPluginClass($pluginClass)) { if (!self::isPluginClass($pluginClass)) {
return false; return null;
} }
return $pluginClass; return $pluginClass;
} }
@ -258,9 +246,17 @@ class PluginManager {
$newClasses = array_diff($classesAfter, $classesBefore); $newClasses = array_diff($classesAfter, $classesBefore);
foreach ($newClasses as $className) { foreach ($newClasses as $className) {
if (!$this->isPluginClass($className)) { if (!self::isPluginClass($className)) {
continue; continue;
} }
if (!self::validatePluginClass($className)) {
$message = "The plugin class '{$className}' isn't correctly implemented: You need to return a proper ID by registering it on maniacontrol.com!";
Logger::logWarning($message);
if (!DEV_MODE) {
$message = 'Fix the plugin or turn on DEV_MODE!';
$this->maniaControl->quit($message, true);
}
}
if (!$this->addPluginClass($className)) { if (!$this->addPluginClass($className)) {
continue; continue;
@ -313,6 +309,20 @@ class PluginManager {
} }
} }
/**
* Validate that the given class is a correctly implemented plugin class
*
* @param string $pluginClass
* @return bool
*/
private static function validatePluginClass($pluginClass) {
if (!self::isPluginClass($pluginClass)) {
return false;
}
/** @var Plugin $pluginClass */
return ($pluginClass::getId() > 0);
}
/** /**
* Add the class to array of loaded plugin classes * Add the class to array of loaded plugin classes
* *