removed 'application' folder to have everything in the root directory

This commit is contained in:
Steffen Schröder
2014-09-29 18:20:09 +02:00
parent 1569fd5488
commit 9642433363
284 changed files with 4 additions and 50 deletions

30
core/Utils/ClassUtil.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace ManiaControl\Utils;
/**
* Utility Class offering Methods related to Classes
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
abstract class ClassUtil {
/**
* Get the Class Name of the given Object
*
* @param mixed $object
* @return string
*/
public static function getClass($object) {
if (is_object($object)) {
return get_class($object);
}
if (is_string($object)) {
return $object;
}
trigger_error("Invalid class param: '" . print_r($object, true) . "'!");
return (string)$object;
}
}

57
core/Utils/ColorUtil.php Normal file
View File

@ -0,0 +1,57 @@
<?php
namespace ManiaControl\Utils;
/**
* Utility Class offering Methods to convert and use ManiaPlanet Colors
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
abstract class ColorUtil {
/**
* Convert the given float value to a color code from red to green
*
* @param float $value
* @return string
*/
public static function floatToStatusColor($value) {
$value = floatval($value);
$red = 1.;
$green = 1.;
if ($value < 0.5) {
$green = $value * 2.;
}
if ($value > 0.5) {
$red = 2. * (1. - $value);
}
$red = ColorUtil::floatToCode($red);
$green = ColorUtil::floatToCode($green);
return $red . $green . '0';
}
/**
* Get hex color representation of the float
*
* @param float $value
* @return string
*/
public static function floatToCode($value) {
$value = floatval($value);
if ($value < 0.) {
$value = 0.;
}
if ($value > 1.) {
$value = 1.;
}
$value *= 15.;
$value = (int)round($value);
if ($value < 10) {
return (string)$value;
}
$codes = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f');
return $codes[$value];
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace ManiaControl\Utils;
/**
* Command Line Helper Class
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class CommandLineHelper {
/**
* Get the command line parameter value with the given name
*
* @param string $paramName
* @return string
*/
public static function getParameter($paramName) {
$paramName = (string)$paramName;
$params = self::getAllParameters();
foreach ($params as $param) {
$parts = explode('=', $param, 2);
if (count($parts) < 2) {
continue;
}
if ($parts[0] !== $paramName) {
continue;
}
return $parts[1];
}
return null;
}
/**
* Get all command line parameters
*
* @return array
*/
public static function getAllParameters() {
global $argv;
return (array)$argv;
}
}

196
core/Utils/Formatter.php Normal file
View File

