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