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