3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
24 use Friendica\Database\DBA;
26 use Friendica\Model\User;
27 use Friendica\Network\HTTPException;
28 use Friendica\Protocol\Activity;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Protocol\Diaspora;
31 use Friendica\Protocol\OStatus;
32 use Friendica\Protocol\Salmon;
35 * Manage compatibility with federated networks
40 const ACTIVITYPUB = 'apub'; // ActivityPub (Pleroma, Mastodon, Osada, ...)
41 const DFRN = 'dfrn'; // Friendica, Mistpark, other DFRN implementations
42 const DIASPORA = 'dspr'; // Diaspora, Hubzilla, Socialhome, Ganggo
43 const FEED = 'feed'; // RSS/Atom feeds with no known "post/notify" protocol
44 const MAIL = 'mail'; // IMAP/POP
45 const OSTATUS = 'stat'; // GNU Social and other OStatus implementations
47 const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
49 const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
51 const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
53 // Supported through a connector
54 const DIASPORA2 = 'dspc'; // Diaspora connector
55 const LINKEDIN = 'lnkd'; // LinkedIn
56 const PUMPIO = 'pump'; // pump.io
57 const STATUSNET = 'stac'; // Statusnet connector
58 const TWITTER = 'twit'; // Twitter
59 const DISCOURSE = 'dscs'; // Discourse
62 const APPNET = 'apdn'; // app.net - Dead protocol
63 const FACEBOOK = 'face'; // Facebook API - Not working anymore, API is closed
64 const GPLUS = 'goog'; // Google+ - Dead in 2019
66 // Currently unsupported
67 const ICALENDAR = 'ical'; // iCalendar
68 const MYSPACE = 'mysp'; // MySpace
69 const NEWS = 'nntp'; // Network News Transfer Protocol
70 const PNUT = 'pnut'; // pnut.io
71 const XMPP = 'xmpp'; // XMPP
72 const ZOT = 'zot!'; // Zot!
74 const PHANTOM = 'unkn'; // Place holder
77 * Returns whether the provided protocol supports following
81 * @throws HTTPException\InternalServerErrorException
83 public static function supportsFollow($protocol): bool
85 if (in_array($protocol, self::NATIVE_SUPPORT)) {
90 'protocol' => $protocol,
93 Hook::callAll('support_follow', $hook_data);
95 return $hook_data['result'] === true;
99 * Returns whether the provided protocol supports revoking inbound follows
103 * @throws HTTPException\InternalServerErrorException
105 public static function supportsRevokeFollow($protocol): bool
107 if (in_array($protocol, self::NATIVE_SUPPORT)) {
112 'protocol' => $protocol,
115 Hook::callAll('support_revoke_follow', $hook_data);
117 return $hook_data['result'] === true;
121 * Returns the address string for the provided profile URL
123 * @param string $profile_url
127 public static function getAddrFromProfileUrl($profile_url)
129 $network = self::matchByProfileUrl($profile_url, $matches);
131 if ($network === self::PHANTOM) {
135 $addr = $matches[2] . '@' . $matches[1];
141 * Guesses the network from a profile URL
143 * @param string $profile_url
144 * @param array $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
147 public static function matchByProfileUrl($profile_url, &$matches = [])
149 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
150 return self::TWITTER;
153 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
157 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
161 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
165 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
166 return self::DIASPORA;
169 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
170 // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
171 return self::DIASPORA;
174 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
175 $statusnet_host = $matches[1];
176 $statusnet_user = $matches[2];
177 $UserData = DI::httpClient()->fetch('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
178 $user = json_decode($UserData);
180 $matches[2] = $user->screen_name;
181 return self::STATUSNET;
186 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
187 || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
189 return self::ACTIVITYPUB;
192 // pumpio (http://host.name/user)
193 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
197 return self::PHANTOM;
201 * Returns a formatted mention from a profile URL and a display name
203 * @param string $profile_url
204 * @param string $display_name
208 public static function formatMention($profile_url, $display_name)
210 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
214 * Send a follow message to a remote server.
216 * @param int $uid User Id
217 * @param array $contact Contact being followed
218 * @param ?string $protocol Expected protocol
219 * @return bool Only returns false in the unlikely case an ActivityPub contact ID doesn't exist (???)
220 * @throws HTTPException\InternalServerErrorException
221 * @throws \ImagickException
223 public static function follow(int $uid, array $contact, ?string $protocol = null): bool
225 $owner = User::getOwnerDataById($uid);
226 if (!DBA::isResult($owner)) {
230 $protocol = $protocol ?? $contact['protocol'];
232 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
233 // create a follow slap
235 'verb' => Activity::FOLLOW,
236 'gravity' => GRAVITY_ACTIVITY,
237 'follow' => $contact['url'],
244 $slap = OStatus::salmon($item, $owner);
246 if (!empty($contact['notify'])) {
247 Salmon::slapper($owner, $contact['notify'], $slap);
249 } elseif ($protocol == Protocol::DIASPORA) {
250 $contact = Diaspora::sendShare($owner, $contact);
251 Logger::notice('share returns: ' . $contact);
252 } elseif ($protocol == Protocol::ACTIVITYPUB) {
253 $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
254 if (empty($activity_id)) {
255 // This really should never happen
259 $success = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $owner['uid'], $activity_id);
260 Logger::notice('Follow returns: ' . $success);
267 * Sends an unfriend message. Does not remove the contact
269 * @param array $user User unfriending
270 * @param array $contact Contact unfriended
271 * @return bool|null true if successful, false if not, null if no remote action was performed
272 * @throws HTTPException\InternalServerErrorException
273 * @throws \ImagickException
275 public static function terminateFriendship(array $user, array $contact): ?bool
277 if (empty($contact['network'])) {
278 throw new \InvalidArgumentException('Missing network key in contact array');
281 $protocol = $contact['network'];
282 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
283 $protocol = $contact['protocol'];
286 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
287 // create an unfollow slap
289 $item['verb'] = Activity::O_UNFOLLOW;
290 $item['gravity'] = GRAVITY_ACTIVITY;
291 $item['follow'] = $contact['url'];
296 $slap = OStatus::salmon($item, $user);
298 if (empty($contact['notify'])) {
299 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
302 return Salmon::slapper($user, $contact['notify'], $slap) === 0;
303 } elseif ($protocol == Protocol::DIASPORA) {
304 return Diaspora::sendUnshare($user, $contact) > 0;
305 } elseif ($protocol == Protocol::ACTIVITYPUB) {
306 return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
309 // Catch-all hook for connector addons
311 'contact' => $contact,
314 Hook::callAll('unfollow', $hook_data);
316 return $hook_data['result'];
320 * Revoke an incoming follow from the provided contact
322 * @param array $contact Private contact (uid != 0) array
323 * @return bool|null true if successful, false if not, null if no action was performed
324 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
325 * @throws \ImagickException
327 public static function revokeFollow(array $contact): ?bool
329 if (empty($contact['network'])) {
330 throw new \InvalidArgumentException('Missing network key in contact array');
333 $protocol = $contact['network'];
334 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
335 $protocol = $contact['protocol'];
338 if ($protocol == Protocol::ACTIVITYPUB) {
339 return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
342 // Catch-all hook for connector addons
344 'contact' => $contact,
347 Hook::callAll('revoke_follow', $hook_data);
349 return $hook_data['result'];
353 * Send a block message to a remote server. Only useful for connector addons.
355 * @param array $contact Public contact record to block
356 * @param int $uid User issuing the block
357 * @return bool|null true if successful, false if not, null if no action was performed
358 * @throws HTTPException\InternalServerErrorException
360 public static function block(array $contact, int $uid): ?bool
362 // Catch-all hook for connector addons
364 'contact' => $contact,
368 Hook::callAll('block', $hook_data);
370 return $hook_data['result'];
374 * Send an unblock message to a remote server. Only useful for connector addons.
376 * @param array $contact Public contact record to unblock
377 * @param int $uid User revoking the block
378 * @return bool|null true if successful, false if not, null if no action was performed
379 * @throws HTTPException\InternalServerErrorException
381 public static function unblock(array $contact, int $uid): ?bool
383 // Catch-all hook for connector addons
385 'contact' => $contact,
389 Hook::callAll('unblock', $hook_data);
391 return $hook_data['result'];