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