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\Model\Contact as ModelContact;
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\Proxy as ProxyUtils;
45 use Friendica\Util\Strings;
48 * Manages and show Contacts and their content
50 class Contact extends BaseModule
52 private static function batchActions()
54 if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
58 $contacts_id = $_POST['contact_batch'];
60 $stmt = DBA::select('contact', ['id', 'archive'], ['id' => $contacts_id, 'uid' => local_user(), 'self' => false, 'deleted' => false]);
61 $orig_records = DBA::toArray($stmt);
64 foreach ($orig_records as $orig_record) {
65 $contact_id = $orig_record['id'];
66 if (!empty($_POST['contacts_batch_update'])) {
67 self::updateContactFromPoll($contact_id);
70 if (!empty($_POST['contacts_batch_block'])) {
71 self::blockContact($contact_id);
74 if (!empty($_POST['contacts_batch_ignore'])) {
75 self::ignoreContact($contact_id);
78 if (!empty($_POST['contacts_batch_archive'])
79 && self::archiveContact($contact_id, $orig_record)
83 if (!empty($_POST['contacts_batch_drop'])) {
84 self::dropContact($orig_record);
88 if ($count_actions > 0) {
89 info(DI::l10n()->tt('%d contact edited.', '%d contacts edited.', $count_actions));
92 DI::baseUrl()->redirect('contact');
95 public static function post(array $parameters = [])
103 // @TODO: Replace with parameter from router
104 if ($a->argv[1] === 'batch') {
105 self::batchActions();
109 // @TODO: Replace with parameter from router
110 $contact_id = intval($a->argv[1]);
115 if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
116 notice(DI::l10n()->t('Could not access contact record.'));
117 DI::baseUrl()->redirect('contact');
118 return; // NOTREACHED
121 Hook::callAll('contact_edit_post', $_POST);
123 $hidden = !empty($_POST['hidden']);
125 $notify = !empty($_POST['notify']);
127 $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
129 $ffi_keyword_denylist = Strings::escapeHtml(trim($_POST['ffi_keyword_denylist'] ?? ''));
131 $priority = intval($_POST['poll'] ?? 0);
132 if ($priority > 5 || $priority < 0) {
136 $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
138 $r = DBA::update('contact', [
139 'priority' => $priority,
142 'notify_new_posts' => $notify,
143 'fetch_further_information' => $fetch_further_information,
144 'ffi_keyword_denylist' => $ffi_keyword_denylist],
145 ['id' => $contact_id, 'uid' => local_user()]
148 if (!DBA::isResult($r)) {
149 notice(DI::l10n()->t('Failed to update contact record.'));
152 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
153 if (DBA::isResult($contact)) {
154 $a->data['contact'] = $contact;
160 /* contact actions */
162 private static function updateContactFromPoll($contact_id)
164 $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
165 if (!DBA::isResult($contact)) {
169 if ($contact['network'] == Protocol::OSTATUS) {
170 $user = Model\User::getById($contact['uid']);
171 $result = Model\Contact::createFromProbe($user, $contact['url'], false, $contact['network']);
173 if ($result['success']) {
174 DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
177 // pull feed and consume it, which should subscribe to the hub.
178 Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
182 private static function updateContactFromProbe($contact_id)
184 $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
185 if (!DBA::isResult($contact)) {
189 // Update the entry in the contact table
190 Model\Contact::updateFromProbe($contact_id, '', true);
192 // Update the entry in the gcontact table
193 Model\GContact::updateFromProbe($contact['url']);
197 * Toggles the blocked status of a contact identified by id.
202 private static function blockContact($contact_id)
204 $blocked = !Model\Contact::isBlockedByUser($contact_id, local_user());
205 Model\Contact::setBlockedForUser($contact_id, local_user(), $blocked);
209 * Toggles the ignored status of a contact identified by id.
214 private static function ignoreContact($contact_id)
216 $ignored = !Model\Contact::isIgnoredByUser($contact_id, local_user());
217 Model\Contact::setIgnoredForUser($contact_id, local_user(), $ignored);
221 * Toggles the archived status of a contact identified by id.
222 * If the current status isn't provided, this will always archive the contact.
225 * @param $orig_record
229 private static function archiveContact($contact_id, $orig_record)
231 $archived = empty($orig_record['archive']);
232 $r = DBA::update('contact', ['archive' => $archived], ['id' => $contact_id, 'uid' => local_user()]);
234 return DBA::isResult($r);
237 private static function dropContact($orig_record)
239 $owner = Model\User::getOwnerDataById(local_user());
240 if (!DBA::isResult($owner)) {
244 Model\Contact::terminateFriendship($owner, $orig_record, true);
245 Model\Contact::remove($orig_record['id']);
248 public static function content(array $parameters = [], $update = 0)
251 return Login::form($_SERVER['REQUEST_URI']);
256 $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
257 $nets = Strings::escapeTags(trim($_GET['nets'] ?? ''));
258 $rel = Strings::escapeTags(trim($_GET['rel'] ?? ''));
259 $group = Strings::escapeTags(trim($_GET['group'] ?? ''));
261 if (empty(DI::page()['aside'])) {
262 DI::page()['aside'] = '';
267 // @TODO: Replace with parameter from router
268 if ($a->argc == 2 && intval($a->argv[1])
269 || $a->argc == 3 && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])
271 $contact_id = intval($a->argv[1]);
272 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
274 if (!DBA::isResult($contact)) {
275 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => 0, 'deleted' => false]);
278 // Don't display contacts that are about to be deleted
279 if ($contact['network'] == Protocol::PHANTOM) {
283 $contact = ModelContact::checkAvatarCacheArray($contact);
286 if (DBA::isResult($contact)) {
287 if ($contact['self']) {
288 // @TODO: Replace with parameter from router
289 if (($a->argc == 3) && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])) {
290 DI::baseUrl()->redirect('profile/' . $contact['nick']);
292 DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile');
296 $a->data['contact'] = $contact;
298 if (($contact['network'] != '') && ($contact['network'] != Protocol::DFRN)) {
299 $network_link = Strings::formatNetworkName($contact['network'], $contact['url']);
306 if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
307 if ($contact['uid'] && in_array($contact['rel'], [Model\Contact::SHARING, Model\Contact::FRIEND])) {
308 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']);
309 } elseif(!$contact['pending']) {
310 $follow_link = 'follow?url=' . urlencode($contact['url']);
314 $wallmessage_link = '';
315 if ($contact['uid'] && Model\Contact::canReceivePrivateMessages($contact)) {
316 $wallmessage_link = 'message/new/' . $contact['id'];
319 $vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
320 '$name' => $contact['name'],
321 '$photo' => $contact['photo'],
322 '$url' => Model\Contact::magicLinkByContact($contact, $contact['url']),
323 '$addr' => $contact['addr'] ?? '',
324 '$network_link' => $network_link,
325 '$network' => DI::l10n()->t('Network:'),
326 '$account_type' => Model\Contact::getAccountType($contact),
327 '$follow' => DI::l10n()->t('Follow'),
328 '$follow_link' => $follow_link,
329 '$unfollow' => DI::l10n()->t('Unfollow'),
330 '$unfollow_link' => $unfollow_link,
331 '$wallmessage' => DI::l10n()->t('Message'),
332 '$wallmessage_link' => $wallmessage_link,
335 $findpeople_widget = '';
337 $networks_widget = '';
340 if ($contact['uid'] != 0) {
341 $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
347 $findpeople_widget = Widget::findPeople();
348 if (isset($_GET['add'])) {
349 $follow_widget = Widget::follow($_GET['add']);
351 $follow_widget = Widget::follow();
354 $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
355 $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
356 $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group);
359 DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $groups_widget . $networks_widget . $rel_widget;
361 $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
362 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
363 '$baseurl' => DI::baseUrl()->get(true),
367 Nav::setSelected('contact');
370 notice(DI::l10n()->t('Permission denied.'));
371 return Login::form();
375 $contact_id = intval($a->argv[1]);
377 throw new BadRequestException();
380 // @TODO: Replace with parameter from router
383 $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
384 if (!DBA::isResult($orig_record)) {
385 throw new NotFoundException(DI::l10n()->t('Contact not found'));
388 if ($cmd === 'update' && ($orig_record['uid'] != 0)) {
389 self::updateContactFromPoll($contact_id);
390 DI::baseUrl()->redirect('contact/' . $contact_id);
394 if ($cmd === 'updateprofile' && ($orig_record['uid'] != 0)) {
395 self::updateContactFromProbe($contact_id);
396 DI::baseUrl()->redirect('contact/' . $contact_id . '/advanced/');
400 if ($cmd === 'block') {
401 self::blockContact($contact_id);
403 $blocked = Model\Contact::isBlockedByUser($contact_id, local_user());
404 info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')));
406 DI::baseUrl()->redirect('contact/' . $contact_id);
410 if ($cmd === 'ignore') {
411 self::ignoreContact($contact_id);
413 $ignored = Model\Contact::isIgnoredByUser($contact_id, local_user());
414 info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')));
416 DI::baseUrl()->redirect('contact/' . $contact_id);
420 if ($cmd === 'archive' && ($orig_record['uid'] != 0)) {
421 $r = self::archiveContact($contact_id, $orig_record);
423 $archived = (($orig_record['archive']) ? 0 : 1);
424 info((($archived) ? DI::l10n()->t('Contact has been archived') : DI::l10n()->t('Contact has been unarchived')));
427 DI::baseUrl()->redirect('contact/' . $contact_id);
431 if ($cmd === 'drop' && ($orig_record['uid'] != 0)) {
432 // Check if we should do HTML-based delete confirmation
433 if (!empty($_REQUEST['confirm'])) {
434 // <form> can't take arguments in its 'action' parameter
435 // so add any arguments as hidden inputs
436 $query = explode_querystring(DI::args()->getQueryString());
438 foreach ($query['args'] as $arg) {
439 if (strpos($arg, 'confirm=') === false) {
440 $arg_parts = explode('=', $arg);
441 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
445 DI::page()['aside'] = '';
447 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
448 '$header' => DI::l10n()->t('Drop contact'),
449 '$contact' => self::getContactTemplateVars($orig_record),
451 '$message' => DI::l10n()->t('Do you really want to delete this contact?'),
452 '$extra_inputs' => $inputs,
453 '$confirm' => DI::l10n()->t('Yes'),
454 '$confirm_url' => $query['base'],
455 '$confirm_name' => 'confirmed',
456 '$cancel' => DI::l10n()->t('Cancel'),
459 // Now check how the user responded to the confirmation query
460 if (!empty($_REQUEST['canceled'])) {
461 DI::baseUrl()->redirect('contact');
464 self::dropContact($orig_record);
465 info(DI::l10n()->t('Contact has been removed.'));
467 DI::baseUrl()->redirect('contact');
470 if ($cmd === 'posts') {
471 return self::getPostsHTML($a, $contact_id);
473 if ($cmd === 'conversations') {
474 return self::getConversationsHMTL($a, $contact_id, $update);
478 $_SESSION['return_path'] = DI::args()->getQueryString();
480 if (!empty($a->data['contact']) && is_array($a->data['contact'])) {
481 $contact = $a->data['contact'];
483 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
484 '$baseurl' => DI::baseUrl()->get(true),
487 $contact['blocked'] = Model\Contact::isBlockedByUser($contact['id'], local_user());
488 $contact['readonly'] = Model\Contact::isIgnoredByUser($contact['id'], local_user());
492 switch ($contact['rel']) {
493 case Model\Contact::FRIEND:
494 $dir_icon = 'images/lrarrow.gif';
495 $relation_text = DI::l10n()->t('You are mutual friends with %s');
498 case Model\Contact::FOLLOWER;
499 $dir_icon = 'images/larrow.gif';
500 $relation_text = DI::l10n()->t('You are sharing with %s');
503 case Model\Contact::SHARING;
504 $dir_icon = 'images/rarrow.gif';
505 $relation_text = DI::l10n()->t('%s is sharing with you');
512 if ($contact['uid'] == 0) {
516 if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
520 $relation_text = sprintf($relation_text, $contact['name']);
522 $url = Model\Contact::magicLink($contact['url']);
523 if (strpos($url, 'redir/') === 0) {
524 $sparkle = ' class="sparkle" ';
529 $insecure = DI::l10n()->t('Private communications are not available for this contact.');
531 $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? DI::l10n()->t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
533 if ($contact['last-update'] > DBA::NULL_DATETIME) {
534 $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? DI::l10n()->t('(Update was successful)') : DI::l10n()->t('(Update was not successful)'));
536 $lblsuggest = (($contact['network'] === Protocol::DFRN) ? DI::l10n()->t('Suggest friends') : '');
538 $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
540 $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']));
543 $tab_str = self::getTabsHTML($a, $contact, 3);
545 $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : '');
547 $fetch_further_information = null;
548 if ($contact['network'] == Protocol::FEED) {
549 $fetch_further_information = [
550 'fetch_further_information',
551 DI::l10n()->t('Fetch further information for feeds'),
552 $contact['fetch_further_information'],
553 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.'),
555 '0' => DI::l10n()->t('Disabled'),
556 '1' => DI::l10n()->t('Fetch information'),
557 '3' => DI::l10n()->t('Fetch keywords'),
558 '2' => DI::l10n()->t('Fetch information and keywords')
563 $poll_interval = null;
564 if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
565 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
568 // Load contactact related actions like hide, suggest, delete and others
569 $contact_actions = self::getContactActions($contact);
571 if ($contact['uid'] != 0) {
572 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
573 $contact_settings_label = DI::l10n()->t('Contact Settings');
576 $contact_settings_label = null;
579 $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
580 $o .= Renderer::replaceMacros($tpl, [
581 '$header' => DI::l10n()->t('Contact'),
582 '$tab_str' => $tab_str,
583 '$submit' => DI::l10n()->t('Submit'),
584 '$lbl_info1' => $lbl_info1,
585 '$lbl_info2' => DI::l10n()->t('Their personal note'),
586 '$reason' => trim(Strings::escapeTags($contact['reason'])),
587 '$infedit' => DI::l10n()->t('Edit contact notes'),
588 '$common_link' => 'common/loc/' . local_user() . '/' . $contact['id'],
589 '$relation_text' => $relation_text,
590 '$visit' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
591 '$blockunblock' => DI::l10n()->t('Block/Unblock contact'),
592 '$ignorecont' => DI::l10n()->t('Ignore contact'),
593 '$lblrecent' => DI::l10n()->t('View conversations'),
594 '$lblsuggest' => $lblsuggest,
595 '$nettype' => $nettype,
596 '$poll_interval' => $poll_interval,
597 '$poll_enabled' => $poll_enabled,
598 '$lastupdtext' => DI::l10n()->t('Last update:'),
599 '$lost_contact' => $lost_contact,
600 '$updpub' => DI::l10n()->t('Update public posts'),
601 '$last_update' => $last_update,
602 '$udnow' => DI::l10n()->t('Update now'),
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_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')],
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 $sql_values = [local_user()];
649 // @TODO: Replace with parameter from router
650 $type = $a->argv[1] ?? '';
654 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
655 // This makes the query look for contact.uid = 0
656 array_unshift($sql_values, 0);
659 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
662 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
663 // This makes the query look for contact.uid = 0
664 array_unshift($sql_values, 0);
667 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
670 $sql_extra = " AND `pending` AND NOT `archive` AND ((`rel` = ?)
671 OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
672 $sql_values[] = Model\Contact::SHARING;
675 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
683 $search_hdr = $search;
684 $search_txt = preg_quote($search);
685 $sql_extra .= " AND (name REGEXP ? OR url REGEXP ? OR nick REGEXP ?)";
686 $sql_values[] = $search_txt;
687 $sql_values[] = $search_txt;
688 $sql_values[] = $search_txt;
692 $sql_extra .= " AND network = ? ";
693 $sql_values[] = $nets;
698 $sql_extra .= " AND `rel` IN (?, ?)";
699 $sql_values[] = Model\Contact::FOLLOWER;
700 $sql_values[] = Model\Contact::FRIEND;
703 $sql_extra .= " AND `rel` IN (?, ?)";
704 $sql_values[] = Model\Contact::SHARING;
705 $sql_values[] = Model\Contact::FRIEND;
708 $sql_extra .= " AND `rel` = ?";
709 $sql_values[] = Model\Contact::FRIEND;
714 $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
715 $sql_values[] = $group;
719 $stmt = DBA::p("SELECT COUNT(*) AS `total`
725 " . Widget::unavailableNetworks(),
728 if (DBA::isResult($stmt)) {
729 $total = DBA::fetch($stmt)['total'];
733 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
735 $sql_values[] = $pager->getStart();
736 $sql_values[] = $pager->getItemsPerPage();
740 $stmt = DBA::p("SELECT *
750 while ($contact = DBA::fetch($stmt)) {
751 $contact['blocked'] = Model\Contact::isBlockedByUser($contact['id'], local_user());
752 $contact['readonly'] = Model\Contact::isIgnoredByUser($contact['id'], local_user());
753 $contacts[] = self::getContactTemplateVars($contact);
759 'label' => DI::l10n()->t('All Contacts'),
761 'sel' => !$type ? 'active' : '',
762 'title' => DI::l10n()->t('Show all contacts'),
763 'id' => 'showall-tab',
767 'label' => DI::l10n()->t('Pending'),
768 'url' => 'contact/pending',
769 'sel' => $type == 'pending' ? 'active' : '',
770 'title' => DI::l10n()->t('Only show pending contacts'),
771 'id' => 'showpending-tab',
775 'label' => DI::l10n()->t('Blocked'),
776 'url' => 'contact/blocked',
777 'sel' => $type == 'blocked' ? 'active' : '',
778 'title' => DI::l10n()->t('Only show blocked contacts'),
779 'id' => 'showblocked-tab',
783 'label' => DI::l10n()->t('Ignored'),
784 'url' => 'contact/ignored',
785 'sel' => $type == 'ignored' ? 'active' : '',
786 'title' => DI::l10n()->t('Only show ignored contacts'),
787 'id' => 'showignored-tab',
791 'label' => DI::l10n()->t('Archived'),
792 'url' => 'contact/archived',
793 'sel' => $type == 'archived' ? 'active' : '',
794 'title' => DI::l10n()->t('Only show archived contacts'),
795 'id' => 'showarchived-tab',
799 'label' => DI::l10n()->t('Hidden'),
800 'url' => 'contact/hidden',
801 'sel' => $type == 'hidden' ? 'active' : '',
802 'title' => DI::l10n()->t('Only show hidden contacts'),
803 'id' => 'showhidden-tab',
807 'label' => DI::l10n()->t('Groups'),
810 'title' => DI::l10n()->t('Organize your contact groups'),
811 'id' => 'contactgroups-tab',
816 $tabs_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
817 $tabs_html = Renderer::replaceMacros($tabs_tpl, ['$tabs' => $tabs]);
820 case 'followers': $header = DI::l10n()->t('Followers'); break;
821 case 'following': $header = DI::l10n()->t('Following'); break;
822 case 'mutuals': $header = DI::l10n()->t('Mutual friends'); break;
823 default: $header = DI::l10n()->t('Contacts');
827 case 'pending': $header .= ' - ' . DI::l10n()->t('Pending'); break;
828 case 'blocked': $header .= ' - ' . DI::l10n()->t('Blocked'); break;
829 case 'hidden': $header .= ' - ' . DI::l10n()->t('Hidden'); break;
830 case 'ignored': $header .= ' - ' . DI::l10n()->t('Ignored'); break;
831 case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
834 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
836 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
837 $o .= Renderer::replaceMacros($tpl, [
838 '$header' => $header,
839 '$tabs' => $tabs_html,
841 '$search' => $search_hdr,
842 '$desc' => DI::l10n()->t('Search your contacts'),
843 '$finding' => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
844 '$submit' => DI::l10n()->t('Find'),
845 '$cmd' => DI::args()->getCommand(),
846 '$contacts' => $contacts,
847 '$contact_drop_confirm' => DI::l10n()->t('Do you really want to delete this contact?'),
849 '$batch_actions' => [
850 'contacts_batch_update' => DI::l10n()->t('Update'),
851 'contacts_batch_block' => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
852 'contacts_batch_ignore' => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
853 'contacts_batch_archive' => DI::l10n()->t('Archive') . '/' . DI::l10n()->t('Unarchive'),
854 'contacts_batch_drop' => DI::l10n()->t('Delete'),
856 '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
857 '$paginate' => $pager->renderFull($total),
864 * List of pages for the Contact TabBar
866 * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
869 * @param array $contact The contact array
870 * @param int $active_tab 1 if tab should be marked as active
872 * @return string HTML string of the contact page tabs buttons.
873 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
875 public static function getTabsHTML($a, $contact, $active_tab)
880 'label' => DI::l10n()->t('Status'),
881 'url' => "contact/" . $contact['id'] . "/conversations",
882 'sel' => (($active_tab == 1) ? 'active' : ''),
883 'title' => DI::l10n()->t('Conversations started by this contact'),
884 'id' => 'status-tab',
888 'label' => DI::l10n()->t('Posts and Comments'),
889 'url' => "contact/" . $contact['id'] . "/posts",
890 'sel' => (($active_tab == 2) ? 'active' : ''),
891 'title' => DI::l10n()->t('Status Messages and Posts'),
896 'label' => DI::l10n()->t('Profile'),
897 'url' => "contact/" . $contact['id'],
898 'sel' => (($active_tab == 3) ? 'active' : ''),
899 'title' => DI::l10n()->t('Profile Details'),
900 'id' => 'profile-tab',
905 // Show this tab only if there is visible friend list
906 $x = Model\GContact::countAllFriends(local_user(), $contact['id']);
908 $tabs[] = ['label' => DI::l10n()->t('Contacts'),
909 'url' => "allfriends/" . $contact['id'],
910 'sel' => (($active_tab == 4) ? 'active' : ''),
911 'title' => DI::l10n()->t('View all contacts'),
912 'id' => 'allfriends-tab',
916 // Show this tab only if there is visible common friend list
917 $common = Model\GContact::countCommonFriends(local_user(), $contact['id']);
919 $tabs[] = ['label' => DI::l10n()->t('Common Friends'),
920 'url' => "common/loc/" . local_user() . "/" . $contact['id'],
921 'sel' => (($active_tab == 5) ? 'active' : ''),
922 'title' => DI::l10n()->t('View all common friends'),
923 'id' => 'common-loc-tab',
928 if (!empty($contact['uid'])) {
929 $tabs[] = ['label' => DI::l10n()->t('Advanced'),
930 'url' => 'contact/' . $contact['id'] . '/advanced/',
931 'sel' => (($active_tab == 6) ? 'active' : ''),
932 'title' => DI::l10n()->t('Advanced Contact Settings'),
933 'id' => 'advanced-tab',
938 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
939 $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
944 private static function getConversationsHMTL($a, $contact_id, $update)
949 // We need the editor here to be able to reshare an item.
953 'allow_location' => $a->user['allow_location'],
954 'default_location' => $a->user['default-location'],
955 'nickname' => $a->user['nickname'],
956 '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'),
957 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
959 'visitor' => 'block',
960 'profile_uid' => local_user(),
962 $o = status_editor($a, $x, 0, true);
966 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
969 $o .= self::getTabsHTML($a, $contact, 1);
972 if (DBA::isResult($contact)) {
973 DI::page()['aside'] = '';
975 $profiledata = Model\Contact::getByURL($contact['url'], false);
977 Model\Profile::load($a, '', $profiledata, true);
979 if ($contact['uid'] == 0) {
980 $o .= Model\Contact::getPostsFromId($contact['id'], true, $update);
982 $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update);
989 private static function getPostsHTML($a, $contact_id)
991 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
993 $o = self::getTabsHTML($a, $contact, 2);
995 if (DBA::isResult($contact)) {
996 DI::page()['aside'] = '';
998 $profiledata = Model\Contact::getByURL($contact['url'], false);
1000 if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
1001 $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
1004 Model\Profile::load($a, '', $profiledata, true);
1006 if ($contact['uid'] == 0) {
1007 $o .= Model\Contact::getPostsFromId($contact['id']);
1009 $o .= Model\Contact::getPostsFromUrl($contact['url']);
1016 public static function getContactTemplateVars(array $rr)
1021 if (!empty($rr['uid']) && !empty($rr['rel'])) {
1022 switch ($rr['rel']) {
1023 case Model\Contact::FRIEND:
1024 $dir_icon = 'images/lrarrow.gif';
1025 $alt_text = DI::l10n()->t('Mutual Friendship');
1028 case Model\Contact::FOLLOWER;
1029 $dir_icon = 'images/larrow.gif';
1030 $alt_text = DI::l10n()->t('is a fan of yours');
1033 case Model\Contact::SHARING;
1034 $dir_icon = 'images/rarrow.gif';
1035 $alt_text = DI::l10n()->t('you are a fan of');
1043 $url = Model\Contact::magicLink($rr['url']);
1045 if (strpos($url, 'redir/') === 0) {
1046 $sparkle = ' class="sparkle" ';
1051 if ($rr['pending']) {
1052 if (in_array($rr['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
1053 $alt_text = DI::l10n()->t('Pending outgoing contact request');
1055 $alt_text = DI::l10n()->t('Pending incoming contact request');
1060 $dir_icon = 'images/larrow.gif';
1061 $alt_text = DI::l10n()->t('This is you');
1067 'img_hover' => DI::l10n()->t('Visit %s\'s profile [%s]', $rr['name'], $rr['url']),
1068 'edit_hover'=> DI::l10n()->t('Edit contact'),
1069 'photo_menu'=> Model\Contact::photoMenu($rr),
1071 'alt_text' => $alt_text,
1072 'dir_icon' => $dir_icon,
1073 'thumb' => ProxyUtils::proxifyUrl($rr['thumb'], false, ProxyUtils::SIZE_THUMB),
1074 'name' => $rr['name'],
1075 'username' => $rr['name'],
1076 'account_type' => Model\Contact::getAccountType($rr),
1077 'sparkle' => $sparkle,
1078 'itemurl' => ($rr['addr'] ?? '') ?: $rr['url'],
1080 'network' => ContactSelector::networkToName($rr['network'], $rr['url'], $rr['protocol']),
1081 'nick' => $rr['nick'],
1086 * Gives a array with actions which can performed to a given contact
1088 * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
1090 * @param array $contact Data about the Contact
1091 * @return array with contact related actions
1093 private static function getContactActions($contact)
1095 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
1096 $contact_actions = [];
1098 // Provide friend suggestion only for Friendica contacts
1099 if ($contact['network'] === Protocol::DFRN) {
1100 $contact_actions['suggest'] = [
1101 'label' => DI::l10n()->t('Suggest friends'),
1102 'url' => 'fsuggest/' . $contact['id'],
1109 if ($poll_enabled) {
1110 $contact_actions['update'] = [
1111 'label' => DI::l10n()->t('Update now'),
1112 'url' => 'contact/' . $contact['id'] . '/update',
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;