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 DI::page()['aside'] = '';
441 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
442 '$header' => DI::l10n()->t('Drop contact'),
443 '$contact' => self::getContactTemplateVars($orig_record),
445 '$message' => DI::l10n()->t('Do you really want to delete this contact?'),
446 '$confirm' => DI::l10n()->t('Yes'),
447 '$confirm_url' => DI::args()->getCommand(),
448 '$confirm_name' => 'confirmed',
449 '$cancel' => DI::l10n()->t('Cancel'),
452 // Now check how the user responded to the confirmation query
453 if (!empty($_REQUEST['canceled'])) {
454 DI::baseUrl()->redirect('contact');
457 self::dropContact($orig_record);
458 info(DI::l10n()->t('Contact has been removed.'));
460 DI::baseUrl()->redirect('contact');
463 if ($cmd === 'posts') {
464 return self::getPostsHTML($a, $contact_id);
466 if ($cmd === 'conversations') {
467 return self::getConversationsHMTL($a, $contact_id, $update);
471 $_SESSION['return_path'] = DI::args()->getQueryString();
473 if (!empty($a->data['contact']) && is_array($a->data['contact'])) {
474 $contact = $a->data['contact'];
476 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
477 '$baseurl' => DI::baseUrl()->get(true),
480 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
481 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
484 switch ($contact['rel']) {
485 case Model\Contact::FRIEND:
486 $relation_text = DI::l10n()->t('You are mutual friends with %s');
489 case Model\Contact::FOLLOWER;
490 $relation_text = DI::l10n()->t('You are sharing with %s');
493 case Model\Contact::SHARING;
494 $relation_text = DI::l10n()->t('%s is sharing with you');
501 if ($contact['uid'] == 0) {
505 if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
509 $relation_text = sprintf($relation_text, $contact['name']);
511 $url = Model\Contact::magicLink($contact['url']);
512 if (strpos($url, 'redir/') === 0) {
513 $sparkle = ' class="sparkle" ';
518 $insecure = DI::l10n()->t('Private communications are not available for this contact.');
520 $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? DI::l10n()->t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
522 if ($contact['last-update'] > DBA::NULL_DATETIME) {
523 $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? DI::l10n()->t('(Update was successful)') : DI::l10n()->t('(Update was not successful)'));
525 $lblsuggest = (($contact['network'] === Protocol::DFRN) ? DI::l10n()->t('Suggest friends') : '');
527 $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
529 $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']));
532 $tab_str = self::getTabsHTML($contact, self::TAB_PROFILE);
534 $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : '');
536 $fetch_further_information = null;
537 if ($contact['network'] == Protocol::FEED) {
538 $fetch_further_information = [
539 'fetch_further_information',
540 DI::l10n()->t('Fetch further information for feeds'),
541 $contact['fetch_further_information'],
542 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.'),
544 '0' => DI::l10n()->t('Disabled'),
545 '1' => DI::l10n()->t('Fetch information'),
546 '3' => DI::l10n()->t('Fetch keywords'),
547 '2' => DI::l10n()->t('Fetch information and keywords')
552 $poll_interval = null;
553 if ((($contact['network'] == Protocol::FEED) && !DI::config()->get('system', 'adjust_poll_frequency')) || ($contact['network']== Protocol::MAIL)) {
554 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
557 // Load contactact related actions like hide, suggest, delete and others
558 $contact_actions = self::getContactActions($contact);
560 if ($contact['uid'] != 0) {
561 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
562 $contact_settings_label = DI::l10n()->t('Contact Settings');
565 $contact_settings_label = null;
568 $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
569 $o .= Renderer::replaceMacros($tpl, [
570 '$header' => DI::l10n()->t('Contact'),
571 '$tab_str' => $tab_str,
572 '$submit' => DI::l10n()->t('Submit'),
573 '$lbl_info1' => $lbl_info1,
574 '$lbl_info2' => DI::l10n()->t('Their personal note'),
575 '$reason' => trim(Strings::escapeTags($contact['reason'])),
576 '$infedit' => DI::l10n()->t('Edit contact notes'),
577 '$common_link' => 'contact/' . $contact['id'] . '/contacts/common',
578 '$relation_text' => $relation_text,
579 '$visit' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
580 '$blockunblock' => DI::l10n()->t('Block/Unblock contact'),
581 '$ignorecont' => DI::l10n()->t('Ignore contact'),
582 '$lblrecent' => DI::l10n()->t('View conversations'),
583 '$lblsuggest' => $lblsuggest,
584 '$nettype' => $nettype,
585 '$poll_interval' => $poll_interval,
586 '$poll_enabled' => $poll_enabled,
587 '$lastupdtext' => DI::l10n()->t('Last update:'),
588 '$lost_contact' => $lost_contact,
589 '$updpub' => DI::l10n()->t('Update public posts'),
590 '$last_update' => $last_update,
591 '$udnow' => DI::l10n()->t('Update now'),
592 '$contact_id' => $contact['id'],
593 '$block_text' => ($contact['blocked'] ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
594 '$ignore_text' => ($contact['readonly'] ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
595 '$insecure' => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
596 '$info' => $contact['info'],
597 '$cinfo' => ['info', '', $contact['info'], ''],
598 '$blocked' => ($contact['blocked'] ? DI::l10n()->t('Currently blocked') : ''),
599 '$ignored' => ($contact['readonly'] ? DI::l10n()->t('Currently ignored') : ''),
600 '$archived' => ($contact['archive'] ? DI::l10n()->t('Currently archived') : ''),
601 '$pending' => ($contact['pending'] ? DI::l10n()->t('Awaiting connection acknowledge') : ''),
602 '$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')],
603 '$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')],
604 '$fetch_further_information' => $fetch_further_information,
605 '$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')],
606 '$photo' => Model\Contact::getPhoto($contact),
607 '$name' => $contact['name'],
608 '$sparkle' => $sparkle,
610 '$profileurllabel'=> DI::l10n()->t('Profile URL'),
611 '$profileurl' => $contact['url'],
612 '$account_type' => Model\Contact::getAccountType($contact),
613 '$location' => BBCode::convert($contact['location']),
614 '$location_label' => DI::l10n()->t('Location:'),
615 '$xmpp' => BBCode::convert($contact['xmpp']),
616 '$xmpp_label' => DI::l10n()->t('XMPP:'),
617 '$about' => BBCode::convert($contact['about'], false),
618 '$about_label' => DI::l10n()->t('About:'),
619 '$keywords' => $contact['keywords'],
620 '$keywords_label' => DI::l10n()->t('Tags:'),
621 '$contact_action_button' => DI::l10n()->t('Actions'),
622 '$contact_actions'=> $contact_actions,
623 '$contact_status' => DI::l10n()->t('Status'),
624 '$contact_settings_label' => $contact_settings_label,
625 '$contact_profile_label' => DI::l10n()->t('Profile'),
628 $arr = ['contact' => $contact, 'output' => $o];
630 Hook::callAll('contact_edit', $arr);
632 return $arr['output'];
635 $sql_values = [local_user()];
637 // @TODO: Replace with parameter from router
638 $type = $a->argv[1] ?? '';
642 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
643 // This makes the query look for contact.uid = 0
644 array_unshift($sql_values, 0);
647 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
650 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
651 // This makes the query look for contact.uid = 0
652 array_unshift($sql_values, 0);
655 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
658 $sql_extra = " AND `pending` AND NOT `archive` AND ((`rel` = ?)
659 OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
660 $sql_values[] = Model\Contact::SHARING;
663 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
671 $search_hdr = $search;
672 $search_txt = preg_quote($search);
673 $sql_extra .= " AND (name REGEXP ? OR url REGEXP ? OR nick REGEXP ?)";
674 $sql_values[] = $search_txt;
675 $sql_values[] = $search_txt;
676 $sql_values[] = $search_txt;
680 $sql_extra .= " AND network = ? ";
681 $sql_values[] = $nets;
686 $sql_extra .= " AND `rel` IN (?, ?)";
687 $sql_values[] = Model\Contact::FOLLOWER;
688 $sql_values[] = Model\Contact::FRIEND;
691 $sql_extra .= " AND `rel` IN (?, ?)";
692 $sql_values[] = Model\Contact::SHARING;
693 $sql_values[] = Model\Contact::FRIEND;
696 $sql_extra .= " AND `rel` = ?";
697 $sql_values[] = Model\Contact::FRIEND;
702 $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
703 $sql_values[] = $group;
707 $stmt = DBA::p("SELECT COUNT(*) AS `total`
713 " . Widget::unavailableNetworks(),
716 if (DBA::isResult($stmt)) {
717 $total = DBA::fetch($stmt)['total'];
721 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
723 $sql_values[] = $pager->getStart();
724 $sql_values[] = $pager->getItemsPerPage();
728 $stmt = DBA::p("SELECT *
738 while ($contact = DBA::fetch($stmt)) {
739 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
740 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
741 $contacts[] = self::getContactTemplateVars($contact);
747 'label' => DI::l10n()->t('All Contacts'),
749 'sel' => !$type ? 'active' : '',
750 'title' => DI::l10n()->t('Show all contacts'),
751 'id' => 'showall-tab',
755 'label' => DI::l10n()->t('Pending'),
756 'url' => 'contact/pending',
757 'sel' => $type == 'pending' ? 'active' : '',
758 'title' => DI::l10n()->t('Only show pending contacts'),
759 'id' => 'showpending-tab',
763 'label' => DI::l10n()->t('Blocked'),
764 'url' => 'contact/blocked',
765 'sel' => $type == 'blocked' ? 'active' : '',
766 'title' => DI::l10n()->t('Only show blocked contacts'),
767 'id' => 'showblocked-tab',
771 'label' => DI::l10n()->t('Ignored'),
772 'url' => 'contact/ignored',
773 'sel' => $type == 'ignored' ? 'active' : '',
774 'title' => DI::l10n()->t('Only show ignored contacts'),
775 'id' => 'showignored-tab',
779 'label' => DI::l10n()->t('Archived'),
780 'url' => 'contact/archived',
781 'sel' => $type == 'archived' ? 'active' : '',
782 'title' => DI::l10n()->t('Only show archived contacts'),
783 'id' => 'showarchived-tab',
787 'label' => DI::l10n()->t('Hidden'),
788 'url' => 'contact/hidden',
789 'sel' => $type == 'hidden' ? 'active' : '',
790 'title' => DI::l10n()->t('Only show hidden contacts'),
791 'id' => 'showhidden-tab',
795 'label' => DI::l10n()->t('Groups'),
798 'title' => DI::l10n()->t('Organize your contact groups'),
799 'id' => 'contactgroups-tab',
804 $tabs_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
805 $tabs_html = Renderer::replaceMacros($tabs_tpl, ['$tabs' => $tabs]);
808 case 'followers': $header = DI::l10n()->t('Followers'); break;
809 case 'following': $header = DI::l10n()->t('Following'); break;
810 case 'mutuals': $header = DI::l10n()->t('Mutual friends'); break;
811 default: $header = DI::l10n()->t('Contacts');
815 case 'pending': $header .= ' - ' . DI::l10n()->t('Pending'); break;
816 case 'blocked': $header .= ' - ' . DI::l10n()->t('Blocked'); break;
817 case 'hidden': $header .= ' - ' . DI::l10n()->t('Hidden'); break;
818 case 'ignored': $header .= ' - ' . DI::l10n()->t('Ignored'); break;
819 case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
822 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
824 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
825 $o .= Renderer::replaceMacros($tpl, [
826 '$header' => $header,
827 '$tabs' => $tabs_html,
829 '$search' => $search_hdr,
830 '$desc' => DI::l10n()->t('Search your contacts'),
831 '$finding' => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
832 '$submit' => DI::l10n()->t('Find'),
833 '$cmd' => DI::args()->getCommand(),
834 '$contacts' => $contacts,
835 '$contact_drop_confirm' => DI::l10n()->t('Do you really want to delete this contact?'),
837 '$batch_actions' => [
838 'contacts_batch_update' => DI::l10n()->t('Update'),
839 'contacts_batch_block' => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
840 'contacts_batch_ignore' => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
841 'contacts_batch_archive' => DI::l10n()->t('Archive') . '/' . DI::l10n()->t('Unarchive'),
842 'contacts_batch_drop' => DI::l10n()->t('Delete'),
844 '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
845 '$paginate' => $pager->renderFull($total),
852 * List of pages for the Contact TabBar
854 * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
856 * @param array $contact The contact array
857 * @param int $active_tab 1 if tab should be marked as active
859 * @return string HTML string of the contact page tabs buttons.
860 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
861 * @throws \ImagickException
863 public static function getTabsHTML(array $contact, int $active_tab)
865 $cid = $pcid = $contact['id'];
866 $data = Model\Contact::getPublicAndUserContacID($contact['id'], local_user());
867 if (!empty($data['user']) && ($contact['id'] == $data['public'])) {
868 $cid = $data['user'];
869 } elseif (!empty($data['public'])) {
870 $pcid = $data['public'];
876 'label' => DI::l10n()->t('Status'),
877 'url' => 'contact/' . $pcid . '/conversations',
878 'sel' => (($active_tab == self::TAB_CONVERSATIONS) ? '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/' . $pcid . '/posts',
886 'sel' => (($active_tab == self::TAB_POSTS) ? 'active' : ''),
887 'title' => DI::l10n()->t('Status Messages and Posts'),
892 'label' => DI::l10n()->t('Profile'),
893 'url' => 'contact/' . $cid,
894 'sel' => (($active_tab == self::TAB_PROFILE) ? 'active' : ''),
895 'title' => DI::l10n()->t('Profile Details'),
896 'id' => 'profile-tab',
899 ['label' => DI::l10n()->t('Contacts'),
900 'url' => 'contact/' . $pcid . '/contacts',
901 'sel' => (($active_tab == self::TAB_CONTACTS) ? 'active' : ''),
902 'title' => DI::l10n()->t('View all known contacts'),
903 'id' => 'contacts-tab',
909 $tabs[] = ['label' => DI::l10n()->t('Advanced'),
910 'url' => 'contact/' . $cid . '/advanced/',
911 'sel' => (($active_tab == self::TAB_ADVANCED) ? 'active' : ''),
912 'title' => DI::l10n()->t('Advanced Contact Settings'),
913 'id' => 'advanced-tab',
918 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
919 $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
924 private static function getConversationsHMTL($a, $contact_id, $update)
929 // We need the editor here to be able to reshare an item.
933 'allow_location' => $a->user['allow_location'],
934 'default_location' => $a->user['default-location'],
935 'nickname' => $a->user['nickname'],
936 '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'),
937 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
939 'visitor' => 'block',
940 'profile_uid' => local_user(),
942 $o = status_editor($a, $x, 0, true);
946 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
949 $o .= self::getTabsHTML($contact, self::TAB_CONVERSATIONS);
952 if (DBA::isResult($contact)) {
953 DI::page()['aside'] = '';
955 $profiledata = Model\Contact::getByURLForUser($contact['url'], local_user());
957 Model\Profile::load($a, '', $profiledata, true);
959 if ($contact['uid'] == 0) {
960 $o .= Model\Contact::getPostsFromId($contact['id'], true, $update);
962 $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update);
969 private static function getPostsHTML($a, $contact_id)
971 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
973 $o = self::getTabsHTML($contact, self::TAB_POSTS);
975 if (DBA::isResult($contact)) {
976 DI::page()['aside'] = '';
978 $profiledata = Model\Contact::getByURLForUser($contact['url'], local_user());
980 if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
981 $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
984 Model\Profile::load($a, '', $profiledata, true);
986 if ($contact['uid'] == 0) {
987 $o .= Model\Contact::getPostsFromId($contact['id']);
989 $o .= Model\Contact::getPostsFromUrl($contact['url']);
997 * Return the fields for the contact template
999 * @param array $contact Contact array
1000 * @return array Template fields
1002 public static function getContactTemplateVars(array $contact)
1006 if (!empty($contact['url']) && isset($contact['uid']) && ($contact['uid'] == 0) && local_user()) {
1007 $personal = Model\Contact::getByURL($contact['url'], false, ['uid', 'rel', 'self'], local_user());
1008 if (!empty($personal)) {
1009 $contact['uid'] = $personal['uid'];
1010 $contact['rel'] = $personal['rel'];
1011 $contact['self'] = $personal['self'];
1015 if (!empty($contact['uid']) && !empty($contact['rel']) && local_user() == $contact['uid']) {
1016 switch ($contact['rel']) {
1017 case Model\Contact::FRIEND:
1018 $alt_text = DI::l10n()->t('Mutual Friendship');
1021 case Model\Contact::FOLLOWER;
1022 $alt_text = DI::l10n()->t('is a fan of yours');
1025 case Model\Contact::SHARING;
1026 $alt_text = DI::l10n()->t('you are a fan of');
1034 $url = Model\Contact::magicLink($contact['url']);
1036 if (strpos($url, 'redir/') === 0) {
1037 $sparkle = ' class="sparkle" ';
1042 if ($contact['pending']) {
1043 if (in_array($contact['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
1044 $alt_text = DI::l10n()->t('Pending outgoing contact request');
1046 $alt_text = DI::l10n()->t('Pending incoming contact request');
1050 if ($contact['self']) {
1051 $alt_text = DI::l10n()->t('This is you');
1052 $url = $contact['url'];
1057 'id' => $contact['id'],
1059 'img_hover' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
1060 'photo_menu' => Model\Contact::photoMenu($contact),
1061 'thumb' => Model\Contact::getThumb($contact),
1062 'alt_text' => $alt_text,
1063 'name' => $contact['name'],
1064 'nick' => $contact['nick'],
1065 'details' => $contact['location'],
1066 'tags' => $contact['keywords'],
1067 'about' => $contact['about'],
1068 'account_type' => Model\Contact::getAccountType($contact),
1069 'sparkle' => $sparkle,
1070 'itemurl' => ($contact['addr'] ?? '') ?: $contact['url'],
1071 'network' => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']),
1076 * Gives a array with actions which can performed to a given contact
1078 * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
1080 * @param array $contact Data about the Contact
1081 * @return array with contact related actions
1083 private static function getContactActions($contact)
1085 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
1086 $contact_actions = [];
1088 // Provide friend suggestion only for Friendica contacts
1089 if ($contact['network'] === Protocol::DFRN) {
1090 $contact_actions['suggest'] = [
1091 'label' => DI::l10n()->t('Suggest friends'),
1092 'url' => 'fsuggest/' . $contact['id'],
1099 if ($poll_enabled) {
1100 $contact_actions['update'] = [
1101 'label' => DI::l10n()->t('Update now'),
1102 'url' => 'contact/' . $contact['id'] . '/update',
1109 if (in_array($contact['network'], Protocol::FEDERATED)) {
1110 $contact_actions['updateprofile'] = [
1111 'label' => DI::l10n()->t('Refetch contact data'),
1112 'url' => 'contact/' . $contact['id'] . '/updateprofile',
1115 'id' => 'updateprofile',
1119 $contact_actions['block'] = [
1120 'label' => (intval($contact['blocked']) ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
1121 'url' => 'contact/' . $contact['id'] . '/block',
1122 'title' => DI::l10n()->t('Toggle Blocked status'),
1123 'sel' => (intval($contact['blocked']) ? 'active' : ''),
1124 'id' => 'toggle-block',
1127 $contact_actions['ignore'] = [
1128 'label' => (intval($contact['readonly']) ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
1129 'url' => 'contact/' . $contact['id'] . '/ignore',
1130 'title' => DI::l10n()->t('Toggle Ignored status'),
1131 'sel' => (intval($contact['readonly']) ? 'active' : ''),
1132 'id' => 'toggle-ignore',
1135 if ($contact['uid'] != 0) {
1136 $contact_actions['archive'] = [
1137 'label' => (intval($contact['archive']) ? DI::l10n()->t('Unarchive') : DI::l10n()->t('Archive')),
1138 'url' => 'contact/' . $contact['id'] . '/archive',
1139 'title' => DI::l10n()->t('Toggle Archive status'),
1140 'sel' => (intval($contact['archive']) ? 'active' : ''),
1141 'id' => 'toggle-archive',
1144 $contact_actions['delete'] = [
1145 'label' => DI::l10n()->t('Delete'),
1146 'url' => 'contact/' . $contact['id'] . '/drop',
1147 'title' => DI::l10n()->t('Delete contact'),
1153 return $contact_actions;