From 31a32457c1adefd2cec0059888389e6d639c7004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Sun, 10 Nov 2013 13:58:08 +0100 Subject: [PATCH] split up tools class in FileUtil and ManialinkUtil --- application/ManiaControl_64077.log | 6 + application/core/authentication.php | 2 +- application/core/commands.php | 6 +- application/core/database.php | 2 +- application/core/fileUtil.php | 79 +++++++++++++ application/core/maniaControlClass.php | 8 +- application/core/manialinkUtil.php | 30 +++++ application/core/server.php | 2 +- application/core/tools.php | 149 ------------------------- 9 files changed, 123 insertions(+), 161 deletions(-) create mode 100644 application/ManiaControl_64077.log create mode 100644 application/core/fileUtil.php delete mode 100644 application/core/tools.php diff --git a/application/ManiaControl_64077.log b/application/ManiaControl_64077.log new file mode 100644 index 00000000..cb4a20f2 --- /dev/null +++ b/application/ManiaControl_64077.log @@ -0,0 +1,6 @@ +[10-Nov-2013 12:57:05 UTC] Loading ManiaControl v0.1... +[10-Nov-2013 12:57:05 UTC] Starting ManiaControl v0.1! +[10-Nov-2013 12:57:05 UTC] Connecting to server at 144.76.158.111:18070... +[10-Nov-2013 12:57:06 UTC] Server connection succesfully established! +[10-Nov-2013 12:57:06 UTC] Script callbacks successfully enabled. +[10-Nov-2013 12:57:06 UTC] Loading completed! diff --git a/application/core/authentication.php b/application/core/authentication.php index 265b8e99..3d2398ba 100644 --- a/application/core/authentication.php +++ b/application/core/authentication.php @@ -28,7 +28,7 @@ class Authentication { $this->mc = $mc; // Load config - $this->config = Tools::loadConfig('authentication.xml'); + $this->config = FileUtil::loadConfig('authentication.xml'); } /** diff --git a/application/core/commands.php b/application/core/commands.php index bebdf941..49b5b7bf 100644 --- a/application/core/commands.php +++ b/application/core/commands.php @@ -31,7 +31,7 @@ class Commands { $this->mc = $mc; // Load config - $this->config = Tools::loadConfig('commands.xml'); + $this->config = FileUtil::loadConfig('commands.xml'); // Register for callbacks $this->mc->callbacks->registerCallbackHandler(Callbacks::CB_MC_5_SECOND, $this, 'each5Seconds'); @@ -531,7 +531,7 @@ class Commands { $title = strtolower(substr($serverInfo['TitleId'], 0, 2)); // Check if map exists $url = 'http://' . $title . '.mania-exchange.com/api/tracks/get_track_info/id/' . $params[1] . '?format=json'; - $mapInfo = Tools::loadFile($url); + $mapInfo = FileUtil::loadFile($url); if (!$mapInfo || strlen($mapInfo) <= 0) { // Invalid id $this->mc->chat->sendError('Invalid MX-Id!', $login); @@ -539,7 +539,7 @@ class Commands { } $mapInfo = json_decode($mapInfo, true); $url = 'http://' . $title . '.mania-exchange.com/tracks/download/' . $params[1]; - $file = Tools::loadFile($url); + $file = FileUtil::loadFile($url); if (!$file) { // Download error $this->mc->chat->sendError('Download failed!', $login); diff --git a/application/core/database.php b/application/core/database.php index 2c483830..a0d16195 100644 --- a/application/core/database.php +++ b/application/core/database.php @@ -32,7 +32,7 @@ class Database { $this->maniaControl = $maniaControl; // Load config - $this->config = Tools::loadConfig('database.xml'); + $this->config = FileUtil::loadConfig('database.xml'); // Get mysql server information $host = $this->config->xpath('host'); diff --git a/application/core/fileUtil.php b/application/core/fileUtil.php new file mode 100644 index 00000000..78fda40e --- /dev/null +++ b/application/core/fileUtil.php @@ -0,0 +1,79 @@ + false); + while (!feof($fsock) && !$info['timed_out']) { + $buffer .= fread($fsock, 1024); + $info = stream_get_meta_data($fsock); + } + fclose($fsock); + + if ($info['timed_out'] || !$buffer) { + return null; + } + if (substr($buffer, 9, 3) != "200") { + return null; + } + + $result = explode("\r\n\r\n", $buffer, 2); + + if (count($result) < 2) { + return null; + } + + return $result[1]; + } + + /** + * Load config xml-file + * + * @param string $fileName + * @return \SimpleXMLElement + */ + public static function loadConfig($fileName) { + if (!$fileName) { + return null; + } + $fileLocation = ManiaControlDir . '/configs/' . $fileName; + if (!file_exists($fileLocation)) { + trigger_error("Config file doesn't exist! ({$fileName})"); + return null; + } + return simplexml_load_file($fileLocation); + } +} + +?> diff --git a/application/core/maniaControlClass.php b/application/core/maniaControlClass.php index 8e08e462..4d25e485 100644 --- a/application/core/maniaControlClass.php +++ b/application/core/maniaControlClass.php @@ -10,12 +10,12 @@ require_once __DIR__ . '/callbacks.php'; require_once __DIR__ . '/chat.php'; require_once __DIR__ . '/commands.php'; require_once __DIR__ . '/database.php'; +require_once __DIR__ . '/fileUtil.php'; require_once __DIR__ . '/manialinkIdHandler.php'; require_once __DIR__ . '/playerHandler.php'; require_once __DIR__ . '/pluginHandler.php'; require_once __DIR__ . '/server.php'; require_once __DIR__ . '/settingManager.php'; -require_once __DIR__ . '/tools.php'; list($endiantest) = array_values(unpack('L1L', pack('V', 1))); if ($endiantest == 1) { require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php'; @@ -61,7 +61,7 @@ class ManiaControl { * Construct ManiaControl */ public function __construct() { - $this->config = Tools::loadConfig('core.xml'); + $this->config = FileUtil::loadConfig('core.xml'); $this->database = new Database($this); $this->settingManager = new SettingManager($this); $this->chat = new Chat($this); @@ -160,10 +160,6 @@ class ManiaControl { * Connect to ManiaPlanet server */ private function connect() { - $enable = $this->server->config->xpath('enable'); - $enable = Tools::toBool($enable[0]); - if (!$enable) return; - // Load remote client $this->client = new \IXR_ClientMulticall_Gbx(); diff --git a/application/core/manialinkUtil.php b/application/core/manialinkUtil.php index 4ed9aab1..ec80b9bd 100644 --- a/application/core/manialinkUtil.php +++ b/application/core/manialinkUtil.php @@ -7,6 +7,36 @@ */ class ManialinkUtil { + /** + * Send the given manialink to players + * + * @param string $manialink + * @param array $logins + * @return bool + */ + public static function sendManialinkPage($client, $manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) { + if (!$client || !$manialinkText) { + return false; + } + if (!$logins) { + return $client->query('SendDisplayManialinkPage', $manialinkText, $timeout, $hideOnClick); + } + if (is_string($logins)) { + return $client->query('SendDisplayManialinkPageToLogin', $logins, $manialinkText, $timeout, $hideOnClick); + } + if (is_array($logins)) { + $success = true; + foreach ($logins as $login) { + $subSuccess = $client->query('SendDisplayManialinkPageToLogin', $login, $manialinkText, $timeout, $hideOnClick); + if (!$subSuccess) { + $success = false; + } + } + return $success; + } + return false; + } + /** * Build new simple xml element * diff --git a/application/core/server.php b/application/core/server.php index 785c85a2..e4721bc9 100644 --- a/application/core/server.php +++ b/application/core/server.php @@ -31,7 +31,7 @@ class Server { $this->mc = $mc; // Load config - $this->config = Tools::loadConfig('server.xml'); + $this->config = FileUtil::loadConfig('server.xml'); // Register for callbacks $this->mc->callbacks->registerCallbackHandler(Callbacks::CB_MC_1_SECOND, $this, 'eachSecond'); diff --git a/application/core/tools.php b/application/core/tools.php deleted file mode 100644 index bfe7a1b4..00000000 --- a/application/core/tools.php +++ /dev/null @@ -1,149 +0,0 @@ -xpath('//' . $setting); - if (empty($settings)) { - return false; - } - else { - foreach ($settings as $setting) { - return self::toBool((string) $setting[0]); - } - } - } - - /** - * Load a remote file - * - * @param string $url - * @return string || null - */ - public static function loadFile($url) { - if (!$url) return false; - $urlData = parse_url($url); - $port = (isset($urlData['port']) ? $urlData['port'] : 80); - - $fsock = fsockopen($urlData['host'], $port); - stream_set_timeout($fsock, 3); - - $query = 'GET ' . $urlData['path'] . ' HTTP/1.0' . PHP_EOL; - $query .= 'Host: ' . $urlData['host'] . PHP_EOL; - $query .= 'Content-Type: UTF-8' . PHP_EOL; - $query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL; - $query .= PHP_EOL; - - fwrite($fsock, $query); - - $buffer = ''; - $info = array('timed_out' => false); - while (!feof($fsock) && !$info['timed_out']) { - $buffer .= fread($fsock, 1024); - $info = stream_get_meta_data($fsock); - } - fclose($fsock); - - if ($info['timed_out'] || !$buffer) { - return null; - } - if (substr($buffer, 9, 3) != "200") { - return null; - } - - $result = explode("\r\n\r\n", $buffer, 2); - - if (count($result) < 2) { - return null; - } - - return $result[1]; - } - - /** - * Convert given data to real boolean - * - * @param - * mixed data - */ - public static function toBool($var) { - if ($var === true) return true; - if ($var === false) return false; - if ($var === null) return false; - if (is_object($var)) { - $var = (string) $var; - } - if (is_int($var)) { - return ($var > 0); - } - else if (is_string($var)) { - $text = strtolower($var); - if ($text === 'true' || $text === 'yes') { - return true; - } - else if ($text === 'false' || $text === 'no') { - return false; - } - else { - return ((int) $text > 0); - } - } - else { - return (bool) $var; - } - } - - /** - * Load config xml-file - * - * @param string $fileName - * @return \SimpleXMLElement - */ - public static function loadConfig($fileName) { - // Load config file from configs folder - $fileLocation = ManiaControlDir . '/configs/' . $fileName; - if (!file_exists($fileLocation)) { - trigger_error("Config file doesn't exist! (" . $fileName . ")", E_USER_ERROR); - } - return simplexml_load_file($fileLocation); - } - - /** - * Send the given manialink to players - * - * @param string $manialink - * @param array $logins - */ - public static function sendManialinkPage($client, $manialink, $logins = null, $timeout = 0, $hideOnClick = false) { - if (!$client || !$manialink) return; - if (!$logins) { - // Send manialink to all players - $client->query('SendDisplayManialinkPage', $manialink, $timeout, $hideOnClick); - } - else if (is_array($logins)) { - // Send manialink to players - foreach ($logins as $login) { - $client->query('SendDisplayManialinkPageToLogin', $login, $manialink, $timeout, $hideOnClick); - } - } - else if (is_string($logins)) { - // Send manialink to player - $client->query('SendDisplayManialinkPageToLogin', $logins, $manialink, $timeout, $hideOnClick); - } - } -} - -?>