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\Util\Network;
17 * Handle ACL management and display
19 * @author Hypolite Petovan <mrpetovan@gmail.com>
21 class ACL extends BaseObject
24 * Returns a select input tag with all the contact of the local user
26 * @param string $selname Name attribute of the select input tag
27 * @param string $selclass Class attribute of the select input tag
28 * @param array $options Available options:
29 * - size: length of the select box
30 * - mutual_friends: Only used for the hook
31 * - single: Only used for the hook
32 * - exclude: Only used for the hook
33 * @param array $preselected Contact ID that should be already selected
36 public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
42 $size = defaults($options, 'size', 4);
43 $mutual = !empty($options['mutual_friends']);
44 $single = !empty($options['single']) && empty($options['multiple']);
45 $exclude = defaults($options, 'exclude', false);
47 switch (defaults($options, 'networks', Protocol::PHANTOM)) {
49 $networks = [NETWORK_DFRN];
52 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
55 if (!empty($a->user['prvnets'])) {
56 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
58 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS];
61 default: /// @TODO Maybe log this call?
65 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
67 Addon::callHooks('contact_select_options', $x);
73 if (!empty($x['mutual'])) {
74 $sql_extra .= sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
77 if (!empty($x['exclude'])) {
78 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
81 if (!empty($x['networks'])) {
82 /// @TODO rewrite to foreach()
83 array_walk($x['networks'], function (&$value) {
84 $value = "'" . DBA::escape($value) . "'";
86 $str_nets = implode(',', $x['networks']);
87 $sql_extra .= " AND `network` IN ( $str_nets ) ";
90 $tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : '');
92 if (!empty($x['single'])) {
93 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
95 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
98 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
99 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
101 ORDER BY `name` ASC ", intval(local_user())
104 $contacts = DBA::toArray($stmt);
106 $arr = ['contact' => $contacts, 'entry' => $o];
108 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
109 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
111 if (DBA::isResult($contacts)) {
112 foreach ($contacts as $contact) {
113 if (in_array($contact['id'], $preselected)) {
114 $selected = ' selected="selected" ';
119 $trimmed = mb_substr($contact['name'], 0, 20);
121 $o .= "<option value=\"{$contact['id']}\" $selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
125 $o .= '</select>' . PHP_EOL;
127 Addon::callHooks($a->module . '_post_' . $selname, $o);
133 * Returns a select input tag with all the contact of the local user
135 * @param string $selname Name attribute of the select input tag
136 * @param string $selclass Class attribute of the select input tag
137 * @param array $preselected Contact IDs that should be already selected
138 * @param int $size Length of the select box
139 * @param int $tabindex Select input tag tabindex attribute
142 public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
148 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
149 // to one recipient. By default our selector allows multiple selects amongst all contacts.
150 $sql_extra = sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
151 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", NETWORK_DFRN, NETWORK_DIASPORA);
153 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
155 $hidepreselected = '';
157 $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
158 $hidepreselected = ' style="display: none;"';
161 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
163 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
164 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
166 ORDER BY `name` ASC ", intval(local_user())
169 $contacts = DBA::toArray($stmt);
171 $arr = ['contact' => $contacts, 'entry' => $o];
173 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
174 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
178 if (DBA::isResult($contacts)) {
179 foreach ($contacts as $contact) {
180 if (in_array($contact['id'], $preselected)) {
181 $selected = ' selected="selected"';
186 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
188 $receiverlist[] = $trimmed;
190 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
194 $o .= '</select>' . PHP_EOL;
197 $o .= implode(', ', $receiverlist);
200 Addon::callHooks($a->module . '_post_' . $selname, $o);
205 private static function fixACL(&$item)
207 $item = intval(str_replace(['<', '>'], ['', ''], $item));
211 * Return the default permission of the provided user array
214 * @return array Hash of contact id lists
216 public static function getDefaultUserPermissions(array $user = null)
220 $acl_regex = '/<([0-9]+)>/i';
222 preg_match_all($acl_regex, defaults($user, 'allow_cid', ''), $matches);
223 $allow_cid = $matches[1];
224 preg_match_all($acl_regex, defaults($user, 'allow_gid', ''), $matches);
225 $allow_gid = $matches[1];
226 preg_match_all($acl_regex, defaults($user, 'deny_cid', ''), $matches);
227 $deny_cid = $matches[1];
228 preg_match_all($acl_regex, defaults($user, 'deny_gid', ''), $matches);
229 $deny_gid = $matches[1];
231 // Reformats the ACL data so that it is accepted by the JS frontend
232 array_walk($allow_cid, 'self::fixACL');
233 array_walk($allow_gid, 'self::fixACL');
234 array_walk($deny_cid, 'self::fixACL');
235 array_walk($deny_gid, 'self::fixACL');
237 Contact::pruneUnavailable($allow_cid);
240 'allow_cid' => $allow_cid,
241 'allow_gid' => $allow_gid,
242 'deny_cid' => $deny_cid,
243 'deny_gid' => $deny_gid,
248 * Return the full jot ACL selector HTML
251 * @param bool $show_jotnets
254 public static function getFullSelectorHTML(array $user = null, $show_jotnets = false)
257 if (empty($user['uid'])) {
261 $perms = self::getDefaultUserPermissions($user);
265 $imap_disabled = !function_exists('imap_open') || Config::get('system', 'imap_disabled');
267 $mail_enabled = false;
268 $pubmail_enabled = false;
270 if (!$imap_disabled) {
271 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
272 if (DBA::isResult($mailacct)) {
273 $mail_enabled = true;
274 $pubmail_enabled = !empty($mailacct['pubmail']);
278 if (empty($user['hidewall'])) {
280 $selected = $pubmail_enabled ? ' checked="checked"' : '';
281 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . L10n::t("Post to Email") . '</div>';
284 Addon::callHooks('jot_networks', $jotnets);
286 $jotnets .= L10n::t('Connectors disabled, since "%s" is enabled.',
287 L10n::t('Hide your profile details from unknown viewers?'));
291 $tpl = get_markup_template('acl_selector.tpl');
292 $o = replace_macros($tpl, [
293 '$showall' => L10n::t('Visible to everybody'),
294 '$show' => L10n::t('show'),
295 '$hide' => L10n::t('don\'t show'),
296 '$allowcid' => json_encode($perms['allow_cid']),
297 '$allowgid' => json_encode($perms['allow_gid']),
298 '$denycid' => json_encode($perms['deny_cid']),
299 '$denygid' => json_encode($perms['deny_gid']),
300 '$networks' => $show_jotnets,
301 '$emailcc' => L10n::t('CC: email addresses'),
302 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
303 '$jotnets' => $jotnets,
304 '$aclModalTitle' => L10n::t('Permissions'),
305 '$aclModalDismiss' => L10n::t('Close'),
307 'aclautomention' => Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'
315 * Searching for global contacts for autocompletion
317 * @brief Searching for global contacts for autocompletion
318 * @param string $search Name or part of a name or nick
319 * @param string $mode Search mode (e.g. "community")
320 * @return array with the search results
322 public static function contactAutocomplete($search, $mode)
324 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
328 // don't search if search term has less than 2 characters
329 if (!$search || mb_strlen($search) < 2) {
333 if (substr($search, 0, 1) === '@') {
334 $search = substr($search, 1);
337 // check if searching in the local global contact table is enabled
338 if (Config::get('system', 'poco_local_search')) {
339 $return = GContact::searchByName($search, $mode);
342 $p = $a->pager['page'] != 1 ? '&p=' . $a->pager['page'] : '';
344 $response = Network::curl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
345 if ($response['success']) {
346 $lsearch = json_decode($response['body'], true);
347 if (!empty($lsearch['results'])) {
348 $return = $lsearch['results'];
353 return defaults($return, []);