]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
096e12c27bd19979eee61950211035bfaa732490
[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 LINKEDIN  = 'lnkd';    // LinkedIn
56         const PUMPIO    = 'pump';    // pump.io
57         const STATUSNET = 'stac';    // Statusnet connector
58         const TWITTER   = 'twit';    // Twitter
59         const DISCOURSE = 'dscs';    // Discourse
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 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                         throw new \InvalidArgumentException('Missing network key in contact array');
186                 }
187
188                 $protocol = $contact['network'];
189                 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
190                         $protocol = $contact['protocol'];
191                 }
192
193                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
194                         // create an unfollow slap
195                         $item = [
196                                 'verb'    => Activity::O_UNFOLLOW,
197                                 'gravity' => Item::GRAVITY_ACTIVITY,
198                                 'follow'  => $contact['url'],
199                                 'body'    => '',
200                                 'title'   => '',
201                                 'guid'    => '',
202                                 'uri-id'  => 0,
203                         ];
204
205                         $slap = OStatus::salmon($item, $user);
206
207                         if (empty($contact['notify'])) {
208                                 return true;
209                                 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
210                         }
211
212                         return Salmon::slapper($user, $contact['notify'], $slap) === 0;
213                 } elseif ($protocol == Protocol::DIASPORA) {
214                         return Diaspora::sendUnshare($user, $contact) > 0;
215                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
216                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
217                 }
218
219                 // Catch-all hook for connector addons
220                 $hook_data = [
221                         'contact' => $contact,
222                         'uid'     => $user['uid'],
223                         'result'  => null,
224                 ];
225                 Hook::callAll('unfollow', $hook_data);
226
227                 return $hook_data['result'];
228         }
229
230         /**
231          * Revoke an incoming follow from the provided contact
232          *
233          * @param array $contact Target public contact (uid == 0) array
234          * @param int   $uid     Source local user id
235          * @return bool|null true if successful, false if not, null if no action was performed
236          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
237          * @throws \ImagickException
238          */
239         public static function revokeFollow(array $contact, int $uid): ?bool
240         {
241                 if (empty($contact['network'])) {
242                         throw new \InvalidArgumentException('Missing network key in contact array');
243                 }
244
245                 $protocol = $contact['network'];
246                 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
247                         $protocol = $contact['protocol'];
248                 }
249
250                 if ($protocol == Protocol::ACTIVITYPUB) {
251                         return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $uid);
252                 }
253
254                 // Catch-all hook for connector addons
255                 $hook_data = [
256                         'contact' => $contact,
257                         'uid'     => $uid,
258                         'result'  => null,
259                 ];
260                 Hook::callAll('revoke_follow', $hook_data);
261
262                 return $hook_data['result'];
263         }
264
265         /**
266          * Send a block message to a remote server. Only useful for connector addons.
267          *
268          * @param array $contact Public contact record to block
269          * @param int   $uid     User issuing the block
270          * @return bool|null true if successful, false if not, null if no action was performed
271          * @throws HTTPException\InternalServerErrorException
272          */
273         public static function block(array $contact, int $uid): ?bool
274         {
275                 // Catch-all hook for connector addons
276                 $hook_data = [
277                         'contact' => $contact,
278                         'uid' => $uid,
279                         'result' => null,
280                 ];
281                 Hook::callAll('block', $hook_data);
282
283                 return $hook_data['result'];
284         }
285
286         /**
287          * Send an unblock message to a remote server. Only useful for connector addons.
288          *
289          * @param array $contact Public contact record to unblock
290          * @param int   $uid     User revoking the block
291          * @return bool|null true if successful, false if not, null if no action was performed
292          * @throws HTTPException\InternalServerErrorException
293          */
294         public static function unblock(array $contact, int $uid): ?bool
295         {
296                 // Catch-all hook for connector addons
297                 $hook_data = [
298                         'contact' => $contact,
299                         'uid' => $uid,
300                         'result' => null,
301                 ];
302                 Hook::callAll('unblock', $hook_data);
303
304                 return $hook_data['result'];
305         }
306 }