]> git.mxchange.org Git - friendica.git/blob - src/Content/ContactSelector.php
fd574889e622acbbcd27e9379cb1c6cc816e3dbe
[friendica.git] / src / Content / ContactSelector.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content;
23
24 use Friendica\Core\Hook;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Util\Network;
29 use Friendica\Util\Strings;
30
31 /**
32  * ContactSelector class
33  */
34 class ContactSelector
35 {
36         static $serverdata = [];
37         static $server_url = [];
38
39         /**
40          * @param string  $current  current
41          * @param boolean $disabled optional, default false
42          * @return string
43          */
44         public static function pollInterval(string $current, bool $disabled = false): string
45         {
46                 $dis = (($disabled) ? ' disabled="disabled" ' : '');
47                 $o = '';
48                 $o .= "<select id=\"contact-poll-interval\" name=\"poll\" $dis />" . "\r\n";
49
50                 $rep = [
51                         0 => DI::l10n()->t('Frequently'),
52                         1 => DI::l10n()->t('Hourly'),
53                         2 => DI::l10n()->t('Twice daily'),
54                         3 => DI::l10n()->t('Daily'),
55                         4 => DI::l10n()->t('Weekly'),
56                         5 => DI::l10n()->t('Monthly')
57                 ];
58
59                 foreach ($rep as $k => $v) {
60                         $selected = (($k == $current) ? " selected=\"selected\" " : "");
61                         $o .= "<option value=\"$k\" $selected >$v</option>\r\n";
62                 }
63                 $o .= "</select>\r\n";
64                 return $o;
65         }
66
67         private static function getServerForProfile(string $profile)
68         {
69                 $server_url = self::getServerURLForProfile($profile);
70
71                 if (!empty(self::$serverdata[$server_url])) {
72                         return self::$serverdata[$server_url];
73                 }
74
75                 // Now query the GServer for the platform name
76                 $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
77
78                 self::$serverdata[$server_url] = $gserver;
79                 return $gserver;
80         }
81
82         /**
83          * @param string $profile Profile URL
84          * @return string Server URL
85          * @throws \Exception
86          */
87         private static function getServerURLForProfile(string $profile): string
88         {
89                 if (!empty(self::$server_url[$profile])) {
90                         return self::$server_url[$profile];
91                 }
92
93                 $server_url = '';
94
95                 // Fetch the server url from the contact table
96                 $contact = DBA::selectFirst('contact', ['baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($profile)]);
97                 if (DBA::isResult($contact) && !empty($contact['baseurl'])) {
98                         $server_url = Strings::normaliseLink($contact['baseurl']);
99                 }
100
101                 if (empty($server_url)) {
102                         // Create the server url out of the profile url
103                         $parts = parse_url($profile);
104                         unset($parts['path']);
105                         $server_url = Strings::normaliseLink(Network::unparseURL($parts));
106                 }
107
108                 self::$server_url[$profile] = $server_url;
109
110                 return $server_url;
111         }
112
113         /**
114          * @param string $network  network of the contact
115          * @param string $profile  optional, default empty
116          * @param string $protocol (Optional) Protocol that is used for the transmission
117          * @param int $gsid ??? (@TODO)
118          * @return string
119          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
120          */
121         public static function networkToName(string $network, string $profile = '', string $protocol = '', int $gsid = 0): string
122         {
123                 $nets = [
124                         Protocol::DFRN      =>   DI::l10n()->t('DFRN'),
125                         Protocol::OSTATUS   =>   DI::l10n()->t('OStatus'),
126                         Protocol::FEED      =>   DI::l10n()->t('RSS/Atom'),
127                         Protocol::MAIL      =>   DI::l10n()->t('Email'),
128                         Protocol::DIASPORA  =>   DI::l10n()->t('Diaspora'),
129                         Protocol::ZOT       =>   DI::l10n()->t('Zot!'),
130                         Protocol::LINKEDIN  =>   DI::l10n()->t('LinkedIn'),
131                         Protocol::XMPP      =>   DI::l10n()->t('XMPP/IM'),
132                         Protocol::MYSPACE   =>   DI::l10n()->t('MySpace'),
133                         Protocol::GPLUS     =>   DI::l10n()->t('Google+'),
134                         Protocol::PUMPIO    =>   DI::l10n()->t('pump.io'),
135                         Protocol::TWITTER   =>   DI::l10n()->t('Twitter'),
136                         Protocol::DISCOURSE =>   DI::l10n()->t('Discourse'),
137                         Protocol::DIASPORA2 =>   DI::l10n()->t('Diaspora Connector'),
138                         Protocol::STATUSNET =>   DI::l10n()->t('GNU Social Connector'),
139                         Protocol::ACTIVITYPUB => DI::l10n()->t('ActivityPub'),
140                         Protocol::PNUT      =>   DI::l10n()->t('pnut'),
141                 ];
142
143                 Hook::callAll('network_to_name', $nets);
144
145                 $search  = array_keys($nets);
146                 $replace = array_values($nets);
147
148                 $networkname = str_replace($search, $replace, $network);
149
150                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
151                         if (!empty($gsid) && !empty(self::$serverdata[$gsid])) {
152                                 $gserver = self::$serverdata[$gsid];
153                         } elseif (!empty($gsid)) {
154                                 $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['id' => $gsid]);
155                                 self::$serverdata[$gsid] = $gserver;
156                         } else {
157                                 $gserver = self::getServerForProfile($profile);
158                         }
159
160                         if (!empty($gserver['platform'])) {
161                                 $platform = $gserver['platform'];
162                         } elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
163                                 $platform = self::networkToName($gserver['network']);
164                         }
165
166                         if (!empty($platform)) {
167                                 $networkname = $platform;
168
169                                 if ($network == Protocol::ACTIVITYPUB) {
170                                         $networkname .= ' (AP)';
171                                 }
172                         }
173                 }
174
175                 if (!empty($protocol) && ($protocol != $network)) {
176                         $networkname = DI::l10n()->t('%s (via %s)', $networkname, self::networkToName($protocol));
177                 }
178
179                 return $networkname;
180         }
181
182         /**
183          * @param string $network network
184          * @param string $profile optional, default empty
185          * @param int $gsid ??? (@TODO)
186          * @return string
187          * @throws \Exception
188          */
189         public static function networkToIcon(string $network, string $profile = "", int $gsid = 0): string
190         {
191                 $nets = [
192                         Protocol::DFRN      =>   'friendica',
193                         Protocol::OSTATUS   =>   'gnu-social', // There is no generic OStatus icon
194                         Protocol::FEED      =>   'rss',
195                         Protocol::MAIL      =>   'inbox',
196                         Protocol::DIASPORA  =>   'diaspora',
197                         Protocol::ZOT       =>   'hubzilla',
198                         Protocol::LINKEDIN  =>   'linkedin',
199                         Protocol::XMPP      =>   'xmpp',
200                         Protocol::MYSPACE   =>   'file-text-o', /// @todo
201                         Protocol::GPLUS     =>   'google-plus',
202                         Protocol::PUMPIO    =>   'file-text-o', /// @todo
203                         Protocol::TWITTER   =>   'twitter',
204                         Protocol::DISCOURSE =>   'dot-circle-o', /// @todo
205                         Protocol::DIASPORA2 =>   'diaspora',
206                         Protocol::STATUSNET =>   'gnu-social',
207                         Protocol::ACTIVITYPUB => 'activitypub',
208                         Protocol::PNUT      =>   'file-text-o', /// @todo
209                 ];
210
211                 $platform_icons = ['diaspora' => 'diaspora', 'friendica' => 'friendica', 'friendika' => 'friendica',
212                         'GNU Social' => 'gnu-social', 'gnusocial' => 'gnu-social', 'hubzilla' => 'hubzilla',
213                         'mastodon' => 'mastodon', 'peertube' => 'peertube', 'pixelfed' => 'pixelfed',
214                         'pleroma' => 'pleroma', 'red' => 'hubzilla', 'redmatrix' => 'hubzilla',
215                         'socialhome' => 'social-home', 'wordpress' => 'wordpress'];
216
217                 $search  = array_keys($nets);
218                 $replace = array_values($nets);
219
220                 $network_icon = str_replace($search, $replace, $network);
221
222                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
223                         if (!empty($gsid) && !empty(self::$serverdata[$gsid])) {
224                                 $gserver = self::$serverdata[$gsid];
225                         } elseif (!empty($gsid)) {
226                                 $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['id' => $gsid]);
227                                 self::$serverdata[$gsid] = $gserver;
228                         } else {
229                                 $gserver = self::getServerForProfile($profile);
230                         }
231                         if (!empty($gserver['platform'])) {
232                                 $network_icon = $platform_icons[strtolower($gserver['platform'])] ?? $network_icon;
233                         }
234                 }
235
236                 return $network_icon;
237         }
238 }