2014-02-07 00:19:53 +01:00
|
|
|
<?php
|
2014-02-07 00:26:32 +01:00
|
|
|
namespace ManiaControl\Files;
|
2014-02-07 00:19:53 +01:00
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
/**
|
|
|
|
* Asynchronous File Reader
|
|
|
|
*
|
|
|
|
* @author kremsy & steeffeen
|
|
|
|
*/
|
2014-02-07 19:33:14 +01:00
|
|
|
class AsynchronousFileReader {
|
2014-02-07 17:27:09 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const TIMEOUT_ERROR = 'Timed out while reading data';
|
|
|
|
const RESPONSE_ERROR = 'Connection or response error';
|
|
|
|
const NO_DATA_ERROR = 'No data returned';
|
|
|
|
const INVALID_RESULT_ERROR = 'Invalid Result';
|
|
|
|
const SOCKET_TIMEOUT = 10;
|
|
|
|
|
|
|
|
|
2014-02-07 00:19:53 +01:00
|
|
|
/**
|
|
|
|
* Private Properties
|
|
|
|
*/
|
|
|
|
private $sockets = array();
|
|
|
|
private $maniaControl = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct
|
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
}
|
|
|
|
|
2014-02-07 14:17:03 +01:00
|
|
|
/**
|
|
|
|
* Appends the Data
|
|
|
|
*/
|
2014-02-07 00:19:53 +01:00
|
|
|
public function appendData() {
|
2014-02-07 12:30:53 +01:00
|
|
|
foreach($this->sockets as $key => &$socket) {
|
2014-02-07 00:19:53 +01:00
|
|
|
/** @var SocketStructure $socket */
|
2014-02-17 00:34:20 +01:00
|
|
|
do {
|
|
|
|
$line = fgets($socket->socket, 4096);
|
|
|
|
if (empty($socket->header) && $line == "\r\n") {
|
|
|
|
$socket->header = $this->parseHeader($socket->streamBuffer);
|
|
|
|
$socket->streamBuffer = "";
|
|
|
|
$line = "";
|
|
|
|
}
|
|
|
|
$socket->streamBuffer .= $line;
|
|
|
|
|
2014-02-17 12:18:56 +01:00
|
|
|
$chunked = isset($socket->header["transfer-encoding"]) && $socket->header["transfer-encoding"] == "chunked" && $line == "0";
|
|
|
|
if ($chunked || isset($socket->header["content-length"]) && strlen($socket->streamBuffer) >= $socket->header["content-length"]) {
|
2014-02-17 00:34:20 +01:00
|
|
|
fclose($socket->socket);
|
|
|
|
unset($this->sockets[$key]);
|
|
|
|
$this->handleContent($socket);
|
|
|
|
continue 2;
|
2014-02-16 22:38:14 +01:00
|
|
|
}
|
2014-02-17 00:38:32 +01:00
|
|
|
|
|
|
|
$meta = stream_get_meta_data($socket->socket);
|
2014-02-17 00:34:20 +01:00
|
|
|
} while($meta["unread_bytes"] > 0);
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 22:38:14 +01:00
|
|
|
|
2014-02-17 00:34:20 +01:00
|
|
|
/**
|
|
|
|
* Handles the Content
|
2014-02-17 00:38:32 +01:00
|
|
|
*
|
2014-02-17 00:34:20 +01:00
|
|
|
* @param $socket
|
|
|
|
*/
|
2014-02-17 00:38:32 +01:00
|
|
|
private function handleContent(SocketStructure $socket) { //TODO timeout handling
|
2014-02-17 00:34:20 +01:00
|
|
|
//if (feof($socket->socket) || time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
|
2014-02-17 00:38:32 +01:00
|
|
|
$result = "";
|
|
|
|
$error = 0;
|
|
|
|
/*if (time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
|
|
|
|
$error = self::TIMEOUT_ERROR;
|
|
|
|
} else*/
|
|
|
|
if ($socket->header["status"] != "200") {
|
|
|
|
$error = self::RESPONSE_ERROR;
|
2014-02-17 12:18:56 +01:00
|
|
|
$result = $this->parseResult($socket);
|
2014-02-17 00:34:20 +01:00
|
|
|
|
2014-02-17 00:38:32 +01:00
|
|
|
} else if ($socket->streamBuffer == '') {
|
|
|
|
$error = self::NO_DATA_ERROR;
|
|
|
|
} else {
|
2014-02-17 12:18:56 +01:00
|
|
|
$result = $this->parseResult($socket);
|
2014-02-17 00:38:32 +01:00
|
|
|
if ($result == self::INVALID_RESULT_ERROR) {
|
|
|
|
$error = self::INVALID_RESULT_ERROR;
|
2014-02-07 00:19:53 +01:00
|
|
|
}
|
2014-02-17 00:38:32 +01:00
|
|
|
}
|
|
|
|
call_user_func($socket->function, $result, $error);
|
2014-02-17 00:34:20 +01:00
|
|
|
//}
|
2014-02-07 00:19:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 13:52:46 +01:00
|
|
|
/**
|
|
|
|
* Parse the Stream Result
|
|
|
|
*
|
2014-02-17 00:34:20 +01:00
|
|
|
* @param SocketStructure $socket
|
|
|
|
* @internal param $streamBuffer
|
2014-02-15 13:52:46 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-02-17 12:18:56 +01:00
|
|
|
private function parseResult(SocketStructure $socket) {
|
2014-02-16 22:38:14 +01:00
|
|
|
|
2014-02-17 12:18:56 +01:00
|
|
|
if (isset($socket->header["transfer-encoding"]) && $socket->header["transfer-encoding"] == "chunked") {
|
2014-02-17 00:34:20 +01:00
|
|
|
$result = $this->decode_chunked($socket->streamBuffer);
|
2014-02-15 13:52:46 +01:00
|
|
|
} else {
|
2014-02-17 00:34:20 +01:00
|
|
|
$result = $socket->streamBuffer;
|
2014-02-15 13:52:46 +01:00
|
|
|
}
|
2014-02-15 15:51:37 +01:00
|
|
|
|
2014-02-17 00:34:20 +01:00
|
|
|
return $this->decompressData($socket->header, $result);
|
2014-02-15 13:52:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-16 22:38:14 +01:00
|
|
|
/**
|
|
|
|
* Checks if the data is Compressed and uncompress it
|
|
|
|
*
|
|
|
|
* @param $header
|
|
|
|
* @param $data
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function decompressData($header, $data) {
|
|
|
|
if (isset($header["content-encoding"])) {
|
|
|
|
switch($header["content-encoding"]) {
|
|
|
|
case "gzip":
|
|
|
|
case "gzip;":
|
|
|
|
return gzdecode($data);
|
|
|
|
case "deflate":
|
|
|
|
case "deflate;":
|
|
|
|
return gzinflate($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2014-02-15 13:52:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode Chunks
|
|
|
|
*
|
|
|
|
* @param $str
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function decode_chunked($str) {
|
|
|
|
for($res = ''; !empty($str); $str = trim($str)) {
|
2014-02-15 17:01:28 +01:00
|
|
|
$pos = strpos($str, "\r\n");
|
2014-02-15 13:52:46 +01:00
|
|
|
$len = hexdec(substr($str, 0, $pos));
|
|
|
|
$res .= substr($str, $pos + 2, $len);
|
|
|
|
$str = substr($str, $pos + 2 + $len);
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-15 14:05:15 +01:00
|
|
|
* Parse the Header
|
2014-02-15 14:08:39 +01:00
|
|
|
*
|
2014-02-15 14:05:15 +01:00
|
|
|
* @param $header
|
2014-02-15 13:52:46 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-02-15 14:05:15 +01:00
|
|
|
function parseHeader($header) {
|
2014-02-15 17:01:28 +01:00
|
|
|
$headers = explode("\r\n", $header);
|
2014-02-15 14:05:15 +01:00
|
|
|
$output = array();
|
2014-02-15 13:52:46 +01:00
|
|
|
|
2014-02-15 14:05:15 +01:00
|
|
|
if ('HTTP' === substr($headers[0], 0, 4)) {
|
|
|
|
list(, $output['status'], $output['status_text']) = explode(' ', $headers[0]);
|
|
|
|
unset($headers[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($headers as $v) {
|
2014-02-17 00:34:20 +01:00
|
|
|
if ($v == "") {
|
|
|
|
break;
|
|
|
|
}
|
2014-02-15 14:05:15 +01:00
|
|
|
$h = preg_split('/:\s*/', $v);
|
|
|
|
$output[strtolower($h[0])] = $h[1];
|
2014-02-15 13:52:46 +01:00
|
|
|
}
|
2014-02-15 14:05:15 +01:00
|
|
|
|
|
|
|
return $output;
|
2014-02-15 13:52:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 14:05:15 +01:00
|
|
|
|
2014-02-16 22:38:14 +01:00
|
|
|
/**
|
|
|
|
* Send Data via POST Method
|
|
|
|
*
|
|
|
|
* @param $url
|
|
|
|
* @param $function
|
|
|
|
* @param $content
|
|
|
|
* @param string $contentType
|
|
|
|
* @return bool|null
|
|
|
|
*/
|
|
|
|
public function postData($url, $function, $content, $compressed = false, $contentType = 'UTF-8') {
|
|
|
|
if (!is_callable($function)) {
|
|
|
|
$this->maniaControl->log("Function is not callable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$url) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$urlData = parse_url($url);
|
|
|
|
$port = (isset($urlData['port']) ? $urlData['port'] : 80);
|
|
|
|
|
|
|
|
$socket = @fsockopen($urlData['host'], $port, $errno, $errstr, 4);
|
|
|
|
if (!$socket) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = 'POST ' . $urlData['path'] . ' HTTP/1.1' . PHP_EOL;
|
|
|
|
$query .= 'Host: ' . $urlData['host'] . PHP_EOL;
|
|
|
|
$query .= 'Accept-Charset: utf-8' . PHP_EOL;
|
|
|
|
$query .= 'Accept-Encoding: gzip, deflate' . PHP_EOL;
|
|
|
|
//$query .= 'Content-Encoding: gzip' . PHP_EOL;
|
|
|
|
$query .= 'Content-Type: text/xml; charset=utf-8;' . PHP_EOL;
|
|
|
|
$query .= 'Keep-Alive: 300' . PHP_EOL;
|
|
|
|
$query .= 'Connection: Keep-Alive' . PHP_EOL;
|
|
|
|
$query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL;
|
|
|
|
$query .= 'Content-Length: ' . strlen($content) . PHP_EOL . PHP_EOL;
|
|
|
|
|
|
|
|
$query .= $content . PHP_EOL;
|
|
|
|
|
|
|
|
fwrite($socket, $query);
|
|
|
|
|
|
|
|
$success = stream_set_blocking($socket, 0);
|
|
|
|
if (!$success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$socketStructure = new SocketStructure($url, $socket, $function);
|
|
|
|
array_push($this->sockets, $socketStructure);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 00:19:53 +01:00
|
|
|
/**
|
|
|
|
* Load a remote file
|
|
|
|
*
|
|
|
|
* @param string $url
|
2014-02-07 12:30:53 +01:00
|
|
|
* @param $function
|
2014-02-07 00:19:53 +01:00
|
|
|
* @param string $contentType
|
2014-02-15 12:23:38 +01:00
|
|
|
* @param string $customHeader
|
2014-02-07 12:30:53 +01:00
|
|
|
* @return bool
|
2014-02-07 00:19:53 +01:00
|
|
|
*/
|
2014-02-15 12:25:34 +01:00
|
|
|
public function loadFile($url, $function, $contentType = 'UTF-8', $customHeader = '') {
|
2014-02-07 12:30:53 +01:00
|
|
|
if (!is_callable($function)) {
|
|
|
|
$this->maniaControl->log("Function is not callable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-07 00:19:53 +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:19:53 +01:00
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
$socket = @fsockopen($urlData['host'], $port, $errno, $errstr, 4);
|
|
|
|
if (!$socket) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-07 00:19:53 +01:00
|
|
|
|
2014-02-15 00:47:42 +01:00
|
|
|
if ($customHeader == '') {
|
2014-02-15 13:52:46 +01:00
|
|
|
$query = 'GET ' . $urlData['path'] . $urlQuery . ' HTTP/1.1' . PHP_EOL;
|
2014-02-15 00:47:42 +01:00
|
|
|
$query .= 'Host: ' . $urlData['host'] . PHP_EOL;
|
|
|
|
$query .= 'Content-Type: ' . $contentType . PHP_EOL;
|
2014-02-15 13:52:46 +01:00
|
|
|
$query .= 'Connection: close' . PHP_EOL;
|
2014-02-15 00:47:42 +01:00
|
|
|
$query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL;
|
|
|
|
$query .= PHP_EOL;
|
|
|
|
} else {
|
|
|
|
$query = $customHeader;
|
|
|
|
}
|
2014-02-07 00:19:53 +01:00
|
|
|
|
|
|
|
fwrite($socket, $query);
|
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
$success = stream_set_blocking($socket, 0);
|
|
|
|
if (!$success) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-07 00:19:53 +01:00
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
$socketStructure = new SocketStructure($url, $socket, $function);
|
2014-02-07 00:19:53 +01:00
|
|
|
array_push($this->sockets, $socketStructure);
|
2014-02-07 12:30:53 +01:00
|
|
|
|
|
|
|
return true;
|
2014-02-07 00:19:53 +01:00
|
|
|
}
|
|
|
|
}
|