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\Core\Renderer;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Contact;
15 use Friendica\Model\GContact;
16 use Friendica\Util\Network;
19 * Handle ACL management and display
21 * @author Hypolite Petovan <hypolite@mrpetovan.com>
23 class ACL extends BaseObject
26 * Returns a select input tag with all the contact of the local user
28 * @param string $selname Name attribute of the select input tag
29 * @param string $selclass Class attribute of the select input tag
30 * @param array $options Available options:
31 * - size: length of the select box
32 * - mutual_friends: Only used for the hook
33 * - single: Only used for the hook
34 * - exclude: Only used for the hook
35 * @param array $preselected Contact ID that should be already selected
38 public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
44 $size = defaults($options, 'size', 4);
45 $mutual = !empty($options['mutual_friends']);
46 $single = !empty($options['single']) && empty($options['multiple']);
47 $exclude = defaults($options, 'exclude', false);
49 switch (defaults($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 Addon::callHooks('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 `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 Addon::callHooks($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 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::FRIEND));
156 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", Protocol::DFRN, Protocol::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::toArray($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 (DBA::isResult($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
255 * @param array $user User array
256 * @param array $default_permissions Static defaults permission array: ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']
257 * @param bool $show_jotnets
260 public static function getFullSelectorHTML(array $user, $show_jotnets = false, array $default_permissions = [])
262 // Defaults user permissions
263 if (empty($default_permissions)) {
264 $default_permissions = self::getDefaultUserPermissions($user);
269 $imap_disabled = !function_exists('imap_open') || Config::get('system', 'imap_disabled');
271 $mail_enabled = false;
272 $pubmail_enabled = false;
274 if (!$imap_disabled) {
275 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
276 if (DBA::isResult($mailacct)) {
277 $mail_enabled = true;
278 $pubmail_enabled = !empty($mailacct['pubmail']);
282 if (empty($default_permissions['hidewall'])) {
284 $selected = $pubmail_enabled ? ' checked="checked"' : '';
285 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . L10n::t("Post to Email") . '</div>';
288 Addon::callHooks('jot_networks', $jotnets);
290 $jotnets .= L10n::t('Connectors disabled, since "%s" is enabled.',
291 L10n::t('Hide your profile details from unknown viewers?'));
295 $tpl = Renderer::getMarkupTemplate('acl_selector.tpl');
296 $o = Renderer::replaceMacros($tpl, [
297 '$showall' => L10n::t('Visible to everybody'),
298 '$show' => L10n::t('show'),
299 '$hide' => L10n::t('don\'t show'),
300 '$allowcid' => json_encode(defaults($default_permissions, 'allow_cid', '')),
301 '$allowgid' => json_encode(defaults($default_permissions, 'allow_gid', '')),
302 '$denycid' => json_encode(defaults($default_permissions, 'deny_cid', '')),
303 '$denygid' => json_encode(defaults($default_permissions, 'deny_gid', '')),
304 '$networks' => $show_jotnets,
305 '$emailcc' => L10n::t('CC: email addresses'),
306 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
307 '$jotnets' => $jotnets,
308 '$aclModalTitle' => L10n::t('Permissions'),
309 '$aclModalDismiss' => L10n::t('Close'),
311 'aclautomention' => Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'
319 * Searching for global contacts for autocompletion
321 * @brief Searching for global contacts for autocompletion
322 * @param string $search Name or part of a name or nick
323 * @param string $mode Search mode (e.g. "community")
324 * @return array with the search results
326 public static function contactAutocomplete($search, $mode)
328 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
332 // don't search if search term has less than 2 characters
333 if (!$search || mb_strlen($search) < 2) {
337 if (substr($search, 0, 1) === '@') {
338 $search = substr($search, 1);
341 // check if searching in the local global contact table is enabled
342 if (Config::get('system', 'poco_local_search')) {
343 $return = GContact::searchByName($search, $mode);
345 $p = defaults($_GET, 'page', 1) != 1 ? '&p=' . defaults($_GET, 'page', 1) : '';
347 $curlResult = Network::curl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
348 if ($curlResult->isSuccess()) {
349 $lsearch = json_decode($curlResult->getBody(), true);
350 if (!empty($lsearch['results'])) {
351 $return = $lsearch['results'];
356 return defaults($return, []);