]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Merge pull request #10854 from MrPetovan/bug/10844-unfollow
[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                 $hook_data = [
88                         'protocol' => $protocol,
89                         'result' => null
90                 ];
91                 Hook::callAll('support_follow', $hook_data);
92
93                 return $hook_data['result'] === true;
94         }
95
96         /**
97          * Returns whether the provided protocol supports revoking inbound follows
98          *
99          * @param $protocol
100          * @return bool
101          * @throws HTTPException\InternalServerErrorException
102          */
103         public static function supportsRevokeFollow($protocol): bool
104         {
105                 if (in_array($protocol, self::NATIVE_SUPPORT)) {
106                         return true;
107                 }
108
109                 $hook_data = [
110                         'protocol' => $protocol,
111                         'result' => null
112                 ];
113                 Hook::callAll('support_revoke_follow', $hook_data);
114
115                 return $hook_data['result'] === true;
116         }
117
118         /**
119          * Returns the address string for the provided profile URL
120          *
121          * @param string $profile_url
122          * @return string
123          * @throws \Exception
124          */
125         public static function getAddrFromProfileUrl($profile_url)
126         {
127                 $network = self::matchByProfileUrl($profile_url, $matches);
128
129                 if ($network === self::PHANTOM) {
130                         return "";
131                 }
132
133                 $addr = $matches[2] . '@' . $matches[1];
134
135                 return $addr;
136         }
137
138         /**
139          * Guesses the network from a profile URL
140          *
141          * @param string $profile_url
142          * @param array  $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
143          * @return string
144          */
145         public static function matchByProfileUrl($profile_url, &$matches = [])
146         {
147                 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
148                         return self::TWITTER;
149                 }
150
151                 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
152                         return self::APPNET;
153                 }
154
155                 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
156                         return self::GPLUS;
157                 }
158
159                 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
160                         return self::DFRN;
161                 }
162
163                 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
164                         return self::DIASPORA;
165                 }
166
167                 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
168                         // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
169                         return self::DIASPORA;
170                 }
171
172                 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
173                         $statusnet_host = $matches[1];
174                         $statusnet_user = $matches[2];
175                         $UserData = DI::httpClient()->fetch('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
176                         $user = json_decode($UserData);
177                         if ($user) {
178                                 $matches[2] = $user->screen_name;
179                                 return self::STATUSNET;
180                         }
181                 }
182
183                 // Mastodon, Pleroma
184                 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
185                         || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
186                 ) {
187                         return self::ACTIVITYPUB;
188                 }
189
190                 // pumpio (http://host.name/user)
191                 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
192                         return self::PUMPIO;
193                 }
194
195                 return self::PHANTOM;
196         }
197
198         /**
199          * Returns a formatted mention from a profile URL and a display name
200          *
201          * @param string $profile_url
202          * @param string $display_name
203          * @return string
204          * @throws \Exception
205          */
206         public static function formatMention($profile_url, $display_name)
207         {
208                 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
209         }
210
211         /**
212          * Sends an unfriend message. Does not remove the contact
213          *
214          * @param array   $user    User unfriending
215          * @param array   $contact Contact unfriended
216          * @return bool|null true if successful, false if not, null if no remote action was performed
217          * @throws HTTPException\InternalServerErrorException
218          * @throws \ImagickException
219          */
220         public static function terminateFriendship(array $user, array $contact): ?bool
221         {
222                 if (empty($contact['network'])) {
223                         throw new \InvalidArgumentException('Missing network key in contact array');
224                 }
225
226                 $protocol = $contact['network'];
227                 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
228                         $protocol = $contact['protocol'];
229                 }
230
231                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
232                         // create an unfollow slap
233                         $item = [];
234                         $item['verb'] = Activity::O_UNFOLLOW;
235                         $item['gravity'] = GRAVITY_ACTIVITY;
236                         $item['follow'] = $contact['url'];
237                         $item['body'] = '';
238                         $item['title'] = '';
239                         $item['guid'] = '';
240                         $item['uri-id'] = 0;
241                         $slap = OStatus::salmon($item, $user);
242
243                         if (empty($contact['notify'])) {
244                                 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
245                         }
246
247                         return Salmon::slapper($user, $contact['notify'], $slap) === 0;
248                 } elseif ($protocol == Protocol::DIASPORA) {
249                         return Diaspora::sendUnshare($user, $contact) > 0;
250                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
251                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
252                 }
253
254                 // Catch-all hook for connector addons
255                 $hook_data = [
256                         'contact' => $contact,
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          * @return bool|null true if successful, false if not, null if no action was performed
269          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
270          * @throws \ImagickException
271          */
272         public static function revokeFollow(array $contact): ?bool
273         {
274                 if (empty($contact['network'])) {
275                         throw new \InvalidArgumentException('Missing network key in contact array');
276                 }
277
278                 $protocol = $contact['network'];
279                 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
280                         $protocol = $contact['protocol'];
281                 }
282
283                 if ($protocol == Protocol::ACTIVITYPUB) {
284                         return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
285                 }
286
287                 // Catch-all hook for connector addons
288                 $hook_data = [
289                         'contact' => $contact,
290                         'result' => null,
291                 ];
292                 Hook::callAll('revoke_follow', $hook_data);
293
294                 return $hook_data['result'];
295         }
296
297         /**
298          * Send a block message to a remote server. Only useful for connector addons.
299          *
300          * @param array $contact Public contact record to block
301          * @param int   $uid     User issuing the block
302          * @return bool|null true if successful, false if not, null if no action was performed
303          * @throws HTTPException\InternalServerErrorException
304          */
305         public static function block(array $contact, int $uid): ?bool
306         {
307                 // Catch-all hook for connector addons
308                 $hook_data = [
309                         'contact' => $contact,
310                         'uid' => $uid,
311                         'result' => null,
312                 ];
313                 Hook::callAll('block', $hook_data);
314
315                 return $hook_data['result'];
316         }
317
318         /**
319          * Send an unblock message to a remote server. Only useful for connector addons.
320          *
321          * @param array $contact Public contact record to unblock
322          * @param int   $uid     User revoking the block
323          * @return bool|null true if successful, false if not, null if no action was performed
324          * @throws HTTPException\InternalServerErrorException
325          */
326         public static function unblock(array $contact, int $uid): ?bool
327         {
328                 // Catch-all hook for connector addons
329                 $hook_data = [
330                         'contact' => $contact,
331                         'uid' => $uid,
332                         'result' => null,
333                 ];
334                 Hook::callAll('unblock', $hook_data);
335
336                 return $hook_data['result'];
337         }
338 }