TrackManiaControl/application/core/Files/AsynchronousFileReader.php

151 lines
3.5 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
2014-02-18 18:13:59 +01:00
use cURL\Exception;
use cURL\Request;
use cURL\Response;
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 00:19:53 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
2014-02-18 18:13:59 +01:00
private $requests = array();
2014-02-07 00:19:53 +01:00
/**
* 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-18 18:13:59 +01:00
foreach($this->requests as $key => $request) {
/** @var Request $request */
try {
if ($request->socketPerform()) {
$request->socketSelect();
2014-02-17 00:34:20 +01:00
}
2014-02-18 18:13:59 +01:00
} catch(Exception $e) {
if ($e->getMessage() == "Cannot perform if there are no requests in queue.") {
unset($this->requests[$key]);
} else {
throw $e;
2014-02-16 22:38:14 +01:00
}
2014-02-07 00:19:53 +01:00
}
2014-02-17 00:38:32 +01:00
}
2014-02-07 00:19:53 +01:00
}
2014-02-15 13:52:46 +01:00
/**
2014-02-18 18:13:59 +01:00
* Load a remote file
2014-02-15 13:52:46 +01:00
*
2014-02-18 18:13:59 +01:00
* @param string $url
* @param $function
* @param string $contentType
* @param string $customHeader
* @return bool
2014-02-15 13:52:46 +01:00
*/
2014-02-18 18:13:59 +01:00
public function loadFile($url, $function, $contentType = 'UTF-8', $customHeader = '') {
if (!is_callable($function)) {
$this->maniaControl->log("Function is not callable");
return false;
2014-02-15 13:52:46 +01:00
}
2014-02-15 15:51:37 +01:00
2014-02-18 18:13:59 +01:00
if (!$url) {
return null;
2014-02-16 22:38:14 +01:00
}
2014-02-15 13:52:46 +01:00
2014-02-18 18:13:59 +01:00
$request = new \cURL\Request($url);
2014-02-15 13:52:46 +01:00
2014-02-18 18:13:59 +01:00
$request->getOptions()->set(CURLOPT_TIMEOUT, 5) //
->set(CURLOPT_HEADER, false) //
->set(CURLOPT_CRLF, true) //
//->set(CURLOPT_HTTPHEADER, array("Content-Type: " . $contentType))
->set(CURLOPT_USERAGENT, 'User-Agent: ManiaControl v' . ManiaControl::VERSION) //
->set(CURLOPT_RETURNTRANSFER, true);
2014-02-15 13:52:46 +01:00
2014-02-15 14:05:15 +01:00
2014-02-18 18:13:59 +01:00
$request->addListener('complete', function (\cURL\Event $event) use (&$function) {
/** @var Response $response */
$response = $event->response;
$error = "";
$content = "";
if ($response->hasError()) {
$error = $response->getError()->getMessage();
} else {
$content = $response->getContent();
2014-02-17 00:34:20 +01:00
}
2014-02-15 14:05:15 +01:00
2014-02-18 18:13:59 +01:00
call_user_func($function, $content, $error);
});
array_push($this->requests, $request);
return true;
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') {
2014-02-18 18:13:59 +01:00
/*if (!is_callable($function)) {
2014-02-16 22:38:14 +01:00
$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);
2014-02-18 18:13:59 +01:00
return true;*/
2014-02-07 00:19:53 +01:00
}
}