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