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