3 * @copyright Copyright (C) 2020, Friendica
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;
25 use Friendica\BaseModule;
26 use Friendica\Content\ContactSelector;
27 use Friendica\Content\Nav;
28 use Friendica\Content\Pager;
29 use Friendica\Content\Text\BBCode;
30 use Friendica\Content\Widget;
31 use Friendica\Core\ACL;
32 use Friendica\Core\Hook;
33 use Friendica\Core\Protocol;
34 use Friendica\Core\Renderer;
35 use Friendica\Core\Theme;
36 use Friendica\Core\Worker;
37 use Friendica\Database\DBA;
40 use Friendica\Module\Security\Login;
41 use Friendica\Network\HTTPException\BadRequestException;
42 use Friendica\Network\HTTPException\NotFoundException;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Strings;
47 * Manages and show Contacts and their content
49 class Contact extends BaseModule
51 const TAB_CONVERSATIONS = 1;
53 const TAB_PROFILE = 3;
54 const TAB_CONTACTS = 4;
55 const TAB_ADVANCED = 5;
57 private static function batchActions()
59 if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
63 $contacts_id = $_POST['contact_batch'];
65 $stmt = DBA::select('contact', ['id', 'archive'], ['id' => $contacts_id, 'uid' => local_user(), 'self' => false, 'deleted' => false]);
66 $orig_records = DBA::toArray($stmt);
69 foreach ($orig_records as $orig_record) {
70 $contact_id = $orig_record['id'];
71 if (!empty($_POST['contacts_batch_update'])) {
72 self::updateContactFromPoll($contact_id);
75 if (!empty($_POST['contacts_batch_block'])) {
76 self::blockContact($contact_id);
79 if (!empty($_POST['contacts_batch_ignore'])) {
80 self::ignoreContact($contact_id);
83 if (!empty($_POST['contacts_batch_archive'])
84 && self::archiveContact($contact_id, $orig_record)
88 if (!empty($_POST['contacts_batch_drop'])) {
89 self::dropContact($orig_record);
93 if ($count_actions > 0) {
94 info(DI::l10n()->tt('%d contact edited.', '%d contacts edited.', $count_actions));
97 DI::baseUrl()->redirect('contact');
100 public static function post(array $parameters = [])
108 // @TODO: Replace with parameter from router
109 if ($a->argv[1] === 'batch') {
110 self::batchActions();
114 // @TODO: Replace with parameter from router
115 $contact_id = intval($a->argv[1]);
120 if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
121 notice(DI::l10n()->t('Could not access contact record.'));
122 DI::baseUrl()->redirect('contact');
123 return; // NOTREACHED
126 Hook::callAll('contact_edit_post', $_POST);
128 $hidden = !empty($_POST['hidden']);
130 $notify = !empty($_POST['notify']);
132 $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
134 $ffi_keyword_denylist = Strings::escapeHtml(trim($_POST['ffi_keyword_denylist'] ?? ''));
136 $priority = intval($_POST['poll'] ?? 0);
137 if ($priority > 5 || $priority < 0) {
141 $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
143 $r = DBA::update('contact', [
144 'priority' => $priority,
147 'notify_new_posts' => $notify,
148 'fetch_further_information' => $fetch_further_information,
149 'ffi_keyword_denylist' => $ffi_keyword_denylist],
150 ['id' => $contact_id, 'uid' => local_user()]
153 if (!DBA::isResult($r)) {
154 notice(DI::l10n()->t('Failed to update contact record.'));
157 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
158 if (DBA::isResult($contact)) {
159 $a->data['contact'] = $contact;
165 /* contact actions */
167 private static function updateContactFromPoll($contact_id)
169 $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
170 if (!DBA::isResult($contact)) {
174 if ($contact['network'] == Protocol::OSTATUS) {
175 $user = Model\User::getById($contact['uid']);
176 $result = Model\Contact::createFromProbe($user, $contact['url'], false, $contact['network']);
178 if ($result['success']) {
179 DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
182 // pull feed and consume it, which should subscribe to the hub.
183 Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
187 private static function updateContactFromProbe($contact_id)
189 $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => [0, local_user()], 'deleted' => false]);
190 if (!DBA::isResult($contact)) {
194 // Update the entry in the contact table
195 Model\Contact::updateFromProbe($contact_id);
199 * Toggles the blocked status of a contact identified by id.
204 private static function blockContact($contact_id)
206 $blocked = !Model\Contact\User::isBlocked($contact_id, local_user());
207 Model\Contact\User::setBlocked($contact_id, local_user(), $blocked);
211 * Toggles the ignored status of a contact identified by id.
216 private static function ignoreContact($contact_id)
218 $ignored = !Model\Contact\User::isIgnored($contact_id, local_user());
219 Model\Contact\User::setIgnored($contact_id, local_user(), $ignored);
223 * Toggles the archived status of a contact identified by id.
224 * If the current status isn't provided, this will always archive the contact.
227 * @param $orig_record
231 private static function archiveContact($contact_id, $orig_record)
233 $archived = empty($orig_record['archive']);
234 $r = DBA::update('contact', ['archive' => $archived], ['id' => $contact_id, 'uid' => local_user()]);
236 return DBA::isResult($r);
239 private static function dropContact($orig_record)
241 $owner = Model\User::getOwnerDataById(local_user());
242 if (!DBA::isResult($owner)) {
246 Model\Contact::terminateFriendship($owner, $orig_record, true);
247 Model\Contact::remove($orig_record['id']);
250 public static function content(array $parameters = [], $update = 0)
253 return Login::form($_SERVER['REQUEST_URI']);
258 $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
259 $nets = Strings::escapeTags(trim($_GET['nets'] ?? ''));
260 $rel = Strings::escapeTags(trim($_GET['rel'] ?? ''));
261 $group = Strings::escapeTags(trim($_GET['group'] ?? ''));
265 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
266 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
267 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
268 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
271 // @TODO: Replace with parameter from router
272 if ($a->argc == 2 && intval($a->argv[1])
273 || $a->argc == 3 && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])
275 $contact_id = intval($a->argv[1]);
277 // Ensure to use the user contact when the public contact was provided
278 $data = Model\Contact::getPublicAndUserContacID($contact_id, local_user());
279 if (!empty($data['user']) && ($contact_id == $data['public'])) {
280 $contact_id = $data['user'];
283 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'deleted' => false]);
285 // Don't display contacts that are about to be deleted
286 if ($contact['network'] == Protocol::PHANTOM) {
291 if (DBA::isResult($contact)) {
292 if ($contact['self']) {
293 // @TODO: Replace with parameter from router
294 if (($a->argc == 3) && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])) {
295 DI::baseUrl()->redirect('profile/' . $contact['nick']);
297 DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile');
301 $a->data['contact'] = $contact;
303 if (($contact['network'] != '') && ($contact['network'] != Protocol::DFRN)) {
304 $network_link = Strings::formatNetworkName($contact['network'], $contact['url']);
311 if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
312 if ($contact['uid'] && in_array($contact['rel'], [Model\Contact::SHARING, Model\Contact::FRIEND])) {
313 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']);
314 } elseif(!$contact['pending']) {
315 $follow_link = 'follow?url=' . urlencode($contact['url']);
319 $wallmessage_link = '';
320 if ($contact['uid'] && Model\Contact::canReceivePrivateMessages($contact)) {
321 $wallmessage_link = 'message/new/' . $contact['id'];
324 $vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
325 '$name' => $contact['name'],
326 '$photo' => Model\Contact::getPhoto($contact),
327 '$url' => Model\Contact::magicLinkByContact($contact, $contact['url']),
328 '$addr' => $contact['addr'] ?? '',
329 '$network_link' => $network_link,
330 '$network' => DI::l10n()->t('Network:'),
331 '$account_type' => Model\Contact::getAccountType($contact),
332 '$follow' => DI::l10n()->t('Follow'),
333 '$follow_link' => $follow_link,
334 '$unfollow' => DI::l10n()->t('Unfollow'),
335 '$unfollow_link' => $unfollow_link,
336 '$wallmessage' => DI::l10n()->t('Message'),
337 '$wallmessage_link' => $wallmessage_link,
340 $findpeople_widget = '';
342 $networks_widget = '';
345 if ($contact['uid'] != 0) {
346 $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
352 $findpeople_widget = Widget::findPeople();
353 if (isset($_GET['add'])) {
354 $follow_widget = Widget::follow($_GET['add']);
356 $follow_widget = Widget::follow();
359 $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
360 $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
361 $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group);
364 DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $groups_widget . $networks_widget . $rel_widget;
366 $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
367 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
368 '$baseurl' => DI::baseUrl()->get(true),
372 Nav::setSelected('contact');
375 notice(DI::l10n()->t('Permission denied.'));
376 return Login::form();
380 $contact_id = intval($a->argv[1]);
382 throw new BadRequestException();
385 // @TODO: Replace with parameter from router
388 $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
389 if (!DBA::isResult($orig_record)) {
390 throw new NotFoundException(DI::l10n()->t('Contact not found'));
393 if ($cmd === 'update' && ($orig_record['uid'] != 0)) {
394 self::updateContactFromPoll($contact_id);
395 DI::baseUrl()->redirect('contact/' . $contact_id);
399 if ($cmd === 'updateprofile') {
400 self::updateContactFromProbe($contact_id);
401 DI::baseUrl()->redirect('contact/' . $contact_id);
405 if ($cmd === 'block') {
406 self::blockContact($contact_id);
408 $blocked = Model\Contact\User::isBlocked($contact_id, local_user());
409 info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')));
411 DI::baseUrl()->redirect('contact/' . $contact_id);
415 if ($cmd === 'ignore') {
416 self::ignoreContact($contact_id);
418 $ignored = Model\Contact\User::isIgnored($contact_id, local_user());
419 info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')));
421 DI::baseUrl()->redirect('contact/' . $contact_id);
425 if ($cmd === 'archive' && ($orig_record['uid'] != 0)) {
426 $r = self::archiveContact($contact_id, $orig_record);
428 $archived = (($orig_record['archive']) ? 0 : 1);
429 info((($archived) ? DI::l10n()->t('Contact has been archived') : DI::l10n()->t('Contact has been unarchived')));
432 DI::baseUrl()->redirect('contact/' . $contact_id);
436 if ($cmd === 'drop' && ($orig_record['uid'] != 0)) {
437 // Check if we should do HTML-based delete confirmation
438 if (!empty($_REQUEST['confirm'])) {
439 // <form> can't take arguments in its 'action' parameter
440 // so add any arguments as hidden inputs
441 $query = explode_querystring(DI::args()->getQueryString());
443 foreach ($query['args'] as $arg) {
444 if (strpos($arg, 'confirm=') === false) {
445 $arg_parts = explode('=', $arg);
446 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
450 DI::page()['aside'] = '';
452 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
453 '$header' => DI::l10n()->t('Drop contact'),
454 '$contact' => self::getContactTemplateVars($orig_record),
456 '$message' => DI::l10n()->t('Do you really want to delete this contact?'),
457 '$extra_inputs' => $inputs,
458 '$confirm' => DI::l10n()->t('Yes'),
459 '$confirm_url' => $query['base'],
460 '$confirm_name' => 'confirmed',
461 '$cancel' => DI::l10n()->t('Cancel'),
464 // Now check how the user responded to the confirmation query
465 if (!empty($_REQUEST['canceled'])) {
466 DI::baseUrl()->redirect('contact');
469 self::dropContact($orig_record);
470 info(DI::l10n()->t('Contact has been removed.'));
472 DI::baseUrl()->redirect('contact');
475 if ($cmd === 'posts') {
476 return self::getPostsHTML($a, $contact_id);
478 if ($cmd === 'conversations') {
479 return self::getConversationsHMTL($a, $contact_id, $update);
483 $_SESSION['return_path'] = DI::args()->getQueryString();
485 if (!empty($a->data['contact']) && is_array($a->data['contact'])) {
486 $contact = $a->data['contact'];
488 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
489 '$baseurl' => DI::baseUrl()->get(true),
492 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
493 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
496 switch ($contact['rel']) {
497 case Model\Contact::FRIEND:
498 $relation_text = DI::l10n()->t('You are mutual friends with %s');
501 case Model\Contact::FOLLOWER;
502 $relation_text = DI::l10n()->t('You are sharing with %s');
505 case Model\Contact::SHARING;
506 $relation_text = DI::l10n()->t('%s is sharing with you');
513 if ($contact['uid'] == 0) {
517 if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
521 $relation_text = sprintf($relation_text, $contact['name']);
523 $url = Model\Contact::magicLink($contact['url']);
524 if (strpos($url, 'redir/') === 0) {
525 $sparkle = ' class="sparkle" ';
530 $insecure = DI::l10n()->t('Private communications are not available for this contact.');
532 $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? DI::l10n()->t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
534 if ($contact['last-update'] > DBA::NULL_DATETIME) {
535 $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? DI::l10n()->t('(Update was successful)') : DI::l10n()->t('(Update was not successful)'));
537 $lblsuggest = (($contact['network'] === Protocol::DFRN) ? DI::l10n()->t('Suggest friends') : '');
539 $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
541 $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']));
544 $tab_str = self::getTabsHTML($contact, self::TAB_PROFILE);
546 $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : '');
548 $fetch_further_information = null;
549 if ($contact['network'] == Protocol::FEED) {
550 $fetch_further_information = [
551 'fetch_further_information',
552 DI::l10n()->t('Fetch further information for feeds'),
553 $contact['fetch_further_information'],
554 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.'),
556 '0' => DI::l10n()->t('Disabled'),
557 '1' => DI::l10n()->t('Fetch information'),
558 '3' => DI::l10n()->t('Fetch keywords'),
559 '2' => DI::l10n()->t('Fetch information and keywords')
564 $poll_interval = null;
565 if ((($contact['network'] == Protocol::FEED) && !DI::config()->get('system', 'adjust_poll_frequency')) || ($contact['network']== Protocol::MAIL)) {
566 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
569 // Load contactact related actions like hide, suggest, delete and others
570 $contact_actions = self::getContactActions($contact);
572 if ($contact['uid'] != 0) {
573 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
574 $contact_settings_label = DI::l10n()->t('Contact Settings');
577 $contact_settings_label = null;
580 $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
581 $o .= Renderer::replaceMacros($tpl, [
582 '$header' => DI::l10n()->t('Contact'),
583 '$tab_str' => $tab_str,
584 '$submit' => DI::l10n()->t('Submit'),
585 '$lbl_info1' => $lbl_info1,
586 '$lbl_info2' => DI::l10n()->t('Their personal note'),
587 '$reason' => trim(Strings::escapeTags($contact['reason'])),
588 '$infedit' => DI::l10n()->t('Edit contact notes'),
589 '$common_link' => 'contact/' . $contact['id'] . '/contacts/common',
590 '$relation_text' => $relation_text,
591 '$visit' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
592 '$blockunblock' => DI::l10n()->t('Block/Unblock contact'),
593 '$ignorecont' => DI::l10n()->t('Ignore contact'),
594 '$lblrecent' => DI::l10n()->t('View conversations'),
595 '$lblsuggest' => $lblsuggest,
596 '$nettype' => $nettype,
597 '$poll_interval' => $poll_interval,
598 '$poll_enabled' => $poll_enabled,
599 '$lastupdtext' => DI::l10n()->t('Last update:'),
600 '$lost_contact' => $lost_contact,
601 '$updpub' => DI::l10n()->t('Update public posts'),
602 '$last_update' => $last_update,
603 '$udnow' => DI::l10n()->t('Update now'),
604 '$contact_id' => $contact['id'],
605 '$block_text' => ($contact['blocked'] ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
606 '$ignore_text' => ($contact['readonly'] ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
607 '$insecure' => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
608 '$info' => $contact['info'],
609 '$cinfo' => ['info', '', $contact['info'], ''],
610 '$blocked' => ($contact['blocked'] ? DI::l10n()->t('Currently blocked') : ''),
611 '$ignored' => ($contact['readonly'] ? DI::l10n()->t('Currently ignored') : ''),
612 '$archived' => ($contact['archive'] ? DI::l10n()->t('Currently archived') : ''),
613 '$pending' => ($contact['pending'] ? DI::l10n()->t('Awaiting connection acknowledge') : ''),
614 '$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')],
615 '$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')],
616 '$fetch_further_information' => $fetch_further_information,
617 '$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')],
618 '$photo' => Model\Contact::getPhoto($contact),
619 '$name' => $contact['name'],
620 '$sparkle' => $sparkle,
622 '$profileurllabel'=> DI::l10n()->t('Profile URL'),
623 '$profileurl' => $contact['url'],
624 '$account_type' => Model\Contact::getAccountType($contact),
625 '$location' => BBCode::convert($contact['location']),
626 '$location_label' => DI::l10n()->t('Location:'),
627 '$xmpp' => BBCode::convert($contact['xmpp']),
628 '$xmpp_label' => DI::l10n()->t('XMPP:'),
629 '$about' => BBCode::convert($contact['about'], false),
630 '$about_label' => DI::l10n()->t('About:'),
631 '$keywords' => $contact['keywords'],
632 '$keywords_label' => DI::l10n()->t('Tags:'),
633 '$contact_action_button' => DI::l10n()->t('Actions'),
634 '$contact_actions'=> $contact_actions,
635 '$contact_status' => DI::l10n()->t('Status'),
636 '$contact_settings_label' => $contact_settings_label,
637 '$contact_profile_label' => DI::l10n()->t('Profile'),
640 $arr = ['contact' => $contact, 'output' => $o];
642 Hook::callAll('contact_edit', $arr);
644 return $arr['output'];
647 $sql_values = [local_user()];
649 // @TODO: Replace with parameter from router
650 $type = $a->argv[1] ?? '';
654 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
655 // This makes the query look for contact.uid = 0
656 array_unshift($sql_values, 0);
659 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
662 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
663 // This makes the query look for contact.uid = 0
664 array_unshift($sql_values, 0);
667 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
670 $sql_extra = " AND `pending` AND NOT `archive` AND ((`rel` = ?)
671 OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
672 $sql_values[] = Model\Contact::SHARING;
675 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
683 $search_hdr = $search;
684 $search_txt = preg_quote($search);
685 $sql_extra .= " AND (name REGEXP ? OR url REGEXP ? OR nick REGEXP ?)";
686 $sql_values[] = $search_txt;
687 $sql_values[] = $search_txt;
688 $sql_values[] = $search_txt;
692 $sql_extra .= " AND network = ? ";
693 $sql_values[] = $nets;
698 $sql_extra .= " AND `rel` IN (?, ?)";
699 $sql_values[] = Model\Contact::FOLLOWER;
700 $sql_values[] = Model\Contact::FRIEND;
703 $sql_extra .= " AND `rel` IN (?, ?)";
704 $sql_values[] = Model\Contact::SHARING;
705 $sql_values[] = Model\Contact::FRIEND;
708 $sql_extra .= " AND `rel` = ?";
709 $sql_values[] = Model\Contact::FRIEND;
714 $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
715 $sql_values[] = $group;
719 $stmt = DBA::p("SELECT COUNT(*) AS `total`
725 " . Widget::unavailableNetworks(),
728 if (DBA::isResult($stmt)) {
729 $total = DBA::fetch($stmt)['total'];
733 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
735 $sql_values[] = $pager->getStart();
736 $sql_values[] = $pager->getItemsPerPage();
740 $stmt = DBA::p("SELECT *
750 while ($contact = DBA::fetch($stmt)) {
751 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
752 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
753 $contacts[] = self::getContactTemplateVars($contact);
759 'label' => DI::l10n()->t('All Contacts'),
761 'sel' => !$type ? 'active' : '',
762 'title' => DI::l10n()->t('Show all contacts'),
763 'id' => 'showall-tab',
767 'label' => DI::l10n()->t('Pending'),
768 'url' => 'contact/pending',
769 'sel' => $type == 'pending' ? 'active' : '',
770 'title' => DI::l10n()->t('Only show pending contacts'),
771 'id' => 'showpending-tab',
775 'label' => DI::l10n()->t('Blocked'),
776 'url' => 'contact/blocked',
777 'sel' => $type == 'blocked' ? 'active' : '',
778 'title' => DI::l10n()->t('Only show blocked contacts'),
779 'id' => 'showblocked-tab',
783 'label' => DI::l10n()->t('Ignored'),
784 'url' => 'contact/ignored',
785 'sel' => $type == 'ignored' ? 'active' : '',
786 'title' => DI::l10n()->t('Only show ignored contacts'),
787 'id' => 'showignored-tab',
791 'label' => DI::l10n()->t('Archived'),
792 'url' => 'contact/archived',
793 'sel' => $type == 'archived' ? 'active' : '',
794 'title' => DI::l10n()->t('Only show archived contacts'),
795 'id' => 'showarchived-tab',
799 'label' => DI::l10n()->t('Hidden'),
800 'url' => 'contact/hidden',
801 'sel' => $type == 'hidden' ? 'active' : '',
802 'title' => DI::l10n()->t('Only show hidden contacts'),
803 'id' => 'showhidden-tab',
807 'label' => DI::l10n()->t('Groups'),
810 'title' => DI::l10n()->t('Organize your contact groups'),
811 'id' => 'contactgroups-tab',
816 $tabs_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
817 $tabs_html = Renderer::replaceMacros($tabs_tpl, ['$tabs' => $tabs]);
820 case 'followers': $header = DI::l10n()->t('Followers'); break;
821 case 'following': $header = DI::l10n()->t('Following'); break;
822 case 'mutuals': $header = DI::l10n()->t('Mutual friends'); break;
823 default: $header = DI::l10n()->t('Contacts');
827 case 'pending': $header .= ' - ' . DI::l10n()->t('Pending'); break;
828 case 'blocked': $header .= ' - ' . DI::l10n()->t('Blocked'); break;
829 case 'hidden': $header .= ' - ' . DI::l10n()->t('Hidden'); break;
830 case 'ignored': $header .= ' - ' . DI::l10n()->t('Ignored'); break;
831 case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
834 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
836 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
837 $o .= Renderer::replaceMacros($tpl, [
838 '$header' => $header,
839 '$tabs' => $tabs_html,
841 '$search' => $search_hdr,
842 '$desc' => DI::l10n()->t('Search your contacts'),
843 '$finding' => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
844 '$submit' => DI::l10n()->t('Find'),
845 '$cmd' => DI::args()->getCommand(),
846 '$contacts' => $contacts,
847 '$contact_drop_confirm' => DI::l10n()->t('Do you really want to delete this contact?'),
849 '$batch_actions' => [
850 'contacts_batch_update' => DI::l10n()->t('Update'),
851 'contacts_batch_block' => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
852 'contacts_batch_ignore' => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
853 'contacts_batch_archive' => DI::l10n()->t('Archive') . '/' . DI::l10n()->t('Unarchive'),
854 'contacts_batch_drop' => DI::l10n()->t('Delete'),
856 '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
857 '$paginate' => $pager->renderFull($total),
864 * List of pages for the Contact TabBar
866 * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
868 * @param array $contact The contact array
869 * @param int $active_tab 1 if tab should be marked as active
871 * @return string HTML string of the contact page tabs buttons.
872 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
873 * @throws \ImagickException
875 public static function getTabsHTML(array $contact, int $active_tab)
877 $cid = $pcid = $contact['id'];
878 $data = Model\Contact::getPublicAndUserContacID($contact['id'], local_user());
879 if (!empty($data['user']) && ($contact['id'] == $data['public'])) {
880 $cid = $data['user'];
881 } elseif (!empty($data['public'])) {
882 $pcid = $data['public'];
888 'label' => DI::l10n()->t('Status'),
889 'url' => 'contact/' . $pcid . '/conversations',
890 'sel' => (($active_tab == self::TAB_CONVERSATIONS) ? 'active' : ''),
891 'title' => DI::l10n()->t('Conversations started by this contact'),
892 'id' => 'status-tab',
896 'label' => DI::l10n()->t('Posts and Comments'),
897 'url' => 'contact/' . $pcid . '/posts',
898 'sel' => (($active_tab == self::TAB_POSTS) ? 'active' : ''),
899 'title' => DI::l10n()->t('Status Messages and Posts'),
904 'label' => DI::l10n()->t('Profile'),
905 'url' => 'contact/' . $cid,
906 'sel' => (($active_tab == self::TAB_PROFILE) ? 'active' : ''),
907 'title' => DI::l10n()->t('Profile Details'),
908 'id' => 'profile-tab',
911 ['label' => DI::l10n()->t('Contacts'),
912 'url' => 'contact/' . $pcid . '/contacts',
913 'sel' => (($active_tab == self::TAB_CONTACTS) ? 'active' : ''),
914 'title' => DI::l10n()->t('View all known contacts'),
915 'id' => 'contacts-tab',
921 $tabs[] = ['label' => DI::l10n()->t('Advanced'),
922 'url' => 'contact/' . $cid . '/advanced/',
923 'sel' => (($active_tab == self::TAB_ADVANCED) ? 'active' : ''),
924 'title' => DI::l10n()->t('Advanced Contact Settings'),
925 'id' => 'advanced-tab',
930 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
931 $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
936 private static function getConversationsHMTL($a, $contact_id, $update)
941 // We need the editor here to be able to reshare an item.
945 'allow_location' => $a->user['allow_location'],
946 'default_location' => $a->user['default-location'],
947 'nickname' => $a->user['nickname'],
948 'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
949 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
951 'visitor' => 'block',
952 'profile_uid' => local_user(),
954 $o = status_editor($a, $x, 0, true);
958 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
961 $o .= self::getTabsHTML($contact, self::TAB_CONVERSATIONS);
964 if (DBA::isResult($contact)) {
965 DI::page()['aside'] = '';
967 $profiledata = Model\Contact::getByURL($contact['url'], false);
969 Model\Profile::load($a, '', $profiledata, true);
971 if ($contact['uid'] == 0) {
972 $o .= Model\Contact::getPostsFromId($contact['id'], true, $update);
974 $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update);
981 private static function getPostsHTML($a, $contact_id)
983 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
985 $o = self::getTabsHTML($contact, self::TAB_POSTS);
987 if (DBA::isResult($contact)) {
988 DI::page()['aside'] = '';
990 $profiledata = Model\Contact::getByURL($contact['url'], false);
992 if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
993 $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
996 Model\Profile::load($a, '', $profiledata, true);
998 if ($contact['uid'] == 0) {
999 $o .= Model\Contact::getPostsFromId($contact['id']);
1001 $o .= Model\Contact::getPostsFromUrl($contact['url']);
1009 * Return the fields for the contact template
1011 * @param array $contact Contact array
1012 * @return array Template fields
1014 public static function getContactTemplateVars(array $contact)
1018 if (!empty($contact['url']) && isset($contact['uid']) && ($contact['uid'] == 0) && local_user()) {
1019 $personal = Model\Contact::getByURL($contact['url'], false, ['uid', 'rel', 'self'], local_user());
1020 if (!empty($personal)) {
1021 $contact['uid'] = $personal['uid'];
1022 $contact['rel'] = $personal['rel'];
1023 $contact['self'] = $personal['self'];
1027 if (!empty($contact['uid']) && !empty($contact['rel']) && local_user() == $contact['uid']) {
1028 switch ($contact['rel']) {
1029 case Model\Contact::FRIEND:
1030 $alt_text = DI::l10n()->t('Mutual Friendship');
1033 case Model\Contact::FOLLOWER;
1034 $alt_text = DI::l10n()->t('is a fan of yours');
1037 case Model\Contact::SHARING;
1038 $alt_text = DI::l10n()->t('you are a fan of');
1046 $url = Model\Contact::magicLink($contact['url']);
1048 if (strpos($url, 'redir/') === 0) {
1049 $sparkle = ' class="sparkle" ';
1054 if ($contact['pending']) {
1055 if (in_array($contact['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
1056 $alt_text = DI::l10n()->t('Pending outgoing contact request');
1058 $alt_text = DI::l10n()->t('Pending incoming contact request');
1062 if ($contact['self']) {
1063 $alt_text = DI::l10n()->t('This is you');
1064 $url = $contact['url'];
1069 'id' => $contact['id'],
1071 'img_hover' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
1072 'photo_menu' => Model\Contact::photoMenu($contact),
1073 'thumb' => Model\Contact::getThumb($contact),
1074 'alt_text' => $alt_text,
1075 'name' => $contact['name'],
1076 'nick' => $contact['nick'],
1077 'details' => $contact['location'],
1078 'tags' => $contact['keywords'],
1079 'about' => $contact['about'],
1080 'account_type' => Model\Contact::getAccountType($contact),
1081 'sparkle' => $sparkle,
1082 'itemurl' => ($contact['addr'] ?? '') ?: $contact['url'],
1083 'network' => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']),
1088 * Gives a array with actions which can performed to a given contact
1090 * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
1092 * @param array $contact Data about the Contact
1093 * @return array with contact related actions
1095 private static function getContactActions($contact)
1097 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
1098 $contact_actions = [];
1100 // Provide friend suggestion only for Friendica contacts
1101 if ($contact['network'] === Protocol::DFRN) {
1102 $contact_actions['suggest'] = [
1103 'label' => DI::l10n()->t('Suggest friends'),
1104 'url' => 'fsuggest/' . $contact['id'],
1111 if ($poll_enabled) {
1112 $contact_actions['update'] = [
1113 'label' => DI::l10n()->t('Update now'),
1114 'url' => 'contact/' . $contact['id'] . '/update',
1121 if (in_array($contact['network'], Protocol::FEDERATED)) {
1122 $contact_actions['updateprofile'] = [
1123 'label' => DI::l10n()->t('Refetch contact data'),
1124 'url' => 'contact/' . $contact['id'] . '/updateprofile',
1127 'id' => 'updateprofile',
1131 $contact_actions['block'] = [
1132 'label' => (intval($contact['blocked']) ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
1133 'url' => 'contact/' . $contact['id'] . '/block',
1134 'title' => DI::l10n()->t('Toggle Blocked status'),
1135 'sel' => (intval($contact['blocked']) ? 'active' : ''),
1136 'id' => 'toggle-block',
1139 $contact_actions['ignore'] = [
1140 'label' => (intval($contact['readonly']) ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
1141 'url' => 'contact/' . $contact['id'] . '/ignore',
1142 'title' => DI::l10n()->t('Toggle Ignored status'),
1143 'sel' => (intval($contact['readonly']) ? 'active' : ''),
1144 'id' => 'toggle-ignore',
1147 if ($contact['uid'] != 0) {
1148 $contact_actions['archive'] = [
1149 'label' => (intval($contact['archive']) ? DI::l10n()->t('Unarchive') : DI::l10n()->t('Archive')),
1150 'url' => 'contact/' . $contact['id'] . '/archive',
1151 'title' => DI::l10n()->t('Toggle Archive status'),
1152 'sel' => (intval($contact['archive']) ? 'active' : ''),
1153 'id' => 'toggle-archive',
1156 $contact_actions['delete'] = [
1157 'label' => DI::l10n()->t('Delete'),
1158 'url' => 'contact/' . $contact['id'] . '/drop',
1159 'title' => DI::l10n()->t('Delete contact'),
1165 return $contact_actions;