3 * @copyright Copyright (C) 2010-2022, 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\Model;
24 use Friendica\Contact\Avatar;
25 use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
26 use Friendica\Content\Pager;
27 use Friendica\Content\Text\HTML;
28 use Friendica\Core\Hook;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Protocol;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session;
33 use Friendica\Core\System;
34 use Friendica\Core\Worker;
35 use Friendica\Database\Database;
36 use Friendica\Database\DBA;
38 use Friendica\Module\NoScrape;
39 use Friendica\Network\HTTPException;
40 use Friendica\Network\Probe;
41 use Friendica\Protocol\Activity;
42 use Friendica\Protocol\ActivityPub;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Images;
45 use Friendica\Util\Network;
46 use Friendica\Util\Proxy;
47 use Friendica\Util\Strings;
50 * functions for interacting with a contact
54 const DEFAULT_AVATAR_PHOTO = '/images/person-300.jpg';
55 const DEFAULT_AVATAR_THUMB = '/images/person-80.jpg';
56 const DEFAULT_AVATAR_MICRO = '/images/person-48.jpg';
62 const LOCK_INSERT = 'contact-insert';
67 * TYPE_UNKNOWN - unknown type
69 * TYPE_PERSON - the account belongs to a person
70 * Associated page types: PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE
72 * TYPE_ORGANISATION - the account belongs to an organisation
73 * Associated page type: PAGE_SOAPBOX
75 * TYPE_NEWS - the account is a news reflector
76 * Associated page type: PAGE_SOAPBOX
78 * TYPE_COMMUNITY - the account is community forum
79 * Associated page types: PAGE_COMMUNITY, PAGE_PRVGROUP
81 * TYPE_RELAY - the account is a relay
82 * This will only be assigned to contacts, not to user accounts
85 const TYPE_UNKNOWN = -1;
86 const TYPE_PERSON = User::ACCOUNT_TYPE_PERSON;
87 const TYPE_ORGANISATION = User::ACCOUNT_TYPE_ORGANISATION;
88 const TYPE_NEWS = User::ACCOUNT_TYPE_NEWS;
89 const TYPE_COMMUNITY = User::ACCOUNT_TYPE_COMMUNITY;
90 const TYPE_RELAY = User::ACCOUNT_TYPE_RELAY;
110 const MIRROR_DEACTIVATED = 0;
111 const MIRROR_FORWARDED = 1;
112 const MIRROR_OWN_POST = 2;
113 const MIRROR_NATIVE_RESHARE = 3;
116 * @param array $fields Array of selected fields, empty for all
117 * @param array $condition Array of fields for condition
118 * @param array $params Array of several parameters
122 public static function selectToArray(array $fields = [], array $condition = [], array $params = []): array
124 return DBA::selectToArray('contact', $fields, $condition, $params);
128 * @param array $fields Array of selected fields, empty for all
129 * @param array $condition Array of fields for condition
130 * @param array $params Array of several parameters
134 public static function selectFirst(array $fields = [], array $condition = [], array $params = [])
136 $contact = DBA::selectFirst('contact', $fields, $condition, $params);
142 * Insert a row into the contact table
143 * Important: You can't use DBA::lastInsertId() after this call since it will be set to 0.
145 * @param array $fields field array
146 * @param int $duplicate_mode Do an update on a duplicate entry
148 * @return int id of the created contact
151 public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT): int
153 if (!empty($fields['baseurl']) && empty($fields['gsid'])) {
154 $fields['gsid'] = GServer::getID($fields['baseurl'], true);
157 $fields['uri-id'] = ItemURI::getIdByURI($fields['url']);
159 if (empty($fields['created'])) {
160 $fields['created'] = DateTimeFormat::utcNow();
163 $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $fields);
164 DBA::insert('contact', $fields, $duplicate_mode);
165 $contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]);
166 if (!DBA::isResult($contact)) {
168 Logger::warning('Created contact could not be found', ['fields' => $fields]);
172 $fields = DI::dbaDefinition()->truncateFieldsForTable('account-user', $contact);
173 DBA::insert('account-user', $fields, Database::INSERT_IGNORE);
174 $account_user = DBA::selectFirst('account-user', ['id'], ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id']]);
175 if (empty($account_user['id'])) {
176 Logger::warning('Account-user entry not found', ['cid' => $contact['id'], 'uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'url' => $contact['url']]);
177 } elseif ($account_user['id'] != $contact['id']) {
178 $duplicate = DBA::selectFirst('contact', [], ['id' => $account_user['id'], 'deleted' => false]);
179 if (!empty($duplicate['id'])) {
180 $ret = Contact::deleteById($contact['id']);
181 Logger::notice('Deleted duplicated contact', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $duplicate['id'], 'uid' => $duplicate['uid'], 'uri-id' => $duplicate['uri-id'], 'url' => $duplicate['url']]);
182 $contact = $duplicate;
184 $ret = DBA::update('account-user', ['id' => $contact['id']], ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id']]);
185 Logger::notice('Updated account-user', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $contact['id'], 'uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'url' => $contact['url']]);
189 Contact\User::insertForContactArray($contact);
191 return $contact['id'];
195 * Delete contact by id
200 public static function deleteById(int $id): bool
202 Logger::debug('Delete contact', ['id' => $id]);
203 DBA::delete('account-user', ['id' => $id]);
204 return DBA::delete('contact', ['id' => $id]);
208 * Updates rows in the contact table
210 * @param array $fields contains the fields that are updated
211 * @param array $condition condition array with the key values
212 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields)
214 * @return boolean was the update successfull?
216 * @todo Let's get rid of boolean type of $old_fields
218 public static function update(array $fields, array $condition, $old_fields = [])
220 $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $fields);
221 $ret = DBA::update('contact', $fields, $condition, $old_fields);
223 // Apply changes to the "user-contact" table on dedicated fields
224 Contact\User::updateByContactUpdate($fields, $condition);
230 * @param integer $id Contact ID
231 * @param array $fields Array of selected fields, empty for all
232 * @return array|boolean Contact record if it exists, false otherwise
235 public static function getById(int $id, array $fields = [])
237 return DBA::selectFirst('contact', $fields, ['id' => $id]);
241 * Fetch the first contact with the provided uri-id.
243 * @param integer $uri_id uri-id of the contact
244 * @param array $fields Array of selected fields, empty for all
245 * @return array|boolean Contact record if it exists, false otherwise
248 public static function getByUriId(int $uri_id, array $fields = [])
250 return DBA::selectFirst('contact', $fields, ['uri-id' => $uri_id], ['order' => ['uid']]);
254 * Fetches a contact by a given url
256 * @param string $url profile url
257 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
258 * @param array $fields Field list
259 * @param integer $uid User ID of the contact
260 * @return array contact array
262 public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0): array
264 if ($update || is_null($update)) {
265 $cid = self::getIdForURL($url, $uid, $update);
270 $contact = self::getById($cid, $fields);
271 if (empty($contact)) {
277 // Add internal fields
279 if (!empty($fields)) {
280 foreach (['id', 'avatar', 'created', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
281 if (!in_array($internal, $fields)) {
282 $fields[] = $internal;
283 $removal[] = $internal;
288 // We first try the nurl (http://server.tld/nick), most common case
289 $options = ['order' => ['id']];
290 $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options);
292 // Then the addr (nick@server.tld)
293 if (!DBA::isResult($contact)) {
294 $contact = DBA::selectFirst('contact', $fields, ['addr' => str_replace('acct:', '', $url), 'uid' => $uid, 'deleted' => false], $options);
297 // Then the alias (which could be anything)
298 if (!DBA::isResult($contact)) {
299 // The link could be provided as http although we stored it as https
300 $ssl_url = str_replace('http://', 'https://', $url);
301 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid];
302 $contact = DBA::selectFirst('contact', $fields, $condition, $options);
305 if (!DBA::isResult($contact)) {
309 // Update the contact in the background if needed
310 $updated = max($contact['success_update'], $contact['created'], $contact['updated'], $contact['last-update'], $contact['failure_update']);
311 if (($updated < DateTimeFormat::utc('now -7 days')) && in_array($contact['network'], Protocol::FEDERATED) && !self::isLocalById($contact['id'])) {
312 Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']);
315 // Remove the internal fields
316 foreach ($removal as $internal) {
317 unset($contact[$internal]);
324 * Fetches a contact for a given user by a given url.
325 * In difference to "getByURL" the function will fetch a public contact when no user contact had been found.
327 * @param string $url profile url
328 * @param integer $uid User ID of the contact
329 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
330 * @param array $fields Field list
331 * @return array contact array
333 public static function getByURLForUser(string $url, int $uid = 0, $update = false, array $fields = []): array
336 $contact = self::getByURL($url, $update, $fields, $uid);
337 if (!empty($contact)) {
338 if (!empty($contact['id'])) {
339 $contact['cid'] = $contact['id'];
346 $contact = self::getByURL($url, $update, $fields);
347 if (!empty($contact['id'])) {
349 $contact['zid'] = $contact['id'];
355 * Tests if the given contact is a follower
357 * @param int $cid Either public contact id or user's contact id
358 * @param int $uid User ID
359 * @param bool $strict If "true" then contact mustn't be set to pending or readonly
361 * @return boolean is the contact id a follower?
362 * @throws HTTPException\InternalServerErrorException
363 * @throws \ImagickException
365 public static function isFollower(int $cid, int $uid, bool $strict = false): bool
367 if (Contact\User::isBlocked($cid, $uid)) {
371 $cdata = self::getPublicAndUserContactID($cid, $uid);
372 if (empty($cdata['user'])) {
376 $condition = ['id' => $cdata['user'], 'rel' => [self::FOLLOWER, self::FRIEND]];
378 $condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
380 return DBA::exists('contact', $condition);
384 * Tests if the given contact url is a follower
386 * @param string $url Contact URL
387 * @param int $uid User ID
388 * @param bool $strict If "true" then contact mustn't be set to pending or readonly
390 * @return boolean is the contact id a follower?
391 * @throws HTTPException\InternalServerErrorException
392 * @throws \ImagickException
394 public static function isFollowerByURL(string $url, int $uid, bool $strict = false): bool
396 $cid = self::getIdForURL($url, $uid);
402 return self::isFollower($cid, $uid, $strict);
406 * Tests if the given user shares with the given contact
408 * @param int $cid Either public contact id or user's contact id
409 * @param int $uid User ID
410 * @param bool $strict If "true" then contact mustn't be set to pending or readonly
412 * @return boolean is the contact sharing with given user?
413 * @throws HTTPException\InternalServerErrorException
414 * @throws \ImagickException
416 public static function isSharing(int $cid, int $uid, bool $strict = false): bool
418 if (Contact\User::isBlocked($cid, $uid)) {
422 $cdata = self::getPublicAndUserContactID($cid, $uid);
423 if (empty($cdata['user'])) {
427 $condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]];
429 $condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
431 return DBA::exists('contact', $condition);
435 * Tests if the given user follow the given contact url
437 * @param string $url Contact URL
438 * @param int $uid User ID
439 * @param bool $strict If "true" then contact mustn't be set to pending or readonly
441 * @return boolean is the contact url being followed?
442 * @throws HTTPException\InternalServerErrorException
443 * @throws \ImagickException
445 public static function isSharingByURL(string $url, int $uid, bool $strict = false): bool
447 $cid = self::getIdForURL($url, $uid);
453 return self::isSharing($cid, $uid, $strict);
457 * Get the basepath for a given contact link
459 * @param string $url The contact link
460 * @param boolean $dont_update Don't update the contact
462 * @return string basepath
463 * @throws HTTPException\InternalServerErrorException
464 * @throws \ImagickException
466 public static function getBasepath(string $url, bool $dont_update = false): string
468 $contact = DBA::selectFirst('contact', ['id', 'baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]);
469 if (!DBA::isResult($contact)) {
473 if (!empty($contact['baseurl'])) {
474 return $contact['baseurl'];
475 } elseif ($dont_update) {
479 // Update the existing contact
480 self::updateFromProbe($contact['id']);
482 // And fetch the result
483 $contact = DBA::selectFirst('contact', ['baseurl'], ['id' => $contact['id']]);
484 if (empty($contact['baseurl'])) {
485 Logger::info('No baseurl for contact', ['url' => $url]);
489 Logger::info('Found baseurl for contact', ['url' => $url, 'baseurl' => $contact['baseurl']]);
490 return $contact['baseurl'];
494 * Check if the given contact url is on the same server
496 * @param string $url The contact link
498 * @return boolean Is it the same server?
500 public static function isLocal(string $url): bool
502 if (!parse_url($url, PHP_URL_SCHEME)) {
503 $addr_parts = explode('@', $url);
504 return (count($addr_parts) == 2) && ($addr_parts[1] == DI::baseUrl()->getHostname());
507 return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl());
511 * Check if the given contact ID is on the same server
513 * @param string $url The contact link
514 * @return boolean Is it the same server?
516 public static function isLocalById(int $cid): bool
518 $contact = DBA::selectFirst('contact', ['url', 'baseurl'], ['id' => $cid]);
519 if (!DBA::isResult($contact)) {
523 if (empty($contact['baseurl'])) {
524 $baseurl = self::getBasepath($contact['url'], true);
526 $baseurl = $contact['baseurl'];
529 return Strings::compareLink($baseurl, DI::baseUrl());
533 * Returns the public contact id of the given user id
535 * @param integer $uid User ID
537 * @return integer|boolean Public contact id for given user id
540 public static function getPublicIdByUserId(int $uid)
542 $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
543 if (!DBA::isResult($self)) {
546 return self::getIdForURL($self['url']);
550 * Returns the contact id for the user and the public contact id for a given contact id
552 * @param int $cid Either public contact id or user's contact id
553 * @param int $uid User ID
555 * @return array with public and user's contact id
556 * @throws HTTPException\InternalServerErrorException
557 * @throws \ImagickException
559 public static function getPublicAndUserContactID(int $cid, int $uid): array
561 // We have to use the legacy function as long as the post update hasn't finished
562 if (DI::config()->get('system', 'post_update_version') < 1427) {
563 return self::legacyGetPublicAndUserContactID($cid, $uid);
566 if (empty($uid) || empty($cid)) {
570 $contact = DBA::selectFirst('account-user-view', ['id', 'uid', 'pid'], ['id' => $cid]);
571 if (!DBA::isResult($contact) || !in_array($contact['uid'], [0, $uid])) {
575 $pcid = $contact['pid'];
576 if ($contact['uid'] == $uid) {
577 $ucid = $contact['id'];
579 $contact = DBA::selectFirst('account-user-view', ['id', 'uid'], ['pid' => $cid, 'uid' => $uid]);
580 if (DBA::isResult($contact)) {
581 $ucid = $contact['id'];
587 return ['public' => $pcid, 'user' => $ucid];
591 * Helper function for "getPublicAndUserContactID"
593 * @param int $cid Either public contact id or user's contact id
594 * @param int $uid User ID
595 * @return array with public and user's contact id
596 * @throws HTTPException\InternalServerErrorException
597 * @throws \ImagickException
599 private static function legacyGetPublicAndUserContactID(int $cid, int $uid): array
601 if (empty($uid) || empty($cid)) {
605 $contact = DBA::selectFirst('contact', ['id', 'uid', 'url'], ['id' => $cid]);
606 if (!DBA::isResult($contact)) {
610 // We quit when the user id don't match the user id of the provided contact
611 if (($contact['uid'] != $uid) && ($contact['uid'] != 0)) {
615 if ($contact['uid'] != 0) {
616 $pcid = self::getIdForURL($contact['url'], 0, false, ['url' => $contact['url']]);
620 $ucid = $contact['id'];
622 $pcid = $contact['id'];
623 $ucid = self::getIdForURL($contact['url'], $uid);
626 return ['public' => $pcid, 'user' => $ucid];
630 * Returns contact details for a given contact id in combination with a user id
632 * @param int $cid A contact ID
633 * @param int $uid The User ID
634 * @param array $fields The selected fields for the contact
635 * @return array The contact details
639 public static function getContactForUser(int $cid, int $uid, array $fields = []): array
641 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => $uid]);
643 if (!DBA::isResult($contact)) {
651 * Creates the self-contact for the provided user id
654 * @return bool Operation success
655 * @throws HTTPException\InternalServerErrorException
657 public static function createSelfFromUserId(int $uid): bool
659 $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'pubkey', 'prvkey'],
660 ['uid' => $uid, 'account_expired' => false]);
661 if (!DBA::isResult($user)) {
666 'uid' => $user['uid'],
667 'created' => DateTimeFormat::utcNow(),
669 'name' => $user['username'],
670 'nick' => $user['nickname'],
671 'pubkey' => $user['pubkey'],
672 'prvkey' => $user['prvkey'],
673 'photo' => User::getAvatarUrl($user),
674 'thumb' => User::getAvatarUrl($user, Proxy::SIZE_THUMB),
675 'micro' => User::getAvatarUrl($user, Proxy::SIZE_MICRO),
678 'url' => DI::baseUrl() . '/profile/' . $user['nickname'],
679 'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname']),
680 'addr' => $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3),
681 'request' => DI::baseUrl() . '/dfrn_request/' . $user['nickname'],
682 'notify' => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'],
683 'poll' => DI::baseUrl() . '/dfrn_poll/' . $user['nickname'],
684 'confirm' => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
685 'poco' => DI::baseUrl() . '/poco/' . $user['nickname'],
686 'name-date' => DateTimeFormat::utcNow(),
687 'uri-date' => DateTimeFormat::utcNow(),
688 'avatar-date' => DateTimeFormat::utcNow(),
694 // Only create the entry if it doesn't exist yet
695 if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
696 $return = (bool)self::insert($contact);
699 // Create the public contact
700 if (!DBA::exists('contact', ['nurl' => $contact['nurl'], 'uid' => 0])) {
701 $contact['self'] = false;
703 $contact['prvkey'] = null;
705 self::insert($contact, Database::INSERT_IGNORE);
712 * Updates the self-contact for the provided user id
715 * @param bool $update_avatar Force the avatar update
716 * @return bool "true" if updated
717 * @throws HTTPException\InternalServerErrorException
719 public static function updateSelfFromUserID(int $uid, bool $update_avatar = false): bool
721 $fields = ['id', 'uri-id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey', 'manually-approve',
722 'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
723 'photo', 'thumb', 'micro', 'header', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network'];
724 $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
725 if (!DBA::isResult($self)) {
729 $fields = ['uid', 'nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
730 $user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'account_expired' => false]);
731 if (!DBA::isResult($user)) {
735 $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
736 'country-name', 'pub_keywords', 'xmpp', 'matrix', 'net-publish'];
737 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
738 if (!DBA::isResult($profile)) {
742 $file_suffix = 'jpg';
743 $url = DI::baseUrl() . '/profile/' . $user['nickname'];
746 'name' => $profile['name'],
747 'nick' => $user['nickname'],
748 'avatar-date' => $self['avatar-date'],
749 'location' => Profile::formatLocation($profile),
750 'about' => $profile['about'],
751 'keywords' => $profile['pub_keywords'],
752 'contact-type' => $user['account-type'],
753 'prvkey' => $user['prvkey'],
754 'pubkey' => $user['pubkey'],
755 'xmpp' => $profile['xmpp'],
756 'matrix' => $profile['matrix'],
757 'network' => Protocol::DFRN,
759 // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
760 'nurl' => Strings::normaliseLink($url),
761 'uri-id' => ItemURI::getIdByURI($url),
762 'addr' => $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3),
763 'request' => DI::baseUrl() . '/dfrn_request/' . $user['nickname'],
764 'notify' => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'],
765 'poll' => DI::baseUrl() . '/dfrn_poll/'. $user['nickname'],
766 'confirm' => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
767 'poco' => DI::baseUrl() . '/poco/' . $user['nickname'],
771 $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
772 if (DBA::isResult($avatar)) {
773 if ($update_avatar) {
774 $fields['avatar-date'] = DateTimeFormat::utcNow();
777 // Creating the path to the avatar, beginning with the file suffix
778 $types = Images::supportedTypes();
779 if (isset($types[$avatar['type']])) {
780 $file_suffix = $types[$avatar['type']];
783 // We are adding a timestamp value so that other systems won't use cached content
784 $timestamp = strtotime($fields['avatar-date']);
786 $prefix = DI::baseUrl() . '/photo/' .$avatar['resource-id'] . '-';
787 $suffix = '.' . $file_suffix . '?ts=' . $timestamp;
789 $fields['photo'] = $prefix . '4' . $suffix;
790 $fields['thumb'] = $prefix . '5' . $suffix;
791 $fields['micro'] = $prefix . '6' . $suffix;
793 // We hadn't found a photo entry, so we use the default avatar
794 $fields['photo'] = self::getDefaultAvatar($fields, Proxy::SIZE_SMALL);
795 $fields['thumb'] = self::getDefaultAvatar($fields, Proxy::SIZE_THUMB);
796 $fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO);
799 $fields['avatar'] = User::getAvatarUrl($user);
800 $fields['header'] = User::getBannerUrl($user);
801 $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
802 $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
803 $fields['unsearchable'] = !$profile['net-publish'];
804 $fields['manually-approve'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
808 foreach ($fields as $field => $content) {
809 if ($self[$field] != $content) {
815 if ($fields['name'] != $self['name']) {
816 $fields['name-date'] = DateTimeFormat::utcNow();
818 $fields['updated'] = DateTimeFormat::utcNow();
819 self::update($fields, ['id' => $self['id']]);
821 // Update the other contacts as well
822 unset($fields['prvkey']);
823 $fields['self'] = false;
824 self::update($fields, ['uri-id' => $self['uri-id'], 'self' => false]);
826 // Update the profile
828 'photo' => User::getAvatarUrl($user),
829 'thumb' => User::getAvatarUrl($user, Proxy::SIZE_THUMB)
832 DBA::update('profile', $fields, ['uid' => $uid]);
839 * Marks a contact for removal
841 * @param int $id contact id
843 * @throws HTTPException\InternalServerErrorException
845 public static function remove(int $id)
847 // We want just to make sure that we don't delete our "self" contact
848 $contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro', 'uid'], ['id' => $id, 'self' => false]);
849 if (!DBA::isResult($contact)) {
853 DBA::delete('account-user', ['id' => $id]);
855 self::clearFollowerFollowingEndpointCache($contact['uid']);
857 // Archive the contact
858 self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
860 if (!DBA::exists('contact', ['uri-id' => $contact['uri-id'], 'deleted' => false])) {
861 Avatar::deleteCache($contact);
864 // Delete it in the background
865 Worker::add(PRIORITY_MEDIUM, 'Contact\Remove', $id);
869 * Unfollow the remote contact
871 * @param array $contact Target user-specific contact (uid != 0) array
873 * @throws HTTPException\InternalServerErrorException
874 * @throws \ImagickException
876 public static function unfollow(array $contact): void
878 if (empty($contact['network'])) {
879 throw new \InvalidArgumentException('Empty network in contact array');
882 if (empty($contact['uid'])) {
883 throw new \InvalidArgumentException('Unexpected public contact record');
886 if (in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
887 $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
888 if (!empty($cdata['public'])) {
889 Worker::add(PRIORITY_HIGH, 'Contact\Unfollow', $cdata['public'], $contact['uid']);
893 self::removeSharer($contact);
897 * Revoke follow privileges of the remote user contact
899 * The local relationship is updated immediately, the eventual remote server is messaged in the background.
901 * @param array $contact User-specific contact array (uid != 0) to revoke the follow from
903 * @throws HTTPException\InternalServerErrorException
904 * @throws \ImagickException
906 public static function revokeFollow(array $contact): void
908 if (empty($contact['network'])) {
909 throw new \InvalidArgumentException('Empty network in contact array');
912 if (empty($contact['uid'])) {
913 throw new \InvalidArgumentException('Unexpected public contact record');
916 if (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND])) {
917 $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
918 if (!empty($cdata['public'])) {
919 Worker::add(PRIORITY_HIGH, 'Contact\RevokeFollow', $cdata['public'], $contact['uid']);
923 self::removeFollower($contact);
927 * Completely severs a relationship with a contact
929 * @param array $contact User-specific contact (uid != 0) array
931 * @throws HTTPException\InternalServerErrorException
932 * @throws \ImagickException
934 public static function terminateFriendship(array $contact)
936 if (empty($contact['network'])) {
937 throw new \InvalidArgumentException('Empty network in contact array');
940 if (empty($contact['uid'])) {
941 throw new \InvalidArgumentException('Unexpected public contact record');
944 $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
946 if (in_array($contact['rel'], [self::SHARING, self::FRIEND]) && !empty($cdata['public'])) {
947 Worker::add(PRIORITY_HIGH, 'Contact\Unfollow', $cdata['public'], $contact['uid']);
950 if (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND]) && !empty($cdata['public'])) {
951 Worker::add(PRIORITY_HIGH, 'Contact\RevokeFollow', $cdata['public'], $contact['uid']);
954 self::remove($contact['id']);
957 private static function clearFollowerFollowingEndpointCache(int $uid)
963 DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'followers:' . $uid);
964 DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'following:' . $uid);
965 DI::cache()->delete(NoScrape::CACHEKEY . $uid);
969 * Marks a contact for archival after a communication issue delay
971 * Contact has refused to recognise us as a friend. We will start a countdown.
972 * If they still don't recognise us in 32 days, the relationship is over,
973 * and we won't waste any more time trying to communicate with them.
974 * This provides for the possibility that their database is temporarily messed
975 * up or some other transient event and that there's a possibility we could recover from it.
977 * @param array $contact contact to mark for archival
979 * @throws HTTPException\InternalServerErrorException
981 public static function markForArchival(array $contact)
983 if (!isset($contact['url']) && !empty($contact['id'])) {
984 $fields = ['id', 'url', 'archive', 'self', 'term-date'];
985 $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
986 if (!DBA::isResult($contact)) {
989 } elseif (!isset($contact['url'])) {
990 Logger::info('Empty contact', ['contact' => $contact, 'callstack' => System::callstack(20)]);
993 Logger::info('Contact is marked for archival', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
995 // Contact already archived or "self" contact? => nothing to do
996 if ($contact['archive'] || $contact['self']) {
1000 if ($contact['term-date'] <= DBA::NULL_DATETIME) {
1001 self::update(['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
1002 self::update(['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
1005 * We really should send a notification to the owner after 2-3 weeks
1006 * so they won't be surprised when the contact vanishes and can take
1007 * remedial action if this was a serious mistake or glitch
1010 /// @todo Check for contact vitality via probing
1011 $archival_days = DI::config()->get('system', 'archival_days', 32);
1013 $expiry = $contact['term-date'] . ' + ' . $archival_days . ' days ';
1014 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($expiry)) {
1015 /* Relationship is really truly dead. archive them rather than
1016 * delete, though if the owner tries to unarchive them we'll start
1017 * the whole process over again.
1019 self::update(['archive' => true], ['id' => $contact['id']]);
1020 self::update(['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
1026 * Cancels the archival countdown
1028 * @see Contact::markForArchival()
1030 * @param array $contact contact to be unmarked for archival
1032 * @throws \Exception
1034 public static function unmarkForArchival(array $contact)
1036 // Always unarchive the relay contact entry
1037 if (!empty($contact['batch']) && !empty($contact['term-date']) && ($contact['term-date'] > DBA::NULL_DATETIME)) {
1038 $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
1039 $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
1040 if (!DBA::exists('contact', array_merge($condition, $fields))) {
1041 self::update($fields, $condition);
1045 $condition = ['`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], DBA::NULL_DATETIME];
1046 $exists = DBA::exists('contact', $condition);
1048 // We don't need to update, we never marked this contact for archival
1053 Logger::info('Contact is marked as vital again', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
1055 if (!isset($contact['url']) && !empty($contact['id'])) {
1056 $fields = ['id', 'url', 'batch'];
1057 $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
1058 if (!DBA::isResult($contact)) {
1063 // It's a miracle. Our dead contact has inexplicably come back to life.
1064 $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
1065 self::update($fields, ['id' => $contact['id']]);
1066 self::update($fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
1070 * Returns the data array for the photo menu of a given contact
1072 * @param array $contact contact
1073 * @param int $uid optional, default 0
1075 * @throws HTTPException\InternalServerErrorException
1076 * @throws \ImagickException
1078 public static function photoMenu(array $contact, int $uid = 0): array
1085 $uid = local_user();
1088 if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
1090 $profile_link = self::magicLinkByContact($contact);
1091 $menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
1096 // Look for our own contact if the uid doesn't match and isn't public
1097 $contact_own = DBA::selectFirst('contact', [], ['nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid]);
1098 if (DBA::isResult($contact_own)) {
1099 return self::photoMenu($contact_own, $uid);
1104 if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
1106 $profile_link = DI::baseUrl() . '/redir/' . $contact['id'];
1108 $profile_link = $contact['url'];
1111 if ($profile_link === 'mailbox') {
1116 $status_link = $profile_link . '/status';
1117 $photos_link = str_replace('/profile/', '/photos/', $profile_link);
1118 $profile_link = $profile_link . '/profile';
1121 if (self::canReceivePrivateMessages($contact) && empty($contact['pending'])) {
1122 $pm_url = DI::baseUrl() . '/message/new/' . $contact['id'];
1125 $contact_url = DI::baseUrl() . '/contact/' . $contact['id'];
1127 $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations';
1130 $unfollow_link = '';
1131 if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
1132 if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
1133 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1';
1134 } elseif(!$contact['pending']) {
1135 $follow_link = 'follow?url=' . urlencode($contact['url']) . '&auto=1';
1141 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
1143 if (empty($contact['uid'])) {
1145 'profile' => [DI::l10n()->t('View Profile') , $profile_link , true],
1146 'network' => [DI::l10n()->t('Network Posts') , $posts_link , false],
1147 'edit' => [DI::l10n()->t('View Contact') , $contact_url , false],
1148 'follow' => [DI::l10n()->t('Connect/Follow'), $follow_link , true],
1149 'unfollow'=> [DI::l10n()->t('UnFollow') , $unfollow_link, true],
1153 'status' => [DI::l10n()->t('View Status') , $status_link , true],
1154 'profile' => [DI::l10n()->t('View Profile') , $profile_link , true],
1155 'photos' => [DI::l10n()->t('View Photos') , $photos_link , true],
1156 'network' => [DI::l10n()->t('Network Posts') , $posts_link , false],
1157 'edit' => [DI::l10n()->t('View Contact') , $contact_url , false],
1158 'pm' => [DI::l10n()->t('Send PM') , $pm_url , false],
1159 'follow' => [DI::l10n()->t('Connect/Follow'), $follow_link , true],
1160 'unfollow'=> [DI::l10n()->t('UnFollow') , $unfollow_link , true],
1163 if (!empty($contact['pending'])) {
1165 $intro = DI::intro()->selectForContact($contact['id']);
1166 $menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro->id, true];
1167 } catch (IntroductionNotFoundException $exception) {
1168 DI::logger()->error('Pending contact doesn\'t have an introduction.', ['exception' => $exception]);
1173 $args = ['contact' => $contact, 'menu' => &$menu];
1175 Hook::callAll('contact_photo_menu', $args);
1177 $menucondensed = [];
1179 foreach ($menu as $menuname => $menuitem) {
1180 if ($menuitem[1] != '') {
1181 $menucondensed[$menuname] = $menuitem;
1185 return $menucondensed;
1189 * Fetch the contact id for a given URL and user
1191 * First lookup in the contact table to find a record matching either `url`, `nurl`,
1192 * `addr` or `alias`.
1194 * If there's no record and we aren't looking for a public contact, we quit.
1195 * If there's one, we check that it isn't time to update the picture else we
1196 * directly return the found contact id.
1198 * Second, we probe the provided $url whether it's http://server.tld/profile or
1199 * nick@server.tld. We quit if we can't get any info back.
1201 * Third, we create the contact record if it doesn't exist
1203 * Fourth, we update the existing record with the new data (avatar, alias, nick)
1204 * if there's any updates
1206 * @param string $url Contact URL
1207 * @param integer $uid The user id for the contact (0 = public contact)
1208 * @param boolean $update true = always update, false = never update, null = update when not found
1209 * @param array $default Default value for creating the contact when everything else fails
1211 * @return integer Contact ID
1212 * @throws HTTPException\InternalServerErrorException
1213 * @throws \ImagickException
1215 public static function getIdForURL(string $url = null, int $uid = 0, $update = null, array $default = []): int
1220 Logger::notice('Empty url, quitting', ['url' => $url, 'user' => $uid, 'default' => $default]);
1224 $contact = self::getByURL($url, false, ['id', 'network', 'uri-id'], $uid);
1226 if (!empty($contact)) {
1227 $contact_id = $contact['id'];
1229 if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) {
1230 Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
1233 } elseif ($uid != 0) {
1234 Logger::debug('Contact does not exist for the user', ['url' => $url, 'uid' => $uid, 'update' => $update]);
1236 } elseif (empty($default) && !is_null($update) && !$update) {
1237 Logger::info('Contact not found, update not desired', ['url' => $url, 'uid' => $uid, 'update' => $update]);
1243 if (empty($default['network']) || $update) {
1244 $data = Probe::uri($url, '', $uid);
1246 // Take the default values when probing failed
1247 if (!empty($default) && !in_array($data['network'], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
1248 $data = array_merge($data, $default);
1250 } elseif (!empty($default['network'])) {
1254 if (($uid == 0) && (empty($data['network']) || ($data['network'] == Protocol::PHANTOM))) {
1255 // Fetch data for the public contact via the first found personal contact
1256 /// @todo Check if this case can happen at all (possibly with mail accounts?)
1257 $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'header', 'contact-type',
1258 'keywords', 'location', 'about', 'unsearchable', 'batch', 'notify', 'poll',
1259 'request', 'confirm', 'poco', 'subscribe', 'network', 'baseurl', 'gsid'];
1261 $personal_contact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `uid` != 0", $url]);
1262 if (!DBA::isResult($personal_contact)) {
1263 $personal_contact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `uid` != 0", Strings::normaliseLink($url)]);
1266 if (DBA::isResult($personal_contact)) {
1267 Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact, 'callstack' => System::callstack(20)]);
1268 $data = $personal_contact;
1269 $data['photo'] = $personal_contact['avatar'];
1270 $data['account-type'] = $personal_contact['contact-type'];
1271 $data['hide'] = $personal_contact['unsearchable'];
1272 unset($data['avatar']);
1273 unset($data['contact-type']);
1274 unset($data['unsearchable']);
1278 if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
1279 Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update, 'callstack' => System::callstack(20)]);
1283 if (!$contact_id && !empty($data['account-type']) && $data['account-type'] == User::ACCOUNT_TYPE_DELETED) {
1284 Logger::info('Contact is a tombstone. It will not be inserted', ['url' => $url, 'uid' => $uid]);
1289 $urls = [Strings::normaliseLink($url), Strings::normaliseLink($data['url'])];
1290 if (!empty($data['alias'])) {
1291 $urls[] = Strings::normaliseLink($data['alias']);
1293 $contact = self::selectFirst(['id'], ['nurl' => $urls, 'uid' => $uid]);
1294 if (!empty($contact['id'])) {
1295 $contact_id = $contact['id'];
1296 Logger::info('Fetched id by url', ['cid' => $contact_id, 'uid' => $uid, 'url' => $url, 'data' => $data]);
1301 // We only insert the basic data. The rest will be done in "updateFromProbeArray"
1304 'url' => $data['url'],
1305 'nurl' => Strings::normaliseLink($data['url']),
1306 'network' => $data['network'],
1307 'created' => DateTimeFormat::utcNow(),
1308 'rel' => self::SHARING,
1314 $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
1316 // Before inserting we do check if the entry does exist now.
1317 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
1318 if (DBA::isResult($contact)) {
1319 $contact_id = $contact['id'];
1320 Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
1322 $contact_id = self::insert($fields);
1324 Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
1329 Logger::warning('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
1333 Logger::info('Contact will be updated', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
1336 if ($data['network'] == Protocol::DIASPORA) {
1337 FContact::updateFromProbeArray($data);
1340 self::updateFromProbeArray($contact_id, $data);
1342 // Don't return a number for a deleted account
1343 if (!empty($data['account-type']) && $data['account-type'] == User::ACCOUNT_TYPE_DELETED) {
1344 Logger::info('Contact is a tombstone', ['url' => $url, 'uid' => $uid]);
1352 * Checks if the contact is archived
1354 * @param int $cid contact id
1356 * @return boolean Is the contact archived?
1357 * @throws HTTPException\InternalServerErrorException
1359 public static function isArchived(int $cid): bool
1365 $contact = DBA::selectFirst('contact', ['archive', 'url', 'batch'], ['id' => $cid]);
1366 if (!DBA::isResult($contact)) {
1370 if ($contact['archive']) {
1374 // Check status of ActivityPub endpoints
1375 $apcontact = APContact::getByURL($contact['url'], false);
1376 if (!empty($apcontact)) {
1377 if (!empty($apcontact['inbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['inbox']])) {
1381 if (!empty($apcontact['sharedinbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['sharedinbox']])) {
1386 // Check status of Diaspora endpoints
1387 if (!empty($contact['batch'])) {
1388 $condition = ['archive' => true, 'uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
1389 return DBA::exists('contact', $condition);
1396 * Checks if the contact is blocked
1398 * @param int $cid contact id
1399 * @return boolean Is the contact blocked?
1400 * @throws HTTPException\InternalServerErrorException
1402 public static function isBlocked(int $cid): bool
1408 $blocked = DBA::selectFirst('contact', ['blocked', 'url'], ['id' => $cid]);
1409 if (!DBA::isResult($blocked)) {
1413 if (Network::isUrlBlocked($blocked['url'])) {
1417 return (bool) $blocked['blocked'];
1421 * Checks if the contact is hidden
1423 * @param int $cid contact id
1424 * @return boolean Is the contact hidden?
1425 * @throws \Exception
1427 public static function isHidden(int $cid): bool
1433 $hidden = DBA::selectFirst('contact', ['hidden'], ['id' => $cid]);
1434 if (!DBA::isResult($hidden)) {
1437 return (bool) $hidden['hidden'];
1441 * Returns posts from a given contact url
1443 * @param string $contact_url Contact URL
1444 * @param bool $thread_mode
1445 * @param int $update Update mode
1446 * @param int $parent Item parent ID for the update mode
1447 * @param bool $only_media Only display media content
1448 * @return string posts in HTML
1449 * @throws \Exception
1451 public static function getPostsFromUrl(string $contact_url, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
1453 return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
1457 * Returns posts from a given contact id
1459 * @param int $cid Contact ID
1460 * @param bool $thread_mode
1461 * @param int $update Update mode
1462 * @param int $parent Item parent ID for the update mode
1463 * @param bool $only_media Only display media content
1464 * @return string posts in HTML
1465 * @throws \Exception
1467 public static function getPostsFromId(int $cid, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
1469 $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
1470 if (!DBA::isResult($contact)) {
1474 if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
1475 $sql = "(`uid` = 0 OR (`uid` = ? AND NOT `global`))";
1480 $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
1483 $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ?)) AND " . $sql,
1484 $cid, GRAVITY_PARENT, $cid, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()];
1486 $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
1487 $cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
1490 if (!empty($parent)) {
1491 $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
1493 $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
1494 if (!empty($last_received)) {
1495 $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
1500 $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))",
1501 Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
1504 if (DI::mode()->isMobile()) {
1505 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
1506 DI::config()->get('system', 'itemspage_network_mobile'));
1508 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
1509 DI::config()->get('system', 'itemspage_network'));
1512 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
1514 $params = ['order' => ['received' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
1516 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
1517 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1518 $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
1524 $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'commented'];
1525 $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params));
1527 if ($pager->getStart() == 0) {
1528 $cdata = self::getPublicAndUserContactID($cid, local_user());
1529 if (!empty($cdata['public'])) {
1530 $pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields);
1531 $items = array_merge($items, $pinned);
1535 $o .= DI::conversation()->create($items, 'contacts', $update, false, 'pinned_commented', local_user());
1537 $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
1538 $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params));
1540 if ($pager->getStart() == 0) {
1541 $cdata = self::getPublicAndUserContactID($cid, local_user());
1542 if (!empty($cdata['public'])) {
1543 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
1544 $cdata['public'], Post\Collection::FEATURED];
1545 $pinned = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params));
1546 $items = array_merge($pinned, $items);
1550 $o .= DI::conversation()->create($items, 'contact-posts', $update);
1554 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
1555 $o .= HTML::scrollLoader();
1557 $o .= $pager->renderMinimal(count($items));
1565 * Returns the account type name
1567 * The function can be called with either the user or the contact array
1569 * @param int $type type of contact or account
1572 public static function getAccountType(int $type): string
1575 case self::TYPE_ORGANISATION:
1576 $account_type = DI::l10n()->t("Organisation");
1579 case self::TYPE_NEWS:
1580 $account_type = DI::l10n()->t('News');
1583 case self::TYPE_COMMUNITY:
1584 $account_type = DI::l10n()->t("Forum");
1592 return $account_type;
1598 * @param int $cid Contact id to block
1599 * @param string $reason Block reason
1600 * @return bool Whether it was successful
1602 public static function block(int $cid, string $reason = null): bool
1604 $return = self::update(['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
1610 * Unblocks a contact
1612 * @param int $cid Contact id to unblock
1613 * @return bool Whether it was successfull
1615 public static function unblock(int $cid): bool
1617 $return = self::update(['blocked' => false, 'block_reason' => null], ['id' => $cid]);
1623 * Ensure that cached avatar exist
1625 * @param integer $cid Contact id
1627 public static function checkAvatarCache(int $cid)
1629 $contact = DBA::selectFirst('contact', ['url', 'network', 'avatar', 'photo', 'thumb', 'micro'], ['id' => $cid, 'uid' => 0, 'self' => false]);
1630 if (!DBA::isResult($contact)) {
1634 if (Network::isLocalLink($contact['url'])) {
1638 if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || DI::config()->get('system', 'cache_contact_avatar')) {
1639 if (!empty($contact['avatar']) && (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']))) {
1640 Logger::info('Adding avatar cache', ['id' => $cid, 'contact' => $contact]);
1641 self::updateAvatar($cid, $contact['avatar'], true);
1644 } elseif (Photo::isPhotoURI($contact['photo']) || Photo::isPhotoURI($contact['thumb']) || Photo::isPhotoURI($contact['micro'])) {
1645 Logger::info('Replacing legacy avatar cache', ['id' => $cid, 'contact' => $contact]);
1646 self::updateAvatar($cid, $contact['avatar'], true);
1648 } elseif (DI::config()->get('system', 'avatar_cache') && (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']))) {
1649 Logger::info('Adding avatar cache file', ['id' => $cid, 'contact' => $contact]);
1650 self::updateAvatar($cid, $contact['avatar'], true);
1656 * Return the photo path for a given contact array in the given size
1658 * @param array $contact contact array
1659 * @param string $field Fieldname of the photo in the contact array
1660 * @param string $size Size of the avatar picture
1661 * @param string $avatar Avatar path that is displayed when no photo had been found
1662 * @param bool $no_update Don't perfom an update if no cached avatar was found
1663 * @return string photo path
1665 private static function getAvatarPath(array $contact, string $size, bool $no_update = false): string
1667 $contact = self::checkAvatarCacheByArray($contact, $no_update);
1669 if (DI::config()->get('system', 'avatar_cache')) {
1671 case Proxy::SIZE_MICRO:
1672 if (!empty($contact['micro']) && !Photo::isPhotoURI($contact['micro'])) {
1673 return $contact['micro'];
1676 case Proxy::SIZE_THUMB:
1677 if (!empty($contact['thumb']) && !Photo::isPhotoURI($contact['thumb'])) {
1678 return $contact['thumb'];
1681 case Proxy::SIZE_SMALL:
1682 if (!empty($contact['photo']) && !Photo::isPhotoURI($contact['photo'])) {
1683 return $contact['photo'];
1689 return self::getAvatarUrlForId($contact['id'], $size, $contact['updated'] ?? '');
1693 * Return the photo path for a given contact array
1695 * @param array $contact Contact array
1696 * @param bool $no_update Don't perfom an update if no cached avatar was found
1697 * @return string photo path
1699 public static function getPhoto(array $contact, bool $no_update = false): string
1701 return self::getAvatarPath($contact, Proxy::SIZE_SMALL, $no_update);
1705 * Return the photo path (thumb size) for a given contact array
1707 * @param array $contact Contact array
1708 * @param bool $no_update Don't perfom an update if no cached avatar was found
1709 * @return string photo path
1711 public static function getThumb(array $contact, bool $no_update = false): string
1713 return self::getAvatarPath($contact, Proxy::SIZE_THUMB, $no_update);
1717 * Return the photo path (micro size) for a given contact array
1719 * @param array $contact Contact array
1720 * @param bool $no_update Don't perfom an update if no cached avatar was found
1721 * @return string photo path
1723 public static function getMicro(array $contact, bool $no_update = false): string
1725 return self::getAvatarPath($contact, Proxy::SIZE_MICRO, $no_update);
1729 * Check the given contact array for avatar cache fields
1731 * @param array $contact
1732 * @param bool $no_update Don't perfom an update if no cached avatar was found
1733 * @return array contact array with avatar cache fields
1735 private static function checkAvatarCacheByArray(array $contact, bool $no_update = false): array
1738 $contact_fields = [];
1739 $fields = ['photo', 'thumb', 'micro'];
1740 foreach ($fields as $field) {
1741 if (isset($contact[$field])) {
1742 $contact_fields[] = $field;
1744 if (isset($contact[$field]) && empty($contact[$field])) {
1749 if (!$update || $no_update) {
1753 $local = !empty($contact['url']) && Network::isLocalLink($contact['url']);
1755 if (!$local && !empty($contact['id']) && !empty($contact['avatar'])) {
1756 self::updateAvatar($contact['id'], $contact['avatar'], true);
1758 $new_contact = self::getById($contact['id'], $contact_fields);
1759 if (DBA::isResult($new_contact)) {
1760 // We only update the cache fields
1761 $contact = array_merge($contact, $new_contact);
1763 } elseif ($local && !empty($contact['avatar'])) {
1767 /// add the default avatars if the fields aren't filled
1768 if (isset($contact['photo']) && empty($contact['photo'])) {
1769 $contact['photo'] = self::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
1771 if (isset($contact['thumb']) && empty($contact['thumb'])) {
1772 $contact['thumb'] = self::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
1774 if (isset($contact['micro']) && empty($contact['micro'])) {
1775 $contact['micro'] = self::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
1782 * Fetch the default header for the given contact
1784 * @param array $contact contact array
1785 * @return string avatar URL
1787 public static function getDefaultHeader(array $contact): string
1789 if (!empty($contact['header'])) {
1790 return $contact['header'];
1793 if (!empty($contact['gsid'])) {
1794 // Use default banners for certain platforms
1795 $gserver = DBA::selectFirst('gserver', ['platform'], ['id' => $contact['gsid']]);
1796 $platform = strtolower($gserver['platform'] ?? '');
1801 switch ($platform) {
1806 * @author Lostinlight <https://mastodon.xyz/@lightone>
1807 * @license CC0 https://creativecommons.org/share-your-work/public-domain/cc0/
1808 * @link https://gitlab.com/lostinlight/per_aspera_ad_astra/-/blob/master/friendica-404/friendica-promo-bubbles.jpg
1810 $header = DI::baseUrl() . '/images/friendica-banner.jpg';
1815 * @author John Liu <https://www.flickr.com/photos/8047705@N02/>
1816 * @license CC BY 2.0 https://creativecommons.org/licenses/by/2.0/
1817 * @link https://www.flickr.com/photos/8047705@N02/5572197407
1819 $header = DI::baseUrl() . '/images/diaspora-banner.jpg';
1823 * Use a random picture.
1824 * The service provides random pictures from Unsplash.
1825 * @license https://unsplash.com/license
1827 $header = 'https://picsum.photos/seed/' . hash('ripemd128', $contact['url']) . '/960/300';
1835 * Fetch the default avatar for the given contact and size
1837 * @param array $contact contact array
1838 * @param string $size Size of the avatar picture
1839 * @return string avatar URL
1841 public static function getDefaultAvatar(array $contact, string $size): string
1844 case Proxy::SIZE_MICRO:
1845 $avatar['size'] = 48;
1846 $default = self::DEFAULT_AVATAR_MICRO;
1849 case Proxy::SIZE_THUMB:
1850 $avatar['size'] = 80;
1851 $default = self::DEFAULT_AVATAR_THUMB;
1854 case Proxy::SIZE_SMALL:
1856 $avatar['size'] = 300;
1857 $default = self::DEFAULT_AVATAR_PHOTO;
1861 if (!DI::config()->get('system', 'remote_avatar_lookup')) {
1863 $type = Contact::TYPE_PERSON;
1865 if (!empty($contact['id'])) {
1866 $account = DBA::selectFirst('account-user-view', ['platform', 'contact-type'], ['id' => $contact['id']]);
1867 $platform = $account['platform'] ?? '';
1868 $type = $account['contact-type'] ?? Contact::TYPE_PERSON;
1871 if (empty($platform) && !empty($contact['uri-id'])) {
1872 $account = DBA::selectFirst('account-user-view', ['platform', 'contact-type'], ['uri-id' => $contact['uri-id']]);
1873 $platform = $account['platform'] ?? '';
1874 $type = $account['contact-type'] ?? Contact::TYPE_PERSON;
1877 switch ($platform) {
1881 * @license GNU Affero General Public License v3.0
1882 * @link https://github.com/msdos621/corgidon/blob/main/public/avatars/original/missing.png
1884 $default = '/images/default/corgidon.png';
1890 * @license GNU Affero General Public License v3.0
1891 * @link https://github.com/diaspora/diaspora/
1893 $default = '/images/default/diaspora.png';
1899 * @license GNU Affero General Public License v3.0
1900 * @link https://github.com/superseriousbusiness/gotosocial/blob/main/web/assets/default_avatars/GoToSocial_icon1.svg
1902 $default = '/images/default/gotosocial.svg';
1908 * @license GNU Affero General Public License v3.0
1909 * @link https://github.com/hometown-fork/hometown/blob/hometown-dev/public/avatars/original/missing.png
1911 $default = '/images/default/hometown.png';
1917 * @license GNU Affero General Public License v3.0
1918 * @link https://github.com/koyuspace/mastodon/blob/main/public/avatars/original/missing.png
1920 $default = '/images/default/koyuspace.png';
1928 * @license GNU Affero General Public License v3.0
1929 * @link https://github.com/mastodon/mastodon/tree/main/public/avatars/original/missing.png
1931 $default = '/images/default/mastodon.png';
1935 if ($type == Contact::TYPE_COMMUNITY) {
1938 * @license GNU Affero General Public License v3.0
1939 * @link https://github.com/Chocobozzz/PeerTube/blob/develop/client/src/assets/images/default-avatar-video-channel.png
1941 $default = '/images/default/peertube-channel.png';
1945 * @license GNU Affero General Public License v3.0
1946 * @link https://github.com/Chocobozzz/PeerTube/blob/develop/client/src/assets/images/default-avatar-account.png
1948 $default = '/images/default/peertube-account.png';
1955 * @license GNU Affero General Public License v3.0
1956 * @link https://git.pleroma.social/pleroma/pleroma/-/blob/develop/priv/static/images/avi.png
1958 $default = '/images/default/pleroma.png';
1964 * @license GNU Affero General Public License v3.0
1965 * @link https://github.com/Plume-org/Plume/blob/main/assets/images/default-avatar.png
1967 $default = '/images/default/plume.png';
1970 return DI::baseUrl() . $default;
1973 if (!empty($contact['xmpp'])) {
1974 $avatar['email'] = $contact['xmpp'];
1975 } elseif (!empty($contact['addr'])) {
1976 $avatar['email'] = $contact['addr'];
1977 } elseif (!empty($contact['url'])) {
1978 $avatar['email'] = $contact['url'];
1980 return DI::baseUrl() . $default;
1983 $avatar['url'] = '';
1984 $avatar['success'] = false;
1986 Hook::callAll('avatar_lookup', $avatar);
1988 if ($avatar['success'] && !empty($avatar['url'])) {
1989 return $avatar['url'];
1992 return DI::baseUrl() . $default;
1996 * Get avatar link for given contact id
1998 * @param integer $cid contact id
1999 * @param string $size One of the Proxy::SIZE_* constants
2000 * @param string $updated Contact update date
2001 * @return string avatar link
2003 public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string
2005 // We have to fetch the "updated" variable when it wasn't provided
2006 // The parameter can be provided to improve performance
2007 if (empty($updated)) {
2008 $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]);
2009 $updated = $account['updated'] ?? '';
2010 $guid = $account['guid'] ?? '';
2013 $guid = urlencode($guid);
2015 $url = DI::baseUrl() . '/photo/contact/';
2017 case Proxy::SIZE_MICRO:
2018 $url .= Proxy::PIXEL_MICRO . '/';
2020 case Proxy::SIZE_THUMB:
2021 $url .= Proxy::PIXEL_THUMB . '/';
2023 case Proxy::SIZE_SMALL:
2024 $url .= Proxy::PIXEL_SMALL . '/';
2026 case Proxy::SIZE_MEDIUM:
2027 $url .= Proxy::PIXEL_MEDIUM . '/';
2029 case Proxy::SIZE_LARGE:
2030 $url .= Proxy::PIXEL_LARGE . '/';
2033 return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
2037 * Get avatar link for given contact URL
2039 * @param string $url contact url
2040 * @param integer $uid user id
2041 * @param string $size One of the Proxy::SIZE_* constants
2042 * @return string avatar link
2044 public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''): string
2046 $condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
2047 Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
2048 $contact = self::selectFirst(['id', 'updated'], $condition, ['order' => ['uid' => true]]);
2049 return self::getAvatarUrlForId($contact['id'] ?? 0, $size, $contact['updated'] ?? '');
2053 * Get header link for given contact id
2055 * @param integer $cid contact id
2056 * @param string $size One of the Proxy::SIZE_* constants
2057 * @param string $updated Contact update date
2058 * @return string header link
2060 public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string
2062 // We have to fetch the "updated" variable when it wasn't provided
2063 // The parameter can be provided to improve performance
2064 if (empty($updated) || empty($guid)) {
2065 $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]);
2066 $updated = $account['updated'] ?? '';
2067 $guid = $account['guid'] ?? '';
2070 $guid = urlencode($guid);
2072 $url = DI::baseUrl() . '/photo/header/';
2074 case Proxy::SIZE_MICRO:
2075 $url .= Proxy::PIXEL_MICRO . '/';
2077 case Proxy::SIZE_THUMB:
2078 $url .= Proxy::PIXEL_THUMB . '/';
2080 case Proxy::SIZE_SMALL:
2081 $url .= Proxy::PIXEL_SMALL . '/';
2083 case Proxy::SIZE_MEDIUM:
2084 $url .= Proxy::PIXEL_MEDIUM . '/';
2086 case Proxy::SIZE_LARGE:
2087 $url .= Proxy::PIXEL_LARGE . '/';
2091 return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
2095 * Updates the avatar links in a contact only if needed
2097 * @param int $cid Contact id
2098 * @param string $avatar Link to avatar picture
2099 * @param bool $force force picture update
2100 * @param bool $create_cache Enforces the creation of cached avatar fields
2103 * @throws HTTPException\InternalServerErrorException
2104 * @throws HTTPException\NotFoundException
2105 * @throws \ImagickException
2107 public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
2109 $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'xmpp', 'addr', 'nurl', 'url', 'network', 'uri-id'],
2110 ['id' => $cid, 'self' => false]);
2111 if (!DBA::isResult($contact)) {
2115 $uid = $contact['uid'];
2117 // Only update the cached photo links of public contacts when they already are cached
2118 if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
2119 if ($contact['avatar'] != $avatar) {
2120 self::update(['avatar' => $avatar], ['id' => $cid]);
2121 Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
2126 // User contacts use are updated through the public contacts
2127 if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
2128 $pcid = self::getIdForURL($contact['url'], 0, false);
2129 if (!empty($pcid)) {
2130 Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
2131 self::updateAvatar($pcid, $avatar, $force, true);
2136 $default_avatar = empty($avatar) || strpos($avatar, self::DEFAULT_AVATAR_PHOTO);
2138 if ($default_avatar) {
2139 $avatar = self::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
2142 $cache_avatar = DI::config()->get('system', 'cache_contact_avatar');
2144 // Local contact avatars don't need to be cached
2145 if ($cache_avatar && Network::isLocalLink($contact['url'])) {
2146 $cache_avatar = !DBA::exists('contact', ['nurl' => $contact['nurl'], 'self' => true]);
2149 if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || $cache_avatar) {
2150 Avatar::deleteCache($contact);
2152 if ($default_avatar && Proxy::isLocalImage($avatar)) {
2153 $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
2155 'thumb' => self::getDefaultAvatar($contact, Proxy::SIZE_THUMB),
2156 'micro' => self::getDefaultAvatar($contact, Proxy::SIZE_MICRO)];
2157 Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
2160 // Use the data from the self account
2161 if (empty($fields)) {
2162 $local_uid = User::getIdForURL($contact['url']);
2163 if (!empty($local_uid)) {
2164 $fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
2165 Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
2169 if (empty($fields)) {
2170 $update = ($contact['avatar'] != $avatar) || $force;
2174 $contact['photo'] ?? '',
2175 $contact['thumb'] ?? '',
2176 $contact['micro'] ?? '',
2179 foreach ($data as $image_uri) {
2180 $image_rid = Photo::ridFromURI($image_uri);
2181 if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
2182 Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
2189 $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
2191 $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
2192 $update = !empty($fields);
2193 Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
2199 $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
2202 Photo::delete(['uid' => $uid, 'contact-id' => $cid, 'photo-type' => Photo::CONTACT_AVATAR]);
2203 $fields = Avatar::fetchAvatarContact($contact, $avatar, $force);
2204 $update = ($avatar . $fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['avatar'] . $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
2213 if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
2214 // Collect all user contacts of the given public contact
2215 $personal_contacts = DBA::select('contact', ['id', 'uid'],
2216 ["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
2217 while ($personal_contact = DBA::fetch($personal_contacts)) {
2218 $cids[] = $personal_contact['id'];
2219 $uids[] = $personal_contact['uid'];
2221 DBA::close($personal_contacts);
2223 if (!empty($cids)) {
2224 // Delete possibly existing cached user contact avatars
2225 Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'photo-type' => Photo::CONTACT_AVATAR]);
2231 Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
2232 self::update($fields, ['id' => $cids]);
2235 public static function deleteContactByUrl(string $url)
2237 // Update contact data for all users
2238 $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)];
2239 $contacts = DBA::select('contact', ['id', 'uid'], $condition);
2240 while ($contact = DBA::fetch($contacts)) {
2241 Logger::info('Deleting contact', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $url]);
2242 self::remove($contact['id']);
2247 * Helper function for "updateFromProbe". Updates personal and public contact
2249 * @param integer $id contact id
2250 * @param integer $uid user id
2251 * @param integer $uri_id Uri-Id
2252 * @param string $url The profile URL of the contact
2253 * @param array $fields The fields that are updated
2255 * @throws \Exception
2257 private static function updateContact(int $id, int $uid, int $uri_id, string $url, array $fields)
2259 if (!self::update($fields, ['id' => $id])) {
2260 Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
2264 self::setAccountUser($id, $uid, $uri_id, $url);
2266 // Archive or unarchive the contact.
2267 $contact = DBA::selectFirst('contact', [], ['id' => $id]);
2268 if (!DBA::isResult($contact)) {
2269 Logger::info('Couldn\'t select contact for archival.', ['id' => $id]);
2273 if (isset($fields['failed'])) {
2274 if ($fields['failed']) {
2275 self::markForArchival($contact);
2277 self::unmarkForArchival($contact);
2281 if ($contact['uid'] != 0) {
2285 // Update contact data for all users
2286 $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)];
2288 $condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB];
2289 self::update($fields, $condition);
2291 // We mustn't set the update fields for OStatus contacts since they are updated in OnePoll
2292 $condition['network'] = Protocol::OSTATUS;
2294 // If the contact failed, propagate the update fields to all contacts
2295 if (empty($fields['failed'])) {
2296 unset($fields['last-update']);
2297 unset($fields['success_update']);
2298 unset($fields['failure_update']);
2301 if (empty($fields)) {
2305 self::update($fields, $condition);
2309 * Create or update an "account-user" entry
2311 * @param integer $id
2312 * @param integer $uid
2313 * @param integer $uri_id
2314 * @param string $url
2317 public static function setAccountUser(int $id, int $uid, int $uri_id, string $url)
2319 if (empty($uri_id)) {
2323 $account_user = DBA::selectFirst('account-user', ['id', 'uid', 'uri-id'], ['id' => $id]);
2324 if (!empty($account_user['uri-id']) && ($account_user['uri-id'] != $uri_id)) {
2325 if ($account_user['uid'] == $uid) {
2326 $ret = DBA::update('account-user', ['uri-id' => $uri_id], ['id' => $id]);
2327 Logger::notice('Updated account-user uri-id', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2329 // This should never happen
2330 Logger::warning('account-user exists for a different uri-id and uid', ['account_user' => $account_user, 'id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2334 $account_user = DBA::selectFirst('account-user', ['id', 'uid', 'uri-id'], ['uid' => $uid, 'uri-id' => $uri_id]);
2335 if (!empty($account_user['id'])) {
2336 if ($account_user['id'] == $id) {
2337 Logger::debug('account-user already exists', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2339 } elseif (!DBA::exists('contact', ['id' => $account_user['id'], 'deleted' => false])) {
2340 $ret = DBA::update('account-user', ['id' => $id], ['uid' => $uid, 'uri-id' => $uri_id]);
2341 Logger::notice('Updated account-user', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2344 Logger::warning('account-user exists for a different contact id', ['account_user' => $account_user, 'id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2345 Worker::add(PRIORITY_HIGH, 'MergeContact', $account_user['id'], $id, $uid);
2346 } elseif (DBA::insert('account-user', ['id' => $id, 'uri-id' => $uri_id, 'uid' => $uid], Database::INSERT_IGNORE)) {
2347 Logger::notice('account-user was added', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2349 Logger::warning('account-user was not added', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2354 * Remove duplicated contacts
2356 * @param string $nurl Normalised contact url
2357 * @param integer $uid User id
2359 * @throws \Exception
2361 public static function removeDuplicates(string $nurl, int $uid)
2363 $condition = ['nurl' => $nurl, 'uid' => $uid, 'self' => false, 'deleted' => false, 'network' => Protocol::FEDERATED];
2364 $count = DBA::count('contact', $condition);
2369 $first_contact = DBA::selectFirst('contact', ['id', 'network'], $condition, ['order' => ['id']]);
2370 if (!DBA::isResult($first_contact)) {
2371 // Shouldn't happen - so we handle it
2375 $first = $first_contact['id'];
2376 Logger::info('Found duplicates', ['count' => $count, 'first' => $first, 'uid' => $uid, 'nurl' => $nurl]);
2378 // Find all duplicates
2379 $condition = ["`nurl` = ? AND `uid` = ? AND `id` != ? AND NOT `self` AND NOT `deleted`", $nurl, $uid, $first];
2380 $duplicates = DBA::select('contact', ['id', 'network'], $condition);
2381 while ($duplicate = DBA::fetch($duplicates)) {
2382 if (!in_array($duplicate['network'], Protocol::FEDERATED)) {
2386 Worker::add(PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
2388 DBA::close($duplicates);
2389 Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl, 'callstack' => System::callstack(20)]);
2394 * Updates contact record by provided id and optional network
2396 * @param integer $id contact id
2397 * @param string $network Optional network we are probing for
2399 * @throws HTTPException\InternalServerErrorException
2400 * @throws \ImagickException
2402 public static function updateFromProbe(int $id, string $network = '')
2404 $contact = DBA::selectFirst('contact', ['uid', 'url'], ['id' => $id]);
2405 if (!DBA::isResult($contact)) {
2409 $ret = Probe::uri($contact['url'], $network, $contact['uid']);
2411 if ($ret['network'] == Protocol::DIASPORA) {
2412 FContact::updateFromProbeArray($ret);
2415 return self::updateFromProbeArray($id, $ret);
2419 * Checks if the given contact has got local data
2422 * @param array $contact
2426 private static function hasLocalData(int $id, array $contact): bool
2428 if (!empty($contact['uri-id']) && DBA::exists('contact', ["`uri-id` = ? AND `uid` != ?", $contact['uri-id'], 0])) {
2429 // User contacts with the same uri-id exist
2431 } elseif (DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($contact['url']), 0])) {
2432 // User contacts with the same nurl exists (compatibility mode for systems with missing uri-id values)
2435 if (DBA::exists('post-tag', ['cid' => $id])) {
2436 // Is tagged in a post
2439 if (DBA::exists('user-contact', ['cid' => $id])) {
2440 // Has got user-contact data
2443 if (Post::exists(['author-id' => $id])) {
2444 // Posts with this author exist
2447 if (Post::exists(['owner-id' => $id])) {
2448 // Posts with this owner exist
2451 if (Post::exists(['causer-id' => $id])) {
2452 // Posts with this causer exist
2455 // We don't have got this contact locally
2460 * Updates contact record by provided id and probed data
2462 * @param integer $id contact id
2463 * @param array $ret Probed data
2465 * @throws HTTPException\InternalServerErrorException
2466 * @throws \ImagickException
2468 private static function updateFromProbeArray(int $id, array $ret): bool
2471 Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
2472 This will reliably kill your communication with old Friendica contacts.
2475 // These fields aren't updated by this routine:
2478 $fields = ['uid', 'uri-id', 'avatar', 'header', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe',
2479 'manually-approve', 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
2480 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item', 'xmpp', 'matrix',
2481 'created', 'last-update'];
2482 $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
2483 if (!DBA::isResult($contact)) {
2487 if (self::isLocal($ret['url'])) {
2488 if ($contact['uid'] == 0) {
2489 Logger::info('Local contacts are not updated here.');
2491 self::updateFromPublicContact($id, $contact);
2496 if (!empty($ret['account-type']) && $ret['account-type'] == User::ACCOUNT_TYPE_DELETED) {
2497 Logger::info('Deleted account', ['id' => $id, 'url' => $ret['url'], 'ret' => $ret]);
2500 // Delete all contacts with the same URL
2501 self::deleteContactByUrl($ret['url']);
2505 $uid = $contact['uid'];
2506 unset($contact['uid']);
2508 $uriid = $contact['uri-id'];
2509 unset($contact['uri-id']);
2511 $pubkey = $contact['pubkey'];
2512 unset($contact['pubkey']);
2514 $created = $contact['created'];
2515 unset($contact['created']);
2517 $last_update = $contact['last-update'];
2518 unset($contact['last-update']);
2520 $contact['photo'] = $contact['avatar'];
2521 unset($contact['avatar']);
2523 $updated = DateTimeFormat::utcNow();
2525 $has_local_data = self::hasLocalData($id, $contact);
2527 if (!in_array($ret['network'], array_merge(Protocol::FEDERATED, [Protocol::ZOT, Protocol::PHANTOM]))) {
2528 // Periodical checks are only done on federated contacts
2529 $failed_next_update = null;
2530 $success_next_update = null;
2531 } elseif ($has_local_data) {
2532 $failed_next_update = GServer::getNextUpdateDate(false, $created, $last_update, !in_array($contact['network'], Protocol::FEDERATED));
2533 $success_next_update = GServer::getNextUpdateDate(true, $created, $last_update, !in_array($contact['network'], Protocol::FEDERATED));
2535 $failed_next_update = DateTimeFormat::utc('now +6 month');
2536 $success_next_update = DateTimeFormat::utc('now +1 month');
2539 if (Strings::normaliseLink($contact['url']) != Strings::normaliseLink($ret['url'])) {
2540 Logger::notice('New URL differs from old URL', ['id' => $id, 'uid' => $uid, 'old' => $contact['url'], 'new' => $ret['url']]);
2541 self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => true, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $failed_next_update, 'failure_update' => $updated]);
2545 // We must not try to update relay contacts via probe. They are no real contacts.
2546 // We check after the probing to be able to correct falsely detected contact types.
2547 if (($contact['contact-type'] == self::TYPE_RELAY) &&
2548 (!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))) {
2549 self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => false, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $success_next_update, 'success_update' => $updated]);
2550 Logger::info('Not updating relais', ['id' => $id, 'url' => $contact['url']]);
2554 // If Probe::uri fails the network code will be different ("feed" or "unkn")
2555 if (($ret['network'] == Protocol::PHANTOM) || (($ret['network'] == Protocol::FEED) && ($ret['network'] != $contact['network']))) {
2556 self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => true, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $failed_next_update, 'failure_update' => $updated]);
2560 if (Strings::normaliseLink($ret['url']) != Strings::normaliseLink($contact['url'])) {
2561 $cid = self::getIdForURL($ret['url'], 0, false);
2562 if (!empty($cid) && ($cid != $id)) {
2563 Logger::notice('URL of contact changed.', ['id' => $id, 'new_id' => $cid, 'old' => $contact['url'], 'new' => $ret['url']]);
2564 return self::updateFromProbeArray($cid, $ret);
2568 if (isset($ret['hide']) && is_bool($ret['hide'])) {
2569 $ret['unsearchable'] = $ret['hide'];
2572 if (isset($ret['account-type']) && is_int($ret['account-type'])) {
2573 $ret['forum'] = false;
2574 $ret['prv'] = false;
2575 $ret['contact-type'] = $ret['account-type'];
2576 if (($ret['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY) && isset($ret['manually-approve'])) {
2577 $ret['forum'] = (bool)!$ret['manually-approve'];
2578 $ret['prv'] = (bool)!$ret['forum'];
2582 $new_pubkey = $ret['pubkey'] ?? '';
2584 if ($uid == 0 && DI::config()->get('system', 'fetch_featured_posts')) {
2585 if ($ret['network'] == Protocol::ACTIVITYPUB) {
2586 $apcontact = APContact::getByURL($ret['url'], false);
2587 if (!empty($apcontact['featured'])) {
2588 Worker::add(PRIORITY_LOW, 'FetchFeaturedPosts', $ret['url']);
2592 $ret['last-item'] = Probe::getLastUpdate($ret);
2593 Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
2597 $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], parse_url($ret['url'], PHP_URL_HOST));
2599 // make sure to not overwrite existing values with blank entries except some technical fields
2600 $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];
2601 foreach ($ret as $key => $val) {
2602 if (!array_key_exists($key, $contact)) {
2604 } elseif (($contact[$key] != '') && ($val === '') && !is_bool($ret[$key]) && !in_array($key, $keep)) {
2605 $ret[$key] = $contact[$key];
2606 } elseif ($ret[$key] != $contact[$key]) {
2611 if (!empty($ret['last-item']) && ($contact['last-item'] < $ret['last-item'])) {
2614 unset($ret['last-item']);
2617 if (empty($uriid)) {
2621 if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
2622 self::updateAvatar($id, $ret['photo'], $update);
2626 self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => false, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $success_next_update, 'success_update' => $updated]);
2628 if (Contact\Relation::isDiscoverable($ret['url'])) {
2629 Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
2632 // Update the public contact
2634 $contact = self::getByURL($ret['url'], false, ['id']);
2635 if (!empty($contact['id'])) {
2636 self::updateFromProbeArray($contact['id'], $ret);
2643 $ret['uri-id'] = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]);
2644 $ret['nurl'] = Strings::normaliseLink($ret['url']);
2645 $ret['updated'] = $updated;
2646 $ret['failed'] = false;
2647 $ret['next-update'] = $success_next_update;
2648 $ret['local-data'] = $has_local_data;
2650 // Only fill the pubkey if it had been empty before. We have to prevent identity theft.
2651 if (empty($pubkey) && !empty($new_pubkey)) {
2652 $ret['pubkey'] = $new_pubkey;
2655 if ((!empty($ret['addr']) && ($ret['addr'] != $contact['addr'])) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
2656 $ret['uri-date'] = $updated;
2659 if ((!empty($ret['name']) && ($ret['name'] != $contact['name'])) || (!empty($ret['nick']) && ($ret['nick'] != $contact['nick']))) {
2660 $ret['name-date'] = $updated;
2663 if (($uid == 0) || in_array($ret['network'], [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
2664 $ret['last-update'] = $updated;
2665 $ret['success_update'] = $updated;
2668 unset($ret['photo']);
2670 self::updateContact($id, $uid, $ret['uri-id'], $ret['url'], $ret);
2672 if (Contact\Relation::isDiscoverable($ret['url'])) {
2673 Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
2679 private static function updateFromPublicContact(int $id, array $contact)
2681 $public = self::getByURL($contact['url'], false);
2685 foreach ($contact as $field => $value) {
2686 if ($field == 'uid') {
2689 if ($public[$field] != $value) {
2690 $fields[$field] = $public[$field];
2693 if (!empty($fields)) {
2694 self::update($fields, ['id' => $id, 'self' => false]);
2695 Logger::info('Updating local contact', ['id' => $id]);
2700 * Updates contact record by provided URL
2702 * @param integer $url contact url
2703 * @return integer Contact id
2704 * @throws HTTPException\InternalServerErrorException
2705 * @throws \ImagickException
2707 public static function updateFromProbeByURL(string $url): int
2709 $id = self::getIdForURL($url);
2715 self::updateFromProbe($id);
2721 * Detects the communication protocol for a given contact url.
2722 * This is used to detect Friendica contacts that we can communicate via AP.
2724 * @param string $url contact url
2725 * @param string $network Network of that contact
2726 * @return string with protocol
2728 public static function getProtocol(string $url, string $network): string
2730 if ($network != Protocol::DFRN) {
2734 $apcontact = APContact::getByURL($url);
2735 if (!empty($apcontact) && !empty($apcontact['generator'])) {
2736 return Protocol::ACTIVITYPUB;
2743 * Takes a $uid and a url/handle and adds a new contact
2745 * Currently if the contact is DFRN, interactive needs to be true, to redirect to the
2746 * dfrn_request page.
2748 * Otherwise this can be used to bulk add StatusNet contacts, Twitter contacts, etc.
2751 * $return['success'] boolean true if successful
2752 * $return['message'] error text if success is false.
2754 * Takes a $uid and a url/handle and adds a new contact
2756 * @param int $uid The user id the contact should be created for
2757 * @param string $url The profile URL of the contact
2758 * @param string $network
2760 * @throws HTTPException\InternalServerErrorException
2761 * @throws HTTPException\NotFoundException
2762 * @throws \ImagickException
2764 public static function createFromProbeForUser(int $uid, string $url, string $network = ''): array
2766 $result = ['cid' => -1, 'success' => false, 'message' => ''];
2768 // remove ajax junk, e.g. Twitter
2769 $url = str_replace('/#!/', '/', $url);
2771 if (!Network::isUrlAllowed($url)) {
2772 $result['message'] = DI::l10n()->t('Disallowed profile URL.');
2776 if (Network::isUrlBlocked($url)) {
2777 $result['message'] = DI::l10n()->t('Blocked domain');
2782 $result['message'] = DI::l10n()->t('Connect URL missing.');
2786 $arr = ['url' => $url, 'contact' => []];
2788 Hook::callAll('follow', $arr);
2791 $result['message'] = DI::l10n()->t('The contact could not be added. Please check the relevant network credentials in your Settings -> Social Networks page.');
2795 if (!empty($arr['contact']['name'])) {
2797 $ret = $arr['contact'];
2800 $ret = Probe::uri($url, $network, $uid);
2802 // Ensure that the public contact exists
2803 if ($ret['network'] != Protocol::PHANTOM) {
2804 self::getIdForURL($url);
2808 if (($network != '') && ($ret['network'] != $network)) {
2809 Logger::notice('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
2813 // check if we already have a contact
2814 // the poll url is more reliable than the profile url, as we may have
2815 // indirect links or webfinger links
2817 $condition = ['uid' => $uid, 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
2818 $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2819 if (!DBA::isResult($contact)) {
2820 $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
2821 $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2824 $protocol = self::getProtocol($ret['url'], $ret['network']);
2826 // This extra param just confuses things, remove it
2827 if ($protocol === Protocol::DIASPORA) {
2828 $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
2831 // do we have enough information?
2832 if (empty($protocol) || ($protocol == Protocol::PHANTOM) || (empty($ret['url']) && empty($ret['addr']))) {
2833 $result['message'] .= DI::l10n()->t('The profile address specified does not provide adequate information.') . EOL;
2834 if (empty($ret['poll'])) {
2835 $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
2837 if (empty($ret['name'])) {
2838 $result['message'] .= DI::l10n()->t('An author or name was not found.') . EOL;
2840 if (empty($ret['url'])) {
2841 $result['message'] .= DI::l10n()->t('No browser URL could be matched to this address.') . EOL;
2843 if (strpos($ret['url'], '@') !== false) {
2844 $result['message'] .= DI::l10n()->t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
2845 $result['message'] .= DI::l10n()->t('Use mailto: in front of address to force email check.') . EOL;
2850 if ($protocol === Protocol::OSTATUS && DI::config()->get('system', 'ostatus_disabled')) {
2851 $result['message'] .= DI::l10n()->t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
2852 $ret['notify'] = '';
2855 if (!$ret['notify']) {
2856 $result['message'] .= DI::l10n()->t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
2859 $writeable = ((($protocol === Protocol::OSTATUS) && ($ret['notify'])) ? 1 : 0);
2861 $subhub = (($protocol === Protocol::OSTATUS) ? true : false);
2863 $hidden = (($protocol === Protocol::MAIL) ? 1 : 0);
2866 if (($protocol == Protocol::ACTIVITYPUB) && isset($ret['manually-approve'])) {
2867 $pending = (bool)$ret['manually-approve'];
2870 if (in_array($protocol, [Protocol::MAIL, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
2874 if (DBA::isResult($contact)) {
2876 $new_relation = (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND]) ? self::FRIEND : self::SHARING);
2878 $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
2879 self::update($fields, ['id' => $contact['id']]);
2881 $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
2883 // create contact record
2886 'created' => DateTimeFormat::utcNow(),
2887 'url' => $ret['url'],
2888 'nurl' => Strings::normaliseLink($ret['url']),
2889 'addr' => $ret['addr'],
2890 'alias' => $ret['alias'],
2891 'batch' => $ret['batch'],
2892 'notify' => $ret['notify'],
2893 'poll' => $ret['poll'],
2894 'poco' => $ret['poco'],
2895 'name' => $ret['name'],
2896 'nick' => $ret['nick'],
2897 'network' => $ret['network'],
2898 'baseurl' => $ret['baseurl'],
2899 'gsid' => $ret['gsid'] ?? null,
2900 'protocol' => $protocol,
2901 'pubkey' => $ret['pubkey'],
2902 'rel' => $new_relation,
2903 'priority'=> $ret['priority'],
2904 'writable'=> $writeable,
2905 'hidden' => $hidden,
2908 'pending' => $pending,
2913 $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]);
2914 if (!DBA::isResult($contact)) {
2915 $result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL;
2919 $contact_id = $contact['id'];
2920 $result['cid'] = $contact_id;
2922 Group::addMember(User::getDefaultGroup($uid), $contact_id);
2924 // Update the avatar
2925 self::updateAvatar($contact_id, $ret['photo']);
2927 // pull feed and consume it, which should subscribe to the hub.
2928 if ($contact['network'] == Protocol::OSTATUS) {
2929 Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
2933 self::updateFromProbeArray($contact_id, $ret);
2935 Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id);
2938 $result['success'] = Protocol::follow($uid, $contact, $protocol);
2944 * @param array $importer Owner (local user) data
2945 * @param array $contact Existing owner-specific contact data we want to expand the relationship with. Optional.
2946 * @param array $datarray An item-like array with at least the 'author-id' and 'author-url' keys for the contact. Mandatory.
2947 * @param bool $sharing True: Contact is now sharing with Owner; False: Contact is now following Owner (default)
2948 * @param string $note Introduction additional message
2949 * @return bool|null True: follow request is accepted; False: relationship is rejected; Null: relationship is pending
2950 * @throws HTTPException\InternalServerErrorException
2951 * @throws \ImagickException
2953 public static function addRelationship(array $importer, array $contact, array $datarray, bool $sharing = false, string $note = '')
2955 // Should always be set
2956 if (empty($datarray['author-id'])) {
2960 $fields = ['id', 'url', 'name', 'nick', 'avatar', 'photo', 'network', 'blocked'];
2961 $pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
2962 if (!DBA::isResult($pub_contact)) {
2963 // Should never happen
2967 // Contact is blocked at node-level
2968 if (self::isBlocked($datarray['author-id'])) {
2972 $url = ($datarray['author-link'] ?? '') ?: $pub_contact['url'];
2973 $name = $pub_contact['name'];
2974 $photo = ($pub_contact['avatar'] ?? '') ?: $pub_contact["photo"];
2975 $nick = $pub_contact['nick'];
2976 $network = $pub_contact['network'];
2978 // Ensure that we don't create a new contact when there already is one
2979 $cid = self::getIdForURL($url, $importer['uid']);
2981 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
2984 self::clearFollowerFollowingEndpointCache($importer['uid']);
2986 if (!empty($contact)) {
2987 if (!empty($contact['pending'])) {
2988 Logger::info('Pending contact request already exists.', ['url' => $url, 'uid' => $importer['uid']]);
2992 // Contact is blocked at user-level
2993 if (!empty($contact['id']) && !empty($importer['id']) &&
2994 Contact\User::isBlocked($contact['id'], $importer['id'])) {
2998 // Make sure that the existing contact isn't archived
2999 self::unmarkForArchival($contact);
3001 if (($contact['rel'] == self::SHARING)
3002 || ($sharing && $contact['rel'] == self::FOLLOWER)) {
3003 self::update(['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
3004 ['id' => $contact['id'], 'uid' => $importer['uid']]);
3007 // Ensure to always have the correct network type, independent from the connection request method
3008 self::updateFromProbe($contact['id']);
3010 Post\UserNotification::insertNotification($pub_contact['id'], Activity::FOLLOW, $importer['uid']);
3014 // send email notification to owner?
3015 if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) {
3016 Logger::notice('ignoring duplicated connection request from pending contact ' . $url);
3020 // create contact record
3021 $contact_id = self::insert([
3022 'uid' => $importer['uid'],
3023 'created' => DateTimeFormat::utcNow(),
3025 'nurl' => Strings::normaliseLink($url),
3028 'network' => $network,
3029 'rel' => self::FOLLOWER,
3036 // Ensure to always have the correct network type, independent from the connection request method
3037 self::updateFromProbe($contact_id);
3039 self::updateAvatar($contact_id, $photo, true);
3041 Post\UserNotification::insertNotification($pub_contact['id'], Activity::FOLLOW, $importer['uid']);
3043 $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
3045 /// @TODO Encapsulate this into a function/method
3046 $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language'];
3047 $user = DBA::selectFirst('user', $fields, ['uid' => $importer['uid']]);
3048 if (DBA::isResult($user) && !in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
3049 // create notification
3050 if (is_array($contact_record)) {
3051 $intro = DI::introFactory()->createNew(
3053 $contact_record['id'],
3056 DI::intro()->save($intro);
3059 Group::addMember(User::getDefaultGroup($importer['uid']), $contact_record['id']);
3061 if (($user['notify-flags'] & Notification\Type::INTRO) && $user['page-flags'] == User::PAGE_FLAGS_NORMAL) {
3062 DI::notify()->createFromArray([
3063 'type' => Notification\Type::INTRO,
3064 'otype' => Notification\ObjectType::INTRO,
3065 'verb' => ($sharing ? Activity::FRIEND : Activity::FOLLOW),
3066 'uid' => $user['uid'],
3067 'cid' => $contact_record['id'],
3068 'link' => DI::baseUrl() . '/notifications/intros',
3071 } elseif (DBA::isResult($user) && in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
3072 if (($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) && ($network != Protocol::DIASPORA)) {
3073 self::createFromProbeForUser($importer['uid'], $url, $network);
3076 $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
3077 $fields = ['pending' => false];
3078 if ($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) {
3079 $fields['rel'] = self::FRIEND;
3082 self::update($fields, $condition);
3092 * Update the local relationship when a local user loses a follower
3094 * @param array $contact User-specific contact (uid != 0) array
3096 * @throws HTTPException\InternalServerErrorException
3097 * @throws \ImagickException
3099 public static function removeFollower(array $contact)
3101 if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) {
3102 self::update(['rel' => self::SHARING], ['id' => $contact['id']]);
3103 } elseif (!empty($contact['id'])) {
3104 self::remove($contact['id']);
3106 DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]);
3110 self::clearFollowerFollowingEndpointCache($contact['uid']);
3112 $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
3114 DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]);
3118 * Update the local relationship when a local user unfollow a contact.
3119 * Removes the contact for sharing-only protocols (feed and mail).
3121 * @param array $contact User-specific contact (uid != 0) array
3122 * @throws HTTPException\InternalServerErrorException
3124 public static function removeSharer(array $contact)
3126 self::clearFollowerFollowingEndpointCache($contact['uid']);
3128 if ($contact['rel'] == self::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
3129 self::remove($contact['id']);
3131 self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]);
3136 * Create a birthday event.
3138 * Update the year and the birthday.
3140 public static function updateBirthdays()
3144 AND (`contact`.`rel` = ? OR `contact`.`rel` = ?)
3145 AND NOT `contact`.`pending`
3146 AND NOT `contact`.`hidden`
3147 AND NOT `contact`.`blocked`
3148 AND NOT `contact`.`archive`
3149 AND NOT `contact`.`deleted`',
3155 $contacts = DBA::select('contact', ['id', 'uid', 'name', 'url', 'bd'], $condition);
3157 while ($contact = DBA::fetch($contacts)) {
3158 Logger::notice('update_contact_birthday: ' . $contact['bd']);
3160 $nextbd = DateTimeFormat::utcNow('Y') . substr($contact['bd'], 4);
3162 if (Event::createBirthday($contact, $nextbd)) {
3166 ['bdyear' => substr($nextbd, 0, 4), 'bd' => $nextbd],
3167 ['id' => $contact['id']]
3171 DBA::close($contacts);
3175 * Remove the unavailable contact ids from the provided list
3177 * @param array $contact_ids Contact id list
3179 * @throws \Exception
3181 public static function pruneUnavailable(array $contact_ids): array
3183 if (empty($contact_ids)) {
3187 $contacts = self::selectToArray(['id'], [
3188 'id' => $contact_ids,
3194 return array_column($contacts, 'id');
3198 * Returns a magic link to authenticate remote visitors
3200 * @todo check if the return is either a fully qualified URL or a relative path to Friendica basedir
3202 * @param string $contact_url The address of the target contact profile
3203 * @param string $url An url that we will be redirected to after the authentication
3205 * @return string with "redir" link
3206 * @throws HTTPException\InternalServerErrorException
3207 * @throws \ImagickException
3209 public static function magicLink(string $contact_url, string $url = ''): string
3211 if (!Session::isAuthenticated()) {
3212 return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
3215 $contact = self::getByURL($contact_url, false);
3216 if (empty($contact)) {
3217 return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
3220 // Prevents endless loop in case only a non-public contact exists for the contact URL
3221 unset($contact['uid']);
3223 return self::magicLinkByContact($contact, $url ?: $contact_url);
3227 * Returns a magic link to authenticate remote visitors
3229 * @param integer $cid The contact id of the target contact profile
3230 * @param string $url An url that we will be redirected to after the authentication
3232 * @return string with "redir" link
3233 * @throws HTTPException\InternalServerErrorException
3234 * @throws \ImagickException
3236 public static function magicLinkById(int $cid, string $url = ''): string
3238 $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
3240 return self::magicLinkByContact($contact, $url);
3244 * Returns a magic link to authenticate remote visitors
3246 * @param array $contact The contact array with "uid", "network" and "url"
3247 * @param string $url An url that we will be redirected to after the authentication
3249 * @return string with "redir" link
3250 * @throws HTTPException\InternalServerErrorException
3251 * @throws \ImagickException
3253 public static function magicLinkByContact(array $contact, string $url = ''): string
3255 $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
3257 if (!Session::isAuthenticated()) {
3258 return $destination;
3261 // Only redirections to the same host do make sense
3262 if (($url != '') && (parse_url($url, PHP_URL_HOST) != parse_url($contact['url'], PHP_URL_HOST))) {
3266 if (DI::pConfig()->get(local_user(), 'system', 'stay_local') && ($url == '')) {
3267 return 'contact/' . $contact['id'] . '/conversations';
3270 if (!empty($contact['network']) && $contact['network'] != Protocol::DFRN) {
3271 return $destination;
3274 if (empty($contact['id'])) {
3275 return $destination;
3278 $redirect = 'redir/' . $contact['id'];
3280 if (($url != '') && !Strings::compareLink($contact['url'], $url)) {
3281 $redirect .= '?url=' . $url;
3288 * Is the contact a forum?
3290 * @param integer $contactid ID of the contact
3292 * @return boolean "true" if it is a forum
3294 public static function isForum(int $contactid): bool
3296 $fields = ['contact-type'];
3297 $condition = ['id' => $contactid];
3298 $contact = DBA::selectFirst('contact', $fields, $condition);
3299 if (!DBA::isResult($contact)) {
3304 return ($contact['contact-type'] == self::TYPE_COMMUNITY);
3308 * Can the remote contact receive private messages?
3310 * @param array $contact
3313 public static function canReceivePrivateMessages(array $contact): bool
3315 $protocol = $contact['network'] ?? $contact['protocol'] ?? Protocol::PHANTOM;
3316 $self = $contact['self'] ?? false;
3318 return in_array($protocol, [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB]) && !$self;
3322 * Search contact table by nick or name
3324 * @param string $search Name or nick
3325 * @param string $mode Search mode (e.g. "community")
3326 * @param int $uid User ID
3328 * @return array with search results
3329 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3331 public static function searchByName(string $search, string $mode = '', int $uid = 0): array
3333 if (empty($search)) {
3337 // check supported networks
3338 $networks = [Protocol::DFRN, Protocol::ACTIVITYPUB];
3339 if (DI::config()->get('system', 'diaspora_enabled')) {
3340 $networks[] = Protocol::DIASPORA;
3343 if (!DI::config()->get('system', 'ostatus_disabled')) {
3344 $networks[] = Protocol::OSTATUS;
3347 $condition = ['network' => $networks, 'failed' => false, 'deleted' => false, 'uid' => $uid];
3350 $condition['blocked'] = false;
3353 // check if we search only communities or every contact
3354 if ($mode === 'community') {
3355 $condition['contact-type'] = self::TYPE_COMMUNITY;
3360 $condition = DBA::mergeConditions($condition,
3361 ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` WHERE `publish` OR `net-publish`))
3362 AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]);
3364 $contacts = self::selectToArray([], $condition);
3369 * Add public contacts from an array
3371 * @param array $urls
3372 * @return array result "count", "added" and "updated"
3374 public static function addByUrls(array $urls): array
3381 foreach ($urls as $url) {
3382 if (empty($url) || !is_string($url)) {
3385 $contact = self::getByURL($url, false, ['id', 'updated']);
3386 if (empty($contact['id']) && Network::isValidHttpUrl($url)) {
3387 Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
3389 } elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {
3390 Worker::add(PRIORITY_LOW, 'UpdateContact', $contact['id']);
3398 return ['count' => $count, 'added' => $added, 'updated' => $updated, 'unchanged' => $unchanged];
3402 * Returns a random, global contact array of the current node
3404 * @return array The profile array
3407 public static function getRandomContact(): array
3409 $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], [
3410 "`uid` = ? AND `network` = ? AND NOT `failed` AND `last-item` > ?",
3411 0, Protocol::DFRN, DateTimeFormat::utc('now - 1 month'),
3412 ], ['order' => ['RAND()']]);
3414 if (DBA::isResult($contact)) {