Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.8 KiB

3 years ago
  1. <?php
  2. /**
  3. * @file src/Model/APContact.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\BaseObject;
  7. use Friendica\Content\Text\HTML;
  8. use Friendica\Core\Logger;
  9. use Friendica\Database\DBA;
  10. use Friendica\Protocol\ActivityPub;
  11. use Friendica\Util\Network;
  12. use Friendica\Util\JsonLD;
  13. use Friendica\Util\DateTimeFormat;
  14. use Friendica\Util\Strings;
  15. require_once 'boot.php';
  16. class APContact extends BaseObject
  17. {
  18. /**
  19. * Resolves the profile url from the address by using webfinger
  20. *
  21. * @param string $addr profile address (user@domain.tld)
  22. * @return string url
  23. */
  24. private static function addrToUrl($addr)
  25. {
  26. $addr_parts = explode('@', $addr);
  27. if (count($addr_parts) != 2) {
  28. return false;
  29. }
  30. $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
  31. $curlResult = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
  32. if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
  33. return false;
  34. }
  35. $data = json_decode($curlResult->getBody(), true);
  36. if (empty($data['links'])) {
  37. return false;
  38. }
  39. foreach ($data['links'] as $link) {
  40. if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
  41. continue;
  42. }
  43. if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
  44. return $link['href'];
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * Fetches a profile from a given url
  51. *
  52. * @param string $url profile url
  53. * @param boolean $update true = always update, false = never update, null = update when not found
  54. * @return array profile array
  55. */
  56. public static function getByURL($url, $update = null)
  57. {
  58. if (empty($url)) {
  59. return false;
  60. }
  61. if (empty($update)) {
  62. $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
  63. if (DBA::isResult($apcontact)) {
  64. return $apcontact;
  65. }
  66. $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
  67. if (DBA::isResult($apcontact)) {
  68. return $apcontact;
  69. }
  70. $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
  71. if (DBA::isResult($apcontact)) {
  72. return $apcontact;
  73. }
  74. if (!is_null($update)) {
  75. return false;
  76. }
  77. }
  78. if (empty(parse_url($url, PHP_URL_SCHEME))) {
  79. $url = self::addrToUrl($url);
  80. if (empty($url)) {
  81. return false;
  82. }
  83. }
  84. $data = ActivityPub::fetchContent($url);
  85. if (empty($data)) {
  86. return false;
  87. }
  88. $compacted = JsonLD::compact($data);
  89. if (empty($compacted['@id'])) {
  90. return false;
  91. }
  92. $apcontact = [];
  93. $apcontact['url'] = $compacted['@id'];
  94. $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
  95. $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
  96. $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
  97. $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
  98. $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
  99. $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
  100. $apcontact['sharedinbox'] = '';
  101. if (!empty($compacted['as:endpoints'])) {
  102. $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
  103. }
  104. $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
  105. $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
  106. if (empty($apcontact['name'])) {
  107. $apcontact['name'] = $apcontact['nick'];
  108. }
  109. $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
  110. $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
  111. if (is_array($apcontact['photo'])) {
  112. $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
  113. }
  114. $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
  115. if (is_array($apcontact['alias'])) {
  116. $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
  117. }
  118. if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
  119. return false;
  120. }
  121. $parts = parse_url($apcontact['url']);
  122. unset($parts['scheme']);
  123. unset($parts['path']);
  124. $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
  125. $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
  126. // To-Do
  127. // manuallyApprovesFollowers
  128. // Unhandled
  129. // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
  130. // Unhandled from Misskey
  131. // sharedInbox, isCat
  132. // Unhandled from Kroeg
  133. // kroeg:blocks, updated
  134. // Check if the address is resolvable
  135. if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
  136. $parts = parse_url($apcontact['url']);
  137. unset($parts['path']);
  138. $apcontact['baseurl'] = Network::unparseURL($parts);
  139. } else {
  140. $apcontact['addr'] = null;
  141. $apcontact['baseurl'] = null;
  142. }
  143. if ($apcontact['url'] == $apcontact['alias']) {
  144. $apcontact['alias'] = null;
  145. }
  146. $apcontact['updated'] = DateTimeFormat::utcNow();
  147. DBA::update('apcontact', $apcontact, ['url' => $url], true);
  148. // Update some data in the contact table with various ways to catch them all
  149. $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
  150. DBA::update('contact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
  151. $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => Strings::normaliseLink($url)]);
  152. while ($contact = DBA::fetch($contacts)) {
  153. Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
  154. }
  155. DBA::close($contacts);
  156. // Update the gcontact table
  157. DBA::update('gcontact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
  158. Logger::log('Updated profile for ' . $url, Logger::DEBUG);
  159. return $apcontact;
  160. }
  161. }