]> git.mxchange.org Git - friendica.git/blob - src/Content/ContactSelector.php
Merge branch 'bug/phpinfo-accessible-hotfix' into develop
[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                         // Create the server url out of the profile url
81                         $parts = parse_url($profile);
82                         unset($parts['path']);
83                         $server_url = Strings::normaliseLink(Network::unparseURL($parts));
84                 }
85
86                 return $server_url;
87         }
88
89         /**
90          * @param string $network  network of the contact
91          * @param string $profile  optional, default empty
92          * @param string $protocol (Optional) Protocol that is used for the transmission
93          * @return string
94          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
95          */
96         public static function networkToName($network, $profile = '', $protocol = '')
97         {
98                 $nets = [
99                         Protocol::DFRN      =>   DI::l10n()->t('DFRN'),
100                         Protocol::OSTATUS   =>   DI::l10n()->t('OStatus'),
101                         Protocol::FEED      =>   DI::l10n()->t('RSS/Atom'),
102                         Protocol::MAIL      =>   DI::l10n()->t('Email'),
103                         Protocol::DIASPORA  =>   DI::l10n()->t('Diaspora'),
104                         Protocol::ZOT       =>   DI::l10n()->t('Zot!'),
105                         Protocol::LINKEDIN  =>   DI::l10n()->t('LinkedIn'),
106                         Protocol::XMPP      =>   DI::l10n()->t('XMPP/IM'),
107                         Protocol::MYSPACE   =>   DI::l10n()->t('MySpace'),
108                         Protocol::GPLUS     =>   DI::l10n()->t('Google+'),
109                         Protocol::PUMPIO    =>   DI::l10n()->t('pump.io'),
110                         Protocol::TWITTER   =>   DI::l10n()->t('Twitter'),
111                         Protocol::DISCOURSE =>   DI::l10n()->t('Discourse'),
112                         Protocol::DIASPORA2 =>   DI::l10n()->t('Diaspora Connector'),
113                         Protocol::STATUSNET =>   DI::l10n()->t('GNU Social Connector'),
114                         Protocol::ACTIVITYPUB => DI::l10n()->t('ActivityPub'),
115                         Protocol::PNUT      =>   DI::l10n()->t('pnut'),
116                 ];
117
118                 Hook::callAll('network_to_name', $nets);
119
120                 $search  = array_keys($nets);
121                 $replace = array_values($nets);
122
123                 $networkname = str_replace($search, $replace, $network);
124
125                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
126                         $server_url = self::getServerURLForProfile($profile);
127
128                         // Now query the GServer for the platform name
129                         $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
130
131                         if (DBA::isResult($gserver)) {
132                                 if (!empty($gserver['platform'])) {
133                                         $platform = $gserver['platform'];
134                                 } elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
135                                         $platform = self::networkToName($gserver['network']);
136                                 }
137
138                                 if (!empty($platform)) {
139                                         $networkname = $platform;
140
141                                         if ($network == Protocol::ACTIVITYPUB) {
142                                                 $networkname .= ' (AP)';
143                                         }
144                                 }
145                         }
146                 }
147
148                 if (!empty($protocol) && ($protocol != $network)) {
149                         $networkname = DI::l10n()->t('%s (via %s)', $networkname, self::networkToName($protocol));
150                 }
151
152                 return $networkname;
153         }
154
155         /**
156          * @param string $network network
157          * @param string $profile optional, default empty
158          * @return string
159          * @throws \Exception
160          */
161         public static function networkToIcon($network, $profile = "")
162         {
163                 $nets = [
164                         Protocol::DFRN      =>   'friendica',
165                         Protocol::OSTATUS   =>   'gnu-social', // There is no generic OStatus icon
166                         Protocol::FEED      =>   'rss',
167                         Protocol::MAIL      =>   'inbox',
168                         Protocol::DIASPORA  =>   'diaspora',
169                         Protocol::ZOT       =>   'hubzilla',
170                         Protocol::LINKEDIN  =>   'linkedin',
171                         Protocol::XMPP      =>   'xmpp',
172                         Protocol::MYSPACE   =>   'file-text-o', /// @todo
173                         Protocol::GPLUS     =>   'google-plus',
174                         Protocol::PUMPIO    =>   'file-text-o', /// @todo
175                         Protocol::TWITTER   =>   'twitter',
176                         Protocol::DISCOURSE =>   'dot-circle-o', /// @todo
177                         Protocol::DIASPORA2 =>   'diaspora',
178                         Protocol::STATUSNET =>   'gnu-social',
179                         Protocol::ACTIVITYPUB => 'activitypub',
180                         Protocol::PNUT      =>   'file-text-o', /// @todo
181                 ];
182
183                 $platform_icons = ['diaspora' => 'diaspora', 'friendica' => 'friendica', 'friendika' => 'friendica',
184                         'GNU Social' => 'gnu-social', 'gnusocial' => 'gnu-social', 'hubzilla' => 'hubzilla',
185                         'mastodon' => 'mastodon', 'peertube' => 'peertube', 'pixelfed' => 'pixelfed',
186                         'pleroma' => 'pleroma', 'red' => 'hubzilla', 'redmatrix' => 'hubzilla',
187                         'socialhome' => 'social-home', 'wordpress' => 'wordpress'];
188
189                 $search  = array_keys($nets);
190                 $replace = array_values($nets);
191
192                 $network_icon = str_replace($search, $replace, $network);
193
194                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
195                         $server_url = self::getServerURLForProfile($profile);
196
197                         // Now query the GServer for the platform name
198                         $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => $server_url]);
199
200                         if (DBA::isResult($gserver) && !empty($gserver['platform'])) {
201                                 $network_icon = $platform_icons[strtolower($gserver['platform'])] ?? $network_icon;
202                         }
203                 }
204
205                 return $network_icon;
206         }
207 }