]> git.mxchange.org Git - friendica.git/blob - src/Core/Protocol.php
Issue 6282: Update the contact data regularly (including the network)
[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 type
74          */
75         public static function matchByProfileUrl($profile_url, &$matches = [])
76         {
77                 if (preg_match('=https?://(twitter\.com)/(.*)=ism', $profile_url, $matches)) {
78                         return self::TWITTER;
79                 }
80
81                 if (preg_match('=https?://(alpha\.app\.net)/(.*)=ism', $profile_url, $matches)) {
82                         return self::APPNET;
83                 }
84
85                 if (preg_match('=https?://(plus\.google\.com)/(.*)=ism', $profile_url, $matches)) {
86                         return self::GPLUS;
87                 }
88
89                 if (preg_match('=https?://(.*)/profile/(.*)=ism', $profile_url, $matches)) {
90                         return self::DFRN;
91                 }
92
93                 if (preg_match('=https?://(.*)/u/(.*)=ism', $profile_url, $matches)) {
94                         return self::DIASPORA;
95                 }
96
97                 if (preg_match('=https?://(.*)/channel/(.*)=ism', $profile_url, $matches)) {
98                         // RedMatrix/Hubzilla is identified as Diaspora - friendica can't connect directly to it
99                         return self::DIASPORA;
100                 }
101
102                 if (preg_match('=https?://(.*)/user/(.*)=ism', $profile_url, $matches)) {
103                         $statusnet_host = $matches[1];
104                         $statusnet_user = $matches[2];
105                         $UserData = Network::fetchUrl('http://' . $statusnet_host . '/api/users/show.json?user_id=' . $statusnet_user);
106                         $user = json_decode($UserData);
107                         if ($user) {
108                                 $matches[2] = $user->screen_name;
109                                 return self::STATUSNET;
110                         }
111                 }
112
113                 // Mastodon, Pleroma
114                 if (preg_match('=https?://(.+?)/users/(.+)=ism', $profile_url, $matches)
115                         || preg_match('=https?://(.+?)/@(.+)=ism', $profile_url, $matches)
116                 ) {
117                         return self::ACTIVITYPUB;
118                 }
119
120                 // pumpio (http://host.name/user)
121                 if (preg_match('=https?://([\.\w]+)/([\.\w]+)$=ism', $profile_url, $matches)) {
122                         return self::PUMPIO;
123                 }
124
125                 return self::PHANTOM;
126         }
127
128         /**
129          * Returns a formatted mention from a profile URL and a display name
130          *
131          * @param string $profile_url
132          * @param string $display_name
133          * @return string
134          */
135         public static function formatMention($profile_url, $display_name)
136         {
137                 return $display_name . ' (' . self::getAddrFromProfileUrl($profile_url) . ')';
138         }
139 }