@ -0,0 +1,196 @@
<?php
namespace ManiaControl\Utils;
use ManiaControl\Logger;
/**
* Class offering Methods to format Texts and Values
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
abstract class Formatter {
/**
* Return the given Text with Escaping around it
*
* @param string $text
* @return string
*/
public static function escapeText($text) {
return '$<' . $text . '$>';
}
/**
* Format the given Time (in Milliseconds)
*
* @param int $time
* @return string
*/
public static function formatTime($time) {
// TODO: use gmdate()
$time = (int)$time;
$milliseconds = $time % 1000;
$seconds = floor($time / 1000);
$minutes = floor($seconds / 60);
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
$seconds -= $hours * 60 + $minutes * 60;
$format = ($hours > 0 ? $hours . ':' : '');
$format .= ($hours > 0 && $minutes < 10 ? '0' : '') . $minutes . ':';
$format .= ($seconds < 10 ? '0' : '') . $seconds . ':';
$format .= ($milliseconds < 100 ? '0' : '') . ($milliseconds < 10 ? '0' : '') . $milliseconds;
return $format;
}
/**
* Format an elapsed time String (2 days ago...) by a given timestamp
*
* @param int $ptime
* @return string
*/
public static function time_elapsed_string($ptime) {
// TODO: refactor code: camelCase!
$etime = time() - $ptime;
if ($etime < 1) {
return '0 seconds';
}
$a = array(12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second');
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
}
}
return '';
}
/**
* Format the given Time (Seconds) to hh:mm:ss
*
* @param int $seconds
* @return string
*/
public static function formatTimeH($seconds) {
return gmdate('H:i:s', $seconds);
}
/**
* Convert the given Time (Seconds) to MySQL Timestamp
*
* @param int $seconds
* @return string
*/
public static function formatTimestamp($seconds) {
return date('Y-m-d H:i:s', $seconds);
}
/**
* Remove possibly dangerous Codes
* (Dangerous Codes are Links and Formats that might screw up the following Styling)
*
* @param string $string
* @return string
*/
public static function stripDirtyCodes($string) {
$string = self::stripLinks($string);
$string = preg_replace('/(?<!\$)((?:\$\$)*)\$[ow<>]/iu', '$1', $string);
return $string;
}
/**
* Remove Links from the String
*
* @param string $string
* @return string
*/
public static function stripLinks($string) {
return preg_replace('/(?<!\$)((?:\$\$)*)\$[hlp](?:\[.*?\])?(.*?)(?:\$[hlp]|(\$z)|$)/iu', '$1$2$3', $string);
}
/**
* Remove all Codes from the String
*
* @param string $string
* @return string
*/
public static function stripCodes($string) {
$string = self::stripLinks($string);
$string = self::stripColors($string);
$string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string);
return $string;
}
/**
* Remove Colors from the String
*
* @param string $string
* @return string
*/
public static function stripColors($string) {
return preg_replace('/(?<!\$)((?:\$\$)*)\$(?:g|[0-9a-f][^\$]{0,2})/iu', '$1', $string);
}
/**
* Map Country Names to 3-letter Nation Abbreviations
* Created by Xymph
* Based on http://en.wikipedia.org/wiki/List_of_IOC_country_codes
* See also http://en.wikipedia.org/wiki/Comparison_of_IOC,_FIFA,_and_ISO_3166_country_codes
*
* @param string $country
* @return string
*/
public static function mapCountry($country) {
$nations = array('Afghanistan' => 'AFG', 'Albania' => 'ALB', 'Algeria' => 'ALG', 'Andorra' => 'AND', 'Angola' => 'ANG', 'Argentina' => 'ARG', 'Armenia' => 'ARM', 'Aruba' => 'ARU', 'Australia' => 'AUS', 'Austria' => 'AUT', 'Azerbaijan' => 'AZE', 'Bahamas' => 'BAH', 'Bahrain' => 'BRN', 'Bangladesh' => 'BAN', 'Barbados' => 'BAR', 'Belarus' => 'BLR', 'Belgium' => 'BEL', 'Belize' => 'BIZ', 'Benin' => 'BEN', 'Bermuda' => 'BER', 'Bhutan' => 'BHU', 'Bolivia' => 'BOL', 'Bosnia&Herzegovina' => 'BIH', 'Botswana' => 'BOT', 'Brazil' => 'BRA', 'Brunei' => 'BRU', 'Bulgaria' => 'BUL', 'Burkina Faso' => 'BUR', 'Burundi' => 'BDI', 'Cambodia' => 'CAM', 'Cameroon' => 'CAR', // actually CMR
'Canada' => 'CAN', 'Cape Verde' => 'CPV', 'Central African Republic' => 'CAF', 'Chad' => 'CHA', 'Chile' => 'CHI', 'China' => 'CHN', 'Chinese Taipei' => 'TPE', 'Colombia' => 'COL', 'Congo' => 'CGO', 'Costa Rica' => 'CRC', 'Croatia' => 'CRO', 'Cuba' => 'CUB', 'Cyprus' => 'CYP', 'Czech Republic' => 'CZE', 'Czech republic' => 'CZE', 'DR Congo' => 'COD', 'Denmark' => 'DEN', 'Djibouti' => 'DJI', 'Dominica' => 'DMA', 'Dominican Republic' => 'DOM', 'Ecuador' => 'ECU', 'Egypt' => 'EGY', 'El Salvador' => 'ESA', 'Eritrea' => 'ERI', 'Estonia' => 'EST', 'Ethiopia' => 'ETH', 'Fiji' => 'FIJ', 'Finland' => 'FIN', 'France' => 'FRA', 'Gabon' => 'GAB', 'Gambia' => 'GAM', 'Georgia' => 'GEO', 'Germany' => 'GER', 'Ghana' => 'GHA', 'Greece' => 'GRE', 'Grenada' => 'GRN', 'Guam' => 'GUM', 'Guatemala' => 'GUA', 'Guinea' => 'GUI', 'Guinea-Bissau' => 'GBS', 'Guyana' => 'GUY', 'Haiti' => 'HAI', 'Honduras' => 'HON', 'Hong Kong' => 'HKG', 'Hungary' => 'HUN', 'Iceland' => 'ISL', 'India' => 'IND', 'Indonesia' => 'INA', 'Iran' => 'IRI', 'Iraq' => 'IRQ', 'Ireland' => 'IRL', 'Israel' => 'ISR', 'Italy' => 'ITA', 'Ivory Coast' => 'CIV', 'Jamaica' => 'JAM', 'Japan' => 'JPN', 'Jordan' => 'JOR', 'Kazakhstan' => 'KAZ', 'Kenya' => 'KEN', 'Kiribati' => 'KIR', 'Korea' => 'KOR', 'Kuwait' => 'KUW', 'Kyrgyzstan' => 'KGZ', 'Laos' => 'LAO', 'Latvia' => 'LAT', 'Lebanon' => 'LIB', 'Lesotho' => 'LES', 'Liberia' => 'LBR', 'Libya' => 'LBA', 'Liechtenstein' => 'LIE', 'Lithuania' => 'LTU', 'Luxembourg' => 'LUX', 'Macedonia' => 'MKD', 'Malawi' => 'MAW', 'Malaysia' => 'MAS', 'Mali' => 'MLI', 'Malta' => 'MLT', 'Mauritania' => 'MTN', 'Mauritius' => 'MRI', 'Mexico' => 'MEX', 'Moldova' => 'MDA', 'Monaco' => 'MON', 'Mongolia' => 'MGL', 'Montenegro' => 'MNE', 'Morocco' => 'MAR', 'Mozambique' => 'MOZ', 'Myanmar' => 'MYA', 'Namibia' => 'NAM', 'Nauru' => 'NRU', 'Nepal' => 'NEP', 'Netherlands' => 'NED', 'New Zealand' => 'NZL', 'Nicaragua' => 'NCA', 'Niger' => 'NIG', 'Nigeria' => 'NGR', 'Norway' => 'NOR', 'Oman' => 'OMA', 'Other Countries' => 'OTH', 'Pakistan' => 'PAK', 'Palau' => 'PLW', 'Palestine' => 'PLE', 'Panama' => 'PAN', 'Paraguay' => 'PAR', 'Peru' => 'PER', 'Philippines' => 'PHI', 'Poland' => 'POL', 'Portugal' => 'POR', 'Puerto Rico' => 'PUR', 'Qatar' => 'QAT', 'Romania' => 'ROM', // actually ROU
'Russia' => 'RUS', 'Rwanda' => 'RWA', 'Samoa' => 'SAM', 'San Marino' => 'SMR', 'Saudi Arabia' => 'KSA', 'Senegal' => 'SEN', 'Serbia' => 'SCG', // actually SRB
'Sierra Leone' => 'SLE', 'Singapore' => 'SIN', 'Slovakia' => 'SVK', 'Slovenia' => 'SLO', 'Somalia' => 'SOM', 'South Africa' => 'RSA', 'Spain' => 'ESP', 'Sri Lanka' => 'SRI', 'Sudan' => 'SUD', 'Suriname' => 'SUR', 'Swaziland' => 'SWZ', 'Sweden' => 'SWE', 'Switzerland' => 'SUI', 'Syria' => 'SYR', 'Taiwan' => 'TWN', 'Tajikistan' => 'TJK', 'Tanzania' => 'TAN', 'Thailand' => 'THA', 'Togo' => 'TOG', 'Tonga' => 'TGA', 'Trinidad and Tobago' => 'TRI', 'Tunisia' => 'TUN', 'Turkey' => 'TUR', 'Turkmenistan' => 'TKM', 'Tuvalu' => 'TUV', 'Uganda' => 'UGA', 'Ukraine' => 'UKR', 'United Arab Emirates' => 'UAE', 'United Kingdom' => 'GBR', 'United States of America' => 'USA', 'Uruguay' => 'URU', 'Uzbekistan' => 'UZB', 'Vanuatu' => 'VAN', 'Venezuela' => 'VEN', 'Vietnam' => 'VIE', 'Yemen' => 'YEM', 'Zambia' => 'ZAM', 'Zimbabwe' => 'ZIM');
if (array_key_exists($country, $nations)) {
return $nations[$country];
}
if ($country) {
Logger::logWarning("Couldn't map Country: '{$country}'!");
}
return 'OTH';
}
/**
* Parse the given Value into a Bool
*
* @param mixed $value
* @return bool
*/
public static function parseBoolean($value) {
if (is_string($value)) {
$value = strtolower($value);
}
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
/**
* Make sure the given Text is encoded in UTF-8
*
* @param string $text
* @return string
*/
public static function utf8($text) {
if (is_array($text)) {
$newArray = array();
foreach ($text as $key => $value) {
if (is_string($value)) {
$newArray[$key] = self::utf8($value);
} else {
$newArray[$key] = $value;
}
}
return $newArray;
}
return mb_convert_encoding($text, 'UTF-8', 'UTF-8');
}
}

