dedimania continue

This commit is contained in:
kremsy 2014-02-18 23:29:13 +01:00 committed by Steffen Schröder
parent bb6b47376c
commit 159e2079ee
3 changed files with 76 additions and 46 deletions

View File

@ -53,10 +53,9 @@ class AsynchronousFileReader {
* @param string $url
* @param $function
* @param string $contentType
* @param string $customHeader
* @return bool
*/
public function loadFile($url, $function, $contentType = 'UTF-8', $customHeader = '') {
public function loadFile($url, $function, $contentType = 'UTF-8') {
if (!is_callable($function)) {
$this->maniaControl->log("Function is not callable");
return false;
@ -70,10 +69,10 @@ class AsynchronousFileReader {
$request = new Request($url);
$request->getOptions()->set(CURLOPT_TIMEOUT, 5) //
->set(CURLOPT_HEADER, false) //
->set(CURLOPT_CRLF, true) //
->set(CURLOPT_ENCODING, "")//
->set(CURLOPT_AUTOREFERER, true)//
->set(CURLOPT_HEADER, false) //don't display response header
->set(CURLOPT_CRLF, true) //linux linefeed
->set(CURLOPT_ENCODING, "")//accept encoding
->set(CURLOPT_AUTOREFERER, true)//accept link reference
->set(CURLOPT_HTTPHEADER, array("Content-Type: " . $contentType)) //
->set(CURLOPT_USERAGENT, 'User-Agent: ManiaControl v' . ManiaControl::VERSION) //
->set(CURLOPT_RETURNTRANSFER, true);
@ -99,13 +98,65 @@ class AsynchronousFileReader {
/**
* Adds a Request to the queue
*
* @param Request $request
*/
public function addRequest(Request $request){
public function addRequest(Request $request) {
array_push($this->requests, $request);
}
public function postData($url, $function, $content, $compressed = false, $contentType = 'UTF-8') {
//TODO update dedimania plugin
/**
* Send Data via POST Method
*
* @param $url
* @param $function
* @param $content
* @param string $compression
* @param string $contentType
* @return bool
*/
public function postData($url, $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8') {
if (!is_callable($function)) {
$this->maniaControl->log("Function is not callable");
return false;
}
if (!$url) {
$this->maniaControl->log("Url is empty");
return false;
}
if ($compression) {
$content = gzencode($content);
}
$request = new Request($url);
$request->getOptions()->set(CURLOPT_HEADER, false) //don't display response header
->set(CURLOPT_CRLF, true) //linux linefeed
->set(CURLOPT_ENCODING, "")//accept encoding
//->set(CURLOPT_AUTOREFERER, true)//accept link reference
->set(CURLOPT_POST, true) //post field
->set(CURLOPT_POSTFIELDS, $content) //post content field
->set(CURLOPT_HTTPHEADER, array("Content-Type: " . $contentType, "Keep-Alive: 300", "Connection: Keep-Alive", "Content-Encoding: gzip")) //
//->set(CURLOPT_HTTPHEADER, array("Content-Type: " . $contentType, "Keep-Alive")) //
->set(CURLOPT_USERAGENT, 'User-Agent: ManiaControl v' . ManiaControl::VERSION) //
->set(CURLOPT_RETURNTRANSFER, true);
$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();
}
call_user_func($function, $content, $error);
});
$this->addRequest($request);
return true;
}
}

View File

@ -1,28 +0,0 @@
<?php
/**
* Socket Structure
*
* @author kremsy & steeffeen
*/
namespace ManiaControl\Files;
/**
* Socket Structure
*/
class SocketStructure {
public $streamBuffer;
public $socket;
public $function;
public $url;
public $creationTime;
public $header;
public function __construct($url, $socket, $function) {
$this->url = $url;
$this->socket = $socket;
$this->function = $function;
$this->creationTime = time();
$this->streamBuffer = '';
$this->header = array();
}
}

View File

@ -76,21 +76,22 @@ class Dedimania implements CallbackListener, TimerListener, Plugin {
* Opens the Dedimania Session
*/
private function openDedimaniaSession() {
//$content = gzcompress($this->encode_request(self::DEDIMANIA_OPENSESSION, array($this->dedimaniaData->toArray())));
$content = $this->encode_request(self::DEDIMANIA_OPENSESSION, array($this->dedimaniaData->toArray()));
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
$this->maniaControl->log("Try to connect on Dedimania");
$data = $this->decode($data);
var_dump($error);
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
$data = $this->decode($data);
if (is_array($data)) {
foreach($data as $index => $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
$this->handleXmlRpcFault($methodResponse);
} else if ($index <= 0) {
$responseData = $methodResponse[0];
var_dump($responseData);
$this->dedimaniaData->sessionId = $responseData['SessionId'];
if ($this->dedimaniaData->sessionId != '') {
$this->maniaControl->log("Dedimania connection successfully established.");
@ -101,7 +102,7 @@ class Dedimania implements CallbackListener, TimerListener, Plugin {
}
}
}
}, $content, self::USE_COMPRESSION);
}, $content, true);
}
/**
@ -149,8 +150,11 @@ class Dedimania implements CallbackListener, TimerListener, Plugin {
$content = $this->encode_request(self::DEDIMANIA_GETRECORDS, $data);
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
$data = $this->decode($data);
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
$data = $this->decode($data);
if (is_array($data)) {
foreach($data as $index => $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
@ -164,7 +168,7 @@ class Dedimania implements CallbackListener, TimerListener, Plugin {
}
$this->updateManialink = true;
return true;
}, $content, self::USE_COMPRESSION);
}, $content, true);
return true;
}
@ -181,8 +185,11 @@ class Dedimania implements CallbackListener, TimerListener, Plugin {
$content = $this->encode_request(self::DEDIMANIA_CHECKSESSION, array($this->dedimaniaData->sessionId));
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
$data = $this->decode($data);
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
$data = $this->decode($data);
if (is_array($data)) {
foreach($data as $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
@ -197,7 +204,7 @@ class Dedimania implements CallbackListener, TimerListener, Plugin {
}
}
}
}, $content, self::USE_COMPRESSION);
}, $content, true);
return;
}