2014-02-07 00:19:53 +01:00
|
|
|
<?php
|
2014-04-12 12:14:37 +02:00
|
|
|
|
2014-02-07 00:26:32 +01:00
|
|
|
namespace ManiaControl\Files;
|
2014-02-07 00:19:53 +01:00
|
|
|
|
2014-04-27 15:21:57 +02:00
|
|
|
use cURL\Event;
|
2014-02-18 18:13:59 +01:00
|
|
|
use cURL\Request;
|
2014-02-07 00:19:53 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
/**
|
|
|
|
* Asynchronous File Reader
|
|
|
|
*
|
2014-05-02 17:40:47 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2015-01-19 11:06:20 +01:00
|
|
|
* @copyright 2014-2015 ManiaControl Team
|
2014-05-02 17:40:47 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-02-07 12:30:53 +01:00
|
|
|
*/
|
2014-02-07 19:33:14 +01:00
|
|
|
class AsynchronousFileReader {
|
2014-06-14 15:48:27 +02:00
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const CONTENT_TYPE_JSON = 'application/json';
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Private properties
|
2014-02-07 00:19:53 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2014-02-07 00:19:53 +01:00
|
|
|
private $maniaControl = null;
|
2014-06-27 00:17:02 +02:00
|
|
|
/** @var Request[] $requests */
|
2014-02-18 18:13:59 +01:00
|
|
|
private $requests = array();
|
2014-02-07 00:19:53 +01:00
|
|
|
|
|
|
|
/**
|
2014-06-27 00:17:02 +02:00
|
|
|
* Construct a new Asynchronous File Reader Instance
|
2014-02-07 00:19:53 +01:00
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
}
|
|
|
|
|
2014-07-17 20:02:23 +02:00
|
|
|
public static function newRequestTest($url) {
|
|
|
|
$request = new Request($url);
|
2015-07-29 14:35:19 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header
|
|
|
|
->set(CURLOPT_CRLF, true)// linux line feed
|
|
|
|
->set(CURLOPT_ENCODING, '')// accept encoding
|
|
|
|
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
|
2014-07-17 20:02:23 +02:00
|
|
|
->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
2014-02-07 14:17:03 +01:00
|
|
|
/**
|
2014-06-27 00:17:02 +02:00
|
|
|
* Append available Data of active Requests
|
2014-02-07 14:17:03 +01:00
|
|
|
*/
|
2014-02-07 00:19:53 +01:00
|
|
|
public function appendData() {
|
2014-05-02 17:40:47 +02:00
|
|
|
foreach ($this->requests as $key => $request) {
|
2014-07-17 20:02:23 +02:00
|
|
|
if ($request->socketPerform()) {
|
|
|
|
$request->socketSelect();
|
|
|
|
} else {
|
|
|
|
unset($this->requests[$key]);
|
|
|
|
}
|
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-06-14 15:48:27 +02:00
|
|
|
* Load a Remote File
|
2014-02-15 13:52:46 +01:00
|
|
|
*
|
2014-06-14 15:48:27 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param callable $function
|
|
|
|
* @param string $contentType
|
|
|
|
* @param int $keepAlive
|
2015-07-29 14:35:19 +02:00
|
|
|
* @param array $headers Additional Headers
|
2014-02-15 13:52:46 +01:00
|
|
|
*/
|
2015-07-29 14:35:19 +02:00
|
|
|
public function loadFile($url, callable $function, $contentType = 'UTF-8', $keepAlive = 0, $headers = array()) {
|
2014-06-27 00:17:02 +02:00
|
|
|
array_push($headers, 'Content-Type: ' . $contentType);
|
2014-02-24 09:49:29 +01:00
|
|
|
if ($keepAlive) {
|
2014-06-14 15:48:27 +02:00
|
|
|
array_push($headers, 'Keep-Alive: ' . $keepAlive);
|
|
|
|
array_push($headers, 'Connection: Keep-Alive');
|
2014-02-23 17:55:32 +01:00
|
|
|
}
|
|
|
|
|
2014-06-27 00:59:45 +02:00
|
|
|
$request = $this->newRequest($url);
|
2015-07-29 14:35:19 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_AUTOREFERER, true)// accept link reference
|
2014-06-27 00:59:45 +02:00
|
|
|
->set(CURLOPT_HTTPHEADER, $headers); // headers
|
2014-02-15 13:52:46 +01:00
|
|
|
|
2014-04-27 15:21:57 +02:00
|
|
|
$request->addListener('complete', function (Event $event) use (&$function) {
|
2014-06-27 00:17:02 +02:00
|
|
|
$error = null;
|
|
|
|
$content = null;
|
|
|
|
if ($event->response->hasError()) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$error = $event->response->getError()->getMessage();
|
2014-02-18 18:13:59 +01:00
|
|
|
} else {
|
2014-06-27 00:17:02 +02:00
|
|
|
$content = $event->response->getContent();
|
2014-02-17 00:34:20 +01:00
|
|
|
}
|
2014-02-18 18:13:59 +01:00
|
|
|
call_user_func($function, $content, $error);
|
|
|
|
});
|
|
|
|
|
2014-02-18 18:44:52 +01:00
|
|
|
$this->addRequest($request);
|
2014-02-15 13:52:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-16 22:38:14 +01:00
|
|
|
/**
|
2015-09-30 22:28:16 +02:00
|
|
|
* Create a new cURL Request for the given URL, DO NOT CALL MANUALLY!
|
2014-06-27 00:17:02 +02:00
|
|
|
*
|
2014-06-27 00:59:45 +02:00
|
|
|
* @param string $url
|
|
|
|
* @return Request
|
2014-06-27 00:17:02 +02:00
|
|
|
*/
|
2015-09-30 22:28:16 +02:00
|
|
|
public function newRequest($url) {
|
2014-06-27 00:59:45 +02:00
|
|
|
$request = new Request($url);
|
2015-07-29 14:35:19 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header
|
|
|
|
->set(CURLOPT_CRLF, true)// linux line feed
|
|
|
|
->set(CURLOPT_ENCODING, '')// accept encoding
|
|
|
|
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
|
2014-06-27 00:59:45 +02:00
|
|
|
->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
|
|
|
|
return $request;
|
2014-06-27 00:17:02 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 20:02:23 +02:00
|
|
|
//TODO remove, they are just for testing dedimania
|
|
|
|
|
2014-06-27 00:17:02 +02:00
|
|
|
/**
|
2015-09-30 22:28:16 +02:00
|
|
|
* Add a Request to the queue, DO NOT CALL MANUALLY!
|
2014-02-18 23:29:13 +01:00
|
|
|
*
|
2014-02-18 18:44:52 +01:00
|
|
|
* @param Request $request
|
2014-02-16 22:38:14 +01:00
|
|
|
*/
|
2015-09-30 22:28:16 +02:00
|
|
|
public function addRequest(Request $request) {
|
2014-02-18 18:44:52 +01:00
|
|
|
array_push($this->requests, $request);
|
|
|
|
}
|
2014-02-16 22:38:14 +01:00
|
|
|
|
2014-08-25 15:33:22 +02:00
|
|
|
public function postDataTest(Request $request, $url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;') {
|
2014-07-05 20:24:54 +02:00
|
|
|
|
|
|
|
$headers = array();
|
|
|
|
array_push($headers, 'Content-Type: ' . $contentType);
|
|
|
|
array_push($headers, 'Keep-Alive: timeout=600, max=2000');
|
|
|
|
array_push($headers, 'Connection: Keep-Alive');
|
|
|
|
|
|
|
|
$content = str_replace(array("\r", "\n"), '', $content);
|
|
|
|
if ($compression) {
|
|
|
|
$content = zlib_encode($content, 31);
|
|
|
|
array_push($headers, 'Content-Encoding: gzip');
|
|
|
|
}
|
|
|
|
|
2015-07-29 14:35:19 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_POST, true)// post method
|
|
|
|
->set(CURLOPT_POSTFIELDS, $content)// post content field
|
2014-07-05 20:24:54 +02:00
|
|
|
->set(CURLOPT_HTTPHEADER, $headers) // headers
|
|
|
|
;
|
|
|
|
$request->addListener('complete', function (Event $event) use (&$function) {
|
|
|
|
$error = null;
|
|
|
|
$content = null;
|
|
|
|
if ($event->response->hasError()) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$error = $event->response->getError()->getMessage();
|
2014-07-05 20:24:54 +02:00
|
|
|
} else {
|
|
|
|
$content = $event->response->getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
call_user_func($function, $content, $error);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->addRequest($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-18 23:29:13 +01:00
|
|
|
/**
|
|
|
|
* Send Data via POST Method
|
|
|
|
*
|
2014-06-27 00:17:02 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param callable $function
|
|
|
|
* @param string $content
|
|
|
|
* @param bool $compression
|
|
|
|
* @param string $contentType
|
2015-07-29 14:35:19 +02:00
|
|
|
* @param array $headers Additional Headers
|
2014-02-18 23:29:13 +01:00
|
|
|
*/
|
2015-07-29 14:35:19 +02:00
|
|
|
public function postData($url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;', $headers = array()) {
|
2015-09-30 22:33:48 +02:00
|
|
|
$httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
|
|
|
|
$httpRequest->setCallable($function)->setContent($content)->setCompression($compression)->setContentType($contentType)->setHeaders($headers);
|
|
|
|
$httpRequest->postData();
|
2014-02-07 00:19:53 +01:00
|
|
|
}
|
2014-06-27 00:17:02 +02:00
|
|
|
}
|