3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
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;
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;
46 * Manages and show Contacts and their content
48 class Contact extends BaseModule
50 const TAB_CONVERSATIONS = 1;
52 const TAB_PROFILE = 3;
53 const TAB_CONTACTS = 4;
54 const TAB_ADVANCED = 5;
56 private static function batchActions()
58 if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
62 $redirectUrl = $_POST['redirect_url'] ?? 'contact';
64 self::checkFormSecurityTokenRedirectOnError($redirectUrl, 'contact_batch_actions');
66 $orig_records = Model\Contact::selectToArray(['id', 'uid'], ['id' => $_POST['contact_batch'], 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
69 foreach ($orig_records as $orig_record) {
70 $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user());
75 if (!empty($_POST['contacts_batch_update']) && $cdata['user']) {
76 self::updateContactFromPoll($cdata['user']);
80 if (!empty($_POST['contacts_batch_block'])) {
81 self::toggleBlockContact($cdata['public']);
85 if (!empty($_POST['contacts_batch_ignore'])) {
86 self::toggleIgnoreContact($cdata['public']);
90 if (!empty($_POST['contacts_batch_drop']) && $cdata['user']
91 && self::dropContact($cdata['user'], local_user())
96 if ($count_actions > 0) {
97 info(DI::l10n()->tt('%d contact edited.', '%d contacts edited.', $count_actions));
100 DI::baseUrl()->redirect($redirectUrl);
103 public static function post(array $parameters = [])
109 // @TODO: Replace with parameter from router
110 if (DI::args()->getArgv()[1] === 'batch') {
111 self::batchActions();
115 // @TODO: Replace with parameter from router
116 $contact_id = intval(DI::args()->getArgv()[1]);
121 if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
122 notice(DI::l10n()->t('Could not access contact record.'));
123 DI::baseUrl()->redirect('contact');
124 return; // NOTREACHED
127 Hook::callAll('contact_edit_post', $_POST);
129 $hidden = !empty($_POST['hidden']);
131 $notify = !empty($_POST['notify']);
133 $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
135 $remote_self = $_POST['remote_self'] ?? false;
137 $ffi_keyword_denylist = Strings::escapeHtml(trim($_POST['ffi_keyword_denylist'] ?? ''));
139 $priority = intval($_POST['poll'] ?? 0);
140 if ($priority > 5 || $priority < 0) {
144 $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
146 $r = DBA::update('contact', [
147 'priority' => $priority,
150 'notify_new_posts' => $notify,
151 'fetch_further_information' => $fetch_further_information,
152 'remote_self' => $remote_self,
153 'ffi_keyword_denylist' => $ffi_keyword_denylist],
154 ['id' => $contact_id, 'uid' => local_user()]
157 if (!DBA::isResult($r)) {
158 notice(DI::l10n()->t('Failed to update contact record.'));
163 /* contact actions */
166 * @param int $contact_id Id of contact with uid != 0
167 * @throws NotFoundException
168 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
169 * @throws \ImagickException
171 private static function updateContactFromPoll(int $contact_id)
173 $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
174 if (!DBA::isResult($contact)) {
178 if ($contact['network'] == Protocol::OSTATUS) {
179 $result = Model\Contact::createFromProbeForUser($contact['uid'], $contact['url'], $contact['network']);
181 if ($result['success']) {
182 DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
185 // pull feed and consume it, which should subscribe to the hub.
186 Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
188 Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id);
193 * @param int $contact_id Id of the contact with uid != 0
194 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
195 * @throws \ImagickException
197 private static function updateContactFromProbe(int $contact_id)
199 $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
200 if (!DBA::isResult($contact)) {
204 // Update the entry in the contact table
205 Model\Contact::updateFromProbe($contact_id);
209 * Toggles the blocked status of a contact identified by id.
211 * @param int $contact_id Id of the contact with uid = 0
214 private static function toggleBlockContact(int $contact_id)
216 $blocked = !Model\Contact\User::isBlocked($contact_id, local_user());
217 Model\Contact\User::setBlocked($contact_id, local_user(), $blocked);
221 * Toggles the ignored status of a contact identified by id.
223 * @param int $contact_id Id of the contact with uid = 0
226 private static function toggleIgnoreContact(int $contact_id)
228 $ignored = !Model\Contact\User::isIgnored($contact_id, local_user());
229 Model\Contact\User::setIgnored($contact_id, local_user(), $ignored);
233 * @param int $contact_id Id for contact with uid != 0
234 * @param int $uid Id for user we want to drop the contact for
236 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
237 * @throws \ImagickException
239 private static function dropContact(int $contact_id, int $uid): bool
241 $contact = Model\Contact::getContactForUser($contact_id, $uid);
242 if (!DBA::isResult($contact)) {
246 $owner = Model\User::getOwnerDataById($uid);
247 if (!DBA::isResult($owner)) {
251 Model\Contact::terminateFriendship($owner, $contact, true);
252 Model\Contact::remove($contact['id']);
257 public static function content(array $parameters = [], $update = 0)
260 return Login::form($_SERVER['REQUEST_URI']);
265 $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
266 $nets = Strings::escapeTags(trim($_GET['nets'] ?? ''));
267 $rel = Strings::escapeTags(trim($_GET['rel'] ?? ''));
268 $group = Strings::escapeTags(trim($_GET['group'] ?? ''));
270 $accounttype = $_GET['accounttype'] ?? '';
271 $accounttypeid = User::getAccountTypeByString($accounttype);
275 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
276 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
277 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
278 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
281 // @TODO: Replace with parameter from router
282 if (DI::args()->getArgc() == 2 && intval(DI::args()->getArgv()[1])
283 || DI::args()->getArgc() == 3 && intval(DI::args()->getArgv()[1]) && in_array(DI::args()->getArgv()[2], ['posts', 'conversations'])
285 $contact_id = intval(DI::args()->getArgv()[1]);
287 // Ensure to use the user contact when the public contact was provided
288 $data = Model\Contact::getPublicAndUserContactID($contact_id, local_user());
289 if (!empty($data['user']) && ($contact_id == $data['public'])) {
290 $contact_id = $data['user'];
293 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'deleted' => false]);
295 // Don't display contacts that are about to be deleted
296 if ($contact['network'] == Protocol::PHANTOM) {
301 if (DBA::isResult($contact)) {
302 if ($contact['self']) {
303 // @TODO: Replace with parameter from router
304 if ((DI::args()->getArgc() == 3) && intval(DI::args()->getArgv()[1]) && in_array(DI::args()->getArgv()[2], ['posts', 'conversations'])) {
305 DI::baseUrl()->redirect('profile/' . $contact['nick']);
307 DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile');
311 $vcard_widget = Widget\VCard::getHTML($contact);
313 $findpeople_widget = '';
315 $account_widget = '';
316 $networks_widget = '';
319 if ($contact['uid'] != 0) {
320 $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
326 $findpeople_widget = Widget::findPeople();
327 if (isset($_GET['add'])) {
328 $follow_widget = Widget::follow($_GET['add']);
330 $follow_widget = Widget::follow();
333 $account_widget = Widget::accounttypes($_SERVER['REQUEST_URI'], $accounttype);
334 $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
335 $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
336 $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group);
339 DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $account_widget . $groups_widget . $networks_widget . $rel_widget;
341 $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
342 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
343 '$baseurl' => DI::baseUrl()->get(true),
347 Nav::setSelected('contact');
350 notice(DI::l10n()->t('Permission denied.'));
351 return Login::form();
354 if (DI::args()->getArgc() == 3) {
355 $contact_id = intval(DI::args()->getArgv()[1]);
357 throw new BadRequestException();
360 // @TODO: Replace with parameter from router
361 $cmd = DI::args()->getArgv()[2];
363 $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
364 if (!DBA::isResult($orig_record)) {
365 throw new NotFoundException(DI::l10n()->t('Contact not found'));
368 if ($cmd === 'posts') {
369 return self::getPostsHTML($a, $contact_id);
372 if ($cmd === 'conversations') {
373 return self::getConversationsHMTL($a, $contact_id, $update);
376 self::checkFormSecurityTokenRedirectOnError('contact/' . $contact_id, 'contact_action', 't');
378 $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user());
380 throw new NotFoundException(DI::l10n()->t('Contact not found'));
383 if ($cmd === 'update' && $cdata['user']) {
384 self::updateContactFromPoll($cdata['user']);
385 DI::baseUrl()->redirect('contact/' . $cdata['public']);
389 if ($cmd === 'updateprofile' && $cdata['user']) {
390 self::updateContactFromProbe($cdata['user']);
391 DI::baseUrl()->redirect('contact/' . $cdata['public']);
395 if ($cmd === 'block') {
396 if (public_contact() === $cdata['public']) {
397 throw new BadRequestException(DI::l10n()->t('You can\'t block yourself'));
400 self::toggleBlockContact($cdata['public']);
402 $blocked = Model\Contact\User::isBlocked($contact_id, local_user());
403 info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')));
405 DI::baseUrl()->redirect('contact/' . $cdata['public']);
409 if ($cmd === 'ignore') {
410 if (public_contact() === $cdata['public']) {
411 throw new BadRequestException(DI::l10n()->t('You can\'t ignore yourself'));
414 self::toggleIgnoreContact($cdata['public']);
416 $ignored = Model\Contact\User::isIgnored($cdata['public'], local_user());
417 info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')));
419 DI::baseUrl()->redirect('contact/' . $cdata['public']);
423 if ($cmd === 'drop' && $cdata['user']) {
424 // Check if we should do HTML-based delete confirmation
425 if (!empty($_REQUEST['confirm'])) {
426 DI::page()['aside'] = '';
428 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
429 '$header' => DI::l10n()->t('Drop contact'),
430 '$contact' => self::getContactTemplateVars($orig_record),
432 '$message' => DI::l10n()->t('Do you really want to delete this contact?'),
433 '$confirm' => DI::l10n()->t('Yes'),
434 '$confirm_url' => DI::args()->getCommand(),
435 '$confirm_name' => 'confirmed',
436 '$cancel' => DI::l10n()->t('Cancel'),
439 // Now check how the user responded to the confirmation query
440 if (!empty($_REQUEST['canceled'])) {
441 DI::baseUrl()->redirect('contact');
444 if (self::dropContact($cdata['user'], local_user())) {
445 info(DI::l10n()->t('Contact has been removed.'));
448 DI::baseUrl()->redirect('contact');
453 $_SESSION['return_path'] = DI::args()->getQueryString();
455 if (!empty($contact)) {
456 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
457 '$baseurl' => DI::baseUrl()->get(true),
460 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
461 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
464 switch ($contact['rel']) {
465 case Model\Contact::FRIEND:
466 $relation_text = DI::l10n()->t('You are mutual friends with %s');
469 case Model\Contact::FOLLOWER;
470 $relation_text = DI::l10n()->t('You are sharing with %s');
473 case Model\Contact::SHARING;
474 $relation_text = DI::l10n()->t('%s is sharing with you');
481 if ($contact['uid'] == 0) {
485 if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
489 $relation_text = sprintf($relation_text, $contact['name']);
491 $url = Model\Contact::magicLinkByContact($contact);
492 if (strpos($url, 'redir/') === 0) {
493 $sparkle = ' class="sparkle" ';
498 $insecure = DI::l10n()->t('Private communications are not available for this contact.');
500 $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? DI::l10n()->t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
502 if ($contact['last-update'] > DBA::NULL_DATETIME) {
503 $last_update .= ' ' . ($contact['failed'] ? DI::l10n()->t('(Update was not successful)') : DI::l10n()->t('(Update was successful)'));
505 $lblsuggest = (($contact['network'] === Protocol::DFRN) ? DI::l10n()->t('Suggest friends') : '');
507 $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
509 $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol'], $contact['gsid']));
512 $tab_str = self::getTabsHTML($contact, self::TAB_PROFILE);
514 $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : '');
516 $fetch_further_information = null;
517 if ($contact['network'] == Protocol::FEED) {
518 $fetch_further_information = [
519 'fetch_further_information',
520 DI::l10n()->t('Fetch further information for feeds'),
521 $contact['fetch_further_information'],
522 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.'),
524 '0' => DI::l10n()->t('Disabled'),
525 '1' => DI::l10n()->t('Fetch information'),
526 '3' => DI::l10n()->t('Fetch keywords'),
527 '2' => DI::l10n()->t('Fetch information and keywords')
532 // Disable remote self for everything except feeds.
533 // There is an issue when you repeat an item from maybe twitter and you got comments from friendica and twitter
534 // Problem is, you couldn't reply to both networks.
535 $allow_remote_self = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::FEED, Protocol::DFRN, Protocol::DIASPORA, Protocol::TWITTER])
536 && DI::config()->get('system', 'allow_users_remote_self');
538 if ($contact['network'] == Protocol::FEED) {
539 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
540 Model\Contact::MIRROR_FORWARDED => DI::l10n()->t('Mirror as forwarded posting'),
541 Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting')];
542 } elseif (in_array($contact['network'], [Protocol::ACTIVITYPUB])) {
543 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
544 Model\Contact::MIRROR_NATIVE_RESHARE => DI::l10n()->t('Native reshare')];
545 } elseif (in_array($contact['network'], [Protocol::DFRN])) {
546 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
547 Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting'),
548 Model\Contact::MIRROR_NATIVE_RESHARE => DI::l10n()->t('Native reshare')];
550 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
551 Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting')];
554 $poll_interval = null;
555 if ((($contact['network'] == Protocol::FEED) && !DI::config()->get('system', 'adjust_poll_frequency')) || ($contact['network'] == Protocol::MAIL)) {
556 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
559 // Load contactact related actions like hide, suggest, delete and others
560 $contact_actions = self::getContactActions($contact);
562 if ($contact['uid'] != 0) {
563 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
564 $contact_settings_label = DI::l10n()->t('Contact Settings');
567 $contact_settings_label = null;
570 $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
571 $o .= Renderer::replaceMacros($tpl, [
572 '$header' => DI::l10n()->t('Contact'),
573 '$tab_str' => $tab_str,
574 '$submit' => DI::l10n()->t('Submit'),
575 '$lbl_info1' => $lbl_info1,
576 '$lbl_info2' => DI::l10n()->t('Their personal note'),
577 '$reason' => trim(Strings::escapeTags($contact['reason'])),
578 '$infedit' => DI::l10n()->t('Edit contact notes'),
579 '$common_link' => 'contact/' . $contact['id'] . '/contacts/common',
580 '$relation_text' => $relation_text,
581 '$visit' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
582 '$blockunblock' => DI::l10n()->t('Block/Unblock contact'),
583 '$ignorecont' => DI::l10n()->t('Ignore contact'),
584 '$lblrecent' => DI::l10n()->t('View conversations'),
585 '$lblsuggest' => $lblsuggest,
586 '$nettype' => $nettype,
587 '$poll_interval' => $poll_interval,
588 '$poll_enabled' => $poll_enabled,
589 '$lastupdtext' => DI::l10n()->t('Last update:'),
590 '$lost_contact' => $lost_contact,
591 '$updpub' => DI::l10n()->t('Update public posts'),
592 '$last_update' => $last_update,
593 '$udnow' => DI::l10n()->t('Update now'),
594 '$contact_id' => $contact['id'],
595 '$block_text' => ($contact['blocked'] ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
596 '$ignore_text' => ($contact['readonly'] ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
597 '$insecure' => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
598 '$info' => $contact['info'],
599 '$cinfo' => ['info', '', $contact['info'], ''],
600 '$blocked' => ($contact['blocked'] ? DI::l10n()->t('Currently blocked') : ''),
601 '$ignored' => ($contact['readonly'] ? DI::l10n()->t('Currently ignored') : ''),
602 '$archived' => ($contact['archive'] ? DI::l10n()->t('Currently archived') : ''),
603 '$pending' => ($contact['pending'] ? DI::l10n()->t('Awaiting connection acknowledge') : ''),
604 '$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')],
605 '$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')],
606 '$fetch_further_information' => $fetch_further_information,
607 '$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')],
608 '$photo' => Model\Contact::getPhoto($contact),
609 '$name' => $contact['name'],
610 '$sparkle' => $sparkle,
612 '$profileurllabel'=> DI::l10n()->t('Profile URL'),
613 '$profileurl' => $contact['url'],
614 '$account_type' => Model\Contact::getAccountType($contact),
615 '$location' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['location']),
616 '$location_label' => DI::l10n()->t('Location:'),
617 '$xmpp' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['xmpp']),
618 '$xmpp_label' => DI::l10n()->t('XMPP:'),
619 '$matrix' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['matrix']),
620 '$matrix_label' => DI::l10n()->t('Matrix:'),
621 '$about' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['about'], BBCode::EXTERNAL),
622 '$about_label' => DI::l10n()->t('About:'),
623 '$keywords' => $contact['keywords'],
624 '$keywords_label' => DI::l10n()->t('Tags:'),
625 '$contact_action_button' => DI::l10n()->t('Actions'),
626 '$contact_actions'=> $contact_actions,
627 '$contact_status' => DI::l10n()->t('Status'),
628 '$contact_settings_label' => $contact_settings_label,
629 '$contact_profile_label' => DI::l10n()->t('Profile'),
630 '$allow_remote_self' => $allow_remote_self,
631 '$remote_self' => ['remote_self',
632 DI::l10n()->t('Mirror postings from this contact'),
633 $contact['remote_self'],
634 DI::l10n()->t('Mark this contact as remote_self, this will cause friendica to repost new entries from this contact.'),
639 $arr = ['contact' => $contact, 'output' => $o];
641 Hook::callAll('contact_edit', $arr);
643 return $arr['output'];
646 $sql_values = [local_user()];
648 // @TODO: Replace with parameter from router
649 $type = DI::args()->getArgv()[1] ?? '';
653 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
654 // This makes the query look for contact.uid = 0
655 array_unshift($sql_values, 0);
658 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
661 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
662 // This makes the query look for contact.uid = 0
663 array_unshift($sql_values, 0);
666 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
669 $sql_extra = " AND `pending` AND NOT `archive` AND NOT `failed` AND ((`rel` = ?)
670 OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
671 $sql_values[] = Model\Contact::SHARING;
674 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
678 if (isset($accounttypeid)) {
679 $sql_extra .= " AND `contact-type` = ?";
680 $sql_values[] = $accounttypeid;
687 $search_hdr = $search;
688 $search_txt = preg_quote($search);
689 $sql_extra .= " AND (name REGEXP ? OR url REGEXP ? OR nick REGEXP ?)";
690 $sql_values[] = $search_txt;
691 $sql_values[] = $search_txt;
692 $sql_values[] = $search_txt;
696 $sql_extra .= " AND network = ? ";
697 $sql_values[] = $nets;
702 $sql_extra .= " AND `rel` IN (?, ?)";
703 $sql_values[] = Model\Contact::FOLLOWER;
704 $sql_values[] = Model\Contact::FRIEND;
707 $sql_extra .= " AND `rel` IN (?, ?)";
708 $sql_values[] = Model\Contact::SHARING;
709 $sql_values[] = Model\Contact::FRIEND;
712 $sql_extra .= " AND `rel` = ?";
713 $sql_values[] = Model\Contact::FRIEND;
718 $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
719 $sql_values[] = $group;
723 $stmt = DBA::p("SELECT COUNT(*) AS `total`
729 " . Widget::unavailableNetworks(),
732 if (DBA::isResult($stmt)) {
733 $total = DBA::fetch($stmt)['total'];
737 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
739 $sql_values[] = $pager->getStart();
740 $sql_values[] = $pager->getItemsPerPage();
744 $stmt = DBA::p("SELECT *
754 while ($contact = DBA::fetch($stmt)) {
755 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
756 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
757 $contacts[] = self::getContactTemplateVars($contact);
763 'label' => DI::l10n()->t('All Contacts'),
765 'sel' => !$type ? 'active' : '',
766 'title' => DI::l10n()->t('Show all contacts'),
767 'id' => 'showall-tab',
771 'label' => DI::l10n()->t('Pending'),
772 'url' => 'contact/pending',
773 'sel' => $type == 'pending' ? 'active' : '',
774 'title' => DI::l10n()->t('Only show pending contacts'),
775 'id' => 'showpending-tab',
779 'label' => DI::l10n()->t('Blocked'),
780 'url' => 'contact/blocked',
781 'sel' => $type == 'blocked' ? 'active' : '',
782 'title' => DI::l10n()->t('Only show blocked contacts'),
783 'id' => 'showblocked-tab',
787 'label' => DI::l10n()->t('Ignored'),
788 'url' => 'contact/ignored',
789 'sel' => $type == 'ignored' ? 'active' : '',
790 'title' => DI::l10n()->t('Only show ignored contacts'),
791 'id' => 'showignored-tab',
795 'label' => DI::l10n()->t('Archived'),
796 'url' => 'contact/archived',
797 'sel' => $type == 'archived' ? 'active' : '',
798 'title' => DI::l10n()->t('Only show archived contacts'),
799 'id' => 'showarchived-tab',
803 'label' => DI::l10n()->t('Hidden'),
804 'url' => 'contact/hidden',
805 'sel' => $type == 'hidden' ? 'active' : '',
806 'title' => DI::l10n()->t('Only show hidden contacts'),
807 'id' => 'showhidden-tab',
811 'label' => DI::l10n()->t('Groups'),
814 'title' => DI::l10n()->t('Organize your contact groups'),
815 'id' => 'contactgroups-tab',
820 $tabs_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
821 $tabs_html = Renderer::replaceMacros($tabs_tpl, ['$tabs' => $tabs]);
824 case 'followers': $header = DI::l10n()->t('Followers'); break;
825 case 'following': $header = DI::l10n()->t('Following'); break;
826 case 'mutuals': $header = DI::l10n()->t('Mutual friends'); break;
827 default: $header = DI::l10n()->t('Contacts');
831 case 'pending': $header .= ' - ' . DI::l10n()->t('Pending'); break;
832 case 'blocked': $header .= ' - ' . DI::l10n()->t('Blocked'); break;
833 case 'hidden': $header .= ' - ' . DI::l10n()->t('Hidden'); break;
834 case 'ignored': $header .= ' - ' . DI::l10n()->t('Ignored'); break;
835 case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
838 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
840 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
841 $o .= Renderer::replaceMacros($tpl, [
842 '$header' => $header,
843 '$tabs' => $tabs_html,
845 '$search' => $search_hdr,
846 '$desc' => DI::l10n()->t('Search your contacts'),
847 '$finding' => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
848 '$submit' => DI::l10n()->t('Find'),
849 '$cmd' => DI::args()->getCommand(),
850 '$contacts' => $contacts,
851 '$form_security_token' => BaseModule::getFormSecurityToken('contact_batch_actions'),
852 '$contact_drop_confirm' => DI::l10n()->t('Do you really want to delete this contact?'),
854 '$batch_actions' => [
855 'contacts_batch_update' => DI::l10n()->t('Update'),
856 'contacts_batch_block' => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
857 'contacts_batch_ignore' => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
858 'contacts_batch_drop' => DI::l10n()->t('Delete'),
860 '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
861 '$paginate' => $pager->renderFull($total),
868 * List of pages for the Contact TabBar
870 * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
872 * @param array $contact The contact array
873 * @param int $active_tab 1 if tab should be marked as active
875 * @return string HTML string of the contact page tabs buttons.
876 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
877 * @throws \ImagickException
879 public static function getTabsHTML(array $contact, int $active_tab)
881 $cid = $pcid = $contact['id'];
882 $data = Model\Contact::getPublicAndUserContactID($contact['id'], local_user());
883 if (!empty($data['user']) && ($contact['id'] == $data['public'])) {
884 $cid = $data['user'];
885 } elseif (!empty($data['public'])) {
886 $pcid = $data['public'];
892 'label' => DI::l10n()->t('Status'),
893 'url' => 'contact/' . $pcid . '/conversations',
894 'sel' => (($active_tab == self::TAB_CONVERSATIONS) ? 'active' : ''),
895 'title' => DI::l10n()->t('Conversations started by this contact'),
896 'id' => 'status-tab',
900 'label' => DI::l10n()->t('Posts and Comments'),
901 'url' => 'contact/' . $pcid . '/posts',
902 'sel' => (($active_tab == self::TAB_POSTS) ? 'active' : ''),
903 'title' => DI::l10n()->t('Status Messages and Posts'),
908 'label' => DI::l10n()->t('Profile'),
909 'url' => 'contact/' . $cid,
910 'sel' => (($active_tab == self::TAB_PROFILE) ? 'active' : ''),
911 'title' => DI::l10n()->t('Profile Details'),
912 'id' => 'profile-tab',
915 ['label' => DI::l10n()->t('Contacts'),
916 'url' => 'contact/' . $pcid . '/contacts',
917 'sel' => (($active_tab == self::TAB_CONTACTS) ? 'active' : ''),
918 'title' => DI::l10n()->t('View all known contacts'),
919 'id' => 'contacts-tab',
924 if (!empty($contact['network']) && in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) && ($cid != $pcid)) {
925 $tabs[] = ['label' => DI::l10n()->t('Advanced'),
926 'url' => 'contact/' . $cid . '/advanced/',
927 'sel' => (($active_tab == self::TAB_ADVANCED) ? 'active' : ''),
928 'title' => DI::l10n()->t('Advanced Contact Settings'),
929 'id' => 'advanced-tab',
934 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
935 $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
940 public static function getConversationsHMTL($a, $contact_id, $update, $parent = 0)
945 // We need the editor here to be able to reshare an item.
947 $o = status_editor($a, [], 0, true);
951 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
954 $o .= self::getTabsHTML($contact, self::TAB_CONVERSATIONS);
957 if (DBA::isResult($contact)) {
959 $profiledata = Model\Contact::getByURLForUser($contact['url'], local_user());
960 DI::page()['aside'] = Widget\VCard::getHTML($profiledata);
962 DI::page()['aside'] = '';
965 if ($contact['uid'] == 0) {
966 $o .= Model\Contact::getPostsFromId($contact['id'], true, $update, $parent);
968 $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update, $parent);
975 private static function getPostsHTML($a, $contact_id)
977 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
979 $o = self::getTabsHTML($contact, self::TAB_POSTS);
981 if (DBA::isResult($contact)) {
982 $profiledata = Model\Contact::getByURLForUser($contact['url'], local_user());
984 if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
985 $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
988 DI::page()['aside'] = Widget\VCard::getHTML($profiledata);
990 if ($contact['uid'] == 0) {
991 $o .= Model\Contact::getPostsFromId($contact['id']);
993 $o .= Model\Contact::getPostsFromUrl($contact['url']);
1001 * Return the fields for the contact template
1003 * @param array $contact Contact array
1004 * @return array Template fields
1006 public static function getContactTemplateVars(array $contact)
1010 if (!empty($contact['url']) && isset($contact['uid']) && ($contact['uid'] == 0) && local_user()) {
1011 $personal = Model\Contact::getByURL($contact['url'], false, ['uid', 'rel', 'self'], local_user());
1012 if (!empty($personal)) {
1013 $contact['uid'] = $personal['uid'];
1014 $contact['rel'] = $personal['rel'];
1015 $contact['self'] = $personal['self'];
1019 if (!empty($contact['uid']) && !empty($contact['rel']) && local_user() == $contact['uid']) {
1020 switch ($contact['rel']) {
1021 case Model\Contact::FRIEND:
1022 $alt_text = DI::l10n()->t('Mutual Friendship');
1025 case Model\Contact::FOLLOWER;
1026 $alt_text = DI::l10n()->t('is a fan of yours');
1029 case Model\Contact::SHARING;
1030 $alt_text = DI::l10n()->t('you are a fan of');
1038 $url = Model\Contact::magicLinkByContact($contact);
1040 if (strpos($url, 'redir/') === 0) {
1041 $sparkle = ' class="sparkle" ';
1046 if ($contact['pending']) {
1047 if (in_array($contact['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
1048 $alt_text = DI::l10n()->t('Pending outgoing contact request');
1050 $alt_text = DI::l10n()->t('Pending incoming contact request');
1054 if ($contact['self']) {
1055 $alt_text = DI::l10n()->t('This is you');
1056 $url = $contact['url'];
1061 'id' => $contact['id'],
1063 'img_hover' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
1064 'photo_menu' => Model\Contact::photoMenu($contact),
1065 'thumb' => Model\Contact::getThumb($contact, true),
1066 'alt_text' => $alt_text,
1067 'name' => $contact['name'],
1068 'nick' => $contact['nick'],
1069 'details' => $contact['location'],
1070 'tags' => $contact['keywords'],
1071 'about' => $contact['about'],
1072 'account_type' => Model\Contact::getAccountType($contact),
1073 'sparkle' => $sparkle,
1074 'itemurl' => ($contact['addr'] ?? '') ?: $contact['url'],
1075 'network' => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol'], $contact['gsid']),
1080 * Gives a array with actions which can performed to a given contact
1082 * This includes actions like e.g. 'block', 'hide', 'delete' and others
1084 * @param array $contact Data about the Contact
1085 * @return array with contact related actions
1087 private static function getContactActions($contact)
1089 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
1090 $contact_actions = [];
1092 $formSecurityToken = self::getFormSecurityToken('contact_action');
1094 // Provide friend suggestion only for Friendica contacts
1095 if ($contact['network'] === Protocol::DFRN) {
1096 $contact_actions['suggest'] = [
1097 'label' => DI::l10n()->t('Suggest friends'),
1098 'url' => 'fsuggest/' . $contact['id'],
1105 if ($poll_enabled) {
1106 $contact_actions['update'] = [
1107 'label' => DI::l10n()->t('Update now'),
1108 'url' => 'contact/' . $contact['id'] . '/update?t=' . $formSecurityToken,
1115 if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
1116 $contact_actions['updateprofile'] = [
1117 'label' => DI::l10n()->t('Refetch contact data'),
1118 'url' => 'contact/' . $contact['id'] . '/updateprofile?t=' . $formSecurityToken,
1121 'id' => 'updateprofile',
1125 $contact_actions['block'] = [
1126 'label' => (intval($contact['blocked']) ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
1127 'url' => 'contact/' . $contact['id'] . '/block?t=' . $formSecurityToken,
1128 'title' => DI::l10n()->t('Toggle Blocked status'),
1129 'sel' => (intval($contact['blocked']) ? 'active' : ''),
1130 'id' => 'toggle-block',
1133 $contact_actions['ignore'] = [
1134 'label' => (intval($contact['readonly']) ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
1135 'url' => 'contact/' . $contact['id'] . '/ignore?t=' . $formSecurityToken,
1136 'title' => DI::l10n()->t('Toggle Ignored status'),
1137 'sel' => (intval($contact['readonly']) ? 'active' : ''),
1138 'id' => 'toggle-ignore',
1141 if ($contact['uid'] != 0) {
1142 $contact_actions['delete'] = [
1143 'label' => DI::l10n()->t('Delete'),
1144 'url' => 'contact/' . $contact['id'] . '/drop?t=' . $formSecurityToken,
1145 'title' => DI::l10n()->t('Delete contact'),
1151 return $contact_actions;