2013-11-09 11:06:04 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
namespace ManiaControl\Plugins;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/Plugin.php';
|
2013-12-04 00:40:37 +01:00
|
|
|
require_once __DIR__ . '/PluginMenu.php';
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-12-14 23:33:23 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
2014-01-18 10:42:19 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2013-12-14 23:33:23 +01:00
|
|
|
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
2013-11-09 11:06:04 +01:00
|
|
|
|
|
|
|
/**
|
2013-12-14 23:33:23 +01:00
|
|
|
* Class managing Plugins
|
2013-11-09 11:06:04 +01:00
|
|
|
*
|
2013-11-12 15:48:25 +01:00
|
|
|
* @author steeffeen & kremsy
|
2013-11-09 11:06:04 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
class PluginManager {
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const TABLE_PLUGINS = 'mc_plugins';
|
2014-01-18 10:42:19 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
2013-12-04 00:40:37 +01:00
|
|
|
private $pluginMenu = null;
|
2013-11-10 02:55:08 +01:00
|
|
|
private $activePlugins = array();
|
|
|
|
private $pluginClasses = array();
|
2013-11-09 11:06:04 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2013-11-12 19:33:25 +01:00
|
|
|
* Construct plugin manager
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
2014-01-18 10:42:19 +01:00
|
|
|
|
2013-12-04 00:40:37 +01:00
|
|
|
$this->pluginMenu = new PluginMenu($maniaControl);
|
|
|
|
$this->maniaControl->configurator->addMenu($this->pluginMenu);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 11:06:04 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Initialize necessary database tables
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2014-01-18 10:42:19 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2013-11-10 02:55:08 +01:00
|
|
|
$pluginsTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLUGINS . "` (
|
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`className` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`active` tinyint(1) NOT NULL DEFAULT '0',
|
|
|
|
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
2013-12-14 23:33:23 +01:00
|
|
|
PRIMARY KEY (`index`),
|
|
|
|
UNIQUE KEY `className` (`className`)
|
2013-11-10 02:55:08 +01:00
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ManiaControl plugin status' AUTO_INCREMENT=1;";
|
2014-01-18 10:42:19 +01:00
|
|
|
$tableStatement = $mysqli->prepare($pluginsTableQuery);
|
|
|
|
if($mysqli->error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$tableStatement->execute();
|
2014-01-18 10:42:19 +01:00
|
|
|
if($tableStatement->error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($tableStatement->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$tableStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-09 11:06:04 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2013-12-14 23:33:23 +01:00
|
|
|
* Check if the plugin is running
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2013-11-10 02:55:08 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-12-14 23:33:23 +01:00
|
|
|
public function isPluginActive($pluginClass) {
|
2014-01-18 10:42:19 +01:00
|
|
|
if(is_object($pluginClass)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginClass = get_class($pluginClass);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-12-14 23:33:23 +01:00
|
|
|
return isset($this->activePlugins[$pluginClass]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given class implements the plugin interface
|
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2013-12-14 23:33:23 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPluginClass($pluginClass) {
|
2014-01-18 10:42:19 +01:00
|
|
|
if(is_object($pluginClass)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginClass = get_class($pluginClass);
|
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if(!in_array(Plugin::PLUGIN_INTERFACE, class_implements($pluginClass))) {
|
2013-11-10 02:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-09 13:29:18 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2013-12-14 23:33:23 +01:00
|
|
|
* Add the class to array of loaded plugin classes
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2013-11-10 02:55:08 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-12-14 23:33:23 +01:00
|
|
|
public function addPluginClass($pluginClass) {
|
2014-01-18 10:42:19 +01:00
|
|
|
if(is_object($pluginClass)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginClass = get_class($pluginClass);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if(in_array($pluginClass, $this->pluginClasses)) {
|
2013-11-10 02:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if(!$this->isPluginClass($pluginClass)) {
|
2013-11-10 02:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-14 23:33:23 +01:00
|
|
|
array_push($this->pluginClasses, $pluginClass);
|
|
|
|
return true;
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 13:34:36 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
/**
|
|
|
|
* Activate and start the plugin with the given name
|
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2014-01-18 10:42:19 +01:00
|
|
|
* @param string $adminLogin
|
2013-12-12 19:41:37 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-18 22:06:07 +01:00
|
|
|
public function activatePlugin($pluginClass, $adminLogin = null) {
|
2014-01-18 10:42:19 +01:00
|
|
|
if(!is_string($pluginClass)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if(!$this->isPluginClass($pluginClass)) {
|
2013-12-12 19:41:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if($this->isPluginActive($pluginClass)) {
|
2013-12-12 19:41:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-14 23:33:23 +01:00
|
|
|
$plugin = new $pluginClass();
|
2014-01-18 10:42:19 +01:00
|
|
|
try {
|
|
|
|
$plugin->load($this->maniaControl);
|
|
|
|
} catch(\Exception $e) {
|
2014-01-18 13:37:28 +01:00
|
|
|
$this->maniaControl->chat->sendError('Error while plugin activation: ' . $e->getMessage(), $adminLogin);
|
2014-01-18 13:36:16 +01:00
|
|
|
$this->maniaControl->log('Error while plugin activation: ' . $e->getMessage());
|
2014-01-18 10:42:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->activePlugins[$pluginClass] = $plugin;
|
2013-12-12 19:41:37 +01:00
|
|
|
$this->savePluginStatus($pluginClass, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-14 23:33:23 +01:00
|
|
|
* Deactivate the plugin with the given class
|
2013-12-12 19:41:37 +01:00
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2013-12-12 19:41:37 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function deactivatePlugin($pluginClass) {
|
2014-01-18 10:42:19 +01:00
|
|
|
if(is_object($pluginClass)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginClass = get_class($pluginClass);
|
2013-12-12 19:41:37 +01:00
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if(!$this->isPluginActive($pluginClass)) {
|
2013-12-12 19:41:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-14 23:33:23 +01:00
|
|
|
$plugin = $this->activePlugins[$pluginClass];
|
|
|
|
unset($this->activePlugins[$pluginClass]);
|
|
|
|
$plugin->unload();
|
|
|
|
$interfaces = class_implements($pluginClass);
|
2014-01-18 10:42:19 +01:00
|
|
|
if(in_array(CallbackListener::CALLBACKLISTENER_INTERFACE, $interfaces)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->maniaControl->callbackManager->unregisterCallbackListener($plugin);
|
|
|
|
$this->maniaControl->callbackManager->unregisterScriptCallbackListener($plugin);
|
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if(in_array(ManialinkPageAnswerListener::MANIALINKPAGEANSWERLISTENER_INTERFACE, $interfaces)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin);
|
|
|
|
}
|
2013-12-12 19:41:37 +01:00
|
|
|
$this->savePluginStatus($pluginClass, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Load complete plugins directory and start all configured plugins
|
|
|
|
*/
|
|
|
|
public function loadPlugins() {
|
|
|
|
$pluginsDirectory = ManiaControlDir . '/plugins/';
|
2014-01-18 10:42:19 +01:00
|
|
|
$pluginFiles = scandir($pluginsDirectory, 0);
|
|
|
|
foreach($pluginFiles as $pluginFile) {
|
|
|
|
if(stripos($pluginFile, '.') === 0) {
|
2013-11-10 02:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$classesBefore = get_declared_classes();
|
2014-01-18 10:42:19 +01:00
|
|
|
$success = include_once $pluginsDirectory . $pluginFile;
|
|
|
|
if(!$success) {
|
2013-11-10 02:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$classesAfter = get_declared_classes();
|
2014-01-18 10:42:19 +01:00
|
|
|
$newClasses = array_diff($classesAfter, $classesBefore);
|
|
|
|
foreach($newClasses as $className) {
|
|
|
|
if(!$this->isPluginClass($className)) {
|
2013-11-10 02:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->addPluginClass($className);
|
2014-01-18 10:42:19 +01:00
|
|
|
if($this->isPluginActive($className)) {
|
2013-11-10 02:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-18 10:42:19 +01:00
|
|
|
if(!$this->getSavedPluginStatus($className)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->activatePlugin($className);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-09 20:31:02 +01:00
|
|
|
|
2013-12-29 19:21:29 +01:00
|
|
|
/**
|
2013-12-31 15:09:25 +01:00
|
|
|
* Returns an Plugin if it is activated
|
|
|
|
*
|
|
|
|
* @param string $pluginClass
|
|
|
|
* @return Plugin
|
2013-12-29 19:21:29 +01:00
|
|
|
*/
|
|
|
|
public function getPlugin($pluginClass) {
|
2014-01-18 10:42:19 +01:00
|
|
|
if($this->isPluginActive($pluginClass)) {
|
2013-12-29 19:21:29 +01:00
|
|
|
return $this->activePlugins[$pluginClass];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-12-09 20:31:02 +01:00
|
|
|
/**
|
2013-12-12 19:41:37 +01:00
|
|
|
* Get all declared plugin class names
|
|
|
|
*
|
2013-12-09 20:31:02 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-12-12 19:41:37 +01:00
|
|
|
public function getPluginClasses() {
|
2013-12-09 20:31:02 +01:00
|
|
|
return $this->pluginClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-12 19:41:37 +01:00
|
|
|
* Get all active plugins
|
|
|
|
*
|
2013-12-09 20:31:02 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-12-12 19:41:37 +01:00
|
|
|
public function getActivePlugins() {
|
2013-12-09 20:31:02 +01:00
|
|
|
return $this->activePlugins;
|
|
|
|
}
|
2013-12-14 23:33:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save plugin status in database
|
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $className
|
2014-01-18 10:42:19 +01:00
|
|
|
* @param bool $active
|
2013-12-14 23:33:23 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function savePluginStatus($className, $active) {
|
2014-01-18 10:42:19 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginStatusQuery = "INSERT INTO `" . self::TABLE_PLUGINS . "` (
|
|
|
|
`className`,
|
|
|
|
`active`
|
|
|
|
) VALUES (
|
|
|
|
?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`active` = VALUES(`active`);";
|
2014-01-18 10:42:19 +01:00
|
|
|
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
|
|
|
|
if($mysqli->error) {
|
2013-12-14 23:33:23 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$activeInt = ($active ? 1 : 0);
|
|
|
|
$pluginStatement->bind_param('si', $className, $activeInt);
|
|
|
|
$pluginStatement->execute();
|
2014-01-18 10:42:19 +01:00
|
|
|
if($pluginStatement->error) {
|
2013-12-14 23:33:23 +01:00
|
|
|
trigger_error($pluginStatement->error);
|
|
|
|
$pluginStatement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$pluginStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get plugin status from database
|
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $className
|
2013-12-14 23:33:23 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function getSavedPluginStatus($className) {
|
2014-01-18 10:42:19 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "`
|
|
|
|
WHERE `className` = ?;";
|
2014-01-18 10:42:19 +01:00
|
|
|
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
|
|
|
|
if($mysqli->error) {
|
2013-12-14 23:33:23 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$pluginStatement->bind_param('s', $className);
|
|
|
|
$pluginStatement->execute();
|
2014-01-18 10:42:19 +01:00
|
|
|
if($pluginStatement->error) {
|
2013-12-14 23:33:23 +01:00
|
|
|
trigger_error($pluginStatement->error);
|
|
|
|
$pluginStatement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$pluginStatement->store_result();
|
2014-01-18 10:42:19 +01:00
|
|
|
if($pluginStatement->num_rows <= 0) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginStatement->free_result();
|
|
|
|
$pluginStatement->close();
|
|
|
|
$this->savePluginStatus($className, false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$pluginStatement->bind_result($activeInt);
|
|
|
|
$pluginStatement->fetch();
|
|
|
|
$active = ($activeInt === 1);
|
|
|
|
$pluginStatement->free_result();
|
|
|
|
$pluginStatement->close();
|
|
|
|
return $active;
|
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|