4 * @file src/Core/Acl.php
7 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Content\Feature;
11 use Friendica\Core\Protocol;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\GContact;
15 use Friendica\Util\Network;
18 * Handle ACL management and display
20 * @author Hypolite Petovan <mrpetovan@gmail.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
37 public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
43 $size = defaults($options, 'size', 4);
44 $mutual = !empty($options['mutual_friends']);
45 $single = !empty($options['single']) && empty($options['multiple']);
46 $exclude = defaults($options, 'exclude', false);
48 switch (defaults($options, 'networks', Protocol::PHANTOM)) {
50 $networks = [Protocol::DFRN];
54 $networks = [Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
58 if (!empty($a->user['prvnets'])) {
59 $networks = [Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
61 $networks = [Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA, Protocol::OSTATUS];
65 default: /// @TODO Maybe log this call?
69 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
71 Addon::callHooks('contact_select_options', $x);
77 if (!empty($x['mutual'])) {
78 $sql_extra .= sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
81 if (!empty($x['exclude'])) {
82 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
85 if (!empty($x['networks'])) {
86 /// @TODO rewrite to foreach()
87 array_walk($x['networks'], function (&$value) {
88 $value = "'" . DBA::escape($value) . "'";
90 $str_nets = implode(',', $x['networks']);
91 $sql_extra .= " AND `network` IN ( $str_nets ) ";
94 $tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : '');
96 if (!empty($x['single'])) {
97 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
99 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
102 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
103 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
105 ORDER BY `name` ASC ", intval(local_user())
108 $contacts = DBA::toArray($stmt);
110 $arr = ['contact' => $contacts, 'entry' => $o];
112 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
113 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
115 if (DBA::isResult($contacts)) {
116 foreach ($contacts as $contact) {
117 if (in_array($contact['id'], $preselected)) {
118 $selected = ' selected="selected" ';
123 $trimmed = mb_substr($contact['name'], 0, 20);
125 $o .= "<option value=\"{$contact['id']}\" $selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
129 $o .= '</select>' . PHP_EOL;
131 Addon::callHooks($a->module . '_post_' . $selname, $o);
137 * Returns a select input tag with all the contact of the local user
139 * @param string $selname Name attribute of the select input tag
140 * @param string $selclass Class attribute of the select input tag
141 * @param array $preselected Contact IDs that should be already selected
142 * @param int $size Length of the select box
143 * @param int $tabindex Select input tag tabindex attribute
146 public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
152 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
153 // to one recipient. By default our selector allows multiple selects amongst all contacts.
154 $sql_extra = sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
155 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", Protocol::DFRN, Protocol::DIASPORA);
157 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
159 $hidepreselected = '';
161 $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
162 $hidepreselected = ' style="display: none;"';
165 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
167 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
168 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
170 ORDER BY `name` ASC ", intval(local_user())
173 $contacts = DBA::toArray($stmt);
175 $arr = ['contact' => $contacts, 'entry' => $o];
177 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
178 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
182 if (DBA::isResult($contacts)) {
183 foreach ($contacts as $contact) {
184 if (in_array($contact['id'], $preselected)) {
185 $selected = ' selected="selected"';
190 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
192 $receiverlist[] = $trimmed;
194 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
198 $o .= '</select>' . PHP_EOL;
201 $o .= implode(', ', $receiverlist);
204 Addon::callHooks($a->module . '_post_' . $selname, $o);
209 private static function fixACL(&$item)
211 $item = intval(str_replace(['<', '>'], ['', ''], $item));
215 * Return the default permission of the provided user array
218 * @return array Hash of contact id lists
220 public static function getDefaultUserPermissions(array $user = null)
224 $acl_regex = '/<([0-9]+)>/i';
226 preg_match_all($acl_regex, defaults($user, 'allow_cid', ''), $matches);
227 $allow_cid = $matches[1];
228 preg_match_all($acl_regex, defaults($user, 'allow_gid', ''), $matches);
229 $allow_gid = $matches[1];
230 preg_match_all($acl_regex, defaults($user, 'deny_cid', ''), $matches);
231 $deny_cid = $matches[1];
232 preg_match_all($acl_regex, defaults($user, 'deny_gid', ''), $matches);
233 $deny_gid = $matches[1];
235 // Reformats the ACL data so that it is accepted by the JS frontend
236 array_walk($allow_cid, 'self::fixACL');
237 array_walk($allow_gid, 'self::fixACL');
238 array_walk($deny_cid, 'self::fixACL');
239 array_walk($deny_gid, 'self::fixACL');
241 Contact::pruneUnavailable($allow_cid);
244 'allow_cid' => $allow_cid,
245 'allow_gid' => $allow_gid,
246 'deny_cid' => $deny_cid,
247 'deny_gid' => $deny_gid,
252 * Return the full jot ACL selector HTML
254 * @param array $user User array
255 * @param array $default_permissions Static defaults permission array: ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']
256 * @param bool $show_jotnets
259 public static function getFullSelectorHTML(array $user, $show_jotnets = false, array $default_permissions = [])
261 // Defaults user permissions
262 if (empty($default_permissions)) {
263 $default_permissions = self::getDefaultUserPermissions($user);
268 $imap_disabled = !function_exists('imap_open') || Config::get('system', 'imap_disabled');
270 $mail_enabled = false;
271 $pubmail_enabled = false;
273 if (!$imap_disabled) {
274 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
275 if (DBA::isResult($mailacct)) {
276 $mail_enabled = true;
277 $pubmail_enabled = !empty($mailacct['pubmail']);
281 if (empty($default_permissions['hidewall'])) {
283 $selected = $pubmail_enabled ? ' checked="checked"' : '';
284 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . L10n::t("Post to Email") . '</div>';
287 Addon::callHooks('jot_networks', $jotnets);
289 $jotnets .= L10n::t('Connectors disabled, since "%s" is enabled.',
290 L10n::t('Hide your profile details from unknown viewers?'));
294 $tpl = get_markup_template('acl_selector.tpl');
295 $o = replace_macros($tpl, [
296 '$showall' => L10n::t('Visible to everybody'),
297 '$show' => L10n::t('show'),
298 '$hide' => L10n::t('don\'t show'),
299 '$allowcid' => json_encode(defaults($default_permissions, 'allow_cid', '')),
300 '$allowgid' => json_encode(defaults($default_permissions, 'allow_gid', '')),
301 '$denycid' => json_encode(defaults($default_permissions, 'deny_cid', '')),
302 '$denygid' => json_encode(defaults($default_permissions, 'deny_gid', '')),
303 '$networks' => $show_jotnets,
304 '$emailcc' => L10n::t('CC: email addresses'),
305 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
306 '$jotnets' => $jotnets,
307 '$aclModalTitle' => L10n::t('Permissions'),
308 '$aclModalDismiss' => L10n::t('Close'),
310 'aclautomention' => Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'
318 * Searching for global contacts for autocompletion
320 * @brief Searching for global contacts for autocompletion
321 * @param string $search Name or part of a name or nick
322 * @param string $mode Search mode (e.g. "community")
323 * @return array with the search results
325 public static function contactAutocomplete($search, $mode)
327 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
331 // don't search if search term has less than 2 characters
332 if (!$search || mb_strlen($search) < 2) {
336 if (substr($search, 0, 1) === '@') {
337 $search = substr($search, 1);
340 // check if searching in the local global contact table is enabled
341 if (Config::get('system', 'poco_local_search')) {
342 $return = GContact::searchByName($search, $mode);
345 $p = $a->pager['page'] != 1 ? '&p=' . $a->pager['page'] : '';
347 $response = Network::curl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
348 if ($response['success']) {
349 $lsearch = json_decode($response['body'], true);
350 if (!empty($lsearch['results'])) {
351 $return = $lsearch['results'];
356 return defaults($return, []);