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