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;
25 use Friendica\Network\HTTPException;
26 use Friendica\Protocol\Activity;
27 use Friendica\Protocol\ActivityPub;
28 use Friendica\Protocol\Diaspora;
29 use Friendica\Protocol\OStatus;
30 use Friendica\Protocol\Salmon;
33 * Manage compatibility with federated networks
38 const ACTIVITYPUB = 'apub'; // ActivityPub (Pleroma, Mastodon, Osada, ...)
39 const DFRN = 'dfrn'; // Friendica, Mistpark, other DFRN implementations
40 const DIASPORA = 'dspr'; // Diaspora, Hubzilla, Socialhome, Ganggo
41 const FEED = 'feed'; // RSS/Atom feeds with no known "post/notify" protocol
42 const MAIL = 'mail'; // IMAP/POP
43 const OSTATUS = 'stat'; // GNU Social and other OStatus implementations
45 const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
47 const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
49 const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
51 // Supported through a connector
52 const DIASPORA2 = 'dspc'; // Diaspora connector
53 const LINKEDIN = 'lnkd'; // LinkedIn
54 const PUMPIO = 'pump'; // pump.io
55 const STATUSNET = 'stac'; // Statusnet connector
56 const TWITTER = 'twit'; // Twitter
57 const DISCOURSE = 'dscs'; // Discourse
60 const APPNET = 'apdn'; // app.net - Dead protocol
61 const FACEBOOK = 'face'; // Facebook API - Not working anymore, API is closed
62 const GPLUS = 'goog'; // Google+ - Dead in 2019
64 // Currently unsupported
65 const ICALENDAR = 'ical'; // iCalendar
66 const MYSPACE = 'mysp'; // MySpace
67 const NEWS = 'nntp'; // Network News Transfer Protocol
68 const PNUT = 'pnut'; // pnut.io
69 const XMPP = 'xmpp'; // XMPP
70 const ZOT = 'zot!'; // Zot!
72 const PHANTOM = 'unkn'; // Place holder
75 * Returns whether the provided protocol supports following
79 * @throws HTTPException\InternalServerErrorException
81 public static function supportsFollow($protocol): bool
83 if (in_array($protocol, self::NATIVE_SUPPORT)) {
88 Hook::callAll('support_follow', $result);
90 return $result === true;
94 * Returns whether the provided protocol supports revoking inbound follows
98 * @throws HTTPException\InternalServerErrorException
100 public static function supportsRevokeFollow($protocol): bool
102 if (in_array($protocol, self::NATIVE_SUPPORT)) {
107 Hook::callAll('support_revoke_follow', $result);
109 return $result === true;
113 * Returns the address string for the provided profile URL
115 * @param string $profile_url
119 public static function getAddrFromProfileUrl($profile_url)
121 $network = self::matchByProfileUrl($profile_url, $matches);
123 if ($network === self::PHANTOM) {
127 $addr = $matches[2] . '@' . $matches[1];
133 * Guesses the network from a profile URL
135 * @param string $profile_url
136 * @param array $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
139 public static function matchByProfileUrl($profile_url, &$matches = [])
141 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
142 return self::TWITTER;
145 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
149 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
153 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
157 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
158 return self::DIASPORA;
161 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
162 // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
163 return self::DIASPORA;
166 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
167 $statusnet_host = $matches[1];
168 $statusnet_user = $matches[2];
169 $UserData = DI::httpClient()->fetch('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
170 $user = json_decode($UserData);
172 $matches[2] = $user->screen_name;
173 return self::STATUSNET;
178 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
179 || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
181 return self::ACTIVITYPUB;
184 // pumpio (http://host.name/user)
185 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
189 return self::PHANTOM;
193 * Returns a formatted mention from a profile URL and a display name
195 * @param string $profile_url
196 * @param string $display_name
200 public static function formatMention($profile_url, $display_name)
202 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
206 * Sends an unfriend message. Does not remove the contact
208 * @param array $user User unfriending
209 * @param array $contact Contact unfriended
210 * @param boolean $two_way Revoke eventual inbound follow as well
211 * @return bool|null true if successful, false if not, null if no action was performed
212 * @throws HTTPException\InternalServerErrorException
213 * @throws \ImagickException
215 public static function terminateFriendship(array $user, array $contact, bool $two_way = false): bool
217 if (empty($contact['network'])) {
218 throw new \InvalidArgumentException('Missing network key in contact array');
221 $protocol = $contact['network'];
222 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
223 $protocol = $contact['protocol'];
226 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
227 // create an unfollow slap
229 $item['verb'] = Activity::O_UNFOLLOW;
230 $item['gravity'] = GRAVITY_ACTIVITY;
231 $item['follow'] = $contact['url'];
236 $slap = OStatus::salmon($item, $user);
238 if (empty($contact['notify'])) {
239 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
242 return Salmon::slapper($user, $contact['notify'], $slap) === 0;
243 } elseif ($protocol == Protocol::DIASPORA) {
244 return Diaspora::sendUnshare($user, $contact) > 0;
245 } elseif ($protocol == Protocol::ACTIVITYPUB) {
247 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
250 return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
253 // Catch-all hook for connector addons
255 'contact' => $contact,
256 'two_way' => $two_way,
259 Hook::callAll('unfollow', $hook_data);
261 return $hook_data['result'];
265 * Revoke an incoming follow from the provided contact
267 * @param array $contact Private contact (uid != 0) array
268 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
269 * @throws \ImagickException
271 public static function revokeFollow(array $contact)
273 if (empty($contact['network'])) {
274 throw new \InvalidArgumentException('Missing network key in contact array');
277 $protocol = $contact['network'];
278 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
279 $protocol = $contact['protocol'];
282 if ($protocol == Protocol::ACTIVITYPUB) {
283 return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
286 // Catch-all hook for connector addons
288 'contact' => $contact,
291 Hook::callAll('revoke_follow', $hook_data);
293 return $hook_data['result'];