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