TrackManiaControl/application/core/Plugins/PluginManager.php

409 lines
10 KiB
PHP
Raw Normal View History

2013-11-09 11:06:04 +01:00
<?php
namespace ManiaControl\Plugins;
use ManiaControl\Callbacks\CallbackListener;
2014-01-18 10:42:19 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
2013-11-09 11:06:04 +01:00
/**
* Class managing Plugins
*
* @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2013-11-09 11:06:04 +01:00
*/
class PluginManager {
/*
* Constants
*/
const TABLE_PLUGINS = 'mc_plugins';
/*
* Private Properties
*/
private $maniaControl = null;
private $pluginMenu = null;
private $pluginInstallMenu = null;
private $activePlugins = array();
private $pluginClasses = array();
2013-11-09 11:06:04 +01:00
/**
* Construct plugin manager
*
2013-12-31 15:09:25 +01:00
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
$this->pluginMenu = new PluginMenu($maniaControl);
$this->maniaControl->configurator->addMenu($this->pluginMenu);
$this->pluginInstallMenu = new PluginInstallMenu($maniaControl);
$this->maniaControl->configurator->addMenu($this->pluginInstallMenu);
}
2013-11-09 11:06:04 +01:00
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$pluginsTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLUGINS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`className` varchar(100) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`),
UNIQUE KEY `className` (`className`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ManiaControl plugin status' AUTO_INCREMENT=1;";
$tableStatement = $mysqli->prepare($pluginsTableQuery);
2014-01-27 20:46:42 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$tableStatement->execute();
2014-01-27 20:46:42 +01:00
if ($tableStatement->error) {
trigger_error($tableStatement->error, E_USER_ERROR);
return false;
}
$tableStatement->close();
return true;
}
2013-11-09 11:06:04 +01:00
/**
* Check if the plugin is running
*
2013-12-31 15:09:25 +01:00
* @param string $pluginClass
* @return bool
*/
public function isPluginActive($pluginClass) {
$pluginClass = $this->getPluginClass($pluginClass);
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
* @return bool
*/
2014-04-30 03:46:42 +02:00
public static function isPluginClass($pluginClass) {
$pluginClass = self::getClass($pluginClass);
2014-05-01 20:29:29 +02:00
if (!class_exists($pluginClass, false)) {
return false;
}
if (!in_array(Plugin::PLUGIN_INTERFACE, class_implements($pluginClass, false))) {
return false;
}
return true;
}
2013-11-09 13:29:18 +01:00
/**
* Add the class to array of loaded plugin classes
*
2013-12-31 15:09:25 +01:00
* @param string $pluginClass
* @return bool
*/
public function addPluginClass($pluginClass) {
$pluginClass = $this->getPluginClass($pluginClass);
2014-01-27 20:46:42 +01:00
if (in_array($pluginClass, $this->pluginClasses)) {
return false;
}
2014-01-27 20:46:42 +01:00
if (!$this->isPluginClass($pluginClass)) {
return false;
}
array_push($this->pluginClasses, $pluginClass);
sort($this->pluginClasses);
return true;
}
2013-11-09 13:34:36 +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
* @return bool
*/
2014-01-18 22:06:07 +01:00
public function activatePlugin($pluginClass, $adminLogin = null) {
2014-01-27 20:46:42 +01:00
if (!is_string($pluginClass)) {
return false;
}
2014-01-27 20:46:42 +01:00
if (!$this->isPluginClass($pluginClass)) {
return false;
}
2014-01-27 20:46:42 +01:00
if ($this->isPluginActive($pluginClass)) {
return false;
}
2014-02-13 00:46:41 +01:00
$plugin = new $pluginClass();
/**
*
* @var Plugin $plugin
*/
$this->activePlugins[$pluginClass] = $plugin;
$this->savePluginStatus($pluginClass, true);
2014-01-18 10:42:19 +01:00
try {
$plugin->load($this->maniaControl);
}
catch (\Exception $e) {
2014-01-18 22:06:54 +01:00
$this->maniaControl->chat->sendError('Error while plugin activating ' . $pluginClass . ': ' . $e->getMessage(), $adminLogin);
$this->maniaControl->log('Error while plugin activation: ' . $pluginClass . ': ' . $e->getMessage());
unset($this->activePlugins[$pluginClass]);
$this->savePluginStatus($pluginClass, false);
2014-01-18 10:42:19 +01:00
return false;
}
$this->savePluginStatus($pluginClass, true);
return true;
}
/**
* Deactivate the plugin with the given class
*
2013-12-31 15:09:25 +01:00
* @param string $pluginClass
* @return bool
*/
public function deactivatePlugin($pluginClass) {
$pluginClass = $this->getPluginClass($pluginClass);
2014-01-27 20:46:42 +01:00
if (!$this->isPluginActive($pluginClass)) {
return false;
}
$plugin = $this->activePlugins[$pluginClass];
/**
*
* @var Plugin $plugin
*/
$plugin->unload();
unset($this->activePlugins[$pluginClass]);
if ($plugin instanceof CallbackListener) {
$this->maniaControl->callbackManager->unregisterCallbackListener($plugin);
$this->maniaControl->callbackManager->unregisterScriptCallbackListener($plugin);
}
if ($plugin instanceof CommandListener) {
$this->maniaControl->commandManager->unregisterCommandListener($plugin);
}
if ($plugin instanceof ManialinkPageAnswerListener) {
$this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin);
}
if ($plugin instanceof TimerListener) {
$this->maniaControl->timerManager->unregisterTimerListenings($plugin);
}
$this->savePluginStatus($pluginClass, false);
return true;
}
2014-02-15 15:57:08 +01:00
/**
* Load complete Plugins Directory and start all configured Plugins
*
* @return array
2014-02-15 15:57:08 +01:00
*/
public function loadPlugins() {
$pluginsDirectory = ManiaControlDir . '/plugins/';
2014-02-15 15:57:08 +01:00
$classesBefore = get_declared_classes();
$this->loadPluginFiles($pluginsDirectory);
$classesAfter = get_declared_classes();
$newPluginClasses = array();
2014-03-01 20:08:16 +01:00
$newClasses = array_diff($classesAfter, $classesBefore);
foreach ($newClasses as $className) {
2014-02-15 15:57:08 +01:00
if (!$this->isPluginClass($className)) {
continue;
}
if (!$this->addPluginClass($className)) {
2014-02-15 15:57:08 +01:00
continue;
}
array_push($newPluginClasses, $className);
2014-05-02 15:35:52 +02:00
/** @var Plugin $className */
$className::prepare($this->maniaControl);
if ($this->getSavedPluginStatus($className)) {
$this->activatePlugin($className);
}
2014-02-15 15:57:08 +01:00
}
return $newPluginClasses;
2014-02-15 15:57:08 +01:00
}
2014-03-01 20:08:16 +01:00
/**
2014-02-15 15:57:08 +01:00
* Load all Plugin Files from the Directory
*
2014-02-15 15:57:08 +01:00
* @param string $directory
*/
public function loadPluginFiles($directory = '') {
2014-02-15 15:57:08 +01:00
$pluginFiles = scandir($directory);
foreach ($pluginFiles as $pluginFile) {
2014-01-27 20:46:42 +01:00
if (stripos($pluginFile, '.') === 0) {
continue;
}
2014-02-15 15:57:08 +01:00
$filePath = $directory . $pluginFile;
if (is_file($filePath)) {
if (!stripos($pluginFile, '.php')) {
continue;
}
2014-02-15 15:57:08 +01:00
$success = include_once $filePath;
if (!$success) {
trigger_error("Error loading File '{$filePath}'!");
}
2014-02-14 18:17:05 +01:00
continue;
}
2014-02-15 15:57:08 +01:00
$dirPath = $directory . $pluginFile;
if (is_dir($dirPath)) {
$this->loadPluginFiles($dirPath . '/');
continue;
}
}
}
2013-12-09 20:31:02 +01:00
2013-12-29 19:21:29 +01:00
/**
2014-02-15 15:57:08 +01:00
* Returns a Plugin if it is activated
*
2013-12-31 15:09:25 +01:00
* @param string $pluginClass
* @return Plugin
2013-12-29 19:21:29 +01:00
*/
public function getPlugin($pluginClass) {
2014-01-27 20:46:42 +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
/**
* Get all declared plugin class names
*
2013-12-09 20:31:02 +01:00
* @return array
*/
public function getPluginClasses() {
2013-12-09 20:31:02 +01:00
return $this->pluginClasses;
}
/**
* Get all active plugins
*
2013-12-09 20:31:02 +01:00
* @return array
*/
public function getActivePlugins() {
2013-12-09 20:31:02 +01:00
return $this->activePlugins;
}
/**
* Save plugin status in database
*
2013-12-31 15:09:25 +01:00
* @param string $className
* @param bool $active
* @return bool
*/
private function savePluginStatus($className, $active) {
$mysqli = $this->maniaControl->database->mysqli;
$pluginStatusQuery = "INSERT INTO `" . self::TABLE_PLUGINS . "` (
`className`,
`active`
) VALUES (
?, ?
) ON DUPLICATE KEY UPDATE
`active` = VALUES(`active`);";
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
2014-01-27 20:46:42 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$activeInt = ($active ? 1 : 0);
$pluginStatement->bind_param('si', $className, $activeInt);
$pluginStatement->execute();
2014-01-27 20:46:42 +01:00
if ($pluginStatement->error) {
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
* @return bool
*/
public function getSavedPluginStatus($className) {
$mysqli = $this->maniaControl->database->mysqli;
$pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "`
WHERE `className` = ?;";
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
2014-01-27 20:46:42 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$pluginStatement->bind_param('s', $className);
$pluginStatement->execute();
2014-01-27 20:46:42 +01:00
if ($pluginStatement->error) {
trigger_error($pluginStatement->error);
$pluginStatement->close();
return false;
}
$pluginStatement->store_result();
2014-01-27 20:46:42 +01:00
if ($pluginStatement->num_rows <= 0) {
$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
/**
* Fetch the Plugins List from the ManiaControl Website
*
* @param $function
2014-03-01 20:08:16 +01:00
*/
2014-04-20 17:49:18 +02:00
public function fetchPluginList($function) {
2014-03-01 20:08:16 +01:00
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
$this->maniaControl->fileReader->loadFile($url, function ($dataJson, $error) use(&$function) {
2014-03-01 20:08:16 +01:00
$data = json_decode($dataJson);
2014-04-20 17:49:18 +02:00
call_user_func($function, $data, $error);
2014-03-01 20:08:16 +01:00
});
}
/**
* Get the Class of the Plugin
*
* @param mixed $pluginClass
* @return string
*/
2014-05-01 23:32:58 +02:00
public static function getPluginClass($pluginClass) {
$pluginClass = self::getClass($pluginClass);
if (!self::isPluginClass($pluginClass)) {
return false;
}
return $pluginClass;
}
/**
* 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;
}
}