diff --git a/application/Structures/SocketStructure.php b/application/Structures/SocketStructure.php new file mode 100644 index 00000000..97504a60 --- /dev/null +++ b/application/Structures/SocketStructure.php @@ -0,0 +1,24 @@ +socket = $socket; + $this->function = $function; + $this->streamBuffer = ''; + } +} \ No newline at end of file diff --git a/application/core/AsynchronousFileReader.php b/application/core/AsynchronousFileReader.php new file mode 100644 index 00000000..661de437 --- /dev/null +++ b/application/core/AsynchronousFileReader.php @@ -0,0 +1,84 @@ +maniaControl = $maniaControl; + $this->maniaControl->timerManager->registerTimerListening($this, 'appendData', 1); + } + + public function appendData() { + foreach($this->sockets as &$socket) { + /** @var SocketStructure $socket */ + $socket->streamBuffer .= fread($socket->socket, 512); + $info = stream_get_meta_data($socket->socket); + + if (feof($socket->socket || $info['timed_out'])) { //TODO special error threadment on timeout + fclose($socket->socket); + + $error = 0; //TODO error constants + if ($info['timed_out'] || !$socket->streamBuffer) { + $error = 1; + } else if (substr($socket->streamBuffer, 9, 3) != "200") { + $error = 2; + } + + $result = explode("\r\n\r\n", $socket->streamBuffer, 2); + + if (count($result) < 2) { + $error = 3; + } + + //TODO call inner function + + } + } + } + + /** + * Load a remote file + * + * @param string $url + * @param string $contentType + * @return string || null + */ + public function loadFile($url, $contentType = 'UTF-8', $function) { + if (!$url) { + return null; + } + $urlData = parse_url($url); + $port = (isset($urlData['port']) ? $urlData['port'] : 80); + + $socket = fsockopen($urlData['host'], $port); + stream_set_timeout($socket, 5); + + + $query = 'GET ' . $urlData['path'] . ' HTTP/1.0' . PHP_EOL; + $query .= 'Host: ' . $urlData['host'] . PHP_EOL; + $query .= 'Content-Type: ' . $contentType . PHP_EOL; + $query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL; + $query .= PHP_EOL; + + fwrite($socket, $query); + + //TODO check error + stream_set_blocking($this->sockets, 0); + + $socketStructure = new SocketStructure($socket, $function); + array_push($this->sockets, $socketStructure); + } +} \ No newline at end of file