]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Add revoke follow feature
[friendica.git] / src / Core / Protocol.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\DI;
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;
31
32 /**
33  * Manage compatibility with federated networks
34  */
35 class Protocol
36 {
37         // Native support
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
44
45         const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
46
47         const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
48
49         const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
50
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
58
59         // Dead protocols
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
63
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!
71
72         const PHANTOM   = 'unkn';    // Place holder
73
74         /**
75          * Returns whether the provided protocol supports following
76          *
77          * @param $protocol
78          * @return bool
79          * @throws HTTPException\InternalServerErrorException
80          */
81         public static function supportsFollow($protocol): bool
82         {
83                 if (in_array($protocol, self::NATIVE_SUPPORT)) {
84                         return true;
85                 }
86
87                 $result = null;
88                 Hook::callAll('support_follow', $result);
89
90                 return $result === true;
91         }
92
93         /**
94          * Returns whether the provided protocol supports revoking inbound follows
95          *
96          * @param $protocol
97          * @return bool
98          * @throws HTTPException\InternalServerErrorException
99          */
100         public static function supportsRevokeFollow($protocol): bool
101         {
102                 if (in_array($protocol, self::NATIVE_SUPPORT)) {
103                         return true;
104                 }
105
106                 $result = null;
107                 Hook::callAll('support_revoke_follow', $result);
108
109                 return $result === true;
110         }
111
112         /**
113          * Returns the address string for the provided profile URL
114          *
115          * @param string $profile_url
116          * @return string
117          * @throws \Exception
118          */
119         public static function getAddrFromProfileUrl($profile_url)
120         {
121                 $network = self::matchByProfileUrl($profile_url, $matches);
122
123                 if ($network === self::PHANTOM) {
124                         return "";
125                 }
126
127                 $addr = $matches[2] . '@' . $matches[1];
128
129                 return $addr;
130         }
131
132         /**
133          * Guesses the network from a profile URL
134          *
135          * @param string $profile_url
136          * @param array  $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
137          * @return string
138          */
139         public static function matchByProfileUrl($profile_url, &$matches = [])
140         {
141                 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
142                         return self::TWITTER;
143                 }
144
145                 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
146                         return self::APPNET;
147                 }
148
149                 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
150                         return self::GPLUS;
151                 }
152
153                 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
154                         return self::DFRN;
155                 }
156
157                 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
158                         return self::DIASPORA;
159                 }
160
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;
164                 }
165
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);
171                         if ($user) {
172                                 $matches[2] = $user->screen_name;
173                                 return self::STATUSNET;
174                         }
175                 }
176
177                 // Mastodon, Pleroma
178                 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
179                         || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
180                 ) {
181                         return self::ACTIVITYPUB;
182                 }
183
184                 // pumpio (http://host.name/user)
185                 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
186                         return self::PUMPIO;
187                 }
188
189                 return self::PHANTOM;
190         }
191
192         /**
193          * Returns a formatted mention from a profile URL and a display name
194          *
195          * @param string $profile_url
196          * @param string $display_name
197          * @return string
198          * @throws \Exception
199          */
200         public static function formatMention($profile_url, $display_name)
201         {
202                 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
203         }
204
205         /**
206          * Sends an unfriend message. Does not remove the contact
207          *
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
214          */
215         public static function terminateFriendship(array $user, array $contact, bool $two_way = false): bool
216         {
217                 if (empty($contact['network'])) {
218                         throw new \InvalidArgumentException('Missing network key in contact array');
219                 }
220
221                 $protocol = $contact['network'];
222                 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
223                         $protocol = $contact['protocol'];
224                 }
225
226                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
227                         // create an unfollow slap
228                         $item = [];
229                         $item['verb'] = Activity::O_UNFOLLOW;
230                         $item['gravity'] = GRAVITY_ACTIVITY;
231                         $item['follow'] = $contact['url'];
232                         $item['body'] = '';
233                         $item['title'] = '';
234                         $item['guid'] = '';
235                         $item['uri-id'] = 0;
236                         $slap = OStatus::salmon($item, $user);
237
238                         if (empty($contact['notify'])) {
239                                 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
240                         }
241
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) {
246                         if ($two_way) {
247                                 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
248                         }
249
250                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
251                 }
252
253                 // Catch-all hook for connector addons
254                 $hook_data = [
255                         'contact' => $contact,
256                         'two_way' => $two_way,
257                         'result' => null
258                 ];
259                 Hook::callAll('unfollow', $hook_data);
260
261                 return $hook_data['result'];
262         }
263
264         /**
265          * Revoke an incoming follow from the provided contact
266          *
267          * @param array $contact Private contact (uid != 0) array
268          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
269          * @throws \ImagickException
270          */
271         public static function revokeFollow(array $contact)
272         {
273                 if (empty($contact['network'])) {
274                         throw new \InvalidArgumentException('Missing network key in contact array');
275                 }
276
277                 $protocol = $contact['network'];
278                 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
279                         $protocol = $contact['protocol'];
280                 }
281
282                 if ($protocol == Protocol::ACTIVITYPUB) {
283                         return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
284                 }
285
286                 // Catch-all hook for connector addons
287                 $hook_data = [
288                         'contact' => $contact,
289                         'result' => null,
290                 ];
291                 Hook::callAll('revoke_follow', $hook_data);
292
293                 return $hook_data['result'];
294         }
295 }