4 * @file src/Core/Acl.php
7 namespace Friendica\Core;
10 use Friendica\BaseObject;
11 use Friendica\Content\Feature;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Contact;
14 use Friendica\Model\GContact;
15 use Friendica\Util\Network;
16 use const CONTACT_IS_FRIEND;
17 use const NETWORK_DFRN;
18 use const NETWORK_DIASPORA;
19 use const NETWORK_FACEBOOK;
20 use const NETWORK_MAIL;
21 use const NETWORK_OSTATUS;
24 use function defaults;
25 use function get_markup_template;
26 use function get_server;
27 use function local_user;
28 use function remote_user;
29 use function replace_macros;
32 * Handle ACL management and display
34 * @author Hypolite Petovan <mrpetovan@gmail.com>
36 class ACL extends BaseObject
39 * Returns a select input tag with all the contact of the local user
41 * @param string $selname Name attribute of the select input tag
42 * @param string $selclass Class attribute of the select input tag
43 * @param array $options Available options:
44 * - size: length of the select box
45 * - mutual_friends: Only used for the hook
46 * - single: Only used for the hook
47 * - exclude: Only used for the hook
48 * @param array $preselected Contact ID that should be already selected
51 public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
57 $size = defaults($options, 'size', 4);
58 $mutual = !empty($options['mutual_friends']);
59 $single = !empty($options['single']) && empty($options['multiple']);
60 $exclude = defaults($options, 'exclude', false);
62 switch (defaults($options, 'networks', Protocol::PHANTOM)) {
64 $networks = [NETWORK_DFRN];
67 if (!empty($a->user['prvnets'])) {
68 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
70 $networks = [NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA];
74 if (!empty($a->user['prvnets'])) {
75 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
77 $networks = [NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS];
80 default: /// @TODO Maybe log this call?
84 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
86 Addon::callHooks('contact_select_options', $x);
92 if (!empty($x['mutual'])) {
93 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
96 if (!empty($x['exclude'])) {
97 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
100 if (!empty($x['networks'])) {
101 /// @TODO rewrite to foreach()
102 array_walk($x['networks'], function (&$value) {
103 $value = "'" . dbesc($value) . "'";
105 $str_nets = implode(',', $x['networks']);
106 $sql_extra .= " AND `network` IN ( $str_nets ) ";
109 $tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : '');
111 if (!empty($x['single'])) {
112 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
114 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
117 $stmt = dba::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
118 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
120 ORDER BY `name` ASC ", intval(local_user())
123 $contacts = dba::inArray($stmt);
125 $arr = ['contact' => $contacts, 'entry' => $o];
127 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
128 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
130 if (DBM::is_result($contacts)) {
131 foreach ($contacts as $contact) {
132 if (in_array($contact['id'], $preselected)) {
133 $selected = ' selected="selected" ';
138 $trimmed = mb_substr($contact['name'], 0, 20);
140 $o .= "<option value=\"{$contact['id']}\" $selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
144 $o .= '</select>' . PHP_EOL;
146 Addon::callHooks($a->module . '_post_' . $selname, $o);
152 * Returns a select input tag with all the contact of the local user
154 * @param string $selname Name attribute of the select input tag
155 * @param string $selclass Class attribute of the select input tag
156 * @param array $preselected Contact IDs that should be already selected
157 * @param int $size Length of the select box
158 * @param int $tabindex Select input tag tabindex attribute
161 public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
167 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
168 // to one recipient. By default our selector allows multiple selects amongst all contacts.
169 $sql_extra = sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
170 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", NETWORK_DFRN, NETWORK_DIASPORA);
172 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
174 $hidepreselected = '';
176 $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
177 $hidepreselected = ' style="display: none;"';
180 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
182 $stmt = dba::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
183 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
185 ORDER BY `name` ASC ", intval(local_user())
188 $contacts = dba::inArray($stmt);
190 $arr = ['contact' => $contacts, 'entry' => $o];
192 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
193 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
197 if (DBM::is_result($contacts)) {
198 foreach ($contacts as $contact) {
199 if (in_array($contact['id'], $preselected)) {
200 $selected = ' selected="selected"';
205 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
207 $receiverlist[] = $trimmed;
209 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
213 $o .= '</select>' . PHP_EOL;
216 $o .= implode(', ', $receiverlist);
219 Addon::callHooks($a->module . '_post_' . $selname, $o);
224 private static function fixACL(&$item) {
225 $item = intval(str_replace(['<', '>'], ['', ''], $item));
229 * Return the default permission of the provided user array
232 * @return array Hash of contact id lists
234 public static function getDefaultUserPermissions(array $user = null)
238 $acl_regex = '/<([0-9]+)>/i';
240 preg_match_all($acl_regex, defaults($user, 'allow_cid', ''), $matches);
241 $allow_cid = $matches[1];
242 preg_match_all($acl_regex, defaults($user, 'allow_gid', ''), $matches);
243 $allow_gid = $matches[1];
244 preg_match_all($acl_regex, defaults($user, 'deny_cid', ''), $matches);
245 $deny_cid = $matches[1];
246 preg_match_all($acl_regex, defaults($user, 'deny_gid', ''), $matches);
247 $deny_gid = $matches[1];
249 // Reformats the ACL data so that it is accepted by the JS frontend
250 array_walk($allow_cid, 'self::fixACL');
251 array_walk($allow_gid, 'self::fixACL');
252 array_walk($deny_cid, 'self::fixACL');
253 array_walk($deny_gid, 'self::fixACL');
255 Contact::pruneUnavailable($allow_cid);
258 'allow_cid' => $allow_cid,
259 'allow_gid' => $allow_gid,
260 'deny_cid' => $deny_cid,
261 'deny_gid' => $deny_gid,
266 * Return the full jot ACL selector HTML
269 * @param bool $show_jotnets
272 public static function getFullSelectorHTML(array $user = null, $show_jotnets = false)
274 $perms = self::getDefaultUserPermissions($user);
278 $imap_disabled = !function_exists('imap_open') || Config::get('system', 'imap_disabled');
280 $mail_enabled = false;
281 $pubmail_enabled = false;
283 if (!$imap_disabled) {
284 $mailacct = dba::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
285 if (DBM::is_result($mailacct)) {
286 $mail_enabled = true;
287 $pubmail_enabled = !empty($mailacct['pubmail']);
291 if (empty($user['hidewall'])) {
293 $selected = $pubmail_enabled ? ' checked="checked"' : '';
294 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . L10n::t("Post to Email") . '</div>';
297 Addon::callHooks('jot_networks', $jotnets);
299 $jotnets .= L10n::t('Connectors disabled, since "%s" is enabled.',
300 L10n::t('Hide your profile details from unknown viewers?'));
304 $tpl = get_markup_template('acl_selector.tpl');
305 $o = replace_macros($tpl, [
306 '$showall' => L10n::t('Visible to everybody'),
307 '$show' => L10n::t('show'),
308 '$hide' => L10n::t('don\'t show'),
309 '$allowcid' => json_encode($perms['allow_cid']),
310 '$allowgid' => json_encode($perms['allow_gid']),
311 '$denycid' => json_encode($perms['deny_cid']),
312 '$denygid' => json_encode($perms['deny_gid']),
313 '$networks' => $show_jotnets,
314 '$emailcc' => L10n::t('CC: email addresses'),
315 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
316 '$jotnets' => $jotnets,
317 '$aclModalTitle' => L10n::t('Permissions'),
318 '$aclModalDismiss' => L10n::t('Close'),
320 'aclautomention' => Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'
328 * Searching for global contacts for autocompletion
330 * @brief Searching for global contacts for autocompletion
331 * @param string $search Name or part of a name or nick
332 * @param string $mode Search mode (e.g. "community")
333 * @return array with the search results
335 public static function contactAutocomplete($search, $mode)
337 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
341 // don't search if search term has less than 2 characters
342 if (!$search || mb_strlen($search) < 2) {
346 if (substr($search, 0, 1) === '@') {
347 $search = substr($search, 1);
350 // check if searching in the local global contact table is enabled
351 if (Config::get('system', 'poco_local_search')) {
352 $return = GContact::searchByName($search, $mode);
355 $p = $a->pager['page'] != 1 ? '&p=' . $a->pager['page'] : '';
357 $response = Network::curl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
358 if ($response['success']) {
359 $lsearch = json_decode($response['body'], true);
360 if (!empty($lsearch['results'])) {
361 $return = $lsearch['results'];
366 return defaults($return, []);