split up tools class in FileUtil and ManialinkUtil

This commit is contained in:
Steffen Schröder 2013-11-10 13:58:08 +01:00
parent 5c8160bec0
commit 31a32457c1
9 changed files with 123 additions and 161 deletions

View File

@ -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!

View File

@ -28,7 +28,7 @@ class Authentication {
$this->mc = $mc; $this->mc = $mc;
// Load config // Load config
$this->config = Tools::loadConfig('authentication.xml'); $this->config = FileUtil::loadConfig('authentication.xml');
} }
/** /**

View File

@ -31,7 +31,7 @@ class Commands {
$this->mc = $mc; $this->mc = $mc;
// Load config // Load config
$this->config = Tools::loadConfig('commands.xml'); $this->config = FileUtil::loadConfig('commands.xml');
// Register for callbacks // Register for callbacks
$this->mc->callbacks->registerCallbackHandler(Callbacks::CB_MC_5_SECOND, $this, 'each5Seconds'); $this->mc->callbacks->registerCallbackHandler(Callbacks::CB_MC_5_SECOND, $this, 'each5Seconds');
@ -531,7 +531,7 @@ class Commands {
$title = strtolower(substr($serverInfo['TitleId'], 0, 2)); $title = strtolower(substr($serverInfo['TitleId'], 0, 2));
// Check if map exists // Check if map exists
$url = 'http://' . $title . '.mania-exchange.com/api/tracks/get_track_info/id/' . $params[1] . '?format=json'; $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) { if (!$mapInfo || strlen($mapInfo) <= 0) {
// Invalid id // Invalid id
$this->mc->chat->sendError('Invalid MX-Id!', $login); $this->mc->chat->sendError('Invalid MX-Id!', $login);
@ -539,7 +539,7 @@ class Commands {
} }
$mapInfo = json_decode($mapInfo, true); $mapInfo = json_decode($mapInfo, true);
$url = 'http://' . $title . '.mania-exchange.com/tracks/download/' . $params[1]; $url = 'http://' . $title . '.mania-exchange.com/tracks/download/' . $params[1];
$file = Tools::loadFile($url); $file = FileUtil::loadFile($url);
if (!$file) { if (!$file) {
// Download error // Download error
$this->mc->chat->sendError('Download failed!', $login); $this->mc->chat->sendError('Download failed!', $login);

View File

@ -32,7 +32,7 @@ class Database {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
// Load config // Load config
$this->config = Tools::loadConfig('database.xml'); $this->config = FileUtil::loadConfig('database.xml');
// Get mysql server information // Get mysql server information
$host = $this->config->xpath('host'); $host = $this->config->xpath('host');

View File

@ -0,0 +1,79 @@
<?php
namespace ManiaControl;
/**
* File utility class
*
* @author steeffeen & kremsy
*/
class FileUtil {
/**
* Load a remote file
*
* @param string $url
* @return string || null
*/
public static function loadFile($url) {
if (!$url) {
return null;
}
$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];
}
/**
* 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);
}
}
?>

View File

@ -10,12 +10,12 @@ require_once __DIR__ . '/callbacks.php';
require_once __DIR__ . '/chat.php'; require_once __DIR__ . '/chat.php';
require_once __DIR__ . '/commands.php'; require_once __DIR__ . '/commands.php';
require_once __DIR__ . '/database.php'; require_once __DIR__ . '/database.php';
require_once __DIR__ . '/fileUtil.php';
require_once __DIR__ . '/manialinkIdHandler.php'; require_once __DIR__ . '/manialinkIdHandler.php';
require_once __DIR__ . '/playerHandler.php'; require_once __DIR__ . '/playerHandler.php';
require_once __DIR__ . '/pluginHandler.php'; require_once __DIR__ . '/pluginHandler.php';
require_once __DIR__ . '/server.php'; require_once __DIR__ . '/server.php';
require_once __DIR__ . '/settingManager.php'; require_once __DIR__ . '/settingManager.php';
require_once __DIR__ . '/tools.php';
list($endiantest) = array_values(unpack('L1L', pack('V', 1))); list($endiantest) = array_values(unpack('L1L', pack('V', 1)));
if ($endiantest == 1) { if ($endiantest == 1) {
require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php'; require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php';
@ -61,7 +61,7 @@ class ManiaControl {
* Construct ManiaControl * Construct ManiaControl
*/ */
public function __construct() { public function __construct() {
$this->config = Tools::loadConfig('core.xml'); $this->config = FileUtil::loadConfig('core.xml');
$this->database = new Database($this); $this->database = new Database($this);
$this->settingManager = new SettingManager($this); $this->settingManager = new SettingManager($this);
$this->chat = new Chat($this); $this->chat = new Chat($this);
@ -160,10 +160,6 @@ class ManiaControl {
* Connect to ManiaPlanet server * Connect to ManiaPlanet server
*/ */
private function connect() { private function connect() {
$enable = $this->server->config->xpath('enable');
$enable = Tools::toBool($enable[0]);
if (!$enable) return;
// Load remote client // Load remote client
$this->client = new \IXR_ClientMulticall_Gbx(); $this->client = new \IXR_ClientMulticall_Gbx();

View File

@ -7,6 +7,36 @@
*/ */
class ManialinkUtil { 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 * Build new simple xml element
* *

View File

@ -31,7 +31,7 @@ class Server {
$this->mc = $mc; $this->mc = $mc;
// Load config // Load config
$this->config = Tools::loadConfig('server.xml'); $this->config = FileUtil::loadConfig('server.xml');
// Register for callbacks // Register for callbacks
$this->mc->callbacks->registerCallbackHandler(Callbacks::CB_MC_1_SECOND, $this, 'eachSecond'); $this->mc->callbacks->registerCallbackHandler(Callbacks::CB_MC_1_SECOND, $this, 'eachSecond');

View File

@ -1,149 +0,0 @@
<?php
namespace ManiaControl;
/**
* Class for basic tools
*
* @author steeffeen
*/
class Tools {
/**
* Check if the given setting is enabled
*
* @param simple_xml_element $config
* @param string $setting
*/
public static function checkSetting($config, $setting) {
$settings = $config->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);
}
}
}
?>