]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Merge remote-tracking branch 'upstream/develop' into write-tags
[friendica.git] / src / Core / Protocol.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Util\Network;
25
26 /**
27  * Manage compatibility with federated networks
28  */
29 class Protocol
30 {
31         // Native support
32         const ACTIVITYPUB = 'apub';    // ActivityPub (Pleroma, Mastodon, Osada, ...)
33         const DFRN        = 'dfrn';    // Friendica, Mistpark, other DFRN implementations
34         const DIASPORA    = 'dspr';    // Diaspora, Hubzilla, Socialhome, Ganggo
35         const FEED        = 'feed';    // RSS/Atom feeds with no known "post/notify" protocol
36         const MAIL        = 'mail';    // IMAP/POP
37         const OSTATUS     = 'stat';    // GNU Social and other OStatus implementations
38
39         const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
40
41         const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
42
43         const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
44
45         // Supported through a connector
46         const DIASPORA2 = 'dspc';    // Diaspora connector
47         const LINKEDIN  = 'lnkd';    // LinkedIn
48         const PUMPIO    = 'pump';    // pump.io
49         const STATUSNET = 'stac';    // Statusnet connector
50         const TWITTER   = 'twit';    // Twitter
51         const DISCOURSE = 'dscs';    // Discourse
52
53         // Dead protocols
54         const APPNET    = 'apdn';    // app.net - Dead protocol
55         const FACEBOOK  = 'face';    // Facebook API - Not working anymore, API is closed
56         const GPLUS     = 'goog';    // Google+ - Dead in 2019
57
58         // Currently unsupported
59         const ICALENDAR = 'ical';    // iCalendar
60         const MYSPACE   = 'mysp';    // MySpace
61         const NEWS      = 'nntp';    // Network News Transfer Protocol
62         const PNUT      = 'pnut';    // pnut.io
63         const XMPP      = 'xmpp';    // XMPP
64         const ZOT       = 'zot!';    // Zot!
65
66         const PHANTOM   = 'unkn';    // Place holder
67
68         /**
69          * Returns the address string for the provided profile URL
70          *
71          * @param string $profile_url
72          * @return string
73          * @throws \Exception
74          */
75         public static function getAddrFromProfileUrl($profile_url)
76         {
77                 $network = self::matchByProfileUrl($profile_url, $matches);
78
79                 if ($network === self::PHANTOM) {
80                         return "";
81                 }
82
83                 $addr = $matches[2] . '@' . $matches[1];
84
85                 return $addr;
86         }
87
88         /**
89          * Guesses the network from a profile URL
90          *
91          * @param string $profile_url
92          * @param array  $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
93          * @return string
94          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
95          */
96         public static function matchByProfileUrl($profile_url, &$matches = [])
97         {
98                 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
99                         return self::TWITTER;
100                 }
101
102                 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
103                         return self::APPNET;
104                 }
105
106                 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
107                         return self::GPLUS;
108                 }
109
110                 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
111                         return self::DFRN;
112                 }
113
114                 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
115                         return self::DIASPORA;
116                 }
117
118                 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
119                         // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
120                         return self::DIASPORA;
121                 }
122
123                 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
124                         $statusnet_host = $matches[1];
125                         $statusnet_user = $matches[2];
126                         $UserData = Network::fetchUrl('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
127                         $user = json_decode($UserData);
128                         if ($user) {
129                                 $matches[2] = $user->screen_name;
130                                 return self::STATUSNET;
131                         }
132                 }
133
134                 // Mastodon, Pleroma
135                 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
136                         || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
137                 ) {
138                         return self::ACTIVITYPUB;
139                 }
140
141                 // pumpio (http://host.name/user)
142                 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
143                         return self::PUMPIO;
144                 }
145
146                 return self::PHANTOM;
147         }
148
149         /**
150          * Returns a formatted mention from a profile URL and a display name
151          *
152          * @param string $profile_url
153          * @param string $display_name
154          * @return string
155          * @throws \Exception
156          */
157         public static function formatMention($profile_url, $display_name)
158         {
159                 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
160         }
161 }