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