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\Worker;
36 use Friendica\Database\DBA;
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 private static function batchActions()
52 if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
56 $contacts_id = $_POST['contact_batch'];
58 $stmt = DBA::select('contact', ['id', 'archive'], ['id' => $contacts_id, 'uid' => local_user(), 'self' => false, 'deleted' => false]);
59 $orig_records = DBA::toArray($stmt);
62 foreach ($orig_records as $orig_record) {
63 $contact_id = $orig_record['id'];
64 if (!empty($_POST['contacts_batch_update'])) {
65 self::updateContactFromPoll($contact_id);
68 if (!empty($_POST['contacts_batch_block'])) {
69 self::blockContact($contact_id);
72 if (!empty($_POST['contacts_batch_ignore'])) {
73 self::ignoreContact($contact_id);
76 if (!empty($_POST['contacts_batch_archive'])
77 && self::archiveContact($contact_id, $orig_record)
81 if (!empty($_POST['contacts_batch_drop'])) {
82 self::dropContact($orig_record);
86 if ($count_actions > 0) {
87 info(DI::l10n()->tt('%d contact edited.', '%d contacts edited.', $count_actions));
90 DI::baseUrl()->redirect('contact');
93 public static function post(array $parameters = [])
101 // @TODO: Replace with parameter from router
102 if ($a->argv[1] === 'batch') {
103 self::batchActions();
107 // @TODO: Replace with parameter from router
108 $contact_id = intval($a->argv[1]);
113 if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
114 notice(DI::l10n()->t('Could not access contact record.'));
115 DI::baseUrl()->redirect('contact');
116 return; // NOTREACHED
119 Hook::callAll('contact_edit_post', $_POST);
121 $hidden = !empty($_POST['hidden']);
123 $notify = !empty($_POST['notify']);
125 $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
127 $ffi_keyword_denylist = Strings::escapeHtml(trim($_POST['ffi_keyword_denylist'] ?? ''));
129 $priority = intval($_POST['poll'] ?? 0);
130 if ($priority > 5 || $priority < 0) {
134 $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
136 $r = DBA::update('contact', [
137 'priority' => $priority,
140 'notify_new_posts' => $notify,
141 'fetch_further_information' => $fetch_further_information,
142 'ffi_keyword_denylist' => $ffi_keyword_denylist],
143 ['id' => $contact_id, 'uid' => local_user()]
146 if (!DBA::isResult($r)) {
147 notice(DI::l10n()->t('Failed to update contact record.'));
150 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
151 if (DBA::isResult($contact)) {
152 $a->data['contact'] = $contact;
158 /* contact actions */
160 private static function updateContactFromPoll($contact_id)
162 $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
163 if (!DBA::isResult($contact)) {
167 if ($contact['network'] == Protocol::OSTATUS) {
168 $user = Model\User::getById($contact['uid']);
169 $result = Model\Contact::createFromProbe($user, $contact['url'], false, $contact['network']);
171 if ($result['success']) {
172 DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
175 // pull feed and consume it, which should subscribe to the hub.
176 Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
180 private static function updateContactFromProbe($contact_id)
182 $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
183 if (!DBA::isResult($contact)) {
187 // Update the entry in the contact table
188 Model\Contact::updateFromProbe($contact_id, '', true);
190 // Update the entry in the gcontact table
191 Model\GContact::updateFromProbe($contact['url']);
195 * Toggles the blocked status of a contact identified by id.
200 private static function blockContact($contact_id)
202 $blocked = !Model\Contact::isBlockedByUser($contact_id, local_user());
203 Model\Contact::setBlockedForUser($contact_id, local_user(), $blocked);
207 * Toggles the ignored status of a contact identified by id.
212 private static function ignoreContact($contact_id)
214 $ignored = !Model\Contact::isIgnoredByUser($contact_id, local_user());
215 Model\Contact::setIgnoredForUser($contact_id, local_user(), $ignored);
219 * Toggles the archived status of a contact identified by id.
220 * If the current status isn't provided, this will always archive the contact.
223 * @param $orig_record
227 private static function archiveContact($contact_id, $orig_record)
229 $archived = empty($orig_record['archive']);
230 $r = DBA::update('contact', ['archive' => $archived], ['id' => $contact_id, 'uid' => local_user()]);
232 return DBA::isResult($r);
235 private static function dropContact($orig_record)
237 $owner = Model\User::getOwnerDataById(local_user());
238 if (!DBA::isResult($owner)) {
242 Model\Contact::terminateFriendship($owner, $orig_record, true);
243 Model\Contact::remove($orig_record['id']);
246 public static function content(array $parameters = [], $update = 0)
249 return Login::form($_SERVER['REQUEST_URI']);
254 $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
255 $nets = Strings::escapeTags(trim($_GET['nets'] ?? ''));
256 $rel = Strings::escapeTags(trim($_GET['rel'] ?? ''));
257 $group = Strings::escapeTags(trim($_GET['group'] ?? ''));
259 if (empty(DI::page()['aside'])) {
260 DI::page()['aside'] = '';
265 // @TODO: Replace with parameter from router
266 if ($a->argc == 2 && intval($a->argv[1])
267 || $a->argc == 3 && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])
269 $contact_id = intval($a->argv[1]);
270 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
272 if (!DBA::isResult($contact)) {
273 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => 0, 'deleted' => false]);
276 // Don't display contacts that are about to be deleted
277 if ($contact['network'] == Protocol::PHANTOM) {
282 if (DBA::isResult($contact)) {
283 if ($contact['self']) {
284 // @TODO: Replace with parameter from router
285 if (($a->argc == 3) && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])) {
286 DI::baseUrl()->redirect('profile/' . $contact['nick']);
288 DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile');
292 $a->data['contact'] = $contact;
294 if (($contact['network'] != '') && ($contact['network'] != Protocol::DFRN)) {
295 $network_link = Strings::formatNetworkName($contact['network'], $contact['url']);
302 if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
303 if ($contact['uid'] && in_array($contact['rel'], [Model\Contact::SHARING, Model\Contact::FRIEND])) {
304 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']);
305 } elseif(!$contact['pending']) {
306 $follow_link = 'follow?url=' . urlencode($contact['url']);
310 $wallmessage_link = '';
311 if ($contact['uid'] && Model\Contact::canReceivePrivateMessages($contact)) {
312 $wallmessage_link = 'message/new/' . $contact['id'];
315 $vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
316 '$name' => $contact['name'],
317 '$photo' => Model\Contact::getPhoto($contact),
318 '$url' => Model\Contact::magicLinkByContact($contact, $contact['url']),
319 '$addr' => $contact['addr'] ?? '',
320 '$network_link' => $network_link,
321 '$network' => DI::l10n()->t('Network:'),
322 '$account_type' => Model\Contact::getAccountType($contact),
323 '$follow' => DI::l10n()->t('Follow'),
324 '$follow_link' => $follow_link,
325 '$unfollow' => DI::l10n()->t('Unfollow'),
326 '$unfollow_link' => $unfollow_link,
327 '$wallmessage' => DI::l10n()->t('Message'),
328 '$wallmessage_link' => $wallmessage_link,
331 $findpeople_widget = '';
333 $networks_widget = '';
336 if ($contact['uid'] != 0) {
337 $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
343 $findpeople_widget = Widget::findPeople();
344 if (isset($_GET['add'])) {
345 $follow_widget = Widget::follow($_GET['add']);
347 $follow_widget = Widget::follow();
350 $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
351 $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
352 $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group);
355 DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $groups_widget . $networks_widget . $rel_widget;
357 $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
358 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
359 '$baseurl' => DI::baseUrl()->get(true),
363 Nav::setSelected('contact');
366 notice(DI::l10n()->t('Permission denied.'));
367 return Login::form();
371 $contact_id = intval($a->argv[1]);
373 throw new BadRequestException();
376 // @TODO: Replace with parameter from router
379 $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
380 if (!DBA::isResult($orig_record)) {
381 throw new NotFoundException(DI::l10n()->t('Contact not found'));
384 if ($cmd === 'update' && ($orig_record['uid'] != 0)) {
385 self::updateContactFromPoll($contact_id);
386 DI::baseUrl()->redirect('contact/' . $contact_id);
390 if ($cmd === 'updateprofile' && ($orig_record['uid'] != 0)) {
391 self::updateContactFromProbe($contact_id);
392 DI::baseUrl()->redirect('contact/' . $contact_id . '/advanced/');
396 if ($cmd === 'block') {
397 self::blockContact($contact_id);
399 $blocked = Model\Contact::isBlockedByUser($contact_id, local_user());
400 info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')));
402 DI::baseUrl()->redirect('contact/' . $contact_id);
406 if ($cmd === 'ignore') {
407 self::ignoreContact($contact_id);
409 $ignored = Model\Contact::isIgnoredByUser($contact_id, local_user());
410 info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')));
412 DI::baseUrl()->redirect('contact/' . $contact_id);
416 if ($cmd === 'archive' && ($orig_record['uid'] != 0)) {
417 $r = self::archiveContact($contact_id, $orig_record);
419 $archived = (($orig_record['archive']) ? 0 : 1);
420 info((($archived) ? DI::l10n()->t('Contact has been archived') : DI::l10n()->t('Contact has been unarchived')));
423 DI::baseUrl()->redirect('contact/' . $contact_id);
427 if ($cmd === 'drop' && ($orig_record['uid'] != 0)) {
428 // Check if we should do HTML-based delete confirmation
429 if (!empty($_REQUEST['confirm'])) {
430 // <form> can't take arguments in its 'action' parameter
431 // so add any arguments as hidden inputs
432 $query = explode_querystring(DI::args()->getQueryString());
434 foreach ($query['args'] as $arg) {
435 if (strpos($arg, 'confirm=') === false) {
436 $arg_parts = explode('=', $arg);
437 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
441 DI::page()['aside'] = '';
443 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
444 '$header' => DI::l10n()->t('Drop contact'),
445 '$contact' => self::getContactTemplateVars($orig_record),
447 '$message' => DI::l10n()->t('Do you really want to delete this contact?'),
448 '$extra_inputs' => $inputs,
449 '$confirm' => DI::l10n()->t('Yes'),
450 '$confirm_url' => $query['base'],
451 '$confirm_name' => 'confirmed',
452 '$cancel' => DI::l10n()->t('Cancel'),
455 // Now check how the user responded to the confirmation query
456 if (!empty($_REQUEST['canceled'])) {
457 DI::baseUrl()->redirect('contact');
460 self::dropContact($orig_record);
461 info(DI::l10n()->t('Contact has been removed.'));
463 DI::baseUrl()->redirect('contact');
466 if ($cmd === 'posts') {
467 return self::getPostsHTML($a, $contact_id);
469 if ($cmd === 'conversations') {
470 return self::getConversationsHMTL($a, $contact_id, $update);
474 $_SESSION['return_path'] = DI::args()->getQueryString();
476 if (!empty($a->data['contact']) && is_array($a->data['contact'])) {
477 $contact = $a->data['contact'];
479 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
480 '$baseurl' => DI::baseUrl()->get(true),
483 $contact['blocked'] = Model\Contact::isBlockedByUser($contact['id'], local_user());
484 $contact['readonly'] = Model\Contact::isIgnoredByUser($contact['id'], local_user());
488 switch ($contact['rel']) {
489 case Model\Contact::FRIEND:
490 $dir_icon = 'images/lrarrow.gif';
491 $relation_text = DI::l10n()->t('You are mutual friends with %s');
494 case Model\Contact::FOLLOWER;
495 $dir_icon = 'images/larrow.gif';
496 $relation_text = DI::l10n()->t('You are sharing with %s');
499 case Model\Contact::SHARING;
500 $dir_icon = 'images/rarrow.gif';
501 $relation_text = DI::l10n()->t('%s is sharing with you');
508 if ($contact['uid'] == 0) {
512 if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
516 $relation_text = sprintf($relation_text, $contact['name']);
518 $url = Model\Contact::magicLink($contact['url']);
519 if (strpos($url, 'redir/') === 0) {
520 $sparkle = ' class="sparkle" ';
525 $insecure = DI::l10n()->t('Private communications are not available for this contact.');
527 $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? DI::l10n()->t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
529 if ($contact['last-update'] > DBA::NULL_DATETIME) {
530 $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? DI::l10n()->t('(Update was successful)') : DI::l10n()->t('(Update was not successful)'));
532 $lblsuggest = (($contact['network'] === Protocol::DFRN) ? DI::l10n()->t('Suggest friends') : '');
534 $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
536 $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']));
539 $tab_str = self::getTabsHTML($a, $contact, 3);
541 $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : '');
543 $fetch_further_information = null;
544 if ($contact['network'] == Protocol::FEED) {
545 $fetch_further_information = [
546 'fetch_further_information',
547 DI::l10n()->t('Fetch further information for feeds'),
548 $contact['fetch_further_information'],
549 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.'),
551 '0' => DI::l10n()->t('Disabled'),
552 '1' => DI::l10n()->t('Fetch information'),
553 '3' => DI::l10n()->t('Fetch keywords'),
554 '2' => DI::l10n()->t('Fetch information and keywords')
559 $poll_interval = null;
560 if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
561 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
564 // Load contactact related actions like hide, suggest, delete and others
565 $contact_actions = self::getContactActions($contact);
567 if ($contact['uid'] != 0) {
568 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
569 $contact_settings_label = DI::l10n()->t('Contact Settings');
572 $contact_settings_label = null;
575 $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
576 $o .= Renderer::replaceMacros($tpl, [
577 '$header' => DI::l10n()->t('Contact'),
578 '$tab_str' => $tab_str,
579 '$submit' => DI::l10n()->t('Submit'),
580 '$lbl_info1' => $lbl_info1,
581 '$lbl_info2' => DI::l10n()->t('Their personal note'),
582 '$reason' => trim(Strings::escapeTags($contact['reason'])),
583 '$infedit' => DI::l10n()->t('Edit contact notes'),
584 '$common_link' => 'common/loc/' . local_user() . '/' . $contact['id'],
585 '$relation_text' => $relation_text,
586 '$visit' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
587 '$blockunblock' => DI::l10n()->t('Block/Unblock contact'),
588 '$ignorecont' => DI::l10n()->t('Ignore contact'),
589 '$lblrecent' => DI::l10n()->t('View conversations'),
590 '$lblsuggest' => $lblsuggest,
591 '$nettype' => $nettype,
592 '$poll_interval' => $poll_interval,
593 '$poll_enabled' => $poll_enabled,
594 '$lastupdtext' => DI::l10n()->t('Last update:'),
595 '$lost_contact' => $lost_contact,
596 '$updpub' => DI::l10n()->t('Update public posts'),
597 '$last_update' => $last_update,
598 '$udnow' => DI::l10n()->t('Update now'),
599 '$contact_id' => $contact['id'],
600 '$block_text' => ($contact['blocked'] ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
601 '$ignore_text' => ($contact['readonly'] ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
602 '$insecure' => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
603 '$info' => $contact['info'],
604 '$cinfo' => ['info', '', $contact['info'], ''],
605 '$blocked' => ($contact['blocked'] ? DI::l10n()->t('Currently blocked') : ''),
606 '$ignored' => ($contact['readonly'] ? DI::l10n()->t('Currently ignored') : ''),
607 '$archived' => ($contact['archive'] ? DI::l10n()->t('Currently archived') : ''),
608 '$pending' => ($contact['pending'] ? DI::l10n()->t('Awaiting connection acknowledge') : ''),
609 '$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')],
610 '$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')],
611 '$fetch_further_information' => $fetch_further_information,
612 '$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')],
613 '$photo' => Model\Contact::getPhoto($contact),
614 '$name' => $contact['name'],
615 '$dir_icon' => $dir_icon,
616 '$sparkle' => $sparkle,
618 '$profileurllabel'=> DI::l10n()->t('Profile URL'),
619 '$profileurl' => $contact['url'],
620 '$account_type' => Model\Contact::getAccountType($contact),
621 '$location' => BBCode::convert($contact['location']),
622 '$location_label' => DI::l10n()->t('Location:'),
623 '$xmpp' => BBCode::convert($contact['xmpp']),
624 '$xmpp_label' => DI::l10n()->t('XMPP:'),
625 '$about' => BBCode::convert($contact['about'], false),
626 '$about_label' => DI::l10n()->t('About:'),
627 '$keywords' => $contact['keywords'],
628 '$keywords_label' => DI::l10n()->t('Tags:'),
629 '$contact_action_button' => DI::l10n()->t('Actions'),
630 '$contact_actions'=> $contact_actions,
631 '$contact_status' => DI::l10n()->t('Status'),
632 '$contact_settings_label' => $contact_settings_label,
633 '$contact_profile_label' => DI::l10n()->t('Profile'),
636 $arr = ['contact' => $contact, 'output' => $o];
638 Hook::callAll('contact_edit', $arr);
640 return $arr['output'];
643 $sql_values = [local_user()];
645 // @TODO: Replace with parameter from router
646 $type = $a->argv[1] ?? '';
650 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
651 // This makes the query look for contact.uid = 0
652 array_unshift($sql_values, 0);
655 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
658 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
659 // This makes the query look for contact.uid = 0
660 array_unshift($sql_values, 0);
663 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
666 $sql_extra = " AND `pending` AND NOT `archive` AND ((`rel` = ?)
667 OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
668 $sql_values[] = Model\Contact::SHARING;
671 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
679 $search_hdr = $search;
680 $search_txt = preg_quote($search);
681 $sql_extra .= " AND (name REGEXP ? OR url REGEXP ? OR nick REGEXP ?)";
682 $sql_values[] = $search_txt;
683 $sql_values[] = $search_txt;
684 $sql_values[] = $search_txt;
688 $sql_extra .= " AND network = ? ";
689 $sql_values[] = $nets;
694 $sql_extra .= " AND `rel` IN (?, ?)";
695 $sql_values[] = Model\Contact::FOLLOWER;
696 $sql_values[] = Model\Contact::FRIEND;
699 $sql_extra .= " AND `rel` IN (?, ?)";
700 $sql_values[] = Model\Contact::SHARING;
701 $sql_values[] = Model\Contact::FRIEND;
704 $sql_extra .= " AND `rel` = ?";
705 $sql_values[] = Model\Contact::FRIEND;
710 $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
711 $sql_values[] = $group;
715 $stmt = DBA::p("SELECT COUNT(*) AS `total`
721 " . Widget::unavailableNetworks(),
724 if (DBA::isResult($stmt)) {
725 $total = DBA::fetch($stmt)['total'];
729 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
731 $sql_values[] = $pager->getStart();
732 $sql_values[] = $pager->getItemsPerPage();
736 $stmt = DBA::p("SELECT *
746 while ($contact = DBA::fetch($stmt)) {
747 $contact['blocked'] = Model\Contact::isBlockedByUser($contact['id'], local_user());
748 $contact['readonly'] = Model\Contact::isIgnoredByUser($contact['id'], local_user());
749 $contacts[] = self::getContactTemplateVars($contact);
755 'label' => DI::l10n()->t('All Contacts'),
757 'sel' => !$type ? 'active' : '',
758 'title' => DI::l10n()->t('Show all contacts'),
759 'id' => 'showall-tab',
763 'label' => DI::l10n()->t('Pending'),
764 'url' => 'contact/pending',
765 'sel' => $type == 'pending' ? 'active' : '',
766 'title' => DI::l10n()->t('Only show pending contacts'),
767 'id' => 'showpending-tab',
771 'label' => DI::l10n()->t('Blocked'),
772 'url' => 'contact/blocked',
773 'sel' => $type == 'blocked' ? 'active' : '',
774 'title' => DI::l10n()->t('Only show blocked contacts'),
775 'id' => 'showblocked-tab',
779 'label' => DI::l10n()->t('Ignored'),
780 'url' => 'contact/ignored',
781 'sel' => $type == 'ignored' ? 'active' : '',
782 'title' => DI::l10n()->t('Only show ignored contacts'),
783 'id' => 'showignored-tab',
787 'label' => DI::l10n()->t('Archived'),
788 'url' => 'contact/archived',
789 'sel' => $type == 'archived' ? 'active' : '',
790 'title' => DI::l10n()->t('Only show archived contacts'),
791 'id' => 'showarchived-tab',
795 'label' => DI::l10n()->t('Hidden'),
796 'url' => 'contact/hidden',
797 'sel' => $type == 'hidden' ? 'active' : '',
798 'title' => DI::l10n()->t('Only show hidden contacts'),
799 'id' => 'showhidden-tab',
803 'label' => DI::l10n()->t('Groups'),
806 'title' => DI::l10n()->t('Organize your contact groups'),
807 'id' => 'contactgroups-tab',
812 $tabs_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
813 $tabs_html = Renderer::replaceMacros($tabs_tpl, ['$tabs' => $tabs]);
816 case 'followers': $header = DI::l10n()->t('Followers'); break;
817 case 'following': $header = DI::l10n()->t('Following'); break;
818 case 'mutuals': $header = DI::l10n()->t('Mutual friends'); break;
819 default: $header = DI::l10n()->t('Contacts');
823 case 'pending': $header .= ' - ' . DI::l10n()->t('Pending'); break;
824 case 'blocked': $header .= ' - ' . DI::l10n()->t('Blocked'); break;
825 case 'hidden': $header .= ' - ' . DI::l10n()->t('Hidden'); break;
826 case 'ignored': $header .= ' - ' . DI::l10n()->t('Ignored'); break;
827 case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
830 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
832 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
833 $o .= Renderer::replaceMacros($tpl, [
834 '$header' => $header,
835 '$tabs' => $tabs_html,
837 '$search' => $search_hdr,
838 '$desc' => DI::l10n()->t('Search your contacts'),
839 '$finding' => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
840 '$submit' => DI::l10n()->t('Find'),
841 '$cmd' => DI::args()->getCommand(),
842 '$contacts' => $contacts,
843 '$contact_drop_confirm' => DI::l10n()->t('Do you really want to delete this contact?'),
845 '$batch_actions' => [
846 'contacts_batch_update' => DI::l10n()->t('Update'),
847 'contacts_batch_block' => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
848 'contacts_batch_ignore' => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
849 'contacts_batch_archive' => DI::l10n()->t('Archive') . '/' . DI::l10n()->t('Unarchive'),
850 'contacts_batch_drop' => DI::l10n()->t('Delete'),
852 '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
853 '$paginate' => $pager->renderFull($total),
860 * List of pages for the Contact TabBar
862 * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
865 * @param array $contact The contact array
866 * @param int $active_tab 1 if tab should be marked as active
868 * @return string HTML string of the contact page tabs buttons.
869 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
871 public static function getTabsHTML($a, $contact, $active_tab)
876 'label' => DI::l10n()->t('Status'),
877 'url' => "contact/" . $contact['id'] . "/conversations",
878 'sel' => (($active_tab == 1) ? 'active' : ''),
879 'title' => DI::l10n()->t('Conversations started by this contact'),
880 'id' => 'status-tab',
884 'label' => DI::l10n()->t('Posts and Comments'),
885 'url' => "contact/" . $contact['id'] . "/posts",
886 'sel' => (($active_tab == 2) ? 'active' : ''),
887 'title' => DI::l10n()->t('Status Messages and Posts'),
892 'label' => DI::l10n()->t('Profile'),
893 'url' => "contact/" . $contact['id'],
894 'sel' => (($active_tab == 3) ? 'active' : ''),
895 'title' => DI::l10n()->t('Profile Details'),
896 'id' => 'profile-tab',
901 // Show this tab only if there is visible friend list
902 $x = Model\GContact::countAllFriends(local_user(), $contact['id']);
904 $tabs[] = ['label' => DI::l10n()->t('Contacts'),
905 'url' => "allfriends/" . $contact['id'],
906 'sel' => (($active_tab == 4) ? 'active' : ''),
907 'title' => DI::l10n()->t('View all contacts'),
908 'id' => 'allfriends-tab',
912 // Show this tab only if there is visible common friend list
913 $common = Model\GContact::countCommonFriends(local_user(), $contact['id']);
915 $tabs[] = ['label' => DI::l10n()->t('Common Friends'),
916 'url' => "common/loc/" . local_user() . "/" . $contact['id'],
917 'sel' => (($active_tab == 5) ? 'active' : ''),
918 'title' => DI::l10n()->t('View all common friends'),
919 'id' => 'common-loc-tab',
924 if (!empty($contact['uid'])) {
925 $tabs[] = ['label' => DI::l10n()->t('Advanced'),
926 'url' => 'contact/' . $contact['id'] . '/advanced/',
927 'sel' => (($active_tab == 6) ? '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 private static function getConversationsHMTL($a, $contact_id, $update)
945 // We need the editor here to be able to reshare an item.
949 'allow_location' => $a->user['allow_location'],
950 'default_location' => $a->user['default-location'],
951 'nickname' => $a->user['nickname'],
952 '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'),
953 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
955 'visitor' => 'block',
956 'profile_uid' => local_user(),
958 $o = status_editor($a, $x, 0, true);
962 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
965 $o .= self::getTabsHTML($a, $contact, 1);
968 if (DBA::isResult($contact)) {
969 DI::page()['aside'] = '';
971 $profiledata = Model\Contact::getByURL($contact['url'], false);
973 Model\Profile::load($a, '', $profiledata, true);
975 if ($contact['uid'] == 0) {
976 $o .= Model\Contact::getPostsFromId($contact['id'], true, $update);
978 $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update);
985 private static function getPostsHTML($a, $contact_id)
987 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
989 $o = self::getTabsHTML($a, $contact, 2);
991 if (DBA::isResult($contact)) {
992 DI::page()['aside'] = '';
994 $profiledata = Model\Contact::getByURL($contact['url'], false);
996 if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
997 $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
1000 Model\Profile::load($a, '', $profiledata, true);
1002 if ($contact['uid'] == 0) {
1003 $o .= Model\Contact::getPostsFromId($contact['id']);
1005 $o .= Model\Contact::getPostsFromUrl($contact['url']);
1012 public static function getContactTemplateVars(array $rr)
1017 if (!empty($rr['uid']) && !empty($rr['rel'])) {
1018 switch ($rr['rel']) {
1019 case Model\Contact::FRIEND:
1020 $dir_icon = 'images/lrarrow.gif';
1021 $alt_text = DI::l10n()->t('Mutual Friendship');
1024 case Model\Contact::FOLLOWER;
1025 $dir_icon = 'images/larrow.gif';
1026 $alt_text = DI::l10n()->t('is a fan of yours');
1029 case Model\Contact::SHARING;
1030 $dir_icon = 'images/rarrow.gif';
1031 $alt_text = DI::l10n()->t('you are a fan of');
1039 $url = Model\Contact::magicLink($rr['url']);
1041 if (strpos($url, 'redir/') === 0) {
1042 $sparkle = ' class="sparkle" ';
1047 if ($rr['pending']) {
1048 if (in_array($rr['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
1049 $alt_text = DI::l10n()->t('Pending outgoing contact request');
1051 $alt_text = DI::l10n()->t('Pending incoming contact request');
1056 $dir_icon = 'images/larrow.gif';
1057 $alt_text = DI::l10n()->t('This is you');
1063 'img_hover' => DI::l10n()->t('Visit %s\'s profile [%s]', $rr['name'], $rr['url']),
1064 'edit_hover'=> DI::l10n()->t('Edit contact'),
1065 'photo_menu'=> Model\Contact::photoMenu($rr),
1067 'alt_text' => $alt_text,
1068 'dir_icon' => $dir_icon,
1069 'thumb' => Model\Contact::getThumb($rr),
1070 'name' => $rr['name'],
1071 'username' => $rr['name'],
1072 'account_type' => Model\Contact::getAccountType($rr),
1073 'sparkle' => $sparkle,
1074 'itemurl' => ($rr['addr'] ?? '') ?: $rr['url'],
1076 'network' => ContactSelector::networkToName($rr['network'], $rr['url'], $rr['protocol']),
1077 'nick' => $rr['nick'],
1082 * Gives a array with actions which can performed to a given contact
1084 * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
1086 * @param array $contact Data about the Contact
1087 * @return array with contact related actions
1089 private static function getContactActions($contact)
1091 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
1092 $contact_actions = [];
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',
1115 $contact_actions['block'] = [
1116 'label' => (intval($contact['blocked']) ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
1117 'url' => 'contact/' . $contact['id'] . '/block',
1118 'title' => DI::l10n()->t('Toggle Blocked status'),
1119 'sel' => (intval($contact['blocked']) ? 'active' : ''),
1120 'id' => 'toggle-block',
1123 $contact_actions['ignore'] = [
1124 'label' => (intval($contact['readonly']) ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
1125 'url' => 'contact/' . $contact['id'] . '/ignore',
1126 'title' => DI::l10n()->t('Toggle Ignored status'),
1127 'sel' => (intval($contact['readonly']) ? 'active' : ''),
1128 'id' => 'toggle-ignore',
1131 if ($contact['uid'] != 0) {
1132 $contact_actions['archive'] = [
1133 'label' => (intval($contact['archive']) ? DI::l10n()->t('Unarchive') : DI::l10n()->t('Archive')),
1134 'url' => 'contact/' . $contact['id'] . '/archive',
1135 'title' => DI::l10n()->t('Toggle Archive status'),
1136 'sel' => (intval($contact['archive']) ? 'active' : ''),
1137 'id' => 'toggle-archive',
1140 $contact_actions['delete'] = [
1141 'label' => DI::l10n()->t('Delete'),
1142 'url' => 'contact/' . $contact['id'] . '/drop',
1143 'title' => DI::l10n()->t('Delete contact'),
1149 return $contact_actions;