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