188
core/Utils/SystemUtil.php Normal file
View File

@ -0,0 +1,188 @@
<?php
namespace ManiaControl\Utils;
use ManiaControl\Logger;
/**
* System Utility Class
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class SystemUtil {
/*
* Constants
*/
const OS_UNIX = 'Unix';
const OS_WIN = 'Windows';
const MIN_PHP_VERSION = '5.4';
/**
* Get whether ManiaControl is running on Windows
*
* @return bool
*/
public static function isWindows() {
return (self::getOS() === self::OS_WIN);
}
/**
* Get the Operating System on which ManiaControl is running
*
* @return string
*/
public static function getOS() {
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
return self::OS_WIN;
}
return self::OS_UNIX;
}
/**
* Check for the requirements to run ManiaControl
*/
public static function checkRequirements() {
$success = true;
// Check for min PHP version
$phpVersion = phpversion();
$message = 'Checking for minimum required PHP-Version ' . self::MIN_PHP_VERSION . ' ... ';
if ($phpVersion < self::MIN_PHP_VERSION) {
Logger::log($message . $phpVersion . ' TOO OLD VERSION!');
Logger::log(' -- Make sure that you install at least PHP ' . self::MIN_PHP_VERSION . '!');
$success = false;
} else {
Logger::log($message . self::MIN_PHP_VERSION . ' OK!');
}
// Check for MySQLi
$message = 'Checking for installed MySQLi ... ';
if (!extension_loaded('mysqli')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have MySQLi installed! Check: http://www.php.net/manual/en/mysqli.installation.php");
$success = false;
} else {
Logger::log($message . 'FOUND!');
}
// Check for cURL
$message = 'Checking for installed cURL ... ';
if (!extension_loaded('curl')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have cURL installed! Check: http://www.php.net/manual/en/curl.installation.php");
$success = false;
} else {
Logger::log($message . 'FOUND!');
}
if (!$success) {
// Missing requirements
self::quit();
}
}
/**
* Stop ManiaControl immediately
*
* @param string $message
* @param bool $errorPrefix
*/
public static function quit($message = null, $errorPrefix = false) {
if ($message) {
if ($errorPrefix) {
Logger::logError($message);
} else {
Logger::log($message);
}
}
exit;
}
/**
* Restart ManiaControl immediately
*/
public static function restart() {
if (SystemUtil::isUnix()) {
self::restartUnix();
} else {
self::restartWindows();
}
}
/**
* Get whether ManiaControl is running on Unix
*
* @return bool
*/
public static function isUnix() {
return (self::getOS() === self::OS_UNIX);
}
/**
* Perform restart on Unix
*/
private static function restartUnix() {
if (!SystemUtil::checkFunctionAvailability('exec')) {
Logger::log("Can't restart ManiaControl because the function 'exec' is disabled!");
return;
}
$fileName = null;
if ($scriptName = CommandLineHelper::getParameter('-sh')) {
$fileName = $scriptName;
} else {
$fileName = 'ManiaControl.sh';
}
$filePath = MANIACONTROL_PATH . $fileName;
if (!is_readable($filePath)) {
Logger::log("Can't restart ManiaControl because the file '{$fileName}' doesn't exist or isn't readable!");
return;
}
$command = 'sh ' . escapeshellarg($filePath) . ' > /dev/null &';
exec($command);
}
/**
* Check whether the given function is available
*
* @param string $functionName
* @return bool
*/
public static function checkFunctionAvailability($functionName) {
return (function_exists($functionName) && !in_array($functionName, self::getDisabledFunctions()));
}
/**
* Get the array of disabled functions
*
* @return array
*/
protected static function getDisabledFunctions() {
$disabledText = ini_get('disable_functions');
return explode(',', $disabledText);
}
/**
* Perform restart on Windows
*/
private static function restartWindows() {
if (!SystemUtil::checkFunctionAvailability('system')) {
Logger::log("Can't restart ManiaControl because the function 'system' is disabled!");
return;
}
$fileName = null;
if ($scriptName = CommandLineHelper::getParameter('-bat')) {
$fileName = $scriptName;
} else {
$fileName = 'ManiaControl.bat';
}
$filePath = MANIACONTROL_PATH . $fileName;
if (!is_readable($filePath)) {
Logger::log("Can't restart ManiaControl because the file '{$fileName}' doesn't exist or isn't readable!");
return;
}
$command = escapeshellarg($filePath);
system($command); // TODO: windows stops here as long as controller is running
}
}

