3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core;
24 use Friendica\Util\Network;
27 * Manage compatibility with federated networks
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
39 const NATIVE_SUPPORT = [self::DFRN, self::DIASPORA, self::OSTATUS, self::FEED, self::MAIL, self::ACTIVITYPUB];
41 const FEDERATED = [self::DFRN, self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB];
43 const SUPPORT_PRIVATE = [self::DFRN, self::DIASPORA, self::MAIL, self::ACTIVITYPUB, self::PUMPIO];
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
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
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!
66 const PHANTOM = 'unkn'; // Place holder
69 * Returns the address string for the provided profile URL
71 * @param string $profile_url
75 public static function getAddrFromProfileUrl($profile_url)
77 $network = self::matchByProfileUrl($profile_url, $matches);
79 if ($network === self::PHANTOM) {
83 $addr = $matches[2] . '@' . $matches[1];
89 * Guesses the network from a profile URL
91 * @param string $profile_url
92 * @param array $matches preg_match return array: [0] => Full match [1] => hostname [2] => username
94 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
96 public static function matchByProfileUrl($profile_url, &$matches = [])
98 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
102 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
106 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
110 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
114 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
115 return self::DIASPORA;
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;
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);
129 $matches[2] = $user->screen_name;
130 return self::STATUSNET;
135 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
136 || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
138 return self::ACTIVITYPUB;
141 // pumpio (http://host.name/user)
142 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
146 return self::PHANTOM;
150 * Returns a formatted mention from a profile URL and a display name
152 * @param string $profile_url
153 * @param string $display_name
157 public static function formatMention($profile_url, $display_name)
159 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';