]> git.mxchange.org Git - friendica.git/blob - src/Content/ContactSelector.php
Merge pull request #8271 from MrPetovan/bug/8229-frio-mobile-back-to-top
[friendica.git] / src / Content / ContactSelector.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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         /**
37          * @param string  $current  current
38          * @param boolean $disabled optional, default false
39          * @return string
40          */
41         public static function pollInterval($current, $disabled = false)
42         {
43                 $dis = (($disabled) ? ' disabled="disabled" ' : '');
44                 $o = '';
45                 $o .= "<select id=\"contact-poll-interval\" name=\"poll\" $dis />" . "\r\n";
46
47                 $rep = [
48                         0 => DI::l10n()->t('Frequently'),
49                         1 => DI::l10n()->t('Hourly'),
50                         2 => DI::l10n()->t('Twice daily'),
51                         3 => DI::l10n()->t('Daily'),
52                         4 => DI::l10n()->t('Weekly'),
53                         5 => DI::l10n()->t('Monthly')
54                 ];
55
56                 foreach ($rep as $k => $v) {
57                         $selected = (($k == $current) ? " selected=\"selected\" " : "");
58                         $o .= "<option value=\"$k\" $selected >$v</option>\r\n";
59                 }
60                 $o .= "</select>\r\n";
61                 return $o;
62         }
63
64         /**
65          * @param string $profile Profile URL
66          * @return string Server URL
67          * @throws \Exception
68          */
69         private static function getServerURLForProfile($profile)
70         {
71                 $server_url = '';
72
73                 // Fetch the server url from the contact table
74                 $contact = DBA::selectFirst('contact', ['baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($profile)]);
75                 if (DBA::isResult($contact) && !empty($contact['baseurl'])) {
76                         $server_url = Strings::normaliseLink($contact['baseurl']);
77                 }
78
79                 if (empty($server_url)) {
80                         // Fetch the server url from the gcontact table
81                         $gcontact = DBA::selectFirst('gcontact', ['server_url'], ['nurl' => Strings::normaliseLink($profile)]);
82                         if (!empty($gcontact) && !empty($gcontact['server_url'])) {
83                                 $server_url = Strings::normaliseLink($gcontact['server_url']);
84                         }
85                 }
86
87                 if (empty($server_url)) {
88                         // Create the server url out of the profile url
89                         $parts = parse_url($profile);
90                         unset($parts['path']);
91                         $server_url = Strings::normaliseLink(Network::unparseURL($parts));
92                 }
93
94                 return $server_url;
95         }
96
97         /**
98          * @param string $network  network of the contact
99          * @param string $profile  optional, default empty
100          * @param string $protocol (Optional) Protocol that is used for the transmission
101          * @return string
102          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
103          */
104         public static function networkToName($network, $profile = '', $protocol = '')
105         {
106                 $nets = [
107                         Protocol::DFRN      =>   DI::l10n()->t('DFRN'),
108                         Protocol::OSTATUS   =>   DI::l10n()->t('OStatus'),
109                         Protocol::FEED      =>   DI::l10n()->t('RSS/Atom'),
110                         Protocol::MAIL      =>   DI::l10n()->t('Email'),
111                         Protocol::DIASPORA  =>   DI::l10n()->t('Diaspora'),
112                         Protocol::ZOT       =>   DI::l10n()->t('Zot!'),
113                         Protocol::LINKEDIN  =>   DI::l10n()->t('LinkedIn'),
114                         Protocol::XMPP      =>   DI::l10n()->t('XMPP/IM'),
115                         Protocol::MYSPACE   =>   DI::l10n()->t('MySpace'),
116                         Protocol::GPLUS     =>   DI::l10n()->t('Google+'),
117                         Protocol::PUMPIO    =>   DI::l10n()->t('pump.io'),
118                         Protocol::TWITTER   =>   DI::l10n()->t('Twitter'),
119                         Protocol::DISCOURSE =>   DI::l10n()->t('Discourse'),
120                         Protocol::DIASPORA2 =>   DI::l10n()->t('Diaspora Connector'),
121                         Protocol::STATUSNET =>   DI::l10n()->t('GNU Social Connector'),
122                         Protocol::ACTIVITYPUB => DI::l10n()->t('ActivityPub'),
123                         Protocol::PNUT      =>   DI::l10n()->t('pnut'),
124                 ];
125
126                 Hook::callAll('network_to_name', $nets);
127
128                 $search  = array_keys($nets);
129                 $replace = array_values($nets);
130
131                 $networkname = str_replace($search, $replace, $network);
132
133                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
134                         $server_url = self::getServerURLForProfile($profile);
135
136                         // Now query the GServer for the platform name
137                         $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
138
139                         if (DBA::isResult($gserver)) {
140                                 if (!empty($gserver['platform'])) {
141                                         $platform = $gserver['platform'];
142                                 } elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
143                                         $platform = self::networkToName($gserver['network']);
144                                 }
145
146                                 if (!empty($platform)) {
147                                         $networkname = $platform;
148
149                                         if ($network == Protocol::ACTIVITYPUB) {
150                                                 $networkname .= ' (AP)';
151                                         }
152                                 }
153                         }
154                 }
155
156                 if (!empty($protocol) && ($protocol != $network)) {
157                         $networkname = DI::l10n()->t('%s (via %s)', $networkname, self::networkToName($protocol));
158                 }
159
160                 return $networkname;
161         }
162
163         /**
164          * @param string $network network
165          * @param string $profile optional, default empty
166          * @return string
167          * @throws \Exception
168          */
169         public static function networkToIcon($network, $profile = "")
170         {
171                 $nets = [
172                         Protocol::DFRN      =>   'friendica',
173                         Protocol::OSTATUS   =>   'gnu-social', // There is no generic OStatus icon
174                         Protocol::FEED      =>   'rss',
175                         Protocol::MAIL      =>   'inbox',
176                         Protocol::DIASPORA  =>   'diaspora',
177                         Protocol::ZOT       =>   'hubzilla',
178                         Protocol::LINKEDIN  =>   'linkedin',
179                         Protocol::XMPP      =>   'xmpp',
180                         Protocol::MYSPACE   =>   'file-text-o', /// @todo
181                         Protocol::GPLUS     =>   'google-plus',
182                         Protocol::PUMPIO    =>   'file-text-o', /// @todo
183                         Protocol::TWITTER   =>   'twitter',
184                         Protocol::DISCOURSE =>   'dot-circle-o', /// @todo
185                         Protocol::DIASPORA2 =>   'diaspora',
186                         Protocol::STATUSNET =>   'gnu-social',
187                         Protocol::ACTIVITYPUB => 'activitypub',
188                         Protocol::PNUT      =>   'file-text-o', /// @todo
189                 ];
190
191                 $platform_icons = ['diaspora' => 'diaspora', 'friendica' => 'friendica', 'friendika' => 'friendica',
192                         'GNU Social' => 'gnu-social', 'gnusocial' => 'gnu-social', 'hubzilla' => 'hubzilla',
193                         'mastodon' => 'mastodon', 'peertube' => 'peertube', 'pixelfed' => 'pixelfed',
194                         'pleroma' => 'pleroma', 'red' => 'hubzilla', 'redmatrix' => 'hubzilla',
195                         'socialhome' => 'social-home', 'wordpress' => 'wordpress'];
196
197                 $search  = array_keys($nets);
198                 $replace = array_values($nets);
199
200                 $network_icon = str_replace($search, $replace, $network);
201
202                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
203                         $server_url = self::getServerURLForProfile($profile);
204
205                         // Now query the GServer for the platform name
206                         $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => $server_url]);
207
208                         if (DBA::isResult($gserver) && !empty($gserver['platform'])) {
209                                 $network_icon = $platform_icons[strtolower($gserver['platform'])] ?? $network_icon;
210                         }
211                 }
212
213                 return $network_icon;
214         }
215 }