]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Merge pull request #11520 from annando/display-polls
[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\User;
26 use Friendica\Network\HTTPException;
27 use Friendica\Protocol\Activity;
28 use Friendica\Protocol\ActivityPub;
29 use Friendica\Protocol\Diaspora;
30 use Friendica\Protocol\OStatus;
31 use Friendica\Protocol\Salmon;
32
33 /**
34  * Manage compatibility with federated networks
35  */
36 class Protocol
37 {
38         // Native support
39         const ACTIVITYPUB = 'apub';    // ActivityPub (Pleroma, Mastodon, Osada, ...)
40         const DFRN        = 'dfrn';    // Friendica, Mistpark, other DFRN implementations
41         const DIASPORA    = 'dspr';    // Diaspora, Hubzilla, Socialhome, Ganggo
42         const FEED        = 'feed';    // RSS/Atom feeds with no known "post/notify" protocol
43         const MAIL        = 'mail';    // IMAP/POP
44         const OSTATUS     = 'stat';    // GNU Social and other OStatus implementations
45
46         const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
47
48         const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
49
50         const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
51
52         // Supported through a connector
53         const DIASPORA2 = 'dspc';    // Diaspora connector
54         const LINKEDIN  = 'lnkd';    // LinkedIn
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 NEWS      = 'nntp';    // Network News Transfer Protocol
69         const PNUT      = 'pnut';    // pnut.io
70         const XMPP      = 'xmpp';    // XMPP
71         const ZOT       = 'zot!';    // Zot!
72
73         const PHANTOM   = 'unkn';    // Place holder
74
75         /**
76          * Returns whether the provided protocol supports following
77          *
78          * @param $protocol
79          * @return bool
80          * @throws HTTPException\InternalServerErrorException
81          */
82         public static function supportsFollow($protocol): bool
83         {
84                 if (in_array($protocol, self::NATIVE_SUPPORT)) {
85                         return true;
86                 }
87
88                 $hook_data = [
89                         'protocol' => $protocol,
90                         'result' => null
91                 ];
92                 Hook::callAll('support_follow', $hook_data);
93
94                 return $hook_data['result'] === true;
95         }
96
97         /**
98          * Returns whether the provided protocol supports revoking inbound follows
99          *
100          * @param $protocol
101          * @return bool
102          * @throws HTTPException\InternalServerErrorException
103          */
104         public static function supportsRevokeFollow($protocol): bool
105         {
106                 if (in_array($protocol, self::NATIVE_SUPPORT)) {
107                         return true;
108                 }
109
110                 $hook_data = [
111                         'protocol' => $protocol,
112                         'result' => null
113                 ];
114                 Hook::callAll('support_revoke_follow', $hook_data);
115
116                 return $hook_data['result'] === true;
117         }
118
119         /**
120          * Send a follow message to a remote server.
121          *
122          * @param int     $uid      User Id
123          * @param array   $contact  Contact being followed
124          * @param ?string $protocol Expected protocol
125          * @return bool Only returns false in the unlikely case an ActivityPub contact ID doesn't exist (???)
126          * @throws HTTPException\InternalServerErrorException
127          * @throws \ImagickException
128          */
129         public static function follow(int $uid, array $contact, ?string $protocol = null): bool
130         {
131                 $owner = User::getOwnerDataById($uid);
132                 if (!DBA::isResult($owner)) {
133                         return true;
134                 }
135
136                 $protocol = $protocol ?? $contact['protocol'];
137
138                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
139                         // create a follow slap
140                         $item = [
141                                 'verb'    => Activity::FOLLOW,
142                                 'gravity' => GRAVITY_ACTIVITY,
143                                 'follow'  => $contact['url'],
144                                 'body'    => '',
145                                 'title'   => '',
146                                 'guid'    => '',
147                                 'uri-id'  => 0,
148                         ];
149
150                         $slap = OStatus::salmon($item, $owner);
151
152                         if (!empty($contact['notify'])) {
153                                 Salmon::slapper($owner, $contact['notify'], $slap);
154                         }
155                 } elseif ($protocol == Protocol::DIASPORA) {
156                         $contact = Diaspora::sendShare($owner, $contact);
157                         Logger::notice('share returns: ' . $contact);
158                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
159                         $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']);
160                         if (empty($activity_id)) {
161                                 // This really should never happen
162                                 return false;
163                         }
164
165                         $success = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $owner['uid'], $activity_id);
166                         Logger::notice('Follow returns: ' . $success);
167                 }
168
169                 return true;
170         }
171
172         /**
173          * Sends an unfollow message. Does not remove the contact
174          *
175          * @param array $contact Target public contact (uid = 0) array
176          * @param array $user    Source local user array
177          * @return bool|null true if successful, false if not, null if no remote action was performed
178          * @throws HTTPException\InternalServerErrorException
179          * @throws \ImagickException
180          */
181         public static function unfollow(array $contact, array $user): ?bool
182         {
183                 if (empty($contact['network'])) {
184                         throw new \InvalidArgumentException('Missing network key in contact array');
185                 }
186
187                 $protocol = $contact['network'];
188                 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
189                         $protocol = $contact['protocol'];
190                 }
191
192                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
193                         // create an unfollow slap
194                         $item = [];
195                         $item['verb'] = Activity::O_UNFOLLOW;
196                         $item['gravity'] = GRAVITY_ACTIVITY;
197                         $item['follow'] = $contact['url'];
198                         $item['body'] = '';
199                         $item['title'] = '';
200                         $item['guid'] = '';
201                         $item['uri-id'] = 0;
202                         $slap = OStatus::salmon($item, $user);
203
204                         if (empty($contact['notify'])) {
205                                 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
206                         }
207
208                         return Salmon::slapper($user, $contact['notify'], $slap) === 0;
209                 } elseif ($protocol == Protocol::DIASPORA) {
210                         return Diaspora::sendUnshare($user, $contact) > 0;
211                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
212                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
213                 }
214
215                 // Catch-all hook for connector addons
216                 $hook_data = [
217                         'contact' => $contact,
218                         'uid'     => $user['uid'],
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 Target public contact (uid == 0) array
230          * @param int   $uid     Source local user id
231          * @return bool|null true if successful, false if not, null if no action was performed
232          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
233          * @throws \ImagickException
234          */
235         public static function revokeFollow(array $contact, int $uid): ?bool
236         {
237                 if (empty($contact['network'])) {
238                         throw new \InvalidArgumentException('Missing network key in contact array');
239                 }
240
241                 $protocol = $contact['network'];
242                 if ($protocol == Protocol::DFRN && !empty($contact['protocol'])) {
243                         $protocol = $contact['protocol'];
244                 }
245
246                 if ($protocol == Protocol::ACTIVITYPUB) {
247                         return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $uid);
248                 }
249
250                 // Catch-all hook for connector addons
251                 $hook_data = [
252                         'contact' => $contact,
253                         'uid'     => $uid,
254                         'result'  => null,
255                 ];
256                 Hook::callAll('revoke_follow', $hook_data);
257
258                 return $hook_data['result'];
259         }
260
261         /**
262          * Send a block message to a remote server. Only useful for connector addons.
263          *
264          * @param array $contact Public contact record to block
265          * @param int   $uid     User issuing the block
266          * @return bool|null true if successful, false if not, null if no action was performed
267          * @throws HTTPException\InternalServerErrorException
268          */
269         public static function block(array $contact, int $uid): ?bool
270         {
271                 // Catch-all hook for connector addons
272                 $hook_data = [
273                         'contact' => $contact,
274                         'uid' => $uid,
275                         'result' => null,
276                 ];
277                 Hook::callAll('block', $hook_data);
278
279                 return $hook_data['result'];
280         }
281
282         /**
283          * Send an unblock message to a remote server. Only useful for connector addons.
284          *
285          * @param array $contact Public contact record to unblock
286          * @param int   $uid     User revoking the block
287          * @return bool|null true if successful, false if not, null if no action was performed
288          * @throws HTTPException\InternalServerErrorException
289          */
290         public static function unblock(array $contact, int $uid): ?bool
291         {
292                 // Catch-all hook for connector addons
293                 $hook_data = [
294                         'contact' => $contact,
295                         'uid' => $uid,
296                         'result' => null,
297                 ];
298                 Hook::callAll('unblock', $hook_data);
299
300                 return $hook_data['result'];
301         }
302 }