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