]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Merge pull request #12078 from MrPetovan/task/4090-move-mod-wallmessage
[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                                 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
209                         }
210
211                         return Salmon::slapper($user, $contact['notify'], $slap) === 0;
212                 } elseif ($protocol == Protocol::DIASPORA) {
213                         return Diaspora::sendUnshare($user, $contact) > 0;
214                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
215                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
216                 }
217
218                 // Catch-all hook for connector addons
219                 $hook_data = [
220                         'contact' => $contact,
221                         'uid'     => $user['uid'],
222                         'result'  => null,
223                 ];
224                 Hook::callAll('unfollow', $hook_data);
225
226                 return $hook_data['result'];
227         }
228
229         /**
230          * Revoke an incoming follow from the provided contact
231          *
232          * @param array $contact Target public contact (uid == 0) array
233          * @param int   $uid     Source local user id
234          * @return bool|null true if successful, false if not, null if no action was performed
235          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
236          * @throws \ImagickException
237          */
238         public static function revokeFollow(array $contact, int $uid): ?bool
239         {
240                 if (empty($contact['network'])) {
241                         throw new \InvalidArgumentException('Missing network key in contact array');
242                 }
243
244                 $protocol = $contact['network'];
245                 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
246                         $protocol = $contact['protocol'];
247                 }
248
249                 if ($protocol == Protocol::ACTIVITYPUB) {
250                         return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $uid);
251                 }
252
253                 // Catch-all hook for connector addons
254                 $hook_data = [
255                         'contact' => $contact,
256                         'uid'     => $uid,
257                         'result'  => null,
258                 ];
259                 Hook::callAll('revoke_follow', $hook_data);
260
261                 return $hook_data['result'];
262         }
263
264         /**
265          * Send a block message to a remote server. Only useful for connector addons.
266          *
267          * @param array $contact Public contact record to block
268          * @param int   $uid     User issuing the block
269          * @return bool|null true if successful, false if not, null if no action was performed
270          * @throws HTTPException\InternalServerErrorException
271          */
272         public static function block(array $contact, int $uid): ?bool
273         {
274                 // Catch-all hook for connector addons
275                 $hook_data = [
276                         'contact' => $contact,
277                         'uid' => $uid,
278                         'result' => null,
279                 ];
280                 Hook::callAll('block', $hook_data);
281
282                 return $hook_data['result'];
283         }
284
285         /**
286          * Send an unblock message to a remote server. Only useful for connector addons.
287          *
288          * @param array $contact Public contact record to unblock
289          * @param int   $uid     User revoking the block
290          * @return bool|null true if successful, false if not, null if no action was performed
291          * @throws HTTPException\InternalServerErrorException
292          */
293         public static function unblock(array $contact, int $uid): ?bool
294         {
295                 // Catch-all hook for connector addons
296                 $hook_data = [
297                         'contact' => $contact,
298                         'uid' => $uid,
299                         'result' => null,
300                 ];
301                 Hook::callAll('unblock', $hook_data);
302
303                 return $hook_data['result'];
304         }
305 }