2015-09-30 22:28:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Files;
|
|
|
|
|
|
|
|
use cURL\Event;
|
2015-09-30 22:51:12 +02:00
|
|
|
use cURL\Request;
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
2015-09-30 22:28:16 +02:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
2015-09-30 22:58:49 +02:00
|
|
|
/**
|
|
|
|
* Asynchronous Http Request Class
|
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2015-09-30 22:58:49 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class AsyncHttpRequest implements UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
2015-09-30 22:58:49 +02:00
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const CONTENT_TYPE_JSON = 'application/json';
|
2017-03-29 20:38:46 +02:00
|
|
|
const CONTENT_TYPE_UTF8 = 'UTF-8';
|
2015-09-30 22:58:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Private properties
|
|
|
|
*/
|
2015-09-30 22:28:16 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
|
|
|
private $maniaControl;
|
|
|
|
|
|
|
|
private $url;
|
|
|
|
private $function;
|
|
|
|
private $content;
|
|
|
|
private $compression = false;
|
|
|
|
private $contentType = 'text/xml; charset=UTF-8;';
|
2017-05-08 18:53:56 +02:00
|
|
|
private $timeout = 60;
|
2015-09-30 22:28:16 +02:00
|
|
|
private $headers = array();
|
|
|
|
|
2015-09-30 22:33:48 +02:00
|
|
|
public function __construct($maniaControl, $url) {
|
2015-09-30 22:28:16 +02:00
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->url = $url;
|
|
|
|
}
|
|
|
|
|
2015-09-30 22:51:12 +02:00
|
|
|
/**
|
|
|
|
* Create a new cURL Request for the given URL
|
|
|
|
*
|
|
|
|
* @param string $url
|
2017-05-08 18:53:56 +02:00
|
|
|
* @param int $timeout
|
|
|
|
* @return \cURL\Request
|
2015-09-30 22:51:12 +02:00
|
|
|
*/
|
2017-05-08 18:53:56 +02:00
|
|
|
private function newRequest($url, $timeout) {
|
2015-09-30 22:51:12 +02:00
|
|
|
$request = new Request($url);
|
2017-05-08 18:53:56 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_TIMEOUT, $timeout)->set(CURLOPT_HEADER, false)// don't display response header
|
2015-09-30 22:51:12 +02:00
|
|
|
->set(CURLOPT_CRLF, true)// linux line feed
|
|
|
|
->set(CURLOPT_ENCODING, '')// accept encoding
|
|
|
|
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
|
2015-11-06 16:24:46 +01:00
|
|
|
->set(CURLOPT_RETURNTRANSFER, true)//
|
|
|
|
->set(CURLOPT_FOLLOWLOCATION, true)// support redirect
|
2022-04-14 12:42:30 +02:00
|
|
|
->set(CURLOPT_SSL_VERIFYPEER, false)
|
|
|
|
->set(CURLOPT_HEADER, true);
|
2015-09-30 22:51:12 +02:00
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Carry out a GetData Request
|
|
|
|
*
|
|
|
|
* @param int $keepAlive
|
|
|
|
*/
|
2015-09-30 22:42:52 +02:00
|
|
|
public function getData($keepAlive = 0) {
|
|
|
|
array_push($this->headers, 'Content-Type: ' . $this->contentType);
|
|
|
|
if ($keepAlive) {
|
2015-11-06 16:52:13 +01:00
|
|
|
array_push($this->headers, 'Keep-Alive: ' . $keepAlive);
|
|
|
|
array_push($this->headers, 'Connection: Keep-Alive');
|
2015-09-30 22:42:52 +02:00
|
|
|
}
|
2017-07-04 12:46:49 +02:00
|
|
|
array_push($this->headers, 'Accept-Charset: utf-8');
|
2015-09-30 22:42:52 +02:00
|
|
|
|
2017-05-08 18:53:56 +02:00
|
|
|
$request = $this->newRequest($this->url, $this->timeout);
|
2015-09-30 22:42:52 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_AUTOREFERER, true)// accept link reference
|
|
|
|
->set(CURLOPT_HTTPHEADER, $this->headers); // headers
|
|
|
|
|
2015-09-30 22:58:49 +02:00
|
|
|
$this->processRequest($request);
|
2015-09-30 22:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-30 22:51:12 +02:00
|
|
|
/**
|
|
|
|
* Carry out a PostData Request
|
|
|
|
*/
|
2015-09-30 22:28:16 +02:00
|
|
|
public function postData() {
|
|
|
|
array_push($this->headers, 'Content-Type: ' . $this->contentType);
|
|
|
|
array_push($this->headers, 'Keep-Alive: timeout=600, max=2000');
|
|
|
|
array_push($this->headers, 'Connection: Keep-Alive');
|
2017-07-04 12:46:49 +02:00
|
|
|
array_push($this->headers, 'Expect:');
|
|
|
|
array_push($this->headers, 'Accept-Charset: utf-8');
|
2015-09-30 22:28:16 +02:00
|
|
|
|
2017-06-28 14:27:56 +02:00
|
|
|
$content = $this->content;
|
2015-09-30 22:28:16 +02:00
|
|
|
if ($this->compression) {
|
2017-07-04 12:52:47 +02:00
|
|
|
$content = gzencode($this->content);
|
2015-11-06 16:52:13 +01:00
|
|
|
array_push($this->headers, 'Content-Encoding: gzip');
|
2015-09-30 22:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-08 18:53:56 +02:00
|
|
|
$request = $this->newRequest($this->url, $this->timeout);
|
2015-09-30 22:28:16 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_POST, true)// post method
|
|
|
|
->set(CURLOPT_POSTFIELDS, $content)// post content field
|
|
|
|
->set(CURLOPT_HTTPHEADER, $this->headers) // headers
|
|
|
|
;
|
2015-09-30 22:58:49 +02:00
|
|
|
|
|
|
|
$this->processRequest($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the Request
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
private function processRequest(Request $request) {
|
2015-10-13 19:12:27 +02:00
|
|
|
$request->addListener('complete', function (Event $event) {
|
2015-09-30 22:28:16 +02:00
|
|
|
$error = null;
|
|
|
|
$content = null;
|
|
|
|
if ($event->response->hasError()) {
|
|
|
|
$error = $event->response->getError()->getMessage();
|
|
|
|
} else {
|
|
|
|
$content = $event->response->getContent();
|
2022-04-14 12:42:30 +02:00
|
|
|
$headers = $event->response->getHeaders();
|
2015-09-30 22:28:16 +02:00
|
|
|
}
|
2022-04-14 12:42:30 +02:00
|
|
|
call_user_func($this->function, $content, $error, $headers);
|
2015-09-30 22:28:16 +02:00
|
|
|
});
|
|
|
|
|
2015-10-13 19:12:27 +02:00
|
|
|
$fileReader = $this->maniaControl->getFileReader();
|
2015-09-30 22:28:16 +02:00
|
|
|
$fileReader->addRequest($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $url
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setURL($url) {
|
|
|
|
$this->url = $url;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-30 22:42:52 +02:00
|
|
|
* @param callable $function
|
2015-09-30 22:28:16 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setCallable($function) {
|
|
|
|
$this->function = $function;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getContent() {
|
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $content
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setContent($content) {
|
|
|
|
$this->content = $content;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getCompression() {
|
|
|
|
return $this->compression;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean $compression
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setCompression($compression) {
|
|
|
|
$this->compression = $compression;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHeaders() {
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $headers
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setHeaders($headers) {
|
2017-03-26 19:44:55 +02:00
|
|
|
if (is_array($headers)) {
|
2015-11-06 16:45:27 +01:00
|
|
|
$this->headers = $headers;
|
|
|
|
}
|
2015-09-30 22:28:16 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2015-09-30 22:33:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContentType() {
|
|
|
|
return $this->contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $contentType
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setContentType($contentType) {
|
|
|
|
$this->contentType = $contentType;
|
|
|
|
return $this;
|
|
|
|
}
|
2017-05-08 18:53:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the Timeout Time
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getTimeout() {
|
|
|
|
return $this->timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Timeout Time
|
|
|
|
*
|
|
|
|
* @param int $timeout
|
|
|
|
*/
|
|
|
|
public function setTimeout($timeout) {
|
|
|
|
$this->timeout = $timeout;
|
|
|
|
}
|
2017-07-04 12:46:49 +02:00
|
|
|
}
|