]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Remove confirm template obsolete uses (except for contacts)
[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\DI;
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          */
95         public static function matchByProfileUrl($profile_url, &$matches = [])
96         {
97                 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
98                         return self::TWITTER;
99                 }
100
101                 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
102                         return self::APPNET;
103                 }
104
105                 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
106                         return self::GPLUS;
107                 }
108
109                 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
110                         return self::DFRN;
111                 }
112
113                 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
114                         return self::DIASPORA;
115                 }
116
117                 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
118                         // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
119                         return self::DIASPORA;
120                 }
121
122                 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
123                         $statusnet_host = $matches[1];
124                         $statusnet_user = $matches[2];
125                         $UserData = DI::httpRequest()->fetch('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
126                         $user = json_decode($UserData);
127                         if ($user) {
128                                 $matches[2] = $user->screen_name;
129                                 return self::STATUSNET;
130                         }
131                 }
132
133                 // Mastodon, Pleroma
134                 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
135                         || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
136                 ) {
137                         return self::ACTIVITYPUB;
138                 }
139
140                 // pumpio (http://host.name/user)
141                 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
142                         return self::PUMPIO;
143                 }
144
145                 return self::PHANTOM;
146         }
147
148         /**
149          * Returns a formatted mention from a profile URL and a display name
150          *
151          * @param string $profile_url
152          * @param string $display_name
153          * @return string
154          * @throws \Exception
155          */
156         public static function formatMention($profile_url, $display_name)
157         {
158                 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
159         }
160 }