3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\App\BaseURL;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Core\Hook;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Session;
32 use Friendica\Core\System;
33 use Friendica\Core\Worker;
34 use Friendica\Database\DBA;
36 use Friendica\Model\Notify\Type;
37 use Friendica\Network\HTTPException;
38 use Friendica\Network\Probe;
39 use Friendica\Protocol\Activity;
40 use Friendica\Protocol\ActivityPub;
41 use Friendica\Protocol\DFRN;
42 use Friendica\Protocol\Diaspora;
43 use Friendica\Protocol\OStatus;
44 use Friendica\Protocol\Salmon;
45 use Friendica\Util\DateTimeFormat;
46 use Friendica\Util\Images;
47 use Friendica\Util\Network;
48 use Friendica\Util\Proxy;
49 use Friendica\Util\Strings;
52 * functions for interacting with a contact
56 const DEFAULT_AVATAR_PHOTO = '/images/person-300.jpg';
57 const DEFAULT_AVATAR_THUMB = '/images/person-80.jpg';
58 const DEFAULT_AVATAR_MICRO = '/images/person-48.jpg';
61 * @deprecated since version 2019.03
62 * @see User::PAGE_FLAGS_NORMAL
64 const PAGE_NORMAL = User::PAGE_FLAGS_NORMAL;
66 * @deprecated since version 2019.03
67 * @see User::PAGE_FLAGS_SOAPBOX
69 const PAGE_SOAPBOX = User::PAGE_FLAGS_SOAPBOX;
71 * @deprecated since version 2019.03
72 * @see User::PAGE_FLAGS_COMMUNITY
74 const PAGE_COMMUNITY = User::PAGE_FLAGS_COMMUNITY;
76 * @deprecated since version 2019.03
77 * @see User::PAGE_FLAGS_FREELOVE
79 const PAGE_FREELOVE = User::PAGE_FLAGS_FREELOVE;
81 * @deprecated since version 2019.03
82 * @see User::PAGE_FLAGS_BLOG
84 const PAGE_BLOG = User::PAGE_FLAGS_BLOG;
86 * @deprecated since version 2019.03
87 * @see User::PAGE_FLAGS_PRVGROUP
89 const PAGE_PRVGROUP = User::PAGE_FLAGS_PRVGROUP;
94 const LOCK_INSERT = 'contact-insert';
99 * TYPE_UNKNOWN - unknown type
101 * TYPE_PERSON - the account belongs to a person
102 * Associated page types: PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE
104 * TYPE_ORGANISATION - the account belongs to an organisation
105 * Associated page type: PAGE_SOAPBOX
107 * TYPE_NEWS - the account is a news reflector
108 * Associated page type: PAGE_SOAPBOX
110 * TYPE_COMMUNITY - the account is community forum
111 * Associated page types: PAGE_COMMUNITY, PAGE_PRVGROUP
113 * TYPE_RELAY - the account is a relay
114 * This will only be assigned to contacts, not to user accounts
117 const TYPE_UNKNOWN = -1;
118 const TYPE_PERSON = User::ACCOUNT_TYPE_PERSON;
119 const TYPE_ORGANISATION = User::ACCOUNT_TYPE_ORGANISATION;
120 const TYPE_NEWS = User::ACCOUNT_TYPE_NEWS;
121 const TYPE_COMMUNITY = User::ACCOUNT_TYPE_COMMUNITY;
122 const TYPE_RELAY = User::ACCOUNT_TYPE_RELAY;
142 * @param array $fields Array of selected fields, empty for all
143 * @param array $condition Array of fields for condition
144 * @param array $params Array of several parameters
148 public static function selectToArray(array $fields = [], array $condition = [], array $params = [])
150 return DBA::selectToArray('contact', $fields, $condition, $params);
154 * @param array $fields Array of selected fields, empty for all
155 * @param array $condition Array of fields for condition
156 * @param array $params Array of several parameters
160 public static function selectFirst(array $fields = [], array $condition = [], array $params = [])
162 $contact = DBA::selectFirst('contact', $fields, $condition, $params);
168 * Insert a row into the contact table
169 * Important: You can't use DBA::lastInsertId() after this call since it will be set to 0.
171 * @param array $fields field array
172 * @param bool $on_duplicate_update Do an update on a duplicate entry
174 * @return boolean was the insert successful?
177 public static function insert(array $fields, bool $on_duplicate_update = false)
179 $ret = DBA::insert('contact', $fields, $on_duplicate_update);
180 $contact = DBA::selectFirst('contact', ['nurl', 'uid'], ['id' => DBA::lastInsertId()]);
181 if (!DBA::isResult($contact)) {
186 // Search for duplicated contacts and get rid of them
187 self::removeDuplicates($contact['nurl'], $contact['uid']);
193 * @param integer $id Contact ID
194 * @param array $fields Array of selected fields, empty for all
195 * @return array|boolean Contact record if it exists, false otherwise
198 public static function getById($id, $fields = [])
200 return DBA::selectFirst('contact', $fields, ['id' => $id]);
204 * Fetches a contact by a given url
206 * @param string $url profile url
207 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
208 * @param array $fields Field list
209 * @param integer $uid User ID of the contact
210 * @return array contact array
212 public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0)
214 if ($update || is_null($update)) {
215 $cid = self::getIdForURL($url, $uid, $update);
220 $contact = self::getById($cid, $fields);
221 if (empty($contact)) {
227 // Add internal fields
229 if (!empty($fields)) {
230 foreach (['id', 'avatar', 'created', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
231 if (!in_array($internal, $fields)) {
232 $fields[] = $internal;
233 $removal[] = $internal;
238 // We first try the nurl (http://server.tld/nick), most common case
239 $options = ['order' => ['id']];
240 $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options);
242 // Then the addr (nick@server.tld)
243 if (!DBA::isResult($contact)) {
244 $contact = DBA::selectFirst('contact', $fields, ['addr' => str_replace('acct:', '', $url), 'uid' => $uid, 'deleted' => false], $options);
247 // Then the alias (which could be anything)
248 if (!DBA::isResult($contact)) {
249 // The link could be provided as http although we stored it as https
250 $ssl_url = str_replace('http://', 'https://', $url);
251 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid];
252 $contact = DBA::selectFirst('contact', $fields, $condition, $options);
255 if (!DBA::isResult($contact)) {
259 // Update the contact in the background if needed
260 $updated = max($contact['success_update'], $contact['created'], $contact['updated'], $contact['last-update'], $contact['failure_update']);
261 if (($updated < DateTimeFormat::utc('now -7 days')) && in_array($contact['network'], Protocol::FEDERATED)) {
262 Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']);
265 // Remove the internal fields
266 foreach ($removal as $internal) {
267 unset($contact[$internal]);
274 * Fetches a contact for a given user by a given url.
275 * In difference to "getByURL" the function will fetch a public contact when no user contact had been found.
277 * @param string $url profile url
278 * @param integer $uid User ID of the contact
279 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
280 * @param array $fields Field list
281 * @return array contact array
283 public static function getByURLForUser(string $url, int $uid = 0, $update = false, array $fields = [])
286 $contact = self::getByURL($url, $update, $fields, $uid);
287 if (!empty($contact)) {
288 if (!empty($contact['id'])) {
289 $contact['cid'] = $contact['id'];
296 $contact = self::getByURL($url, $update, $fields);
297 if (!empty($contact['id'])) {
299 $contact['zid'] = $contact['id'];
305 * Tests if the given contact is a follower
307 * @param int $cid Either public contact id or user's contact id
308 * @param int $uid User ID
310 * @return boolean is the contact id a follower?
311 * @throws HTTPException\InternalServerErrorException
312 * @throws \ImagickException
314 public static function isFollower($cid, $uid)
316 if (Contact\User::isBlocked($cid, $uid)) {
320 $cdata = self::getPublicAndUserContacID($cid, $uid);
321 if (empty($cdata['user'])) {
325 $condition = ['id' => $cdata['user'], 'rel' => [self::FOLLOWER, self::FRIEND]];
326 return DBA::exists('contact', $condition);
330 * Tests if the given contact url is a follower
332 * @param string $url Contact URL
333 * @param int $uid User ID
335 * @return boolean is the contact id a follower?
336 * @throws HTTPException\InternalServerErrorException
337 * @throws \ImagickException
339 public static function isFollowerByURL($url, $uid)
341 $cid = self::getIdForURL($url, $uid);
347 return self::isFollower($cid, $uid);
351 * Tests if the given user follow the given contact
353 * @param int $cid Either public contact id or user's contact id
354 * @param int $uid User ID
356 * @return boolean is the contact url being followed?
357 * @throws HTTPException\InternalServerErrorException
358 * @throws \ImagickException
360 public static function isSharing($cid, $uid)
362 if (Contact\User::isBlocked($cid, $uid)) {
366 $cdata = self::getPublicAndUserContacID($cid, $uid);
367 if (empty($cdata['user'])) {
371 $condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]];
372 return DBA::exists('contact', $condition);
376 * Tests if the given user follow the given contact url
378 * @param string $url Contact URL
379 * @param int $uid User ID
381 * @return boolean is the contact url being followed?
382 * @throws HTTPException\InternalServerErrorException
383 * @throws \ImagickException
385 public static function isSharingByURL($url, $uid)
387 $cid = self::getIdForURL($url, $uid);
393 return self::isSharing($cid, $uid);
397 * Get the basepath for a given contact link
399 * @param string $url The contact link
400 * @param boolean $dont_update Don't update the contact
402 * @return string basepath
403 * @throws HTTPException\InternalServerErrorException
404 * @throws \ImagickException
406 public static function getBasepath($url, $dont_update = false)
408 $contact = DBA::selectFirst('contact', ['id', 'baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]);
409 if (!DBA::isResult($contact)) {
413 if (!empty($contact['baseurl'])) {
414 return $contact['baseurl'];
415 } elseif ($dont_update) {
419 // Update the existing contact
420 self::updateFromProbe($contact['id']);
422 // And fetch the result
423 $contact = DBA::selectFirst('contact', ['baseurl'], ['id' => $contact['id']]);
424 if (empty($contact['baseurl'])) {
425 Logger::info('No baseurl for contact', ['url' => $url]);
429 Logger::info('Found baseurl for contact', ['url' => $url, 'baseurl' => $contact['baseurl']]);
430 return $contact['baseurl'];
434 * Check if the given contact url is on the same server
436 * @param string $url The contact link
438 * @return boolean Is it the same server?
440 public static function isLocal($url)
442 return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl());
446 * Check if the given contact ID is on the same server
448 * @param string $url The contact link
450 * @return boolean Is it the same server?
452 public static function isLocalById(int $cid)
454 $contact = DBA::selectFirst('contact', ['url', 'baseurl'], ['id' => $cid]);
455 if (!DBA::isResult($contact)) {
459 if (empty($contact['baseurl'])) {
460 $baseurl = self::getBasepath($contact['url'], true);
462 $baseurl = $contact['baseurl'];
465 return Strings::compareLink($baseurl, DI::baseUrl());
469 * Returns the public contact id of the given user id
471 * @param integer $uid User ID
473 * @return integer|boolean Public contact id for given user id
476 public static function getPublicIdByUserId($uid)
478 $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
479 if (!DBA::isResult($self)) {
482 return self::getIdForURL($self['url']);
486 * Returns the contact id for the user and the public contact id for a given contact id
488 * @param int $cid Either public contact id or user's contact id
489 * @param int $uid User ID
491 * @return array with public and user's contact id
492 * @throws HTTPException\InternalServerErrorException
493 * @throws \ImagickException
495 public static function getPublicAndUserContacID($cid, $uid)
497 if (empty($uid) || empty($cid)) {
501 $contact = DBA::selectFirst('contact', ['id', 'uid', 'url'], ['id' => $cid]);
502 if (!DBA::isResult($contact)) {
506 // We quit when the user id don't match the user id of the provided contact
507 if (($contact['uid'] != $uid) && ($contact['uid'] != 0)) {
511 if ($contact['uid'] != 0) {
512 $pcid = Contact::getIdForURL($contact['url'], 0, false, ['url' => $contact['url']]);
516 $ucid = $contact['id'];
518 $pcid = $contact['id'];
519 $ucid = Contact::getIdForURL($contact['url'], $uid);
522 return ['public' => $pcid, 'user' => $ucid];
526 * Returns contact details for a given contact id in combination with a user id
528 * @param int $cid A contact ID
529 * @param int $uid The User ID
530 * @param array $fields The selected fields for the contact
532 * @return array The contact details
536 public static function getContactForUser($cid, $uid, array $fields = [])
538 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => $uid]);
540 if (!DBA::isResult($contact)) {
548 * Creates the self-contact for the provided user id
551 * @return bool Operation success
552 * @throws HTTPException\InternalServerErrorException
554 public static function createSelfFromUserId($uid)
556 // Only create the entry if it doesn't exist yet
557 if (DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
561 $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'pubkey', 'prvkey'], ['uid' => $uid]);
562 if (!DBA::isResult($user)) {
566 $return = DBA::insert('contact', [
567 'uid' => $user['uid'],
568 'created' => DateTimeFormat::utcNow(),
570 'name' => $user['username'],
571 'nick' => $user['nickname'],
572 'pubkey' => $user['pubkey'],
573 'prvkey' => $user['prvkey'],
574 'photo' => DI::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
575 'thumb' => DI::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
576 'micro' => DI::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg',
579 'url' => DI::baseUrl() . '/profile/' . $user['nickname'],
580 'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname']),
581 'addr' => $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3),
582 'request' => DI::baseUrl() . '/dfrn_request/' . $user['nickname'],
583 'notify' => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'],
584 'poll' => DI::baseUrl() . '/dfrn_poll/' . $user['nickname'],
585 'confirm' => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
586 'poco' => DI::baseUrl() . '/poco/' . $user['nickname'],
587 'name-date' => DateTimeFormat::utcNow(),
588 'uri-date' => DateTimeFormat::utcNow(),
589 'avatar-date' => DateTimeFormat::utcNow(),
597 * Updates the self-contact for the provided user id
600 * @param boolean $update_avatar Force the avatar update
601 * @throws HTTPException\InternalServerErrorException
603 public static function updateSelfFromUserID($uid, $update_avatar = false)
605 $fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey',
606 'xmpp', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
607 'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco'];
608 $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
609 if (!DBA::isResult($self)) {
613 $fields = ['nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
614 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
615 if (!DBA::isResult($user)) {
619 $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
620 'country-name', 'pub_keywords', 'xmpp', 'net-publish'];
621 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
622 if (!DBA::isResult($profile)) {
626 $file_suffix = 'jpg';
628 $fields = ['name' => $profile['name'], 'nick' => $user['nickname'],
629 'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile),
630 'about' => $profile['about'], 'keywords' => $profile['pub_keywords'],
631 'contact-type' => $user['account-type'], 'prvkey' => $user['prvkey'],
632 'pubkey' => $user['pubkey'], 'xmpp' => $profile['xmpp']];
634 $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
635 if (DBA::isResult($avatar)) {
636 if ($update_avatar) {
637 $fields['avatar-date'] = DateTimeFormat::utcNow();
640 // Creating the path to the avatar, beginning with the file suffix
641 $types = Images::supportedTypes();
642 if (isset($types[$avatar['type']])) {
643 $file_suffix = $types[$avatar['type']];
646 // We are adding a timestamp value so that other systems won't use cached content
647 $timestamp = strtotime($fields['avatar-date']);
649 $prefix = DI::baseUrl() . '/photo/' .$avatar['resource-id'] . '-';
650 $suffix = '.' . $file_suffix . '?ts=' . $timestamp;
652 $fields['photo'] = $prefix . '4' . $suffix;
653 $fields['thumb'] = $prefix . '5' . $suffix;
654 $fields['micro'] = $prefix . '6' . $suffix;
656 // We hadn't found a photo entry, so we use the default avatar
657 $fields['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
658 $fields['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
659 $fields['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
662 $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
663 $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
664 $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
665 $fields['unsearchable'] = !$profile['net-publish'];
667 // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
668 $fields['url'] = DI::baseUrl() . '/profile/' . $user['nickname'];
669 $fields['nurl'] = Strings::normaliseLink($fields['url']);
670 $fields['addr'] = $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
671 $fields['request'] = DI::baseUrl() . '/dfrn_request/' . $user['nickname'];
672 $fields['notify'] = DI::baseUrl() . '/dfrn_notify/' . $user['nickname'];
673 $fields['poll'] = DI::baseUrl() . '/dfrn_poll/'. $user['nickname'];
674 $fields['confirm'] = DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'];
675 $fields['poco'] = DI::baseUrl() . '/poco/' . $user['nickname'];
679 foreach ($fields as $field => $content) {
680 if ($self[$field] != $content) {
686 if ($fields['name'] != $self['name']) {
687 $fields['name-date'] = DateTimeFormat::utcNow();
689 $fields['updated'] = DateTimeFormat::utcNow();
690 DBA::update('contact', $fields, ['id' => $self['id']]);
692 // Update the public contact as well
693 DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
695 // Update the profile
696 $fields = ['photo' => DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix,
697 'thumb' => DI::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix];
698 DBA::update('profile', $fields, ['uid' => $uid]);
703 * Marks a contact for removal
705 * @param int $id contact id
707 * @throws HTTPException\InternalServerErrorException
709 public static function remove($id)
711 // We want just to make sure that we don't delete our "self" contact
712 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $id, 'self' => false]);
713 if (!DBA::isResult($contact) || !intval($contact['uid'])) {
717 // Archive the contact
718 DBA::update('contact', ['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
720 // Delete it in the background
721 Worker::add(PRIORITY_MEDIUM, 'RemoveContact', $id);
725 * Sends an unfriend message. Does not remove the contact
727 * @param array $user User unfriending
728 * @param array $contact Contact unfriended
729 * @param boolean $dissolve Remove the contact on the remote side
731 * @throws HTTPException\InternalServerErrorException
732 * @throws \ImagickException
734 public static function terminateFriendship(array $user, array $contact, $dissolve = false)
736 if (empty($contact['network'])) {
740 $protocol = $contact['network'];
741 if (($protocol == Protocol::DFRN) && !self::isLegacyDFRNContact($contact)) {
742 $protocol = Protocol::ACTIVITYPUB;
745 if (($protocol == Protocol::DFRN) && $dissolve) {
746 DFRN::deliver($user, $contact, 'placeholder', true);
747 } elseif (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
748 // create an unfollow slap
750 $item['verb'] = Activity::O_UNFOLLOW;
751 $item['gravity'] = GRAVITY_ACTIVITY;
752 $item['follow'] = $contact["url"];
757 $item['attach'] = '';
758 $slap = OStatus::salmon($item, $user);
760 if (!empty($contact['notify'])) {
761 Salmon::slapper($user, $contact['notify'], $slap);
763 } elseif ($protocol == Protocol::DIASPORA) {
764 Diaspora::sendUnshare($user, $contact);
765 } elseif ($protocol == Protocol::ACTIVITYPUB) {
766 ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
769 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
775 * Marks a contact for archival after a communication issue delay
777 * Contact has refused to recognise us as a friend. We will start a countdown.
778 * If they still don't recognise us in 32 days, the relationship is over,
779 * and we won't waste any more time trying to communicate with them.
780 * This provides for the possibility that their database is temporarily messed
781 * up or some other transient event and that there's a possibility we could recover from it.
783 * @param array $contact contact to mark for archival
785 * @throws HTTPException\InternalServerErrorException
787 public static function markForArchival(array $contact)
789 if (!isset($contact['url']) && !empty($contact['id'])) {
790 $fields = ['id', 'url', 'archive', 'self', 'term-date'];
791 $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
792 if (!DBA::isResult($contact)) {
795 } elseif (!isset($contact['url'])) {
796 Logger::info('Empty contact', ['contact' => $contact, 'callstack' => System::callstack(20)]);
799 Logger::info('Contact is marked for archival', ['id' => $contact['id']]);
801 // Contact already archived or "self" contact? => nothing to do
802 if ($contact['archive'] || $contact['self']) {
806 if ($contact['term-date'] <= DBA::NULL_DATETIME) {
807 DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
808 DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
811 * We really should send a notification to the owner after 2-3 weeks
812 * so they won't be surprised when the contact vanishes and can take
813 * remedial action if this was a serious mistake or glitch
816 /// @todo Check for contact vitality via probing
817 $archival_days = DI::config()->get('system', 'archival_days', 32);
819 $expiry = $contact['term-date'] . ' + ' . $archival_days . ' days ';
820 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($expiry)) {
821 /* Relationship is really truly dead. archive them rather than
822 * delete, though if the owner tries to unarchive them we'll start
823 * the whole process over again.
825 DBA::update('contact', ['archive' => true], ['id' => $contact['id']]);
826 DBA::update('contact', ['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
832 * Cancels the archival countdown
834 * @see Contact::markForArchival()
836 * @param array $contact contact to be unmarked for archival
840 public static function unmarkForArchival(array $contact)
842 // Always unarchive the relay contact entry
843 if (!empty($contact['batch']) && !empty($contact['term-date']) && ($contact['term-date'] > DBA::NULL_DATETIME)) {
844 $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
845 $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
846 DBA::update('contact', $fields, $condition);
849 $condition = ['`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], DBA::NULL_DATETIME];
850 $exists = DBA::exists('contact', $condition);
852 // We don't need to update, we never marked this contact for archival
857 Logger::info('Contact is marked as vital again', ['id' => $contact['id']]);
859 if (!isset($contact['url']) && !empty($contact['id'])) {
860 $fields = ['id', 'url', 'batch'];
861 $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
862 if (!DBA::isResult($contact)) {
867 // It's a miracle. Our dead contact has inexplicably come back to life.
868 $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
869 DBA::update('contact', $fields, ['id' => $contact['id']]);
870 DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
874 * Returns the data array for the photo menu of a given contact
876 * @param array $contact contact
877 * @param int $uid optional, default 0
879 * @throws HTTPException\InternalServerErrorException
880 * @throws \ImagickException
882 public static function photoMenu(array $contact, $uid = 0)
887 $contact_drop_link = '';
894 if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
896 $profile_link = self::magicLink($contact['url']);
897 $menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
902 // Look for our own contact if the uid doesn't match and isn't public
903 $contact_own = DBA::selectFirst('contact', [], ['nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid]);
904 if (DBA::isResult($contact_own)) {
905 return self::photoMenu($contact_own, $uid);
910 if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
912 $profile_link = DI::baseUrl() . '/redir/' . $contact['id'];
914 $profile_link = $contact['url'];
917 if ($profile_link === 'mailbox') {
922 $status_link = $profile_link . '/status';
923 $photos_link = str_replace('/profile/', '/photos/', $profile_link);
924 $profile_link = $profile_link . '/profile';
927 if (self::canReceivePrivateMessages($contact) && empty($contact['pending'])) {
928 $pm_url = DI::baseUrl() . '/message/new/' . $contact['id'];
931 if (($contact['network'] == Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
932 $poke_link = 'contact/' . $contact['id'] . '/poke';
935 $contact_url = DI::baseUrl() . '/contact/' . $contact['id'];
937 $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations';
939 if (!$contact['self']) {
940 $contact_drop_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/drop?confirm=1';
945 if (!$contact['self'] && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
946 if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
947 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1';
948 } elseif(!$contact['pending']) {
949 $follow_link = 'follow?url=' . urlencode($contact['url']) . '&auto=1';
953 if (!empty($follow_link) || !empty($unfollow_link)) {
954 $contact_drop_link = '';
959 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
961 if (empty($contact['uid'])) {
963 'profile' => [DI::l10n()->t('View Profile') , $profile_link , true],
964 'network' => [DI::l10n()->t('Network Posts') , $posts_link , false],
965 'edit' => [DI::l10n()->t('View Contact') , $contact_url , false],
966 'follow' => [DI::l10n()->t('Connect/Follow'), $follow_link , true],
967 'unfollow'=> [DI::l10n()->t('UnFollow') , $unfollow_link, true],
971 'status' => [DI::l10n()->t('View Status') , $status_link , true],
972 'profile' => [DI::l10n()->t('View Profile') , $profile_link , true],
973 'photos' => [DI::l10n()->t('View Photos') , $photos_link , true],
974 'network' => [DI::l10n()->t('Network Posts') , $posts_link , false],
975 'edit' => [DI::l10n()->t('View Contact') , $contact_url , false],
976 'drop' => [DI::l10n()->t('Drop Contact') , $contact_drop_link, false],
977 'pm' => [DI::l10n()->t('Send PM') , $pm_url , false],
978 'poke' => [DI::l10n()->t('Poke') , $poke_link , false],
979 'follow' => [DI::l10n()->t('Connect/Follow'), $follow_link , true],
980 'unfollow'=> [DI::l10n()->t('UnFollow') , $unfollow_link , true],
983 if (!empty($contact['pending'])) {
984 $intro = DBA::selectFirst('intro', ['id'], ['contact-id' => $contact['id']]);
985 if (DBA::isResult($intro)) {
986 $menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro['id'], true];
991 $args = ['contact' => $contact, 'menu' => &$menu];
993 Hook::callAll('contact_photo_menu', $args);
997 foreach ($menu as $menuname => $menuitem) {
998 if ($menuitem[1] != '') {
999 $menucondensed[$menuname] = $menuitem;
1003 return $menucondensed;
1007 * Fetch the contact id for a given URL and user
1009 * First lookup in the contact table to find a record matching either `url`, `nurl`,
1010 * `addr` or `alias`.
1012 * If there's no record and we aren't looking for a public contact, we quit.
1013 * If there's one, we check that it isn't time to update the picture else we
1014 * directly return the found contact id.
1016 * Second, we probe the provided $url whether it's http://server.tld/profile or
1017 * nick@server.tld. We quit if we can't get any info back.
1019 * Third, we create the contact record if it doesn't exist
1021 * Fourth, we update the existing record with the new data (avatar, alias, nick)
1022 * if there's any updates
1024 * @param string $url Contact URL
1025 * @param integer $uid The user id for the contact (0 = public contact)
1026 * @param boolean $update true = always update, false = never update, null = update when not found
1027 * @param array $default Default value for creating the contact when everything else fails
1029 * @return integer Contact ID
1030 * @throws HTTPException\InternalServerErrorException
1031 * @throws \ImagickException
1033 public static function getIdForURL($url, $uid = 0, $update = null, $default = [])
1038 Logger::notice('Empty url, quitting', ['url' => $url, 'user' => $uid, 'default' => $default]);
1042 $contact = self::getByURL($url, false, ['id', 'network'], $uid);
1044 if (!empty($contact)) {
1045 $contact_id = $contact["id"];
1047 if (empty($update)) {
1048 Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
1051 } elseif ($uid != 0) {
1052 Logger::debug('Contact does not exist for the user', ['url' => $url, 'uid' => $uid, 'update' => $update]);
1054 } elseif (empty($default) && !is_null($update) && !$update) {
1055 Logger::info('Contact not found, update not desired', ['url' => $url, 'uid' => $uid, 'update' => $update]);
1061 if (empty($default['network']) || $update) {
1062 $data = Probe::uri($url, "", $uid);
1064 // Take the default values when probing failed
1065 if (!empty($default) && !in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
1066 $data = array_merge($data, $default);
1068 } elseif (!empty($default['network'])) {
1072 if (($uid == 0) && (empty($data['network']) || ($data['network'] == Protocol::PHANTOM))) {
1073 // Fetch data for the public contact via the first found personal contact
1074 /// @todo Check if this case can happen at all (possibly with mail accounts?)
1075 $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'contact-type',
1076 'keywords', 'location', 'about', 'unsearchable', 'batch', 'notify', 'poll',
1077 'request', 'confirm', 'poco', 'subscribe', 'network', 'baseurl', 'gsid'];
1079 $personal_contact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `uid` != 0", $url]);
1080 if (!DBA::isResult($personal_contact)) {
1081 $personal_contact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `uid` != 0", Strings::normaliseLink($url)]);
1084 if (DBA::isResult($personal_contact)) {
1085 Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact, 'callstack' => System::callstack(20)]);
1086 $data = $personal_contact;
1087 $data['photo'] = $personal_contact['avatar'];
1088 $data['account-type'] = $personal_contact['contact-type'];
1089 $data['hide'] = $personal_contact['unsearchable'];
1090 unset($data['avatar']);
1091 unset($data['contact-type']);
1092 unset($data['unsearchable']);
1096 if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
1097 Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update, 'callstack' => System::callstack(20)]);
1102 $urls = [Strings::normaliseLink($url), Strings::normaliseLink($data['url'])];
1103 if (!empty($data['alias'])) {
1104 $urls[] = Strings::normaliseLink($data['alias']);
1106 $contact = self::selectFirst(['id'], ['nurl' => $urls, 'uid' => $uid]);
1107 if (!empty($contact['id'])) {
1108 $contact_id = $contact['id'];
1109 Logger::info('Fetched id by url', ['cid' => $contact_id, 'uid' => $uid, 'url' => $url, 'probed_url' => $data['url'], 'alias' => $data['alias'], 'addr' => $data['addr']]);
1114 // We only insert the basic data. The rest will be done in "updateFromProbeArray"
1117 'url' => $data['url'],
1118 'nurl' => Strings::normaliseLink($data['url']),
1119 'network' => $data['network'],
1120 'created' => DateTimeFormat::utcNow(),
1121 'rel' => self::SHARING,
1127 $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
1129 // Before inserting we do check if the entry does exist now.
1130 if (DI::lock()->acquire(self::LOCK_INSERT, 0)) {
1131 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
1132 if (DBA::isResult($contact)) {
1133 $contact_id = $contact['id'];
1134 Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
1136 DBA::insert('contact', $fields);
1137 $contact_id = DBA::lastInsertId();
1139 Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
1142 DI::lock()->release(self::LOCK_INSERT);
1144 Logger::warning('Contact lock had not been acquired');
1148 Logger::info('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
1152 Logger::info('Contact will be updated', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
1155 self::updateFromProbeArray($contact_id, $data);
1161 * Checks if the contact is archived
1163 * @param int $cid contact id
1165 * @return boolean Is the contact archived?
1166 * @throws HTTPException\InternalServerErrorException
1168 public static function isArchived(int $cid)
1174 $contact = DBA::selectFirst('contact', ['archive', 'url', 'batch'], ['id' => $cid]);
1175 if (!DBA::isResult($contact)) {
1179 if ($contact['archive']) {
1183 // Check status of ActivityPub endpoints
1184 $apcontact = APContact::getByURL($contact['url'], false);
1185 if (!empty($apcontact)) {
1186 if (!empty($apcontact['inbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['inbox']])) {
1190 if (!empty($apcontact['sharedinbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['sharedinbox']])) {
1195 // Check status of Diaspora endpoints
1196 if (!empty($contact['batch'])) {
1197 $condition = ['archive' => true, 'uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
1198 return DBA::exists('contact', $condition);
1205 * Checks if the contact is blocked
1207 * @param int $cid contact id
1209 * @return boolean Is the contact blocked?
1210 * @throws HTTPException\InternalServerErrorException
1212 public static function isBlocked($cid)
1218 $blocked = DBA::selectFirst('contact', ['blocked', 'url'], ['id' => $cid]);
1219 if (!DBA::isResult($blocked)) {
1223 if (Network::isUrlBlocked($blocked['url'])) {
1227 return (bool) $blocked['blocked'];
1231 * Checks if the contact is hidden
1233 * @param int $cid contact id
1235 * @return boolean Is the contact hidden?
1236 * @throws \Exception
1238 public static function isHidden($cid)
1244 $hidden = DBA::selectFirst('contact', ['hidden'], ['id' => $cid]);
1245 if (!DBA::isResult($hidden)) {
1248 return (bool) $hidden['hidden'];
1252 * Returns posts from a given contact url
1254 * @param string $contact_url Contact URL
1255 * @param bool $thread_mode
1256 * @param int $update Update mode
1257 * @param int $parent Item parent ID for the update mode
1258 * @return string posts in HTML
1259 * @throws \Exception
1261 public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0)
1263 return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent);
1267 * Returns posts from a given contact id
1269 * @param int $cid Contact ID
1270 * @param bool $thread_mode
1271 * @param int $update Update mode
1272 * @param int $parent Item parent ID for the update mode
1273 * @return string posts in HTML
1274 * @throws \Exception
1276 public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0)
1280 $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
1281 if (!DBA::isResult($contact)) {
1285 if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
1286 $sql = "`item`.`uid` IN (0, ?)";
1288 $sql = "`item`.`uid` = ?";
1291 $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
1294 $condition = ["(`$contact_field` = ? OR (`causer-id` = ? AND `post-type` = ?)) AND `gravity` = ? AND " . $sql,
1295 $cid, $cid, Item::PT_ANNOUNCEMENT, GRAVITY_PARENT, local_user()];
1297 $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
1298 $cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
1301 if (!empty($parent)) {
1302 $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
1304 $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
1305 if (!empty($last_received)) {
1306 $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
1310 if (DI::mode()->isMobile()) {
1311 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
1312 DI::config()->get('system', 'itemspage_network_mobile'));
1314 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
1315 DI::config()->get('system', 'itemspage_network'));
1318 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
1320 $params = ['order' => ['received' => true], 'group_by' => ['uri-id'],
1321 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
1323 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
1324 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1325 $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
1331 $r = Item::selectForUser(local_user(), ['uri', 'gravity', 'parent-uri'], $condition, $params);
1333 while ($item = DBA::fetch($r)) {
1334 if ($item['gravity'] != GRAVITY_PARENT) {
1335 $item['uri'] = $item['parent-uri'];
1337 unset($item['parent-uri']);
1338 unset($item['gravity']);
1344 $o .= conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
1346 $r = Item::selectForUser(local_user(), [], $condition, $params);
1348 $items = Item::inArray($r);
1350 $o .= conversation($a, $items, 'contact-posts', $update);
1354 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
1355 $o .= HTML::scrollLoader();
1357 $o .= $pager->renderMinimal(count($items));
1365 * Returns the account type name
1367 * The function can be called with either the user or the contact array
1369 * @param array $contact contact or user array
1372 public static function getAccountType(array $contact)
1374 // There are several fields that indicate that the contact or user is a forum
1375 // "page-flags" is a field in the user table,
1376 // "forum" and "prv" are used in the contact table. They stand for User::PAGE_FLAGS_COMMUNITY and User::PAGE_FLAGS_PRVGROUP.
1377 if ((isset($contact['page-flags']) && (intval($contact['page-flags']) == User::PAGE_FLAGS_COMMUNITY))
1378 || (isset($contact['page-flags']) && (intval($contact['page-flags']) == User::PAGE_FLAGS_PRVGROUP))
1379 || (isset($contact['forum']) && intval($contact['forum']))
1380 || (isset($contact['prv']) && intval($contact['prv']))
1381 || (isset($contact['community']) && intval($contact['community']))
1383 $type = self::TYPE_COMMUNITY;
1385 $type = self::TYPE_PERSON;
1388 // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
1389 if (isset($contact["contact-type"])) {
1390 $type = $contact["contact-type"];
1393 if (isset($contact["account-type"])) {
1394 $type = $contact["account-type"];
1398 case self::TYPE_ORGANISATION:
1399 $account_type = DI::l10n()->t("Organisation");
1402 case self::TYPE_NEWS:
1403 $account_type = DI::l10n()->t('News');
1406 case self::TYPE_COMMUNITY:
1407 $account_type = DI::l10n()->t("Forum");
1415 return $account_type;
1423 * @throws \Exception
1425 public static function block($cid, $reason = null)
1427 $return = DBA::update('contact', ['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
1433 * Unblocks a contact
1437 * @throws \Exception
1439 public static function unblock($cid)
1441 $return = DBA::update('contact', ['blocked' => false, 'block_reason' => null], ['id' => $cid]);
1447 * Ensure that cached avatar exist
1449 * @param integer $cid
1451 public static function checkAvatarCache(int $cid)
1453 $contact = DBA::selectFirst('contact', ['url', 'avatar', 'photo', 'thumb', 'micro'], ['id' => $cid, 'uid' => 0, 'self' => false]);
1454 if (!DBA::isResult($contact)) {
1458 if (empty($contact['avatar']) || (!empty($contact['photo']) && !empty($contact['thumb']) && !empty($contact['micro']))) {
1462 Logger::info('Adding avatar cache', ['id' => $cid, 'contact' => $contact]);
1464 self::updateAvatar($cid, $contact['avatar'], true);
1468 * Return the photo path for a given contact array in the given size
1470 * @param array $contact contact array
1471 * @param string $field Fieldname of the photo in the contact array
1472 * @param string $default Default path when no picture had been found
1473 * @param string $size Size of the avatar picture
1474 * @param string $avatar Avatar path that is displayed when no photo had been found
1475 * @return string photo path
1477 private static function getAvatarPath(array $contact, string $field, string $default, string $size, string $avatar)
1479 if (!empty($contact)) {
1480 $contact = self::checkAvatarCacheByArray($contact);
1481 if (!empty($contact[$field])) {
1482 $avatar = $contact[$field];
1486 if (empty($avatar)) {
1490 if (Proxy::isLocalImage($avatar)) {
1493 return Proxy::proxifyUrl($avatar, false, $size);
1498 * Return the photo path for a given contact array
1500 * @param array $contact Contact array
1501 * @param string $avatar Avatar path that is displayed when no photo had been found
1502 * @return string photo path
1504 public static function getPhoto(array $contact, string $avatar = '')
1506 return self::getAvatarPath($contact, 'photo', DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO, Proxy::SIZE_SMALL, $avatar);
1510 * Return the photo path (thumb size) for a given contact array
1512 * @param array $contact Contact array
1513 * @param string $avatar Avatar path that is displayed when no photo had been found
1514 * @return string photo path
1516 public static function getThumb(array $contact, string $avatar = '')
1518 return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . self::DEFAULT_AVATAR_THUMB, Proxy::SIZE_THUMB, $avatar);
1522 * Return the photo path (micro size) for a given contact array
1524 * @param array $contact Contact array
1525 * @param string $avatar Avatar path that is displayed when no photo had been found
1526 * @return string photo path
1528 public static function getMicro(array $contact, string $avatar = '')
1530 return self::getAvatarPath($contact, 'micro', DI::baseUrl() . self::DEFAULT_AVATAR_MICRO, Proxy::SIZE_MICRO, $avatar);
1534 * Check the given contact array for avatar cache fields
1536 * @param array $contact
1537 * @return array contact array with avatar cache fields
1539 private static function checkAvatarCacheByArray(array $contact)
1542 $contact_fields = [];
1543 $fields = ['photo', 'thumb', 'micro'];
1544 foreach ($fields as $field) {
1545 if (isset($contact[$field])) {
1546 $contact_fields[] = $field;
1548 if (isset($contact[$field]) && empty($contact[$field])) {
1557 if (!empty($contact['id']) && !empty($contact['avatar'])) {
1558 self::updateAvatar($contact['id'], $contact['avatar'], true);
1560 $new_contact = self::getById($contact['id'], $contact_fields);
1561 if (DBA::isResult($new_contact)) {
1562 // We only update the cache fields
1563 $contact = array_merge($contact, $new_contact);
1567 /// add the default avatars if the fields aren't filled
1568 if (isset($contact['photo']) && empty($contact['photo'])) {
1569 $contact['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
1571 if (isset($contact['thumb']) && empty($contact['thumb'])) {
1572 $contact['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
1574 if (isset($contact['micro']) && empty($contact['micro'])) {
1575 $contact['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
1582 * Updates the avatar links in a contact only if needed
1584 * @param int $cid Contact id
1585 * @param string $avatar Link to avatar picture
1586 * @param bool $force force picture update
1587 * @param bool $create_cache Enforces the creation of cached avatar fields
1590 * @throws HTTPException\InternalServerErrorException
1591 * @throws HTTPException\NotFoundException
1592 * @throws \ImagickException
1594 public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
1596 $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url', 'network'], ['id' => $cid, 'self' => false]);
1597 if (!DBA::isResult($contact)) {
1601 $uid = $contact['uid'];
1603 // Only update the cached photo links of public contacts when they already are cached
1604 if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
1605 if ($contact['avatar'] != $avatar) {
1606 DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
1607 Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
1612 // User contacts use are updated through the public contacts
1613 if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
1614 $pcid = self::getIdForURL($contact['url'], false);
1615 if (!empty($pcid)) {
1616 Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
1617 self::updateAvatar($pcid, $avatar, $force, true);
1622 // Replace cached avatar pictures from the default avatar with the default avatars in different sizes
1623 if (strpos($avatar, self::DEFAULT_AVATAR_PHOTO)) {
1624 $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
1625 'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO,
1626 'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB,
1627 'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO];
1628 Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
1631 // Use the data from the self account
1632 if (empty($fields)) {
1633 $local_uid = User::getIdForURL($contact['url']);
1634 if (!empty($local_uid)) {
1635 $fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
1636 Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
1640 if (empty($fields)) {
1641 $update = ($contact['avatar'] != $avatar) || $force;
1645 $contact['photo'] ?? '',
1646 $contact['thumb'] ?? '',
1647 $contact['micro'] ?? '',
1650 foreach ($data as $image_uri) {
1651 $image_rid = Photo::ridFromURI($image_uri);
1652 if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
1653 Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
1660 $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
1662 $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
1663 $update = !empty($fields);
1664 Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
1670 $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
1679 if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
1680 // Collect all user contacts of the given public contact
1681 $personal_contacts = DBA::select('contact', ['id', 'uid'],
1682 ["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
1683 while ($personal_contact = DBA::fetch($personal_contacts)) {
1684 $cids[] = $personal_contact['id'];
1685 $uids[] = $personal_contact['uid'];
1687 DBA::close($personal_contacts);
1689 if (!empty($cids)) {
1690 // Delete possibly existing cached user contact avatars
1691 Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'album' => Photo::CONTACT_PHOTOS]);
1697 Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
1698 DBA::update('contact', $fields, ['id' => $cids]);
1702 * Helper function for "updateFromProbe". Updates personal and public contact
1704 * @param integer $id contact id
1705 * @param integer $uid user id
1706 * @param string $url The profile URL of the contact
1707 * @param array $fields The fields that are updated
1709 * @throws \Exception
1711 private static function updateContact($id, $uid, $url, array $fields)
1713 if (!DBA::update('contact', $fields, ['id' => $id])) {
1714 Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
1718 // Search for duplicated contacts and get rid of them
1719 if (self::removeDuplicates(Strings::normaliseLink($url), $uid) || ($uid != 0)) {
1723 // Archive or unarchive the contact. We only need to do this for the public contact.
1724 // The archive/unarchive function will update the personal contacts by themselves.
1725 $contact = DBA::selectFirst('contact', [], ['id' => $id]);
1726 if (!DBA::isResult($contact)) {
1727 Logger::info('Couldn\'t select contact for archival.', ['id' => $id]);
1731 if (!empty($fields['success_update'])) {
1732 self::unmarkForArchival($contact);
1733 } elseif (!empty($fields['failure_update'])) {
1734 self::markForArchival($contact);
1737 $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url), 'network' => Protocol::FEDERATED];
1739 // These contacts are sharing with us, we don't poll them.
1740 // This means that we don't set the update fields in "OnePoll.php".
1741 $condition['rel'] = self::SHARING;
1742 DBA::update('contact', $fields, $condition);
1744 unset($fields['last-update']);
1745 unset($fields['success_update']);
1746 unset($fields['failure_update']);
1748 if (empty($fields)) {
1752 // We are polling these contacts, so we mustn't set the update fields here.
1753 $condition['rel'] = [self::FOLLOWER, self::FRIEND];
1754 DBA::update('contact', $fields, $condition);
1758 * Remove duplicated contacts
1760 * @param string $nurl Normalised contact url
1761 * @param integer $uid User id
1763 * @throws \Exception
1765 public static function removeDuplicates(string $nurl, int $uid)
1767 $condition = ['nurl' => $nurl, 'uid' => $uid, 'deleted' => false, 'network' => Protocol::FEDERATED];
1768 $count = DBA::count('contact', $condition);
1773 $first_contact = DBA::selectFirst('contact', ['id', 'network'], $condition, ['order' => ['id']]);
1774 if (!DBA::isResult($first_contact)) {
1775 // Shouldn't happen - so we handle it
1779 $first = $first_contact['id'];
1780 Logger::info('Found duplicates', ['count' => $count, 'first' => $first, 'uid' => $uid, 'nurl' => $nurl]);
1781 if (($uid != 0 && ($first_contact['network'] == Protocol::DFRN))) {
1782 // Don't handle non public DFRN duplicates by now (legacy DFRN is very special because of the key handling)
1783 Logger::info('Not handling non public DFRN duplicate', ['uid' => $uid, 'nurl' => $nurl]);
1787 // Find all duplicates
1788 $condition = ["`nurl` = ? AND `uid` = ? AND `id` != ? AND NOT `self` AND NOT `deleted`", $nurl, $uid, $first];
1789 $duplicates = DBA::select('contact', ['id', 'network'], $condition);
1790 while ($duplicate = DBA::fetch($duplicates)) {
1791 if (!in_array($duplicate['network'], Protocol::FEDERATED)) {
1795 Worker::add(PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
1797 DBA::close($duplicates);
1798 Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl]);
1803 * @param integer $id contact id
1804 * @param string $network Optional network we are probing for
1806 * @throws HTTPException\InternalServerErrorException
1807 * @throws \ImagickException
1809 public static function updateFromProbe(int $id, string $network = '')
1811 $contact = DBA::selectFirst('contact', ['uid', 'url'], ['id' => $id]);
1812 if (!DBA::isResult($contact)) {
1816 $ret = Probe::uri($contact['url'], $network, $contact['uid']);
1817 return self::updateFromProbeArray($id, $ret);
1821 * @param integer $id contact id
1822 * @param array $ret Probed data
1824 * @throws HTTPException\InternalServerErrorException
1825 * @throws \ImagickException
1827 private static function updateFromProbeArray(int $id, array $ret)
1830 Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
1831 This will reliably kill your communication with old Friendica contacts.
1834 // These fields aren't updated by this routine:
1835 // 'xmpp', 'sensitive'
1837 $fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe', 'manually-approve',
1838 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
1839 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item'];
1840 $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
1841 if (!DBA::isResult($contact)) {
1845 $uid = $contact['uid'];
1846 unset($contact['uid']);
1848 $pubkey = $contact['pubkey'];
1849 unset($contact['pubkey']);
1851 $contact['photo'] = $contact['avatar'];
1852 unset($contact['avatar']);
1854 $updated = DateTimeFormat::utcNow();
1856 // We must not try to update relay contacts via probe. They are no real contacts.
1857 // We check after the probing to be able to correct falsely detected contact types.
1858 if (($contact['contact-type'] == self::TYPE_RELAY) &&
1859 (!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))) {
1860 self::updateContact($id, $uid, $contact['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
1861 Logger::info('Not updating relais', ['id' => $id, 'url' => $contact['url']]);
1865 // If Probe::uri fails the network code will be different ("feed" or "unkn")
1866 if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
1868 self::updateContact($id, $uid, $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
1873 if (isset($ret['hide']) && is_bool($ret['hide'])) {
1874 $ret['unsearchable'] = $ret['hide'];
1877 if (isset($ret['account-type']) && is_int($ret['account-type'])) {
1878 $ret['forum'] = false;
1879 $ret['prv'] = false;
1880 $ret['contact-type'] = $ret['account-type'];
1881 if (($ret['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY) && isset($ret['manually-approve'])) {
1882 $ret['forum'] = (bool)!$ret['manually-approve'];
1883 $ret['prv'] = (bool)!$ret['forum'];
1887 $new_pubkey = $ret['pubkey'] ?? '';
1890 $ret['last-item'] = Probe::getLastUpdate($ret);
1891 Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
1896 // make sure to not overwrite existing values with blank entries except some technical fields
1897 $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];
1898 foreach ($ret as $key => $val) {
1899 if (!array_key_exists($key, $contact)) {
1901 } elseif (($contact[$key] != '') && ($val === '') && !is_bool($ret[$key]) && !in_array($key, $keep)) {
1902 $ret[$key] = $contact[$key];
1903 } elseif ($ret[$key] != $contact[$key]) {
1908 if (!empty($ret['last-item']) && ($contact['last-item'] < $ret['last-item'])) {
1911 unset($ret['last-item']);
1914 if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
1915 self::updateAvatar($id, $ret['photo'], $update);
1919 self::updateContact($id, $uid, $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
1921 if (Contact\Relation::isDiscoverable($ret['url'])) {
1922 Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
1925 // Update the public contact
1927 $contact = self::getByURL($ret['url'], false, ['id']);
1928 if (!empty($contact['id'])) {
1929 self::updateFromProbeArray($contact['id'], $ret);
1936 $ret['nurl'] = Strings::normaliseLink($ret['url']);
1937 $ret['updated'] = $updated;
1939 // Only fill the pubkey if it had been empty before. We have to prevent identity theft.
1940 if (empty($pubkey) && !empty($new_pubkey)) {
1941 $ret['pubkey'] = $new_pubkey;
1944 if ((!empty($ret['addr']) && ($ret['addr'] != $contact['addr'])) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
1945 $ret['uri-date'] = DateTimeFormat::utcNow();
1948 if (($ret['name'] != $contact['name']) || ($ret['nick'] != $contact['nick'])) {
1949 $ret['name-date'] = $updated;
1953 $ret['last-update'] = $updated;
1954 $ret['success_update'] = $updated;
1955 $ret['failed'] = false;
1958 unset($ret['photo']);
1960 self::updateContact($id, $uid, $ret['url'], $ret);
1962 if (Contact\Relation::isDiscoverable($ret['url'])) {
1963 Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
1970 * @param integer $url contact url
1971 * @return integer Contact id
1972 * @throws HTTPException\InternalServerErrorException
1973 * @throws \ImagickException
1975 public static function updateFromProbeByURL($url)
1977 $id = self::getIdForURL($url);
1983 self::updateFromProbe($id);
1989 * Detects if a given contact array belongs to a legacy DFRN connection
1991 * @param array $contact
1994 public static function isLegacyDFRNContact($contact)
1996 // Newer Friendica contacts are connected via AP, then these fields aren't set
1997 return !empty($contact['dfrn-id']) || !empty($contact['issued-id']);
2001 * Detects the communication protocol for a given contact url.
2002 * This is used to detect Friendica contacts that we can communicate via AP.
2004 * @param string $url contact url
2005 * @param string $network Network of that contact
2006 * @return string with protocol
2008 public static function getProtocol($url, $network)
2010 if ($network != Protocol::DFRN) {
2014 $apcontact = APContact::getByURL($url);
2015 if (!empty($apcontact) && !empty($apcontact['generator'])) {
2016 return Protocol::ACTIVITYPUB;
2023 * Takes a $uid and a url/handle and adds a new contact
2025 * Currently if the contact is DFRN, interactive needs to be true, to redirect to the
2026 * dfrn_request page.
2028 * Otherwise this can be used to bulk add StatusNet contacts, Twitter contacts, etc.
2031 * $return['success'] boolean true if successful
2032 * $return['message'] error text if success is false.
2034 * Takes a $uid and a url/handle and adds a new contact
2036 * @param array $user The user the contact should be created for
2037 * @param string $url The profile URL of the contact
2038 * @param bool $interactive
2039 * @param string $network
2041 * @throws HTTPException\InternalServerErrorException
2042 * @throws HTTPException\NotFoundException
2043 * @throws \ImagickException
2045 public static function createFromProbe(array $user, $url, $interactive = false, $network = '')
2047 $result = ['cid' => -1, 'success' => false, 'message' => ''];
2049 // remove ajax junk, e.g. Twitter
2050 $url = str_replace('/#!/', '/', $url);
2052 if (!Network::isUrlAllowed($url)) {
2053 $result['message'] = DI::l10n()->t('Disallowed profile URL.');
2057 if (Network::isUrlBlocked($url)) {
2058 $result['message'] = DI::l10n()->t('Blocked domain');
2063 $result['message'] = DI::l10n()->t('Connect URL missing.');
2067 $arr = ['url' => $url, 'contact' => []];
2069 Hook::callAll('follow', $arr);
2072 $result['message'] = DI::l10n()->t('The contact could not be added. Please check the relevant network credentials in your Settings -> Social Networks page.');
2076 if (!empty($arr['contact']['name'])) {
2077 $ret = $arr['contact'];
2079 $ret = Probe::uri($url, $network, $user['uid']);
2082 if (($network != '') && ($ret['network'] != $network)) {
2083 Logger::log('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
2087 // check if we already have a contact
2088 // the poll url is more reliable than the profile url, as we may have
2089 // indirect links or webfinger links
2091 $condition = ['uid' => $user['uid'], 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
2092 $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2093 if (!DBA::isResult($contact)) {
2094 $condition = ['uid' => $user['uid'], 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
2095 $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2098 $protocol = self::getProtocol($ret['url'], $ret['network']);
2100 if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) {
2102 if (strlen(DI::baseUrl()->getUrlPath())) {
2103 $myaddr = bin2hex(DI::baseUrl() . '/profile/' . $user['nickname']);
2105 $myaddr = bin2hex($user['nickname'] . '@' . DI::baseUrl()->getHostname());
2108 DI::baseUrl()->redirect($ret['request'] . "&addr=$myaddr");
2112 } elseif (DI::config()->get('system', 'dfrn_only') && ($ret['network'] != Protocol::DFRN)) {
2113 $result['message'] = DI::l10n()->t('This site is not configured to allow communications with other networks.') . EOL;
2114 $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
2118 // This extra param just confuses things, remove it
2119 if ($protocol === Protocol::DIASPORA) {
2120 $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
2123 // do we have enough information?
2124 if (empty($protocol) || ($protocol == Protocol::PHANTOM) || (empty($ret['url']) && empty($ret['addr']))) {
2125 $result['message'] .= DI::l10n()->t('The profile address specified does not provide adequate information.') . EOL;
2126 if (empty($ret['poll'])) {
2127 $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
2129 if (empty($ret['name'])) {
2130 $result['message'] .= DI::l10n()->t('An author or name was not found.') . EOL;
2132 if (empty($ret['url'])) {
2133 $result['message'] .= DI::l10n()->t('No browser URL could be matched to this address.') . EOL;
2135 if (strpos($ret['url'], '@') !== false) {
2136 $result['message'] .= DI::l10n()->t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
2137 $result['message'] .= DI::l10n()->t('Use mailto: in front of address to force email check.') . EOL;
2142 if ($protocol === Protocol::OSTATUS && DI::config()->get('system', 'ostatus_disabled')) {
2143 $result['message'] .= DI::l10n()->t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
2144 $ret['notify'] = '';
2147 if (!$ret['notify']) {
2148 $result['message'] .= DI::l10n()->t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
2151 $writeable = ((($protocol === Protocol::OSTATUS) && ($ret['notify'])) ? 1 : 0);
2153 $subhub = (($protocol === Protocol::OSTATUS) ? true : false);
2155 $hidden = (($protocol === Protocol::MAIL) ? 1 : 0);
2158 if (($protocol == Protocol::ACTIVITYPUB) && isset($ret['manually-approve'])) {
2159 $pending = (bool)$ret['manually-approve'];
2162 if (in_array($protocol, [Protocol::MAIL, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
2166 if (DBA::isResult($contact)) {
2168 $new_relation = (($contact['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING);
2170 $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
2171 DBA::update('contact', $fields, ['id' => $contact['id']]);
2173 $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
2175 // create contact record
2177 'uid' => $user['uid'],
2178 'created' => DateTimeFormat::utcNow(),
2179 'url' => $ret['url'],
2180 'nurl' => Strings::normaliseLink($ret['url']),
2181 'addr' => $ret['addr'],
2182 'alias' => $ret['alias'],
2183 'batch' => $ret['batch'],
2184 'notify' => $ret['notify'],
2185 'poll' => $ret['poll'],
2186 'poco' => $ret['poco'],
2187 'name' => $ret['name'],
2188 'nick' => $ret['nick'],
2189 'network' => $ret['network'],
2190 'baseurl' => $ret['baseurl'],
2191 'gsid' => $ret['gsid'] ?? null,
2192 'protocol' => $protocol,
2193 'pubkey' => $ret['pubkey'],
2194 'rel' => $new_relation,
2195 'priority'=> $ret['priority'],
2196 'writable'=> $writeable,
2197 'hidden' => $hidden,
2200 'pending' => $pending,
2205 $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $user['uid']]);
2206 if (!DBA::isResult($contact)) {
2207 $result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL;
2211 $contact_id = $contact['id'];
2212 $result['cid'] = $contact_id;
2214 Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id);
2216 // Update the avatar
2217 self::updateAvatar($contact_id, $ret['photo']);
2219 // pull feed and consume it, which should subscribe to the hub.
2221 Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");
2223 $owner = User::getOwnerDataById($user['uid']);
2225 if (DBA::isResult($owner)) {
2226 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
2227 // create a follow slap
2229 $item['verb'] = Activity::FOLLOW;
2230 $item['gravity'] = GRAVITY_ACTIVITY;
2231 $item['follow'] = $contact["url"];
2233 $item['title'] = '';
2235 $item['uri-id'] = 0;
2236 $item['attach'] = '';
2238 $slap = OStatus::salmon($item, $owner);
2240 if (!empty($contact['notify'])) {
2241 Salmon::slapper($owner, $contact['notify'], $slap);
2243 } elseif ($protocol == Protocol::DIASPORA) {
2244 $ret = Diaspora::sendShare($owner, $contact);
2245 Logger::log('share returns: ' . $ret);
2246 } elseif ($protocol == Protocol::ACTIVITYPUB) {
2247 $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact_id);
2248 if (empty($activity_id)) {
2249 // This really should never happen
2253 $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $user['uid'], $activity_id);
2254 Logger::log('Follow returns: ' . $ret);
2258 $result['success'] = true;
2263 * Updated contact's SSL policy
2265 * @param array $contact Contact array
2266 * @param string $new_policy New policy, valid: self,full
2268 * @return array Contact array with updated values
2269 * @throws \Exception
2271 public static function updateSslPolicy(array $contact, $new_policy)
2273 $ssl_changed = false;
2274 if ((intval($new_policy) == BaseURL::SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'], 'https:')) {
2275 $ssl_changed = true;
2276 $contact['url'] = str_replace('https:', 'http:', $contact['url']);
2277 $contact['request'] = str_replace('https:', 'http:', $contact['request']);
2278 $contact['notify'] = str_replace('https:', 'http:', $contact['notify']);
2279 $contact['poll'] = str_replace('https:', 'http:', $contact['poll']);
2280 $contact['confirm'] = str_replace('https:', 'http:', $contact['confirm']);
2281 $contact['poco'] = str_replace('https:', 'http:', $contact['poco']);
2284 if ((intval($new_policy) == BaseURL::SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'], 'http:')) {
2285 $ssl_changed = true;
2286 $contact['url'] = str_replace('http:', 'https:', $contact['url']);
2287 $contact['request'] = str_replace('http:', 'https:', $contact['request']);
2288 $contact['notify'] = str_replace('http:', 'https:', $contact['notify']);
2289 $contact['poll'] = str_replace('http:', 'https:', $contact['poll']);
2290 $contact['confirm'] = str_replace('http:', 'https:', $contact['confirm']);
2291 $contact['poco'] = str_replace('http:', 'https:', $contact['poco']);
2295 $fields = ['url' => $contact['url'], 'request' => $contact['request'],
2296 'notify' => $contact['notify'], 'poll' => $contact['poll'],
2297 'confirm' => $contact['confirm'], 'poco' => $contact['poco']];
2298 DBA::update('contact', $fields, ['id' => $contact['id']]);
2305 * @param array $importer Owner (local user) data
2306 * @param array $contact Existing owner-specific contact data we want to expand the relationship with. Optional.
2307 * @param array $datarray An item-like array with at least the 'author-id' and 'author-url' keys for the contact. Mandatory.
2308 * @param bool $sharing True: Contact is now sharing with Owner; False: Contact is now following Owner (default)
2309 * @param string $note Introduction additional message
2310 * @return bool|null True: follow request is accepted; False: relationship is rejected; Null: relationship is pending
2311 * @throws HTTPException\InternalServerErrorException
2312 * @throws \ImagickException
2314 public static function addRelationship(array $importer, array $contact, array $datarray, $sharing = false, $note = '')
2316 // Should always be set
2317 if (empty($datarray['author-id'])) {
2321 $fields = ['url', 'name', 'nick', 'avatar', 'photo', 'network', 'blocked'];
2322 $pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
2323 if (!DBA::isResult($pub_contact)) {
2324 // Should never happen
2328 // Contact is blocked at node-level
2329 if (self::isBlocked($datarray['author-id'])) {
2333 $url = ($datarray['author-link'] ?? '') ?: $pub_contact['url'];
2334 $name = $pub_contact['name'];
2335 $photo = ($pub_contact['avatar'] ?? '') ?: $pub_contact["photo"];
2336 $nick = $pub_contact['nick'];
2337 $network = $pub_contact['network'];
2339 // Ensure that we don't create a new contact when there already is one
2340 $cid = self::getIdForURL($url, $importer['uid']);
2342 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
2345 if (!empty($contact)) {
2346 if (!empty($contact['pending'])) {
2347 Logger::info('Pending contact request already exists.', ['url' => $url, 'uid' => $importer['uid']]);
2351 // Contact is blocked at user-level
2352 if (!empty($contact['id']) && !empty($importer['id']) &&
2353 Contact\User::isBlocked($contact['id'], $importer['id'])) {
2357 // Make sure that the existing contact isn't archived
2358 self::unmarkForArchival($contact);
2360 if (($contact['rel'] == self::SHARING)
2361 || ($sharing && $contact['rel'] == self::FOLLOWER)) {
2362 DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
2363 ['id' => $contact['id'], 'uid' => $importer['uid']]);
2366 // Ensure to always have the correct network type, independent from the connection request method
2367 self::updateFromProbe($contact['id']);
2371 // send email notification to owner?
2372 if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) {
2373 Logger::log('ignoring duplicated connection request from pending contact ' . $url);
2377 // create contact record
2378 DBA::insert('contact', [
2379 'uid' => $importer['uid'],
2380 'created' => DateTimeFormat::utcNow(),
2382 'nurl' => Strings::normaliseLink($url),
2385 'network' => $network,
2386 'rel' => self::FOLLOWER,
2393 $contact_id = DBA::lastInsertId();
2395 // Ensure to always have the correct network type, independent from the connection request method
2396 self::updateFromProbe($contact_id);
2398 self::updateAvatar($contact_id, $photo, true);
2400 $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
2402 /// @TODO Encapsulate this into a function/method
2403 $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language'];
2404 $user = DBA::selectFirst('user', $fields, ['uid' => $importer['uid']]);
2405 if (DBA::isResult($user) && !in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
2406 // create notification
2407 $hash = Strings::getRandomHex();
2409 if (is_array($contact_record)) {
2410 DBA::insert('intro', ['uid' => $importer['uid'], 'contact-id' => $contact_record['id'],
2411 'blocked' => false, 'knowyou' => false, 'note' => $note,
2412 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow()]);
2415 Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);
2417 if (($user['notify-flags'] & Type::INTRO) &&
2418 in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL])) {
2421 'type' => Type::INTRO,
2422 'notify_flags' => $user['notify-flags'],
2423 'language' => $user['language'],
2424 'to_name' => $user['username'],
2425 'to_email' => $user['email'],
2426 'uid' => $user['uid'],
2427 'link' => DI::baseUrl() . '/notifications/intros',
2428 'source_name' => ((strlen(stripslashes($contact_record['name']))) ? stripslashes($contact_record['name']) : DI::l10n()->t('[Name Withheld]')),
2429 'source_link' => $contact_record['url'],
2430 'source_photo' => $contact_record['photo'],
2431 'verb' => ($sharing ? Activity::FRIEND : Activity::FOLLOW),
2435 } elseif (DBA::isResult($user) && in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
2436 if (($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) && ($network != Protocol::DIASPORA)) {
2437 self::createFromProbe($importer, $url, false, $network);
2440 $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
2441 $fields = ['pending' => false];
2442 if ($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) {
2443 $fields['rel'] = Contact::FRIEND;
2446 DBA::update('contact', $fields, $condition);
2455 public static function removeFollower($importer, $contact, array $datarray = [], $item = "")
2457 if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) {
2458 DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]);
2460 Contact::remove($contact['id']);
2464 public static function removeSharer($importer, $contact, array $datarray = [], $item = "")
2466 if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::FOLLOWER)) {
2467 DBA::update('contact', ['rel' => self::FOLLOWER], ['id' => $contact['id']]);
2469 Contact::remove($contact['id']);
2474 * Create a birthday event.
2476 * Update the year and the birthday.
2478 public static function updateBirthdays()
2482 AND `bd` > "0001-01-01"
2483 AND SUBSTRING(`bd`, 1, 4) != `bdyear`
2484 AND (`contact`.`rel` = ? OR `contact`.`rel` = ?)
2485 AND NOT `contact`.`pending`
2486 AND NOT `contact`.`hidden`
2487 AND NOT `contact`.`blocked`
2488 AND NOT `contact`.`archive`
2489 AND NOT `contact`.`deleted`',
2494 $contacts = DBA::select('contact', ['id', 'uid', 'name', 'url', 'bd'], $condition);
2496 while ($contact = DBA::fetch($contacts)) {
2497 Logger::log('update_contact_birthday: ' . $contact['bd']);
2499 $nextbd = DateTimeFormat::utcNow('Y') . substr($contact['bd'], 4);
2501 if (Event::createBirthday($contact, $nextbd)) {
2505 ['bdyear' => substr($nextbd, 0, 4), 'bd' => $nextbd],
2506 ['id' => $contact['id']]
2510 DBA::close($contacts);
2514 * Remove the unavailable contact ids from the provided list
2516 * @param array $contact_ids Contact id list
2518 * @throws \Exception
2520 public static function pruneUnavailable(array $contact_ids)
2522 if (empty($contact_ids)) {
2526 $contacts = Contact::selectToArray(['id'], [
2527 'id' => $contact_ids,
2533 return array_column($contacts, 'id');
2537 * Returns a magic link to authenticate remote visitors
2539 * @todo check if the return is either a fully qualified URL or a relative path to Friendica basedir
2541 * @param string $contact_url The address of the target contact profile
2542 * @param string $url An url that we will be redirected to after the authentication
2544 * @return string with "redir" link
2545 * @throws HTTPException\InternalServerErrorException
2546 * @throws \ImagickException
2548 public static function magicLink($contact_url, $url = '')
2550 if (!Session::isAuthenticated()) {
2551 return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
2554 $contact = self::getByURL($contact_url, false);
2555 if (empty($contact)) {
2556 return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
2559 // Prevents endless loop in case only a non-public contact exists for the contact URL
2560 unset($contact['uid']);
2562 return self::magicLinkByContact($contact, $url ?: $contact_url);
2566 * Returns a magic link to authenticate remote visitors
2568 * @param integer $cid The contact id of the target contact profile
2569 * @param string $url An url that we will be redirected to after the authentication
2571 * @return string with "redir" link
2572 * @throws HTTPException\InternalServerErrorException
2573 * @throws \ImagickException
2575 public static function magicLinkbyId($cid, $url = '')
2577 $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
2579 return self::magicLinkByContact($contact, $url);
2583 * Returns a magic link to authenticate remote visitors
2585 * @param array $contact The contact array with "uid", "network" and "url"
2586 * @param string $url An url that we will be redirected to after the authentication
2588 * @return string with "redir" link
2589 * @throws HTTPException\InternalServerErrorException
2590 * @throws \ImagickException
2592 public static function magicLinkByContact($contact, $url = '')
2594 $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
2596 if (!Session::isAuthenticated()) {
2597 return $destination;
2600 // Only redirections to the same host do make sense
2601 if (($url != '') && (parse_url($url, PHP_URL_HOST) != parse_url($contact['url'], PHP_URL_HOST))) {
2605 if (DI::pConfig()->get(local_user(), 'system', 'stay_local') && ($url == '')) {
2606 return 'contact/' . $contact['id'] . '/conversations';
2609 if ($contact['network'] != Protocol::DFRN) {
2610 return $destination;
2613 if (!empty($contact['uid'])) {
2614 return self::magicLink($contact['url'], $url);
2617 if (empty($contact['id'])) {
2618 return $destination;
2621 $redirect = 'redir/' . $contact['id'];
2623 if (($url != '') && !Strings::compareLink($contact['url'], $url)) {
2624 $redirect .= '?url=' . $url;
2631 * Is the contact a forum?
2633 * @param integer $contactid ID of the contact
2635 * @return boolean "true" if it is a forum
2637 public static function isForum($contactid)
2639 $fields = ['forum', 'prv'];
2640 $condition = ['id' => $contactid];
2641 $contact = DBA::selectFirst('contact', $fields, $condition);
2642 if (!DBA::isResult($contact)) {
2647 return ($contact['forum'] || $contact['prv']);
2651 * Can the remote contact receive private messages?
2653 * @param array $contact
2656 public static function canReceivePrivateMessages(array $contact)
2658 $protocol = $contact['network'] ?? $contact['protocol'] ?? Protocol::PHANTOM;
2659 $self = $contact['self'] ?? false;
2661 return in_array($protocol, [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB]) && !$self;
2665 * Search contact table by nick or name
2667 * @param string $search Name or nick
2668 * @param string $mode Search mode (e.g. "community")
2670 * @return array with search results
2671 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2673 public static function searchByName($search, $mode = '')
2675 if (empty($search)) {
2679 // check supported networks
2680 if (DI::config()->get('system', 'diaspora_enabled')) {
2681 $diaspora = Protocol::DIASPORA;
2683 $diaspora = Protocol::DFRN;
2686 if (!DI::config()->get('system', 'ostatus_disabled')) {
2687 $ostatus = Protocol::OSTATUS;
2689 $ostatus = Protocol::DFRN;
2692 // check if we search only communities or every contact
2693 if ($mode === 'community') {
2694 $extra_sql = sprintf(' AND `contact-type` = %d', Contact::TYPE_COMMUNITY);
2701 $results = DBA::p("SELECT * FROM `contact`
2702 WHERE NOT `unsearchable` AND `network` IN (?, ?, ?, ?) AND
2703 NOT `failed` AND `uid` = ? AND
2704 (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?) $extra_sql
2705 ORDER BY `nurl` DESC LIMIT 1000",
2706 Protocol::DFRN, Protocol::ACTIVITYPUB, $ostatus, $diaspora, 0, $search, $search, $search
2709 $contacts = DBA::toArray($results);
2714 * Add public contacts from an array
2716 * @param array $urls
2717 * @return array result "count", "added" and "updated"
2719 public static function addByUrls(array $urls)
2726 foreach ($urls as $url) {
2727 $contact = Contact::getByURL($url, false, ['id', 'updated']);
2728 if (empty($contact['id'])) {
2729 Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
2731 } elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {
2732 Worker::add(PRIORITY_LOW, 'UpdateContact', $contact['id']);
2740 return ['count' => $count, 'added' => $added, 'updated' => $updated, 'unchanged' => $unchanged];
2744 * Returns a random, global contact of the current node
2746 * @return string The profile URL
2749 public static function getRandomUrl()
2751 $r = DBA::selectFirst('contact', ['url'], [
2752 "`uid` = ? AND `network` = ? AND NOT `failed` AND `last-item` > ?",
2753 0, Protocol::DFRN, DateTimeFormat::utc('now - 1 month'),
2754 ], ['order' => ['RAND()']]);
2756 if (DBA::isResult($r)) {