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