TrackManiaControl/application/core/Files/AsynchronousFileReader.php

106 lines
2.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\Callbacks\TimerListener;
use ManiaControl\ManiaControl;
2014-02-07 12:30:53 +01:00
/**
* Asynchronous File Reader
*
* @author kremsy & steeffeen
*/
2014-02-07 00:19:53 +01:00
class AsynchronousFileReader implements TimerListener {
/**
* Private Properties
*/
private $sockets = array();
private $maniaControl = null;
/**
* Construct
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
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 14:05:10 +01:00
$socket->streamBuffer .= fread($socket->socket, 1024);
2014-02-07 00:19:53 +01:00
$info = stream_get_meta_data($socket->socket);
2014-02-07 12:30:53 +01:00
if (feof($socket->socket) || $info['timed_out']) {
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-07 12:30:53 +01:00
if ($info['timed_out']) {
throw new \Exception("Timed out while reading data from " . $socket->url);
}
if (substr($socket->streamBuffer, 9, 3) != "200") {
throw new \Exception("Connection or response error on " . $socket->url);
}
if ($socket->streamBuffer == '') {
throw new \Exception("No data returned from " . $socket->url);
2014-02-07 00:19:53 +01:00
}
$result = explode("\r\n\r\n", $socket->streamBuffer, 2);
if (count($result) < 2) {
2014-02-07 12:30:53 +01:00
throw new \Exception("Invalid Result");
2014-02-07 00:19:53 +01:00
}
2014-02-07 12:30:53 +01:00
call_user_func($socket->function, $result[1]);
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-07 12:30:53 +01:00
* @return bool
2014-02-07 00:19:53 +01:00
*/
2014-02-07 12:30:53 +01:00
public function loadFile($url, $function, $contentType = 'UTF-8') {
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;
}
stream_set_timeout($socket, 10);
2014-02-07 00:19:53 +01:00
2014-02-07 12:30:53 +01:00
$query = 'GET ' . $urlData['path'] . $urlQuery . ' HTTP/1.0' . PHP_EOL;
2014-02-07 00:19:53 +01:00
$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);
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
}
}