]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
bump version 2023.12
[friendica.git] / src / Core / Protocol.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Database\DBA;
25 use Friendica\Model\Item;
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;
33
34 /**
35  * Manage compatibility with federated networks
36  */
37 class Protocol
38 {
39         // Native support
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
46
47         const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
48
49         const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
50
51         const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
52
53         // Supported through a connector
54         const DIASPORA2 = 'dspc';    // Diaspora connector
55         const PUMPIO    = 'pump';    // pump.io
56         const STATUSNET = 'stac';    // Statusnet connector
57         const TWITTER   = 'twit';    // Twitter
58         const DISCOURSE = 'dscs';    // Discourse
59         const TUMBLR    = 'tmbl';    // Tumblr
60         const BLUESKY   = 'bsky';    // Bluesky
61
62         // Dead protocols
63         const APPNET    = 'apdn';    // app.net - Dead protocol
64         const FACEBOOK  = 'face';    // Facebook API - Not working anymore, API is closed
65         const GPLUS     = 'goog';    // Google+ - Dead in 2019
66
67         // Currently unsupported
68         const ICALENDAR = 'ical';    // iCalendar
69         const MYSPACE   = 'mysp';    // MySpace
70         const LINKEDIN  = 'lnkd';    // LinkedIn
71         const NEWS      = 'nntp';    // Network News Transfer Protocol
72         const PNUT      = 'pnut';    // pnut.io
73         const XMPP      = 'xmpp';    // XMPP
74         const ZOT       = 'zot!';    // Zot!
75
76         const PHANTOM   = 'unkn';    // Place holder
77
78         /**
79          * Returns whether the provided protocol supports following
80          *
81          * @param $protocol
82          * @return bool
83          * @throws HTTPException\InternalServerErrorException
84          */
85         public static function supportsFollow($protocol): bool
86         {
87                 if (in_array($protocol, self::NATIVE_SUPPORT)) {
88                         return true;
89                 }
90
91                 $hook_data = [
92                         'protocol' => $protocol,
93                         'result' => null
94                 ];
95                 Hook::callAll('support_follow', $hook_data);
96
97                 return $hook_data['result'] === true;
98         }
99
100         /**
101          * Returns whether the provided protocol supports revoking inbound follows
102          *
103          * @param $protocol
104          * @return bool
105          * @throws HTTPException\InternalServerErrorException
106          */
107         public static function supportsRevokeFollow($protocol): bool
108         {
109                 if (in_array($protocol, self::NATIVE_SUPPORT)) {
110                         return true;
111                 }
112
113                 $hook_data = [
114                         'protocol' => $protocol,
115                         'result' => null
116                 ];
117                 Hook::callAll('support_revoke_follow', $hook_data);
118
119                 return $hook_data['result'] === true;
120         }
121
122         /**
123          * Send a follow message to a remote server.
124          *
125          * @param int     $uid      User Id
126          * @param array   $contact  Contact being followed
127          * @param ?string $protocol Expected protocol
128          * @return bool Only returns false in the unlikely case an ActivityPub contact ID doesn't exist (???)
129          * @throws HTTPException\InternalServerErrorException
130          * @throws \ImagickException
131          */
132         public static function follow(int $uid, array $contact, ?string $protocol = null): bool
133         {
134                 $owner = User::getOwnerDataById($uid);
135                 if (!DBA::isResult($owner)) {
136                         return true;
137                 }
138
139                 $protocol = $protocol ?? $contact['protocol'];
140
141                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
142                         // create a follow slap
143                         $item = [
144                                 'verb'    => Activity::FOLLOW,
145                                 'gravity' => Item::GRAVITY_ACTIVITY,
146                                 'follow'  => $contact['url'],
147                                 'body'    => '',
148                                 'title'   => '',
149                                 'guid'    => '',
150                                 'uri-id'  => 0,
151                         ];
152
153                         $slap = OStatus::salmon($item, $owner);
154
155                         if (!empty($contact['notify'])) {
156                                 Salmon::slapper($owner, $contact['notify'], $slap);
157                         }
158                 } elseif ($protocol == Protocol::DIASPORA) {
159                         $contact = Diaspora::sendShare($owner, $contact);
160                         Logger::notice('share returns: ' . $contact);
161                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
162                         $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
163                         if (empty($activity_id)) {
164                                 // This really should never happen
165                                 return false;
166                         }
167
168                         $success = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $owner['uid'], $activity_id);
169                         Logger::notice('Follow returns: ' . $success);
170                 }
171
172                 return true;
173         }
174
175         /**
176          * Sends an unfollow message. Does not remove the contact
177          *
178          * @param array $contact Target public contact (uid = 0) array
179          * @param array $owner   Source owner-view record
180          * @return bool|null true if successful, false if not, null if no remote action was performed
181          * @throws HTTPException\InternalServerErrorException
182          * @throws \ImagickException
183          */
184         public static function unfollow(array $contact, array $owner): ?bool
185         {
186                 if (empty($contact['network'])) {
187                         Logger::notice('Contact has got no network, we quit here', ['id' => $contact['id']]);
188                         return null;
189                 }
190
191                 $protocol = $contact['network'];
192                 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
193                         $protocol = $contact['protocol'];
194                 }
195
196                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
197                         // create an unfollow slap
198                         $item = [
199                                 'verb'    => Activity::O_UNFOLLOW,
200                                 'gravity' => Item::GRAVITY_ACTIVITY,
201                                 'follow'  => $contact['url'],
202                                 'body'    => '',
203                                 'title'   => '',
204                                 'guid'    => '',
205                                 'uri-id'  => 0,
206                         ];
207
208                         $slap = OStatus::salmon($item, $owner);
209
210                         if (empty($contact['notify'])) {
211                                 Logger::notice('OStatus/DFRN Contact is missing notify, we quit here', ['id' => $contact['id']]);
212                                 return null;
213                         }
214
215                         return Salmon::slapper($owner, $contact['notify'], $slap) === 0;
216                 } elseif ($protocol == Protocol::DIASPORA) {
217                         return Diaspora::sendUnshare($owner, $contact) > 0;
218                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
219                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $owner);
220                 }
221
222                 // Catch-all hook for connector addons
223                 $hook_data = [
224                         'contact' => $contact,
225                         'uid'     => $owner['uid'],
226                         'result'  => null,
227                 ];
228                 Hook::callAll('unfollow', $hook_data);
229
230                 return $hook_data['result'];
231         }
232
233         /**
234          * Revoke an incoming follow from the provided contact
235          *
236          * @param array $contact Target public contact (uid == 0) array
237          * @param array $owner   Source owner-view record
238          * @return bool|null true if successful, false if not, null if no action was performed
239          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
240          * @throws \ImagickException
241          */
242         public static function revokeFollow(array $contact, array $owner): ?bool
243         {
244                 if (empty($contact['network'])) {
245                         throw new \InvalidArgumentException('Missing network key in contact array');
246                 }
247
248                 $protocol = $contact['network'];
249                 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
250                         $protocol = $contact['protocol'];
251                 }
252
253                 if ($protocol == Protocol::ACTIVITYPUB) {
254                         return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $owner);
255                 }
256
257                 // Catch-all hook for connector addons
258                 $hook_data = [
259                         'contact' => $contact,
260                         'uid'     => $owner['uid'],
261                         'result'  => null,
262                 ];
263                 Hook::callAll('revoke_follow', $hook_data);
264
265                 return $hook_data['result'];
266         }
267
268         /**
269          * Send a block message to a remote server. Only useful for connector addons.
270          *
271          * @param array $contact Public contact record to block
272          * @param int   $uid     User issuing the block
273          * @return bool|null true if successful, false if not, null if no action was performed
274          * @throws HTTPException\InternalServerErrorException
275          */
276         public static function block(array $contact, int $uid): ?bool
277         {
278                 // Catch-all hook for connector addons
279                 $hook_data = [
280                         'contact' => $contact,
281                         'uid' => $uid,
282                         'result' => null,
283                 ];
284                 Hook::callAll('block', $hook_data);
285
286                 return $hook_data['result'];
287         }
288
289         /**
290          * Send an unblock message to a remote server. Only useful for connector addons.
291          *
292          * @param array $contact Public contact record to unblock
293          * @param int   $uid     User revoking the block
294          * @return bool|null true if successful, false if not, null if no action was performed
295          * @throws HTTPException\InternalServerErrorException
296          */
297         public static function unblock(array $contact, int $uid): ?bool
298         {
299                 // Catch-all hook for connector addons
300                 $hook_data = [
301                         'contact' => $contact,
302                         'uid' => $uid,
303                         'result' => null,
304                 ];
305                 Hook::callAll('unblock', $hook_data);
306
307                 return $hook_data['result'];
308         }
309
310         /**
311          * Returns whether the provided protocol supports probing for contacts
312          *
313          * @param $protocol
314          * @return bool
315          * @throws HTTPException\InternalServerErrorException
316          */
317         public static function supportsProbe($protocol): bool
318         {
319                 // "Mail" can only be probed for a specific user in a specific condition, so we are ignoring it here.
320                 if ($protocol == self::MAIL) {
321                         return false;
322                 }
323
324                 if (in_array($protocol, array_merge(self::NATIVE_SUPPORT, [self::ZOT, self::PHANTOM]))) {
325                         return true;
326                 }
327
328                 $hook_data = [
329                         'protocol' => $protocol,
330                         'result' => null
331                 ];
332                 Hook::callAll('support_probe', $hook_data);
333
334                 return $hook_data['result'] === true;
335         }
336 }