]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact.php
Move contact conversation to its own module class
[friendica.git] / src / Module / Contact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\ContactSelector;
26 use Friendica\Content\Nav;
27 use Friendica\Content\Pager;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Content\Widget;
30 use Friendica\Core\Hook;
31 use Friendica\Core\Protocol;
32 use Friendica\Core\Renderer;
33 use Friendica\Core\Theme;
34 use Friendica\Core\Worker;
35 use Friendica\Database\DBA;
36 use Friendica\DI;
37 use Friendica\Model;
38 use Friendica\Model\User;
39 use Friendica\Module\Security\Login;
40 use Friendica\Network\HTTPException\BadRequestException;
41 use Friendica\Network\HTTPException\NotFoundException;
42 use Friendica\Util\DateTimeFormat;
43 use Friendica\Util\Strings;
44
45 /**
46  *  Manages and show Contacts and their content
47  */
48 class Contact extends BaseModule
49 {
50         const TAB_CONVERSATIONS = 1;
51         const TAB_POSTS = 2;
52         const TAB_PROFILE = 3;
53         const TAB_CONTACTS = 4;
54         const TAB_ADVANCED = 5;
55         const TAB_MEDIA = 6;
56
57         private static function batchActions()
58         {
59                 if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
60                         return;
61                 }
62
63                 $redirectUrl = $_POST['redirect_url'] ?? 'contact';
64
65                 self::checkFormSecurityTokenRedirectOnError($redirectUrl, 'contact_batch_actions');
66
67                 $orig_records = Model\Contact::selectToArray(['id', 'uid'], ['id' => $_POST['contact_batch'], 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
68
69                 $count_actions = 0;
70                 foreach ($orig_records as $orig_record) {
71                         $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user());
72                         if (empty($cdata) || public_contact() === $cdata['public']) {
73                                 // No action available on your own contact
74                                 continue;
75                         }
76
77                         if (!empty($_POST['contacts_batch_update']) && $cdata['user']) {
78                                 self::updateContactFromPoll($cdata['user']);
79                                 $count_actions++;
80                         }
81
82                         if (!empty($_POST['contacts_batch_block'])) {
83                                 self::toggleBlockContact($cdata['public'], local_user());
84                                 $count_actions++;
85                         }
86
87                         if (!empty($_POST['contacts_batch_ignore'])) {
88                                 self::toggleIgnoreContact($cdata['public']);
89                                 $count_actions++;
90                         }
91                 }
92                 if ($count_actions > 0) {
93                         info(DI::l10n()->tt('%d contact edited.', '%d contacts edited.', $count_actions));
94                 }
95
96                 DI::baseUrl()->redirect($redirectUrl);
97         }
98
99         public function post()
100         {
101                 if (!local_user()) {
102                         return;
103                 }
104
105                 // @TODO: Replace with parameter from router
106                 if (DI::args()->getArgv()[1] === 'batch') {
107                         self::batchActions();
108                         return;
109                 }
110
111                 // @TODO: Replace with parameter from router
112                 $contact_id = intval(DI::args()->getArgv()[1]);
113                 if (!$contact_id) {
114                         return;
115                 }
116
117                 if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
118                         notice(DI::l10n()->t('Could not access contact record.'));
119                         DI::baseUrl()->redirect('contact');
120                         return; // NOTREACHED
121                 }
122
123                 Hook::callAll('contact_edit_post', $_POST);
124
125                 $hidden = !empty($_POST['hidden']);
126
127                 $notify = !empty($_POST['notify']);
128
129                 $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
130
131                 $remote_self = $_POST['remote_self'] ?? false;
132
133                 $ffi_keyword_denylist = Strings::escapeHtml(trim($_POST['ffi_keyword_denylist'] ?? ''));
134
135                 $priority = intval($_POST['poll'] ?? 0);
136                 if ($priority > 5 || $priority < 0) {
137                         $priority = 0;
138                 }
139
140                 $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
141
142                 $r = Model\Contact::update([
143                         'priority'   => $priority,
144                         'info'       => $info,
145                         'hidden'     => $hidden,
146                         'notify_new_posts' => $notify,
147                         'fetch_further_information' => $fetch_further_information,
148                         'remote_self' => $remote_self,
149                         'ffi_keyword_denylist'     => $ffi_keyword_denylist],
150                         ['id' => $contact_id, 'uid' => local_user()]
151                 );
152
153                 if (!DBA::isResult($r)) {
154                         notice(DI::l10n()->t('Failed to update contact record.'));
155                 }
156                 return;
157         }
158
159         /* contact actions */
160
161         /**
162          * @param int $contact_id Id of contact with uid != 0
163          * @throws NotFoundException
164          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
165          * @throws \ImagickException
166          */
167         private static function updateContactFromPoll(int $contact_id)
168         {
169                 $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
170                 if (!DBA::isResult($contact)) {
171                         return;
172                 }
173
174                 if ($contact['network'] == Protocol::OSTATUS) {
175                         $result = Model\Contact::createFromProbeForUser($contact['uid'], $contact['url'], $contact['network']);
176
177                         if ($result['success']) {
178                                 Model\Contact::update(['subhub' => 1], ['id' => $contact_id]);
179                         }
180
181                         // pull feed and consume it, which should subscribe to the hub.
182                         Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
183                 } else {
184                         Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id);
185                 }
186         }
187
188         /**
189          * @param int $contact_id Id of the contact with uid != 0
190          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
191          * @throws \ImagickException
192          */
193         private static function updateContactFromProbe(int $contact_id)
194         {
195                 $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
196                 if (!DBA::isResult($contact)) {
197                         return;
198                 }
199
200                 // Update the entry in the contact table
201                 Model\Contact::updateFromProbe($contact_id);
202         }
203
204         /**
205          * Toggles the blocked status of a contact identified by id.
206          *
207          * @param int $contact_id Id of the contact with uid = 0
208          * @param int $owner_id   Id of the user we want to block the contact for
209          * @throws \Exception
210          */
211         private static function toggleBlockContact(int $contact_id, int $owner_id)
212         {
213                 $blocked = !Model\Contact\User::isBlocked($contact_id, $owner_id);
214                 Model\Contact\User::setBlocked($contact_id, $owner_id, $blocked);
215         }
216
217         /**
218          * Toggles the ignored status of a contact identified by id.
219          *
220          * @param int $contact_id Id of the contact with uid = 0
221          * @throws \Exception
222          */
223         private static function toggleIgnoreContact(int $contact_id)
224         {
225                 $ignored = !Model\Contact\User::isIgnored($contact_id, local_user());
226                 Model\Contact\User::setIgnored($contact_id, local_user(), $ignored);
227         }
228
229         public function content($update = 0): string
230         {
231                 if (!local_user()) {
232                         return Login::form($_SERVER['REQUEST_URI']);
233                 }
234
235                 $search = trim($_GET['search'] ?? '');
236                 $nets   = trim($_GET['nets']   ?? '');
237                 $rel    = trim($_GET['rel']    ?? '');
238                 $group  = trim($_GET['group']  ?? '');
239
240                 $accounttype = $_GET['accounttype'] ?? '';
241                 $accounttypeid = User::getAccountTypeByString($accounttype);
242
243                 $page = DI::page();
244
245                 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
246                 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
247                 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
248                 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
249
250                 $contact = null;
251                 // @TODO: Replace with parameter from router
252                 if (DI::args()->getArgc() == 2 && intval(DI::args()->getArgv()[1])) {
253                         $contact_id = intval(DI::args()->getArgv()[1]);
254
255                         // Ensure to use the user contact when the public contact was provided
256                         $data = Model\Contact::getPublicAndUserContactID($contact_id, local_user());
257                         if (!empty($data['user']) && ($contact_id == $data['public'])) {
258                                 $contact_id = $data['user'];
259                         }
260
261                         if (!empty($data)) {
262                                 $contact = DBA::selectFirst('contact', [], [
263                                         'id'      => $contact_id,
264                                         'uid'     => [0, local_user()],
265                                         'deleted' => false
266                                 ]);
267
268                                 // Don't display contacts that are about to be deleted
269                                 if (DBA::isResult($contact) && !empty($contact['network']) && $contact['network'] == Protocol::PHANTOM) {
270                                         $contact = false;
271                                 }
272                         }
273                 }
274
275                 if (DBA::isResult($contact)) {
276                         if ($contact['self']) {
277                                 DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile');
278                         }
279
280                         $vcard_widget = Widget\VCard::getHTML($contact);
281
282                         $findpeople_widget = '';
283                         $follow_widget = '';
284                         $account_widget = '';
285                         $networks_widget = '';
286                         $rel_widget = '';
287
288                         if ($contact['uid'] != 0) {
289                                 $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
290                         } else {
291                                 $groups_widget = '';
292                         }
293                 } else {
294                         $vcard_widget = '';
295                         $findpeople_widget = Widget::findPeople();
296                         if (isset($_GET['add'])) {
297                                 $follow_widget = Widget::follow($_GET['add']);
298                         } else {
299                                 $follow_widget = Widget::follow();
300                         }
301
302                         $account_widget = Widget::accounttypes($_SERVER['REQUEST_URI'], $accounttype);
303                         $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
304                         $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
305                         $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group);
306                 }
307
308                 DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $account_widget . $groups_widget . $networks_widget . $rel_widget;
309
310                 $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
311                 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
312                         '$baseurl' => DI::baseUrl()->get(true),
313                 ]);
314
315                 $o = '';
316                 Nav::setSelected('contact');
317
318                 if (DI::args()->getArgc() == 3) {
319                         $contact_id = intval(DI::args()->getArgv()[1]);
320                         if (!$contact_id) {
321                                 throw new BadRequestException();
322                         }
323
324                         // @TODO: Replace with parameter from router
325                         $cmd = DI::args()->getArgv()[2];
326
327                         $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
328                         if (!DBA::isResult($orig_record)) {
329                                 throw new NotFoundException(DI::l10n()->t('Contact not found'));
330                         }
331
332                         self::checkFormSecurityTokenRedirectOnError('contact/' . $contact_id, 'contact_action', 't');
333
334                         $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user());
335                         if (empty($cdata)) {
336                                 throw new NotFoundException(DI::l10n()->t('Contact not found'));
337                         }
338
339                         if ($cmd === 'update' && $cdata['user']) {
340                                 self::updateContactFromPoll($cdata['user']);
341                                 DI::baseUrl()->redirect('contact/' . $contact_id);
342                                 // NOTREACHED
343                         }
344
345                         if ($cmd === 'updateprofile' && $cdata['user']) {
346                                 self::updateContactFromProbe($cdata['user']);
347                                 DI::baseUrl()->redirect('contact/' . $contact_id);
348                                 // NOTREACHED
349                         }
350
351                         if ($cmd === 'block') {
352                                 if (public_contact() === $cdata['public']) {
353                                         throw new BadRequestException(DI::l10n()->t('You can\'t block yourself'));
354                                 }
355
356                                 self::toggleBlockContact($cdata['public'], local_user());
357
358                                 $blocked = Model\Contact\User::isBlocked($contact_id, local_user());
359                                 info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')));
360
361                                 DI::baseUrl()->redirect('contact/' . $contact_id);
362                                 // NOTREACHED
363                         }
364
365                         if ($cmd === 'ignore') {
366                                 if (public_contact() === $cdata['public']) {
367                                         throw new BadRequestException(DI::l10n()->t('You can\'t ignore yourself'));
368                                 }
369
370                                 self::toggleIgnoreContact($cdata['public']);
371
372                                 $ignored = Model\Contact\User::isIgnored($cdata['public'], local_user());
373                                 info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')));
374
375                                 DI::baseUrl()->redirect('contact/' . $contact_id);
376                                 // NOTREACHED
377                         }
378                 }
379
380                 $_SESSION['return_path'] = DI::args()->getQueryString();
381
382                 if (!empty($contact)) {
383                         DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
384                                 '$baseurl' => DI::baseUrl()->get(true),
385                         ]);
386
387                         $contact['blocked']  = Model\Contact\User::isBlocked($contact['id'], local_user());
388                         $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
389
390                         $relation_text = '';
391                         switch ($contact['rel']) {
392                                 case Model\Contact::FRIEND:
393                                         $relation_text = DI::l10n()->t('You are mutual friends with %s');
394                                         break;
395
396                                 case Model\Contact::FOLLOWER;
397                                         $relation_text = DI::l10n()->t('You are sharing with %s');
398                                         break;
399
400                                 case Model\Contact::SHARING;
401                                         $relation_text = DI::l10n()->t('%s is sharing with you');
402                                         break;
403
404                                 default:
405                                         break;
406                         }
407
408                         if ($contact['uid'] == 0) {
409                                 $relation_text = '';
410                         }
411
412                         if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
413                                 $relation_text = '';
414                         }
415
416                         $relation_text = sprintf($relation_text, $contact['name']);
417
418                         $url = Model\Contact::magicLinkByContact($contact);
419                         if (strpos($url, 'redir/') === 0) {
420                                 $sparkle = ' class="sparkle" ';
421                         } else {
422                                 $sparkle = '';
423                         }
424
425                         $insecure = DI::l10n()->t('Private communications are not available for this contact.');
426
427                         $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? DI::l10n()->t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
428
429                         if ($contact['last-update'] > DBA::NULL_DATETIME) {
430                                 $last_update .= ' ' . ($contact['failed'] ? DI::l10n()->t('(Update was not successful)') : DI::l10n()->t('(Update was successful)'));
431                         }
432                         $lblsuggest = (($contact['network'] === Protocol::DFRN) ? DI::l10n()->t('Suggest friends') : '');
433
434                         $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
435
436                         $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol'], $contact['gsid']));
437
438                         // tabs
439                         $tab_str = self::getTabsHTML($contact, self::TAB_PROFILE);
440
441                         $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : '');
442
443                         $fetch_further_information = null;
444                         if ($contact['network'] == Protocol::FEED) {
445                                 $fetch_further_information = [
446                                         'fetch_further_information',
447                                         DI::l10n()->t('Fetch further information for feeds'),
448                                         $contact['fetch_further_information'],
449                                         DI::l10n()->t('Fetch information like preview pictures, title and teaser from the feed item. You can activate this if the feed doesn\'t contain much text. Keywords are taken from the meta header in the feed item and are posted as hash tags.'),
450                                         [
451                                                 '0' => DI::l10n()->t('Disabled'),
452                                                 '1' => DI::l10n()->t('Fetch information'),
453                                                 '3' => DI::l10n()->t('Fetch keywords'),
454                                                 '2' => DI::l10n()->t('Fetch information and keywords')
455                                         ]
456                                 ];
457                         }
458
459                         // Disable remote self for everything except feeds.
460                         // There is an issue when you repeat an item from maybe twitter and you got comments from friendica and twitter
461                         // Problem is, you couldn't reply to both networks.
462                         $allow_remote_self = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::FEED, Protocol::DFRN, Protocol::DIASPORA, Protocol::TWITTER])
463                                 && DI::config()->get('system', 'allow_users_remote_self');
464
465                         if ($contact['network'] == Protocol::FEED) {
466                                 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
467                                         Model\Contact::MIRROR_FORWARDED => DI::l10n()->t('Mirror as forwarded posting'),
468                                         Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting')];
469                         } elseif (in_array($contact['network'], [Protocol::ACTIVITYPUB])) {
470                                 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
471                                 Model\Contact::MIRROR_NATIVE_RESHARE => DI::l10n()->t('Native reshare')];
472                         } elseif (in_array($contact['network'], [Protocol::DFRN])) {
473                                 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
474                                 Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting'),
475                                 Model\Contact::MIRROR_NATIVE_RESHARE => DI::l10n()->t('Native reshare')];
476                         } else {
477                                 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
478                                         Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting')];
479                         }
480
481                         $poll_interval = null;
482                         if ((($contact['network'] == Protocol::FEED) && !DI::config()->get('system', 'adjust_poll_frequency')) || ($contact['network'] == Protocol::MAIL)) {
483                                 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
484                         }
485
486                         // Load contactact related actions like hide, suggest, delete and others
487                         $contact_actions = self::getContactActions($contact);
488
489                         if ($contact['uid'] != 0) {
490                                 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
491                                 $contact_settings_label = DI::l10n()->t('Contact Settings');
492                         } else {
493                                 $lbl_info1 = null;
494                                 $contact_settings_label = null;
495                         }
496
497                         $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
498                         $o .= Renderer::replaceMacros($tpl, [
499                                 '$header'         => DI::l10n()->t('Contact'),
500                                 '$tab_str'        => $tab_str,
501                                 '$submit'         => DI::l10n()->t('Submit'),
502                                 '$lbl_info1'      => $lbl_info1,
503                                 '$lbl_info2'      => DI::l10n()->t('Their personal note'),
504                                 '$reason'         => trim($contact['reason']),
505                                 '$infedit'        => DI::l10n()->t('Edit contact notes'),
506                                 '$common_link'    => 'contact/' . $contact['id'] . '/contacts/common',
507                                 '$relation_text'  => $relation_text,
508                                 '$visit'          => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
509                                 '$blockunblock'   => DI::l10n()->t('Block/Unblock contact'),
510                                 '$ignorecont'     => DI::l10n()->t('Ignore contact'),
511                                 '$lblrecent'      => DI::l10n()->t('View conversations'),
512                                 '$lblsuggest'     => $lblsuggest,
513                                 '$nettype'        => $nettype,
514                                 '$poll_interval'  => $poll_interval,
515                                 '$poll_enabled'   => $poll_enabled,
516                                 '$lastupdtext'    => DI::l10n()->t('Last update:'),
517                                 '$lost_contact'   => $lost_contact,
518                                 '$updpub'         => DI::l10n()->t('Update public posts'),
519                                 '$last_update'    => $last_update,
520                                 '$udnow'          => DI::l10n()->t('Update now'),
521                                 '$contact_id'     => $contact['id'],
522                                 '$block_text'     => ($contact['blocked'] ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
523                                 '$ignore_text'    => ($contact['readonly'] ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
524                                 '$insecure'       => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
525                                 '$info'           => $contact['info'],
526                                 '$cinfo'          => ['info', '', $contact['info'], ''],
527                                 '$blocked'        => ($contact['blocked'] ? DI::l10n()->t('Currently blocked') : ''),
528                                 '$ignored'        => ($contact['readonly'] ? DI::l10n()->t('Currently ignored') : ''),
529                                 '$archived'       => ($contact['archive'] ? DI::l10n()->t('Currently archived') : ''),
530                                 '$pending'        => ($contact['pending'] ? DI::l10n()->t('Awaiting connection acknowledge') : ''),
531                                 '$hidden'         => ['hidden', DI::l10n()->t('Hide this contact from others'), ($contact['hidden'] == 1), DI::l10n()->t('Replies/likes to your public posts <strong>may</strong> still be visible')],
532                                 '$notify'         => ['notify', DI::l10n()->t('Notification for new posts'), ($contact['notify_new_posts'] == 1), DI::l10n()->t('Send a notification of every new post of this contact')],
533                                 '$fetch_further_information' => $fetch_further_information,
534                                 '$ffi_keyword_denylist' => ['ffi_keyword_denylist', DI::l10n()->t('Keyword Deny List'), $contact['ffi_keyword_denylist'], DI::l10n()->t('Comma separated list of keywords that should not be converted to hashtags, when "Fetch information and keywords" is selected')],
535                                 '$photo'          => Model\Contact::getPhoto($contact),
536                                 '$name'           => $contact['name'],
537                                 '$sparkle'        => $sparkle,
538                                 '$url'            => $url,
539                                 '$profileurllabel'=> DI::l10n()->t('Profile URL'),
540                                 '$profileurl'     => $contact['url'],
541                                 '$account_type'   => Model\Contact::getAccountType($contact),
542                                 '$location'       => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['location']),
543                                 '$location_label' => DI::l10n()->t('Location:'),
544                                 '$xmpp'           => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['xmpp']),
545                                 '$xmpp_label'     => DI::l10n()->t('XMPP:'),
546                                 '$matrix'         => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['matrix']),
547                                 '$matrix_label'   => DI::l10n()->t('Matrix:'),
548                                 '$about'          => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['about'], BBCode::EXTERNAL),
549                                 '$about_label'    => DI::l10n()->t('About:'),
550                                 '$keywords'       => $contact['keywords'],
551                                 '$keywords_label' => DI::l10n()->t('Tags:'),
552                                 '$contact_action_button' => DI::l10n()->t('Actions'),
553                                 '$contact_actions'=> $contact_actions,
554                                 '$contact_status' => DI::l10n()->t('Status'),
555                                 '$contact_settings_label' => $contact_settings_label,
556                                 '$contact_profile_label' => DI::l10n()->t('Profile'),
557                                 '$allow_remote_self' => $allow_remote_self,
558                                 '$remote_self'       => ['remote_self',
559                                         DI::l10n()->t('Mirror postings from this contact'),
560                                         $contact['remote_self'],
561                                         DI::l10n()->t('Mark this contact as remote_self, this will cause friendica to repost new entries from this contact.'),
562                                         $remote_self_options
563                                 ],
564                         ]);
565
566                         $arr = ['contact' => $contact, 'output' => $o];
567
568                         Hook::callAll('contact_edit', $arr);
569
570                         return $arr['output'];
571                 }
572
573                 $sql_values = [local_user()];
574
575                 // @TODO: Replace with parameter from router
576                 $type = DI::args()->getArgv()[1] ?? '';
577
578                 switch ($type) {
579                         case 'blocked':
580                                 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
581                                 // This makes the query look for contact.uid = 0
582                                 array_unshift($sql_values, 0);
583                                 break;
584                         case 'hidden':
585                                 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
586                                 break;
587                         case 'ignored':
588                                 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
589                                 // This makes the query look for contact.uid = 0
590                                 array_unshift($sql_values, 0);
591                                 break;
592                         case 'archived':
593                                 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
594                                 break;
595                         case 'pending':
596                                 $sql_extra = " AND `pending` AND NOT `archive` AND NOT `failed` AND ((`rel` = ?)
597                                         OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
598                                 $sql_values[] = Model\Contact::SHARING;
599                                 break;
600                         default:
601                                 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
602                                 break;
603                 }
604
605                 if (isset($accounttypeid)) {
606                         $sql_extra .= " AND `contact-type` = ?";
607                         $sql_values[] = $accounttypeid;
608                 }
609
610                 $searching = false;
611                 $search_hdr = null;
612                 if ($search) {
613                         $searching = true;
614                         $search_hdr = $search;
615                         $search_txt = preg_quote($search);
616                         $sql_extra .= " AND (name REGEXP ? OR url REGEXP ? OR nick REGEXP ?)";
617                         $sql_values[] = $search_txt;
618                         $sql_values[] = $search_txt;
619                         $sql_values[] = $search_txt;
620                 }
621
622                 if ($nets) {
623                         $sql_extra .= " AND network = ? ";
624                         $sql_values[] = $nets;
625                 }
626
627                 switch ($rel) {
628                         case 'followers':
629                                 $sql_extra .= " AND `rel` IN (?, ?)";
630                                 $sql_values[] = Model\Contact::FOLLOWER;
631                                 $sql_values[] = Model\Contact::FRIEND;
632                                 break;
633                         case 'following':
634                                 $sql_extra .= " AND `rel` IN (?, ?)";
635                                 $sql_values[] = Model\Contact::SHARING;
636                                 $sql_values[] = Model\Contact::FRIEND;
637                                 break;
638                         case 'mutuals':
639                                 $sql_extra .= " AND `rel` = ?";
640                                 $sql_values[] = Model\Contact::FRIEND;
641                                 break;
642                 }
643
644                 if ($group) {
645                         $sql_extra .= " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
646                         $sql_values[] = $group;
647                 }
648
649                 $networks = Widget::unavailableNetworks();
650                 $sql_extra .= " AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")";
651                 $sql_values = array_merge($sql_values, $networks);
652
653                 $condition = ["`uid` = ? AND NOT `self` AND NOT `deleted`" . $sql_extra];
654                 $condition = array_merge($condition, $sql_values);
655
656                 $total = DBA::count('contact', $condition);
657
658                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
659
660                 $contacts = [];
661
662                 $stmt = DBA::select('contact', [], $condition, ['order' => ['name'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);
663
664                 while ($contact = DBA::fetch($stmt)) {
665                         $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
666                         $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
667                         $contacts[] = self::getContactTemplateVars($contact);
668                 }
669                 DBA::close($stmt);
670
671                 $tabs = [
672                         [
673                                 'label' => DI::l10n()->t('All Contacts'),
674                                 'url'   => 'contact',
675                                 'sel'   => !$type ? 'active' : '',
676                                 'title' => DI::l10n()->t('Show all contacts'),
677                                 'id'    => 'showall-tab',
678                                 'accesskey' => 'l',
679                         ],
680                         [
681                                 'label' => DI::l10n()->t('Pending'),
682                                 'url'   => 'contact/pending',
683                                 'sel'   => $type == 'pending' ? 'active' : '',
684                                 'title' => DI::l10n()->t('Only show pending contacts'),
685                                 'id'    => 'showpending-tab',
686                                 'accesskey' => 'p',
687                         ],
688                         [
689                                 'label' => DI::l10n()->t('Blocked'),
690                                 'url'   => 'contact/blocked',
691                                 'sel'   => $type == 'blocked' ? 'active' : '',
692                                 'title' => DI::l10n()->t('Only show blocked contacts'),
693                                 'id'    => 'showblocked-tab',
694                                 'accesskey' => 'b',
695                         ],
696                         [
697                                 'label' => DI::l10n()->t('Ignored'),
698                                 'url'   => 'contact/ignored',
699                                 'sel'   => $type == 'ignored' ? 'active' : '',
700                                 'title' => DI::l10n()->t('Only show ignored contacts'),
701                                 'id'    => 'showignored-tab',
702                                 'accesskey' => 'i',
703                         ],
704                         [
705                                 'label' => DI::l10n()->t('Archived'),
706                                 'url'   => 'contact/archived',
707                                 'sel'   => $type == 'archived' ? 'active' : '',
708                                 'title' => DI::l10n()->t('Only show archived contacts'),
709                                 'id'    => 'showarchived-tab',
710                                 'accesskey' => 'y',
711                         ],
712                         [
713                                 'label' => DI::l10n()->t('Hidden'),
714                                 'url'   => 'contact/hidden',
715                                 'sel'   => $type == 'hidden' ? 'active' : '',
716                                 'title' => DI::l10n()->t('Only show hidden contacts'),
717                                 'id'    => 'showhidden-tab',
718                                 'accesskey' => 'h',
719                         ],
720                         [
721                                 'label' => DI::l10n()->t('Groups'),
722                                 'url'   => 'group',
723                                 'sel'   => '',
724                                 'title' => DI::l10n()->t('Organize your contact groups'),
725                                 'id'    => 'contactgroups-tab',
726                                 'accesskey' => 'e',
727                         ],
728                 ];
729
730                 $tabs_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
731                 $tabs_html = Renderer::replaceMacros($tabs_tpl, ['$tabs' => $tabs]);
732
733                 switch ($rel) {
734                         case 'followers': $header = DI::l10n()->t('Followers'); break;
735                         case 'following': $header = DI::l10n()->t('Following'); break;
736                         case 'mutuals':   $header = DI::l10n()->t('Mutual friends'); break;
737                         default:          $header = DI::l10n()->t('Contacts');
738                 }
739
740                 switch ($type) {
741                         case 'pending':  $header .= ' - ' . DI::l10n()->t('Pending'); break;
742                         case 'blocked':  $header .= ' - ' . DI::l10n()->t('Blocked'); break;
743                         case 'hidden':   $header .= ' - ' . DI::l10n()->t('Hidden'); break;
744                         case 'ignored':  $header .= ' - ' . DI::l10n()->t('Ignored'); break;
745                         case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
746                 }
747
748                 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
749
750                 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
751                 $o .= Renderer::replaceMacros($tpl, [
752                         '$header'     => $header,
753                         '$tabs'       => $tabs_html,
754                         '$total'      => $total,
755                         '$search'     => $search_hdr,
756                         '$desc'       => DI::l10n()->t('Search your contacts'),
757                         '$finding'    => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
758                         '$submit'     => DI::l10n()->t('Find'),
759                         '$cmd'        => DI::args()->getCommand(),
760                         '$contacts'   => $contacts,
761                         '$form_security_token'  => BaseModule::getFormSecurityToken('contact_batch_actions'),
762                         'multiselect' => 1,
763                         '$batch_actions' => [
764                                 'contacts_batch_update'  => DI::l10n()->t('Update'),
765                                 'contacts_batch_block'   => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
766                                 'contacts_batch_ignore'  => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
767                         ],
768                         '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
769                         '$paginate'   => $pager->renderFull($total),
770                 ]);
771
772                 return $o;
773         }
774
775         /**
776          * List of pages for the Contact TabBar
777          *
778          * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
779          *
780          * @param array $contact    The contact array
781          * @param int   $active_tab 1 if tab should be marked as active
782          *
783          * @return string HTML string of the contact page tabs buttons.
784          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
785          * @throws \ImagickException
786          */
787         public static function getTabsHTML(array $contact, int $active_tab)
788         {
789                 $cid = $pcid = $contact['id'];
790                 $data = Model\Contact::getPublicAndUserContactID($contact['id'], local_user());
791                 if (!empty($data['user']) && ($contact['id'] == $data['public'])) {
792                         $cid = $data['user'];
793                 } elseif (!empty($data['public'])) {
794                         $pcid = $data['public'];
795                 }
796
797                 // tabs
798                 $tabs = [
799                         [
800                                 'label' => DI::l10n()->t('Status'),
801                                 'url'   => 'contact/' . $pcid . '/conversations',
802                                 'sel'   => (($active_tab == self::TAB_CONVERSATIONS) ? 'active' : ''),
803                                 'title' => DI::l10n()->t('Conversations started by this contact'),
804                                 'id'    => 'status-tab',
805                                 'accesskey' => 'm',
806                         ],
807                         [
808                                 'label' => DI::l10n()->t('Posts and Comments'),
809                                 'url'   => 'contact/' . $pcid . '/posts',
810                                 'sel'   => (($active_tab == self::TAB_POSTS) ? 'active' : ''),
811                                 'title' => DI::l10n()->t('Status Messages and Posts'),
812                                 'id'    => 'posts-tab',
813                                 'accesskey' => 'p',
814                         ],
815                         [
816                                 'label' => DI::l10n()->t('Media'),
817                                 'url'   => 'contact/' . $pcid . '/media',
818                                 'sel'   => (($active_tab == self::TAB_MEDIA) ? 'active' : ''),
819                                 'title' => DI::l10n()->t('Posts containing media objects'),
820                                 'id'    => 'media-tab',
821                                 'accesskey' => 'd',
822                         ],
823                         [
824                                 'label' => DI::l10n()->t('Profile'),
825                                 'url'   => 'contact/' . $cid,
826                                 'sel'   => (($active_tab == self::TAB_PROFILE) ? 'active' : ''),
827                                 'title' => DI::l10n()->t('Profile Details'),
828                                 'id'    => 'profile-tab',
829                                 'accesskey' => 'o',
830                         ],
831                         ['label' => DI::l10n()->t('Contacts'),
832                                 'url'   => 'contact/' . $pcid . '/contacts',
833                                 'sel'   => (($active_tab == self::TAB_CONTACTS) ? 'active' : ''),
834                                 'title' => DI::l10n()->t('View all known contacts'),
835                                 'id'    => 'contacts-tab',
836                                 'accesskey' => 't'
837                         ],
838                 ];
839
840                 if (!empty($contact['network']) && in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) && ($cid != $pcid)) {
841                         $tabs[] = ['label' => DI::l10n()->t('Advanced'),
842                                 'url'   => 'contact/' . $cid . '/advanced/',
843                                 'sel'   => (($active_tab == self::TAB_ADVANCED) ? 'active' : ''),
844                                 'title' => DI::l10n()->t('Advanced Contact Settings'),
845                                 'id'    => 'advanced-tab',
846                                 'accesskey' => 'r'
847                         ];
848                 }
849
850                 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
851                 $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
852
853                 return $tab_str;
854         }
855
856         /**
857          * Return the fields for the contact template
858          *
859          * @param array $contact Contact array
860          * @return array Template fields
861          */
862         public static function getContactTemplateVars(array $contact)
863         {
864                 $alt_text = '';
865
866                 if (!empty($contact['url']) && isset($contact['uid']) && ($contact['uid'] == 0) && local_user()) {
867                         $personal = Model\Contact::getByURL($contact['url'], false, ['uid', 'rel', 'self'], local_user());
868                         if (!empty($personal)) {
869                                 $contact['uid'] = $personal['uid'];
870                                 $contact['rel'] = $personal['rel'];
871                                 $contact['self'] = $personal['self'];
872                         }
873                 }
874
875                 if (!empty($contact['uid']) && !empty($contact['rel']) && local_user() == $contact['uid']) {
876                         switch ($contact['rel']) {
877                                 case Model\Contact::FRIEND:
878                                         $alt_text = DI::l10n()->t('Mutual Friendship');
879                                         break;
880
881                                 case Model\Contact::FOLLOWER;
882                                         $alt_text = DI::l10n()->t('is a fan of yours');
883                                         break;
884
885                                 case Model\Contact::SHARING;
886                                         $alt_text = DI::l10n()->t('you are a fan of');
887                                         break;
888
889                                 default:
890                                         break;
891                         }
892                 }
893
894                 $url = Model\Contact::magicLinkByContact($contact);
895
896                 if (strpos($url, 'redir/') === 0) {
897                         $sparkle = ' class="sparkle" ';
898                 } else {
899                         $sparkle = '';
900                 }
901
902                 if ($contact['pending']) {
903                         if (in_array($contact['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
904                                 $alt_text = DI::l10n()->t('Pending outgoing contact request');
905                         } else {
906                                 $alt_text = DI::l10n()->t('Pending incoming contact request');
907                         }
908                 }
909
910                 if ($contact['self']) {
911                         $alt_text = DI::l10n()->t('This is you');
912                         $url = $contact['url'];
913                         $sparkle = '';
914                 }
915
916                 return [
917                         'id'           => $contact['id'],
918                         'url'          => $url,
919                         'img_hover'    => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
920                         'photo_menu'   => Model\Contact::photoMenu($contact),
921                         'thumb'        => Model\Contact::getThumb($contact, true),
922                         'alt_text'     => $alt_text,
923                         'name'         => $contact['name'],
924                         'nick'         => $contact['nick'],
925                         'details'      => $contact['location'],
926                         'tags'         => $contact['keywords'],
927                         'about'        => $contact['about'],
928                         'account_type' => Model\Contact::getAccountType($contact),
929                         'sparkle'      => $sparkle,
930                         'itemurl'      => ($contact['addr'] ?? '') ?: $contact['url'],
931                         'network'      => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol'], $contact['gsid']),
932                 ];
933         }
934
935         /**
936          * Gives a array with actions which can performed to a given contact
937          *
938          * This includes actions like e.g. 'block', 'hide', 'delete' and others
939          *
940          * @param array $contact Data about the Contact
941          * @return array with contact related actions
942          */
943         private static function getContactActions($contact)
944         {
945                 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
946                 $contact_actions = [];
947
948                 $formSecurityToken = self::getFormSecurityToken('contact_action');
949
950                 // Provide friend suggestion only for Friendica contacts
951                 if ($contact['network'] === Protocol::DFRN) {
952                         $contact_actions['suggest'] = [
953                                 'label' => DI::l10n()->t('Suggest friends'),
954                                 'url'   => 'fsuggest/' . $contact['id'],
955                                 'title' => '',
956                                 'sel'   => '',
957                                 'id'    => 'suggest',
958                         ];
959                 }
960
961                 if ($poll_enabled) {
962                         $contact_actions['update'] = [
963                                 'label' => DI::l10n()->t('Update now'),
964                                 'url'   => 'contact/' . $contact['id'] . '/update?t=' . $formSecurityToken,
965                                 'title' => '',
966                                 'sel'   => '',
967                                 'id'    => 'update',
968                         ];
969                 }
970
971                 if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
972                         $contact_actions['updateprofile'] = [
973                                 'label' => DI::l10n()->t('Refetch contact data'),
974                                 'url'   => 'contact/' . $contact['id'] . '/updateprofile?t=' . $formSecurityToken,
975                                 'title' => '',
976                                 'sel'   => '',
977                                 'id'    => 'updateprofile',
978                         ];
979                 }
980
981                 $contact_actions['block'] = [
982                         'label' => (intval($contact['blocked']) ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
983                         'url'   => 'contact/' . $contact['id'] . '/block?t=' . $formSecurityToken,
984                         'title' => DI::l10n()->t('Toggle Blocked status'),
985                         'sel'   => (intval($contact['blocked']) ? 'active' : ''),
986                         'id'    => 'toggle-block',
987                 ];
988
989                 $contact_actions['ignore'] = [
990                         'label' => (intval($contact['readonly']) ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
991                         'url'   => 'contact/' . $contact['id'] . '/ignore?t=' . $formSecurityToken,
992                         'title' => DI::l10n()->t('Toggle Ignored status'),
993                         'sel'   => (intval($contact['readonly']) ? 'active' : ''),
994                         'id'    => 'toggle-ignore',
995                 ];
996
997                 if ($contact['uid'] != 0 && Protocol::supportsRevokeFollow($contact['network']) && in_array($contact['rel'], [Model\Contact::FOLLOWER, Model\Contact::FRIEND])) {
998                         $contact_actions['revoke_follow'] = [
999                                 'label' => DI::l10n()->t('Revoke Follow'),
1000                                 'url'   => 'contact/' . $contact['id'] . '/revoke',
1001                                 'title' => DI::l10n()->t('Revoke the follow from this contact'),
1002                                 'sel'   => '',
1003                                 'id'    => 'revoke_follow',
1004                         ];
1005                 }
1006
1007                 return $contact_actions;
1008         }
1009 }