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\Core\Hook;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Core\Session;
30 use Friendica\Core\System;
31 use Friendica\Core\Worker;
32 use Friendica\Database\DBA;
34 use Friendica\Model\Notify\Type;
35 use Friendica\Network\HTTPException;
36 use Friendica\Network\Probe;
37 use Friendica\Protocol\Activity;
38 use Friendica\Protocol\ActivityPub;
39 use Friendica\Protocol\DFRN;
40 use Friendica\Protocol\Diaspora;
41 use Friendica\Protocol\OStatus;
42 use Friendica\Protocol\Salmon;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Images;
45 use Friendica\Util\Network;
46 use Friendica\Util\Strings;
49 * functions for interacting with a contact
54 * @deprecated since version 2019.03
55 * @see User::PAGE_FLAGS_NORMAL
57 const PAGE_NORMAL = User::PAGE_FLAGS_NORMAL;
59 * @deprecated since version 2019.03
60 * @see User::PAGE_FLAGS_SOAPBOX
62 const PAGE_SOAPBOX = User::PAGE_FLAGS_SOAPBOX;
64 * @deprecated since version 2019.03
65 * @see User::PAGE_FLAGS_COMMUNITY
67 const PAGE_COMMUNITY = User::PAGE_FLAGS_COMMUNITY;
69 * @deprecated since version 2019.03
70 * @see User::PAGE_FLAGS_FREELOVE
72 const PAGE_FREELOVE = User::PAGE_FLAGS_FREELOVE;
74 * @deprecated since version 2019.03
75 * @see User::PAGE_FLAGS_BLOG
77 const PAGE_BLOG = User::PAGE_FLAGS_BLOG;
79 * @deprecated since version 2019.03
80 * @see User::PAGE_FLAGS_PRVGROUP
82 const PAGE_PRVGROUP = User::PAGE_FLAGS_PRVGROUP;
90 * TYPE_UNKNOWN - the account has been imported from gcontact where this is the default type value
92 * TYPE_PERSON - the account belongs to a person
93 * Associated page types: PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE
95 * TYPE_ORGANISATION - the account belongs to an organisation
96 * Associated page type: PAGE_SOAPBOX
98 * TYPE_NEWS - the account is a news reflector
99 * Associated page type: PAGE_SOAPBOX
101 * TYPE_COMMUNITY - the account is community forum
102 * Associated page types: PAGE_COMMUNITY, PAGE_PRVGROUP
104 * TYPE_RELAY - the account is a relay
105 * This will only be assigned to contacts, not to user accounts
108 const TYPE_UNKNOWN = -1;
109 const TYPE_PERSON = User::ACCOUNT_TYPE_PERSON;
110 const TYPE_ORGANISATION = User::ACCOUNT_TYPE_ORGANISATION;
111 const TYPE_NEWS = User::ACCOUNT_TYPE_NEWS;
112 const TYPE_COMMUNITY = User::ACCOUNT_TYPE_COMMUNITY;
113 const TYPE_RELAY = User::ACCOUNT_TYPE_RELAY;
132 * @param array $fields Array of selected fields, empty for all
133 * @param array $condition Array of fields for condition
134 * @param array $params Array of several parameters
138 public static function selectToArray(array $fields = [], array $condition = [], array $params = [])
140 return DBA::selectToArray('contact', $fields, $condition, $params);
144 * @param array $fields Array of selected fields, empty for all
145 * @param array $condition Array of fields for condition
146 * @param array $params Array of several parameters
150 public static function selectFirst(array $fields = [], array $condition = [], array $params = [])
152 $contact = DBA::selectFirst('contact', $fields, $condition, $params);
158 * Insert a row into the contact table
159 * Important: You can't use DBA::lastInsertId() after this call since it will be set to 0.
161 * @param array $fields field array
162 * @param bool $on_duplicate_update Do an update on a duplicate entry
164 * @return boolean was the insert successful?
167 public static function insert(array $fields, bool $on_duplicate_update = false)
169 $ret = DBA::insert('contact', $fields, $on_duplicate_update);
170 $contact = DBA::selectFirst('contact', ['nurl', 'uid'], ['id' => DBA::lastInsertId()]);
171 if (!DBA::isResult($contact)) {
176 // Search for duplicated contacts and get rid of them
177 self::removeDuplicates($contact['nurl'], $contact['uid']);
183 * @param integer $id Contact ID
184 * @param array $fields Array of selected fields, empty for all
185 * @return array|boolean Contact record if it exists, false otherwise
188 public static function getById($id, $fields = [])
190 return DBA::selectFirst('contact', $fields, ['id' => $id]);
194 * Fetches a contact by a given url
196 * @param string $url profile url
197 * @param integer $uid User ID of the contact
198 * @param array $fields Field list
199 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
200 * @return array contact array
202 public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0)
204 if ($update || is_null($update)) {
205 $cid = self::getIdForURL($url, $uid, !($update ?? false));
209 return self::getById($cid, $fields);
212 // We first try the nurl (http://server.tld/nick), most common case
213 $options = ['order' => ['id']];
214 $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options);
216 // Then the addr (nick@server.tld)
217 if (!DBA::isResult($contact)) {
218 $contact = DBA::selectFirst('contact', $fields, ['addr' => str_replace('acct:', '', $url), 'uid' => $uid, 'deleted' => false], $options);
221 // Then the alias (which could be anything)
222 if (!DBA::isResult($contact)) {
223 // The link could be provided as http although we stored it as https
224 $ssl_url = str_replace('http://', 'https://', $url);
225 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid];
226 $contact = DBA::selectFirst('contact', $fields, $condition, $options);
232 * Fetches a contact for a given user by a given url.
233 * In difference to "getByURL" the function will fetch a public contact when no user contact had been found.
235 * @param string $url profile url
236 * @param integer $uid User ID of the contact
237 * @param array $fields Field list
238 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
239 * @return array contact array
241 public static function getByURLForUser(string $url, int $uid = 0, $update = null, array $fields = [])
244 $contact = self::getByURL($url, $update, $fields, $uid);
245 if (!empty($contact)) {
246 if (!empty($contact['id'])) {
247 $contact['cid'] = $contact['id'];
254 $contact = self::getByURL($url, $update, $fields);
255 if (!empty($contact['id'])) {
257 $contact['zid'] = $contact['id'];
263 * Tests if the given contact is a follower
265 * @param int $cid Either public contact id or user's contact id
266 * @param int $uid User ID
268 * @return boolean is the contact id a follower?
269 * @throws HTTPException\InternalServerErrorException
270 * @throws \ImagickException
272 public static function isFollower($cid, $uid)
274 if (self::isBlockedByUser($cid, $uid)) {
278 $cdata = self::getPublicAndUserContacID($cid, $uid);
279 if (empty($cdata['user'])) {
283 $condition = ['id' => $cdata['user'], 'rel' => [self::FOLLOWER, self::FRIEND]];
284 return DBA::exists('contact', $condition);
288 * Tests if the given contact url is a follower
290 * @param string $url Contact URL
291 * @param int $uid User ID
293 * @return boolean is the contact id a follower?
294 * @throws HTTPException\InternalServerErrorException
295 * @throws \ImagickException
297 public static function isFollowerByURL($url, $uid)
299 $cid = self::getIdForURL($url, $uid, true);
305 return self::isFollower($cid, $uid);
309 * Tests if the given user follow the given contact
311 * @param int $cid Either public contact id or user's contact id
312 * @param int $uid User ID
314 * @return boolean is the contact url being followed?
315 * @throws HTTPException\InternalServerErrorException
316 * @throws \ImagickException
318 public static function isSharing($cid, $uid)
320 if (self::isBlockedByUser($cid, $uid)) {
324 $cdata = self::getPublicAndUserContacID($cid, $uid);
325 if (empty($cdata['user'])) {
329 $condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]];
330 return DBA::exists('contact', $condition);
334 * Tests if the given user follow the given contact url
336 * @param string $url Contact URL
337 * @param int $uid User ID
339 * @return boolean is the contact url being followed?
340 * @throws HTTPException\InternalServerErrorException
341 * @throws \ImagickException
343 public static function isSharingByURL($url, $uid)
345 $cid = self::getIdForURL($url, $uid, true);
351 return self::isSharing($cid, $uid);
355 * Get the basepath for a given contact link
357 * @param string $url The contact link
358 * @param boolean $dont_update Don't update the contact
360 * @return string basepath
361 * @throws HTTPException\InternalServerErrorException
362 * @throws \ImagickException
364 public static function getBasepath($url, $dont_update = false)
366 $contact = DBA::selectFirst('contact', ['id', 'baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]);
367 if (!DBA::isResult($contact)) {
371 if (!empty($contact['baseurl'])) {
372 return $contact['baseurl'];
373 } elseif ($dont_update) {
377 // Update the existing contact
378 self::updateFromProbe($contact['id'], '', true);
380 // And fetch the result
381 $contact = DBA::selectFirst('contact', ['baseurl'], ['id' => $contact['id']]);
382 if (empty($contact['baseurl'])) {
383 Logger::info('No baseurl for contact', ['url' => $url]);
387 Logger::info('Found baseurl for contact', ['url' => $url, 'baseurl' => $contact['baseurl']]);
388 return $contact['baseurl'];
392 * Check if the given contact url is on the same server
394 * @param string $url The contact link
396 * @return boolean Is it the same server?
398 public static function isLocal($url)
400 return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl());
404 * Check if the given contact ID is on the same server
406 * @param string $url The contact link
408 * @return boolean Is it the same server?
410 public static function isLocalById(int $cid)
412 $contact = DBA::selectFirst('contact', ['url', 'baseurl'], ['id' => $cid]);
413 if (!DBA::isResult($contact)) {
417 if (empty($contact['baseurl'])) {
418 $baseurl = self::getBasepath($contact['url'], true);
420 $baseurl = $contact['baseurl'];
423 return Strings::compareLink($baseurl, DI::baseUrl());
427 * Returns the public contact id of the given user id
429 * @param integer $uid User ID
431 * @return integer|boolean Public contact id for given user id
434 public static function getPublicIdByUserId($uid)
436 $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
437 if (!DBA::isResult($self)) {
440 return self::getIdForURL($self['url'], 0, true);
444 * Returns the contact id for the user and the public contact id for a given contact id
446 * @param int $cid Either public contact id or user's contact id
447 * @param int $uid User ID
449 * @return array with public and user's contact id
450 * @throws HTTPException\InternalServerErrorException
451 * @throws \ImagickException
453 public static function getPublicAndUserContacID($cid, $uid)
455 if (empty($uid) || empty($cid)) {
459 $contact = DBA::selectFirst('contact', ['id', 'uid', 'url'], ['id' => $cid]);
460 if (!DBA::isResult($contact)) {
464 // We quit when the user id don't match the user id of the provided contact
465 if (($contact['uid'] != $uid) && ($contact['uid'] != 0)) {
469 if ($contact['uid'] != 0) {
470 $pcid = Contact::getIdForURL($contact['url'], 0, true, ['url' => $contact['url']]);
474 $ucid = $contact['id'];
476 $pcid = $contact['id'];
477 $ucid = Contact::getIdForURL($contact['url'], $uid, true);
480 return ['public' => $pcid, 'user' => $ucid];
484 * Returns contact details for a given contact id in combination with a user id
486 * @param int $cid A contact ID
487 * @param int $uid The User ID
488 * @param array $fields The selected fields for the contact
490 * @return array The contact details
494 public static function getContactForUser($cid, $uid, array $fields = [])
496 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => $uid]);
498 if (!DBA::isResult($contact)) {
506 * Block contact id for user id
508 * @param int $cid Either public contact id or user's contact id
509 * @param int $uid User ID
510 * @param boolean $blocked Is the contact blocked or unblocked?
513 public static function setBlockedForUser($cid, $uid, $blocked)
515 $cdata = self::getPublicAndUserContacID($cid, $uid);
520 if ($cdata['user'] != 0) {
521 DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
524 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
528 * Returns "block" state for contact id and user id
530 * @param int $cid Either public contact id or user's contact id
531 * @param int $uid User ID
533 * @return boolean is the contact id blocked for the given user?
536 public static function isBlockedByUser($cid, $uid)
538 $cdata = self::getPublicAndUserContacID($cid, $uid);
543 $public_blocked = false;
545 if (!empty($cdata['public'])) {
546 $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
547 if (DBA::isResult($public_contact)) {
548 $public_blocked = $public_contact['blocked'];
552 $user_blocked = $public_blocked;
554 if (!empty($cdata['user'])) {
555 $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
556 if (DBA::isResult($user_contact)) {
557 $user_blocked = $user_contact['blocked'];
561 if ($user_blocked != $public_blocked) {
562 DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
565 return $user_blocked;
569 * Ignore contact id for user id
571 * @param int $cid Either public contact id or user's contact id
572 * @param int $uid User ID
573 * @param boolean $ignored Is the contact ignored or unignored?
576 public static function setIgnoredForUser($cid, $uid, $ignored)
578 $cdata = self::getPublicAndUserContacID($cid, $uid);
583 if ($cdata['user'] != 0) {
584 DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
587 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
591 * Returns "ignore" state for contact id and user id
593 * @param int $cid Either public contact id or user's contact id
594 * @param int $uid User ID
596 * @return boolean is the contact id ignored for the given user?
599 public static function isIgnoredByUser($cid, $uid)
601 $cdata = self::getPublicAndUserContacID($cid, $uid);
606 $public_ignored = false;
608 if (!empty($cdata['public'])) {
609 $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
610 if (DBA::isResult($public_contact)) {
611 $public_ignored = $public_contact['ignored'];
615 $user_ignored = $public_ignored;
617 if (!empty($cdata['user'])) {
618 $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
619 if (DBA::isResult($user_contact)) {
620 $user_ignored = $user_contact['readonly'];
624 if ($user_ignored != $public_ignored) {
625 DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
628 return $user_ignored;
632 * Set "collapsed" for contact id and user id
634 * @param int $cid Either public contact id or user's contact id
635 * @param int $uid User ID
636 * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
639 public static function setCollapsedForUser($cid, $uid, $collapsed)
641 $cdata = self::getPublicAndUserContacID($cid, $uid);
646 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
650 * Returns "collapsed" state for contact id and user id
652 * @param int $cid Either public contact id or user's contact id
653 * @param int $uid User ID
655 * @return boolean is the contact id blocked for the given user?
656 * @throws HTTPException\InternalServerErrorException
657 * @throws \ImagickException
659 public static function isCollapsedByUser($cid, $uid)
661 $cdata = self::getPublicAndUserContacID($cid, $uid);
668 if (!empty($cdata['public'])) {
669 $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
670 if (DBA::isResult($public_contact)) {
671 $collapsed = $public_contact['collapsed'];
679 * Returns a list of contacts belonging in a group
685 public static function getByGroupId($gid)
690 $stmt = DBA::p('SELECT `group_member`.`contact-id`, `contact`.*
692 INNER JOIN `group_member`
693 ON `contact`.`id` = `group_member`.`contact-id`
695 AND `contact`.`uid` = ?
696 AND NOT `contact`.`self`
697 AND NOT `contact`.`deleted`
698 AND NOT `contact`.`blocked`
699 AND NOT `contact`.`pending`
700 ORDER BY `contact`.`name` ASC',
705 if (DBA::isResult($stmt)) {
706 $return = DBA::toArray($stmt);
714 * Creates the self-contact for the provided user id
717 * @return bool Operation success
718 * @throws HTTPException\InternalServerErrorException
720 public static function createSelfFromUserId($uid)
722 // Only create the entry if it doesn't exist yet
723 if (DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
727 $user = DBA::selectFirst('user', ['uid', 'username', 'nickname'], ['uid' => $uid]);
728 if (!DBA::isResult($user)) {
732 $return = DBA::insert('contact', [
733 'uid' => $user['uid'],
734 'created' => DateTimeFormat::utcNow(),
736 'name' => $user['username'],
737 'nick' => $user['nickname'],
738 'photo' => DI::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
739 'thumb' => DI::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
740 'micro' => DI::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg',
743 'url' => DI::baseUrl() . '/profile/' . $user['nickname'],
744 'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname']),
745 'addr' => $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3),
746 'request' => DI::baseUrl() . '/dfrn_request/' . $user['nickname'],
747 'notify' => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'],
748 'poll' => DI::baseUrl() . '/dfrn_poll/' . $user['nickname'],
749 'confirm' => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
750 'poco' => DI::baseUrl() . '/poco/' . $user['nickname'],
751 'name-date' => DateTimeFormat::utcNow(),
752 'uri-date' => DateTimeFormat::utcNow(),
753 'avatar-date' => DateTimeFormat::utcNow(),
761 * Updates the self-contact for the provided user id
764 * @param boolean $update_avatar Force the avatar update
765 * @throws HTTPException\InternalServerErrorException
767 public static function updateSelfFromUserID($uid, $update_avatar = false)
769 $fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar',
770 'xmpp', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
771 'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco'];
772 $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
773 if (!DBA::isResult($self)) {
777 $fields = ['nickname', 'page-flags', 'account-type'];
778 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
779 if (!DBA::isResult($user)) {
783 $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
784 'country-name', 'pub_keywords', 'xmpp', 'net-publish'];
785 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
786 if (!DBA::isResult($profile)) {
790 $file_suffix = 'jpg';
792 $fields = ['name' => $profile['name'], 'nick' => $user['nickname'],
793 'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile),
794 'about' => $profile['about'], 'keywords' => $profile['pub_keywords'],
795 'contact-type' => $user['account-type'],
796 'xmpp' => $profile['xmpp']];
798 $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
799 if (DBA::isResult($avatar)) {
800 if ($update_avatar) {
801 $fields['avatar-date'] = DateTimeFormat::utcNow();
804 // Creating the path to the avatar, beginning with the file suffix
805 $types = Images::supportedTypes();
806 if (isset($types[$avatar['type']])) {
807 $file_suffix = $types[$avatar['type']];
810 // We are adding a timestamp value so that other systems won't use cached content
811 $timestamp = strtotime($fields['avatar-date']);
813 $prefix = DI::baseUrl() . '/photo/' .$avatar['resource-id'] . '-';
814 $suffix = '.' . $file_suffix . '?ts=' . $timestamp;
816 $fields['photo'] = $prefix . '4' . $suffix;
817 $fields['thumb'] = $prefix . '5' . $suffix;
818 $fields['micro'] = $prefix . '6' . $suffix;
820 // We hadn't found a photo entry, so we use the default avatar
821 $fields['photo'] = DI::baseUrl() . '/images/person-300.jpg';
822 $fields['thumb'] = DI::baseUrl() . '/images/person-80.jpg';
823 $fields['micro'] = DI::baseUrl() . '/images/person-48.jpg';
826 $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
827 $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
828 $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
829 $fields['unsearchable'] = !$profile['net-publish'];
831 // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
832 $fields['url'] = DI::baseUrl() . '/profile/' . $user['nickname'];
833 $fields['nurl'] = Strings::normaliseLink($fields['url']);
834 $fields['addr'] = $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
835 $fields['request'] = DI::baseUrl() . '/dfrn_request/' . $user['nickname'];
836 $fields['notify'] = DI::baseUrl() . '/dfrn_notify/' . $user['nickname'];
837 $fields['poll'] = DI::baseUrl() . '/dfrn_poll/'. $user['nickname'];
838 $fields['confirm'] = DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'];
839 $fields['poco'] = DI::baseUrl() . '/poco/' . $user['nickname'];
843 foreach ($fields as $field => $content) {
844 if ($self[$field] != $content) {
850 if ($fields['name'] != $self['name']) {
851 $fields['name-date'] = DateTimeFormat::utcNow();
853 $fields['updated'] = DateTimeFormat::utcNow();
854 DBA::update('contact', $fields, ['id' => $self['id']]);
856 // Update the public contact as well
857 DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
859 // Update the profile
860 $fields = ['photo' => DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix,
861 'thumb' => DI::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix];
862 DBA::update('profile', $fields, ['uid' => $uid]);
867 * Marks a contact for removal
869 * @param int $id contact id
871 * @throws HTTPException\InternalServerErrorException
873 public static function remove($id)
875 // We want just to make sure that we don't delete our "self" contact
876 $contact = DBA::selectFirst('contact', ['uid'], ['id' => $id, 'self' => false]);
877 if (!DBA::isResult($contact) || !intval($contact['uid'])) {
881 // Archive the contact
882 DBA::update('contact', ['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
884 // Delete it in the background
885 Worker::add(PRIORITY_MEDIUM, 'RemoveContact', $id);
889 * Sends an unfriend message. Does not remove the contact
891 * @param array $user User unfriending
892 * @param array $contact Contact unfriended
893 * @param boolean $dissolve Remove the contact on the remote side
895 * @throws HTTPException\InternalServerErrorException
896 * @throws \ImagickException
898 public static function terminateFriendship(array $user, array $contact, $dissolve = false)
900 if (empty($contact['network'])) {
904 $protocol = $contact['network'];
905 if (($protocol == Protocol::DFRN) && !self::isLegacyDFRNContact($contact)) {
906 $protocol = Protocol::ACTIVITYPUB;
909 if (($protocol == Protocol::DFRN) && $dissolve) {
910 DFRN::deliver($user, $contact, 'placeholder', true);
911 } elseif (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
912 // create an unfollow slap
914 $item['verb'] = Activity::O_UNFOLLOW;
915 $item['gravity'] = GRAVITY_ACTIVITY;
916 $item['follow'] = $contact["url"];
921 $item['attach'] = '';
922 $slap = OStatus::salmon($item, $user);
924 if (!empty($contact['notify'])) {
925 Salmon::slapper($user, $contact['notify'], $slap);
927 } elseif ($protocol == Protocol::DIASPORA) {
928 Diaspora::sendUnshare($user, $contact);
929 } elseif ($protocol == Protocol::ACTIVITYPUB) {
930 ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
933 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
939 * Marks a contact for archival after a communication issue delay
941 * Contact has refused to recognise us as a friend. We will start a countdown.
942 * If they still don't recognise us in 32 days, the relationship is over,
943 * and we won't waste any more time trying to communicate with them.
944 * This provides for the possibility that their database is temporarily messed
945 * up or some other transient event and that there's a possibility we could recover from it.
947 * @param array $contact contact to mark for archival
949 * @throws HTTPException\InternalServerErrorException
951 public static function markForArchival(array $contact)
953 if (!isset($contact['url']) && !empty($contact['id'])) {
954 $fields = ['id', 'url', 'archive', 'self', 'term-date'];
955 $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
956 if (!DBA::isResult($contact)) {
959 } elseif (!isset($contact['url'])) {
960 Logger::info('Empty contact', ['contact' => $contact, 'callstack' => System::callstack(20)]);
963 Logger::info('Contact is marked for archival', ['id' => $contact['id']]);
965 // Contact already archived or "self" contact? => nothing to do
966 if ($contact['archive'] || $contact['self']) {
970 if ($contact['term-date'] <= DBA::NULL_DATETIME) {
971 DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
972 DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
975 * We really should send a notification to the owner after 2-3 weeks
976 * so they won't be surprised when the contact vanishes and can take
977 * remedial action if this was a serious mistake or glitch
980 /// @todo Check for contact vitality via probing
981 $archival_days = DI::config()->get('system', 'archival_days', 32);
983 $expiry = $contact['term-date'] . ' + ' . $archival_days . ' days ';
984 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($expiry)) {
985 /* Relationship is really truly dead. archive them rather than
986 * delete, though if the owner tries to unarchive them we'll start
987 * the whole process over again.
989 DBA::update('contact', ['archive' => true], ['id' => $contact['id']]);
990 DBA::update('contact', ['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
991 GContact::updateFromPublicContactURL($contact['url']);
997 * Cancels the archival countdown
999 * @see Contact::markForArchival()
1001 * @param array $contact contact to be unmarked for archival
1003 * @throws \Exception
1005 public static function unmarkForArchival(array $contact)
1007 // Always unarchive the relay contact entry
1008 if (!empty($contact['batch']) && !empty($contact['term-date']) && ($contact['term-date'] > DBA::NULL_DATETIME)) {
1009 $fields = ['term-date' => DBA::NULL_DATETIME, 'archive' => false];
1010 $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
1011 DBA::update('contact', $fields, $condition);
1014 $condition = ['`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], DBA::NULL_DATETIME];
1015 $exists = DBA::exists('contact', $condition);
1017 // We don't need to update, we never marked this contact for archival
1022 Logger::info('Contact is marked as vital again', ['id' => $contact['id']]);
1024 if (!isset($contact['url']) && !empty($contact['id'])) {
1025 $fields = ['id', 'url', 'batch'];
1026 $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
1027 if (!DBA::isResult($contact)) {
1032 // It's a miracle. Our dead contact has inexplicably come back to life.
1033 $fields = ['term-date' => DBA::NULL_DATETIME, 'archive' => false];
1034 DBA::update('contact', $fields, ['id' => $contact['id']]);
1035 DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
1036 GContact::updateFromPublicContactURL($contact['url']);
1040 * Returns the data array for the photo menu of a given contact
1042 * @param array $contact contact
1043 * @param int $uid optional, default 0
1045 * @throws HTTPException\InternalServerErrorException
1046 * @throws \ImagickException
1048 public static function photoMenu(array $contact, $uid = 0)
1053 $contact_drop_link = '';
1057 $uid = local_user();
1060 if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
1062 $profile_link = self::magicLink($contact['url']);
1063 $menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
1068 // Look for our own contact if the uid doesn't match and isn't public
1069 $contact_own = DBA::selectFirst('contact', [], ['nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid]);
1070 if (DBA::isResult($contact_own)) {
1071 return self::photoMenu($contact_own, $uid);
1076 if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
1078 $profile_link = DI::baseUrl() . '/redir/' . $contact['id'];
1080 $profile_link = $contact['url'];
1083 if ($profile_link === 'mailbox') {
1088 $status_link = $profile_link . '/status';
1089 $photos_link = str_replace('/profile/', '/photos/', $profile_link);
1090 $profile_link = $profile_link . '/profile';
1093 if (self::canReceivePrivateMessages($contact) && empty($contact['pending'])) {
1094 $pm_url = DI::baseUrl() . '/message/new/' . $contact['id'];
1097 if (($contact['network'] == Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
1098 $poke_link = 'contact/' . $contact['id'] . '/poke';
1101 $contact_url = DI::baseUrl() . '/contact/' . $contact['id'];
1103 $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations';
1105 if (!$contact['self']) {
1106 $contact_drop_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/drop?confirm=1';
1110 $unfollow_link = '';
1111 if (!$contact['self'] && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
1112 if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
1113 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']);
1114 } elseif(!$contact['pending']) {
1115 $follow_link = 'follow?url=' . urlencode($contact['url']);
1119 if (!empty($follow_link) || !empty($unfollow_link)) {
1120 $contact_drop_link = '';
1125 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
1127 if (empty($contact['uid'])) {
1129 'profile' => [DI::l10n()->t('View Profile') , $profile_link , true],
1130 'network' => [DI::l10n()->t('Network Posts') , $posts_link , false],
1131 'edit' => [DI::l10n()->t('View Contact') , $contact_url , false],
1132 'follow' => [DI::l10n()->t('Connect/Follow'), $follow_link , true],
1133 'unfollow'=> [DI::l10n()->t('UnFollow') , $unfollow_link, true],
1137 'status' => [DI::l10n()->t('View Status') , $status_link , true],
1138 'profile' => [DI::l10n()->t('View Profile') , $profile_link , true],
1139 'photos' => [DI::l10n()->t('View Photos') , $photos_link , true],
1140 'network' => [DI::l10n()->t('Network Posts') , $posts_link , false],
1141 'edit' => [DI::l10n()->t('View Contact') , $contact_url , false],
1142 'drop' => [DI::l10n()->t('Drop Contact') , $contact_drop_link, false],
1143 'pm' => [DI::l10n()->t('Send PM') , $pm_url , false],
1144 'poke' => [DI::l10n()->t('Poke') , $poke_link , false],
1145 'follow' => [DI::l10n()->t('Connect/Follow'), $follow_link , true],
1146 'unfollow'=> [DI::l10n()->t('UnFollow') , $unfollow_link , true],
1149 if (!empty($contact['pending'])) {
1150 $intro = DBA::selectFirst('intro', ['id'], ['contact-id' => $contact['id']]);
1151 if (DBA::isResult($intro)) {
1152 $menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro['id'], true];
1157 $args = ['contact' => $contact, 'menu' => &$menu];
1159 Hook::callAll('contact_photo_menu', $args);
1161 $menucondensed = [];
1163 foreach ($menu as $menuname => $menuitem) {
1164 if ($menuitem[1] != '') {
1165 $menucondensed[$menuname] = $menuitem;
1169 return $menucondensed;
1173 * Returns ungrouped contact count or list for user
1175 * Returns either the total number of ungrouped contacts for the given user
1176 * id or a paginated list of ungrouped contacts.
1178 * @param int $uid uid
1180 * @throws \Exception
1182 public static function getUngroupedList($uid)
1192 SELECT DISTINCT(`contact-id`)
1194 INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
1195 WHERE `group`.`uid` = %d
1196 )", intval($uid), intval($uid));
1200 * Have a look at all contact tables for a given profile url.
1201 * This function works as a replacement for probing the contact.
1203 * @param string $url Contact URL
1204 * @param integer $cid Contact ID
1206 * @return array Contact array in the "probe" structure
1208 private static function getProbeDataFromDatabase($url, $cid = null)
1210 // The link could be provided as http although we stored it as https
1211 $ssl_url = str_replace('http://', 'https://', $url);
1213 $fields = ['id', 'uid', 'url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
1214 'photo', 'keywords', 'location', 'about', 'network',
1215 'priority', 'batch', 'request', 'confirm', 'poco'];
1218 $data = DBA::selectFirst('contact', $fields, ['id' => $cid]);
1219 if (DBA::isResult($data)) {
1224 $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]);
1226 if (!DBA::isResult($data)) {
1227 $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
1228 $data = DBA::selectFirst('contact', $fields, $condition);
1231 if (DBA::isResult($data)) {
1232 // For security reasons we don't fetch key data from our users
1233 $data["pubkey"] = '';
1237 $fields = ['url', 'addr', 'alias', 'notify', 'name', 'nick',
1238 'photo', 'keywords', 'location', 'about', 'network'];
1239 $data = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]);
1241 if (!DBA::isResult($data)) {
1242 $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
1243 $data = DBA::selectFirst('contact', $fields, $condition);
1246 if (DBA::isResult($data)) {
1247 $data["pubkey"] = '';
1249 $data["priority"] = 0;
1250 $data["batch"] = '';
1251 $data["request"] = '';
1252 $data["confirm"] = '';
1257 $data = ActivityPub::probeProfile($url, false);
1258 if (!empty($data)) {
1262 $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
1263 'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
1264 $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]);
1266 if (!DBA::isResult($data)) {
1267 $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
1268 $data = DBA::selectFirst('contact', $fields, $condition);
1271 if (DBA::isResult($data)) {
1272 $data["pubkey"] = '';
1273 $data["keywords"] = '';
1274 $data["location"] = '';
1275 $data["about"] = '';
1284 * Fetch the contact id for a given URL and user
1286 * First lookup in the contact table to find a record matching either `url`, `nurl`,
1287 * `addr` or `alias`.
1289 * If there's no record and we aren't looking for a public contact, we quit.
1290 * If there's one, we check that it isn't time to update the picture else we
1291 * directly return the found contact id.
1293 * Second, we probe the provided $url whether it's http://server.tld/profile or
1294 * nick@server.tld. We quit if we can't get any info back.
1296 * Third, we create the contact record if it doesn't exist
1298 * Fourth, we update the existing record with the new data (avatar, alias, nick)
1299 * if there's any updates
1301 * @param string $url Contact URL
1302 * @param integer $uid The user id for the contact (0 = public contact)
1303 * @param boolean $no_update Don't update the contact
1304 * @param array $default Default value for creating the contact when every else fails
1305 * @param boolean $in_loop Internally used variable to prevent an endless loop
1307 * @return integer Contact ID
1308 * @throws HTTPException\InternalServerErrorException
1309 * @throws \ImagickException
1311 public static function getIdForURL($url, $uid = 0, $no_update = false, $default = [], $in_loop = false)
1313 Logger::info('Get contact data', ['url' => $url, 'user' => $uid]);
1321 $contact = self::getByURL($url, false, ['id', 'avatar', 'updated', 'network'], $uid);
1323 if (!empty($contact)) {
1324 $contact_id = $contact["id"];
1325 $update_contact = false;
1327 // Update the contact every 7 days (Don't update mail or feed contacts)
1328 if (in_array($contact['network'], Protocol::FEDERATED)) {
1329 $update_contact = ($contact['updated'] < DateTimeFormat::utc('now -7 days'));
1331 // We force the update if the avatar is empty
1332 if (empty($contact['avatar'])) {
1333 $update_contact = true;
1335 } elseif (empty($default) && in_array($contact['network'], [Protocol::MAIL, Protocol::PHANTOM]) && ($uid == 0)) {
1336 // Update public mail accounts via their user's accounts
1337 $fields = ['network', 'addr', 'name', 'nick', 'avatar', 'photo', 'thumb', 'micro'];
1338 $mailcontact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]);
1339 if (!DBA::isResult($mailcontact)) {
1340 $mailcontact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]);
1343 if (DBA::isResult($mailcontact)) {
1344 DBA::update('contact', $mailcontact, ['id' => $contact_id]);
1348 // Update the contact in the background if needed but it is called by the frontend
1349 if ($update_contact && $no_update && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
1350 Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id, ($uid == 0 ? 'force' : ''));
1353 if (!$update_contact || $no_update) {
1356 } elseif ($uid != 0) {
1357 // Non-existing user-specific contact, exiting
1361 if ($no_update && empty($default)) {
1362 // When we don't want to update, we look if we know this contact in any way
1363 $data = self::getProbeDataFromDatabase($url, $contact_id);
1364 $background_update = true;
1365 } elseif ($no_update && !empty($default['network'])) {
1366 // If there are default values, take these
1368 $background_update = false;
1371 $background_update = false;
1375 $data = Probe::uri($url, "", $uid);
1378 // Take the default values when probing failed
1379 if (!empty($default) && !in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
1380 $data = array_merge($data, $default);
1383 if (empty($data) || ($data['network'] == Protocol::PHANTOM)) {
1384 Logger::info('No valid network found', ['url' => $url, 'data' => $data, 'callstack' => System::callstack(20)]);
1388 if (!empty($data['baseurl'])) {
1389 $data['baseurl'] = GServer::cleanURL($data['baseurl']);
1392 if (!empty($data['baseurl']) && empty($data['gsid'])) {
1393 $data['gsid'] = GServer::getID($data['baseurl']);
1396 if (!$contact_id && !empty($data['alias']) && ($data['alias'] != $data['url']) && !$in_loop) {
1397 $contact_id = self::getIdForURL($data["alias"], $uid, true, $default, true);
1403 'created' => DateTimeFormat::utcNow(),
1404 'url' => $data['url'],
1405 'nurl' => Strings::normaliseLink($data['url']),
1406 'addr' => $data['addr'] ?? '',
1407 'alias' => $data['alias'] ?? '',
1408 'notify' => $data['notify'] ?? '',
1409 'poll' => $data['poll'] ?? '',
1410 'name' => $data['name'] ?? '',
1411 'nick' => $data['nick'] ?? '',
1412 'photo' => $data['photo'] ?? '',
1413 'keywords' => $data['keywords'] ?? '',
1414 'location' => $data['location'] ?? '',
1415 'about' => $data['about'] ?? '',
1416 'network' => $data['network'],
1417 'pubkey' => $data['pubkey'] ?? '',
1418 'rel' => self::SHARING,
1419 'priority' => $data['priority'] ?? 0,
1420 'batch' => $data['batch'] ?? '',
1421 'request' => $data['request'] ?? '',
1422 'confirm' => $data['confirm'] ?? '',
1423 'poco' => $data['poco'] ?? '',
1424 'baseurl' => $data['baseurl'] ?? '',
1425 'gsid' => $data['gsid'] ?? null,
1426 'name-date' => DateTimeFormat::utcNow(),
1427 'uri-date' => DateTimeFormat::utcNow(),
1428 'avatar-date' => DateTimeFormat::utcNow(),
1434 $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
1436 // Before inserting we do check if the entry does exist now.
1437 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
1438 if (!DBA::isResult($contact)) {
1439 Logger::info('Create new contact', $fields);
1441 self::insert($fields);
1443 // We intentionally aren't using lastInsertId here. There is a chance for duplicates.
1444 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
1445 if (!DBA::isResult($contact)) {
1446 Logger::info('Contact creation failed', $fields);
1451 Logger::info('Contact had been created before', ['id' => $contact["id"], 'url' => $url, 'contact' => $fields]);
1454 $contact_id = $contact["id"];
1457 if (!empty($data['photo']) && ($data['network'] != Protocol::FEED)) {
1458 self::updateAvatar($data['photo'], $uid, $contact_id);
1461 if (in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
1462 if ($background_update) {
1463 // Update in the background when we fetched the data solely from the database
1464 Worker::add(PRIORITY_MEDIUM, "UpdateContact", $contact_id, ($uid == 0 ? 'force' : ''));
1466 // Else do a direct update
1467 self::updateFromProbe($contact_id, '', false);
1469 // Update the gcontact entry
1471 GContact::updateFromPublicContactID($contact_id);
1472 if (($data['network'] == Protocol::ACTIVITYPUB) && in_array(DI::config()->get('system', 'gcontact_discovery'), [GContact::DISCOVERY_DIRECT, GContact::DISCOVERY_RECURSIVE])) {
1473 GContact::discoverFollowers($data['url']);
1478 $fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'baseurl', 'gsid'];
1479 $contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);
1481 // This condition should always be true
1482 if (!DBA::isResult($contact)) {
1487 'url' => $data['url'],
1488 'nurl' => Strings::normaliseLink($data['url']),
1489 'updated' => DateTimeFormat::utcNow()
1492 $fields = ['addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'baseurl', 'gsid'];
1494 foreach ($fields as $field) {
1495 $updated[$field] = ($data[$field] ?? '') ?: $contact[$field];
1498 if (($updated['addr'] != $contact['addr']) || (!empty($data['alias']) && ($data['alias'] != $contact['alias']))) {
1499 $updated['uri-date'] = DateTimeFormat::utcNow();
1502 if (($data['name'] != $contact['name']) || ($data['nick'] != $contact['nick'])) {
1503 $updated['name-date'] = DateTimeFormat::utcNow();
1506 DBA::update('contact', $updated, ['id' => $contact_id], $contact);
1513 * Checks if the contact is archived
1515 * @param int $cid contact id
1517 * @return boolean Is the contact archived?
1518 * @throws HTTPException\InternalServerErrorException
1520 public static function isArchived(int $cid)
1526 $contact = DBA::selectFirst('contact', ['archive', 'url', 'batch'], ['id' => $cid]);
1527 if (!DBA::isResult($contact)) {
1531 if ($contact['archive']) {
1535 // Check status of ActivityPub endpoints
1536 $apcontact = APContact::getByURL($contact['url'], false);
1537 if (!empty($apcontact)) {
1538 if (!empty($apcontact['inbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['inbox']])) {
1542 if (!empty($apcontact['sharedinbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['sharedinbox']])) {
1547 // Check status of Diaspora endpoints
1548 if (!empty($contact['batch'])) {
1549 $condition = ['archive' => true, 'uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
1550 return DBA::exists('contact', $condition);
1557 * Checks if the contact is blocked
1559 * @param int $cid contact id
1561 * @return boolean Is the contact blocked?
1562 * @throws HTTPException\InternalServerErrorException
1564 public static function isBlocked($cid)
1570 $blocked = DBA::selectFirst('contact', ['blocked', 'url'], ['id' => $cid]);
1571 if (!DBA::isResult($blocked)) {
1575 if (Network::isUrlBlocked($blocked['url'])) {
1579 return (bool) $blocked['blocked'];
1583 * Checks if the contact is hidden
1585 * @param int $cid contact id
1587 * @return boolean Is the contact hidden?
1588 * @throws \Exception
1590 public static function isHidden($cid)
1596 $hidden = DBA::selectFirst('contact', ['hidden'], ['id' => $cid]);
1597 if (!DBA::isResult($hidden)) {
1600 return (bool) $hidden['hidden'];
1604 * Returns posts from a given contact url
1606 * @param string $contact_url Contact URL
1607 * @param bool $thread_mode
1608 * @param int $update
1609 * @return string posts in HTML
1610 * @throws \Exception
1612 public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0)
1614 return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update);
1618 * Returns posts from a given contact id
1620 * @param integer $cid
1621 * @param bool $thread_mode
1622 * @param integer $update
1623 * @return string posts in HTML
1624 * @throws \Exception
1626 public static function getPostsFromId($cid, $thread_mode = false, $update = 0)
1630 $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
1631 if (!DBA::isResult($contact)) {
1635 if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
1636 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))";
1638 $sql = "`item`.`uid` = ?";
1641 $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
1644 $condition = ["`$contact_field` = ? AND `gravity` = ? AND " . $sql,
1645 $cid, GRAVITY_PARENT, local_user()];
1647 $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
1648 $cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
1651 if (DI::mode()->isMobile()) {
1652 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
1653 DI::config()->get('system', 'itemspage_network_mobile'));
1655 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
1656 DI::config()->get('system', 'itemspage_network'));
1659 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
1661 $params = ['order' => ['received' => true],
1662 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
1665 $r = Item::selectThreadForUser(local_user(), ['uri'], $condition, $params);
1667 $items = Item::inArray($r);
1669 $o = conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
1671 $r = Item::selectForUser(local_user(), [], $condition, $params);
1673 $items = Item::inArray($r);
1675 $o = conversation($a, $items, 'contact-posts', false);
1679 $o .= $pager->renderMinimal(count($items));
1686 * Returns the account type name
1688 * The function can be called with either the user or the contact array
1690 * @param array $contact contact or user array
1693 public static function getAccountType(array $contact)
1695 // There are several fields that indicate that the contact or user is a forum
1696 // "page-flags" is a field in the user table,
1697 // "forum" and "prv" are used in the contact table. They stand for User::PAGE_FLAGS_COMMUNITY and User::PAGE_FLAGS_PRVGROUP.
1698 // "community" is used in the gcontact table and is true if the contact is User::PAGE_FLAGS_COMMUNITY or User::PAGE_FLAGS_PRVGROUP.
1699 if ((isset($contact['page-flags']) && (intval($contact['page-flags']) == User::PAGE_FLAGS_COMMUNITY))
1700 || (isset($contact['page-flags']) && (intval($contact['page-flags']) == User::PAGE_FLAGS_PRVGROUP))
1701 || (isset($contact['forum']) && intval($contact['forum']))
1702 || (isset($contact['prv']) && intval($contact['prv']))
1703 || (isset($contact['community']) && intval($contact['community']))
1705 $type = self::TYPE_COMMUNITY;
1707 $type = self::TYPE_PERSON;
1710 // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
1711 if (isset($contact["contact-type"])) {
1712 $type = $contact["contact-type"];
1715 if (isset($contact["account-type"])) {
1716 $type = $contact["account-type"];
1720 case self::TYPE_ORGANISATION:
1721 $account_type = DI::l10n()->t("Organisation");
1724 case self::TYPE_NEWS:
1725 $account_type = DI::l10n()->t('News');
1728 case self::TYPE_COMMUNITY:
1729 $account_type = DI::l10n()->t("Forum");
1737 return $account_type;
1745 * @throws \Exception
1747 public static function block($cid, $reason = null)
1749 $return = DBA::update('contact', ['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
1755 * Unblocks a contact
1759 * @throws \Exception
1761 public static function unblock($cid)
1763 $return = DBA::update('contact', ['blocked' => false, 'block_reason' => null], ['id' => $cid]);
1769 * Updates the avatar links in a contact only if needed
1771 * @param string $avatar Link to avatar picture
1772 * @param int $uid User id of contact owner
1773 * @param int $cid Contact id
1774 * @param bool $force force picture update
1777 * @throws HTTPException\InternalServerErrorException
1778 * @throws HTTPException\NotFoundException
1779 * @throws \ImagickException
1781 public static function updateAvatar($avatar, $uid, $cid, $force = false)
1783 $contact = DBA::selectFirst('contact', ['avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid, 'self' => false]);
1784 if (!DBA::isResult($contact)) {
1789 $contact['photo'] ?? '',
1790 $contact['thumb'] ?? '',
1791 $contact['micro'] ?? '',
1794 foreach ($data as $image_uri) {
1795 $image_rid = Photo::ridFromURI($image_uri);
1796 if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
1797 Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
1802 if (($contact["avatar"] != $avatar) || $force) {
1803 $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
1806 $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
1807 DBA::update('contact', $fields, ['id' => $cid]);
1809 // Update the public contact (contact id = 0)
1811 $pcontact = DBA::selectFirst('contact', ['id'], ['nurl' => $contact['nurl'], 'uid' => 0]);
1812 if (DBA::isResult($pcontact)) {
1813 DBA::update('contact', $fields, ['id' => $pcontact['id']]);
1821 * Helper function for "updateFromProbe". Updates personal and public contact
1823 * @param integer $id contact id
1824 * @param integer $uid user id
1825 * @param string $url The profile URL of the contact
1826 * @param array $fields The fields that are updated
1828 * @throws \Exception
1830 private static function updateContact($id, $uid, $url, array $fields)
1832 if (!DBA::update('contact', $fields, ['id' => $id])) {
1833 Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
1837 // Search for duplicated contacts and get rid of them
1838 if (self::removeDuplicates(Strings::normaliseLink($url), $uid) || ($uid != 0)) {
1842 // Update the corresponding gcontact entry
1843 GContact::updateFromPublicContactID($id);
1845 // Archive or unarchive the contact. We only need to do this for the public contact.
1846 // The archive/unarchive function will update the personal contacts by themselves.
1847 $contact = DBA::selectFirst('contact', [], ['id' => $id]);
1848 if (!DBA::isResult($contact)) {
1849 Logger::info('Couldn\'t select contact for archival.', ['id' => $id]);
1853 if (!empty($fields['success_update'])) {
1854 self::unmarkForArchival($contact);
1855 } elseif (!empty($fields['failure_update'])) {
1856 self::markForArchival($contact);
1859 $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url), 'network' => Protocol::FEDERATED];
1861 // These contacts are sharing with us, we don't poll them.
1862 // This means that we don't set the update fields in "OnePoll.php".
1863 $condition['rel'] = self::SHARING;
1864 DBA::update('contact', $fields, $condition);
1866 unset($fields['last-update']);
1867 unset($fields['success_update']);
1868 unset($fields['failure_update']);
1870 if (empty($fields)) {
1874 // We are polling these contacts, so we mustn't set the update fields here.
1875 $condition['rel'] = [self::FOLLOWER, self::FRIEND];
1876 DBA::update('contact', $fields, $condition);
1880 * Remove duplicated contacts
1882 * @param string $nurl Normalised contact url
1883 * @param integer $uid User id
1885 * @throws \Exception
1887 public static function removeDuplicates(string $nurl, int $uid)
1889 $condition = ['nurl' => $nurl, 'uid' => $uid, 'deleted' => false, 'network' => Protocol::FEDERATED];
1890 $count = DBA::count('contact', $condition);
1895 $first_contact = DBA::selectFirst('contact', ['id', 'network'], $condition, ['order' => ['id']]);
1896 if (!DBA::isResult($first_contact)) {
1897 // Shouldn't happen - so we handle it
1901 $first = $first_contact['id'];
1902 Logger::info('Found duplicates', ['count' => $count, 'first' => $first, 'uid' => $uid, 'nurl' => $nurl]);
1903 if (($uid != 0 && ($first_contact['network'] == Protocol::DFRN))) {
1904 // Don't handle non public DFRN duplicates by now (legacy DFRN is very special because of the key handling)
1905 Logger::info('Not handling non public DFRN duplicate', ['uid' => $uid, 'nurl' => $nurl]);
1909 // Find all duplicates
1910 $condition = ["`nurl` = ? AND `uid` = ? AND `id` != ? AND NOT `self` AND NOT `deleted`", $nurl, $uid, $first];
1911 $duplicates = DBA::select('contact', ['id', 'network'], $condition);
1912 while ($duplicate = DBA::fetch($duplicates)) {
1913 if (!in_array($duplicate['network'], Protocol::FEDERATED)) {
1917 Worker::add(PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
1919 DBA::close($duplicates);
1920 Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl]);
1925 * @param integer $id contact id
1926 * @param string $network Optional network we are probing for
1927 * @param boolean $force Optional forcing of network probing (otherwise we use the cached data)
1929 * @throws HTTPException\InternalServerErrorException
1930 * @throws \ImagickException
1932 public static function updateFromProbe($id, $network = '', $force = false)
1935 Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
1936 This will reliably kill your communication with old Friendica contacts.
1939 // These fields aren't updated by this routine:
1940 // 'xmpp', 'sensitive'
1942 $fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe',
1943 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
1944 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey'];
1945 $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
1946 if (!DBA::isResult($contact)) {
1950 $uid = $contact['uid'];
1951 unset($contact['uid']);
1953 $pubkey = $contact['pubkey'];
1954 unset($contact['pubkey']);
1956 $contact['photo'] = $contact['avatar'];
1957 unset($contact['avatar']);
1959 $ret = Probe::uri($contact['url'], $network, $uid, !$force);
1961 $updated = DateTimeFormat::utcNow();
1963 // We must not try to update relay contacts via probe. They are no real contacts.
1964 // We check after the probing to be able to correct falsely detected contact types.
1965 if (($contact['contact-type'] == self::TYPE_RELAY) &&
1966 (!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))) {
1967 self::updateContact($id, $uid, $contact['url'], ['last-update' => $updated, 'success_update' => $updated]);
1968 Logger::info('Not updating relais', ['id' => $id, 'url' => $contact['url']]);
1972 // If Probe::uri fails the network code will be different ("feed" or "unkn")
1973 if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
1974 if ($force && ($uid == 0)) {
1975 self::updateContact($id, $uid, $ret['url'], ['last-update' => $updated, 'failure_update' => $updated]);
1980 if (isset($ret['hide']) && is_bool($ret['hide'])) {
1981 $ret['unsearchable'] = $ret['hide'];
1984 if (isset($ret['account-type']) && is_int($ret['account-type'])) {
1985 $ret['forum'] = false;
1986 $ret['prv'] = false;
1987 $ret['contact-type'] = $ret['account-type'];
1988 if ($ret['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
1989 $apcontact = APContact::getByURL($ret['url'], false);
1990 if (isset($apcontact['manually-approve'])) {
1991 $ret['forum'] = (bool)!$apcontact['manually-approve'];
1992 $ret['prv'] = (bool)!$ret['forum'];
1997 $new_pubkey = $ret['pubkey'];
2001 // make sure to not overwrite existing values with blank entries except some technical fields
2002 $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];
2003 foreach ($ret as $key => $val) {
2004 if (!array_key_exists($key, $contact)) {
2006 } elseif (($contact[$key] != '') && ($val === '') && !is_bool($ret[$key]) && !in_array($key, $keep)) {
2007 $ret[$key] = $contact[$key];
2008 } elseif ($ret[$key] != $contact[$key]) {
2013 if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
2014 self::updateAvatar($ret['photo'], $uid, $id, $update || $force);
2019 self::updateContact($id, $uid, $ret['url'], ['last-update' => $updated, 'success_update' => $updated]);
2022 // Update the public contact
2024 self::updateFromProbeByURL($ret['url']);
2030 $ret['nurl'] = Strings::normaliseLink($ret['url']);
2031 $ret['updated'] = $updated;
2033 // Only fill the pubkey if it had been empty before. We have to prevent identity theft.
2034 if (empty($pubkey) && !empty($new_pubkey)) {
2035 $ret['pubkey'] = $new_pubkey;
2038 if (($ret['addr'] != $contact['addr']) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
2039 $ret['uri-date'] = DateTimeFormat::utcNow();
2042 if (($ret['name'] != $contact['name']) || ($ret['nick'] != $contact['nick'])) {
2043 $ret['name-date'] = $updated;
2046 if ($force && ($uid == 0)) {
2047 $ret['last-update'] = $updated;
2048 $ret['success_update'] = $updated;
2051 unset($ret['photo']);
2053 self::updateContact($id, $uid, $ret['url'], $ret);
2058 public static function updateFromProbeByURL($url, $force = false)
2060 $id = self::getIdForURL($url);
2066 self::updateFromProbe($id, '', $force);
2072 * Detects if a given contact array belongs to a legacy DFRN connection
2074 * @param array $contact
2077 public static function isLegacyDFRNContact($contact)
2079 // Newer Friendica contacts are connected via AP, then these fields aren't set
2080 return !empty($contact['dfrn-id']) || !empty($contact['issued-id']);
2084 * Detects the communication protocol for a given contact url.
2085 * This is used to detect Friendica contacts that we can communicate via AP.
2087 * @param string $url contact url
2088 * @param string $network Network of that contact
2089 * @return string with protocol
2091 public static function getProtocol($url, $network)
2093 if ($network != Protocol::DFRN) {
2097 $apcontact = APContact::getByURL($url);
2098 if (!empty($apcontact) && !empty($apcontact['generator'])) {
2099 return Protocol::ACTIVITYPUB;
2106 * Takes a $uid and a url/handle and adds a new contact
2108 * Currently if the contact is DFRN, interactive needs to be true, to redirect to the
2109 * dfrn_request page.
2111 * Otherwise this can be used to bulk add StatusNet contacts, Twitter contacts, etc.
2114 * $return['success'] boolean true if successful
2115 * $return['message'] error text if success is false.
2117 * Takes a $uid and a url/handle and adds a new contact
2119 * @param array $user The user the contact should be created for
2120 * @param string $url The profile URL of the contact
2121 * @param bool $interactive
2122 * @param string $network
2124 * @throws HTTPException\InternalServerErrorException
2125 * @throws HTTPException\NotFoundException
2126 * @throws \ImagickException
2128 public static function createFromProbe(array $user, $url, $interactive = false, $network = '')
2130 $result = ['cid' => -1, 'success' => false, 'message' => ''];
2132 // remove ajax junk, e.g. Twitter
2133 $url = str_replace('/#!/', '/', $url);
2135 if (!Network::isUrlAllowed($url)) {
2136 $result['message'] = DI::l10n()->t('Disallowed profile URL.');
2140 if (Network::isUrlBlocked($url)) {
2141 $result['message'] = DI::l10n()->t('Blocked domain');
2146 $result['message'] = DI::l10n()->t('Connect URL missing.');
2150 $arr = ['url' => $url, 'contact' => []];
2152 Hook::callAll('follow', $arr);
2155 $result['message'] = DI::l10n()->t('The contact could not be added. Please check the relevant network credentials in your Settings -> Social Networks page.');
2159 if (!empty($arr['contact']['name'])) {
2160 $ret = $arr['contact'];
2162 $ret = Probe::uri($url, $network, $user['uid'], false);
2165 if (($network != '') && ($ret['network'] != $network)) {
2166 Logger::log('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
2170 // check if we already have a contact
2171 // the poll url is more reliable than the profile url, as we may have
2172 // indirect links or webfinger links
2174 $condition = ['uid' => $user['uid'], 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
2175 $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2176 if (!DBA::isResult($contact)) {
2177 $condition = ['uid' => $user['uid'], 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
2178 $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2181 $protocol = self::getProtocol($ret['url'], $ret['network']);
2183 if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) {
2185 if (strlen(DI::baseUrl()->getUrlPath())) {
2186 $myaddr = bin2hex(DI::baseUrl() . '/profile/' . $user['nickname']);
2188 $myaddr = bin2hex($user['nickname'] . '@' . DI::baseUrl()->getHostname());
2191 DI::baseUrl()->redirect($ret['request'] . "&addr=$myaddr");
2195 } elseif (DI::config()->get('system', 'dfrn_only') && ($ret['network'] != Protocol::DFRN)) {
2196 $result['message'] = DI::l10n()->t('This site is not configured to allow communications with other networks.') . EOL;
2197 $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
2201 // This extra param just confuses things, remove it
2202 if ($protocol === Protocol::DIASPORA) {
2203 $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
2206 // do we have enough information?
2207 if (empty($ret['name']) || empty($ret['poll']) || (empty($ret['url']) && empty($ret['addr']))) {
2208 $result['message'] .= DI::l10n()->t('The profile address specified does not provide adequate information.') . EOL;
2209 if (empty($ret['poll'])) {
2210 $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
2212 if (empty($ret['name'])) {
2213 $result['message'] .= DI::l10n()->t('An author or name was not found.') . EOL;
2215 if (empty($ret['url'])) {
2216 $result['message'] .= DI::l10n()->t('No browser URL could be matched to this address.') . EOL;
2218 if (strpos($ret['url'], '@') !== false) {
2219 $result['message'] .= DI::l10n()->t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
2220 $result['message'] .= DI::l10n()->t('Use mailto: in front of address to force email check.') . EOL;
2225 if ($protocol === Protocol::OSTATUS && DI::config()->get('system', 'ostatus_disabled')) {
2226 $result['message'] .= DI::l10n()->t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
2227 $ret['notify'] = '';
2230 if (!$ret['notify']) {
2231 $result['message'] .= DI::l10n()->t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
2234 $writeable = ((($protocol === Protocol::OSTATUS) && ($ret['notify'])) ? 1 : 0);
2236 $subhub = (($protocol === Protocol::OSTATUS) ? true : false);
2238 $hidden = (($protocol === Protocol::MAIL) ? 1 : 0);
2241 if ($protocol == Protocol::ACTIVITYPUB) {
2242 $apcontact = APContact::getByURL($ret['url'], false);
2243 if (isset($apcontact['manually-approve'])) {
2244 $pending = (bool)$apcontact['manually-approve'];
2248 if (in_array($protocol, [Protocol::MAIL, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
2252 if (DBA::isResult($contact)) {
2254 $new_relation = (($contact['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING);
2256 $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
2257 DBA::update('contact', $fields, ['id' => $contact['id']]);
2259 $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
2261 // create contact record
2263 'uid' => $user['uid'],
2264 'created' => DateTimeFormat::utcNow(),
2265 'url' => $ret['url'],
2266 'nurl' => Strings::normaliseLink($ret['url']),
2267 'addr' => $ret['addr'],
2268 'alias' => $ret['alias'],
2269 'batch' => $ret['batch'],
2270 'notify' => $ret['notify'],
2271 'poll' => $ret['poll'],
2272 'poco' => $ret['poco'],
2273 'name' => $ret['name'],
2274 'nick' => $ret['nick'],
2275 'network' => $ret['network'],
2276 'baseurl' => $ret['baseurl'],
2277 'gsid' => $ret['gsid'] ?? null,
2278 'protocol' => $protocol,
2279 'pubkey' => $ret['pubkey'],
2280 'rel' => $new_relation,
2281 'priority'=> $ret['priority'],
2282 'writable'=> $writeable,
2283 'hidden' => $hidden,
2286 'pending' => $pending,
2291 $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $user['uid']]);
2292 if (!DBA::isResult($contact)) {
2293 $result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL;
2297 $contact_id = $contact['id'];
2298 $result['cid'] = $contact_id;
2300 Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id);
2302 // Update the avatar
2303 self::updateAvatar($ret['photo'], $user['uid'], $contact_id);
2305 // pull feed and consume it, which should subscribe to the hub.
2307 Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");
2309 $owner = User::getOwnerDataById($user['uid']);
2311 if (DBA::isResult($owner)) {
2312 if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
2313 // create a follow slap
2315 $item['verb'] = Activity::FOLLOW;
2316 $item['gravity'] = GRAVITY_ACTIVITY;
2317 $item['follow'] = $contact["url"];
2319 $item['title'] = '';
2321 $item['uri-id'] = 0;
2322 $item['attach'] = '';
2324 $slap = OStatus::salmon($item, $owner);
2326 if (!empty($contact['notify'])) {
2327 Salmon::slapper($owner, $contact['notify'], $slap);
2329 } elseif ($protocol == Protocol::DIASPORA) {
2330 $ret = Diaspora::sendShare($owner, $contact);
2331 Logger::log('share returns: ' . $ret);
2332 } elseif ($protocol == Protocol::ACTIVITYPUB) {
2333 $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact_id);
2334 if (empty($activity_id)) {
2335 // This really should never happen
2339 $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $user['uid'], $activity_id);
2340 Logger::log('Follow returns: ' . $ret);
2344 $result['success'] = true;
2349 * Updated contact's SSL policy
2351 * @param array $contact Contact array
2352 * @param string $new_policy New policy, valid: self,full
2354 * @return array Contact array with updated values
2355 * @throws \Exception
2357 public static function updateSslPolicy(array $contact, $new_policy)
2359 $ssl_changed = false;
2360 if ((intval($new_policy) == BaseURL::SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'], 'https:')) {
2361 $ssl_changed = true;
2362 $contact['url'] = str_replace('https:', 'http:', $contact['url']);
2363 $contact['request'] = str_replace('https:', 'http:', $contact['request']);
2364 $contact['notify'] = str_replace('https:', 'http:', $contact['notify']);
2365 $contact['poll'] = str_replace('https:', 'http:', $contact['poll']);
2366 $contact['confirm'] = str_replace('https:', 'http:', $contact['confirm']);
2367 $contact['poco'] = str_replace('https:', 'http:', $contact['poco']);
2370 if ((intval($new_policy) == BaseURL::SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'], 'http:')) {
2371 $ssl_changed = true;
2372 $contact['url'] = str_replace('http:', 'https:', $contact['url']);
2373 $contact['request'] = str_replace('http:', 'https:', $contact['request']);
2374 $contact['notify'] = str_replace('http:', 'https:', $contact['notify']);
2375 $contact['poll'] = str_replace('http:', 'https:', $contact['poll']);
2376 $contact['confirm'] = str_replace('http:', 'https:', $contact['confirm']);
2377 $contact['poco'] = str_replace('http:', 'https:', $contact['poco']);
2381 $fields = ['url' => $contact['url'], 'request' => $contact['request'],
2382 'notify' => $contact['notify'], 'poll' => $contact['poll'],
2383 'confirm' => $contact['confirm'], 'poco' => $contact['poco']];
2384 DBA::update('contact', $fields, ['id' => $contact['id']]);
2391 * @param array $importer Owner (local user) data
2392 * @param array $contact Existing owner-specific contact data we want to expand the relationship with. Optional.
2393 * @param array $datarray An item-like array with at least the 'author-id' and 'author-url' keys for the contact. Mandatory.
2394 * @param bool $sharing True: Contact is now sharing with Owner; False: Contact is now following Owner (default)
2395 * @param string $note Introduction additional message
2396 * @return bool|null True: follow request is accepted; False: relationship is rejected; Null: relationship is pending
2397 * @throws HTTPException\InternalServerErrorException
2398 * @throws \ImagickException
2400 public static function addRelationship(array $importer, array $contact, array $datarray, $sharing = false, $note = '')
2402 // Should always be set
2403 if (empty($datarray['author-id'])) {
2407 $fields = ['url', 'name', 'nick', 'avatar', 'photo', 'network', 'blocked'];
2408 $pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
2409 if (!DBA::isResult($pub_contact)) {
2410 // Should never happen
2414 // Contact is blocked at node-level
2415 if (self::isBlocked($datarray['author-id'])) {
2419 $url = ($datarray['author-link'] ?? '') ?: $pub_contact['url'];
2420 $name = $pub_contact['name'];
2421 $photo = ($pub_contact['avatar'] ?? '') ?: $pub_contact["photo"];
2422 $nick = $pub_contact['nick'];
2423 $network = $pub_contact['network'];
2425 // Ensure that we don't create a new contact when there already is one
2426 $cid = self::getIdForURL($url, $importer['uid']);
2428 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
2431 if (!empty($contact)) {
2432 if (!empty($contact['pending'])) {
2433 Logger::info('Pending contact request already exists.', ['url' => $url, 'uid' => $importer['uid']]);
2437 // Contact is blocked at user-level
2438 if (!empty($contact['id']) && !empty($importer['id']) &&
2439 self::isBlockedByUser($contact['id'], $importer['id'])) {
2443 // Make sure that the existing contact isn't archived
2444 self::unmarkForArchival($contact);
2446 if (($contact['rel'] == self::SHARING)
2447 || ($sharing && $contact['rel'] == self::FOLLOWER)) {
2448 DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
2449 ['id' => $contact['id'], 'uid' => $importer['uid']]);
2452 // Ensure to always have the correct network type, independent from the connection request method
2453 self::updateFromProbe($contact['id'], '', true);
2457 // send email notification to owner?
2458 if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) {
2459 Logger::log('ignoring duplicated connection request from pending contact ' . $url);
2463 // create contact record
2464 DBA::insert('contact', [
2465 'uid' => $importer['uid'],
2466 'created' => DateTimeFormat::utcNow(),
2468 'nurl' => Strings::normaliseLink($url),
2472 'network' => $network,
2473 'rel' => self::FOLLOWER,
2480 $contact_id = DBA::lastInsertId();
2482 // Ensure to always have the correct network type, independent from the connection request method
2483 self::updateFromProbe($contact_id, '', true);
2485 Contact::updateAvatar($photo, $importer["uid"], $contact_id, true);
2487 $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
2489 /// @TODO Encapsulate this into a function/method
2490 $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language'];
2491 $user = DBA::selectFirst('user', $fields, ['uid' => $importer['uid']]);
2492 if (DBA::isResult($user) && !in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
2493 // create notification
2494 $hash = Strings::getRandomHex();
2496 if (is_array($contact_record)) {
2497 DBA::insert('intro', ['uid' => $importer['uid'], 'contact-id' => $contact_record['id'],
2498 'blocked' => false, 'knowyou' => false, 'note' => $note,
2499 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow()]);
2502 Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);
2504 if (($user['notify-flags'] & Type::INTRO) &&
2505 in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL])) {
2508 'type' => Type::INTRO,
2509 'notify_flags' => $user['notify-flags'],
2510 'language' => $user['language'],
2511 'to_name' => $user['username'],
2512 'to_email' => $user['email'],
2513 'uid' => $user['uid'],
2514 'link' => DI::baseUrl() . '/notifications/intros',
2515 'source_name' => ((strlen(stripslashes($contact_record['name']))) ? stripslashes($contact_record['name']) : DI::l10n()->t('[Name Withheld]')),
2516 'source_link' => $contact_record['url'],
2517 'source_photo' => $contact_record['photo'],
2518 'verb' => ($sharing ? Activity::FRIEND : Activity::FOLLOW),
2522 } elseif (DBA::isResult($user) && in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
2523 if (($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) && ($network != Protocol::DIASPORA)) {
2524 self::createFromProbe($importer, $url, false, $network);
2527 $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
2528 $fields = ['pending' => false];
2529 if ($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) {
2530 $fields['rel'] = Contact::FRIEND;
2533 DBA::update('contact', $fields, $condition);
2542 public static function removeFollower($importer, $contact, array $datarray = [], $item = "")
2544 if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) {
2545 DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]);
2547 Contact::remove($contact['id']);
2551 public static function removeSharer($importer, $contact, array $datarray = [], $item = "")
2553 if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::FOLLOWER)) {
2554 DBA::update('contact', ['rel' => self::FOLLOWER], ['id' => $contact['id']]);
2556 Contact::remove($contact['id']);
2561 * Create a birthday event.
2563 * Update the year and the birthday.
2565 public static function updateBirthdays()
2569 AND `bd` > "0001-01-01"
2570 AND SUBSTRING(`bd`, 1, 4) != `bdyear`
2571 AND (`contact`.`rel` = ? OR `contact`.`rel` = ?)
2572 AND NOT `contact`.`pending`
2573 AND NOT `contact`.`hidden`
2574 AND NOT `contact`.`blocked`
2575 AND NOT `contact`.`archive`
2576 AND NOT `contact`.`deleted`',
2581 $contacts = DBA::select('contact', ['id', 'uid', 'name', 'url', 'bd'], $condition);
2583 while ($contact = DBA::fetch($contacts)) {
2584 Logger::log('update_contact_birthday: ' . $contact['bd']);
2586 $nextbd = DateTimeFormat::utcNow('Y') . substr($contact['bd'], 4);
2588 if (Event::createBirthday($contact, $nextbd)) {
2592 ['bdyear' => substr($nextbd, 0, 4), 'bd' => $nextbd],
2593 ['id' => $contact['id']]
2597 DBA::close($contacts);
2601 * Remove the unavailable contact ids from the provided list
2603 * @param array $contact_ids Contact id list
2605 * @throws \Exception
2607 public static function pruneUnavailable(array $contact_ids)
2609 if (empty($contact_ids)) {
2613 $contacts = Contact::selectToArray(['id'], [
2614 'id' => $contact_ids,
2620 return array_column($contacts, 'id');
2624 * Returns a magic link to authenticate remote visitors
2626 * @todo check if the return is either a fully qualified URL or a relative path to Friendica basedir
2628 * @param string $contact_url The address of the target contact profile
2629 * @param string $url An url that we will be redirected to after the authentication
2631 * @return string with "redir" link
2632 * @throws HTTPException\InternalServerErrorException
2633 * @throws \ImagickException
2635 public static function magicLink($contact_url, $url = '')
2637 if (!Session::isAuthenticated()) {
2638 return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
2641 $data = self::getProbeDataFromDatabase($contact_url);
2643 return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
2646 // Prevents endless loop in case only a non-public contact exists for the contact URL
2647 unset($data['uid']);
2649 return self::magicLinkByContact($data, $url ?: $contact_url);
2653 * Returns a magic link to authenticate remote visitors
2655 * @param integer $cid The contact id of the target contact profile
2656 * @param string $url An url that we will be redirected to after the authentication
2658 * @return string with "redir" link
2659 * @throws HTTPException\InternalServerErrorException
2660 * @throws \ImagickException
2662 public static function magicLinkbyId($cid, $url = '')
2664 $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
2666 return self::magicLinkByContact($contact, $url);
2670 * Returns a magic link to authenticate remote visitors
2672 * @param array $contact The contact array with "uid", "network" and "url"
2673 * @param string $url An url that we will be redirected to after the authentication
2675 * @return string with "redir" link
2676 * @throws HTTPException\InternalServerErrorException
2677 * @throws \ImagickException
2679 public static function magicLinkByContact($contact, $url = '')
2681 $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
2683 if (!Session::isAuthenticated() || ($contact['network'] != Protocol::DFRN)) {
2684 return $destination;
2687 // Only redirections to the same host do make sense
2688 if (($url != '') && (parse_url($url, PHP_URL_HOST) != parse_url($contact['url'], PHP_URL_HOST))) {
2692 if (!empty($contact['uid'])) {
2693 return self::magicLink($contact['url'], $url);
2696 if (empty($contact['id'])) {
2697 return $destination;
2700 $redirect = 'redir/' . $contact['id'];
2702 if (($url != '') && !Strings::compareLink($contact['url'], $url)) {
2703 $redirect .= '?url=' . $url;
2710 * Remove a contact from all groups
2712 * @param integer $contact_id
2714 * @return boolean Success
2716 public static function removeFromGroups($contact_id)
2718 return DBA::delete('group_member', ['contact-id' => $contact_id]);
2722 * Is the contact a forum?
2724 * @param integer $contactid ID of the contact
2726 * @return boolean "true" if it is a forum
2728 public static function isForum($contactid)
2730 $fields = ['forum', 'prv'];
2731 $condition = ['id' => $contactid];
2732 $contact = DBA::selectFirst('contact', $fields, $condition);
2733 if (!DBA::isResult($contact)) {
2738 return ($contact['forum'] || $contact['prv']);
2742 * Can the remote contact receive private messages?
2744 * @param array $contact
2747 public static function canReceivePrivateMessages(array $contact)
2749 $protocol = $contact['network'] ?? $contact['protocol'] ?? Protocol::PHANTOM;
2750 $self = $contact['self'] ?? false;
2752 return in_array($protocol, [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB]) && !$self;