TrackManiaControl/application/core/Files/AsynchronousFileReader.php

195 lines
4.6 KiB
PHP
Raw Normal View History

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-07 17:27:09 +01:00
$socket->streamBuffer .= fread($socket->socket, 4096);
if (feof($socket->socket) || time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
2014-02-07 00:19:53 +01:00
fclose($socket->socket);
2014-02-07 12:30:53 +01:00
unset($this->sockets[$key]);
2014-02-07 00:19:53 +01:00
2014-02-08 16:58:12 +01:00
$result = "";
2014-02-07 17:27:09 +01:00
$error = 0;
if (time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
$error = self::TIMEOUT_ERROR;
} else if (substr($socket->streamBuffer, 9, 3) != "200") {
2014-02-15 13:52:46 +01:00
$error = self::RESPONSE_ERROR;
$result = $this->parseResult($socket->streamBuffer);
2014-02-15 12:40:08 +01:00
2014-02-07 17:27:09 +01:00
} else if ($socket->streamBuffer == '') {
$error = self::NO_DATA_ERROR;
} else {
2014-02-15 13:52:46 +01:00
$result = $this->parseResult($socket->streamBuffer);
if ($result == self::INVALID_RESULT_ERROR) {
2014-02-07 17:27:09 +01:00
$error = self::INVALID_RESULT_ERROR;
}
2014-02-07 00:19:53 +01:00
}
2014-02-15 13:52:46 +01:00
2014-02-08 16:58:12 +01:00
call_user_func($socket->function, $result, $error);
2014-02-07 00:19:53 +01:00
}
}
}
2014-02-15 13:52:46 +01:00
/**
* Parse the Stream Result
*
* @param $streamBuffer
* @return string
*/
private function parseResult($streamBuffer) {
$resultArray = explode(PHP_EOL . PHP_EOL, $streamBuffer, 2);
if (count($resultArray) < 2) {
$result = self::INVALID_RESULT_ERROR;
} else {
$header = $this->parse_header($resultArray[0]);
if (isset($header["Transfer-Encoding"])) {
$result = $this->decode_chunked($resultArray[1]);
} else {
$result = $resultArray[1];
}
}
return $result;
}
/**
* Decode Chunks
*
* @param $str
* @return string
*/
private function decode_chunked($str) {
for($res = ''; !empty($str); $str = trim($str)) {
$pos = strpos($str, PHP_EOL);
$len = hexdec(substr($str, 0, $pos));
$res .= substr($str, $pos + 2, $len);
$str = substr($str, $pos + 2 + $len);
}
return $res;
}
/**
* Parse The Header
*
* @param $raw_headers
* @return array
*/
private function parse_header($raw_headers) {
$headers = array();
$key = '';
foreach(explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if (!isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} elseif (is_array($headers[$h[0]])) {
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); // [+]
} else {
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [+]
}
$key = $h[0];
} else {
if (substr($h[0], 0, 1) == "\t") // [+]
{
$headers[$key] .= "\r\n\t" . trim($h[0]);
} elseif (!$key) {
$headers[0] = trim($h[0]);
}
trim($h[0]);
}
}
return $headers;
}
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
}
}