3 namespace Friendica\Module;
6 use Friendica\BaseModule;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Pager;
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Content\Widget;
12 use Friendica\Core\ACL;
13 use Friendica\Core\Hook;
14 use Friendica\Core\L10n;
15 use Friendica\Core\Protocol;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\Worker;
18 use Friendica\Database\DBA;
21 use Friendica\Module\Security\Login;
22 use Friendica\Network\HTTPException\BadRequestException;
23 use Friendica\Network\HTTPException\NotFoundException;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Proxy as ProxyUtils;
26 use Friendica\Util\Strings;
29 * Manages and show Contacts and their content
31 class Contact extends BaseModule
33 private static function batchActions()
35 if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
39 $contacts_id = $_POST['contact_batch'];
41 $stmt = DBA::select('contact', ['id', 'archive'], ['id' => $contacts_id, 'uid' => local_user(), 'self' => false, 'deleted' => false]);
42 $orig_records = DBA::toArray($stmt);
45 foreach ($orig_records as $orig_record) {
46 $contact_id = $orig_record['id'];
47 if (!empty($_POST['contacts_batch_update'])) {
48 self::updateContactFromPoll($contact_id);
51 if (!empty($_POST['contacts_batch_block'])) {
52 self::blockContact($contact_id);
55 if (!empty($_POST['contacts_batch_ignore'])) {
56 self::ignoreContact($contact_id);
59 if (!empty($_POST['contacts_batch_archive'])
60 && self::archiveContact($contact_id, $orig_record)
64 if (!empty($_POST['contacts_batch_drop'])) {
65 self::dropContact($orig_record);
69 if ($count_actions > 0) {
70 info(DI::l10n()->tt('%d contact edited.', '%d contacts edited.', $count_actions));
73 DI::baseUrl()->redirect('contact');
76 public static function post(array $parameters = [])
84 // @TODO: Replace with parameter from router
85 if ($a->argv[1] === 'batch') {
90 // @TODO: Replace with parameter from router
91 $contact_id = intval($a->argv[1]);
96 if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
97 notice(DI::l10n()->t('Could not access contact record.') . EOL);
98 DI::baseUrl()->redirect('contact');
102 Hook::callAll('contact_edit_post', $_POST);
104 $profile_id = intval($_POST['profile-assign'] ?? 0);
106 if (!DBA::exists('profile', ['id' => $profile_id, 'uid' => local_user()])) {
107 notice(DI::l10n()->t('Could not locate selected profile.') . EOL);
112 $hidden = !empty($_POST['hidden']);
114 $notify = !empty($_POST['notify']);
116 $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
118 $ffi_keyword_blacklist = Strings::escapeHtml(trim($_POST['ffi_keyword_blacklist'] ?? ''));
120 $priority = intval($_POST['poll'] ?? 0);
121 if ($priority > 5 || $priority < 0) {
125 $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
127 $r = DBA::update('contact', [
128 'profile-id' => $profile_id,
129 'priority' => $priority,
132 'notify_new_posts' => $notify,
133 'fetch_further_information' => $fetch_further_information,
134 'ffi_keyword_blacklist' => $ffi_keyword_blacklist],
135 ['id' => $contact_id, 'uid' => local_user()]
138 if (DBA::isResult($r)) {
139 info(DI::l10n()->t('Contact updated.') . EOL);
141 notice(DI::l10n()->t('Failed to update contact record.') . EOL);
144 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
145 if (DBA::isResult($contact)) {
146 $a->data['contact'] = $contact;
152 /* contact actions */
154 private static function updateContactFromPoll($contact_id)
156 $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
157 if (!DBA::isResult($contact)) {
161 $uid = $contact['uid'];
163 if ($contact['network'] == Protocol::OSTATUS) {
164 $result = Model\Contact::createFromProbe($uid, $contact['url'], false, $contact['network']);
166 if ($result['success']) {
167 DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
170 // pull feed and consume it, which should subscribe to the hub.
171 Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
175 private static function updateContactFromProbe($contact_id)
177 $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
178 if (!DBA::isResult($contact)) {
182 // Update the entry in the contact table
183 Model\Contact::updateFromProbe($contact_id, '', true);
185 // Update the entry in the gcontact table
186 Model\GContact::updateFromProbe($contact['url']);
190 * Toggles the blocked status of a contact identified by id.
195 private static function blockContact($contact_id)
197 $blocked = !Model\Contact::isBlockedByUser($contact_id, local_user());
198 Model\Contact::setBlockedForUser($contact_id, local_user(), $blocked);
202 * Toggles the ignored status of a contact identified by id.
207 private static function ignoreContact($contact_id)
209 $ignored = !Model\Contact::isIgnoredByUser($contact_id, local_user());
210 Model\Contact::setIgnoredForUser($contact_id, local_user(), $ignored);
214 * Toggles the archived status of a contact identified by id.
215 * If the current status isn't provided, this will always archive the contact.
218 * @param $orig_record
222 private static function archiveContact($contact_id, $orig_record)
224 $archived = empty($orig_record['archive']);
225 $r = DBA::update('contact', ['archive' => $archived], ['id' => $contact_id, 'uid' => local_user()]);
227 return DBA::isResult($r);
230 private static function dropContact($orig_record)
232 $owner = Model\User::getOwnerDataById(local_user());
233 if (!DBA::isResult($owner)) {
237 Model\Contact::terminateFriendship($owner, $orig_record, true);
238 Model\Contact::remove($orig_record['id']);
241 public static function content(array $parameters = [], $update = 0)
244 return Login::form($_SERVER['REQUEST_URI']);
249 $nets = $_GET['nets'] ?? '';
250 $rel = $_GET['rel'] ?? '';
252 if (empty(DI::page()['aside'])) {
253 DI::page()['aside'] = '';
258 // @TODO: Replace with parameter from router
259 if ($a->argc == 2 && intval($a->argv[1])
260 || $a->argc == 3 && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])
262 $contact_id = intval($a->argv[1]);
263 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
265 if (!DBA::isResult($contact)) {
266 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => 0, 'deleted' => false]);
269 // Don't display contacts that are about to be deleted
270 if ($contact['network'] == Protocol::PHANTOM) {
275 if (DBA::isResult($contact)) {
276 if ($contact['self']) {
277 // @TODO: Replace with parameter from router
278 if (($a->argc == 3) && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])) {
279 DI::baseUrl()->redirect('profile/' . $contact['nick']);
281 DI::baseUrl()->redirect('profile/' . $contact['nick'] . '?tab=profile');
285 $a->data['contact'] = $contact;
287 if (($contact['network'] != '') && ($contact['network'] != Protocol::DFRN)) {
288 $network_link = Strings::formatNetworkName($contact['network'], $contact['url']);
295 if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
296 if ($contact['uid'] && in_array($contact['rel'], [Model\Contact::SHARING, Model\Contact::FRIEND])) {
297 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']);
298 } elseif(!$contact['pending']) {
299 $follow_link = 'follow?url=' . urlencode($contact['url']);
303 $wallmessage_link = '';
304 if ($contact['uid'] && Model\Contact::canReceivePrivateMessages($contact)) {
305 $wallmessage_link = 'message/new/' . $contact['id'];
308 $vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
309 '$name' => $contact['name'],
310 '$photo' => $contact['photo'],
311 '$url' => Model\Contact::magicLinkByContact($contact, $contact['url']),
312 '$addr' => $contact['addr'] ?? '',
313 '$network_link' => $network_link,
314 '$network' => DI::l10n()->t('Network:'),
315 '$account_type' => Model\Contact::getAccountType($contact),
316 '$follow' => DI::l10n()->t('Follow'),
317 '$follow_link' => $follow_link,
318 '$unfollow' => DI::l10n()->t('Unfollow'),
319 '$unfollow_link' => $unfollow_link,
320 '$wallmessage' => DI::l10n()->t('Message'),
321 '$wallmessage_link' => $wallmessage_link,
324 $findpeople_widget = '';
326 $networks_widget = '';
330 $findpeople_widget = Widget::findPeople();
331 if (isset($_GET['add'])) {
332 $follow_widget = Widget::follow($_GET['add']);
334 $follow_widget = Widget::follow();
337 $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
338 $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
341 if ($contact['uid'] != 0) {
342 $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
344 $groups_widget = null;
347 DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $groups_widget . $networks_widget . $rel_widget;
349 $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
350 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
351 '$baseurl' => DI::baseUrl()->get(true),
356 Nav::setSelected('contact');
359 notice(DI::l10n()->t('Permission denied.') . EOL);
360 return Login::form();
364 $contact_id = intval($a->argv[1]);
366 throw new BadRequestException();
369 // @TODO: Replace with parameter from router
372 $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
373 if (!DBA::isResult($orig_record)) {
374 throw new NotFoundException(DI::l10n()->t('Contact not found'));
377 if ($cmd === 'update' && ($orig_record['uid'] != 0)) {
378 self::updateContactFromPoll($contact_id);
379 DI::baseUrl()->redirect('contact/' . $contact_id);
383 if ($cmd === 'updateprofile' && ($orig_record['uid'] != 0)) {
384 self::updateContactFromProbe($contact_id);
385 DI::baseUrl()->redirect('crepair/' . $contact_id);
389 if ($cmd === 'block') {
390 self::blockContact($contact_id);
392 $blocked = Model\Contact::isBlockedByUser($contact_id, local_user());
393 info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')) . EOL);
395 DI::baseUrl()->redirect('contact/' . $contact_id);
399 if ($cmd === 'ignore') {
400 self::ignoreContact($contact_id);
402 $ignored = Model\Contact::isIgnoredByUser($contact_id, local_user());
403 info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')) . EOL);
405 DI::baseUrl()->redirect('contact/' . $contact_id);
409 if ($cmd === 'archive' && ($orig_record['uid'] != 0)) {
410 $r = self::archiveContact($contact_id, $orig_record);
412 $archived = (($orig_record['archive']) ? 0 : 1);
413 info((($archived) ? DI::l10n()->t('Contact has been archived') : DI::l10n()->t('Contact has been unarchived')) . EOL);
416 DI::baseUrl()->redirect('contact/' . $contact_id);
420 if ($cmd === 'drop' && ($orig_record['uid'] != 0)) {
421 // Check if we should do HTML-based delete confirmation
422 if (!empty($_REQUEST['confirm'])) {
423 // <form> can't take arguments in its 'action' parameter
424 // so add any arguments as hidden inputs
425 $query = explode_querystring(DI::args()->getQueryString());
427 foreach ($query['args'] as $arg) {
428 if (strpos($arg, 'confirm=') === false) {
429 $arg_parts = explode('=', $arg);
430 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
434 DI::page()['aside'] = '';
436 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
437 '$header' => DI::l10n()->t('Drop contact'),
438 '$contact' => self::getContactTemplateVars($orig_record),
440 '$message' => DI::l10n()->t('Do you really want to delete this contact?'),
441 '$extra_inputs' => $inputs,
442 '$confirm' => DI::l10n()->t('Yes'),
443 '$confirm_url' => $query['base'],
444 '$confirm_name' => 'confirmed',
445 '$cancel' => DI::l10n()->t('Cancel'),
448 // Now check how the user responded to the confirmation query
449 if (!empty($_REQUEST['canceled'])) {
450 DI::baseUrl()->redirect('contact');
453 self::dropContact($orig_record);
454 info(DI::l10n()->t('Contact has been removed.') . EOL);
456 DI::baseUrl()->redirect('contact');
459 if ($cmd === 'posts') {
460 return self::getPostsHTML($a, $contact_id);
462 if ($cmd === 'conversations') {
463 return self::getConversationsHMTL($a, $contact_id, $update);
467 $_SESSION['return_path'] = DI::args()->getQueryString();
469 if (!empty($a->data['contact']) && is_array($a->data['contact'])) {
470 $contact = $a->data['contact'];
472 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
473 '$baseurl' => DI::baseUrl()->get(true),
476 $contact['blocked'] = Model\Contact::isBlockedByUser($contact['id'], local_user());
477 $contact['readonly'] = Model\Contact::isIgnoredByUser($contact['id'], local_user());
481 switch ($contact['rel']) {
482 case Model\Contact::FRIEND:
483 $dir_icon = 'images/lrarrow.gif';
484 $relation_text = DI::l10n()->t('You are mutual friends with %s');
487 case Model\Contact::FOLLOWER;
488 $dir_icon = 'images/larrow.gif';
489 $relation_text = DI::l10n()->t('You are sharing with %s');
492 case Model\Contact::SHARING;
493 $dir_icon = 'images/rarrow.gif';
494 $relation_text = DI::l10n()->t('%s is sharing with you');
501 if ($contact['uid'] == 0) {
505 if (!in_array($contact['network'], Protocol::FEDERATED)) {
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($a, $contact, 3);
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 (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
554 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
557 $profile_select = null;
558 if ($contact['network'] == Protocol::DFRN) {
559 $profile_select = ContactSelector::profileAssign($contact['profile-id'], $contact['network'] !== Protocol::DFRN);
562 // Load contactact related actions like hide, suggest, delete and others
563 $contact_actions = self::getContactActions($contact);
565 if ($contact['uid'] != 0) {
566 $lbl_vis1 = DI::l10n()->t('Profile Visibility');
567 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
568 $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_vis1' => $lbl_vis1,
581 '$lbl_vis2' => DI::l10n()->t('Please choose the profile you would like to display to %s when viewing your profile securely.', $contact['name']),
582 '$lbl_info1' => $lbl_info1,
583 '$lbl_info2' => DI::l10n()->t('Their personal note'),
584 '$reason' => trim(Strings::escapeTags($contact['reason'])),
585 '$infedit' => DI::l10n()->t('Edit contact notes'),
586 '$common_link' => 'common/loc/' . local_user() . '/' . $contact['id'],
587 '$relation_text' => $relation_text,
588 '$visit' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
589 '$blockunblock' => DI::l10n()->t('Block/Unblock contact'),
590 '$ignorecont' => DI::l10n()->t('Ignore contact'),
591 '$lblcrepair' => DI::l10n()->t('Repair URL settings'),
592 '$lblrecent' => DI::l10n()->t('View conversations'),
593 '$lblsuggest' => $lblsuggest,
594 '$nettype' => $nettype,
595 '$poll_interval' => $poll_interval,
596 '$poll_enabled' => $poll_enabled,
597 '$lastupdtext' => DI::l10n()->t('Last update:'),
598 '$lost_contact' => $lost_contact,
599 '$updpub' => DI::l10n()->t('Update public posts'),
600 '$last_update' => $last_update,
601 '$udnow' => DI::l10n()->t('Update now'),
602 '$profile_select' => $profile_select,
603 '$contact_id' => $contact['id'],
604 '$block_text' => ($contact['blocked'] ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
605 '$ignore_text' => ($contact['readonly'] ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
606 '$insecure' => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
607 '$info' => $contact['info'],
608 '$cinfo' => ['info', '', $contact['info'], ''],
609 '$blocked' => ($contact['blocked'] ? DI::l10n()->t('Currently blocked') : ''),
610 '$ignored' => ($contact['readonly'] ? DI::l10n()->t('Currently ignored') : ''),
611 '$archived' => ($contact['archive'] ? DI::l10n()->t('Currently archived') : ''),
612 '$pending' => ($contact['pending'] ? DI::l10n()->t('Awaiting connection acknowledge') : ''),
613 '$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')],
614 '$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')],
615 '$fetch_further_information' => $fetch_further_information,
616 '$ffi_keyword_blacklist' => ['ffi_keyword_blacklist', DI::l10n()->t('Blacklisted keywords'), $contact['ffi_keyword_blacklist'], DI::l10n()->t('Comma separated list of keywords that should not be converted to hashtags, when "Fetch information and keywords" is selected')],
617 '$photo' => $contact['photo'],
618 '$name' => $contact['name'],
619 '$dir_icon' => $dir_icon,
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 $select_uid = local_user();
649 // @TODO: Replace with parameter from router
650 $type = $a->argv[1] ?? '';
654 $sql_extra = sprintf(" AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = %d and `user-contact`.`blocked`)", intval(local_user()));
658 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
661 $sql_extra = sprintf(" AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = %d and `user-contact`.`ignored`)", intval(local_user()));
665 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
668 $sql_extra = sprintf(" AND `pending` AND NOT `archive` AND ((`rel` = %d)
669 OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))", Model\Contact::SHARING);
672 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
675 $sql_extra .= sprintf(" AND `network` != '%s' ", Protocol::PHANTOM);
677 $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
678 $nets = Strings::escapeTags(trim($_GET['nets'] ?? ''));
679 $rel = Strings::escapeTags(trim($_GET['rel'] ?? ''));
683 'label' => DI::l10n()->t('All Contacts'),
685 'sel' => !$type ? 'active' : '',
686 'title' => DI::l10n()->t('Show all contacts'),
687 'id' => 'showall-tab',
691 'label' => DI::l10n()->t('Pending'),
692 'url' => 'contact/pending',
693 'sel' => $type == 'pending' ? 'active' : '',
694 'title' => DI::l10n()->t('Only show pending contacts'),
695 'id' => 'showpending-tab',
699 'label' => DI::l10n()->t('Blocked'),
700 'url' => 'contact/blocked',
701 'sel' => $type == 'blocked' ? 'active' : '',
702 'title' => DI::l10n()->t('Only show blocked contacts'),
703 'id' => 'showblocked-tab',
707 'label' => DI::l10n()->t('Ignored'),
708 'url' => 'contact/ignored',
709 'sel' => $type == 'ignored' ? 'active' : '',
710 'title' => DI::l10n()->t('Only show ignored contacts'),
711 'id' => 'showignored-tab',
715 'label' => DI::l10n()->t('Archived'),
716 'url' => 'contact/archived',
717 'sel' => $type == 'archived' ? 'active' : '',
718 'title' => DI::l10n()->t('Only show archived contacts'),
719 'id' => 'showarchived-tab',
723 'label' => DI::l10n()->t('Hidden'),
724 'url' => 'contact/hidden',
725 'sel' => $type == 'hidden' ? 'active' : '',
726 'title' => DI::l10n()->t('Only show hidden contacts'),
727 'id' => 'showhidden-tab',
731 'label' => DI::l10n()->t('Groups'),
734 'title' => DI::l10n()->t('Organize your contact groups'),
735 'id' => 'contactgroups-tab',
740 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
741 $t = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
748 $search_hdr = $search;
749 $search_txt = DBA::escape(Strings::protectSprintf(preg_quote($search)));
750 $sql_extra .= " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') ";
754 $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets));
758 case 'followers': $sql_extra .= " AND `rel` IN (1, 3)"; break;
759 case 'following': $sql_extra .= " AND `rel` IN (2, 3)"; break;
760 case 'mutuals': $sql_extra .= " AND `rel` = 3"; break;
763 $sql_extra .= " AND NOT `deleted` ";
765 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= Model\Contact::FRIEND)) ? sprintf(" AND `rel` = %d ", intval($sort_type)) : '');
767 $sql_extra3 = Widget::unavailableNetworks();
769 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
770 WHERE `uid` = %d AND `self` = 0 $sql_extra $sql_extra2 $sql_extra3",
773 if (DBA::isResult($r)) {
774 $total = $r[0]['total'];
776 $pager = new Pager(DI::args()->getQueryString());
780 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 $sql_extra $sql_extra2 $sql_extra3 ORDER BY `name` ASC LIMIT %d , %d ",
783 $pager->getItemsPerPage()
785 if (DBA::isResult($r)) {
786 foreach ($r as $rr) {
787 $rr['blocked'] = Model\Contact::isBlockedByUser($rr['id'], local_user());
788 $rr['readonly'] = Model\Contact::isIgnoredByUser($rr['id'], local_user());
789 $contacts[] = self::getContactTemplateVars($rr);
794 case 'followers': $header = DI::l10n()->t('Followers'); break;
795 case 'following': $header = DI::l10n()->t('Following'); break;
796 case 'mutuals': $header = DI::l10n()->t('Mutual friends'); break;
797 default: $header = DI::l10n()->t('Contacts');
801 case 'pending': $header .= ' - ' . DI::l10n()->t('Pending'); break;
802 case 'blocked': $header .= ' - ' . DI::l10n()->t('Blocked'); break;
803 case 'hidden': $header .= ' - ' . DI::l10n()->t('Hidden'); break;
804 case 'ignored': $header .= ' - ' . DI::l10n()->t('Ignored'); break;
805 case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
808 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
810 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
811 $o .= Renderer::replaceMacros($tpl, [
812 '$header' => $header,
815 '$search' => $search_hdr,
816 '$desc' => DI::l10n()->t('Search your contacts'),
817 '$finding' => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
818 '$submit' => DI::l10n()->t('Find'),
819 '$cmd' => DI::args()->getCommand(),
820 '$contacts' => $contacts,
821 '$contact_drop_confirm' => DI::l10n()->t('Do you really want to delete this contact?'),
823 '$batch_actions' => [
824 'contacts_batch_update' => DI::l10n()->t('Update'),
825 'contacts_batch_block' => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
826 'contacts_batch_ignore' => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
827 'contacts_batch_archive' => DI::l10n()->t('Archive') . '/' . DI::l10n()->t('Unarchive'),
828 'contacts_batch_drop' => DI::l10n()->t('Delete'),
830 '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
831 '$paginate' => $pager->renderFull($total),
838 * List of pages for the Contact TabBar
840 * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
843 * @param array $contact The contact array
844 * @param int $active_tab 1 if tab should be marked as active
846 * @return string HTML string of the contact page tabs buttons.
847 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
849 public static function getTabsHTML($a, $contact, $active_tab)
854 'label' => DI::l10n()->t('Status'),
855 'url' => "contact/" . $contact['id'] . "/conversations",
856 'sel' => (($active_tab == 1) ? 'active' : ''),
857 'title' => DI::l10n()->t('Conversations started by this contact'),
858 'id' => 'status-tab',
862 'label' => DI::l10n()->t('Posts and Comments'),
863 'url' => "contact/" . $contact['id'] . "/posts",
864 'sel' => (($active_tab == 2) ? 'active' : ''),
865 'title' => DI::l10n()->t('Status Messages and Posts'),
870 'label' => DI::l10n()->t('Profile'),
871 'url' => "contact/" . $contact['id'],
872 'sel' => (($active_tab == 3) ? 'active' : ''),
873 'title' => DI::l10n()->t('Profile Details'),
874 'id' => 'profile-tab',
879 // Show this tab only if there is visible friend list
880 $x = Model\GContact::countAllFriends(local_user(), $contact['id']);
882 $tabs[] = ['label' => DI::l10n()->t('Contacts'),
883 'url' => "allfriends/" . $contact['id'],
884 'sel' => (($active_tab == 4) ? 'active' : ''),
885 'title' => DI::l10n()->t('View all contacts'),
886 'id' => 'allfriends-tab',
890 // Show this tab only if there is visible common friend list
891 $common = Model\GContact::countCommonFriends(local_user(), $contact['id']);
893 $tabs[] = ['label' => DI::l10n()->t('Common Friends'),
894 'url' => "common/loc/" . local_user() . "/" . $contact['id'],
895 'sel' => (($active_tab == 5) ? 'active' : ''),
896 'title' => DI::l10n()->t('View all common friends'),
897 'id' => 'common-loc-tab',
902 if (!empty($contact['uid'])) {
903 $tabs[] = ['label' => DI::l10n()->t('Advanced'),
904 'url' => 'crepair/' . $contact['id'],
905 'sel' => (($active_tab == 6) ? 'active' : ''),
906 'title' => DI::l10n()->t('Advanced Contact Settings'),
907 'id' => 'advanced-tab',
912 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
913 $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
918 private static function getConversationsHMTL($a, $contact_id, $update)
923 // We need the editor here to be able to reshare an item.
927 'allow_location' => $a->user['allow_location'],
928 'default_location' => $a->user['default-location'],
929 'nickname' => $a->user['nickname'],
930 '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'),
931 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
933 'visitor' => 'block',
934 'profile_uid' => local_user(),
936 $o = status_editor($a, $x, 0, true);
940 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
943 $o .= self::getTabsHTML($a, $contact, 1);
946 if (DBA::isResult($contact)) {
947 DI::page()['aside'] = '';
949 $profiledata = Model\Contact::getDetailsByURL($contact['url']);
951 Model\Profile::load($a, '', 0, $profiledata, true);
952 $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update);
958 private static function getPostsHTML($a, $contact_id)
960 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
962 $o = self::getTabsHTML($a, $contact, 2);
964 if (DBA::isResult($contact)) {
965 DI::page()['aside'] = '';
967 $profiledata = Model\Contact::getDetailsByURL($contact['url']);
969 if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
970 $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
973 Model\Profile::load($a, '', 0, $profiledata, true);
974 $o .= Model\Contact::getPostsFromUrl($contact['url']);
980 public static function getContactTemplateVars(array $rr)
985 if (!empty($rr['uid']) && !empty($rr['rel'])) {
986 switch ($rr['rel']) {
987 case Model\Contact::FRIEND:
988 $dir_icon = 'images/lrarrow.gif';
989 $alt_text = DI::l10n()->t('Mutual Friendship');
992 case Model\Contact::FOLLOWER;
993 $dir_icon = 'images/larrow.gif';
994 $alt_text = DI::l10n()->t('is a fan of yours');
997 case Model\Contact::SHARING;
998 $dir_icon = 'images/rarrow.gif';
999 $alt_text = DI::l10n()->t('you are a fan of');
1007 $url = Model\Contact::magicLink($rr['url']);
1009 if (strpos($url, 'redir/') === 0) {
1010 $sparkle = ' class="sparkle" ';
1015 if ($rr['pending']) {
1016 if (in_array($rr['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
1017 $alt_text = DI::l10n()->t('Pending outgoing contact request');
1019 $alt_text = DI::l10n()->t('Pending incoming contact request');
1024 $dir_icon = 'images/larrow.gif';
1025 $alt_text = DI::l10n()->t('This is you');
1031 'img_hover' => DI::l10n()->t('Visit %s\'s profile [%s]', $rr['name'], $rr['url']),
1032 'edit_hover'=> DI::l10n()->t('Edit contact'),
1033 'photo_menu'=> Model\Contact::photoMenu($rr),
1035 'alt_text' => $alt_text,
1036 'dir_icon' => $dir_icon,
1037 'thumb' => ProxyUtils::proxifyUrl($rr['thumb'], false, ProxyUtils::SIZE_THUMB),
1038 'name' => $rr['name'],
1039 'username' => $rr['name'],
1040 'account_type' => Model\Contact::getAccountType($rr),
1041 'sparkle' => $sparkle,
1042 'itemurl' => ($rr['addr'] ?? '') ?: $rr['url'],
1044 'network' => ContactSelector::networkToName($rr['network'], $rr['url'], $rr['protocol']),
1045 'nick' => $rr['nick'],
1050 * Gives a array with actions which can performed to a given contact
1052 * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
1054 * @param array $contact Data about the Contact
1055 * @return array with contact related actions
1057 private static function getContactActions($contact)
1059 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
1060 $contact_actions = [];
1062 // Provide friend suggestion only for Friendica contacts
1063 if ($contact['network'] === Protocol::DFRN) {
1064 $contact_actions['suggest'] = [
1065 'label' => DI::l10n()->t('Suggest friends'),
1066 'url' => 'fsuggest/' . $contact['id'],
1073 if ($poll_enabled) {
1074 $contact_actions['update'] = [
1075 'label' => DI::l10n()->t('Update now'),
1076 'url' => 'contact/' . $contact['id'] . '/update',
1083 $contact_actions['block'] = [
1084 'label' => (intval($contact['blocked']) ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
1085 'url' => 'contact/' . $contact['id'] . '/block',
1086 'title' => DI::l10n()->t('Toggle Blocked status'),
1087 'sel' => (intval($contact['blocked']) ? 'active' : ''),
1088 'id' => 'toggle-block',
1091 $contact_actions['ignore'] = [
1092 'label' => (intval($contact['readonly']) ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
1093 'url' => 'contact/' . $contact['id'] . '/ignore',
1094 'title' => DI::l10n()->t('Toggle Ignored status'),
1095 'sel' => (intval($contact['readonly']) ? 'active' : ''),
1096 'id' => 'toggle-ignore',
1099 if ($contact['uid'] != 0) {
1100 $contact_actions['archive'] = [
1101 'label' => (intval($contact['archive']) ? DI::l10n()->t('Unarchive') : DI::l10n()->t('Archive')),
1102 'url' => 'contact/' . $contact['id'] . '/archive',
1103 'title' => DI::l10n()->t('Toggle Archive status'),
1104 'sel' => (intval($contact['archive']) ? 'active' : ''),
1105 'id' => 'toggle-archive',
1108 $contact_actions['delete'] = [
1109 'label' => DI::l10n()->t('Delete'),
1110 'url' => 'contact/' . $contact['id'] . '/drop',
1111 'title' => DI::l10n()->t('Delete contact'),
1117 return $contact_actions;