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