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