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