]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Merge pull request #11099 from nupplaphil/feat/session_cache
[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\Database\DBA;
25 use Friendica\DI;
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' => 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 unfriend message. Does not remove the contact
175          *
176          * @param array   $user    User unfriending
177          * @param array   $contact Contact unfriended
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 terminateFriendship(array $user, array $contact): ?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                         $item['verb'] = Activity::O_UNFOLLOW;
197                         $item['gravity'] = GRAVITY_ACTIVITY;
198                         $item['follow'] = $contact['url'];
199                         $item['body'] = '';
200                         $item['title'] = '';
201                         $item['guid'] = '';
202                         $item['uri-id'] = 0;
203                         $slap = OStatus::salmon($item, $user);
204
205                         if (empty($contact['notify'])) {
206                                 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
207                         }
208
209                         return Salmon::slapper($user, $contact['notify'], $slap) === 0;
210                 } elseif ($protocol == Protocol::DIASPORA) {
211                         return Diaspora::sendUnshare($user, $contact) > 0;
212                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
213                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
214                 }
215
216                 // Catch-all hook for connector addons
217                 $hook_data = [
218                         'contact' => $contact,
219                         'result' => null
220                 ];
221                 Hook::callAll('unfollow', $hook_data);
222
223                 return $hook_data['result'];
224         }
225
226         /**
227          * Revoke an incoming follow from the provided contact
228          *
229          * @param array $contact Private contact (uid != 0) array
230          * @return bool|null true if successful, false if not, null if no action was performed
231          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
232          * @throws \ImagickException
233          */
234         public static function revokeFollow(array $contact): ?bool
235         {
236                 if (empty($contact['network'])) {
237                         throw new \InvalidArgumentException('Missing network key in contact array');
238                 }
239
240                 $protocol = $contact['network'];
241                 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
242                         $protocol = $contact['protocol'];
243                 }
244
245                 if ($protocol == Protocol::ACTIVITYPUB) {
246                         return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
247                 }
248
249                 // Catch-all hook for connector addons
250                 $hook_data = [
251                         'contact' => $contact,
252                         'result' => null,
253                 ];
254                 Hook::callAll('revoke_follow', $hook_data);
255
256                 return $hook_data['result'];
257         }
258
259         /**
260          * Send a block message to a remote server. Only useful for connector addons.
261          *
262          * @param array $contact Public contact record to block
263          * @param int   $uid     User issuing the block
264          * @return bool|null true if successful, false if not, null if no action was performed
265          * @throws HTTPException\InternalServerErrorException
266          */
267         public static function block(array $contact, int $uid): ?bool
268         {
269                 // Catch-all hook for connector addons
270                 $hook_data = [
271                         'contact' => $contact,
272                         'uid' => $uid,
273                         'result' => null,
274                 ];
275                 Hook::callAll('block', $hook_data);
276
277                 return $hook_data['result'];
278         }
279
280         /**
281          * Send an unblock message to a remote server. Only useful for connector addons.
282          *
283          * @param array $contact Public contact record to unblock
284          * @param int   $uid     User revoking the block
285          * @return bool|null true if successful, false if not, null if no action was performed
286          * @throws HTTPException\InternalServerErrorException
287          */
288         public static function unblock(array $contact, int $uid): ?bool
289         {
290                 // Catch-all hook for connector addons
291                 $hook_data = [
292                         'contact' => $contact,
293                         'uid' => $uid,
294                         'result' => null,
295                 ];
296                 Hook::callAll('unblock', $hook_data);
297
298                 return $hook_data['result'];
299         }
300 }