diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 6698ce9b..b734665f 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -14,7 +14,7 @@ use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; use ManiaControl\Plugins\PluginManager; use ManiaControl\Server\Server; -use StatisticManager; +use ManiaControl\Statistics\StatisticManager; require_once __DIR__ . '/Callbacks/CallbackListener.php'; require_once __DIR__ . '/Commands/CommandListener.php'; @@ -33,7 +33,7 @@ require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfosearcher.inc.php'; require_once __DIR__ . '/Manialinks/ManialinkManager.php'; -require_once __DIR__ . '/Statistics/StatisticManager.php'; +require_once __DIR__ . '/Statistics/StatisticManager.php'; require_once __DIR__ . '/Maps/Map.php'; require_once __DIR__ . '/Maps/MapManager.php'; require_once __DIR__ . '/Maps/MapList.php'; @@ -64,6 +64,8 @@ class ManiaControl implements CommandListener { */ const VERSION = '0.01'; const API_VERSION = '2013-04-16'; + const OS_UNIX = 'Unix'; + const OS_WIN = 'Windows'; /** * Public properties @@ -88,7 +90,7 @@ class ManiaControl implements CommandListener { public $settingManager = null; public $statisticManager = null; public $updateManager = null; - + /** * Private properties */ @@ -116,9 +118,10 @@ class ManiaControl implements CommandListener { $this->configurator = new Configurator($this); $this->pluginManager = new PluginManager($this); $this->updateManager = new UpdateManager($this); - + // Register for commands $this->commandManager->registerCommandListener('version', $this, 'command_Version'); + $this->commandManager->registerCommandListener('restart', $this, 'command_Restart', true); } /** @@ -130,6 +133,28 @@ class ManiaControl implements CommandListener { logMessage($message); } + /** + * Get the Operating System on which ManiaControl is running + * + * @param string $compareOS + * @return string + */ + public function getOS($compareOS = null) { + $windows = defined('PHP_WINDOWS_VERSION_MAJOR'); + if ($compareOS) { + // Return bool whether OS equals $compareOS + if ($compareOS == self::OS_WIN) { + return $windows; + } + return !$windows; + } + // Return OS + if ($windows) { + return self::OS_WIN; + } + return self::OS_UNIX; + } + /** * Return message composed of client error message and error code * @@ -144,15 +169,29 @@ class ManiaControl implements CommandListener { } /** - * Send ManiaControl version + * Handle Version Command * - * @param array $chat + * @param array $chatCallback * @param Player $player - * @return bool */ - public function command_Version(array $chat, Player $player) { + public function command_Version(array $chatCallback, Player $player) { $message = 'This server is using ManiaControl v' . ManiaControl::VERSION . '!'; - return $this->chat->sendInformation($message, $player->login); + $this->chat->sendInformation($message, $player->login); + } + + /** + * Handle Restart AdminCommand + * + * @param array $chatCallback + * @param Player $player + */ + public function command_Restart(array $chatCallback, Player $player) { + if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + $this->authenticationManager->sendNotAllowed($player); + return; + } + $this->log($player->login . ' requested ManiaControl Restart.'); + $this->restart(); } /** @@ -161,6 +200,11 @@ class ManiaControl implements CommandListener { * @param string $message */ public function quit($message = '') { + // Log quit reason + if ($message) { + $this->log($message); + } + // OnShutdown callback $this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONSHUTDOWN, array(CallbackManager::CB_MC_ONSHUTDOWN)); @@ -172,11 +216,6 @@ class ManiaControl implements CommandListener { $this->client->query('SendHideManialinkPage'); } - // Log quit reason - if ($message) { - $this->log($message); - } - // Shutdown if ($this->client) { $this->client->Terminate(); @@ -186,6 +225,39 @@ class ManiaControl implements CommandListener { exit(); } + /** + * Restart ManiaControl + * + * @param string $message + */ + public function restart($message = null) { + // Shutdown callback + $this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONSHUTDOWN, array(CallbackManager::CB_MC_ONSHUTDOWN)); + + // Announce restart + $this->chat->sendInformation('Restarting ManiaControl...'); + $this->log('Restarting ManiaControl...'); + if ($message) $this->log($message); + + // Hide widgets + $this->client->query('SendHideManialinkPage'); + + // Close connection + $this->client->Terminate(); + + // Execute start script in background + if ($this->getOS(self::OS_UNIX)) { + $command = 'sh ' . escapeshellarg(ManiaControlDir . '/ManiaControl.sh') . ' > /dev/null &'; + exec($command); + } + else { + $command = 'start /B ' . escapeshellarg(ManiaControlDir . '/ManiaControl.bat'); + pclose(popen($command, 'r')); + } + + exit(); + } + /** * Run ManiaControl */ diff --git a/application/core/Statistics/StatisticManager.php b/application/core/Statistics/StatisticManager.php index fef00c75..b37ce8c5 100644 --- a/application/core/Statistics/StatisticManager.php +++ b/application/core/Statistics/StatisticManager.php @@ -1,175 +1,34 @@ maniaControl = $maniaControl; - $this->mysqli = $this->maniaControl->database->mysqli; $this->initTables(); - - $this->statisticCollector = new StatisticCollector($maniaControl); - - //Store Stats MetaData - $this->storeStatMetaData(); - } - - /** - * Get the value of an statistic - * - * @param $statName - * @param $playerId - * @param bool $serverLogin - * @return int - */ - public function getStatisticData($statName, $playerId, $serverLogin = false) { - $statId = $this->getStatId($statName); - - if($statId == null) { - return -1; - } - - if(!$serverLogin) { - $query = "SELECT SUM(value) as value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . ";"; - } else { - $query = "SELECT value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . " AND `serverLogin` = '" . $serverLogin . "';"; - } - - $result = $this->mysqli->query($query); - if(!$result) { - trigger_error($this->mysqli->error); - } - - $row = $result->fetch_object(); - - $result->close(); - return $row->value; - } - - /** - * Store Stats Meta Data from the Database - */ - private function storeStatMetaData() { - $query = "SELECT * FROM `" . self::TABLE_STATMETADATA . "`;"; - $result = $this->mysqli->query($query); - if(!$result) { - trigger_error($this->mysqli->error); - } - - while($row = $result->fetch_object()) { - $this->stats[$row->name] = $row; - } - - $result->close(); - } - - /** - * Returns the Stats Id - * - * @param $statName - * @return int - */ - private function getStatId($statName) { - if(isset($this->stats[$statName])) { - $stat = $this->stats[$statName]; - return $stat->index; - } else { - return null; - } - } - - /** - * Inserts a Stat into the database - * - * @param $statName - * @param Player $player - * @param string $serverLogin - * @param $value , value to Add - * @param string $statType - */ - public function insertStat($statName, Player $player, $serverLogin, $value, $statType = self::STAT_TYPE_INT) { - $statId = $this->getStatId($statName); - - if($statId == null) { - return false; - } - - if($player->isFakePlayer()) { - return true; - } - - $query = "INSERT INTO `" . self::TABLE_STATISTICS . "` ( - `serverLogin`, - `playerId`, - `statId`, - `value` - ) VALUES ( - ?, ?, ?, ? - ) ON DUPLICATE KEY UPDATE - `value` = `value` + VALUES(`value`);"; - - $statement = $this->mysqli->prepare($query); - if($this->mysqli->error) { - trigger_error($this->mysqli->error); - return false; - } - $statement->bind_param('siii', $serverLogin, $player->index, $statId, $value); - $statement->execute(); - if($statement->error) { - trigger_error($statement->error); - $statement->close(); - return false; - } - - $statement->close(); - return true; - } - - /** - * Increments a Statistic by one - * - * @param $statName - * @param Player $playerId - * @param $serverLogin - * @return bool - */ - public function incrementStat($statName, Player $player, $serverLogin) { - return $this->insertStat($statName, $player, $serverLogin, 1); } /** @@ -177,36 +36,39 @@ class StatisticManager { * * @param string $statName * @param string $statDescription + * @return bool */ public function defineStatMetaData($statName, $statDescription = '') { - $query = "INSERT IGNORE INTO `" . self::TABLE_STATMETADATA . "` ( - `name`, - `description` + $mysqli = $this->maniaControl->database->mysqli; + $query = "INSERT IGNORE INTO `" . self::TABLE_STATMETADATA . "` ( + `name`, + `description` ) VALUES ( - ?, ? + ?, ? );"; - $statement = $this->mysqli->prepare($query); - if($this->mysqli->error) { - trigger_error($this->mysqli->error); + $statement = $mysqli->prepare($query); + if ($mysqli->error) { + trigger_error($mysqli->error); return false; } $statement->bind_param('ss', $statName, $statDescription); $statement->execute(); - if($statement->error) { + if ($statement->error) { trigger_error($statement->error); $statement->close(); return false; } - $statement->close(); + return true; } /** - * Initialize necessary database tables + * Initialize necessary Database Tables * * @return bool */ private function initTables() { + $mysqli = $this->maniaControl->database->mysqli; $query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATMETADATA . "` ( `index` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, @@ -214,43 +76,36 @@ class StatisticManager { PRIMARY KEY (`index`), UNIQUE KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Meta Data' AUTO_INCREMENT=1;"; - - $statement = $this->mysqli->prepare($query); - if($this->mysqli->error) { - trigger_error($this->mysqli->error, E_USER_ERROR); - + $statement = $mysqli->prepare($query); + if ($mysqli->error) { + trigger_error($mysqli->error, E_USER_ERROR); return false; } $statement->execute(); - if($statement->error) { + if ($statement->error) { trigger_error($statement->error, E_USER_ERROR); - return false; } $statement->close(); - + $query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` ( - `index` int(11) NOT NULL AUTO_INCREMENT, - `serverLogin` varchar(50) NOT NULL, + `statId` int(11) NOT NULL AUTO_INCREMENT, `playerId` int(11) NOT NULL, - `statId` int(11) NOT NULL, + `serverId` int(11) NOT NULL, `value` int(20) COLLATE utf8_unicode_ci NOT NULL, - PRIMARY KEY (`index`), - UNIQUE KEY `unique` (`statId`,`playerId`,`serverLogin`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;"; - - $statement = $this->mysqli->prepare($query); - if($this->mysqli->error) { - trigger_error($this->mysqli->error, E_USER_ERROR); - + UNIQUE KEY `unique` (`statId`,`playerId`,`serverId`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Data' AUTO_INCREMENT=1;"; + $statement = $mysqli->prepare($query); + if ($mysqli->error) { + trigger_error($mysqli->error, E_USER_ERROR); return false; } $statement->execute(); - if($statement->error) { + if ($statement->error) { trigger_error($statement->error, E_USER_ERROR); - return false; } $statement->close(); + return true; } -} \ No newline at end of file +}