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 the address string for the provided profile URL
77 * @param string $profile_url
81 public static function getAddrFromProfileUrl($profile_url)
83 $network = self::matchByProfileUrl($profile_url, $matches);
85 if ($network === self::PHANTOM) {
89 $addr = $matches[2] . '@' . $matches[1];
95 * Guesses the network from a profile URL
97 * @param string $profile_url
98 * @param array $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
101 public static function matchByProfileUrl($profile_url, &$matches = [])
103 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
104 return self::TWITTER;
107 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
111 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
115 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
119 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
120 return self::DIASPORA;
123 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
124 // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
125 return self::DIASPORA;
128 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
129 $statusnet_host = $matches[1];
130 $statusnet_user = $matches[2];
131 $UserData = DI::httpClient()->fetch('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
132 $user = json_decode($UserData);
134 $matches[2] = $user->screen_name;
135 return self::STATUSNET;
140 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
141 || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
143 return self::ACTIVITYPUB;
146 // pumpio (http://host.name/user)
147 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
151 return self::PHANTOM;
155 * Returns a formatted mention from a profile URL and a display name
157 * @param string $profile_url
158 * @param string $display_name
162 public static function formatMention($profile_url, $display_name)
164 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
168 * Sends an unfriend message. Does not remove the contact
170 * @param array $user User unfriending
171 * @param array $contact Contact unfriended
172 * @param boolean $two_way Revoke eventual inbound follow as well
173 * @return bool|null true if successful, false if not, null if no action was performed
174 * @throws HTTPException\InternalServerErrorException
175 * @throws \ImagickException
177 public static function terminateFriendship(array $user, array $contact, bool $two_way = false): bool
179 if (empty($contact['network'])) {
180 throw new \InvalidArgumentException('Missing network key in contact array');
183 $protocol = $contact['network'];
184 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
185 $protocol = $contact['protocol'];
188 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
189 // create an unfollow slap
191 $item['verb'] = Activity::O_UNFOLLOW;
192 $item['gravity'] = GRAVITY_ACTIVITY;
193 $item['follow'] = $contact['url'];
198 $slap = OStatus::salmon($item, $user);
200 if (empty($contact['notify'])) {
201 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
204 return Salmon::slapper($user, $contact['notify'], $slap) === 0;
205 } elseif ($protocol == Protocol::DIASPORA) {
206 return Diaspora::sendUnshare($user, $contact) > 0;
207 } elseif ($protocol == Protocol::ACTIVITYPUB) {
209 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
212 return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
215 // Catch-all addon hook
217 'contact' => $contact,
218 'two_way' => $two_way,
221 Hook::callAll('unfollow', $hook_data);
223 return $hook_data['result'];