]> git.mxchange.org Git - friendica.git/blob - src/Core/ACL.php
Fixup command argument
[friendica.git] / src / Core / ACL.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Core;
23
24 use Friendica\App\Page;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Group;
29 use Friendica\Model\User;
30
31 /**
32  * Handle ACL management and display
33  */
34 class ACL
35 {
36         /**
37          * Returns the default lock state for the given user id
38          * @param int $uid 
39          * @return bool "true" if the default settings are non public
40          */
41         public static function getLockstateForUserId(int $uid)
42         {
43                 $user = User::getById($uid, ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
44                 return !empty($user['allow_cid']) || !empty($user['allow_gid']) || !empty($user['deny_cid']) || !empty($user['deny_gid']);
45         }
46
47         /**
48          * Returns a select input tag for private message recipient
49          *
50          * @param int  $selected Existing recipien contact ID
51          * @return string
52          * @throws \Exception
53          */
54         public static function getMessageContactSelectHTML(int $selected = null)
55         {
56                 $o = '';
57
58                 $page = DI::page();
59
60                 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
61                 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
62                 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
63                 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
64
65                 $condition = [
66                         'uid' => local_user(),
67                         'self' => false,
68                         'blocked' => false,
69                         'pending' => false,
70                         'archive' => false,
71                         'deleted' => false,
72                         'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
73                         'network' => Protocol::SUPPORT_PRIVATE,
74                 ];
75
76                 $contacts = Contact::selectToArray(
77                         ['id', 'name', 'addr', 'micro'],
78                         DBA::mergeConditions($condition, ["`notify` != ''"])
79                 );
80
81                 $arr = ['contact' => $contacts, 'entry' => $o];
82
83                 Hook::callAll(DI::module()->getName() . '_pre_recipient', $arr);
84
85                 $tpl = Renderer::getMarkupTemplate('acl/message_recipient.tpl');
86                 $o = Renderer::replaceMacros($tpl, [
87                         '$contacts' => $contacts,
88                         '$selected' => $selected,
89                 ]);
90
91                 Hook::callAll(DI::module()->getName() . '_post_recipient', $o);
92
93                 return $o;
94         }
95
96         /**
97          * Returns a minimal ACL block for self-only permissions
98          *
99          * @param int    $localUserId
100          * @param string $explanation
101          * @return string
102          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
103          */
104         public static function getSelfOnlyHTML(int $localUserId, string $explanation)
105         {
106                 $selfPublicContactId = Contact::getPublicIdByUserId($localUserId);
107
108                 $tpl = Renderer::getMarkupTemplate('acl/self_only.tpl');
109                 $o = Renderer::replaceMacros($tpl, [
110                         '$selfPublicContactId' => $selfPublicContactId,
111                         '$explanation' => $explanation,
112                 ]);
113
114                 return $o;
115         }
116
117         /**
118          * Return the default permission of the provided user array
119          *
120          * @param array $user
121          * @return array Hash of contact id lists
122          * @throws \Exception
123          */
124         public static function getDefaultUserPermissions(array $user = null)
125         {
126                 $aclFormatter = DI::aclFormatter();
127
128                 return [
129                         'allow_cid' => Contact::pruneUnavailable($aclFormatter->expand($user['allow_cid'] ?? '')),
130                         'allow_gid' => $aclFormatter->expand($user['allow_gid'] ?? ''),
131                         'deny_cid'  => $aclFormatter->expand($user['deny_cid']  ?? ''),
132                         'deny_gid'  => $aclFormatter->expand($user['deny_gid']  ?? ''),
133                 ];
134         }
135
136         /**
137          * Returns the ACL list of contacts for a given user id
138          *
139          * @param int   $user_id
140          * @param array $condition Additional contact lookup table conditions
141          * @return array
142          * @throws \Exception
143          */
144         public static function getContactListByUserId(int $user_id, array $condition = [])
145         {
146                 $fields = ['id', 'name', 'addr', 'micro'];
147                 $params = ['order' => ['name']];
148                 $acl_contacts = Contact::selectToArray(
149                         $fields,
150                         array_merge([
151                                 'uid' => $user_id,
152                                 'self' => false,
153                                 'blocked' => false,
154                                 'archive' => false,
155                                 'deleted' => false,
156                                 'pending' => false,
157                                 'network' => Protocol::FEDERATED,
158                                 'rel' => [Contact::FOLLOWER, Contact::FRIEND]
159                         ], $condition),
160                         $params
161                 );
162
163                 $acl_yourself = Contact::selectFirst($fields, ['uid' => $user_id, 'self' => true]);
164                 $acl_yourself['name'] = DI::l10n()->t('Yourself');
165
166                 $acl_contacts[] = $acl_yourself;
167
168                 $acl_forums = Contact::selectToArray($fields,
169                         ['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
170                         'network' => Protocol::FEDERATED, 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
171                 );
172
173                 $acl_contacts = array_merge($acl_forums, $acl_contacts);
174
175                 array_walk($acl_contacts, function (&$value) {
176                         $value['type'] = 'contact';
177                 });
178
179                 return $acl_contacts;
180         }
181
182         /**
183          * Returns the ACL list of groups (including meta-groups) for a given user id
184          *
185          * @param int $user_id
186          * @return array
187          */
188         public static function getGroupListByUserId(int $user_id)
189         {
190                 $acl_groups = [
191                         [
192                                 'id' => Group::FOLLOWERS,
193                                 'name' => DI::l10n()->t('Followers'),
194                                 'addr' => '',
195                                 'micro' => 'images/twopeople.png',
196                                 'type' => 'group',
197                         ],
198                         [
199                                 'id' => Group::MUTUALS,
200                                 'name' => DI::l10n()->t('Mutuals'),
201                                 'addr' => '',
202                                 'micro' => 'images/twopeople.png',
203                                 'type' => 'group',
204                         ]
205                 ];
206                 foreach (Group::getByUserId($user_id) as $group) {
207                         $acl_groups[] = [
208                                 'id' => $group['id'],
209                                 'name' => $group['name'],
210                                 'addr' => '',
211                                 'micro' => 'images/twopeople.png',
212                                 'type' => 'group',
213                         ];
214                 }
215
216                 return $acl_groups;
217         }
218
219         /**
220          * Return the full jot ACL selector HTML
221          *
222          * @param Page   $page
223          * @param int    $uid                   User ID
224          * @param bool   $for_federation
225          * @param array  $default_permissions   Static defaults permission array:
226          *                                      [
227          *                                      'allow_cid' => [],
228          *                                      'allow_gid' => [],
229          *                                      'deny_cid' => [],
230          *                                      'deny_gid' => []
231          *                                      ]
232          * @param array  $condition
233          * @param string $form_prefix
234          * @return string
235          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
236          */
237         public static function getFullSelectorHTML(
238                 Page $page,
239                 int $uid = null,
240                 bool $for_federation = false,
241                 array $default_permissions = [],
242                 array $condition = [],
243                 $form_prefix = ''
244         ) {
245                 if (empty($uid)) {
246                         return '';
247                 }
248
249                 static $input_group_id = 0;
250
251                 $user = User::getById($uid);
252
253                 $input_group_id++;
254
255                 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
256                 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
257                 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
258                 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
259
260                 // Defaults user permissions
261                 if (empty($default_permissions)) {
262                         $default_permissions = self::getDefaultUserPermissions($user);
263                 }
264
265                 $default_permissions = [
266                         'allow_cid' => $default_permissions['allow_cid'] ?? [],
267                         'allow_gid' => $default_permissions['allow_gid'] ?? [],
268                         'deny_cid'  => $default_permissions['deny_cid']  ?? [],
269                         'deny_gid'  => $default_permissions['deny_gid']  ?? [],
270                 ];
271
272                 if (count($default_permissions['allow_cid'])
273                         + count($default_permissions['allow_gid'])
274                         + count($default_permissions['deny_cid'])
275                         + count($default_permissions['deny_gid'])) {
276                         $visibility = 'custom';
277                 } else {
278                         $visibility = 'public';
279                         // Default permission display for custom panel
280                         $default_permissions['allow_gid'] = [Group::FOLLOWERS];
281                 }
282
283                 $jotnets_fields = [];
284                 if ($for_federation) {
285                         if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
286                                 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $user['uid']]);
287                                 if (DBA::isResult($mailacct)) {
288                                         $jotnets_fields[] = [
289                                                 'type' => 'checkbox',
290                                                 'field' => [
291                                                         'pubmail_enable',
292                                                         DI::l10n()->t('Post to Email'),
293                                                         !empty($mailacct['pubmail'])
294                                                 ]
295                                         ];
296         
297                                 }
298                         }
299                         Hook::callAll('jot_networks', $jotnets_fields);
300                 }
301
302                 $acl_contacts = self::getContactListByUserId($user['uid'], $condition);
303
304                 $acl_groups = self::getGroupListByUserId($user['uid']);
305
306                 $acl_list = array_merge($acl_groups, $acl_contacts);
307
308                 $input_names = [
309                         'visibility'    => $form_prefix ? $form_prefix . '[visibility]'    : 'visibility',
310                         'group_allow'   => $form_prefix ? $form_prefix . '[group_allow]'   : 'group_allow',
311                         'contact_allow' => $form_prefix ? $form_prefix . '[contact_allow]' : 'contact_allow',
312                         'group_deny'    => $form_prefix ? $form_prefix . '[group_deny]'    : 'group_deny',
313                         'contact_deny'  => $form_prefix ? $form_prefix . '[contact_deny]'  : 'contact_deny',
314                         'emailcc'       => $form_prefix ? $form_prefix . '[emailcc]'       : 'emailcc',
315                 ];
316
317                 $tpl = Renderer::getMarkupTemplate('acl/full_selector.tpl');
318                 $o = Renderer::replaceMacros($tpl, [
319                         '$public_title'   => DI::l10n()->t('Public'),
320                         '$public_desc'    => DI::l10n()->t('This content will be shown to all your followers and can be seen in the community pages and by anyone with its link.'),
321                         '$custom_title'   => DI::l10n()->t('Limited/Private'),
322                         '$custom_desc'    => DI::l10n()->t('This content will be shown only to the people in the first box, to the exception of the people mentioned in the second box. It won\'t appear anywhere public.'),
323                         '$allow_label'    => DI::l10n()->t('Show to:'),
324                         '$deny_label'     => DI::l10n()->t('Except to:'),
325                         '$emailcc'        => DI::l10n()->t('CC: email addresses'),
326                         '$emtitle'        => DI::l10n()->t('Example: bob@example.com, mary@example.com'),
327                         '$jotnets_summary' => DI::l10n()->t('Connectors'),
328                         '$visibility'     => $visibility,
329                         '$acl_contacts'   => $acl_contacts,
330                         '$acl_groups'     => $acl_groups,
331                         '$acl_list'       => $acl_list,
332                         '$contact_allow'  => implode(',', $default_permissions['allow_cid']),
333                         '$group_allow'    => implode(',', $default_permissions['allow_gid']),
334                         '$contact_deny'   => implode(',', $default_permissions['deny_cid']),
335                         '$group_deny'     => implode(',', $default_permissions['deny_gid']),
336                         '$for_federation' => $for_federation,
337                         '$jotnets_fields' => $jotnets_fields,
338                         '$input_names'    => $input_names,
339                         '$input_group_id' => $input_group_id,
340                 ]);
341
342                 return $o;
343         }
344 }