4 * @file src/Core/Acl.php
7 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Content\Feature;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\GContact;
14 use Friendica\Core\Session;
15 use Friendica\Util\Network;
18 * Handle ACL management and display
20 * @author Hypolite Petovan <hypolite@mrpetovan.com>
22 class ACL extends BaseObject
25 * Returns a select input tag with all the contact of the local user
27 * @param string $selname Name attribute of the select input tag
28 * @param string $selclass Class attribute of the select input tag
29 * @param array $options Available options:
30 * - size: length of the select box
31 * - mutual_friends: Only used for the hook
32 * - single: Only used for the hook
33 * - exclude: Only used for the hook
34 * @param array $preselected Contact ID that should be already selected
38 public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
44 $size = ($options['size'] ?? 0) ?: 4;
45 $mutual = !empty($options['mutual_friends']);
46 $single = !empty($options['single']) && empty($options['multiple']);
47 $exclude = $options['exclude'] ?? false;
49 switch (($options['networks'] ?? '') ?: Protocol::PHANTOM) {
51 $networks = [Protocol::DFRN];
55 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
59 if (!empty($a->user['prvnets'])) {
60 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
62 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA, Protocol::OSTATUS];
66 default: /// @TODO Maybe log this call?
70 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
72 Hook::callAll('contact_select_options', $x);
78 if (!empty($x['mutual'])) {
79 $sql_extra .= sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
82 if (!empty($x['exclude'])) {
83 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
86 if (!empty($x['networks'])) {
87 /// @TODO rewrite to foreach()
88 array_walk($x['networks'], function (&$value) {
89 $value = "'" . DBA::escape($value) . "'";
91 $str_nets = implode(',', $x['networks']);
92 $sql_extra .= " AND `network` IN ( $str_nets ) ";
95 $tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : '');
97 if (!empty($x['single'])) {
98 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
100 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
103 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
104 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != ''
106 ORDER BY `name` ASC ", intval(local_user())
109 $contacts = DBA::toArray($stmt);
111 $arr = ['contact' => $contacts, 'entry' => $o];
113 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
114 Hook::callAll($a->module . '_pre_' . $selname, $arr);
116 if (DBA::isResult($contacts)) {
117 foreach ($contacts as $contact) {
118 if (in_array($contact['id'], $preselected)) {
119 $selected = ' selected="selected" ';
124 $trimmed = mb_substr($contact['name'], 0, 20);
126 $o .= "<option value=\"{$contact['id']}\" $selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
130 $o .= '</select>' . PHP_EOL;
132 Hook::callAll($a->module . '_post_' . $selname, $o);
138 * Returns a select input tag with all the contact of the local user
140 * @param string $selname Name attribute of the select input tag
141 * @param string $selclass Class attribute of the select input tag
142 * @param array $preselected Contact IDs that should be already selected
143 * @param int $size Length of the select box
144 * @param int $tabindex Select input tag tabindex attribute
148 public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
154 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
155 // to one recipient. By default our selector allows multiple selects amongst all contacts.
156 $sql_extra = sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
157 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", Protocol::DFRN, Protocol::DIASPORA);
159 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
161 $hidepreselected = '';
163 $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
164 $hidepreselected = ' style="display: none;"';
167 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
169 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
170 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != ''
172 ORDER BY `name` ASC ", intval(local_user())
175 $contacts = DBA::toArray($stmt);
177 $arr = ['contact' => $contacts, 'entry' => $o];
179 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
180 Hook::callAll($a->module . '_pre_' . $selname, $arr);
184 if (DBA::isResult($contacts)) {
185 foreach ($contacts as $contact) {
186 if (in_array($contact['id'], $preselected)) {
187 $selected = ' selected="selected"';
192 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
194 $receiverlist[] = $trimmed;
196 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
200 $o .= '</select>' . PHP_EOL;
203 $o .= implode(', ', $receiverlist);
206 Hook::callAll($a->module . '_post_' . $selname, $o);
211 private static function fixACL(&$item)
213 $item = intval(str_replace(['<', '>'], ['', ''], $item));
217 * Return the default permission of the provided user array
220 * @return array Hash of contact id lists
223 public static function getDefaultUserPermissions(array $user = null)
227 $acl_regex = '/<([0-9]+)>/i';
229 preg_match_all($acl_regex, $user['allow_cid'] ?? '', $matches);
230 $allow_cid = $matches[1];
231 preg_match_all($acl_regex, $user['allow_gid'] ?? '', $matches);
232 $allow_gid = $matches[1];
233 preg_match_all($acl_regex, $user['deny_cid'] ?? '', $matches);
234 $deny_cid = $matches[1];
235 preg_match_all($acl_regex, $user['deny_gid'] ?? '', $matches);
236 $deny_gid = $matches[1];
238 // Reformats the ACL data so that it is accepted by the JS frontend
239 array_walk($allow_cid, 'self::fixACL');
240 array_walk($allow_gid, 'self::fixACL');
241 array_walk($deny_cid, 'self::fixACL');
242 array_walk($deny_gid, 'self::fixACL');
244 Contact::pruneUnavailable($allow_cid);
247 'allow_cid' => $allow_cid,
248 'allow_gid' => $allow_gid,
249 'deny_cid' => $deny_cid,
250 'deny_gid' => $deny_gid,
255 * Return the full jot ACL selector HTML
257 * @param array $user User array
258 * @param bool $show_jotnets
259 * @param array $default_permissions Static defaults permission array: ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']
261 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
263 public static function getFullSelectorHTML(array $user = null, $show_jotnets = false, array $default_permissions = [])
265 // Defaults user permissions
266 if (empty($default_permissions)) {
267 $default_permissions = self::getDefaultUserPermissions($user);
270 $jotnets_fields = [];
272 $mail_enabled = false;
273 $pubmail_enabled = false;
275 if (function_exists('imap_open') && !Config::get('system', 'imap_disabled')) {
276 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
277 if (DBA::isResult($mailacct)) {
278 $mail_enabled = true;
279 $pubmail_enabled = !empty($mailacct['pubmail']);
283 if (empty($default_permissions['hidewall'])) {
285 $jotnets_fields[] = [
286 'type' => 'checkbox',
289 L10n::t('Post to Email'),
295 Hook::callAll('jot_networks', $jotnets_fields);
299 $tpl = Renderer::getMarkupTemplate('acl_selector.tpl');
300 $o = Renderer::replaceMacros($tpl, [
301 '$showall' => L10n::t('Visible to everybody'),
302 '$show' => L10n::t('show'),
303 '$hide' => L10n::t('don\'t show'),
304 '$allowcid' => json_encode(($default_permissions['allow_cid'] ?? '') ?: []), // We need arrays for
305 '$allowgid' => json_encode(($default_permissions['allow_gid'] ?? '') ?: []), // Javascript since we
306 '$denycid' => json_encode(($default_permissions['deny_cid'] ?? '') ?: []), // call .remove() and
307 '$denygid' => json_encode(($default_permissions['deny_gid'] ?? '') ?: []), // .push() on these values
308 '$networks' => $show_jotnets,
309 '$emailcc' => L10n::t('CC: email addresses'),
310 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
311 '$jotnets_enabled' => empty($default_permissions['hidewall']),
312 '$jotnets_summary' => L10n::t('Connectors'),
313 '$jotnets_fields' => $jotnets_fields,
314 '$jotnets_disabled_label' => L10n::t('Connectors disabled, since "%s" is enabled.', L10n::t('Hide your profile details from unknown viewers?')),
315 '$aclModalTitle' => L10n::t('Permissions'),
316 '$aclModalDismiss' => L10n::t('Close'),
318 'aclautomention' => !empty($user['uid']) && Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'