diff options
Diffstat (limited to 'lib/Service/CurlService.php')
-rw-r--r-- | lib/Service/CurlService.php | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index cbea8819..ccf315cd 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -37,6 +37,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TPathTools; use Exception; use OCA\Social\AP; +use OCA\Social\Exceptions\HostMetaException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\RedundancyLimitException; @@ -121,7 +122,13 @@ class CurlService { throw new InvalidResourceException(); } - $request = new Request('/.well-known/webfinger'); + try { + $path = $this->hostMeta($host); + } catch (HostMetaException $e) { + $path = '/.well-known/webfinger'; + } + + $request = new Request($path); $request->addData('resource', 'acct:' . $account); $request->setAddress($host); @@ -139,6 +146,32 @@ class CurlService { /** + * @param string $host + * + * @return string + * @throws HostMetaException + */ + public function hostMeta(string &$host): string { + $request = new Request('/.well-known/host-meta'); + $request->setAddress($host); + + try { + $result = $this->request($request); + } catch (Exception $e) { + $this->miscService->log( + 'hostMeta Exception - ' . get_class($e) . ' - ' . $e->getMessage(), 0 + ); + throw new HostMetaException($e->getMessage()); + } + + $url = $this->get('Link.@attributes.template', $result, ''); + $host = parse_url($url, PHP_URL_HOST); + + return parse_url($url, PHP_URL_PATH); + } + + + /** * @param string $account * * @return Person @@ -252,9 +285,16 @@ class CurlService { } $this->miscService->log( - '[>>] request: ' . json_encode($request) . ' - result: ' . $result, 1 + '[>>] request: ' . json_encode($request) . ' - content-type: ' + . $request->getContentType() . ' - result: ' . $result, 1 ); + if (strpos($request->getContentType(), 'application/xrd') === 0) { + $xml = simplexml_load_string($result); + $result = json_encode($xml, JSON_UNESCAPED_SLASHES); + $this->miscService->log('XRD conversion to JSON: ' . $result, 1); + } + $result = json_decode((string)$result, true); if (is_array($result)) { return $result; @@ -427,6 +467,8 @@ class CurlService { $this->parseRequestResultCurl($curl, $request); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); + $request->setContentType(($contentType === null) ? '' : $contentType); $request->setResultCode($code); $this->parseRequestResultCode301($code, $request); |