]> git.mxchange.org Git - friendica.git/blob - src/Core/ACL.php
update PL translations THX waldis (#5590)
[friendica.git] / src / Core / ACL.php
1 <?php
2
3 /**
4  * @file src/Core/Acl.php
5  */
6
7 namespace Friendica\Core;
8
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;
15
16 /**
17  * Handle ACL management and display
18  *
19  * @author Hypolite Petovan <mrpetovan@gmail.com>
20  */
21 class ACL extends BaseObject
22 {
23         /**
24          * Returns a select input tag with all the contact of the local user
25          *
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
34          * @return string
35          */
36         public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
37         {
38                 $a = self::getApp();
39
40                 $networks = null;
41
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);
46
47                 switch (defaults($options, 'networks', Protocol::PHANTOM)) {
48                         case 'DFRN_ONLY':
49                                 $networks = [NETWORK_DFRN];
50                                 break;
51                         case 'PRIVATE':
52                                 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
53                                 break;
54                         case 'TWO_WAY':
55                                 if (!empty($a->user['prvnets'])) {
56                                         $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
57                                 } else {
58                                         $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS];
59                                 }
60                                 break;
61                         default: /// @TODO Maybe log this call?
62                                 break;
63                 }
64
65                 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
66
67                 Addon::callHooks('contact_select_options', $x);
68
69                 $o = '';
70
71                 $sql_extra = '';
72
73                 if (!empty($x['mutual'])) {
74                         $sql_extra .= sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
75                 }
76
77                 if (!empty($x['exclude'])) {
78                         $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
79                 }
80
81                 if (!empty($x['networks'])) {
82                         /// @TODO rewrite to foreach()
83                         array_walk($x['networks'], function (&$value) {
84                                 $value = "'" . DBA::escape($value) . "'";
85                         });
86                         $str_nets = implode(',', $x['networks']);
87                         $sql_extra .= " AND `network` IN ( $str_nets ) ";
88                 }
89
90                 $tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : '');
91
92                 if (!empty($x['single'])) {
93                         $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
94                 } else {
95                         $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
96                 }
97
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` != ''
100                         $sql_extra
101                         ORDER BY `name` ASC ", intval(local_user())
102                 );
103
104                 $contacts = DBA::toArray($stmt);
105
106                 $arr = ['contact' => $contacts, 'entry' => $o];
107
108                 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
109                 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
110
111                 if (DBA::isResult($contacts)) {
112                         foreach ($contacts as $contact) {
113                                 if (in_array($contact['id'], $preselected)) {
114                                         $selected = ' selected="selected" ';
115                                 } else {
116                                         $selected = '';
117                                 }
118
119                                 $trimmed = mb_substr($contact['name'], 0, 20);
120
121                                 $o .= "<option value=\"{$contact['id']}\" $selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
122                         }
123                 }
124
125                 $o .= '</select>' . PHP_EOL;
126
127                 Addon::callHooks($a->module . '_post_' . $selname, $o);
128
129                 return $o;
130         }
131
132         /**
133          * Returns a select input tag with all the contact of the local user
134          *
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
140          * @return string
141          */
142         public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
143         {
144                 $a = self::getApp();
145
146                 $o = '';
147
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);
152
153                 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
154
155                 $hidepreselected = '';
156                 if ($preselected) {
157                         $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
158                         $hidepreselected = ' style="display: none;"';
159                 }
160
161                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
162
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` != ''
165                         $sql_extra
166                         ORDER BY `name` ASC ", intval(local_user())
167                 );
168
169                 $contacts = DBA::toArray($stmt);
170
171                 $arr = ['contact' => $contacts, 'entry' => $o];
172
173                 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
174                 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
175
176                 $receiverlist = [];
177
178                 if (DBA::isResult($contacts)) {
179                         foreach ($contacts as $contact) {
180                                 if (in_array($contact['id'], $preselected)) {
181                                         $selected = ' selected="selected"';
182                                 } else {
183                                         $selected = '';
184                                 }
185
186                                 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
187
188                                 $receiverlist[] = $trimmed;
189
190                                 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
191                         }
192                 }
193
194                 $o .= '</select>' . PHP_EOL;
195
196                 if ($preselected) {
197                         $o .= implode(', ', $receiverlist);
198                 }
199
200                 Addon::callHooks($a->module . '_post_' . $selname, $o);
201
202                 return $o;
203         }
204
205         private static function fixACL(&$item)
206         {
207                 $item = intval(str_replace(['<', '>'], ['', ''], $item));
208         }
209
210         /**
211          * Return the default permission of the provided user array
212          *
213          * @param array $user
214          * @return array Hash of contact id lists
215          */
216         public static function getDefaultUserPermissions(array $user = null)
217         {
218                 $matches = [];
219
220                 $acl_regex = '/<([0-9]+)>/i';
221
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];
230
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');
236
237                 Contact::pruneUnavailable($allow_cid);
238
239                 return [
240                         'allow_cid' => $allow_cid,
241                         'allow_gid' => $allow_gid,
242                         'deny_cid' => $deny_cid,
243                         'deny_gid' => $deny_gid,
244                 ];
245         }
246
247         /**
248          * Return the full jot ACL selector HTML
249          *
250          * @param array $user
251          * @param bool  $show_jotnets
252          * @return string
253          */
254         public static function getFullSelectorHTML(array $user = null, $show_jotnets = false)
255         {
256
257                 if (empty($user['uid'])) {
258                         return '';
259                 }
260
261                 $perms = self::getDefaultUserPermissions($user);
262
263                 $jotnets = '';
264                 if ($show_jotnets) {
265                         $imap_disabled = !function_exists('imap_open') || Config::get('system', 'imap_disabled');
266
267                         $mail_enabled = false;
268                         $pubmail_enabled = false;
269
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']);
275                                 }
276                         }
277
278                         if (empty($user['hidewall'])) {
279                                 if ($mail_enabled) {
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>';
282                                 }
283
284                                 Addon::callHooks('jot_networks', $jotnets);
285                         } else {
286                                 $jotnets .= L10n::t('Connectors disabled, since "%s" is enabled.',
287                                                 L10n::t('Hide your profile details from unknown viewers?'));
288                         }
289                 }
290
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'),
306                         '$features' => [
307                                 'aclautomention' => Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'
308                         ],
309                 ]);
310
311                 return $o;
312         }
313
314         /**
315          * Searching for global contacts for autocompletion
316          *
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
321          */
322         public static function contactAutocomplete($search, $mode)
323         {
324                 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
325                         return [];
326                 }
327
328                 // don't search if search term has less than 2 characters
329                 if (!$search || mb_strlen($search) < 2) {
330                         return [];
331                 }
332
333                 if (substr($search, 0, 1) === '@') {
334                         $search = substr($search, 1);
335                 }
336
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);
340                 } else {
341                         $a = self::getApp();
342                         $p = $a->pager['page'] != 1 ? '&p=' . $a->pager['page'] : '';
343
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'];
349                                 }
350                         }
351                 }
352
353                 return defaults($return, []);
354         }
355 }