3 * @copyright Copyright (C) 2010-2021, the Friendica project
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;
24 use Friendica\BaseModule;
25 use Friendica\Content\ContactSelector;
26 use Friendica\Content\Nav;
27 use Friendica\Content\Pager;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Content\Widget;
30 use Friendica\Core\Hook;
31 use Friendica\Core\Protocol;
32 use Friendica\Core\Renderer;
33 use Friendica\Core\Theme;
34 use Friendica\Core\Worker;
35 use Friendica\Database\DBA;
38 use Friendica\Model\User;
39 use Friendica\Module\Security\Login;
40 use Friendica\Network\HTTPException\BadRequestException;
41 use Friendica\Network\HTTPException\NotFoundException;
42 use Friendica\Util\DateTimeFormat;
43 use Friendica\Util\Strings;
46 * Manages and show Contacts and their content
48 class Contact extends BaseModule
50 const TAB_CONVERSATIONS = 1;
52 const TAB_PROFILE = 3;
53 const TAB_CONTACTS = 4;
54 const TAB_ADVANCED = 5;
56 private static function batchActions()
58 if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
62 $redirectUrl = $_POST['redirect_url'] ?? 'contact';
64 self::checkFormSecurityTokenRedirectOnError($redirectUrl, 'contact_batch_actions');
66 $orig_records = Model\Contact::selectToArray(['id', 'uid'], ['id' => $_POST['contact_batch'], 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
69 foreach ($orig_records as $orig_record) {
70 $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user());
75 if (!empty($_POST['contacts_batch_update']) && $cdata['user']) {
76 self::updateContactFromPoll($cdata['user']);
80 if (!empty($_POST['contacts_batch_block'])) {
81 self::toggleBlockContact($cdata['public']);
85 if (!empty($_POST['contacts_batch_ignore'])) {
86 self::toggleIgnoreContact($cdata['public']);
90 if (!empty($_POST['contacts_batch_drop']) && $cdata['user']
91 && self::dropContact($cdata['user'], local_user())
96 if ($count_actions > 0) {
97 info(DI::l10n()->tt('%d contact edited.', '%d contacts edited.', $count_actions));
100 DI::baseUrl()->redirect($redirectUrl);
103 public static function post(array $parameters = [])
109 // @TODO: Replace with parameter from router
110 if (DI::args()->getArgv()[1] === 'batch') {
111 self::batchActions();
115 // @TODO: Replace with parameter from router
116 $contact_id = intval(DI::args()->getArgv()[1]);
121 if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
122 notice(DI::l10n()->t('Could not access contact record.'));
123 DI::baseUrl()->redirect('contact');
124 return; // NOTREACHED
127 Hook::callAll('contact_edit_post', $_POST);
129 $hidden = !empty($_POST['hidden']);
131 $notify = !empty($_POST['notify']);
133 $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
135 $remote_self = $_POST['remote_self'] ?? false;
137 $ffi_keyword_denylist = Strings::escapeHtml(trim($_POST['ffi_keyword_denylist'] ?? ''));
139 $priority = intval($_POST['poll'] ?? 0);
140 if ($priority > 5 || $priority < 0) {
144 $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
146 $r = DBA::update('contact', [
147 'priority' => $priority,
150 'notify_new_posts' => $notify,
151 'fetch_further_information' => $fetch_further_information,
152 'remote_self' => $remote_self,
153 'ffi_keyword_denylist' => $ffi_keyword_denylist],
154 ['id' => $contact_id, 'uid' => local_user()]
157 if (!DBA::isResult($r)) {
158 notice(DI::l10n()->t('Failed to update contact record.'));
163 /* contact actions */
166 * @param int $contact_id Id of contact with uid != 0
167 * @throws NotFoundException
168 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
169 * @throws \ImagickException
171 private static function updateContactFromPoll(int $contact_id)
173 $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
174 if (!DBA::isResult($contact)) {
178 if ($contact['network'] == Protocol::OSTATUS) {
179 $result = Model\Contact::createFromProbeForUser($contact['uid'], $contact['url'], $contact['network']);
181 if ($result['success']) {
182 DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
185 // pull feed and consume it, which should subscribe to the hub.
186 Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
188 Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id);
193 * @param int $contact_id Id of the contact with uid != 0
194 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
195 * @throws \ImagickException
197 private static function updateContactFromProbe(int $contact_id)
199 $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
200 if (!DBA::isResult($contact)) {
204 // Update the entry in the contact table
205 Model\Contact::updateFromProbe($contact_id);
209 * Toggles the blocked status of a contact identified by id.
211 * @param int $contact_id Id of the contact with uid = 0
214 private static function toggleBlockContact(int $contact_id)
216 $blocked = !Model\Contact\User::isBlocked($contact_id, local_user());
217 Model\Contact\User::setBlocked($contact_id, local_user(), $blocked);
221 * Toggles the ignored status of a contact identified by id.
223 * @param int $contact_id Id of the contact with uid = 0
226 private static function toggleIgnoreContact(int $contact_id)
228 $ignored = !Model\Contact\User::isIgnored($contact_id, local_user());
229 Model\Contact\User::setIgnored($contact_id, local_user(), $ignored);
233 * @param int $contact_id Id for contact with uid != 0
234 * @param int $uid Id for user we want to drop the contact for
236 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
237 * @throws \ImagickException
239 private static function dropContact(int $contact_id, int $uid): bool
241 $contact = Model\Contact::getContactForUser($contact_id, $uid);
242 if (!DBA::isResult($contact)) {
246 $owner = Model\User::getOwnerDataById($uid);
247 if (!DBA::isResult($owner)) {
251 Model\Contact::terminateFriendship($owner, $contact, true);
252 Model\Contact::remove($contact['id']);
257 public static function content(array $parameters = [], $update = 0)
260 return Login::form($_SERVER['REQUEST_URI']);
265 $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
266 $nets = Strings::escapeTags(trim($_GET['nets'] ?? ''));
267 $rel = Strings::escapeTags(trim($_GET['rel'] ?? ''));
268 $group = Strings::escapeTags(trim($_GET['group'] ?? ''));
270 $accounttype = $_GET['accounttype'] ?? '';
271 $accounttypeid = User::getAccountTypeByString($accounttype);
275 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
276 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
277 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
278 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
281 // @TODO: Replace with parameter from router
282 if (DI::args()->getArgc() == 2 && intval(DI::args()->getArgv()[1])
283 || DI::args()->getArgc() == 3 && intval(DI::args()->getArgv()[1]) && in_array(DI::args()->getArgv()[2], ['posts', 'conversations', 'media'])
285 $contact_id = intval(DI::args()->getArgv()[1]);
287 // Ensure to use the user contact when the public contact was provided
288 $data = Model\Contact::getPublicAndUserContactID($contact_id, local_user());
289 if (!empty($data['user']) && ($contact_id == $data['public'])) {
290 $contact_id = $data['user'];
294 $contact = DBA::selectFirst('contact', [], [
296 'uid' => [0, local_user()],
300 // Don't display contacts that are about to be deleted
301 if (DBA::isResult($contact) && !empty($contact['network']) && $contact['network'] == Protocol::PHANTOM) {
307 if (DBA::isResult($contact)) {
308 if ($contact['self']) {
309 // @TODO: Replace with parameter from router
310 if ((DI::args()->getArgc() == 3) && intval(DI::args()->getArgv()[1]) && in_array(DI::args()->getArgv()[2], ['posts', 'conversations', 'media'])) {
311 DI::baseUrl()->redirect('profile/' . $contact['nick']);
313 DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile');
317 $vcard_widget = Widget\VCard::getHTML($contact);
319 $findpeople_widget = '';
321 $account_widget = '';
322 $networks_widget = '';
325 if ($contact['uid'] != 0) {
326 $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
332 $findpeople_widget = Widget::findPeople();
333 if (isset($_GET['add'])) {
334 $follow_widget = Widget::follow($_GET['add']);
336 $follow_widget = Widget::follow();
339 $account_widget = Widget::accounttypes($_SERVER['REQUEST_URI'], $accounttype);
340 $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
341 $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
342 $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group);
345 DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $account_widget . $groups_widget . $networks_widget . $rel_widget;
347 $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
348 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
349 '$baseurl' => DI::baseUrl()->get(true),
353 Nav::setSelected('contact');
356 notice(DI::l10n()->t('Permission denied.'));
357 return Login::form();
360 if (DI::args()->getArgc() == 3) {
361 $contact_id = intval(DI::args()->getArgv()[1]);
363 throw new BadRequestException();
366 // @TODO: Replace with parameter from router
367 $cmd = DI::args()->getArgv()[2];
369 $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
370 if (!DBA::isResult($orig_record)) {
371 throw new NotFoundException(DI::l10n()->t('Contact not found'));
374 if ($cmd === 'posts') {
375 return self::getPostsHTML($a, $contact_id);
378 if ($cmd === 'media') {
379 return self::getPostsHTML($a, $contact_id); // TODO
382 if ($cmd === 'conversations') {
383 return self::getConversationsHMTL($a, $contact_id, $update);
386 self::checkFormSecurityTokenRedirectOnError('contact/' . $contact_id, 'contact_action', 't');
388 $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user());
390 throw new NotFoundException(DI::l10n()->t('Contact not found'));
393 if ($cmd === 'update' && $cdata['user']) {
394 self::updateContactFromPoll($cdata['user']);
395 DI::baseUrl()->redirect('contact/' . $cdata['public']);
399 if ($cmd === 'updateprofile' && $cdata['user']) {
400 self::updateContactFromProbe($cdata['user']);
401 DI::baseUrl()->redirect('contact/' . $cdata['public']);
405 if ($cmd === 'block') {
406 if (public_contact() === $cdata['public']) {
407 throw new BadRequestException(DI::l10n()->t('You can\'t block yourself'));
410 self::toggleBlockContact($cdata['public']);
412 $blocked = Model\Contact\User::isBlocked($contact_id, local_user());
413 info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')));
415 DI::baseUrl()->redirect('contact/' . $cdata['public']);
419 if ($cmd === 'ignore') {
420 if (public_contact() === $cdata['public']) {
421 throw new BadRequestException(DI::l10n()->t('You can\'t ignore yourself'));
424 self::toggleIgnoreContact($cdata['public']);
426 $ignored = Model\Contact\User::isIgnored($cdata['public'], local_user());
427 info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')));
429 DI::baseUrl()->redirect('contact/' . $cdata['public']);
433 if ($cmd === 'drop' && $cdata['user']) {
434 // Check if we should do HTML-based delete confirmation
435 if (!empty($_REQUEST['confirm'])) {
436 DI::page()['aside'] = '';
438 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
439 '$header' => DI::l10n()->t('Drop contact'),
440 '$contact' => self::getContactTemplateVars($orig_record),
442 '$message' => DI::l10n()->t('Do you really want to delete this contact?'),
443 '$confirm' => DI::l10n()->t('Yes'),
444 '$confirm_url' => DI::args()->getCommand(),
445 '$confirm_name' => 't',
446 '$confirm_value' => BaseModule::getFormSecurityToken('contact_action'),
447 '$cancel' => DI::l10n()->t('Cancel'),
450 // Now check how the user responded to the confirmation query
451 if (!empty($_REQUEST['canceled'])) {
452 DI::baseUrl()->redirect('contact');
455 if (self::dropContact($cdata['user'], local_user())) {
456 info(DI::l10n()->t('Contact has been removed.'));
459 DI::baseUrl()->redirect('contact');
464 $_SESSION['return_path'] = DI::args()->getQueryString();
466 if (!empty($contact)) {
467 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
468 '$baseurl' => DI::baseUrl()->get(true),
471 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
472 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
475 switch ($contact['rel']) {
476 case Model\Contact::FRIEND:
477 $relation_text = DI::l10n()->t('You are mutual friends with %s');
480 case Model\Contact::FOLLOWER;
481 $relation_text = DI::l10n()->t('You are sharing with %s');
484 case Model\Contact::SHARING;
485 $relation_text = DI::l10n()->t('%s is sharing with you');
492 if ($contact['uid'] == 0) {
496 if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
500 $relation_text = sprintf($relation_text, $contact['name']);
502 $url = Model\Contact::magicLinkByContact($contact);
503 if (strpos($url, 'redir/') === 0) {
504 $sparkle = ' class="sparkle" ';
509 $insecure = DI::l10n()->t('Private communications are not available for this contact.');
511 $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? DI::l10n()->t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
513 if ($contact['last-update'] > DBA::NULL_DATETIME) {
514 $last_update .= ' ' . ($contact['failed'] ? DI::l10n()->t('(Update was not successful)') : DI::l10n()->t('(Update was successful)'));
516 $lblsuggest = (($contact['network'] === Protocol::DFRN) ? DI::l10n()->t('Suggest friends') : '');
518 $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
520 $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol'], $contact['gsid']));
523 $tab_str = self::getTabsHTML($contact, self::TAB_PROFILE);
525 $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : '');
527 $fetch_further_information = null;
528 if ($contact['network'] == Protocol::FEED) {
529 $fetch_further_information = [
530 'fetch_further_information',
531 DI::l10n()->t('Fetch further information for feeds'),
532 $contact['fetch_further_information'],
533 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.'),
535 '0' => DI::l10n()->t('Disabled'),
536 '1' => DI::l10n()->t('Fetch information'),
537 '3' => DI::l10n()->t('Fetch keywords'),
538 '2' => DI::l10n()->t('Fetch information and keywords')
543 // Disable remote self for everything except feeds.
544 // There is an issue when you repeat an item from maybe twitter and you got comments from friendica and twitter
545 // Problem is, you couldn't reply to both networks.
546 $allow_remote_self = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::FEED, Protocol::DFRN, Protocol::DIASPORA, Protocol::TWITTER])
547 && DI::config()->get('system', 'allow_users_remote_self');
549 if ($contact['network'] == Protocol::FEED) {
550 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
551 Model\Contact::MIRROR_FORWARDED => DI::l10n()->t('Mirror as forwarded posting'),
552 Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting')];
553 } elseif (in_array($contact['network'], [Protocol::ACTIVITYPUB])) {
554 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
555 Model\Contact::MIRROR_NATIVE_RESHARE => DI::l10n()->t('Native reshare')];
556 } elseif (in_array($contact['network'], [Protocol::DFRN])) {
557 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
558 Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting'),
559 Model\Contact::MIRROR_NATIVE_RESHARE => DI::l10n()->t('Native reshare')];
561 $remote_self_options = [Model\Contact::MIRROR_DEACTIVATED => DI::l10n()->t('No mirroring'),
562 Model\Contact::MIRROR_OWN_POST => DI::l10n()->t('Mirror as my own posting')];
565 $poll_interval = null;
566 if ((($contact['network'] == Protocol::FEED) && !DI::config()->get('system', 'adjust_poll_frequency')) || ($contact['network'] == Protocol::MAIL)) {
567 $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
570 // Load contactact related actions like hide, suggest, delete and others
571 $contact_actions = self::getContactActions($contact);
573 if ($contact['uid'] != 0) {
574 $lbl_info1 = DI::l10n()->t('Contact Information / Notes');
575 $contact_settings_label = DI::l10n()->t('Contact Settings');
578 $contact_settings_label = null;
581 $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
582 $o .= Renderer::replaceMacros($tpl, [
583 '$header' => DI::l10n()->t('Contact'),
584 '$tab_str' => $tab_str,
585 '$submit' => DI::l10n()->t('Submit'),
586 '$lbl_info1' => $lbl_info1,
587 '$lbl_info2' => DI::l10n()->t('Their personal note'),
588 '$reason' => trim(Strings::escapeTags($contact['reason'])),
589 '$infedit' => DI::l10n()->t('Edit contact notes'),
590 '$common_link' => 'contact/' . $contact['id'] . '/contacts/common',
591 '$relation_text' => $relation_text,
592 '$visit' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
593 '$blockunblock' => DI::l10n()->t('Block/Unblock contact'),
594 '$ignorecont' => DI::l10n()->t('Ignore contact'),
595 '$lblrecent' => DI::l10n()->t('View conversations'),
596 '$lblsuggest' => $lblsuggest,
597 '$nettype' => $nettype,
598 '$poll_interval' => $poll_interval,
599 '$poll_enabled' => $poll_enabled,
600 '$lastupdtext' => DI::l10n()->t('Last update:'),
601 '$lost_contact' => $lost_contact,
602 '$updpub' => DI::l10n()->t('Update public posts'),
603 '$last_update' => $last_update,
604 '$udnow' => DI::l10n()->t('Update now'),
605 '$contact_id' => $contact['id'],
606 '$block_text' => ($contact['blocked'] ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
607 '$ignore_text' => ($contact['readonly'] ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
608 '$insecure' => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
609 '$info' => $contact['info'],
610 '$cinfo' => ['info', '', $contact['info'], ''],
611 '$blocked' => ($contact['blocked'] ? DI::l10n()->t('Currently blocked') : ''),
612 '$ignored' => ($contact['readonly'] ? DI::l10n()->t('Currently ignored') : ''),
613 '$archived' => ($contact['archive'] ? DI::l10n()->t('Currently archived') : ''),
614 '$pending' => ($contact['pending'] ? DI::l10n()->t('Awaiting connection acknowledge') : ''),
615 '$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')],
616 '$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')],
617 '$fetch_further_information' => $fetch_further_information,
618 '$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')],
619 '$photo' => Model\Contact::getPhoto($contact),
620 '$name' => $contact['name'],
621 '$sparkle' => $sparkle,
623 '$profileurllabel'=> DI::l10n()->t('Profile URL'),
624 '$profileurl' => $contact['url'],
625 '$account_type' => Model\Contact::getAccountType($contact),
626 '$location' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['location']),
627 '$location_label' => DI::l10n()->t('Location:'),
628 '$xmpp' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['xmpp']),
629 '$xmpp_label' => DI::l10n()->t('XMPP:'),
630 '$matrix' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['matrix']),
631 '$matrix_label' => DI::l10n()->t('Matrix:'),
632 '$about' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['about'], BBCode::EXTERNAL),
633 '$about_label' => DI::l10n()->t('About:'),
634 '$keywords' => $contact['keywords'],
635 '$keywords_label' => DI::l10n()->t('Tags:'),
636 '$contact_action_button' => DI::l10n()->t('Actions'),
637 '$contact_actions'=> $contact_actions,
638 '$contact_status' => DI::l10n()->t('Status'),
639 '$contact_settings_label' => $contact_settings_label,
640 '$contact_profile_label' => DI::l10n()->t('Profile'),
641 '$allow_remote_self' => $allow_remote_self,
642 '$remote_self' => ['remote_self',
643 DI::l10n()->t('Mirror postings from this contact'),
644 $contact['remote_self'],
645 DI::l10n()->t('Mark this contact as remote_self, this will cause friendica to repost new entries from this contact.'),
650 $arr = ['contact' => $contact, 'output' => $o];
652 Hook::callAll('contact_edit', $arr);
654 return $arr['output'];
657 $sql_values = [local_user()];
659 // @TODO: Replace with parameter from router
660 $type = DI::args()->getArgv()[1] ?? '';
664 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)";
665 // This makes the query look for contact.uid = 0
666 array_unshift($sql_values, 0);
669 $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
672 $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)";
673 // This makes the query look for contact.uid = 0
674 array_unshift($sql_values, 0);
677 $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
680 $sql_extra = " AND `pending` AND NOT `archive` AND NOT `failed` AND ((`rel` = ?)
681 OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))";
682 $sql_values[] = Model\Contact::SHARING;
685 $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
689 if (isset($accounttypeid)) {
690 $sql_extra .= " AND `contact-type` = ?";
691 $sql_values[] = $accounttypeid;
698 $search_hdr = $search;
699 $search_txt = preg_quote($search);
700 $sql_extra .= " AND (name REGEXP ? OR url REGEXP ? OR nick REGEXP ?)";
701 $sql_values[] = $search_txt;
702 $sql_values[] = $search_txt;
703 $sql_values[] = $search_txt;
707 $sql_extra .= " AND network = ? ";
708 $sql_values[] = $nets;
713 $sql_extra .= " AND `rel` IN (?, ?)";
714 $sql_values[] = Model\Contact::FOLLOWER;
715 $sql_values[] = Model\Contact::FRIEND;
718 $sql_extra .= " AND `rel` IN (?, ?)";
719 $sql_values[] = Model\Contact::SHARING;
720 $sql_values[] = Model\Contact::FRIEND;
723 $sql_extra .= " AND `rel` = ?";
724 $sql_values[] = Model\Contact::FRIEND;
729 $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
730 $sql_values[] = $group;
734 $stmt = DBA::p("SELECT COUNT(*) AS `total`
740 " . Widget::unavailableNetworks(),
743 if (DBA::isResult($stmt)) {
744 $total = DBA::fetch($stmt)['total'];
748 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
750 $sql_values[] = $pager->getStart();
751 $sql_values[] = $pager->getItemsPerPage();
755 $stmt = DBA::p("SELECT *
765 while ($contact = DBA::fetch($stmt)) {
766 $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
767 $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());
768 $contacts[] = self::getContactTemplateVars($contact);
774 'label' => DI::l10n()->t('All Contacts'),
776 'sel' => !$type ? 'active' : '',
777 'title' => DI::l10n()->t('Show all contacts'),
778 'id' => 'showall-tab',
782 'label' => DI::l10n()->t('Pending'),
783 'url' => 'contact/pending',
784 'sel' => $type == 'pending' ? 'active' : '',
785 'title' => DI::l10n()->t('Only show pending contacts'),
786 'id' => 'showpending-tab',
790 'label' => DI::l10n()->t('Blocked'),
791 'url' => 'contact/blocked',
792 'sel' => $type == 'blocked' ? 'active' : '',
793 'title' => DI::l10n()->t('Only show blocked contacts'),
794 'id' => 'showblocked-tab',
798 'label' => DI::l10n()->t('Ignored'),
799 'url' => 'contact/ignored',
800 'sel' => $type == 'ignored' ? 'active' : '',
801 'title' => DI::l10n()->t('Only show ignored contacts'),
802 'id' => 'showignored-tab',
806 'label' => DI::l10n()->t('Archived'),
807 'url' => 'contact/archived',
808 'sel' => $type == 'archived' ? 'active' : '',
809 'title' => DI::l10n()->t('Only show archived contacts'),
810 'id' => 'showarchived-tab',
814 'label' => DI::l10n()->t('Hidden'),
815 'url' => 'contact/hidden',
816 'sel' => $type == 'hidden' ? 'active' : '',
817 'title' => DI::l10n()->t('Only show hidden contacts'),
818 'id' => 'showhidden-tab',
822 'label' => DI::l10n()->t('Groups'),
825 'title' => DI::l10n()->t('Organize your contact groups'),
826 'id' => 'contactgroups-tab',
831 $tabs_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
832 $tabs_html = Renderer::replaceMacros($tabs_tpl, ['$tabs' => $tabs]);
835 case 'followers': $header = DI::l10n()->t('Followers'); break;
836 case 'following': $header = DI::l10n()->t('Following'); break;
837 case 'mutuals': $header = DI::l10n()->t('Mutual friends'); break;
838 default: $header = DI::l10n()->t('Contacts');
842 case 'pending': $header .= ' - ' . DI::l10n()->t('Pending'); break;
843 case 'blocked': $header .= ' - ' . DI::l10n()->t('Blocked'); break;
844 case 'hidden': $header .= ' - ' . DI::l10n()->t('Hidden'); break;
845 case 'ignored': $header .= ' - ' . DI::l10n()->t('Ignored'); break;
846 case 'archived': $header .= ' - ' . DI::l10n()->t('Archived'); break;
849 $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
851 $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
852 $o .= Renderer::replaceMacros($tpl, [
853 '$header' => $header,
854 '$tabs' => $tabs_html,
856 '$search' => $search_hdr,
857 '$desc' => DI::l10n()->t('Search your contacts'),
858 '$finding' => $searching ? DI::l10n()->t('Results for: %s', $search) : '',
859 '$submit' => DI::l10n()->t('Find'),
860 '$cmd' => DI::args()->getCommand(),
861 '$contacts' => $contacts,
862 '$form_security_token' => BaseModule::getFormSecurityToken('contact_batch_actions'),
863 '$contact_drop_confirm' => DI::l10n()->t('Do you really want to delete this contact?'),
865 '$batch_actions' => [
866 'contacts_batch_update' => DI::l10n()->t('Update'),
867 'contacts_batch_block' => DI::l10n()->t('Block') . '/' . DI::l10n()->t('Unblock'),
868 'contacts_batch_ignore' => DI::l10n()->t('Ignore') . '/' . DI::l10n()->t('Unignore'),
869 'contacts_batch_drop' => DI::l10n()->t('Delete'),
871 '$h_batch_actions' => DI::l10n()->t('Batch Actions'),
872 '$paginate' => $pager->renderFull($total),
879 * List of pages for the Contact TabBar
881 * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
883 * @param array $contact The contact array
884 * @param int $active_tab 1 if tab should be marked as active
886 * @return string HTML string of the contact page tabs buttons.
887 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
888 * @throws \ImagickException
890 public static function getTabsHTML(array $contact, int $active_tab)
892 $cid = $pcid = $contact['id'];
893 $data = Model\Contact::getPublicAndUserContactID($contact['id'], local_user());
894 if (!empty($data['user']) && ($contact['id'] == $data['public'])) {
895 $cid = $data['user'];
896 } elseif (!empty($data['public'])) {
897 $pcid = $data['public'];
903 'label' => DI::l10n()->t('Status'),
904 'url' => 'contact/' . $pcid . '/conversations',
905 'sel' => (($active_tab == self::TAB_CONVERSATIONS) ? 'active' : ''),
906 'title' => DI::l10n()->t('Conversations started by this contact'),
907 'id' => 'status-tab',
911 'label' => DI::l10n()->t('Posts and Comments'),
912 'url' => 'contact/' . $pcid . '/posts',
913 'sel' => (($active_tab == self::TAB_POSTS) ? 'active' : ''),
914 'title' => DI::l10n()->t('Status Messages and Posts'),
919 'label' => DI::l10n()->t('Profile'),
920 'url' => 'contact/' . $cid,
921 'sel' => (($active_tab == self::TAB_PROFILE) ? 'active' : ''),
922 'title' => DI::l10n()->t('Profile Details'),
923 'id' => 'profile-tab',
926 ['label' => DI::l10n()->t('Contacts'),
927 'url' => 'contact/' . $pcid . '/contacts',
928 'sel' => (($active_tab == self::TAB_CONTACTS) ? 'active' : ''),
929 'title' => DI::l10n()->t('View all known contacts'),
930 'id' => 'contacts-tab',
935 if (!empty($contact['network']) && in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) && ($cid != $pcid)) {
936 $tabs[] = ['label' => DI::l10n()->t('Advanced'),
937 'url' => 'contact/' . $cid . '/advanced/',
938 'sel' => (($active_tab == self::TAB_ADVANCED) ? 'active' : ''),
939 'title' => DI::l10n()->t('Advanced Contact Settings'),
940 'id' => 'advanced-tab',
945 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
946 $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
951 public static function getConversationsHMTL($a, $contact_id, $update, $parent = 0)
956 // We need the editor here to be able to reshare an item.
958 $o = DI::conversation()->statusEditor([], 0, true);
962 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
965 $o .= self::getTabsHTML($contact, self::TAB_CONVERSATIONS);
968 if (DBA::isResult($contact)) {
970 $profiledata = Model\Contact::getByURLForUser($contact['url'], local_user());
971 DI::page()['aside'] = Widget\VCard::getHTML($profiledata);
973 DI::page()['aside'] = '';
976 if ($contact['uid'] == 0) {
977 $o .= Model\Contact::getPostsFromId($contact['id'], true, $update, $parent);
979 $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update, $parent);
986 private static function getPostsHTML($a, $contact_id)
988 $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
990 $o = self::getTabsHTML($contact, self::TAB_POSTS);
992 if (DBA::isResult($contact)) {
993 $profiledata = Model\Contact::getByURLForUser($contact['url'], local_user());
995 if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
996 $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
999 DI::page()['aside'] = Widget\VCard::getHTML($profiledata);
1001 if ($contact['uid'] == 0) {
1002 $o .= Model\Contact::getPostsFromId($contact['id']);
1004 $o .= Model\Contact::getPostsFromUrl($contact['url']);
1012 * Return the fields for the contact template
1014 * @param array $contact Contact array
1015 * @return array Template fields
1017 public static function getContactTemplateVars(array $contact)
1021 if (!empty($contact['url']) && isset($contact['uid']) && ($contact['uid'] == 0) && local_user()) {
1022 $personal = Model\Contact::getByURL($contact['url'], false, ['uid', 'rel', 'self'], local_user());
1023 if (!empty($personal)) {
1024 $contact['uid'] = $personal['uid'];
1025 $contact['rel'] = $personal['rel'];
1026 $contact['self'] = $personal['self'];
1030 if (!empty($contact['uid']) && !empty($contact['rel']) && local_user() == $contact['uid']) {
1031 switch ($contact['rel']) {
1032 case Model\Contact::FRIEND:
1033 $alt_text = DI::l10n()->t('Mutual Friendship');
1036 case Model\Contact::FOLLOWER;
1037 $alt_text = DI::l10n()->t('is a fan of yours');
1040 case Model\Contact::SHARING;
1041 $alt_text = DI::l10n()->t('you are a fan of');
1049 $url = Model\Contact::magicLinkByContact($contact);
1051 if (strpos($url, 'redir/') === 0) {
1052 $sparkle = ' class="sparkle" ';
1057 if ($contact['pending']) {
1058 if (in_array($contact['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
1059 $alt_text = DI::l10n()->t('Pending outgoing contact request');
1061 $alt_text = DI::l10n()->t('Pending incoming contact request');
1065 if ($contact['self']) {
1066 $alt_text = DI::l10n()->t('This is you');
1067 $url = $contact['url'];
1072 'id' => $contact['id'],
1074 'img_hover' => DI::l10n()->t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
1075 'photo_menu' => Model\Contact::photoMenu($contact),
1076 'thumb' => Model\Contact::getThumb($contact, true),
1077 'alt_text' => $alt_text,
1078 'name' => $contact['name'],
1079 'nick' => $contact['nick'],
1080 'details' => $contact['location'],
1081 'tags' => $contact['keywords'],
1082 'about' => $contact['about'],
1083 'account_type' => Model\Contact::getAccountType($contact),
1084 'sparkle' => $sparkle,
1085 'itemurl' => ($contact['addr'] ?? '') ?: $contact['url'],
1086 'network' => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol'], $contact['gsid']),
1091 * Gives a array with actions which can performed to a given contact
1093 * This includes actions like e.g. 'block', 'hide', 'delete' and others
1095 * @param array $contact Data about the Contact
1096 * @return array with contact related actions
1098 private static function getContactActions($contact)
1100 $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
1101 $contact_actions = [];
1103 $formSecurityToken = self::getFormSecurityToken('contact_action');
1105 // Provide friend suggestion only for Friendica contacts
1106 if ($contact['network'] === Protocol::DFRN) {
1107 $contact_actions['suggest'] = [
1108 'label' => DI::l10n()->t('Suggest friends'),
1109 'url' => 'fsuggest/' . $contact['id'],
1116 if ($poll_enabled) {
1117 $contact_actions['update'] = [
1118 'label' => DI::l10n()->t('Update now'),
1119 'url' => 'contact/' . $contact['id'] . '/update?t=' . $formSecurityToken,
1126 if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
1127 $contact_actions['updateprofile'] = [
1128 'label' => DI::l10n()->t('Refetch contact data'),
1129 'url' => 'contact/' . $contact['id'] . '/updateprofile?t=' . $formSecurityToken,
1132 'id' => 'updateprofile',
1136 $contact_actions['block'] = [
1137 'label' => (intval($contact['blocked']) ? DI::l10n()->t('Unblock') : DI::l10n()->t('Block')),
1138 'url' => 'contact/' . $contact['id'] . '/block?t=' . $formSecurityToken,
1139 'title' => DI::l10n()->t('Toggle Blocked status'),
1140 'sel' => (intval($contact['blocked']) ? 'active' : ''),
1141 'id' => 'toggle-block',
1144 $contact_actions['ignore'] = [
1145 'label' => (intval($contact['readonly']) ? DI::l10n()->t('Unignore') : DI::l10n()->t('Ignore')),
1146 'url' => 'contact/' . $contact['id'] . '/ignore?t=' . $formSecurityToken,
1147 'title' => DI::l10n()->t('Toggle Ignored status'),
1148 'sel' => (intval($contact['readonly']) ? 'active' : ''),
1149 'id' => 'toggle-ignore',
1152 if ($contact['uid'] != 0) {
1153 $contact_actions['delete'] = [
1154 'label' => DI::l10n()->t('Delete'),
1155 'url' => 'contact/' . $contact['id'] . '/drop?t=' . $formSecurityToken,
1156 'title' => DI::l10n()->t('Delete contact'),
1162 return $contact_actions;