2013-11-09 11:06:04 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
namespace ManiaControl\Plugins;
|
|
|
|
|
2013-12-14 23:33:23 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
2014-04-28 17:53:54 +02:00
|
|
|
use ManiaControl\Callbacks\TimerListener;
|
|
|
|
use ManiaControl\Commands\CommandListener;
|
2014-08-07 13:47:26 +02:00
|
|
|
use ManiaControl\Files\FileUtil;
|
2014-08-03 13:29:54 +02:00
|
|
|
use ManiaControl\Logger;
|
2014-05-02 17:40:47 +02:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
2013-11-09 11:06:04 +01:00
|
|
|
|
|
|
|
/**
|
2013-12-14 23:33:23 +01:00
|
|
|
* Class managing Plugins
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
|
|
* @copyright 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-09 11:06:04 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
class PluginManager {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-11-10 02:55:08 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-05-13 16:40:05 +02:00
|
|
|
const TABLE_PLUGINS = 'mc_plugins';
|
|
|
|
const CB_PLUGIN_LOADED = 'PluginManager.PluginLoaded';
|
2014-05-04 17:00:03 +02:00
|
|
|
const CB_PLUGIN_UNLOADED = 'PluginManager.PluginUnloaded';
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-07-25 16:28:47 +02:00
|
|
|
* Private properties
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2013-11-10 02:55:08 +01:00
|
|
|
private $maniaControl = null;
|
2014-07-25 16:28:47 +02:00
|
|
|
/** @var PluginMenu $pluginMenu */
|
2014-04-28 17:53:54 +02:00
|
|
|
private $pluginMenu = null;
|
2014-07-25 16:28:47 +02:00
|
|
|
/** @var InstallMenu $pluginInstallMenu */
|
2014-04-28 17:53:54 +02:00
|
|
|
private $pluginInstallMenu = null;
|
2014-05-27 22:44:22 +02:00
|
|
|
/** @var Plugin[] $activePlugins */
|
2013-11-10 02:55:08 +01:00
|
|
|
private $activePlugins = array();
|
2014-05-27 22:44:22 +02:00
|
|
|
/** @var string[] $pluginClasses */
|
2013-11-10 02:55:08 +01:00
|
|
|
private $pluginClasses = array();
|
2013-11-09 11:06:04 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Construct a new plugin manager instance
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-07-25 16:28:47 +02:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2013-12-04 00:40:37 +01:00
|
|
|
$this->pluginMenu = new PluginMenu($maniaControl);
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getConfigurator()
|
|
|
|
->addMenu($this->pluginMenu);
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-07-01 21:23:50 +02:00
|
|
|
$this->pluginInstallMenu = new InstallMenu($maniaControl);
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getConfigurator()
|
|
|
|
->addMenu($this->pluginInstallMenu);
|
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
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2013-11-10 02:55:08 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2014-08-05 02:17:41 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()
|
|
|
|
->getMysqli();
|
2013-11-10 02:55:08 +01:00
|
|
|
$pluginsTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLUGINS . "` (
|
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
2014-02-13 14:58:04 +01:00
|
|
|
`className` varchar(100) NOT NULL,
|
2013-11-10 02:55:08 +01:00
|
|
|
`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-05-02 17:40:47 +02:00
|
|
|
$tableStatement = $mysqli->prepare($pluginsTableQuery);
|
2014-01-27 20:46:42 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$tableStatement->execute();
|
2014-01-27 20:46:42 +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
|
|
|
|
2014-05-15 14:20:44 +02:00
|
|
|
/**
|
|
|
|
* Get the Plugin Id if the given Class is a Plugin
|
|
|
|
*
|
|
|
|
* @param string $pluginClass
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function getPluginId($pluginClass) {
|
|
|
|
if (self::isPluginClass($pluginClass)) {
|
|
|
|
/** @var Plugin $pluginClass */
|
|
|
|
return $pluginClass::getId();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given class implements the plugin interface
|
|
|
|
*
|
|
|
|
* @param string $pluginClass
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isPluginClass($pluginClass) {
|
|
|
|
$pluginClass = self::getClass($pluginClass);
|
|
|
|
if (!class_exists($pluginClass, false)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-15 14:25:19 +02:00
|
|
|
$interfaces = class_implements($pluginClass, false);
|
|
|
|
if (!$interfaces) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!in_array(Plugin::PLUGIN_INTERFACE, $interfaces)) {
|
2014-05-15 14:20:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-05-04 17:00:03 +02:00
|
|
|
* Deactivate the Plugin with the given Class
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2013-11-10 02:55:08 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-02 17:40:47 +02:00
|
|
|
public function deactivatePlugin($pluginClass) {
|
2014-04-28 17:53:54 +02:00
|
|
|
$pluginClass = $this->getPluginClass($pluginClass);
|
2014-05-04 17:00:03 +02:00
|
|
|
if (!$pluginClass) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
if (!$this->isPluginActive($pluginClass)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-04 17:00:03 +02:00
|
|
|
|
|
|
|
/** @var Plugin $plugin */
|
2014-05-02 17:40:47 +02:00
|
|
|
$plugin = $this->activePlugins[$pluginClass];
|
|
|
|
unset($this->activePlugins[$pluginClass]);
|
2014-05-04 17:00:03 +02:00
|
|
|
|
|
|
|
$plugin->unload();
|
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
if ($plugin instanceof CallbackListener) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getCallbackManager()
|
|
|
|
->unregisterCallbackListener($plugin);
|
|
|
|
$this->maniaControl->getCallbackManager()
|
|
|
|
->unregisterScriptCallbackListener($plugin);
|
2014-05-02 17:40:47 +02:00
|
|
|
}
|
|
|
|
if ($plugin instanceof CommandListener) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getCommandManager()
|
|
|
|
->unregisterCommandListener($plugin);
|
2014-05-02 17:40:47 +02:00
|
|
|
}
|
|
|
|
if ($plugin instanceof ManialinkPageAnswerListener) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getManialinkManager()
|
|
|
|
->unregisterManialinkPageAnswerListener($plugin);
|
2014-05-02 17:40:47 +02:00
|
|
|
}
|
|
|
|
if ($plugin instanceof TimerListener) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getTimerManager()
|
|
|
|
->unregisterTimerListenings($plugin);
|
2014-05-02 17:40:47 +02:00
|
|
|
}
|
2014-05-04 17:00:03 +02:00
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
$this->savePluginStatus($pluginClass, false);
|
2014-05-04 17:00:03 +02:00
|
|
|
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getCallbackManager()
|
|
|
|
->triggerCallback(self::CB_PLUGIN_UNLOADED, $pluginClass, $plugin);
|
2014-05-04 17:00:03 +02:00
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
return true;
|
2013-12-14 23:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-02 17:40:47 +02:00
|
|
|
* Get the Class of the Plugin
|
|
|
|
*
|
|
|
|
* @param mixed $pluginClass
|
|
|
|
* @return string
|
2013-12-14 23:33:23 +01:00
|
|
|
*/
|
2014-05-02 17:40:47 +02:00
|
|
|
public static function getPluginClass($pluginClass) {
|
2014-05-02 04:03:56 +02:00
|
|
|
$pluginClass = self::getClass($pluginClass);
|
2014-05-02 17:40:47 +02:00
|
|
|
if (!self::isPluginClass($pluginClass)) {
|
2013-11-10 02:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
return $pluginClass;
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 13:29:18 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
/**
|
2014-05-03 21:57:38 +02:00
|
|
|
* Check if the Plugin is currently running
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2013-12-12 19:41:37 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-02 17:40:47 +02:00
|
|
|
public function isPluginActive($pluginClass) {
|
2014-04-28 17:53:54 +02:00
|
|
|
$pluginClass = $this->getPluginClass($pluginClass);
|
2014-05-02 17:40:47 +02:00
|
|
|
return isset($this->activePlugins[$pluginClass]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-03 21:57:38 +02:00
|
|
|
* Save Plugin Status in Database
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @param bool $active
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function savePluginStatus($className, $active) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()
|
|
|
|
->getMysqli();
|
2014-05-02 17:40:47 +02:00
|
|
|
$pluginStatusQuery = "INSERT INTO `" . self::TABLE_PLUGINS . "` (
|
|
|
|
`className`,
|
|
|
|
`active`
|
|
|
|
) VALUES (
|
|
|
|
?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`active` = VALUES(`active`);";
|
|
|
|
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
2013-12-12 19:41:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
$activeInt = ($active ? 1 : 0);
|
|
|
|
$pluginStatement->bind_param('si', $className, $activeInt);
|
|
|
|
$pluginStatement->execute();
|
|
|
|
if ($pluginStatement->error) {
|
|
|
|
trigger_error($pluginStatement->error);
|
|
|
|
$pluginStatement->close();
|
|
|
|
return false;
|
2014-04-28 17:53:54 +02:00
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
$pluginStatement->close();
|
2013-12-12 19:41:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:57:08 +01:00
|
|
|
/**
|
|
|
|
* Load complete Plugins Directory and start all configured Plugins
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-05-27 22:50:28 +02:00
|
|
|
* @return string[]
|
2014-02-15 15:57:08 +01:00
|
|
|
*/
|
|
|
|
public function loadPlugins() {
|
2014-05-03 21:57:38 +02:00
|
|
|
$pluginsDirectory = ManiaControlDir . 'plugins' . DIRECTORY_SEPARATOR;
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-02-15 15:57:08 +01:00
|
|
|
$classesBefore = get_declared_classes();
|
|
|
|
$this->loadPluginFiles($pluginsDirectory);
|
|
|
|
$classesAfter = get_declared_classes();
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-05-02 04:03:56 +02:00
|
|
|
$newPluginClasses = array();
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-03-01 20:08:16 +01:00
|
|
|
$newClasses = array_diff($classesAfter, $classesBefore);
|
2014-04-28 17:53:54 +02:00
|
|
|
foreach ($newClasses as $className) {
|
2014-02-15 15:57:08 +01:00
|
|
|
if (!$this->isPluginClass($className)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-05-02 04:03:56 +02:00
|
|
|
if (!$this->addPluginClass($className)) {
|
2014-02-15 15:57:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-02 04:03:56 +02:00
|
|
|
array_push($newPluginClasses, $className);
|
2014-05-02 15:35:52 +02:00
|
|
|
|
|
|
|
/** @var Plugin $className */
|
2014-05-02 04:03:56 +02:00
|
|
|
$className::prepare($this->maniaControl);
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-05-02 04:03:56 +02:00
|
|
|
if ($this->getSavedPluginStatus($className)) {
|
|
|
|
$this->activatePlugin($className);
|
|
|
|
}
|
2014-02-15 15:57:08 +01:00
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-05-02 04:03:56 +02:00
|
|
|
return $newPluginClasses;
|
2014-02-15 15:57:08 +01:00
|
|
|
}
|
2014-03-01 20:08:16 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-02-15 15:57:08 +01:00
|
|
|
* Load all Plugin Files from the Directory
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-02-15 15:57:08 +01:00
|
|
|
* @param string $directory
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
2014-04-16 22:53:14 +02:00
|
|
|
public function loadPluginFiles($directory = '') {
|
2014-06-29 22:30:01 +02:00
|
|
|
if (!is_readable($directory) || !is_dir($directory)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-15 15:57:08 +01:00
|
|
|
$pluginFiles = scandir($directory);
|
2014-04-28 17:53:54 +02:00
|
|
|
foreach ($pluginFiles as $pluginFile) {
|
2014-07-26 16:31:47 +02:00
|
|
|
if (substr($pluginFile, 0, 1) === '.') {
|
2013-11-10 02:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-02-15 15:57:08 +01:00
|
|
|
$filePath = $directory . $pluginFile;
|
|
|
|
if (is_file($filePath)) {
|
2014-08-07 13:47:26 +02:00
|
|
|
if (!FileUtil::isPhpFileName($pluginFile)) {
|
2014-05-01 20:20:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-02-15 15:57:08 +01:00
|
|
|
$success = include_once $filePath;
|
|
|
|
if (!$success) {
|
2014-08-03 13:29:54 +02:00
|
|
|
Logger::logError("Couldn't load file '{$filePath}'!");
|
2014-02-15 15:57:08 +01:00
|
|
|
}
|
2014-02-14 18:17:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-02-15 15:57:08 +01:00
|
|
|
$dirPath = $directory . $pluginFile;
|
|
|
|
if (is_dir($dirPath)) {
|
2014-05-03 21:37:28 +02:00
|
|
|
$this->loadPluginFiles($dirPath . DIRECTORY_SEPARATOR);
|
2013-11-10 02:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-09 20:31:02 +01:00
|
|
|
|
2013-12-29 19:21:29 +01:00
|
|
|
/**
|
2014-05-02 17:40:47 +02:00
|
|
|
* Add the class to array of loaded plugin classes
|
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $pluginClass
|
2013-12-14 23:33:23 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-02 17:40:47 +02:00
|
|
|
public function addPluginClass($pluginClass) {
|
|
|
|
$pluginClass = $this->getPluginClass($pluginClass);
|
|
|
|
if (in_array($pluginClass, $this->pluginClasses)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
if (!$this->isPluginClass($pluginClass)) {
|
2013-12-14 23:33:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
array_push($this->pluginClasses, $pluginClass);
|
|
|
|
sort($this->pluginClasses);
|
2013-12-14 23:33:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get plugin status from database
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2013-12-31 15:09:25 +01:00
|
|
|
* @param string $className
|
2013-12-14 23:33:23 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-16 22:53:14 +02:00
|
|
|
public function getSavedPluginStatus($className) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()
|
|
|
|
->getMysqli();
|
2013-12-14 23:33:23 +01:00
|
|
|
$pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "`
|
|
|
|
WHERE `className` = ?;";
|
2014-05-02 17:40:47 +02:00
|
|
|
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
|
2014-01-27 20:46:42 +01:00
|
|
|
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-27 20:46:42 +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-27 20:46:42 +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;
|
|
|
|
}
|
2014-03-01 20:08:16 +01:00
|
|
|
|
|
|
|
/**
|
2014-05-02 17:40:47 +02:00
|
|
|
* Activate and start the plugin with the given name
|
|
|
|
*
|
|
|
|
* @param string $pluginClass
|
|
|
|
* @param string $adminLogin
|
|
|
|
* @return bool
|
2014-03-01 20:08:16 +01:00
|
|
|
*/
|
2014-05-02 17:40:47 +02:00
|
|
|
public function activatePlugin($pluginClass, $adminLogin = null) {
|
|
|
|
if (!$this->isPluginClass($pluginClass)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($this->isPluginActive($pluginClass)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-04 16:53:36 +02:00
|
|
|
/** @var Plugin $plugin */
|
2014-05-02 17:40:47 +02:00
|
|
|
$plugin = new $pluginClass();
|
2014-05-04 16:53:36 +02:00
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
try {
|
|
|
|
$plugin->load($this->maniaControl);
|
|
|
|
} catch (\Exception $e) {
|
2014-05-04 16:53:36 +02:00
|
|
|
$message = "Error during Plugin Activation of '{$pluginClass}': '{$e->getMessage()}'";
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getChat()
|
|
|
|
->sendError($message, $adminLogin);
|
2014-08-05 01:49:13 +02:00
|
|
|
Logger::logError($message);
|
2014-05-02 17:40:47 +02:00
|
|
|
$this->savePluginStatus($pluginClass, false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:53:36 +02:00
|
|
|
$this->activePlugins[$pluginClass] = $plugin;
|
2014-05-02 17:40:47 +02:00
|
|
|
$this->savePluginStatus($pluginClass, true);
|
2014-05-04 17:00:03 +02:00
|
|
|
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getCallbackManager()
|
|
|
|
->triggerCallback(self::CB_PLUGIN_LOADED, $pluginClass, $plugin);
|
2014-05-04 17:00:03 +02:00
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
return true;
|
2014-03-01 20:08:16 +01:00
|
|
|
}
|
2014-04-27 15:12:09 +02:00
|
|
|
|
2014-06-20 00:47:32 +02:00
|
|
|
/**
|
|
|
|
* Check if the Plugin with the given ID is already installed and loaded
|
|
|
|
*
|
|
|
|
* @param int $pluginId
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPluginIdInstalled($pluginId) {
|
|
|
|
foreach ($this->pluginClasses as $pluginClass) {
|
|
|
|
/** @var Plugin $pluginClass */
|
|
|
|
if ($pluginClass::getId() == $pluginId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-28 17:53:54 +02:00
|
|
|
/**
|
2014-05-02 17:40:47 +02:00
|
|
|
* Returns a Plugin if it is activated
|
|
|
|
*
|
|
|
|
* @param string $pluginClass
|
|
|
|
* @return Plugin
|
2014-04-28 17:53:54 +02:00
|
|
|
*/
|
2014-05-02 17:40:47 +02:00
|
|
|
public function getPlugin($pluginClass) {
|
|
|
|
if ($this->isPluginActive($pluginClass)) {
|
|
|
|
return $this->activePlugins[$pluginClass];
|
2014-05-02 04:03:56 +02:00
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
return null;
|
2014-05-02 04:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-02 17:40:47 +02:00
|
|
|
* Get all declared plugin class names
|
|
|
|
*
|
2014-05-27 22:50:28 +02:00
|
|
|
* @return string[]
|
2014-05-02 04:03:56 +02:00
|
|
|
*/
|
2014-05-02 17:40:47 +02:00
|
|
|
public function getPluginClasses() {
|
|
|
|
return $this->pluginClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-27 22:50:28 +02:00
|
|
|
* Get the Ids of all active Plugins
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-05-27 22:50:28 +02:00
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getActivePluginsIds() {
|
|
|
|
$pluginsIds = array();
|
|
|
|
foreach ($this->getActivePlugins() as $plugin) {
|
|
|
|
$pluginId = $plugin::getId();
|
|
|
|
if (is_numeric($pluginId)) {
|
|
|
|
array_push($pluginsIds, $pluginId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pluginsIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all active Plugins
|
|
|
|
*
|
|
|
|
* @return Plugin[]
|
2014-05-02 17:40:47 +02:00
|
|
|
*/
|
|
|
|
public function getActivePlugins() {
|
|
|
|
return $this->activePlugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the Plugins List from the ManiaControl Website
|
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param callable $function
|
2014-05-02 17:40:47 +02:00
|
|
|
*/
|
2014-05-13 16:40:05 +02:00
|
|
|
public function fetchPluginList(callable $function) {
|
2014-05-02 17:40:47 +02:00
|
|
|
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getFileReader()
|
|
|
|
->loadFile($url, function ($dataJson, $error) use (&$function) {
|
2014-08-07 13:47:26 +02:00
|
|
|
$data = json_decode($dataJson);
|
|
|
|
call_user_func($function, $data, $error);
|
|
|
|
});
|
2014-04-28 17:53:54 +02:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|