96
core/Utils/WebReader.php Normal file
View File

@ -0,0 +1,96 @@
<?php
namespace ManiaControl\Utils;
use cURL\Request;
use cURL\Response;
use ManiaControl\ManiaControl;
/**
* Reader Utility Class for efficient Web Requests
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
abstract class WebReader {
/**
* Load a URL via GET
*
* @param string $url
* @param callable $function
* @return Response
*/
public static function getUrl($url, callable $function = null) {
$request = static::newRequest($url);
$response = $request->send();
if ($function) {
static::performCallback($response, $function);
}
return $response;
}
/**
* @deprecated
* @see WebReader::getUrl()
*/
public static function loadUrl($url, callable $function = null) {
if ($function) {
return static::getUrl($url, $function);
}
return static::getUrl($url);
}
/**
* Create a new cURL Request for the given URL
*
* @param string $url
* @return Request
*/
protected static function newRequest($url) {
$request = new Request($url);
$options = $request->getOptions();
$options->set(CURLOPT_TIMEOUT, 5) // timeout
->set(CURLOPT_HEADER, false) // don't display response header
->set(CURLOPT_CRLF, true) // linux line feed
->set(CURLOPT_ENCODING, '') // accept encoding
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent
->set(CURLOPT_RETURNTRANSFER, true) // return instead of output content
->set(CURLOPT_AUTOREFERER, true); // follow redirects
return $request;
}
/**
* Perform the given callback function with the response
*
* @param Response $response
* @param callable $function
*/
protected static function performCallback(Response $response, callable $function) {
$content = $response->getContent();
$error = $response->getError()->getMessage();
call_user_func($function, $content, $error);
}
/**
* Load a URL via POST
*
* @param string $url
* @param string $content
* @param callable $function
* @return Response
*/
public static function postUrl($url, $content = null, callable $function = null) {
$request = static::newRequest($url);
$request->getOptions()->set(CURLOPT_POST, true); // post method
if ($content) {
$request->getOptions()->set(CURLOPT_POSTFIELDS, $content); // post content field
}
$response = $request->send();
if ($function) {
static::performCallback($response, $function);
}
return $response;
}
}