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