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