2018-09-20 07:42:52 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nextcloud - Social Support
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Social\Service;
|
|
|
|
|
|
|
|
|
2018-10-12 06:44:35 +00:00
|
|
|
use daita\MySmallPhpTools\Model\Request;
|
2018-11-27 16:59:19 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
|
|
|
use daita\MySmallPhpTools\Traits\TPathTools;
|
2018-11-28 15:21:56 +00:00
|
|
|
use Exception;
|
|
|
|
use OCA\Social\Exceptions\Request410Exception;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Exceptions\RequestException;
|
2018-11-27 16:59:19 +00:00
|
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
2018-09-20 07:42:52 +00:00
|
|
|
|
|
|
|
class CurlService {
|
|
|
|
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
use TArrayTools;
|
|
|
|
use TPathTools;
|
|
|
|
|
|
|
|
|
|
|
|
const ASYNC_TOKEN = '/async/token/{token}';
|
2018-11-28 11:08:07 +00:00
|
|
|
const USER_AGENT = 'Nextcloud Social';
|
2018-11-27 16:59:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** @var ConfigService */
|
|
|
|
private $configService;
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* CurlService constructor.
|
2018-09-20 07:42:52 +00:00
|
|
|
*
|
2018-11-27 16:59:19 +00:00
|
|
|
* @param ConfigService $configService
|
2018-09-20 07:42:52 +00:00
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
2018-11-27 16:59:19 +00:00
|
|
|
public function __construct(ConfigService $configService, MiscService $miscService) {
|
|
|
|
$this->configService = $configService;
|
2018-09-20 07:42:52 +00:00
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return array
|
2018-10-01 12:14:23 +00:00
|
|
|
* @throws RequestException
|
2018-11-28 15:21:56 +00:00
|
|
|
* @throws Request410Exception
|
2018-09-20 07:42:52 +00:00
|
|
|
*/
|
2018-09-28 11:41:24 +00:00
|
|
|
public function request(Request $request): array {
|
2018-12-04 23:46:04 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
$curl = $this->initRequest($request);
|
2018-09-20 07:42:52 +00:00
|
|
|
|
|
|
|
$this->initRequestPost($curl, $request);
|
|
|
|
$this->initRequestPut($curl, $request);
|
|
|
|
$this->initRequestDelete($curl, $request);
|
|
|
|
|
|
|
|
$result = curl_exec($curl);
|
|
|
|
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
$this->parseRequestResultCode301($code);
|
2018-09-28 11:41:24 +00:00
|
|
|
// $this->parseRequestResultCode401($code);
|
2018-10-01 12:14:23 +00:00
|
|
|
$this->parseRequestResultCode404($code, $request);
|
2018-11-28 15:21:56 +00:00
|
|
|
$this->parseRequestResultCode410($code);
|
2018-09-20 07:42:52 +00:00
|
|
|
// $this->parseRequestResultCode503($code);
|
|
|
|
// $this->parseRequestResultCode500($code);
|
|
|
|
// $this->parseRequestResult($result);
|
2018-10-01 12:14:23 +00:00
|
|
|
|
2018-11-16 11:47:14 +00:00
|
|
|
$ret = json_decode((string)$result, true);
|
2018-11-22 04:38:29 +00:00
|
|
|
// if ($ret === null) {
|
|
|
|
// throw new RequestException('500 Internal server error - could not parse JSON response');
|
|
|
|
// }
|
2018-09-28 11:41:24 +00:00
|
|
|
if (!is_array($ret)) {
|
|
|
|
$ret = ['_result' => $result];
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret['_address'] = $request->getAddress();
|
2018-11-21 19:22:46 +00:00
|
|
|
$ret['_path'] = $request->getUrl();
|
2018-09-28 11:41:24 +00:00
|
|
|
$ret['_code'] = $code;
|
2018-09-20 07:42:52 +00:00
|
|
|
|
2018-12-04 23:46:04 +00:00
|
|
|
$this->miscService->log(
|
|
|
|
'[>>] request: ' . json_encode($request) . ' - result: ' . json_encode($ret), 1
|
|
|
|
);
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
return $ret;
|
2018-09-20 07:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-04 23:46:04 +00:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
2018-11-28 11:08:07 +00:00
|
|
|
public function assignUserAgent(Request $request) {
|
|
|
|
$request->setUserAgent(
|
|
|
|
self::USER_AGENT . ' ' . $this->configService->getAppValue('installed_version')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
*
|
|
|
|
* @throws SocialAppConfigException
|
|
|
|
*/
|
|
|
|
public function asyncWithToken(string $token) {
|
|
|
|
$address = $this->configService->getUrlSocial();
|
|
|
|
$parse = parse_url($address);
|
|
|
|
$host = $this->get('host', $parse, '');
|
|
|
|
$path = $this->withEndSlash($this->get('path', $parse, '')) . $this->withoutBeginSlash(
|
|
|
|
self::ASYNC_TOKEN
|
|
|
|
);
|
|
|
|
$path = str_replace('{token}', $token, $path);
|
|
|
|
|
|
|
|
$request = new Request($path, Request::TYPE_POST);
|
|
|
|
$request->setAddress($host);
|
|
|
|
try {
|
|
|
|
$this->request($request);
|
2018-11-28 15:21:56 +00:00
|
|
|
} catch (Exception $e) {
|
2018-11-27 16:59:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return resource
|
|
|
|
*/
|
2018-09-28 11:41:24 +00:00
|
|
|
private function initRequest(Request $request) {
|
2018-09-20 07:42:52 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
$curl = $this->generateCurlRequest($request);
|
|
|
|
$headers = $request->getHeaders();
|
2018-09-20 07:42:52 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
|
2018-09-20 07:42:52 +00:00
|
|
|
|
2018-11-28 11:08:07 +00:00
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
|
2018-12-04 10:58:00 +00:00
|
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $request->getTimeout());
|
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $request->getTimeout());
|
2018-09-20 07:42:52 +00:00
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
return $curl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return resource
|
|
|
|
*/
|
2018-09-28 11:41:24 +00:00
|
|
|
private function generateCurlRequest(Request $request) {
|
|
|
|
$url = 'https://' . $request->getAddress() . $request->getParsedUrl();
|
2018-09-20 07:42:52 +00:00
|
|
|
if ($request->getType() !== Request::TYPE_GET) {
|
|
|
|
$curl = curl_init($url);
|
|
|
|
} else {
|
2018-11-12 22:57:32 +00:00
|
|
|
$curl = curl_init($url . '?' . $request->getUrlData());
|
2018-09-20 07:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $curl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param resource $curl
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
private function initRequestPost($curl, Request $request) {
|
|
|
|
if ($request->getType() !== Request::TYPE_POST) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param resource $curl
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
private function initRequestPut($curl, Request $request) {
|
|
|
|
if ($request->getType() !== Request::TYPE_PUT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param resource $curl
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
private function initRequestDelete($curl, Request $request) {
|
|
|
|
if ($request->getType() !== Request::TYPE_DELETE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $code
|
|
|
|
*
|
2018-10-01 12:14:23 +00:00
|
|
|
* @throws RequestException
|
2018-09-20 07:42:52 +00:00
|
|
|
*/
|
|
|
|
private function parseRequestResultCode301($code) {
|
|
|
|
if ($code === 301) {
|
2018-10-01 12:14:23 +00:00
|
|
|
throw new RequestException('301 Moved Permanently');
|
2018-09-20 07:42:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-20 12:07:22 +00:00
|
|
|
/**
|
|
|
|
* @param int $code
|
|
|
|
*
|
2018-10-01 12:14:23 +00:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2018-11-28 15:21:56 +00:00
|
|
|
* @throws Request410Exception
|
2018-09-20 12:07:22 +00:00
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
private function parseRequestResultCode404(int $code, Request $request) {
|
2018-09-20 12:07:22 +00:00
|
|
|
if ($code === 404) {
|
2018-11-28 15:21:56 +00:00
|
|
|
throw new Request410Exception('404 Not Found - ' . json_encode($request));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $code
|
|
|
|
*
|
|
|
|
* @throws Request410Exception
|
|
|
|
*/
|
|
|
|
private function parseRequestResultCode410(int $code) {
|
|
|
|
if ($code === 410) {
|
|
|
|
throw new Request410Exception();
|
2018-09-20 12:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
|
|
|
|
}
|