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