2013-11-10 13:58:08 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-07 00:26:32 +01:00
|
|
|
namespace ManiaControl\Files;
|
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
2014-03-31 21:04:15 +02:00
|
|
|
use ManiaControl\Formatter;
|
2013-11-10 13:58:08 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-12 12:14:37 +02:00
|
|
|
* Files Utility Class
|
2013-11-10 13:58:08 +01:00
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
2014-04-12 12:14:37 +02:00
|
|
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-10 13:58:08 +01:00
|
|
|
*/
|
2013-11-27 02:42:39 +01:00
|
|
|
abstract class FileUtil {
|
2014-01-06 14:22:48 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
/**
|
|
|
|
* Load a remote file
|
|
|
|
*
|
|
|
|
* @param string $url
|
2013-12-27 21:10:53 +01:00
|
|
|
* @param string $contentType
|
2013-12-23 16:14:03 +01:00
|
|
|
* @return string || null
|
|
|
|
*/
|
2013-12-27 21:10:53 +01:00
|
|
|
public static function loadFile($url, $contentType = 'UTF-8') {
|
2013-12-23 16:14:03 +01:00
|
|
|
if (!$url) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-07 12:30:53 +01:00
|
|
|
$urlData = parse_url($url);
|
|
|
|
$port = (isset($urlData['port']) ? $urlData['port'] : 80);
|
|
|
|
$urlQuery = isset($urlData['query']) ? "?" . $urlData['query'] : "";
|
2014-02-07 00:26:32 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
$fsock = fsockopen($urlData['host'], $port);
|
|
|
|
stream_set_timeout($fsock, 3);
|
2014-02-07 00:26:32 +01:00
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
$query = 'GET ' . $urlData['path'] . $urlQuery . ' HTTP/1.0' . PHP_EOL;
|
2013-12-23 16:14:03 +01:00
|
|
|
$query .= 'Host: ' . $urlData['host'] . PHP_EOL;
|
2013-12-27 21:10:53 +01:00
|
|
|
$query .= 'Content-Type: ' . $contentType . PHP_EOL;
|
2013-12-23 16:14:03 +01:00
|
|
|
$query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL;
|
|
|
|
$query .= PHP_EOL;
|
2014-01-08 21:52:42 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
fwrite($fsock, $query);
|
2014-02-07 00:26:32 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
$buffer = '';
|
2014-02-07 00:26:32 +01:00
|
|
|
$info = array('timed_out' => false);
|
|
|
|
while(!feof($fsock) && !$info['timed_out']) {
|
2013-12-23 16:14:03 +01:00
|
|
|
$buffer .= fread($fsock, 1024);
|
|
|
|
$info = stream_get_meta_data($fsock);
|
|
|
|
}
|
|
|
|
fclose($fsock);
|
2014-02-07 00:26:32 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
if ($info['timed_out'] || !$buffer) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (substr($buffer, 9, 3) != "200") {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-07 00:26:32 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
$result = explode("\r\n\r\n", $buffer, 2);
|
2014-02-07 00:26:32 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
if (count($result) < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-07 00:26:32 +01:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
return $result[1];
|
|
|
|
}
|
2014-01-06 14:22:48 +01:00
|
|
|
|
2013-11-10 13:58:08 +01:00
|
|
|
/**
|
|
|
|
* Load config xml-file
|
|
|
|
*
|
2014-01-06 14:22:48 +01:00
|
|
|
* @param string $fileName
|
2013-11-10 13:58:08 +01:00
|
|
|
* @return \SimpleXMLElement
|
|
|
|
*/
|
|
|
|
public static function loadConfig($fileName) {
|
|
|
|
$fileLocation = ManiaControlDir . '/configs/' . $fileName;
|
|
|
|
if (!file_exists($fileLocation)) {
|
|
|
|
trigger_error("Config file doesn't exist! ({$fileName})");
|
|
|
|
return null;
|
|
|
|
}
|
2013-11-24 23:55:54 +01:00
|
|
|
if (!is_readable($fileLocation)) {
|
2014-05-01 20:15:58 +02:00
|
|
|
trigger_error("Config file isn't readable! Please check your file permissions. ({$fileName})");
|
2013-11-24 23:55:54 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-10 13:58:08 +01:00
|
|
|
return simplexml_load_file($fileLocation);
|
|
|
|
}
|
2013-11-27 02:42:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return file name cleared from special characters
|
|
|
|
*
|
2014-01-06 14:22:48 +01:00
|
|
|
* @param string $fileName
|
2013-11-27 02:42:39 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getClearedFileName($fileName) {
|
2014-03-31 21:04:15 +02:00
|
|
|
$fileName = Formatter::stripCodes($fileName);
|
|
|
|
$fileName = str_replace(array('\\', '/', ':', '*', '?', '"', '<', '>', '|'), '_', $fileName);
|
2014-03-31 21:37:53 +02:00
|
|
|
$fileName = preg_replace( '/[^[:print:]]/', '', $fileName);
|
2014-03-31 21:04:15 +02:00
|
|
|
return $fileName;
|
2013-11-27 02:42:39 +01:00
|
|
|
}
|
2013-11-10 13:58:08 +01:00
|
|
|
}
|