2018-09-28 11:41:24 +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-11-12 22:57:32 +00:00
|
|
|
use daita\MySmallPhpTools\Exceptions\ArrayNotFoundException;
|
2018-10-12 06:44:35 +00:00
|
|
|
use daita\MySmallPhpTools\Model\Request;
|
2018-11-12 22:57:32 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
|
|
|
use daita\MySmallPhpTools\Traits\TPathTools;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Exceptions\RequestException;
|
2018-11-12 22:57:32 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Model\Instance;
|
|
|
|
use OCA\Social\Model\InstancePath;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
class InstanceService {
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
use TPathTools;
|
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/** @var ConfigService */
|
|
|
|
private $configService;
|
|
|
|
|
|
|
|
|
|
|
|
private $curlService;
|
|
|
|
|
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UriIdService constructor.
|
|
|
|
*
|
|
|
|
* @param ConfigService $configService
|
|
|
|
* @param CurlService $curlService
|
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ConfigService $configService, CurlService $curlService, MiscService $miscService
|
|
|
|
) {
|
|
|
|
$this->configService = $configService;
|
|
|
|
$this->curlService = $curlService;
|
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
/**
|
|
|
|
* @param string $account
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @throws RequestException
|
|
|
|
*/
|
|
|
|
public function retrieveAccount(string $account) {
|
|
|
|
$account = $this->withoutBeginAt($account);
|
|
|
|
|
|
|
|
list($username, $host) = explode('@', $account);
|
|
|
|
|
|
|
|
$request = new Request('/.well-known/webfinger');
|
|
|
|
$request->addData('resource', 'acct:' . $account);
|
|
|
|
$request->setAddress($host);
|
|
|
|
$result = $this->curlService->request($request);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$link = $this->extractArray('rel', 'self', $this->getArray('links', $result));
|
|
|
|
} catch (ArrayNotFoundException $e) {
|
|
|
|
throw new RequestException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->retrieveObject($this->get('href', $link, ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
*
|
|
|
|
* @return mixed
|
2018-10-01 12:14:23 +00:00
|
|
|
* @throws RequestException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
|
|
|
public function retrieveObject($id) {
|
|
|
|
$url = parse_url($id);
|
|
|
|
$request = new Request($url['path'], Request::TYPE_GET);
|
|
|
|
$request->setAddress($url['host']);
|
|
|
|
|
2018-11-14 10:38:34 +00:00
|
|
|
$result = $this->curlService->request($request);
|
|
|
|
if (is_array($result)) {
|
|
|
|
$result['_host'] = $url['host'];
|
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-11-14 10:38:34 +00:00
|
|
|
return $result;
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
|
|
|
|
/**
|
2018-11-12 22:57:32 +00:00
|
|
|
* @param ACore $activity
|
2018-10-01 12:14:23 +00:00
|
|
|
*
|
|
|
|
* @return Instance[]
|
|
|
|
*/
|
2018-11-12 22:57:32 +00:00
|
|
|
public function getInstancesFromActivity(ACore $activity): array {
|
2018-10-01 12:14:23 +00:00
|
|
|
$instances = [];
|
2018-11-12 22:57:32 +00:00
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
foreach ($activity->getInstancePaths() as $instancePath) {
|
|
|
|
$this->addInstances($instancePath, $instances);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param InstancePath $instancePath
|
|
|
|
* @param Instance[] $instances
|
|
|
|
*/
|
|
|
|
private function addInstances(InstancePath $instancePath, array &$instances) {
|
|
|
|
$address = $this->getHostFromUriId($instancePath->getUri());
|
2018-11-12 22:57:32 +00:00
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
if ($address === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($instances as $instance) {
|
|
|
|
if ($instance->getAddress() === $address) {
|
|
|
|
$instance->addPath($instancePath);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$instance = new Instance($address);
|
|
|
|
$instance->addPath($instancePath);
|
|
|
|
$instances[] = $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uriId
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getHostFromUriId(string $uriId) {
|
|
|
|
$ignoreThose = [
|
|
|
|
'',
|
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
];
|
|
|
|
|
|
|
|
if (in_array($uriId, $ignoreThose)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = parse_url($uriId);
|
|
|
|
if (!is_array($url) || !array_key_exists('host', $url)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url['host'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
2018-10-01 12:14:23 +00:00
|
|
|
|