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\Database\DBM;
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 = [NETWORK_DFRN];
53 if (!empty($a->user['prvnets'])) {
54 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
56 $networks = [NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA];
60 if (!empty($a->user['prvnets'])) {
61 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
63 $networks = [NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS];
66 default: /// @TODO Maybe log this call?
70 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
72 Addon::callHooks('contact_select_options', $x);
78 if (!empty($x['mutual'])) {
79 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_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 = "'" . dbesc($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 `notify` != ''
106 ORDER BY `name` ASC ", intval(local_user())
109 $contacts = DBA::inArray($stmt);
111 $arr = ['contact' => $contacts, 'entry' => $o];
113 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
114 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
116 if (DBM::is_result($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 Addon::callHooks($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
147 public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
153 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
154 // to one recipient. By default our selector allows multiple selects amongst all contacts.
155 $sql_extra = sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
156 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", NETWORK_DFRN, NETWORK_DIASPORA);
158 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
160 $hidepreselected = '';
162 $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
163 $hidepreselected = ' style="display: none;"';
166 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
168 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
169 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
171 ORDER BY `name` ASC ", intval(local_user())
174 $contacts = DBA::inArray($stmt);
176 $arr = ['contact' => $contacts, 'entry' => $o];
178 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
179 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
183 if (DBM::is_result($contacts)) {
184 foreach ($contacts as $contact) {
185 if (in_array($contact['id'], $preselected)) {
186 $selected = ' selected="selected"';
191 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
193 $receiverlist[] = $trimmed;
195 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
199 $o .= '</select>' . PHP_EOL;
202 $o .= implode(', ', $receiverlist);
205 Addon::callHooks($a->module . '_post_' . $selname, $o);
210 private static function fixACL(&$item)
212 $item = intval(str_replace(['<', '>'], ['', ''], $item));
216 * Return the default permission of the provided user array
219 * @return array Hash of contact id lists
221 public static function getDefaultUserPermissions(array $user = null)
225 $acl_regex = '/<([0-9]+)>/i';
227 preg_match_all($acl_regex, defaults($user, 'allow_cid', ''), $matches);
228 $allow_cid = $matches[1];
229 preg_match_all($acl_regex, defaults($user, 'allow_gid', ''), $matches);
230 $allow_gid = $matches[1];
231 preg_match_all($acl_regex, defaults($user, 'deny_cid', ''), $matches);
232 $deny_cid = $matches[1];
233 preg_match_all($acl_regex, defaults($user, 'deny_gid', ''), $matches);
234 $deny_gid = $matches[1];
236 // Reformats the ACL data so that it is accepted by the JS frontend
237 array_walk($allow_cid, 'self::fixACL');
238 array_walk($allow_gid, 'self::fixACL');
239 array_walk($deny_cid, 'self::fixACL');
240 array_walk($deny_gid, 'self::fixACL');
242 Contact::pruneUnavailable($allow_cid);
245 'allow_cid' => $allow_cid,
246 'allow_gid' => $allow_gid,
247 'deny_cid' => $deny_cid,
248 'deny_gid' => $deny_gid,
253 * Return the full jot ACL selector HTML
256 * @param bool $show_jotnets
259 public static function getFullSelectorHTML(array $user = null, $show_jotnets = false)
262 if (empty($user['uid'])) {
266 $perms = self::getDefaultUserPermissions($user);
270 $imap_disabled = !function_exists('imap_open') || Config::get('system', 'imap_disabled');
272 $mail_enabled = false;
273 $pubmail_enabled = false;
275 if (!$imap_disabled) {
276 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
277 if (DBM::is_result($mailacct)) {
278 $mail_enabled = true;
279 $pubmail_enabled = !empty($mailacct['pubmail']);
283 if (empty($user['hidewall'])) {
285 $selected = $pubmail_enabled ? ' checked="checked"' : '';
286 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . L10n::t("Post to Email") . '</div>';
289 Addon::callHooks('jot_networks', $jotnets);
291 $jotnets .= L10n::t('Connectors disabled, since "%s" is enabled.',
292 L10n::t('Hide your profile details from unknown viewers?'));
296 $tpl = get_markup_template('acl_selector.tpl');
297 $o = replace_macros($tpl, [
298 '$showall' => L10n::t('Visible to everybody'),
299 '$show' => L10n::t('show'),
300 '$hide' => L10n::t('don\'t show'),
301 '$allowcid' => json_encode($perms['allow_cid']),
302 '$allowgid' => json_encode($perms['allow_gid']),
303 '$denycid' => json_encode($perms['deny_cid']),
304 '$denygid' => json_encode($perms['deny_gid']),
305 '$networks' => $show_jotnets,
306 '$emailcc' => L10n::t('CC: email addresses'),
307 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
308 '$jotnets' => $jotnets,
309 '$aclModalTitle' => L10n::t('Permissions'),
310 '$aclModalDismiss' => L10n::t('Close'),
312 'aclautomention' => Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'
320 * Searching for global contacts for autocompletion
322 * @brief Searching for global contacts for autocompletion
323 * @param string $search Name or part of a name or nick
324 * @param string $mode Search mode (e.g. "community")
325 * @return array with the search results
327 public static function contactAutocomplete($search, $mode)
329 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
333 // don't search if search term has less than 2 characters
334 if (!$search || mb_strlen($search) < 2) {
338 if (substr($search, 0, 1) === '@') {
339 $search = substr($search, 1);
342 // check if searching in the local global contact table is enabled
343 if (Config::get('system', 'poco_local_search')) {
344 $return = GContact::searchByName($search, $mode);
347 $p = $a->pager['page'] != 1 ? '&p=' . $a->pager['page'] : '';
349 $response = Network::curl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
350 if ($response['success']) {
351 $lsearch = json_decode($response['body'], true);
352 if (!empty($lsearch['results'])) {
353 $return = $lsearch['results'];
358 return defaults($return, []);