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