]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact.php
Detect and remove contact duplicates
[friendica.git] / src / Model / Contact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Contact\Avatar;
25 use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
26 use Friendica\Content\Pager;
27 use Friendica\Content\Text\HTML;
28 use Friendica\Core\Hook;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Protocol;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session;
33 use Friendica\Core\System;
34 use Friendica\Core\Worker;
35 use Friendica\Database\Database;
36 use Friendica\Database\DBA;
37 use Friendica\DI;
38 use Friendica\Module\NoScrape;
39 use Friendica\Network\HTTPException;
40 use Friendica\Network\Probe;
41 use Friendica\Protocol\Activity;
42 use Friendica\Protocol\ActivityPub;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Images;
45 use Friendica\Util\Network;
46 use Friendica\Util\Proxy;
47 use Friendica\Util\Strings;
48
49 /**
50  * functions for interacting with a contact
51  */
52 class Contact
53 {
54         const DEFAULT_AVATAR_PHOTO = '/images/person-300.jpg';
55         const DEFAULT_AVATAR_THUMB = '/images/person-80.jpg';
56         const DEFAULT_AVATAR_MICRO = '/images/person-48.jpg';
57
58         /**
59          * @}
60          */
61
62         const LOCK_INSERT = 'contact-insert';
63
64         /**
65          * Account types
66          *
67          * TYPE_UNKNOWN - unknown type
68          *
69          * TYPE_PERSON - the account belongs to a person
70          *      Associated page types: PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE
71          *
72          * TYPE_ORGANISATION - the account belongs to an organisation
73          *      Associated page type: PAGE_SOAPBOX
74          *
75          * TYPE_NEWS - the account is a news reflector
76          *      Associated page type: PAGE_SOAPBOX
77          *
78          * TYPE_COMMUNITY - the account is community forum
79          *      Associated page types: PAGE_COMMUNITY, PAGE_PRVGROUP
80          *
81          * TYPE_RELAY - the account is a relay
82          *      This will only be assigned to contacts, not to user accounts
83          * @{
84          */
85         const TYPE_UNKNOWN =     -1;
86         const TYPE_PERSON =       User::ACCOUNT_TYPE_PERSON;
87         const TYPE_ORGANISATION = User::ACCOUNT_TYPE_ORGANISATION;
88         const TYPE_NEWS =         User::ACCOUNT_TYPE_NEWS;
89         const TYPE_COMMUNITY =    User::ACCOUNT_TYPE_COMMUNITY;
90         const TYPE_RELAY =        User::ACCOUNT_TYPE_RELAY;
91         /**
92          * @}
93          */
94
95         /**
96          * Contact_is
97          *
98          * Relationship types
99          * @{
100          */
101         const NOTHING  = 0;
102         const FOLLOWER = 1;
103         const SHARING  = 2;
104         const FRIEND   = 3;
105         const SELF     = 4;
106         /**
107          * @}
108          */
109
110          const MIRROR_DEACTIVATED = 0;
111          const MIRROR_FORWARDED = 1;
112          const MIRROR_OWN_POST = 2;
113          const MIRROR_NATIVE_RESHARE = 3;
114
115          /**
116          * @param array $fields    Array of selected fields, empty for all
117          * @param array $condition Array of fields for condition
118          * @param array $params    Array of several parameters
119          * @return array
120          * @throws \Exception
121          */
122         public static function selectToArray(array $fields = [], array $condition = [], array $params = []): array
123         {
124                 return DBA::selectToArray('contact', $fields, $condition, $params);
125         }
126
127         /**
128          * @param array $fields    Array of selected fields, empty for all
129          * @param array $condition Array of fields for condition
130          * @param array $params    Array of several parameters
131          * @return array|bool
132          * @throws \Exception
133          */
134         public static function selectFirst(array $fields = [], array $condition = [], array $params = [])
135         {
136                 $contact = DBA::selectFirst('contact', $fields, $condition, $params);
137
138                 return $contact;
139         }
140
141         /**
142          * Insert a row into the contact table
143          * Important: You can't use DBA::lastInsertId() after this call since it will be set to 0.
144          *
145          * @param array $fields         field array
146          * @param int   $duplicate_mode Do an update on a duplicate entry
147          *
148          * @return int  id of the created contact
149          * @throws \Exception
150          */
151         public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT): int
152         {
153                 if (!empty($fields['baseurl']) && empty($fields['gsid'])) {
154                         $fields['gsid'] = GServer::getID($fields['baseurl'], true);
155                 }
156
157                 $fields['uri-id'] = ItemURI::getIdByURI($fields['url']);
158
159                 if (empty($fields['created'])) {
160                         $fields['created'] = DateTimeFormat::utcNow();
161                 }
162
163                 $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $fields);
164                 DBA::insert('contact', $fields, $duplicate_mode);
165                 $contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]);
166                 if (!DBA::isResult($contact)) {
167                         // Shouldn't happen
168                         Logger::warning('Created contact could not be found', ['fields' => $fields]);
169                         return 0;
170                 }
171
172                 $fields = DI::dbaDefinition()->truncateFieldsForTable('account-user', $contact);
173                 DBA::insert('account-user', $fields, Database::INSERT_IGNORE);
174                 $account_user = DBA::selectFirst('account-user', ['id'], ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id']]);
175                 if (empty($account_user['id'])) {
176                         Logger::warning('Account-user entry not found', ['cid' => $contact['id'], 'uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'url' => $contact['url']]);
177                 } elseif ($account_user['id'] != $contact['id']) {
178                         $duplicate = DBA::selectFirst('contact', [], ['id' => $account_user['id'], 'deleted' => false]);
179                         if (!empty($duplicate['id'])) {
180                                 $ret = Contact::deleteById($contact['id']);
181                                 Logger::notice('Deleted duplicated contact', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $duplicate['id'], 'uid' => $duplicate['uid'], 'uri-id' => $duplicate['uri-id'], 'url' => $duplicate['url']]);
182                                 $contact = $duplicate;
183                         } else {
184                                 $ret = DBA::update('account-user', ['id' => $contact['id']], ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id']]);
185                                 Logger::notice('Updated account-user', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $contact['id'], 'uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'url' => $contact['url']]);
186                         }
187                 }
188
189                 Contact\User::insertForContactArray($contact);
190
191                 return $contact['id'];
192         }
193
194         /**
195          * Delete contact by id
196          *
197          * @param integer $id
198          * @return boolean
199          */
200         public static function deleteById(int $id): bool
201         {
202                 Logger::debug('Delete contact', ['id' => $id]);
203                 DBA::delete('account-user', ['id' => $id]);
204                 return DBA::delete('contact', ['id' => $id]);
205         }
206
207         /**
208          * Updates rows in the contact table
209          *
210          * @param array         $fields     contains the fields that are updated
211          * @param array         $condition  condition array with the key values
212          * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields)
213          *
214          * @return boolean was the update successfull?
215          * @throws \Exception
216          * @todo Let's get rid of boolean type of $old_fields
217          */
218         public static function update(array $fields, array $condition, $old_fields = [])
219         {
220                 $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $fields);
221                 $ret = DBA::update('contact', $fields, $condition, $old_fields);
222
223                 // Apply changes to the "user-contact" table on dedicated fields
224                 Contact\User::updateByContactUpdate($fields, $condition);
225
226                 return $ret;
227         }
228
229         /**
230          * @param integer $id     Contact ID
231          * @param array   $fields Array of selected fields, empty for all
232          * @return array|boolean Contact record if it exists, false otherwise
233          * @throws \Exception
234          */
235         public static function getById(int $id, array $fields = [])
236         {
237                 return DBA::selectFirst('contact', $fields, ['id' => $id]);
238         }
239
240         /**
241          * Fetch the first contact with the provided uri-id.
242          *
243          * @param integer $uri_id uri-id of the contact
244          * @param array   $fields Array of selected fields, empty for all
245          * @return array|boolean Contact record if it exists, false otherwise
246          * @throws \Exception
247          */
248         public static function getByUriId(int $uri_id, array $fields = [])
249         {
250                 return DBA::selectFirst('contact', $fields, ['uri-id' => $uri_id], ['order' => ['uid']]);
251         }
252
253         /**
254          * Fetches a contact by a given url
255          *
256          * @param string  $url    profile url
257          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
258          * @param array   $fields Field list
259          * @param integer $uid    User ID of the contact
260          * @return array contact array
261          */
262         public static function getByURL(string $url, $update = null, array $fields = [], int $uid = 0): array
263         {
264                 if ($update || is_null($update)) {
265                         $cid = self::getIdForURL($url, $uid, $update);
266                         if (empty($cid)) {
267                                 return [];
268                         }
269
270                         $contact = self::getById($cid, $fields);
271                         if (empty($contact)) {
272                                 return [];
273                         }
274                         return $contact;
275                 }
276
277                 // Add internal fields
278                 $removal = [];
279                 if (!empty($fields)) {
280                         foreach (['id', 'avatar', 'created', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
281                                 if (!in_array($internal, $fields)) {
282                                         $fields[] = $internal;
283                                         $removal[] = $internal;
284                                 }
285                         }
286                 }
287
288                 // We first try the nurl (http://server.tld/nick), most common case
289                 $options = ['order' => ['id']];
290                 $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options);
291
292                 // Then the addr (nick@server.tld)
293                 if (!DBA::isResult($contact)) {
294                         $contact = DBA::selectFirst('contact', $fields, ['addr' => str_replace('acct:', '', $url), 'uid' => $uid, 'deleted' => false], $options);
295                 }
296
297                 // Then the alias (which could be anything)
298                 if (!DBA::isResult($contact)) {
299                         // The link could be provided as http although we stored it as https
300                         $ssl_url = str_replace('http://', 'https://', $url);
301                         $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid];
302                         $contact = DBA::selectFirst('contact', $fields, $condition, $options);
303                 }
304
305                 if (!DBA::isResult($contact)) {
306                         return [];
307                 }
308
309                 // Update the contact in the background if needed
310                 $updated = max($contact['success_update'], $contact['created'], $contact['updated'], $contact['last-update'], $contact['failure_update']);
311                 if (($updated < DateTimeFormat::utc('now -7 days')) && in_array($contact['network'], Protocol::FEDERATED) && !self::isLocalById($contact['id'])) {
312                         Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']);
313                 }
314
315                 // Remove the internal fields
316                 foreach ($removal as $internal) {
317                         unset($contact[$internal]);
318                 }
319
320                 return $contact;
321         }
322
323         /**
324          * Fetches a contact for a given user by a given url.
325          * In difference to "getByURL" the function will fetch a public contact when no user contact had been found.
326          *
327          * @param string  $url    profile url
328          * @param integer $uid    User ID of the contact
329          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
330          * @param array   $fields Field list
331          * @return array contact array
332          */
333         public static function getByURLForUser(string $url, int $uid = 0, $update = false, array $fields = []): array
334         {
335                 if ($uid != 0) {
336                         $contact = self::getByURL($url, $update, $fields, $uid);
337                         if (!empty($contact)) {
338                                 if (!empty($contact['id'])) {
339                                         $contact['cid'] = $contact['id'];
340                                         $contact['zid'] = 0;
341                                 }
342                                 return $contact;
343                         }
344                 }
345
346                 $contact = self::getByURL($url, $update, $fields);
347                 if (!empty($contact['id'])) {
348                         $contact['cid'] = 0;
349                         $contact['zid'] = $contact['id'];
350                 }
351                 return $contact;
352         }
353
354         /**
355          * Tests if the given contact is a follower
356          *
357          * @param int  $cid    Either public contact id or user's contact id
358          * @param int  $uid    User ID
359          * @param bool $strict If "true" then contact mustn't be set to pending or readonly
360          *
361          * @return boolean is the contact id a follower?
362          * @throws HTTPException\InternalServerErrorException
363          * @throws \ImagickException
364          */
365         public static function isFollower(int $cid, int $uid, bool $strict = false): bool
366         {
367                 if (Contact\User::isBlocked($cid, $uid)) {
368                         return false;
369                 }
370
371                 $cdata = self::getPublicAndUserContactID($cid, $uid);
372                 if (empty($cdata['user'])) {
373                         return false;
374                 }
375
376                 $condition = ['id' => $cdata['user'], 'rel' => [self::FOLLOWER, self::FRIEND]];
377                 if ($strict) {
378                         $condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
379                 }
380                 return DBA::exists('contact', $condition);
381         }
382
383         /**
384          * Tests if the given contact url is a follower
385          *
386          * @param string $url    Contact URL
387          * @param int    $uid    User ID
388          * @param bool   $strict If "true" then contact mustn't be set to pending or readonly
389          *
390          * @return boolean is the contact id a follower?
391          * @throws HTTPException\InternalServerErrorException
392          * @throws \ImagickException
393          */
394         public static function isFollowerByURL(string $url, int $uid, bool $strict = false): bool
395         {
396                 $cid = self::getIdForURL($url, $uid);
397
398                 if (empty($cid)) {
399                         return false;
400                 }
401
402                 return self::isFollower($cid, $uid, $strict);
403         }
404
405         /**
406          * Tests if the given user shares with the given contact
407          *
408          * @param int  $cid    Either public contact id or user's contact id
409          * @param int  $uid    User ID
410          * @param bool $strict If "true" then contact mustn't be set to pending or readonly
411          *
412          * @return boolean is the contact sharing with given user?
413          * @throws HTTPException\InternalServerErrorException
414          * @throws \ImagickException
415          */
416         public static function isSharing(int $cid, int $uid, bool $strict = false): bool
417         {
418                 if (Contact\User::isBlocked($cid, $uid)) {
419                         return false;
420                 }
421
422                 $cdata = self::getPublicAndUserContactID($cid, $uid);
423                 if (empty($cdata['user'])) {
424                         return false;
425                 }
426
427                 $condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]];
428                 if ($strict) {
429                         $condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
430                 }
431                 return DBA::exists('contact', $condition);
432         }
433
434         /**
435          * Tests if the given user follow the given contact url
436          *
437          * @param string $url    Contact URL
438          * @param int    $uid    User ID
439          * @param bool   $strict If "true" then contact mustn't be set to pending or readonly
440          *
441          * @return boolean is the contact url being followed?
442          * @throws HTTPException\InternalServerErrorException
443          * @throws \ImagickException
444          */
445         public static function isSharingByURL(string $url, int $uid, bool $strict = false): bool
446         {
447                 $cid = self::getIdForURL($url, $uid);
448
449                 if (empty($cid)) {
450                         return false;
451                 }
452
453                 return self::isSharing($cid, $uid, $strict);
454         }
455
456         /**
457          * Get the basepath for a given contact link
458          *
459          * @param string $url The contact link
460          * @param boolean $dont_update Don't update the contact
461          *
462          * @return string basepath
463          * @throws HTTPException\InternalServerErrorException
464          * @throws \ImagickException
465          */
466         public static function getBasepath(string $url, bool $dont_update = false): string
467         {
468                 $contact = DBA::selectFirst('contact', ['id', 'baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]);
469                 if (!DBA::isResult($contact)) {
470                         return '';
471                 }
472
473                 if (!empty($contact['baseurl'])) {
474                         return $contact['baseurl'];
475                 } elseif ($dont_update) {
476                         return '';
477                 }
478
479                 // Update the existing contact
480                 self::updateFromProbe($contact['id']);
481
482                 // And fetch the result
483                 $contact = DBA::selectFirst('contact', ['baseurl'], ['id' => $contact['id']]);
484                 if (empty($contact['baseurl'])) {
485                         Logger::info('No baseurl for contact', ['url' => $url]);
486                         return '';
487                 }
488
489                 Logger::info('Found baseurl for contact', ['url' => $url, 'baseurl' => $contact['baseurl']]);
490                 return $contact['baseurl'];
491         }
492
493         /**
494          * Check if the given contact url is on the same server
495          *
496          * @param string $url The contact link
497          *
498          * @return boolean Is it the same server?
499          */
500         public static function isLocal(string $url): bool
501         {
502                 if (!parse_url($url, PHP_URL_SCHEME)) {
503                         $addr_parts = explode('@', $url);
504                         return (count($addr_parts) == 2) && ($addr_parts[1] == DI::baseUrl()->getHostname());
505                 }
506
507                 return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl());
508         }
509
510         /**
511          * Check if the given contact ID is on the same server
512          *
513          * @param string $url The contact link
514          * @return boolean Is it the same server?
515          */
516         public static function isLocalById(int $cid): bool
517         {
518                 $contact = DBA::selectFirst('contact', ['url', 'baseurl'], ['id' => $cid]);
519                 if (!DBA::isResult($contact)) {
520                         return false;
521                 }
522
523                 if (empty($contact['baseurl'])) {
524                         $baseurl = self::getBasepath($contact['url'], true);
525                 } else {
526                         $baseurl = $contact['baseurl'];
527                 }
528
529                 return Strings::compareLink($baseurl, DI::baseUrl());
530         }
531
532         /**
533          * Returns the public contact id of the given user id
534          *
535          * @param  integer $uid User ID
536          *
537          * @return integer|boolean Public contact id for given user id
538          * @throws \Exception
539          */
540         public static function getPublicIdByUserId(int $uid)
541         {
542                 $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
543                 if (!DBA::isResult($self)) {
544                         return false;
545                 }
546                 return self::getIdForURL($self['url']);
547         }
548
549         /**
550          * Returns the contact id for the user and the public contact id for a given contact id
551          *
552          * @param int $cid Either public contact id or user's contact id
553          * @param int $uid User ID
554          *
555          * @return array with public and user's contact id
556          * @throws HTTPException\InternalServerErrorException
557          * @throws \ImagickException
558          */
559         public static function getPublicAndUserContactID(int $cid, int $uid): array
560         {
561                 // We have to use the legacy function as long as the post update hasn't finished
562                 if (DI::config()->get('system', 'post_update_version') < 1427) {
563                         return self::legacyGetPublicAndUserContactID($cid, $uid);
564                 }
565
566                 if (empty($uid) || empty($cid)) {
567                         return [];
568                 }
569
570                 $contact = DBA::selectFirst('account-user-view', ['id', 'uid', 'pid'], ['id' => $cid]);
571                 if (!DBA::isResult($contact) || !in_array($contact['uid'], [0, $uid])) {
572                         return [];
573                 }
574
575                 $pcid = $contact['pid'];
576                 if ($contact['uid'] == $uid) {
577                         $ucid = $contact['id'];
578                 } else {
579                         $contact = DBA::selectFirst('account-user-view', ['id', 'uid'], ['pid' => $cid, 'uid' => $uid]);
580                         if (DBA::isResult($contact)) {
581                                 $ucid = $contact['id'];
582                         } else {
583                                 $ucid = 0;
584                         }
585                 }
586
587                 return ['public' => $pcid, 'user' => $ucid];
588         }
589
590         /**
591          * Helper function for "getPublicAndUserContactID"
592          *
593          * @param int $cid Either public contact id or user's contact id
594          * @param int $uid User ID
595          * @return array with public and user's contact id
596          * @throws HTTPException\InternalServerErrorException
597          * @throws \ImagickException
598          */
599         private static function legacyGetPublicAndUserContactID(int $cid, int $uid): array
600         {
601                 if (empty($uid) || empty($cid)) {
602                         return [];
603                 }
604
605                 $contact = DBA::selectFirst('contact', ['id', 'uid', 'url'], ['id' => $cid]);
606                 if (!DBA::isResult($contact)) {
607                         return [];
608                 }
609
610                 // We quit when the user id don't match the user id of the provided contact
611                 if (($contact['uid'] != $uid) && ($contact['uid'] != 0)) {
612                         return [];
613                 }
614
615                 if ($contact['uid'] != 0) {
616                         $pcid = self::getIdForURL($contact['url'], 0, false, ['url' => $contact['url']]);
617                         if (empty($pcid)) {
618                                 return [];
619                         }
620                         $ucid = $contact['id'];
621                 } else {
622                         $pcid = $contact['id'];
623                         $ucid = self::getIdForURL($contact['url'], $uid);
624                 }
625
626                 return ['public' => $pcid, 'user' => $ucid];
627         }
628
629         /**
630          * Returns contact details for a given contact id in combination with a user id
631          *
632          * @param int $cid A contact ID
633          * @param int $uid The User ID
634          * @param array $fields The selected fields for the contact
635          * @return array The contact details
636          *
637          * @throws \Exception
638          */
639         public static function getContactForUser(int $cid, int $uid, array $fields = []): array
640         {
641                 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => $uid]);
642
643                 if (!DBA::isResult($contact)) {
644                         return [];
645                 } else {
646                         return $contact;
647                 }
648         }
649
650         /**
651          * Creates the self-contact for the provided user id
652          *
653          * @param int $uid
654          * @return bool Operation success
655          * @throws HTTPException\InternalServerErrorException
656          */
657         public static function createSelfFromUserId(int $uid): bool
658         {
659                 $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'pubkey', 'prvkey'],
660                         ['uid' => $uid, 'account_expired' => false]);
661                 if (!DBA::isResult($user)) {
662                         return false;
663                 }
664
665                 $contact = [
666                         'uid'         => $user['uid'],
667                         'created'     => DateTimeFormat::utcNow(),
668                         'self'        => 1,
669                         'name'        => $user['username'],
670                         'nick'        => $user['nickname'],
671                         'pubkey'      => $user['pubkey'],
672                         'prvkey'      => $user['prvkey'],
673                         'photo'       => User::getAvatarUrl($user),
674                         'thumb'       => User::getAvatarUrl($user, Proxy::SIZE_THUMB),
675                         'micro'       => User::getAvatarUrl($user, Proxy::SIZE_MICRO),
676                         'blocked'     => 0,
677                         'pending'     => 0,
678                         'url'         => DI::baseUrl() . '/profile/' . $user['nickname'],
679                         'nurl'        => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname']),
680                         'addr'        => $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3),
681                         'request'     => DI::baseUrl() . '/dfrn_request/' . $user['nickname'],
682                         'notify'      => DI::baseUrl() . '/dfrn_notify/'  . $user['nickname'],
683                         'poll'        => DI::baseUrl() . '/dfrn_poll/'    . $user['nickname'],
684                         'confirm'     => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
685                         'poco'        => DI::baseUrl() . '/poco/'         . $user['nickname'],
686                         'name-date'   => DateTimeFormat::utcNow(),
687                         'uri-date'    => DateTimeFormat::utcNow(),
688                         'avatar-date' => DateTimeFormat::utcNow(),
689                         'closeness'   => 0
690                 ];
691
692                 $return = true;
693
694                 // Only create the entry if it doesn't exist yet
695                 if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
696                         $return = (bool)self::insert($contact);
697                 }
698
699                 // Create the public contact
700                 if (!DBA::exists('contact', ['nurl' => $contact['nurl'], 'uid' => 0])) {
701                         $contact['self']   = false;
702                         $contact['uid']    = 0;
703                         $contact['prvkey'] = null;
704
705                         self::insert($contact, Database::INSERT_IGNORE);
706                 }
707
708                 return $return;
709         }
710
711         /**
712          * Updates the self-contact for the provided user id
713          *
714          * @param int   $uid
715          * @param bool  $update_avatar Force the avatar update
716          * @return bool "true" if updated
717          * @throws HTTPException\InternalServerErrorException
718          */
719         public static function updateSelfFromUserID(int $uid, bool $update_avatar = false): bool
720         {
721                 $fields = ['id', 'uri-id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey', 'manually-approve',
722                         'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
723                         'photo', 'thumb', 'micro', 'header', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network'];
724                 $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
725                 if (!DBA::isResult($self)) {
726                         return false;
727                 }
728
729                 $fields = ['uid', 'nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
730                 $user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'account_expired' => false]);
731                 if (!DBA::isResult($user)) {
732                         return false;
733                 }
734
735                 $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
736                         'country-name', 'pub_keywords', 'xmpp', 'matrix', 'net-publish'];
737                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
738                 if (!DBA::isResult($profile)) {
739                         return false;
740                 }
741
742                 $file_suffix = 'jpg';
743                 $url = DI::baseUrl() . '/profile/' . $user['nickname'];
744
745                 $fields = [
746                         'name'         => $profile['name'],
747                         'nick'         => $user['nickname'],
748                         'avatar-date'  => $self['avatar-date'],
749                         'location'     => Profile::formatLocation($profile),
750                         'about'        => $profile['about'],
751                         'keywords'     => $profile['pub_keywords'],
752                         'contact-type' => $user['account-type'],
753                         'prvkey'       => $user['prvkey'],
754                         'pubkey'       => $user['pubkey'],
755                         'xmpp'         => $profile['xmpp'],
756                         'matrix'       => $profile['matrix'],
757                         'network'      => Protocol::DFRN,
758                         'url'          => $url,
759                         // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
760                         'nurl'         => Strings::normaliseLink($url),
761                         'uri-id'       => ItemURI::getIdByURI($url),
762                         'addr'         => $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3),
763                         'request'      => DI::baseUrl() . '/dfrn_request/' . $user['nickname'],
764                         'notify'       => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'],
765                         'poll'         => DI::baseUrl() . '/dfrn_poll/'. $user['nickname'],
766                         'confirm'      => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
767                         'poco'         => DI::baseUrl() . '/poco/' . $user['nickname'],
768                 ];
769
770
771                 $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
772                 if (DBA::isResult($avatar)) {
773                         if ($update_avatar) {
774                                 $fields['avatar-date'] = DateTimeFormat::utcNow();
775                         }
776
777                         // Creating the path to the avatar, beginning with the file suffix
778                         $types = Images::supportedTypes();
779                         if (isset($types[$avatar['type']])) {
780                                 $file_suffix = $types[$avatar['type']];
781                         }
782
783                         // We are adding a timestamp value so that other systems won't use cached content
784                         $timestamp = strtotime($fields['avatar-date']);
785
786                         $prefix = DI::baseUrl() . '/photo/' .$avatar['resource-id'] . '-';
787                         $suffix = '.' . $file_suffix . '?ts=' . $timestamp;
788
789                         $fields['photo'] = $prefix . '4' . $suffix;
790                         $fields['thumb'] = $prefix . '5' . $suffix;
791                         $fields['micro'] = $prefix . '6' . $suffix;
792                 } else {
793                         // We hadn't found a photo entry, so we use the default avatar
794                         $fields['photo'] = self::getDefaultAvatar($fields, Proxy::SIZE_SMALL);
795                         $fields['thumb'] = self::getDefaultAvatar($fields, Proxy::SIZE_THUMB);
796                         $fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO);
797                 }
798
799                 $fields['avatar'] = User::getAvatarUrl($user);
800                 $fields['header'] = User::getBannerUrl($user);
801                 $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
802                 $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
803                 $fields['unsearchable'] = !$profile['net-publish'];
804                 $fields['manually-approve'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
805
806                 $update = false;
807
808                 foreach ($fields as $field => $content) {
809                         if ($self[$field] != $content) {
810                                 $update = true;
811                         }
812                 }
813
814                 if ($update) {
815                         if ($fields['name'] != $self['name']) {
816                                 $fields['name-date'] = DateTimeFormat::utcNow();
817                         }
818                         $fields['updated'] = DateTimeFormat::utcNow();
819                         self::update($fields, ['id' => $self['id']]);
820
821                         // Update the other contacts as well
822                         unset($fields['prvkey']);
823                         $fields['self'] = false;
824                         self::update($fields, ['uri-id' => $self['uri-id'], 'self' => false]);
825
826                         // Update the profile
827                         $fields = [
828                                 'photo' => User::getAvatarUrl($user),
829                                 'thumb' => User::getAvatarUrl($user, Proxy::SIZE_THUMB)
830                         ];
831
832                         DBA::update('profile', $fields, ['uid' => $uid]);
833                 }
834
835                 return $update;
836         }
837
838         /**
839          * Marks a contact for removal
840          *
841          * @param int $id contact id
842          * @return void
843          * @throws HTTPException\InternalServerErrorException
844          */
845         public static function remove(int $id)
846         {
847                 // We want just to make sure that we don't delete our "self" contact
848                 $contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro', 'uid'], ['id' => $id, 'self' => false]);
849                 if (!DBA::isResult($contact)) {
850                         return;
851                 }
852
853                 DBA::delete('account-user', ['id' => $id]);
854
855                 self::clearFollowerFollowingEndpointCache($contact['uid']);
856
857                 // Archive the contact
858                 self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
859
860                 if (!DBA::exists('contact', ['uri-id' => $contact['uri-id'], 'deleted' => false])) {
861                         Avatar::deleteCache($contact);
862                 }
863
864                 // Delete it in the background
865                 Worker::add(PRIORITY_MEDIUM, 'Contact\Remove', $id);
866         }
867
868         /**
869          * Unfollow the remote contact
870          *
871          * @param array $contact Target user-specific contact (uid != 0) array
872          * @return void
873          * @throws HTTPException\InternalServerErrorException
874          * @throws \ImagickException
875          */
876         public static function unfollow(array $contact): void
877         {
878                 if (empty($contact['network'])) {
879                         throw new \InvalidArgumentException('Empty network in contact array');
880                 }
881
882                 if (empty($contact['uid'])) {
883                         throw new \InvalidArgumentException('Unexpected public contact record');
884                 }
885
886                 if (in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
887                         $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
888                         if (!empty($cdata['public'])) {
889                                 Worker::add(PRIORITY_HIGH, 'Contact\Unfollow', $cdata['public'], $contact['uid']);
890                         }
891                 }
892
893                 self::removeSharer($contact);
894         }
895
896         /**
897          * Revoke follow privileges of the remote user contact
898          *
899          * The local relationship is updated immediately, the eventual remote server is messaged in the background.
900          *
901          * @param array $contact User-specific contact array (uid != 0) to revoke the follow from
902          * @return void
903          * @throws HTTPException\InternalServerErrorException
904          * @throws \ImagickException
905          */
906         public static function revokeFollow(array $contact): void
907         {
908                 if (empty($contact['network'])) {
909                         throw new \InvalidArgumentException('Empty network in contact array');
910                 }
911
912                 if (empty($contact['uid'])) {
913                         throw new \InvalidArgumentException('Unexpected public contact record');
914                 }
915
916                 if (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND])) {
917                         $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
918                         if (!empty($cdata['public'])) {
919                                 Worker::add(PRIORITY_HIGH, 'Contact\RevokeFollow', $cdata['public'], $contact['uid']);
920                         }
921                 }
922
923                 self::removeFollower($contact);
924         }
925
926         /**
927          * Completely severs a relationship with a contact
928          *
929          * @param array $contact User-specific contact (uid != 0) array
930          * @return void
931          * @throws HTTPException\InternalServerErrorException
932          * @throws \ImagickException
933          */
934         public static function terminateFriendship(array $contact)
935         {
936                 if (empty($contact['network'])) {
937                         throw new \InvalidArgumentException('Empty network in contact array');
938                 }
939
940                 if (empty($contact['uid'])) {
941                         throw new \InvalidArgumentException('Unexpected public contact record');
942                 }
943
944                 $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
945
946                 if (in_array($contact['rel'], [self::SHARING, self::FRIEND]) && !empty($cdata['public'])) {
947                         Worker::add(PRIORITY_HIGH, 'Contact\Unfollow', $cdata['public'], $contact['uid']);
948                 }
949
950                 if (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND]) && !empty($cdata['public'])) {
951                         Worker::add(PRIORITY_HIGH, 'Contact\RevokeFollow', $cdata['public'], $contact['uid']);
952                 }
953
954                 self::remove($contact['id']);
955         }
956
957         private static function clearFollowerFollowingEndpointCache(int $uid)
958         {
959                 if (empty($uid)) {
960                         return;
961                 }
962
963                 DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'followers:' . $uid);
964                 DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'following:' . $uid);
965                 DI::cache()->delete(NoScrape::CACHEKEY . $uid);
966         }
967
968         /**
969          * Marks a contact for archival after a communication issue delay
970          *
971          * Contact has refused to recognise us as a friend. We will start a countdown.
972          * If they still don't recognise us in 32 days, the relationship is over,
973          * and we won't waste any more time trying to communicate with them.
974          * This provides for the possibility that their database is temporarily messed
975          * up or some other transient event and that there's a possibility we could recover from it.
976          *
977          * @param array $contact contact to mark for archival
978          * @return void
979          * @throws HTTPException\InternalServerErrorException
980          */
981         public static function markForArchival(array $contact)
982         {
983                 if (!isset($contact['url']) && !empty($contact['id'])) {
984                         $fields = ['id', 'url', 'archive', 'self', 'term-date'];
985                         $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
986                         if (!DBA::isResult($contact)) {
987                                 return;
988                         }
989                 } elseif (!isset($contact['url'])) {
990                         Logger::info('Empty contact', ['contact' => $contact, 'callstack' => System::callstack(20)]);
991                 }
992
993                 Logger::info('Contact is marked for archival', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
994
995                 // Contact already archived or "self" contact? => nothing to do
996                 if ($contact['archive'] || $contact['self']) {
997                         return;
998                 }
999
1000                 if ($contact['term-date'] <= DBA::NULL_DATETIME) {
1001                         self::update(['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
1002                         self::update(['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
1003                 } else {
1004                         /* @todo
1005                          * We really should send a notification to the owner after 2-3 weeks
1006                          * so they won't be surprised when the contact vanishes and can take
1007                          * remedial action if this was a serious mistake or glitch
1008                          */
1009
1010                         /// @todo Check for contact vitality via probing
1011                         $archival_days = DI::config()->get('system', 'archival_days', 32);
1012
1013                         $expiry = $contact['term-date'] . ' + ' . $archival_days . ' days ';
1014                         if (DateTimeFormat::utcNow() > DateTimeFormat::utc($expiry)) {
1015                                 /* Relationship is really truly dead. archive them rather than
1016                                  * delete, though if the owner tries to unarchive them we'll start
1017                                  * the whole process over again.
1018                                  */
1019                                 self::update(['archive' => true], ['id' => $contact['id']]);
1020                                 self::update(['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
1021                         }
1022                 }
1023         }
1024
1025         /**
1026          * Cancels the archival countdown
1027          *
1028          * @see   Contact::markForArchival()
1029          *
1030          * @param array $contact contact to be unmarked for archival
1031          * @return void
1032          * @throws \Exception
1033          */
1034         public static function unmarkForArchival(array $contact)
1035         {
1036                 // Always unarchive the relay contact entry
1037                 if (!empty($contact['batch']) && !empty($contact['term-date']) && ($contact['term-date'] > DBA::NULL_DATETIME)) {
1038                         $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
1039                         $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
1040                         if (!DBA::exists('contact', array_merge($condition, $fields))) {
1041                                 self::update($fields, $condition);
1042                         }
1043                 }
1044
1045                 $condition = ['`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], DBA::NULL_DATETIME];
1046                 $exists = DBA::exists('contact', $condition);
1047
1048                 // We don't need to update, we never marked this contact for archival
1049                 if (!$exists) {
1050                         return;
1051                 }
1052
1053                 Logger::info('Contact is marked as vital again', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
1054
1055                 if (!isset($contact['url']) && !empty($contact['id'])) {
1056                         $fields = ['id', 'url', 'batch'];
1057                         $contact = DBA::selectFirst('contact', $fields, ['id' => $contact['id']]);
1058                         if (!DBA::isResult($contact)) {
1059                                 return;
1060                         }
1061                 }
1062
1063                 // It's a miracle. Our dead contact has inexplicably come back to life.
1064                 $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
1065                 self::update($fields, ['id' => $contact['id']]);
1066                 self::update($fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
1067         }
1068
1069         /**
1070          * Returns the data array for the photo menu of a given contact
1071          *
1072          * @param array $contact contact
1073          * @param int   $uid     optional, default 0
1074          * @return array
1075          * @throws HTTPException\InternalServerErrorException
1076          * @throws \ImagickException
1077          */
1078         public static function photoMenu(array $contact, int $uid = 0): array
1079         {
1080                 $pm_url = '';
1081                 $status_link = '';
1082                 $photos_link = '';
1083
1084                 if ($uid == 0) {
1085                         $uid = local_user();
1086                 }
1087
1088                 if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
1089                         if ($uid == 0) {
1090                                 $profile_link = self::magicLinkByContact($contact);
1091                                 $menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
1092
1093                                 return $menu;
1094                         }
1095
1096                         // Look for our own contact if the uid doesn't match and isn't public
1097                         $contact_own = DBA::selectFirst('contact', [], ['nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid]);
1098                         if (DBA::isResult($contact_own)) {
1099                                 return self::photoMenu($contact_own, $uid);
1100                         }
1101                 }
1102
1103                 $sparkle = false;
1104                 if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
1105                         $sparkle = true;
1106                         $profile_link = DI::baseUrl() . '/redir/' . $contact['id'];
1107                 } else {
1108                         $profile_link = $contact['url'];
1109                 }
1110
1111                 if ($profile_link === 'mailbox') {
1112                         $profile_link = '';
1113                 }
1114
1115                 if ($sparkle) {
1116                         $status_link = $profile_link . '/status';
1117                         $photos_link = str_replace('/profile/', '/photos/', $profile_link);
1118                         $profile_link = $profile_link . '/profile';
1119                 }
1120
1121                 if (self::canReceivePrivateMessages($contact) && empty($contact['pending'])) {
1122                         $pm_url = DI::baseUrl() . '/message/new/' . $contact['id'];
1123                 }
1124
1125                 $contact_url = DI::baseUrl() . '/contact/' . $contact['id'];
1126
1127                 $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations';
1128
1129                 $follow_link = '';
1130                 $unfollow_link = '';
1131                 if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
1132                         if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
1133                                 $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1';
1134                         } elseif(!$contact['pending']) {
1135                                 $follow_link = 'follow?url=' . urlencode($contact['url']) . '&auto=1';
1136                         }
1137                 }
1138
1139                 /**
1140                  * Menu array:
1141                  * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
1142                  */
1143                 if (empty($contact['uid'])) {
1144                         $menu = [
1145                                 'profile' => [DI::l10n()->t('View Profile')  , $profile_link , true],
1146                                 'network' => [DI::l10n()->t('Network Posts') , $posts_link   , false],
1147                                 'edit'    => [DI::l10n()->t('View Contact')  , $contact_url  , false],
1148                                 'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link  , true],
1149                                 'unfollow'=> [DI::l10n()->t('UnFollow')      , $unfollow_link, true],
1150                         ];
1151                 } else {
1152                         $menu = [
1153                                 'status'  => [DI::l10n()->t('View Status')   , $status_link      , true],
1154                                 'profile' => [DI::l10n()->t('View Profile')  , $profile_link     , true],
1155                                 'photos'  => [DI::l10n()->t('View Photos')   , $photos_link      , true],
1156                                 'network' => [DI::l10n()->t('Network Posts') , $posts_link       , false],
1157                                 'edit'    => [DI::l10n()->t('View Contact')  , $contact_url      , false],
1158                                 'pm'      => [DI::l10n()->t('Send PM')       , $pm_url           , false],
1159                                 'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link      , true],
1160                                 'unfollow'=> [DI::l10n()->t('UnFollow')      , $unfollow_link    , true],
1161                         ];
1162
1163                         if (!empty($contact['pending'])) {
1164                                 try {
1165                                         $intro = DI::intro()->selectForContact($contact['id']);
1166                                         $menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro->id, true];
1167                                 } catch (IntroductionNotFoundException $exception) {
1168                                         DI::logger()->error('Pending contact doesn\'t have an introduction.', ['exception' => $exception]);
1169                                 }
1170                         }
1171                 }
1172
1173                 $args = ['contact' => $contact, 'menu' => &$menu];
1174
1175                 Hook::callAll('contact_photo_menu', $args);
1176
1177                 $menucondensed = [];
1178
1179                 foreach ($menu as $menuname => $menuitem) {
1180                         if ($menuitem[1] != '') {
1181                                 $menucondensed[$menuname] = $menuitem;
1182                         }
1183                 }
1184
1185                 return $menucondensed;
1186         }
1187
1188         /**
1189          * Fetch the contact id for a given URL and user
1190          *
1191          * First lookup in the contact table to find a record matching either `url`, `nurl`,
1192          * `addr` or `alias`.
1193          *
1194          * If there's no record and we aren't looking for a public contact, we quit.
1195          * If there's one, we check that it isn't time to update the picture else we
1196          * directly return the found contact id.
1197          *
1198          * Second, we probe the provided $url whether it's http://server.tld/profile or
1199          * nick@server.tld. We quit if we can't get any info back.
1200          *
1201          * Third, we create the contact record if it doesn't exist
1202          *
1203          * Fourth, we update the existing record with the new data (avatar, alias, nick)
1204          * if there's any updates
1205          *
1206          * @param string  $url       Contact URL
1207          * @param integer $uid       The user id for the contact (0 = public contact)
1208          * @param boolean $update    true = always update, false = never update, null = update when not found
1209          * @param array   $default   Default value for creating the contact when everything else fails
1210          *
1211          * @return integer Contact ID
1212          * @throws HTTPException\InternalServerErrorException
1213          * @throws \ImagickException
1214          */
1215         public static function getIdForURL(string $url = null, int $uid = 0, $update = null, array $default = []): int
1216         {
1217                 $contact_id = 0;
1218
1219                 if (empty($url)) {
1220                         Logger::notice('Empty url, quitting', ['url' => $url, 'user' => $uid, 'default' => $default]);
1221                         return 0;
1222                 }
1223
1224                 $contact = self::getByURL($url, false, ['id', 'network', 'uri-id'], $uid);
1225
1226                 if (!empty($contact)) {
1227                         $contact_id = $contact['id'];
1228
1229                         if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) {
1230                                 Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
1231                                 return $contact_id;
1232                         }
1233                 } elseif ($uid != 0) {
1234                         Logger::debug('Contact does not exist for the user', ['url' => $url, 'uid' => $uid, 'update' => $update]);
1235                         return 0;
1236                 } elseif (empty($default) && !is_null($update) && !$update) {
1237                         Logger::info('Contact not found, update not desired', ['url' => $url, 'uid' => $uid, 'update' => $update]);
1238                         return 0;
1239                 }
1240
1241                 $data = [];
1242
1243                 if (empty($default['network']) || $update) {
1244                         $data = Probe::uri($url, '', $uid);
1245
1246                         // Take the default values when probing failed
1247                         if (!empty($default) && !in_array($data['network'], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
1248                                 $data = array_merge($data, $default);
1249                         }
1250                 } elseif (!empty($default['network'])) {
1251                         $data = $default;
1252                 }
1253
1254                 if (($uid == 0) && (empty($data['network']) || ($data['network'] == Protocol::PHANTOM))) {
1255                         // Fetch data for the public contact via the first found personal contact
1256                         /// @todo Check if this case can happen at all (possibly with mail accounts?)
1257                         $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'header', 'contact-type',
1258                                 'keywords', 'location', 'about', 'unsearchable', 'batch', 'notify', 'poll',
1259                                 'request', 'confirm', 'poco', 'subscribe', 'network', 'baseurl', 'gsid'];
1260
1261                         $personal_contact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `uid` != 0", $url]);
1262                         if (!DBA::isResult($personal_contact)) {
1263                                 $personal_contact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `uid` != 0", Strings::normaliseLink($url)]);
1264                         }
1265
1266                         if (DBA::isResult($personal_contact)) {
1267                                 Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact, 'callstack' => System::callstack(20)]);
1268                                 $data = $personal_contact;
1269                                 $data['photo'] = $personal_contact['avatar'];
1270                                 $data['account-type'] = $personal_contact['contact-type'];
1271                                 $data['hide'] = $personal_contact['unsearchable'];
1272                                 unset($data['avatar']);
1273                                 unset($data['contact-type']);
1274                                 unset($data['unsearchable']);
1275                         }
1276                 }
1277
1278                 if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
1279                         Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update, 'callstack' => System::callstack(20)]);
1280                         return 0;
1281                 }
1282
1283                 if (!$contact_id && !empty($data['account-type']) && $data['account-type'] == User::ACCOUNT_TYPE_DELETED) {
1284                         Logger::info('Contact is a tombstone. It will not be inserted', ['url' => $url, 'uid' => $uid]);
1285                         return 0;
1286                 }
1287
1288                 if (!$contact_id) {
1289                         $urls = [Strings::normaliseLink($url), Strings::normaliseLink($data['url'])];
1290                         if (!empty($data['alias'])) {
1291                                 $urls[] = Strings::normaliseLink($data['alias']);
1292                         }
1293                         $contact = self::selectFirst(['id'], ['nurl' => $urls, 'uid' => $uid]);
1294                         if (!empty($contact['id'])) {
1295                                 $contact_id = $contact['id'];
1296                                 Logger::info('Fetched id by url', ['cid' => $contact_id, 'uid' => $uid, 'url' => $url, 'data' => $data]);
1297                         }
1298                 }
1299
1300                 if (!$contact_id) {
1301                         // We only insert the basic data. The rest will be done in "updateFromProbeArray"
1302                         $fields = [
1303                                 'uid'       => $uid,
1304                                 'url'       => $data['url'],
1305                                 'nurl'      => Strings::normaliseLink($data['url']),
1306                                 'network'   => $data['network'],
1307                                 'created'   => DateTimeFormat::utcNow(),
1308                                 'rel'       => self::SHARING,
1309                                 'writable'  => 1,
1310                                 'blocked'   => 0,
1311                                 'readonly'  => 0,
1312                                 'pending'   => 0];
1313
1314                         $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
1315
1316                         // Before inserting we do check if the entry does exist now.
1317                         $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
1318                         if (DBA::isResult($contact)) {
1319                                 $contact_id = $contact['id'];
1320                                 Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
1321                         } else {
1322                                 $contact_id = self::insert($fields);
1323                                 if ($contact_id) {
1324                                         Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
1325                                 }
1326                         }
1327
1328                         if (!$contact_id) {
1329                                 Logger::warning('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
1330                                 return 0;
1331                         }
1332                 } else {
1333                         Logger::info('Contact will be updated', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
1334                 }
1335
1336                 if ($data['network'] == Protocol::DIASPORA) {
1337                         FContact::updateFromProbeArray($data);
1338                 }
1339
1340                 self::updateFromProbeArray($contact_id, $data);
1341
1342                 // Don't return a number for a deleted account
1343                 if (!empty($data['account-type']) && $data['account-type'] == User::ACCOUNT_TYPE_DELETED) {
1344                         Logger::info('Contact is a tombstone', ['url' => $url, 'uid' => $uid]);
1345                         return 0;
1346                 }
1347
1348                 return $contact_id;
1349         }
1350
1351         /**
1352          * Checks if the contact is archived
1353          *
1354          * @param int $cid contact id
1355          *
1356          * @return boolean Is the contact archived?
1357          * @throws HTTPException\InternalServerErrorException
1358          */
1359         public static function isArchived(int $cid): bool
1360         {
1361                 if ($cid == 0) {
1362                         return false;
1363                 }
1364
1365                 $contact = DBA::selectFirst('contact', ['archive', 'url', 'batch'], ['id' => $cid]);
1366                 if (!DBA::isResult($contact)) {
1367                         return false;
1368                 }
1369
1370                 if ($contact['archive']) {
1371                         return true;
1372                 }
1373
1374                 // Check status of ActivityPub endpoints
1375                 $apcontact = APContact::getByURL($contact['url'], false);
1376                 if (!empty($apcontact)) {
1377                         if (!empty($apcontact['inbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['inbox']])) {
1378                                 return true;
1379                         }
1380
1381                         if (!empty($apcontact['sharedinbox']) && DBA::exists('inbox-status', ['archive' => true, 'url' => $apcontact['sharedinbox']])) {
1382                                 return true;
1383                         }
1384                 }
1385
1386                 // Check status of Diaspora endpoints
1387                 if (!empty($contact['batch'])) {
1388                         $condition = ['archive' => true, 'uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
1389                         return DBA::exists('contact', $condition);
1390                 }
1391
1392                 return false;
1393         }
1394
1395         /**
1396          * Checks if the contact is blocked
1397          *
1398          * @param int $cid contact id
1399          * @return boolean Is the contact blocked?
1400          * @throws HTTPException\InternalServerErrorException
1401          */
1402         public static function isBlocked(int $cid): bool
1403         {
1404                 if ($cid == 0) {
1405                         return false;
1406                 }
1407
1408                 $blocked = DBA::selectFirst('contact', ['blocked', 'url'], ['id' => $cid]);
1409                 if (!DBA::isResult($blocked)) {
1410                         return false;
1411                 }
1412
1413                 if (Network::isUrlBlocked($blocked['url'])) {
1414                         return true;
1415                 }
1416
1417                 return (bool) $blocked['blocked'];
1418         }
1419
1420         /**
1421          * Checks if the contact is hidden
1422          *
1423          * @param int $cid contact id
1424          * @return boolean Is the contact hidden?
1425          * @throws \Exception
1426          */
1427         public static function isHidden(int $cid): bool
1428         {
1429                 if ($cid == 0) {
1430                         return false;
1431                 }
1432
1433                 $hidden = DBA::selectFirst('contact', ['hidden'], ['id' => $cid]);
1434                 if (!DBA::isResult($hidden)) {
1435                         return false;
1436                 }
1437                 return (bool) $hidden['hidden'];
1438         }
1439
1440         /**
1441          * Returns posts from a given contact url
1442          *
1443          * @param string $contact_url Contact URL
1444          * @param bool   $thread_mode
1445          * @param int    $update      Update mode
1446          * @param int    $parent      Item parent ID for the update mode
1447          * @param bool   $only_media  Only display media content
1448          * @return string posts in HTML
1449          * @throws \Exception
1450          */
1451         public static function getPostsFromUrl(string $contact_url, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
1452         {
1453                 return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
1454         }
1455
1456         /**
1457          * Returns posts from a given contact id
1458          *
1459          * @param int  $cid         Contact ID
1460          * @param bool $thread_mode
1461          * @param int  $update      Update mode
1462          * @param int  $parent      Item parent ID for the update mode
1463          * @param bool $only_media  Only display media content
1464          * @return string posts in HTML
1465          * @throws \Exception
1466          */
1467         public static function getPostsFromId(int $cid, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
1468         {
1469                 $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
1470                 if (!DBA::isResult($contact)) {
1471                         return '';
1472                 }
1473
1474                 if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
1475                         $sql = "(`uid` = 0 OR (`uid` = ? AND NOT `global`))";
1476                 } else {
1477                         $sql = "`uid` = ?";
1478                 }
1479
1480                 $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
1481
1482                 if ($thread_mode) {
1483                         $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ?)) AND " . $sql,
1484                                 $cid, GRAVITY_PARENT, $cid, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()];
1485                 } else {
1486                         $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
1487                                 $cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
1488                 }
1489
1490                 if (!empty($parent)) {
1491                         $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
1492                 } else {
1493                         $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
1494                         if (!empty($last_received)) {
1495                                 $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
1496                         }
1497                 }
1498
1499                 if ($only_media) {
1500                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))",
1501                                 Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
1502                 }
1503
1504                 if (DI::mode()->isMobile()) {
1505                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
1506                                 DI::config()->get('system', 'itemspage_network_mobile'));
1507                 } else {
1508                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
1509                                 DI::config()->get('system', 'itemspage_network'));
1510                 }
1511
1512                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
1513
1514                 $params = ['order' => ['received' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
1515
1516                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
1517                         $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1518                         $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
1519                 } else {
1520                         $o = '';
1521                 }
1522
1523                 if ($thread_mode) {
1524                         $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'commented'];
1525                         $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params));
1526
1527                         if ($pager->getStart() == 0) {
1528                                 $cdata = self::getPublicAndUserContactID($cid, local_user());
1529                                 if (!empty($cdata['public'])) {
1530                                         $pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields);
1531                                         $items = array_merge($items, $pinned);
1532                                 }
1533                         }
1534
1535                         $o .= DI::conversation()->create($items, 'contacts', $update, false, 'pinned_commented', local_user());
1536                 } else {
1537                         $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
1538                         $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params));
1539
1540                         if ($pager->getStart() == 0) {
1541                                 $cdata = self::getPublicAndUserContactID($cid, local_user());
1542                                 if (!empty($cdata['public'])) {
1543                                         $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
1544                                                 $cdata['public'], Post\Collection::FEATURED];
1545                                         $pinned = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params));
1546                                         $items = array_merge($pinned, $items);
1547                                 }
1548                         }
1549
1550                         $o .= DI::conversation()->create($items, 'contact-posts', $update);
1551                 }
1552
1553                 if (!$update) {
1554                         if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
1555                                 $o .= HTML::scrollLoader();
1556                         } else {
1557                                 $o .= $pager->renderMinimal(count($items));
1558                         }
1559                 }
1560
1561                 return $o;
1562         }
1563
1564         /**
1565          * Returns the account type name
1566          *
1567          * The function can be called with either the user or the contact array
1568          *
1569          * @param int $type type of contact or account
1570          * @return string
1571          */
1572         public static function getAccountType(int $type): string
1573         {
1574                 switch ($type) {
1575                         case self::TYPE_ORGANISATION:
1576                                 $account_type = DI::l10n()->t("Organisation");
1577                                 break;
1578
1579                         case self::TYPE_NEWS:
1580                                 $account_type = DI::l10n()->t('News');
1581                                 break;
1582
1583                         case self::TYPE_COMMUNITY:
1584                                 $account_type = DI::l10n()->t("Forum");
1585                                 break;
1586
1587                         default:
1588                                 $account_type = "";
1589                                 break;
1590                 }
1591
1592                 return $account_type;
1593         }
1594
1595         /**
1596          * Blocks a contact
1597          *
1598          * @param int $cid Contact id to block
1599          * @param string $reason Block reason
1600          * @return bool Whether it was successful
1601          */
1602         public static function block(int $cid, string $reason = null): bool
1603         {
1604                 $return = self::update(['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
1605
1606                 return $return;
1607         }
1608
1609         /**
1610          * Unblocks a contact
1611          *
1612          * @param int $cid Contact id to unblock
1613          * @return bool Whether it was successfull
1614          */
1615         public static function unblock(int $cid): bool
1616         {
1617                 $return = self::update(['blocked' => false, 'block_reason' => null], ['id' => $cid]);
1618
1619                 return $return;
1620         }
1621
1622         /**
1623          * Ensure that cached avatar exist
1624          *
1625          * @param integer $cid Contact id
1626          */
1627         public static function checkAvatarCache(int $cid)
1628         {
1629                 $contact = DBA::selectFirst('contact', ['url', 'network', 'avatar', 'photo', 'thumb', 'micro'], ['id' => $cid, 'uid' => 0, 'self' => false]);
1630                 if (!DBA::isResult($contact)) {
1631                         return;
1632                 }
1633
1634                 if (Network::isLocalLink($contact['url'])) {
1635                         return;
1636                 }
1637
1638                 if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || DI::config()->get('system', 'cache_contact_avatar')) {
1639                         if (!empty($contact['avatar']) && (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']))) {
1640                                 Logger::info('Adding avatar cache', ['id' => $cid, 'contact' => $contact]);
1641                                 self::updateAvatar($cid, $contact['avatar'], true);
1642                                 return;
1643                         }
1644                 } elseif (Photo::isPhotoURI($contact['photo']) || Photo::isPhotoURI($contact['thumb']) || Photo::isPhotoURI($contact['micro'])) {
1645                         Logger::info('Replacing legacy avatar cache', ['id' => $cid, 'contact' => $contact]);
1646                         self::updateAvatar($cid, $contact['avatar'], true);
1647                         return;
1648                 } elseif (DI::config()->get('system', 'avatar_cache') && (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']))) {
1649                         Logger::info('Adding avatar cache file', ['id' => $cid, 'contact' => $contact]);
1650                         self::updateAvatar($cid, $contact['avatar'], true);
1651                 return;
1652                 }
1653         }
1654
1655         /**
1656          * Return the photo path for a given contact array in the given size
1657          *
1658          * @param array $contact    contact array
1659          * @param string $field     Fieldname of the photo in the contact array
1660          * @param string $size      Size of the avatar picture
1661          * @param string $avatar    Avatar path that is displayed when no photo had been found
1662          * @param bool  $no_update Don't perfom an update if no cached avatar was found
1663          * @return string photo path
1664          */
1665         private static function getAvatarPath(array $contact, string $size, bool $no_update = false): string
1666         {
1667                 $contact = self::checkAvatarCacheByArray($contact, $no_update);
1668
1669                 if (DI::config()->get('system', 'avatar_cache')) {
1670                         switch ($size) {
1671                                 case Proxy::SIZE_MICRO:
1672                                         if (!empty($contact['micro']) && !Photo::isPhotoURI($contact['micro'])) {
1673                                                 return $contact['micro'];
1674                                         }
1675                                         break;
1676                                 case Proxy::SIZE_THUMB:
1677                                         if (!empty($contact['thumb']) && !Photo::isPhotoURI($contact['thumb'])) {
1678                                                 return $contact['thumb'];
1679                                         }
1680                                         break;
1681                                 case Proxy::SIZE_SMALL:
1682                                         if (!empty($contact['photo']) && !Photo::isPhotoURI($contact['photo'])) {
1683                                                 return $contact['photo'];
1684                                         }
1685                                         break;
1686                         }
1687                 }
1688
1689                 return self::getAvatarUrlForId($contact['id'], $size, $contact['updated'] ?? '');
1690         }
1691
1692         /**
1693          * Return the photo path for a given contact array
1694          *
1695          * @param array  $contact   Contact array
1696          * @param bool   $no_update Don't perfom an update if no cached avatar was found
1697          * @return string photo path
1698          */
1699         public static function getPhoto(array $contact, bool $no_update = false): string
1700         {
1701                 return self::getAvatarPath($contact, Proxy::SIZE_SMALL, $no_update);
1702         }
1703
1704         /**
1705          * Return the photo path (thumb size) for a given contact array
1706          *
1707          * @param array  $contact   Contact array
1708          * @param bool   $no_update Don't perfom an update if no cached avatar was found
1709          * @return string photo path
1710          */
1711         public static function getThumb(array $contact, bool $no_update = false): string
1712         {
1713                 return self::getAvatarPath($contact, Proxy::SIZE_THUMB, $no_update);
1714         }
1715
1716         /**
1717          * Return the photo path (micro size) for a given contact array
1718          *
1719          * @param array  $contact   Contact array
1720          * @param bool   $no_update Don't perfom an update if no cached avatar was found
1721          * @return string photo path
1722          */
1723         public static function getMicro(array $contact, bool $no_update = false): string
1724         {
1725                 return self::getAvatarPath($contact, Proxy::SIZE_MICRO, $no_update);
1726         }
1727
1728         /**
1729          * Check the given contact array for avatar cache fields
1730          *
1731          * @param array $contact
1732          * @param bool  $no_update Don't perfom an update if no cached avatar was found
1733          * @return array contact array with avatar cache fields
1734          */
1735         private static function checkAvatarCacheByArray(array $contact, bool $no_update = false): array
1736         {
1737                 $update = false;
1738                 $contact_fields = [];
1739                 $fields = ['photo', 'thumb', 'micro'];
1740                 foreach ($fields as $field) {
1741                         if (isset($contact[$field])) {
1742                                 $contact_fields[] = $field;
1743                         }
1744                         if (isset($contact[$field]) && empty($contact[$field])) {
1745                                 $update = true;
1746                         }
1747                 }
1748
1749                 if (!$update || $no_update) {
1750                         return $contact;
1751                 }
1752
1753                 $local = !empty($contact['url']) && Network::isLocalLink($contact['url']);
1754
1755                 if (!$local && !empty($contact['id']) && !empty($contact['avatar'])) {
1756                         self::updateAvatar($contact['id'], $contact['avatar'], true);
1757
1758                         $new_contact = self::getById($contact['id'], $contact_fields);
1759                         if (DBA::isResult($new_contact)) {
1760                                 // We only update the cache fields
1761                                 $contact = array_merge($contact, $new_contact);
1762                         }
1763                 } elseif ($local && !empty($contact['avatar'])) {
1764                         return $contact;
1765                 }
1766
1767                 /// add the default avatars if the fields aren't filled
1768                 if (isset($contact['photo']) && empty($contact['photo'])) {
1769                         $contact['photo'] = self::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
1770                 }
1771                 if (isset($contact['thumb']) && empty($contact['thumb'])) {
1772                         $contact['thumb'] = self::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
1773                 }
1774                 if (isset($contact['micro']) && empty($contact['micro'])) {
1775                         $contact['micro'] = self::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
1776                 }
1777
1778                 return $contact;
1779         }
1780
1781         /**
1782          * Fetch the default header for the given contact
1783          *
1784          * @param array $contact  contact array
1785          * @return string avatar URL
1786          */
1787         public static function getDefaultHeader(array $contact): string
1788         {
1789                 if (!empty($contact['header'])) {
1790                         return $contact['header'];
1791                 }
1792
1793                 if (!empty($contact['gsid'])) {
1794                         // Use default banners for certain platforms
1795                         $gserver = DBA::selectFirst('gserver', ['platform'], ['id' => $contact['gsid']]);
1796                         $platform = strtolower($gserver['platform'] ?? '');
1797                 } else {
1798                         $platform = '';
1799                 }
1800
1801                 switch ($platform) {
1802                         case 'friendica':
1803                         case 'friendika':
1804                                 /**
1805                                  * Picture credits
1806                                  * @author  Lostinlight <https://mastodon.xyz/@lightone>
1807                                  * @license CC0 https://creativecommons.org/share-your-work/public-domain/cc0/
1808                                  * @link    https://gitlab.com/lostinlight/per_aspera_ad_astra/-/blob/master/friendica-404/friendica-promo-bubbles.jpg
1809                                  */
1810                                 $header = DI::baseUrl() . '/images/friendica-banner.jpg';
1811                                 break;
1812                         case 'diaspora':
1813                                 /**
1814                                  * Picture credits
1815                                  * @author  John Liu <https://www.flickr.com/photos/8047705@N02/>
1816                                  * @license CC BY 2.0 https://creativecommons.org/licenses/by/2.0/
1817                                  * @link    https://www.flickr.com/photos/8047705@N02/5572197407
1818                                  */
1819                                 $header = DI::baseUrl() . '/images/diaspora-banner.jpg';
1820                                 break;
1821                         default:
1822                                 /**
1823                                  * Use a random picture.
1824                                  * The service provides random pictures from Unsplash.
1825                                  * @license https://unsplash.com/license
1826                                  */
1827                                 $header = 'https://picsum.photos/seed/' . hash('ripemd128', $contact['url']) . '/960/300';
1828                                 break;
1829                 }
1830
1831                 return $header;
1832         }
1833
1834         /**
1835          * Fetch the default avatar for the given contact and size
1836          *
1837          * @param array $contact  contact array
1838          * @param string $size    Size of the avatar picture
1839          * @return string avatar URL
1840          */
1841         public static function getDefaultAvatar(array $contact, string $size): string
1842         {
1843                 switch ($size) {
1844                         case Proxy::SIZE_MICRO:
1845                                 $avatar['size'] = 48;
1846                                 $default = self::DEFAULT_AVATAR_MICRO;
1847                                 break;
1848
1849                         case Proxy::SIZE_THUMB:
1850                                 $avatar['size'] = 80;
1851                                 $default = self::DEFAULT_AVATAR_THUMB;
1852                                 break;
1853
1854                         case Proxy::SIZE_SMALL:
1855                         default:
1856                                 $avatar['size'] = 300;
1857                                 $default = self::DEFAULT_AVATAR_PHOTO;
1858                                 break;
1859                 }
1860
1861                 if (!DI::config()->get('system', 'remote_avatar_lookup')) {
1862                         $platform = '';
1863                         $type     = Contact::TYPE_PERSON;
1864
1865                         if (!empty($contact['id'])) {
1866                                 $account = DBA::selectFirst('account-user-view', ['platform', 'contact-type'], ['id' => $contact['id']]);
1867                                 $platform = $account['platform'] ?? '';
1868                                 $type     = $account['contact-type'] ?? Contact::TYPE_PERSON;
1869                         }
1870
1871                         if (empty($platform) && !empty($contact['uri-id'])) {
1872                                 $account = DBA::selectFirst('account-user-view', ['platform', 'contact-type'], ['uri-id' => $contact['uri-id']]);
1873                                 $platform = $account['platform'] ?? '';
1874                                 $type     = $account['contact-type'] ?? Contact::TYPE_PERSON;
1875                         }
1876
1877                         switch ($platform) {
1878                                 case 'corgidon':
1879                                         /**
1880                                          * Picture credits
1881                                          * @license GNU Affero General Public License v3.0
1882                                          * @link    https://github.com/msdos621/corgidon/blob/main/public/avatars/original/missing.png
1883                                          */
1884                                         $default = '/images/default/corgidon.png';
1885                                         break;
1886
1887                                 case 'diaspora':
1888                                         /**
1889                                          * Picture credits
1890                                          * @license GNU Affero General Public License v3.0
1891                                          * @link    https://github.com/diaspora/diaspora/
1892                                          */
1893                                         $default = '/images/default/diaspora.png';
1894                                         break;
1895
1896                                 case 'gotosocial':
1897                                         /**
1898                                          * Picture credits
1899                                          * @license GNU Affero General Public License v3.0
1900                                          * @link    https://github.com/superseriousbusiness/gotosocial/blob/main/web/assets/default_avatars/GoToSocial_icon1.svg
1901                                          */
1902                                         $default = '/images/default/gotosocial.svg';
1903                                         break;
1904
1905                                 case 'hometown':
1906                                         /**
1907                                          * Picture credits
1908                                          * @license GNU Affero General Public License v3.0
1909                                          * @link    https://github.com/hometown-fork/hometown/blob/hometown-dev/public/avatars/original/missing.png
1910                                          */
1911                                         $default = '/images/default/hometown.png';
1912                                         break;
1913
1914                                 case 'koyuspace':
1915                                         /**
1916                                          * Picture credits
1917                                          * @license GNU Affero General Public License v3.0
1918                                          * @link    https://github.com/koyuspace/mastodon/blob/main/public/avatars/original/missing.png
1919                                          */
1920                                         $default = '/images/default/koyuspace.png';
1921                                         break;
1922
1923                                 case 'ecko':
1924                                 case 'qoto':
1925                                 case 'mastodon':
1926                                         /**
1927                                          * Picture credits
1928                                          * @license GNU Affero General Public License v3.0
1929                                          * @link    https://github.com/mastodon/mastodon/tree/main/public/avatars/original/missing.png
1930                                          */
1931                                         $default = '/images/default/mastodon.png';
1932                                         break;
1933
1934                                 case 'peertube':
1935                                         if ($type == Contact::TYPE_COMMUNITY) {
1936                                                 /**
1937                                                  * Picture credits
1938                                                  * @license GNU Affero General Public License v3.0
1939                                                  * @link    https://github.com/Chocobozzz/PeerTube/blob/develop/client/src/assets/images/default-avatar-video-channel.png
1940                                                  */
1941                                                 $default = '/images/default/peertube-channel.png';
1942                                         } else {
1943                                                 /**
1944                                                  * Picture credits
1945                                                  * @license GNU Affero General Public License v3.0
1946                                                  * @link    https://github.com/Chocobozzz/PeerTube/blob/develop/client/src/assets/images/default-avatar-account.png
1947                                                  */
1948                                                 $default = '/images/default/peertube-account.png';
1949                                         }
1950                                         break;
1951
1952                                 case 'pleroma':
1953                                         /**
1954                                          * Picture credits
1955                                          * @license GNU Affero General Public License v3.0
1956                                          * @link    https://git.pleroma.social/pleroma/pleroma/-/blob/develop/priv/static/images/avi.png
1957                                          */
1958                                         $default = '/images/default/pleroma.png';
1959                                         break;
1960
1961                                 case 'plume':
1962                                         /**
1963                                          * Picture credits
1964                                          * @license GNU Affero General Public License v3.0
1965                                          * @link    https://github.com/Plume-org/Plume/blob/main/assets/images/default-avatar.png
1966                                          */
1967                                         $default = '/images/default/plume.png';
1968                                         break;
1969                         }
1970                         return DI::baseUrl() . $default;
1971                 }
1972
1973                 if (!empty($contact['xmpp'])) {
1974                         $avatar['email'] = $contact['xmpp'];
1975                 } elseif (!empty($contact['addr'])) {
1976                         $avatar['email'] = $contact['addr'];
1977                 } elseif (!empty($contact['url'])) {
1978                         $avatar['email'] = $contact['url'];
1979                 } else {
1980                         return DI::baseUrl() . $default;
1981                 }
1982
1983                 $avatar['url'] = '';
1984                 $avatar['success'] = false;
1985
1986                 Hook::callAll('avatar_lookup', $avatar);
1987
1988                 if ($avatar['success'] && !empty($avatar['url'])) {
1989                         return $avatar['url'];
1990                 }
1991
1992                 return DI::baseUrl() . $default;
1993         }
1994
1995         /**
1996          * Get avatar link for given contact id
1997          *
1998          * @param integer $cid     contact id
1999          * @param string  $size    One of the Proxy::SIZE_* constants
2000          * @param string  $updated Contact update date
2001          * @return string avatar link
2002          */
2003         public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string
2004         {
2005                 // We have to fetch the "updated" variable when it wasn't provided
2006                 // The parameter can be provided to improve performance
2007                 if (empty($updated)) {
2008                         $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]);
2009                         $updated = $account['updated'] ?? '';
2010                         $guid = $account['guid'] ?? '';
2011                 }
2012
2013                 $guid = urlencode($guid);
2014
2015                 $url = DI::baseUrl() . '/photo/contact/';
2016                 switch ($size) {
2017                         case Proxy::SIZE_MICRO:
2018                                 $url .= Proxy::PIXEL_MICRO . '/';
2019                                 break;
2020                         case Proxy::SIZE_THUMB:
2021                                 $url .= Proxy::PIXEL_THUMB . '/';
2022                                 break;
2023                         case Proxy::SIZE_SMALL:
2024                                 $url .= Proxy::PIXEL_SMALL . '/';
2025                                 break;
2026                         case Proxy::SIZE_MEDIUM:
2027                                 $url .= Proxy::PIXEL_MEDIUM . '/';
2028                                 break;
2029                         case Proxy::SIZE_LARGE:
2030                                 $url .= Proxy::PIXEL_LARGE . '/';
2031                                 break;
2032                 }
2033                 return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
2034         }
2035
2036         /**
2037          * Get avatar link for given contact URL
2038          *
2039          * @param string  $url  contact url
2040          * @param integer $uid  user id
2041          * @param string  $size One of the Proxy::SIZE_* constants
2042          * @return string avatar link
2043          */
2044         public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''): string
2045         {
2046                 $condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
2047                         Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
2048                 $contact = self::selectFirst(['id', 'updated'], $condition, ['order' => ['uid' => true]]);
2049                 return self::getAvatarUrlForId($contact['id'] ?? 0, $size, $contact['updated'] ?? '');
2050         }
2051
2052         /**
2053          * Get header link for given contact id
2054          *
2055          * @param integer $cid     contact id
2056          * @param string  $size    One of the Proxy::SIZE_* constants
2057          * @param string  $updated Contact update date
2058          * @return string header link
2059          */
2060         public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string
2061         {
2062                 // We have to fetch the "updated" variable when it wasn't provided
2063                 // The parameter can be provided to improve performance
2064                 if (empty($updated) || empty($guid)) {
2065                         $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]);
2066                         $updated = $account['updated'] ?? '';
2067                         $guid = $account['guid'] ?? '';
2068                 }
2069
2070                 $guid = urlencode($guid);
2071
2072                 $url = DI::baseUrl() . '/photo/header/';
2073                 switch ($size) {
2074                         case Proxy::SIZE_MICRO:
2075                                 $url .= Proxy::PIXEL_MICRO . '/';
2076                                 break;
2077                         case Proxy::SIZE_THUMB:
2078                                 $url .= Proxy::PIXEL_THUMB . '/';
2079                                 break;
2080                         case Proxy::SIZE_SMALL:
2081                                 $url .= Proxy::PIXEL_SMALL . '/';
2082                                 break;
2083                         case Proxy::SIZE_MEDIUM:
2084                                 $url .= Proxy::PIXEL_MEDIUM . '/';
2085                                 break;
2086                         case Proxy::SIZE_LARGE:
2087                                 $url .= Proxy::PIXEL_LARGE . '/';
2088                                 break;
2089                 }
2090
2091                 return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
2092         }
2093
2094         /**
2095          * Updates the avatar links in a contact only if needed
2096          *
2097          * @param int    $cid          Contact id
2098          * @param string $avatar       Link to avatar picture
2099          * @param bool   $force        force picture update
2100          * @param bool   $create_cache Enforces the creation of cached avatar fields
2101          *
2102          * @return void
2103          * @throws HTTPException\InternalServerErrorException
2104          * @throws HTTPException\NotFoundException
2105          * @throws \ImagickException
2106          */
2107         public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
2108         {
2109                 $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'xmpp', 'addr', 'nurl', 'url', 'network', 'uri-id'],
2110                         ['id' => $cid, 'self' => false]);
2111                 if (!DBA::isResult($contact)) {
2112                         return;
2113                 }
2114
2115                 $uid = $contact['uid'];
2116
2117                 // Only update the cached photo links of public contacts when they already are cached
2118                 if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
2119                         if ($contact['avatar'] != $avatar) {
2120                                 self::update(['avatar' => $avatar], ['id' => $cid]);
2121                                 Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
2122                         }
2123                         return;
2124                 }
2125
2126                 // User contacts use are updated through the public contacts
2127                 if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
2128                         $pcid = self::getIdForURL($contact['url'], 0, false);
2129                         if (!empty($pcid)) {
2130                                 Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
2131                                 self::updateAvatar($pcid, $avatar, $force, true);
2132                                 return;
2133                         }
2134                 }
2135
2136                 $default_avatar = empty($avatar) || strpos($avatar, self::DEFAULT_AVATAR_PHOTO);
2137
2138                 if ($default_avatar) {
2139                         $avatar = self::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
2140                 }
2141
2142                 $cache_avatar = DI::config()->get('system', 'cache_contact_avatar');
2143
2144                 // Local contact avatars don't need to be cached
2145                 if ($cache_avatar && Network::isLocalLink($contact['url'])) {
2146                         $cache_avatar = !DBA::exists('contact', ['nurl' => $contact['nurl'], 'self' => true]);
2147                 }
2148
2149                 if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || $cache_avatar) {
2150                         Avatar::deleteCache($contact);
2151
2152                         if ($default_avatar && Proxy::isLocalImage($avatar)) {
2153                                 $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
2154                                         'photo' => $avatar,
2155                                         'thumb' => self::getDefaultAvatar($contact, Proxy::SIZE_THUMB),
2156                                         'micro' => self::getDefaultAvatar($contact, Proxy::SIZE_MICRO)];
2157                                 Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
2158                         }
2159
2160                         // Use the data from the self account
2161                         if (empty($fields)) {
2162                                 $local_uid = User::getIdForURL($contact['url']);
2163                                 if (!empty($local_uid)) {
2164                                         $fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
2165                                         Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
2166                                 }
2167                         }
2168
2169                         if (empty($fields)) {
2170                                 $update = ($contact['avatar'] != $avatar) || $force;
2171
2172                                 if (!$update) {
2173                                         $data = [
2174                                                 $contact['photo'] ?? '',
2175                                                 $contact['thumb'] ?? '',
2176                                                 $contact['micro'] ?? '',
2177                                         ];
2178
2179                                         foreach ($data as $image_uri) {
2180                                                 $image_rid = Photo::ridFromURI($image_uri);
2181                                                 if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
2182                                                         Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
2183                                                         $update = true;
2184                                                 }
2185                                         }
2186                                 }
2187
2188                                 if ($update) {
2189                                         $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
2190                                         if ($photos) {
2191                                                 $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
2192                                                 $update = !empty($fields);
2193                                                 Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
2194                                         } else {
2195                                                 $update = false;
2196                                         }
2197                                 }
2198                         } else {
2199                                 $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
2200                         }
2201                 } else {
2202                         Photo::delete(['uid' => $uid, 'contact-id' => $cid, 'photo-type' => Photo::CONTACT_AVATAR]);
2203                         $fields = Avatar::fetchAvatarContact($contact, $avatar, $force);
2204                         $update = ($avatar . $fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['avatar'] . $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
2205                 }
2206
2207                 if (!$update) {
2208                         return;
2209                 }
2210
2211                 $cids = [];
2212                 $uids = [];
2213                 if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
2214                         // Collect all user contacts of the given public contact
2215                         $personal_contacts = DBA::select('contact', ['id', 'uid'],
2216                                 ["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
2217                         while ($personal_contact = DBA::fetch($personal_contacts)) {
2218                                 $cids[] = $personal_contact['id'];
2219                                 $uids[] = $personal_contact['uid'];
2220                         }
2221                         DBA::close($personal_contacts);
2222
2223                         if (!empty($cids)) {
2224                                 // Delete possibly existing cached user contact avatars
2225                                 Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'photo-type' => Photo::CONTACT_AVATAR]);
2226                         }
2227                 }
2228
2229                 $cids[] = $cid;
2230                 $uids[] = $uid;
2231                 Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
2232                 self::update($fields, ['id' => $cids]);
2233         }
2234
2235         public static function deleteContactByUrl(string $url)
2236         {
2237                 // Update contact data for all users
2238                 $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)];
2239                 $contacts = DBA::select('contact', ['id', 'uid'], $condition);
2240                 while ($contact = DBA::fetch($contacts)) {
2241                         Logger::info('Deleting contact', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $url]);
2242                         self::remove($contact['id']);
2243                 }
2244         }
2245
2246         /**
2247          * Helper function for "updateFromProbe". Updates personal and public contact
2248          *
2249          * @param integer $id     contact id
2250          * @param integer $uid    user id
2251          * @param integer $uri_id Uri-Id
2252          * @param string  $url    The profile URL of the contact
2253          * @param array   $fields The fields that are updated
2254          *
2255          * @throws \Exception
2256          */
2257         private static function updateContact(int $id, int $uid, int $uri_id, string $url, array $fields)
2258         {
2259                 if (!self::update($fields, ['id' => $id])) {
2260                         Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
2261                         return;
2262                 }
2263
2264                 self::setAccountUser($id, $uid, $uri_id, $url);
2265
2266                 // Archive or unarchive the contact.
2267                 $contact = DBA::selectFirst('contact', [], ['id' => $id]);
2268                 if (!DBA::isResult($contact)) {
2269                         Logger::info('Couldn\'t select contact for archival.', ['id' => $id]);
2270                         return;
2271                 }
2272
2273                 if (isset($fields['failed'])) {
2274                         if ($fields['failed']) {
2275                                 self::markForArchival($contact);
2276                         } else {
2277                                 self::unmarkForArchival($contact);
2278                         }
2279                 }
2280
2281                 if ($contact['uid'] != 0) {
2282                         return;
2283                 }
2284
2285                 // Update contact data for all users
2286                 $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)];
2287
2288                 $condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB];
2289                 self::update($fields, $condition);
2290
2291                 // We mustn't set the update fields for OStatus contacts since they are updated in OnePoll
2292                 $condition['network'] = Protocol::OSTATUS;
2293
2294                 // If the contact failed, propagate the update fields to all contacts
2295                 if (empty($fields['failed'])) {
2296                         unset($fields['last-update']);
2297                         unset($fields['success_update']);
2298                         unset($fields['failure_update']);
2299                 }
2300
2301                 if (empty($fields)) {
2302                         return;
2303                 }
2304
2305                 self::update($fields, $condition);
2306         }
2307
2308         /**
2309          * Create or update an "account-user" entry
2310          *
2311          * @param integer $id
2312          * @param integer $uid
2313          * @param integer $uri_id
2314          * @param string $url
2315          * @return void
2316          */
2317         public static function setAccountUser(int $id, int $uid, int $uri_id, string $url)
2318         {
2319                 if (empty($uri_id)) {
2320                         return;
2321                 }
2322
2323                 $account_user = DBA::selectFirst('account-user', ['id', 'uid', 'uri-id'], ['id' => $id]);
2324                 if (!empty($account_user['uri-id']) && ($account_user['uri-id'] != $uri_id)) {
2325                         if ($account_user['uid'] == $uid) {
2326                                 $ret = DBA::update('account-user', ['uri-id' => $uri_id], ['id' => $id]);
2327                                 Logger::notice('Updated account-user uri-id', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2328                         } else {
2329                                 // This should never happen
2330                                 Logger::warning('account-user exists for a different uri-id and uid', ['account_user' => $account_user, 'id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2331                         }
2332                 }
2333
2334                 $account_user = DBA::selectFirst('account-user', ['id', 'uid', 'uri-id'], ['uid' => $uid, 'uri-id' => $uri_id]);
2335                 if (!empty($account_user['id'])) {
2336                         if ($account_user['id'] == $id) {
2337                                 Logger::debug('account-user already exists', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2338                                 return;
2339                         } elseif (!DBA::exists('contact', ['id' => $account_user['id'], 'deleted' => false])) {
2340                                 $ret = DBA::update('account-user', ['id' => $id], ['uid' => $uid, 'uri-id' => $uri_id]);
2341                                 Logger::notice('Updated account-user', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2342                                 return;
2343                         }
2344                         Logger::warning('account-user exists for a different contact id', ['account_user' => $account_user, 'id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2345                         Worker::add(PRIORITY_HIGH, 'MergeContact', $account_user['id'], $id, $uid);
2346                 } elseif (DBA::insert('account-user', ['id' => $id, 'uri-id' => $uri_id, 'uid' => $uid], Database::INSERT_IGNORE)) {
2347                         Logger::notice('account-user was added', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2348                 } else {
2349                         Logger::warning('account-user was not added', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
2350                 }
2351         }
2352
2353         /**
2354          * Remove duplicated contacts
2355          *
2356          * @param string  $nurl  Normalised contact url
2357          * @param integer $uid   User id
2358          * @return boolean
2359          * @throws \Exception
2360          */
2361         public static function removeDuplicates(string $nurl, int $uid)
2362         {
2363                 $condition = ['nurl' => $nurl, 'uid' => $uid, 'self' => false, 'deleted' => false, 'network' => Protocol::FEDERATED];
2364                 $count = DBA::count('contact', $condition);
2365                 if ($count <= 1) {
2366                         return false;
2367                 }
2368
2369                 $first_contact = DBA::selectFirst('contact', ['id', 'network'], $condition, ['order' => ['id']]);
2370                 if (!DBA::isResult($first_contact)) {
2371                         // Shouldn't happen - so we handle it
2372                         return false;
2373                 }
2374
2375                 $first = $first_contact['id'];
2376                 Logger::info('Found duplicates', ['count' => $count, 'first' => $first, 'uid' => $uid, 'nurl' => $nurl]);
2377
2378                 // Find all duplicates
2379                 $condition = ["`nurl` = ? AND `uid` = ? AND `id` != ? AND NOT `self` AND NOT `deleted`", $nurl, $uid, $first];
2380                 $duplicates = DBA::select('contact', ['id', 'network'], $condition);
2381                 while ($duplicate = DBA::fetch($duplicates)) {
2382                         if (!in_array($duplicate['network'], Protocol::FEDERATED)) {
2383                                 continue;
2384                         }
2385
2386                         Worker::add(PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
2387                 }
2388                 DBA::close($duplicates);
2389                 Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl, 'callstack' => System::callstack(20)]);
2390                 return true;
2391         }
2392
2393         /**
2394          * Updates contact record by provided id and optional network
2395          *
2396          * @param integer $id      contact id
2397          * @param string  $network Optional network we are probing for
2398          * @return boolean
2399          * @throws HTTPException\InternalServerErrorException
2400          * @throws \ImagickException
2401          */
2402         public static function updateFromProbe(int $id, string $network = '')
2403         {
2404                 $contact = DBA::selectFirst('contact', ['uid', 'url'], ['id' => $id]);
2405                 if (!DBA::isResult($contact)) {
2406                         return false;
2407                 }
2408
2409                 $ret = Probe::uri($contact['url'], $network, $contact['uid']);
2410
2411                 if ($ret['network'] == Protocol::DIASPORA) {
2412                         FContact::updateFromProbeArray($ret);
2413                 }
2414
2415                 return self::updateFromProbeArray($id, $ret);
2416         }
2417
2418         /**
2419          * Checks if the given contact has got local data
2420          *
2421          * @param int   $id
2422          * @param array $contact
2423          *
2424          * @return boolean
2425          */
2426         private static function hasLocalData(int $id, array $contact): bool
2427         {
2428                 if (!empty($contact['uri-id']) && DBA::exists('contact', ["`uri-id` = ? AND `uid` != ?", $contact['uri-id'], 0])) {
2429                         // User contacts with the same uri-id exist
2430                         return true;
2431                 } elseif (DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($contact['url']), 0])) {
2432                         // User contacts with the same nurl exists (compatibility mode for systems with missing uri-id values)
2433                         return true;
2434                 }
2435                 if (DBA::exists('post-tag', ['cid' => $id])) {
2436                         // Is tagged in a post
2437                         return true;
2438                 }
2439                 if (DBA::exists('user-contact', ['cid' => $id])) {
2440                         // Has got user-contact data
2441                         return true;
2442                 }
2443                 if (Post::exists(['author-id' => $id])) {
2444                         // Posts with this author exist
2445                         return true;
2446                 }
2447                 if (Post::exists(['owner-id' => $id])) {
2448                         // Posts with this owner exist
2449                         return true;
2450                 }
2451                 if (Post::exists(['causer-id' => $id])) {
2452                         // Posts with this causer exist
2453                         return true;
2454                 }
2455                 // We don't have got this contact locally
2456                 return false;
2457         }
2458
2459         /**
2460          * Updates contact record by provided id and probed data
2461          *
2462          * @param integer $id      contact id
2463          * @param array   $ret     Probed data
2464          * @return boolean
2465          * @throws HTTPException\InternalServerErrorException
2466          * @throws \ImagickException
2467          */
2468         private static function updateFromProbeArray(int $id, array $ret): bool
2469         {
2470                 /*
2471                   Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
2472                   This will reliably kill your communication with old Friendica contacts.
2473                  */
2474
2475                 // These fields aren't updated by this routine:
2476                 // 'sensitive'
2477
2478                 $fields = ['uid', 'uri-id', 'avatar', 'header', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe',
2479                         'manually-approve', 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
2480                         'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item', 'xmpp', 'matrix',
2481                         'created', 'last-update'];
2482                 $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
2483                 if (!DBA::isResult($contact)) {
2484                         return false;
2485                 }
2486
2487                 if (self::isLocal($ret['url'])) {
2488                         if ($contact['uid'] == 0) {
2489                                 Logger::info('Local contacts are not updated here.');
2490                         } else {
2491                                 self::updateFromPublicContact($id, $contact);
2492                         }
2493                         return true;
2494                 }
2495
2496                 if (!empty($ret['account-type']) && $ret['account-type'] == User::ACCOUNT_TYPE_DELETED) {
2497                         Logger::info('Deleted account', ['id' => $id, 'url' => $ret['url'], 'ret' => $ret]);
2498                         self::remove($id);
2499
2500                         // Delete all contacts with the same URL
2501                         self::deleteContactByUrl($ret['url']);
2502                         return true;
2503                 }
2504
2505                 $uid = $contact['uid'];
2506                 unset($contact['uid']);
2507
2508                 $uriid = $contact['uri-id'];
2509                 unset($contact['uri-id']);
2510
2511                 $pubkey = $contact['pubkey'];
2512                 unset($contact['pubkey']);
2513
2514                 $created = $contact['created'];
2515                 unset($contact['created']);
2516
2517                 $last_update = $contact['last-update'];
2518                 unset($contact['last-update']);
2519
2520                 $contact['photo'] = $contact['avatar'];
2521                 unset($contact['avatar']);
2522
2523                 $updated = DateTimeFormat::utcNow();
2524
2525                 $has_local_data = self::hasLocalData($id, $contact);
2526
2527                 if (!in_array($ret['network'], array_merge(Protocol::FEDERATED, [Protocol::ZOT, Protocol::PHANTOM]))) {
2528                         // Periodical checks are only done on federated contacts
2529                         $failed_next_update  = null;
2530                         $success_next_update = null;
2531                 } elseif ($has_local_data) {
2532                         $failed_next_update  = GServer::getNextUpdateDate(false, $created, $last_update, !in_array($contact['network'], Protocol::FEDERATED));
2533                         $success_next_update = GServer::getNextUpdateDate(true, $created, $last_update, !in_array($contact['network'], Protocol::FEDERATED));
2534                 } else {
2535                         $failed_next_update  = DateTimeFormat::utc('now +6 month');
2536                         $success_next_update = DateTimeFormat::utc('now +1 month');
2537                 }
2538
2539                 if (Strings::normaliseLink($contact['url']) != Strings::normaliseLink($ret['url'])) {
2540                         Logger::notice('New URL differs from old URL', ['id' => $id, 'uid' => $uid, 'old' => $contact['url'], 'new' => $ret['url']]);
2541                         self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => true, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $failed_next_update, 'failure_update' => $updated]);
2542                         return false;
2543                 }
2544
2545                 // We must not try to update relay contacts via probe. They are no real contacts.
2546                 // We check after the probing to be able to correct falsely detected contact types.
2547                 if (($contact['contact-type'] == self::TYPE_RELAY) &&
2548                         (!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))) {
2549                         self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => false, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $success_next_update, 'success_update' => $updated]);
2550                         Logger::info('Not updating relais', ['id' => $id, 'url' => $contact['url']]);
2551                         return true;
2552                 }
2553
2554                 // If Probe::uri fails the network code will be different ("feed" or "unkn")
2555                 if (($ret['network'] == Protocol::PHANTOM) || (($ret['network'] == Protocol::FEED) && ($ret['network'] != $contact['network']))) {
2556                         self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => true, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $failed_next_update, 'failure_update' => $updated]);
2557                         return false;
2558                 }
2559
2560                 if (Strings::normaliseLink($ret['url']) != Strings::normaliseLink($contact['url'])) {
2561                         $cid = self::getIdForURL($ret['url'], 0, false);
2562                         if (!empty($cid) && ($cid != $id)) {
2563                                 Logger::notice('URL of contact changed.', ['id' => $id, 'new_id' => $cid, 'old' => $contact['url'], 'new' => $ret['url']]);
2564                                 return self::updateFromProbeArray($cid, $ret);
2565                         }
2566                 }
2567
2568                 if (isset($ret['hide']) && is_bool($ret['hide'])) {
2569                         $ret['unsearchable'] = $ret['hide'];
2570                 }
2571
2572                 if (isset($ret['account-type']) && is_int($ret['account-type'])) {
2573                         $ret['forum'] = false;
2574                         $ret['prv'] = false;
2575                         $ret['contact-type'] = $ret['account-type'];
2576                         if (($ret['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY) && isset($ret['manually-approve'])) {
2577                                 $ret['forum'] = (bool)!$ret['manually-approve'];
2578                                 $ret['prv'] = (bool)!$ret['forum'];
2579                         }
2580                 }
2581
2582                 $new_pubkey = $ret['pubkey'] ?? '';
2583
2584                 if ($uid == 0 && DI::config()->get('system', 'fetch_featured_posts')) {
2585                         if ($ret['network'] == Protocol::ACTIVITYPUB) {
2586                                 $apcontact = APContact::getByURL($ret['url'], false);
2587                                 if (!empty($apcontact['featured'])) {
2588                                         Worker::add(PRIORITY_LOW, 'FetchFeaturedPosts', $ret['url']);
2589                                 }
2590                         }
2591
2592                         $ret['last-item'] = Probe::getLastUpdate($ret);
2593                         Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
2594                 }
2595
2596                 $update = false;
2597                 $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], parse_url($ret['url'], PHP_URL_HOST));
2598
2599                 // make sure to not overwrite existing values with blank entries except some technical fields
2600                 $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];
2601                 foreach ($ret as $key => $val) {
2602                         if (!array_key_exists($key, $contact)) {
2603                                 unset($ret[$key]);
2604                         } elseif (($contact[$key] != '') && ($val === '') && !is_bool($ret[$key]) && !in_array($key, $keep)) {
2605                                 $ret[$key] = $contact[$key];
2606                         } elseif ($ret[$key] != $contact[$key]) {
2607                                 $update = true;
2608                         }
2609                 }
2610
2611                 if (!empty($ret['last-item']) && ($contact['last-item'] < $ret['last-item'])) {
2612                         $update = true;
2613                 } else {
2614                         unset($ret['last-item']);
2615                 }
2616
2617                 if (empty($uriid)) {
2618                         $update = true;
2619                 }
2620
2621                 if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
2622                         self::updateAvatar($id, $ret['photo'], $update);
2623                 }
2624
2625                 if (!$update) {
2626                         self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => false, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $success_next_update, 'success_update' => $updated]);
2627
2628                         if (Contact\Relation::isDiscoverable($ret['url'])) {
2629                                 Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
2630                         }
2631
2632                         // Update the public contact
2633                         if ($uid != 0) {
2634                                 $contact = self::getByURL($ret['url'], false, ['id']);
2635                                 if (!empty($contact['id'])) {
2636                                         self::updateFromProbeArray($contact['id'], $ret);
2637                                 }
2638                         }
2639
2640                         return true;
2641                 }
2642
2643                 $ret['uri-id']      = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]);
2644                 $ret['nurl']        = Strings::normaliseLink($ret['url']);
2645                 $ret['updated']     = $updated;
2646                 $ret['failed']      = false;
2647                 $ret['next-update'] = $success_next_update;
2648                 $ret['local-data']  = $has_local_data;
2649
2650                 // Only fill the pubkey if it had been empty before. We have to prevent identity theft.
2651                 if (empty($pubkey) && !empty($new_pubkey)) {
2652                         $ret['pubkey'] = $new_pubkey;
2653                 }
2654
2655                 if ((!empty($ret['addr']) && ($ret['addr'] != $contact['addr'])) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
2656                         $ret['uri-date'] = $updated;
2657                 }
2658
2659                 if ((!empty($ret['name']) && ($ret['name'] != $contact['name'])) || (!empty($ret['nick']) && ($ret['nick'] != $contact['nick']))) {
2660                         $ret['name-date'] = $updated;
2661                 }
2662
2663                 if (($uid == 0) || in_array($ret['network'], [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
2664                         $ret['last-update'] = $updated;
2665                         $ret['success_update'] = $updated;
2666                 }
2667
2668                 unset($ret['photo']);
2669
2670                 self::updateContact($id, $uid, $ret['uri-id'], $ret['url'], $ret);
2671
2672                 if (Contact\Relation::isDiscoverable($ret['url'])) {
2673                         Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
2674                 }
2675
2676                 return true;
2677         }
2678
2679         private static function updateFromPublicContact(int $id, array $contact)
2680         {
2681                 $public = self::getByURL($contact['url'], false);
2682
2683                 $fields = [];
2684
2685                 foreach ($contact as $field => $value) {
2686                         if ($field == 'uid') {
2687                                 continue;
2688                         }
2689                         if ($public[$field] != $value) {
2690                                 $fields[$field] = $public[$field];
2691                         }
2692                 }
2693                 if (!empty($fields)) {
2694                         self::update($fields, ['id' => $id, 'self' => false]);
2695                         Logger::info('Updating local contact', ['id' => $id]);
2696                 }
2697         }
2698
2699         /**
2700          * Updates contact record by provided URL
2701          *
2702          * @param integer $url contact url
2703          * @return integer Contact id
2704          * @throws HTTPException\InternalServerErrorException
2705          * @throws \ImagickException
2706          */
2707         public static function updateFromProbeByURL(string $url): int
2708         {
2709                 $id = self::getIdForURL($url);
2710
2711                 if (empty($id)) {
2712                         return $id;
2713                 }
2714
2715                 self::updateFromProbe($id);
2716
2717                 return $id;
2718         }
2719
2720         /**
2721          * Detects the communication protocol for a given contact url.
2722          * This is used to detect Friendica contacts that we can communicate via AP.
2723          *
2724          * @param string $url contact url
2725          * @param string $network Network of that contact
2726          * @return string with protocol
2727          */
2728         public static function getProtocol(string $url, string $network): string
2729         {
2730                 if ($network != Protocol::DFRN) {
2731                         return $network;
2732                 }
2733
2734                 $apcontact = APContact::getByURL($url);
2735                 if (!empty($apcontact) && !empty($apcontact['generator'])) {
2736                         return Protocol::ACTIVITYPUB;
2737                 } else {
2738                         return $network;
2739                 }
2740         }
2741
2742         /**
2743          * Takes a $uid and a url/handle and adds a new contact
2744          *
2745          * Currently if the contact is DFRN, interactive needs to be true, to redirect to the
2746          * dfrn_request page.
2747          *
2748          * Otherwise this can be used to bulk add StatusNet contacts, Twitter contacts, etc.
2749          *
2750          * Returns an array
2751          * $return['success'] boolean true if successful
2752          * $return['message'] error text if success is false.
2753          *
2754          * Takes a $uid and a url/handle and adds a new contact
2755          *
2756          * @param int    $uid         The user id the contact should be created for
2757          * @param string $url         The profile URL of the contact
2758          * @param string $network
2759          * @return array
2760          * @throws HTTPException\InternalServerErrorException
2761          * @throws HTTPException\NotFoundException
2762          * @throws \ImagickException
2763          */
2764         public static function createFromProbeForUser(int $uid, string $url, string $network = ''): array
2765         {
2766                 $result = ['cid' => -1, 'success' => false, 'message' => ''];
2767
2768                 // remove ajax junk, e.g. Twitter
2769                 $url = str_replace('/#!/', '/', $url);
2770
2771                 if (!Network::isUrlAllowed($url)) {
2772                         $result['message'] = DI::l10n()->t('Disallowed profile URL.');
2773                         return $result;
2774                 }
2775
2776                 if (Network::isUrlBlocked($url)) {
2777                         $result['message'] = DI::l10n()->t('Blocked domain');
2778                         return $result;
2779                 }
2780
2781                 if (!$url) {
2782                         $result['message'] = DI::l10n()->t('Connect URL missing.');
2783                         return $result;
2784                 }
2785
2786                 $arr = ['url' => $url, 'contact' => []];
2787
2788                 Hook::callAll('follow', $arr);
2789
2790                 if (empty($arr)) {
2791                         $result['message'] = DI::l10n()->t('The contact could not be added. Please check the relevant network credentials in your Settings -> Social Networks page.');
2792                         return $result;
2793                 }
2794
2795                 if (!empty($arr['contact']['name'])) {
2796                         $probed = false;
2797                         $ret = $arr['contact'];
2798                 } else {
2799                         $probed = true;
2800                         $ret = Probe::uri($url, $network, $uid);
2801
2802                         // Ensure that the public contact exists
2803                         if ($ret['network'] != Protocol::PHANTOM) {
2804                                 self::getIdForURL($url);
2805                         }
2806                 }
2807
2808                 if (($network != '') && ($ret['network'] != $network)) {
2809                         Logger::notice('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
2810                         return $result;
2811                 }
2812
2813                 // check if we already have a contact
2814                 // the poll url is more reliable than the profile url, as we may have
2815                 // indirect links or webfinger links
2816
2817                 $condition = ['uid' => $uid, 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
2818                 $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2819                 if (!DBA::isResult($contact)) {
2820                         $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
2821                         $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
2822                 }
2823
2824                 $protocol = self::getProtocol($ret['url'], $ret['network']);
2825
2826                 // This extra param just confuses things, remove it
2827                 if ($protocol === Protocol::DIASPORA) {
2828                         $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
2829                 }
2830
2831                 // do we have enough information?
2832                 if (empty($protocol) || ($protocol == Protocol::PHANTOM) || (empty($ret['url']) && empty($ret['addr']))) {
2833                         $result['message'] .= DI::l10n()->t('The profile address specified does not provide adequate information.') . EOL;
2834                         if (empty($ret['poll'])) {
2835                                 $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
2836                         }
2837                         if (empty($ret['name'])) {
2838                                 $result['message'] .= DI::l10n()->t('An author or name was not found.') . EOL;
2839                         }
2840                         if (empty($ret['url'])) {
2841                                 $result['message'] .= DI::l10n()->t('No browser URL could be matched to this address.') . EOL;
2842                         }
2843                         if (strpos($ret['url'], '@') !== false) {
2844                                 $result['message'] .= DI::l10n()->t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
2845                                 $result['message'] .= DI::l10n()->t('Use mailto: in front of address to force email check.') . EOL;
2846                         }
2847                         return $result;
2848                 }
2849
2850                 if ($protocol === Protocol::OSTATUS && DI::config()->get('system', 'ostatus_disabled')) {
2851                         $result['message'] .= DI::l10n()->t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
2852                         $ret['notify'] = '';
2853                 }
2854
2855                 if (!$ret['notify']) {
2856                         $result['message'] .= DI::l10n()->t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
2857                 }
2858
2859                 $writeable = ((($protocol === Protocol::OSTATUS) && ($ret['notify'])) ? 1 : 0);
2860
2861                 $subhub = (($protocol === Protocol::OSTATUS) ? true : false);
2862
2863                 $hidden = (($protocol === Protocol::MAIL) ? 1 : 0);
2864
2865                 $pending = false;
2866                 if (($protocol == Protocol::ACTIVITYPUB) && isset($ret['manually-approve'])) {
2867                         $pending = (bool)$ret['manually-approve'];
2868                 }
2869
2870                 if (in_array($protocol, [Protocol::MAIL, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
2871                         $writeable = 1;
2872                 }
2873
2874                 if (DBA::isResult($contact)) {
2875                         // update contact
2876                         $new_relation = (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND]) ? self::FRIEND : self::SHARING);
2877
2878                         $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
2879                         self::update($fields, ['id' => $contact['id']]);
2880                 } else {
2881                         $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
2882
2883                         // create contact record
2884                         self::insert([
2885                                 'uid'     => $uid,
2886                                 'created' => DateTimeFormat::utcNow(),
2887                                 'url'     => $ret['url'],
2888                                 'nurl'    => Strings::normaliseLink($ret['url']),
2889                                 'addr'    => $ret['addr'],
2890                                 'alias'   => $ret['alias'],
2891                                 'batch'   => $ret['batch'],
2892                                 'notify'  => $ret['notify'],
2893                                 'poll'    => $ret['poll'],
2894                                 'poco'    => $ret['poco'],
2895                                 'name'    => $ret['name'],
2896                                 'nick'    => $ret['nick'],
2897                                 'network' => $ret['network'],
2898                                 'baseurl' => $ret['baseurl'],
2899                                 'gsid'    => $ret['gsid'] ?? null,
2900                                 'protocol' => $protocol,
2901                                 'pubkey'  => $ret['pubkey'],
2902                                 'rel'     => $new_relation,
2903                                 'priority'=> $ret['priority'],
2904                                 'writable'=> $writeable,
2905                                 'hidden'  => $hidden,
2906                                 'blocked' => 0,
2907                                 'readonly'=> 0,
2908                                 'pending' => $pending,
2909                                 'subhub'  => $subhub
2910                         ]);
2911                 }
2912
2913                 $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]);
2914                 if (!DBA::isResult($contact)) {
2915                         $result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL;
2916                         return $result;
2917                 }
2918
2919                 $contact_id = $contact['id'];
2920                 $result['cid'] = $contact_id;
2921
2922                 Group::addMember(User::getDefaultGroup($uid), $contact_id);
2923
2924                 // Update the avatar
2925                 self::updateAvatar($contact_id, $ret['photo']);
2926
2927                 // pull feed and consume it, which should subscribe to the hub.
2928                 if ($contact['network'] == Protocol::OSTATUS) {
2929                         Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
2930                 }
2931
2932                 if ($probed) {
2933                         self::updateFromProbeArray($contact_id, $ret);
2934                 } else {
2935                         Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id);
2936                 }
2937
2938                 $result['success'] = Protocol::follow($uid, $contact, $protocol);
2939
2940                 return $result;
2941         }
2942
2943         /**
2944          * @param array  $importer Owner (local user) data
2945          * @param array  $contact  Existing owner-specific contact data we want to expand the relationship with. Optional.
2946          * @param array  $datarray An item-like array with at least the 'author-id' and 'author-url' keys for the contact. Mandatory.
2947          * @param bool   $sharing  True: Contact is now sharing with Owner; False: Contact is now following Owner (default)
2948          * @param string $note     Introduction additional message
2949          * @return bool|null True: follow request is accepted; False: relationship is rejected; Null: relationship is pending
2950          * @throws HTTPException\InternalServerErrorException
2951          * @throws \ImagickException
2952          */
2953         public static function addRelationship(array $importer, array $contact, array $datarray, bool $sharing = false, string $note = '')
2954         {
2955                 // Should always be set
2956                 if (empty($datarray['author-id'])) {
2957                         return false;
2958                 }
2959
2960                 $fields = ['id', 'url', 'name', 'nick', 'avatar', 'photo', 'network', 'blocked'];
2961                 $pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
2962                 if (!DBA::isResult($pub_contact)) {
2963                         // Should never happen
2964                         return false;
2965                 }
2966
2967                 // Contact is blocked at node-level
2968                 if (self::isBlocked($datarray['author-id'])) {
2969                         return false;
2970                 }
2971
2972                 $url = ($datarray['author-link'] ?? '') ?: $pub_contact['url'];
2973                 $name = $pub_contact['name'];
2974                 $photo = ($pub_contact['avatar'] ?? '') ?: $pub_contact["photo"];
2975                 $nick = $pub_contact['nick'];
2976                 $network = $pub_contact['network'];
2977
2978                 // Ensure that we don't create a new contact when there already is one
2979                 $cid = self::getIdForURL($url, $importer['uid']);
2980                 if (!empty($cid)) {
2981                         $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
2982                 }
2983
2984                 self::clearFollowerFollowingEndpointCache($importer['uid']);
2985
2986                 if (!empty($contact)) {
2987                         if (!empty($contact['pending'])) {
2988                                 Logger::info('Pending contact request already exists.', ['url' => $url, 'uid' => $importer['uid']]);
2989                                 return null;
2990                         }
2991
2992                         // Contact is blocked at user-level
2993                         if (!empty($contact['id']) && !empty($importer['id']) &&
2994                                 Contact\User::isBlocked($contact['id'], $importer['id'])) {
2995                                 return false;
2996                         }
2997
2998                         // Make sure that the existing contact isn't archived
2999                         self::unmarkForArchival($contact);
3000
3001                         if (($contact['rel'] == self::SHARING)
3002                                 || ($sharing && $contact['rel'] == self::FOLLOWER)) {
3003                                 self::update(['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
3004                                                 ['id' => $contact['id'], 'uid' => $importer['uid']]);
3005                         }
3006
3007                         // Ensure to always have the correct network type, independent from the connection request method
3008                         self::updateFromProbe($contact['id']);
3009
3010                         Post\UserNotification::insertNotification($pub_contact['id'], Activity::FOLLOW, $importer['uid']);
3011
3012                         return true;
3013                 } else {
3014                         // send email notification to owner?
3015                         if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) {
3016                                 Logger::notice('ignoring duplicated connection request from pending contact ' . $url);
3017                                 return null;
3018                         }
3019
3020                         // create contact record
3021                         $contact_id = self::insert([
3022                                 'uid'      => $importer['uid'],
3023                                 'created'  => DateTimeFormat::utcNow(),
3024                                 'url'      => $url,
3025                                 'nurl'     => Strings::normaliseLink($url),
3026                                 'name'     => $name,
3027                                 'nick'     => $nick,
3028                                 'network'  => $network,
3029                                 'rel'      => self::FOLLOWER,
3030                                 'blocked'  => 0,
3031                                 'readonly' => 0,
3032                                 'pending'  => 1,
3033                                 'writable' => 1,
3034                         ]);
3035
3036                         // Ensure to always have the correct network type, independent from the connection request method
3037                         self::updateFromProbe($contact_id);
3038
3039                         self::updateAvatar($contact_id, $photo, true);
3040
3041                         Post\UserNotification::insertNotification($pub_contact['id'], Activity::FOLLOW, $importer['uid']);
3042
3043                         $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
3044
3045                         /// @TODO Encapsulate this into a function/method
3046                         $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language'];
3047                         $user = DBA::selectFirst('user', $fields, ['uid' => $importer['uid']]);
3048                         if (DBA::isResult($user) && !in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
3049                                 // create notification
3050                                 if (is_array($contact_record)) {
3051                                         $intro = DI::introFactory()->createNew(
3052                                                 $importer['uid'],
3053                                                 $contact_record['id'],
3054                                                 $note
3055                                         );
3056                                         DI::intro()->save($intro);
3057                                 }
3058
3059                                 Group::addMember(User::getDefaultGroup($importer['uid']), $contact_record['id']);
3060
3061                                 if (($user['notify-flags'] & Notification\Type::INTRO) && $user['page-flags'] == User::PAGE_FLAGS_NORMAL) {
3062                                         DI::notify()->createFromArray([
3063                                                 'type'  => Notification\Type::INTRO,
3064                                                 'otype' => Notification\ObjectType::INTRO,
3065                                                 'verb'  => ($sharing ? Activity::FRIEND : Activity::FOLLOW),
3066                                                 'uid'   => $user['uid'],
3067                                                 'cid'   => $contact_record['id'],
3068                                                 'link'  => DI::baseUrl() . '/notifications/intros',
3069                                         ]);
3070                                 }
3071                         } elseif (DBA::isResult($user) && in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
3072                                 if (($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) && ($network != Protocol::DIASPORA)) {
3073                                         self::createFromProbeForUser($importer['uid'], $url, $network);
3074                                 }
3075
3076                                 $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
3077                                 $fields = ['pending' => false];
3078                                 if ($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) {
3079                                         $fields['rel'] = self::FRIEND;
3080                                 }
3081
3082                                 self::update($fields, $condition);
3083
3084                                 return true;
3085                         }
3086                 }
3087
3088                 return null;
3089         }
3090
3091         /**
3092          * Update the local relationship when a local user loses a follower
3093          *
3094          * @param array $contact User-specific contact (uid != 0) array
3095          * @return void
3096          * @throws HTTPException\InternalServerErrorException
3097          * @throws \ImagickException
3098          */
3099         public static function removeFollower(array $contact)
3100         {
3101                 if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) {
3102                         self::update(['rel' => self::SHARING], ['id' => $contact['id']]);
3103                 } elseif (!empty($contact['id'])) {
3104                         self::remove($contact['id']);
3105                 } else {
3106                         DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]);
3107                         return;
3108                 }
3109
3110                 self::clearFollowerFollowingEndpointCache($contact['uid']);
3111
3112                 $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
3113
3114                 DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]);
3115         }
3116
3117         /**
3118          * Update the local relationship when a local user unfollow a contact.
3119          * Removes the contact for sharing-only protocols (feed and mail).
3120          *
3121          * @param array $contact User-specific contact (uid != 0) array
3122          * @throws HTTPException\InternalServerErrorException
3123          */
3124         public static function removeSharer(array $contact)
3125         {
3126                 self::clearFollowerFollowingEndpointCache($contact['uid']);
3127
3128                 if ($contact['rel'] == self::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
3129                         self::remove($contact['id']);
3130                 } else {
3131                         self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]);
3132                 }
3133         }
3134
3135         /**
3136          * Create a birthday event.
3137          *
3138          * Update the year and the birthday.
3139          */
3140         public static function updateBirthdays()
3141         {
3142                 $condition = [
3143                         '`bd` > ?
3144                         AND (`contact`.`rel` = ? OR `contact`.`rel` = ?)
3145                         AND NOT `contact`.`pending`
3146                         AND NOT `contact`.`hidden`
3147                         AND NOT `contact`.`blocked`
3148                         AND NOT `contact`.`archive`
3149                         AND NOT `contact`.`deleted`',
3150                         DBA::NULL_DATE,
3151                         self::SHARING,
3152                         self::FRIEND
3153                 ];
3154
3155                 $contacts = DBA::select('contact', ['id', 'uid', 'name', 'url', 'bd'], $condition);
3156
3157                 while ($contact = DBA::fetch($contacts)) {
3158                         Logger::notice('update_contact_birthday: ' . $contact['bd']);
3159
3160                         $nextbd = DateTimeFormat::utcNow('Y') . substr($contact['bd'], 4);
3161
3162                         if (Event::createBirthday($contact, $nextbd)) {
3163                                 // update bdyear
3164                                 DBA::update(
3165                                         'contact',
3166                                         ['bdyear' => substr($nextbd, 0, 4), 'bd' => $nextbd],
3167                                         ['id' => $contact['id']]
3168                                 );
3169                         }
3170                 }
3171                 DBA::close($contacts);
3172         }
3173
3174         /**
3175          * Remove the unavailable contact ids from the provided list
3176          *
3177          * @param array $contact_ids Contact id list
3178          * @return array
3179          * @throws \Exception
3180          */
3181         public static function pruneUnavailable(array $contact_ids): array
3182         {
3183                 if (empty($contact_ids)) {
3184                         return [];
3185                 }
3186
3187                 $contacts = self::selectToArray(['id'], [
3188                         'id'      => $contact_ids,
3189                         'blocked' => false,
3190                         'pending' => false,
3191                         'archive' => false,
3192                 ]);
3193
3194                 return array_column($contacts, 'id');
3195         }
3196
3197         /**
3198          * Returns a magic link to authenticate remote visitors
3199          *
3200          * @todo  check if the return is either a fully qualified URL or a relative path to Friendica basedir
3201          *
3202          * @param string $contact_url The address of the target contact profile
3203          * @param string $url         An url that we will be redirected to after the authentication
3204          *
3205          * @return string with "redir" link
3206          * @throws HTTPException\InternalServerErrorException
3207          * @throws \ImagickException
3208          */
3209         public static function magicLink(string $contact_url, string $url = ''): string
3210         {
3211                 if (!Session::isAuthenticated()) {
3212                         return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
3213                 }
3214
3215                 $contact = self::getByURL($contact_url, false);
3216                 if (empty($contact)) {
3217                         return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
3218                 }
3219
3220                 // Prevents endless loop in case only a non-public contact exists for the contact URL
3221                 unset($contact['uid']);
3222
3223                 return self::magicLinkByContact($contact, $url ?: $contact_url);
3224         }
3225
3226         /**
3227          * Returns a magic link to authenticate remote visitors
3228          *
3229          * @param integer $cid The contact id of the target contact profile
3230          * @param string  $url An url that we will be redirected to after the authentication
3231          *
3232          * @return string with "redir" link
3233          * @throws HTTPException\InternalServerErrorException
3234          * @throws \ImagickException
3235          */
3236         public static function magicLinkById(int $cid, string $url = ''): string
3237         {
3238                 $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
3239
3240                 return self::magicLinkByContact($contact, $url);
3241         }
3242
3243         /**
3244          * Returns a magic link to authenticate remote visitors
3245          *
3246          * @param array  $contact The contact array with "uid", "network" and "url"
3247          * @param string $url     An url that we will be redirected to after the authentication
3248          *
3249          * @return string with "redir" link
3250          * @throws HTTPException\InternalServerErrorException
3251          * @throws \ImagickException
3252          */
3253         public static function magicLinkByContact(array $contact, string $url = ''): string
3254         {
3255                 $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
3256
3257                 if (!Session::isAuthenticated()) {
3258                         return $destination;
3259                 }
3260
3261                 // Only redirections to the same host do make sense
3262                 if (($url != '') && (parse_url($url, PHP_URL_HOST) != parse_url($contact['url'], PHP_URL_HOST))) {
3263                         return $url;
3264                 }
3265
3266                 if (DI::pConfig()->get(local_user(), 'system', 'stay_local') && ($url == '')) {
3267                         return 'contact/' . $contact['id'] . '/conversations';
3268                 }
3269
3270                 if (!empty($contact['network']) && $contact['network'] != Protocol::DFRN) {
3271                         return $destination;
3272                 }
3273
3274                 if (empty($contact['id'])) {
3275                         return $destination;
3276                 }
3277
3278                 $redirect = 'redir/' . $contact['id'];
3279
3280                 if (($url != '') && !Strings::compareLink($contact['url'], $url)) {
3281                         $redirect .= '?url=' . $url;
3282                 }
3283
3284                 return $redirect;
3285         }
3286
3287         /**
3288          * Is the contact a forum?
3289          *
3290          * @param integer $contactid ID of the contact
3291          *
3292          * @return boolean "true" if it is a forum
3293          */
3294         public static function isForum(int $contactid): bool
3295         {
3296                 $fields = ['contact-type'];
3297                 $condition = ['id' => $contactid];
3298                 $contact = DBA::selectFirst('contact', $fields, $condition);
3299                 if (!DBA::isResult($contact)) {
3300                         return false;
3301                 }
3302
3303                 // Is it a forum?
3304                 return ($contact['contact-type'] == self::TYPE_COMMUNITY);
3305         }
3306
3307         /**
3308          * Can the remote contact receive private messages?
3309          *
3310          * @param array $contact
3311          * @return bool
3312          */
3313         public static function canReceivePrivateMessages(array $contact): bool
3314         {
3315                 $protocol = $contact['network'] ?? $contact['protocol'] ?? Protocol::PHANTOM;
3316                 $self = $contact['self'] ?? false;
3317
3318                 return in_array($protocol, [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB]) && !$self;
3319         }
3320
3321         /**
3322          * Search contact table by nick or name
3323          *
3324          * @param string $search Name or nick
3325          * @param string $mode   Search mode (e.g. "community")
3326          * @param int    $uid    User ID
3327          *
3328          * @return array with search results
3329          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3330          */
3331         public static function searchByName(string $search, string $mode = '', int $uid = 0): array
3332         {
3333                 if (empty($search)) {
3334                         return [];
3335                 }
3336
3337                 // check supported networks
3338                 $networks = [Protocol::DFRN, Protocol::ACTIVITYPUB];
3339                 if (DI::config()->get('system', 'diaspora_enabled')) {
3340                         $networks[] = Protocol::DIASPORA;
3341                 }
3342
3343                 if (!DI::config()->get('system', 'ostatus_disabled')) {
3344                         $networks[] = Protocol::OSTATUS;
3345                 }
3346
3347                 $condition = ['network' => $networks, 'failed' => false, 'deleted' => false, 'uid' => $uid];
3348
3349                 if ($uid == 0) {
3350                         $condition['blocked'] = false;
3351                 }
3352
3353                 // check if we search only communities or every contact
3354                 if ($mode === 'community') {
3355                         $condition['contact-type'] = self::TYPE_COMMUNITY;
3356                 }
3357
3358                 $search .= '%';
3359
3360                 $condition = DBA::mergeConditions($condition,
3361                         ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` WHERE `publish` OR `net-publish`))
3362                         AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]);
3363
3364                 $contacts = self::selectToArray([], $condition);
3365                 return $contacts;
3366         }
3367
3368         /**
3369          * Add public contacts from an array
3370          *
3371          * @param array $urls
3372          * @return array result "count", "added" and "updated"
3373          */
3374         public static function addByUrls(array $urls): array
3375         {
3376                 $added = 0;
3377                 $updated = 0;
3378                 $unchanged = 0;
3379                 $count = 0;
3380
3381                 foreach ($urls as $url) {
3382                         if (empty($url) || !is_string($url)) {
3383                                 continue;
3384                         }
3385                         $contact = self::getByURL($url, false, ['id', 'updated']);
3386                         if (empty($contact['id']) && Network::isValidHttpUrl($url)) {
3387                                 Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
3388                                 ++$added;
3389                         } elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {
3390                                 Worker::add(PRIORITY_LOW, 'UpdateContact', $contact['id']);
3391                                 ++$updated;
3392                         } else {
3393                                 ++$unchanged;
3394                         }
3395                         ++$count;
3396                 }
3397
3398                 return ['count' => $count, 'added' => $added, 'updated' => $updated, 'unchanged' => $unchanged];
3399         }
3400
3401         /**
3402          * Returns a random, global contact array of the current node
3403          *
3404          * @return array The profile array
3405          * @throws Exception
3406          */
3407         public static function getRandomContact(): array
3408         {
3409                 $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], [
3410                         "`uid` = ? AND `network` = ? AND NOT `failed` AND `last-item` > ?",
3411                         0, Protocol::DFRN, DateTimeFormat::utc('now - 1 month'),
3412                 ], ['order' => ['RAND()']]);
3413
3414                 if (DBA::isResult($contact)) {
3415                         return $contact;
3416                 }
3417
3418                 return [];
3419         }
3420 }