3 * @copyright Copyright (C) 2010-2024, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Content;
24 use Friendica\Core\Hook;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
28 use Friendica\Util\Network;
29 use Friendica\Util\Strings;
32 * ContactSelector class
36 static $serverdata = [];
37 static $server_url = [];
40 * @param string $current current
41 * @param boolean $disabled optional, default false
44 public static function pollInterval(string $current, bool $disabled = false): string
46 $dis = (($disabled) ? ' disabled="disabled" ' : '');
48 $o .= "<select id=\"contact-poll-interval\" name=\"poll\" $dis />" . "\r\n";
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')
59 foreach ($rep as $k => $v) {
60 $selected = (($k == $current) ? " selected=\"selected\" " : "");
61 $o .= "<option value=\"$k\" $selected >$v</option>\r\n";
63 $o .= "</select>\r\n";
67 private static function getServerForProfile(string $profile)
69 $server_url = self::getServerURLForProfile($profile);
71 if (!empty(self::$serverdata[$server_url])) {
72 return self::$serverdata[$server_url];
75 // Now query the GServer for the platform name
76 $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
78 self::$serverdata[$server_url] = $gserver;
83 * @param string $profile Profile URL
84 * @return string Server URL
87 private static function getServerURLForProfile(string $profile): string
89 if (!empty(self::$server_url[$profile])) {
90 return self::$server_url[$profile];
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']);
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));
108 self::$server_url[$profile] = $server_url;
114 * Determines network name
116 * @param string $network network of the contact
117 * @param string $profile optional, default empty
118 * @param string $protocol (Optional) Protocol that is used for the transmission
119 * @param int $gsid Server id
121 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
123 public static function networkToName(string $network, string $profile = '', string $protocol = '', int $gsid = null): string
126 Protocol::DFRN => DI::l10n()->t('DFRN'),
127 Protocol::OSTATUS => DI::l10n()->t('OStatus'),
128 Protocol::FEED => DI::l10n()->t('RSS/Atom'),
129 Protocol::MAIL => DI::l10n()->t('Email'),
130 Protocol::DIASPORA => DI::l10n()->t('Diaspora'),
131 Protocol::ZOT => DI::l10n()->t('Zot!'),
132 Protocol::LINKEDIN => DI::l10n()->t('LinkedIn'),
133 Protocol::XMPP => DI::l10n()->t('XMPP/IM'),
134 Protocol::MYSPACE => DI::l10n()->t('MySpace'),
135 Protocol::GPLUS => DI::l10n()->t('Google+'),
136 Protocol::PUMPIO => DI::l10n()->t('pump.io'),
137 Protocol::TWITTER => DI::l10n()->t('Twitter'),
138 Protocol::DISCOURSE => DI::l10n()->t('Discourse'),
139 Protocol::DIASPORA2 => DI::l10n()->t('Diaspora Connector'),
140 Protocol::STATUSNET => DI::l10n()->t('GNU Social Connector'),
141 Protocol::ACTIVITYPUB => DI::l10n()->t('ActivityPub'),
142 Protocol::PNUT => DI::l10n()->t('pnut'),
143 Protocol::TUMBLR => DI::l10n()->t('Tumblr'),
144 Protocol::BLUESKY => DI::l10n()->t('Bluesky'),
147 Hook::callAll('network_to_name', $nets);
149 $search = array_keys($nets);
150 $replace = array_values($nets);
152 $networkname = str_replace($search, $replace, $network);
154 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
155 if (!empty($gsid) && !empty(self::$serverdata[$gsid])) {
156 $gserver = self::$serverdata[$gsid];
157 } elseif (!empty($gsid)) {
158 $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['id' => $gsid]);
159 self::$serverdata[$gsid] = $gserver;
161 $gserver = self::getServerForProfile($profile);
164 if (!empty($gserver['platform'])) {
165 $platform = $gserver['platform'];
166 } elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
167 $platform = self::networkToName($gserver['network']);
170 if (!empty($platform)) {
171 $networkname = $platform;
173 if ($network == Protocol::ACTIVITYPUB) {
174 $networkname .= ' (AP)';
179 if (!empty($protocol) && ($protocol != $network)) {
180 $networkname = DI::l10n()->t('%s (via %s)', $networkname, self::networkToName($protocol));
187 * Determines network's icon name
189 * @param string $network network
190 * @param string $profile optional, default empty
191 * @param int $gsid Server id
192 * @return string Name for network icon
195 public static function networkToIcon(string $network, string $profile = "", int $gsid = null): string
198 Protocol::DFRN => 'friendica',
199 Protocol::OSTATUS => 'gnu-social', // There is no generic OStatus icon
200 Protocol::FEED => 'rss',
201 Protocol::MAIL => 'inbox',
202 Protocol::DIASPORA => 'diaspora',
203 Protocol::ZOT => 'hubzilla',
204 Protocol::LINKEDIN => 'linkedin',
205 Protocol::XMPP => 'xmpp',
206 Protocol::MYSPACE => 'file-text-o', /// @todo
207 Protocol::GPLUS => 'google-plus',
208 Protocol::PUMPIO => 'file-text-o', /// @todo
209 Protocol::TWITTER => 'twitter',
210 Protocol::DISCOURSE => 'dot-circle-o', /// @todo
211 Protocol::DIASPORA2 => 'diaspora',
212 Protocol::STATUSNET => 'gnu-social',
213 Protocol::ACTIVITYPUB => 'activitypub',
214 Protocol::PNUT => 'file-text-o', /// @todo
215 Protocol::TUMBLR => 'tumblr',
216 Protocol::BLUESKY => 'circle', /// @todo
219 $platform_icons = ['diaspora' => 'diaspora', 'friendica' => 'friendica', 'friendika' => 'friendica',
220 'GNU Social' => 'gnu-social', 'gnusocial' => 'gnu-social', 'hubzilla' => 'hubzilla',
221 'mastodon' => 'mastodon', 'peertube' => 'peertube', 'pixelfed' => 'pixelfed',
222 'pleroma' => 'pleroma', 'red' => 'hubzilla', 'redmatrix' => 'hubzilla',
223 'socialhome' => 'social-home', 'wordpress' => 'wordpress', 'lemmy' => 'users',
224 'plume' => 'plume', 'funkwhale' => 'funkwhale', 'nextcloud' => 'nextcloud', 'drupal' => 'drupal',
225 'firefish' => 'fire', 'calckey' => 'calculator', 'kbin' => 'check', 'threads' => 'instagram'];
227 $search = array_keys($nets);
228 $replace = array_values($nets);
230 $network_icon = str_replace($search, $replace, $network);
232 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
233 if (!empty($gsid) && !empty(self::$serverdata[$gsid])) {
234 $gserver = self::$serverdata[$gsid];
235 } elseif (!empty($gsid)) {
236 $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['id' => $gsid]);
237 self::$serverdata[$gsid] = $gserver;
239 $gserver = self::getServerForProfile($profile);
241 if (!empty($gserver['platform'])) {
242 $network_icon = $platform_icons[strtolower($gserver['platform'])] ?? $network_icon;
246 return $network_icon;