]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
ActivityPub: Update relation when receiving a follow reject message
[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\DI;
25 use Friendica\Network\HTTPException;
26 use Friendica\Protocol\Activity;
27 use Friendica\Protocol\ActivityPub;
28 use Friendica\Protocol\Diaspora;
29 use Friendica\Protocol\OStatus;
30 use Friendica\Protocol\Salmon;
31
32 /**
33  * Manage compatibility with federated networks
34  */
35 class Protocol
36 {
37         // Native support
38         const ACTIVITYPUB = 'apub';    // ActivityPub (Pleroma, Mastodon, Osada, ...)
39         const DFRN        = 'dfrn';    // Friendica, Mistpark, other DFRN implementations
40         const DIASPORA    = 'dspr';    // Diaspora, Hubzilla, Socialhome, Ganggo
41         const FEED        = 'feed';    // RSS/Atom feeds with no known "post/notify" protocol
42         const MAIL        = 'mail';    // IMAP/POP
43         const OSTATUS     = 'stat';    // GNU Social and other OStatus implementations
44
45         const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
46
47         const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
48
49         const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
50
51         // Supported through a connector
52         const DIASPORA2 = 'dspc';    // Diaspora connector
53         const LINKEDIN  = 'lnkd';    // LinkedIn
54         const PUMPIO    = 'pump';    // pump.io
55         const STATUSNET = 'stac';    // Statusnet connector
56         const TWITTER   = 'twit';    // Twitter
57         const DISCOURSE = 'dscs';    // Discourse
58
59         // Dead protocols
60         const APPNET    = 'apdn';    // app.net - Dead protocol
61         const FACEBOOK  = 'face';    // Facebook API - Not working anymore, API is closed
62         const GPLUS     = 'goog';    // Google+ - Dead in 2019
63
64         // Currently unsupported
65         const ICALENDAR = 'ical';    // iCalendar
66         const MYSPACE   = 'mysp';    // MySpace
67         const NEWS      = 'nntp';    // Network News Transfer Protocol
68         const PNUT      = 'pnut';    // pnut.io
69         const XMPP      = 'xmpp';    // XMPP
70         const ZOT       = 'zot!';    // Zot!
71
72         const PHANTOM   = 'unkn';    // Place holder
73
74         /**
75          * Returns the address string for the provided profile URL
76          *
77          * @param string $profile_url
78          * @return string
79          * @throws \Exception
80          */
81         public static function getAddrFromProfileUrl($profile_url)
82         {
83                 $network = self::matchByProfileUrl($profile_url, $matches);
84
85                 if ($network === self::PHANTOM) {
86                         return "";
87                 }
88
89                 $addr = $matches[2] . '@' . $matches[1];
90
91                 return $addr;
92         }
93
94         /**
95          * Guesses the network from a profile URL
96          *
97          * @param string $profile_url
98          * @param array  $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
99          * @return string
100          */
101         public static function matchByProfileUrl($profile_url, &$matches = [])
102         {
103                 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
104                         return self::TWITTER;
105                 }
106
107                 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
108                         return self::APPNET;
109                 }
110
111                 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
112                         return self::GPLUS;
113                 }
114
115                 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
116                         return self::DFRN;
117                 }
118
119                 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
120                         return self::DIASPORA;
121                 }
122
123                 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
124                         // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
125                         return self::DIASPORA;
126                 }
127
128                 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
129                         $statusnet_host = $matches[1];
130                         $statusnet_user = $matches[2];
131                         $UserData = DI::httpClient()->fetch('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
132                         $user = json_decode($UserData);
133                         if ($user) {
134                                 $matches[2] = $user->screen_name;
135                                 return self::STATUSNET;
136                         }
137                 }
138
139                 // Mastodon, Pleroma
140                 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
141                         || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
142                 ) {
143                         return self::ACTIVITYPUB;
144                 }
145
146                 // pumpio (http://host.name/user)
147                 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
148                         return self::PUMPIO;
149                 }
150
151                 return self::PHANTOM;
152         }
153
154         /**
155          * Returns a formatted mention from a profile URL and a display name
156          *
157          * @param string $profile_url
158          * @param string $display_name
159          * @return string
160          * @throws \Exception
161          */
162         public static function formatMention($profile_url, $display_name)
163         {
164                 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
165         }
166
167         /**
168          * Sends an unfriend message. Does not remove the contact
169          *
170          * @param array   $user    User unfriending
171          * @param array   $contact Contact unfriended
172          * @param boolean $two_way Revoke eventual inbound follow as well
173          * @return bool|null true if successful, false if not, null if no action was performed
174          * @throws HTTPException\InternalServerErrorException
175          * @throws \ImagickException
176          */
177         public static function terminateFriendship(array $user, array $contact, bool $two_way = false): bool
178         {
179                 if (empty($contact['network'])) {
180                         throw new \InvalidArgumentException('Missing network key in contact array');
181                 }
182
183                 $protocol = $contact['network'];
184                 if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
185                         $protocol = $contact['protocol'];
186                 }
187
188                 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
189                         // create an unfollow slap
190                         $item = [];
191                         $item['verb'] = Activity::O_UNFOLLOW;
192                         $item['gravity'] = GRAVITY_ACTIVITY;
193                         $item['follow'] = $contact['url'];
194                         $item['body'] = '';
195                         $item['title'] = '';
196                         $item['guid'] = '';
197                         $item['uri-id'] = 0;
198                         $slap = OStatus::salmon($item, $user);
199
200                         if (empty($contact['notify'])) {
201                                 throw new \InvalidArgumentException('Missing expected "notify" key in OStatus/DFRN contact');
202                         }
203
204                         return Salmon::slapper($user, $contact['notify'], $slap) === 0;
205                 } elseif ($protocol == Protocol::DIASPORA) {
206                         return Diaspora::sendUnshare($user, $contact) > 0;
207                 } elseif ($protocol == Protocol::ACTIVITYPUB) {
208                         if ($two_way) {
209                                 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
210                         }
211
212                         return ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
213                 }
214
215                 // Catch-all addon hook
216                 $hook_data = [
217                         'contact' => $contact,
218                         'two_way' => $two_way,
219                         'result' => null
220                 ];
221                 Hook::callAll('unfollow', $hook_data);
222
223                 return $hook_data['result'];
224         }
225 }