Replace "q" calls
[friendica.git/.git] / src / Core / NotificationsManager.php
1 <?php
2 /**
3  * @file src/Core/NotificationsManager.php
4  * @brief Methods for read and write notifications from/to database
5  *  or for formatting notifications
6  */
7 namespace Friendica\Core;
8
9 use Friendica\BaseObject;
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Content\Text\HTML;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Item;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Proxy as ProxyUtils;
17 use Friendica\Util\Temporal;
18 use Friendica\Util\XML;
19
20 /**
21  * @brief Methods for read and write notifications from/to database
22  *  or for formatting notifications
23  */
24 class NotificationsManager extends BaseObject
25 {
26         /**
27          * @brief set some extra note properties
28          *
29          * @param array $notes array of note arrays from db
30          * @return array Copy of input array with added properties
31          *
32          * Set some extra properties to note array from db:
33          *  - timestamp as int in default TZ
34          *  - date_rel : relative date string
35          *  - msg_html: message as html string
36          *  - msg_plain: message as plain text string
37          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38          */
39         private function _set_extra($notes)
40         {
41                 $rets = [];
42                 foreach ($notes as $n) {
43                         $local_time = DateTimeFormat::local($n['date']);
44                         $n['timestamp'] = strtotime($local_time);
45                         $n['date_rel'] = Temporal::getRelativeDate($n['date']);
46                         $n['msg_html'] = BBCode::convert($n['msg'], false);
47                         $n['msg_plain'] = explode("\n", trim(HTML::toPlaintext($n['msg_html'], 0)))[0];
48
49                         $rets[] = $n;
50                 }
51                 return $rets;
52         }
53
54         /**
55          * @brief Get all notifications for local_user()
56          *
57          * @param array  $filter optional Array "column name"=>value: filter query by columns values
58          * @param array  $order  optional Array to order by
59          * @param string $limit  optional Query limits
60          *
61          * @return array|bool of results or false on errors
62          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
63          */
64         public function getAll($filter = [], $order = ['date' => 'DESC'], $limit = "")
65         {
66                 $params = [];
67
68                 $params['order'] = $order;
69
70                 if (!empty($limit)) {
71                         $params['limit'] = $limit;
72                 }
73
74                 $dbFilter = array_merge($filter, ['uid' => local_user()]);
75
76                 $r = DBA::select('notify', [], $dbFilter, $order, $params);
77
78                 if (DBA::isResult($r)) {
79                         return $this->_set_extra($r);
80                 }
81
82                 return false;
83         }
84
85         /**
86          * @brief Get one note for local_user() by $id value
87          *
88          * @param int $id identity
89          * @return array note values or null if not found
90          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
91          */
92         public function getByID($id)
93         {
94                 $r = DBA::selectFirst('notify', ['id' => $id, 'uid' => local_user()]);
95                 if (DBA::isResult($r)) {
96                         return $this->_set_extra($r)[0];
97                 }
98                 return null;
99         }
100
101         /**
102          * @brief set seen state of $note of local_user()
103          *
104          * @param array $note note array
105          * @param bool  $seen optional true or false, default true
106          * @return bool true on success, false on errors
107          * @throws \Exception
108          */
109         public function setSeen($note, $seen = true)
110         {
111                 return DBA::update('notify', ['seen' => $seen], ['link' => $note['link'], 'parent' => $note['parent'], 'otype' => $note['otype'], 'uid' => local_user()]);
112         }
113
114         /**
115          * @brief set seen state of all notifications of local_user()
116          *
117          * @param bool $seen optional true or false. default true
118          * @return bool true on success, false on error
119          * @throws \Exception
120          */
121         public function setAllSeen($seen = true)
122         {
123                 return DBA::update('notify', ['seen' => $seen], ['uid' => local_user()]);
124         }
125
126         /**
127          * @brief List of pages for the Notifications TabBar
128          *
129          * @return array with with notifications TabBar data
130          * @throws \Exception
131          */
132         public function getTabs()
133         {
134                 $selected = defaults(self::getApp()->argv, 1, '');
135
136                 $tabs = [
137                         [
138                                 'label' => L10n::t('System'),
139                                 'url'   => 'notifications/system',
140                                 'sel'   => (($selected == 'system') ? 'active' : ''),
141                                 'id'    => 'system-tab',
142                                 'accesskey' => 'y',
143                         ],
144                         [
145                                 'label' => L10n::t('Network'),
146                                 'url'   => 'notifications/network',
147                                 'sel'   => (($selected == 'network') ? 'active' : ''),
148                                 'id'    => 'network-tab',
149                                 'accesskey' => 'w',
150                         ],
151                         [
152                                 'label' => L10n::t('Personal'),
153                                 'url'   => 'notifications/personal',
154                                 'sel'   => (($selected == 'personal') ? 'active' : ''),
155                                 'id'    => 'personal-tab',
156                                 'accesskey' => 'r',
157                         ],
158                         [
159                                 'label' => L10n::t('Home'),
160                                 'url'   => 'notifications/home',
161                                 'sel'   => (($selected == 'home') ? 'active' : ''),
162                                 'id'    => 'home-tab',
163                                 'accesskey' => 'h',
164                         ],
165                         [
166                                 'label' => L10n::t('Introductions'),
167                                 'url'   => 'notifications/intros',
168                                 'sel'   => (($selected == 'intros') ? 'active' : ''),
169                                 'id'    => 'intro-tab',
170                                 'accesskey' => 'i',
171                         ],
172                 ];
173
174                 return $tabs;
175         }
176
177         /**
178          * @brief Format the notification query in an usable array
179          *
180          * @param array  $notifs The array from the db query
181          * @param string $ident  The notifications identifier (e.g. network)
182          * @return array
183          *                       string 'label' => The type of the notification
184          *                       string 'link' => URL to the source
185          *                       string 'image' => The avatar image
186          *                       string 'url' => The profile url of the contact
187          *                       string 'text' => The notification text
188          *                       string 'when' => The date of the notification
189          *                       string 'ago' => T relative date of the notification
190          *                       bool 'seen' => Is the notification marked as "seen"
191          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
192          */
193         private function formatNotifs(array $notifs, $ident = "")
194         {
195                 $arr = [];
196
197                 if (DBA::isResult($notifs)) {
198                         foreach ($notifs as $it) {
199                                 // Because we use different db tables for the notification query
200                                 // we have sometimes $it['unseen'] and sometimes $it['seen].
201                                 // So we will have to transform $it['unseen']
202                                 if (array_key_exists('unseen', $it)) {
203                                         $it['seen'] = ($it['unseen'] > 0 ? false : true);
204                                 }
205
206                                 // For feed items we use the user's contact, since the avatar is mostly self choosen.
207                                 if (!empty($it['network']) && $it['network'] == Protocol::FEED) {
208                                         $it['author-avatar'] = $it['contact-avatar'];
209                                 }
210
211                                 // Depending on the identifier of the notification we need to use different defaults
212                                 switch ($ident) {
213                                         case 'system':
214                                                 $default_item_label = 'notify';
215                                                 $default_item_link = System::baseUrl(true) . '/notify/view/' . $it['id'];
216                                                 $default_item_image = ProxyUtils::proxifyUrl($it['photo'], false, ProxyUtils::SIZE_MICRO);
217                                                 $default_item_url = $it['url'];
218                                                 $default_item_text = strip_tags(BBCode::convert($it['msg']));
219                                                 $default_item_when = DateTimeFormat::local($it['date'], 'r');
220                                                 $default_item_ago = Temporal::getRelativeDate($it['date']);
221                                                 break;
222
223                                         case 'home':
224                                                 $default_item_label = 'comment';
225                                                 $default_item_link = System::baseUrl(true) . '/display/' . $it['parent-guid'];
226                                                 $default_item_image = ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO);
227                                                 $default_item_url = $it['author-link'];
228                                                 $default_item_text = L10n::t("%s commented on %s's post", $it['author-name'], $it['parent-author-name']);
229                                                 $default_item_when = DateTimeFormat::local($it['created'], 'r');
230                                                 $default_item_ago = Temporal::getRelativeDate($it['created']);
231                                                 break;
232
233                                         default:
234                                                 $default_item_label = (($it['id'] == $it['parent']) ? 'post' : 'comment');
235                                                 $default_item_link = System::baseUrl(true) . '/display/' . $it['parent-guid'];
236                                                 $default_item_image = ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO);
237                                                 $default_item_url = $it['author-link'];
238                                                 $default_item_text = (($it['id'] == $it['parent'])
239                                                                         ? L10n::t("%s created a new post", $it['author-name'])
240                                                                         : L10n::t("%s commented on %s's post", $it['author-name'], $it['parent-author-name']));
241                                                 $default_item_when = DateTimeFormat::local($it['created'], 'r');
242                                                 $default_item_ago = Temporal::getRelativeDate($it['created']);
243                                 }
244
245                                 // Transform the different types of notification in an usable array
246                                 switch ($it['verb']) {
247                                         case ACTIVITY_LIKE:
248                                                 $notif = [
249                                                         'label' => 'like',
250                                                         'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
251                                                         'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
252                                                         'url' => $it['author-link'],
253                                                         'text' => L10n::t("%s liked %s's post", $it['author-name'], $it['parent-author-name']),
254                                                         'when' => $default_item_when,
255                                                         'ago' => $default_item_ago,
256                                                         'seen' => $it['seen']
257                                                 ];
258                                                 break;
259
260                                         case ACTIVITY_DISLIKE:
261                                                 $notif = [
262                                                         'label' => 'dislike',
263                                                         'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
264                                                         'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
265                                                         'url' => $it['author-link'],
266                                                         'text' => L10n::t("%s disliked %s's post", $it['author-name'], $it['parent-author-name']),
267                                                         'when' => $default_item_when,
268                                                         'ago' => $default_item_ago,
269                                                         'seen' => $it['seen']
270                                                 ];
271                                                 break;
272
273                                         case ACTIVITY_ATTEND:
274                                                 $notif = [
275                                                         'label' => 'attend',
276                                                         'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
277                                                         'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
278                                                         'url' => $it['author-link'],
279                                                         'text' => L10n::t("%s is attending %s's event", $it['author-name'], $it['parent-author-name']),
280                                                         'when' => $default_item_when,
281                                                         'ago' => $default_item_ago,
282                                                         'seen' => $it['seen']
283                                                 ];
284                                                 break;
285
286                                         case ACTIVITY_ATTENDNO:
287                                                 $notif = [
288                                                         'label' => 'attendno',
289                                                         'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
290                                                         'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
291                                                         'url' => $it['author-link'],
292                                                         'text' => L10n::t("%s is not attending %s's event", $it['author-name'], $it['parent-author-name']),
293                                                         'when' => $default_item_when,
294                                                         'ago' => $default_item_ago,
295                                                         'seen' => $it['seen']
296                                                 ];
297                                                 break;
298
299                                         case ACTIVITY_ATTENDMAYBE:
300                                                 $notif = [
301                                                         'label' => 'attendmaybe',
302                                                         'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
303                                                         'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
304                                                         'url' => $it['author-link'],
305                                                         'text' => L10n::t("%s may attend %s's event", $it['author-name'], $it['parent-author-name']),
306                                                         'when' => $default_item_when,
307                                                         'ago' => $default_item_ago,
308                                                         'seen' => $it['seen']
309                                                 ];
310                                                 break;
311
312                                         case ACTIVITY_FRIEND:
313                                                 if (!isset($it['object'])) {
314                                                         $notif = [
315                                                                 'label' => 'friend',
316                                                                 'link' => $default_item_link,
317                                                                 'image' => $default_item_image,
318                                                                 'url' => $default_item_url,
319                                                                 'text' => $default_item_text,
320                                                                 'when' => $default_item_when,
321                                                                 'ago' => $default_item_ago,
322                                                                 'seen' => $it['seen']
323                                                         ];
324                                                         break;
325                                                 }
326                                                 /// @todo Check if this part here is used at all
327                                                 Logger::log('Complete data: ' . json_encode($it) . ' - ' . System::callstack(20), Logger::DEBUG);
328
329                                                 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
330                                                 $obj = XML::parseString($xmlhead . $it['object']);
331                                                 $it['fname'] = $obj->title;
332
333                                                 $notif = [
334                                                         'label' => 'friend',
335                                                         'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
336                                                         'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
337                                                         'url' => $it['author-link'],
338                                                         'text' => L10n::t("%s is now friends with %s", $it['author-name'], $it['fname']),
339                                                         'when' => $default_item_when,
340                                                         'ago' => $default_item_ago,
341                                                         'seen' => $it['seen']
342                                                 ];
343                                                 break;
344
345                                         default:
346                                                 $notif = [
347                                                         'label' => $default_item_label,
348                                                         'link' => $default_item_link,
349                                                         'image' => $default_item_image,
350                                                         'url' => $default_item_url,
351                                                         'text' => $default_item_text,
352                                                         'when' => $default_item_when,
353                                                         'ago' => $default_item_ago,
354                                                         'seen' => $it['seen']
355                                                 ];
356                                 }
357
358                                 $arr[] = $notif;
359                         }
360                 }
361
362                 return $arr;
363         }
364
365         /**
366          * @brief Get network notifications
367          *
368          * @param int|string $seen    If 0 only include notifications into the query
369          *                            which aren't marked as "seen"
370          * @param int        $start   Start the query at this point
371          * @param int        $limit   Maximum number of query results
372          *
373          * @return array with
374          *    string 'ident' => Notification identifier
375          *    array 'notifications' => Network notifications
376          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
377          */
378         public function networkNotifs($seen = 0, $start = 0, $limit = 80)
379         {
380                 $ident = 'network';
381                 $notifs = [];
382
383                 $condition = ['wall' => false, 'uid' => local_user()];
384
385                 if ($seen === 0) {
386                         $condition['unseen'] = true;
387                 }
388
389                 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
390                         'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
391                 $params = ['order' => ['created' => true], 'limit' => [$start, $limit]];
392
393                 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
394
395                 if (DBA::isResult($items)) {
396                         $notifs = $this->formatNotifs(Item::inArray($items), $ident);
397                 }
398
399                 $arr = [
400                         'notifications' => $notifs,
401                         'ident' => $ident,
402                 ];
403
404                 return $arr;
405         }
406
407         /**
408          * @brief Get system notifications
409          *
410          * @param int|string $seen    If 0 only include notifications into the query
411          *                            which aren't marked as "seen"
412          * @param int        $start   Start the query at this point
413          * @param int        $limit   Maximum number of query results
414          *
415          * @return array with
416          *    string 'ident' => Notification identifier
417          *    array 'notifications' => System notifications
418          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
419          */
420         public function systemNotifs($seen = 0, $start = 0, $limit = 80)
421         {
422                 $ident = 'system';
423                 $notifs = [];
424                 $sql_seen = "";
425
426                 if ($seen === 0) {
427                         $filter = ['`uid` = ? AND NOT `seen`', local_user()];
428                 } else {
429                         $filter = ['uid' => local_user()];
430                 }
431
432                 $params = [];
433                 $params['limit'] = [$start, $limit];
434
435                 $r = DBA::select('notify',
436                         ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'],
437                         $filter,
438                         $params);
439
440                 if (DBA::isResult($r)) {
441                         $notifs = $this->formatNotifs(DBA::toArray($r), $ident);
442                 }
443
444                 $arr = [
445                         'notifications' => $notifs,
446                         'ident' => $ident,
447                 ];
448
449                 return $arr;
450         }
451
452         /**
453          * @brief Get personal notifications
454          *
455          * @param int|string $seen    If 0 only include notifications into the query
456          *                            which aren't marked as "seen"
457          * @param int        $start   Start the query at this point
458          * @param int        $limit   Maximum number of query results
459          *
460          * @return array with
461          *    string 'ident' => Notification identifier
462          *    array 'notifications' => Personal notifications
463          * @throws \Exception
464          */
465         public function personalNotifs($seen = 0, $start = 0, $limit = 80)
466         {
467                 $ident = 'personal';
468                 $notifs = [];
469
470                 $myurl = str_replace('http://', '', self::getApp()->contact['nurl']);
471                 $diasp_url = str_replace('/profile/', '/u/', $myurl);
472
473                 $condition = ["NOT `wall` AND `uid` = ? AND (`item`.`author-id` = ? OR `item`.`tag` REGEXP ? OR `item`.`tag` REGEXP ?)",
474                         local_user(), public_contact(), $myurl . '\\]', $diasp_url . '\\]'];
475
476                 if ($seen === 0) {
477                         $condition[0] .= " AND `unseen`";
478                 }
479
480                 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
481                         'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
482                 $params = ['order' => ['created' => true], 'limit' => [$start, $limit]];
483
484                 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
485
486                 if (DBA::isResult($items)) {
487                         $notifs = $this->formatNotifs(Item::inArray($items), $ident);
488                 }
489
490                 $arr = [
491                         'notifications' => $notifs,
492                         'ident' => $ident,
493                 ];
494
495                 return $arr;
496         }
497
498         /**
499          * @brief Get home notifications
500          *
501          * @param int|string $seen    If 0 only include notifications into the query
502          *                            which aren't marked as "seen"
503          * @param int        $start   Start the query at this point
504          * @param int        $limit   Maximum number of query results
505          *
506          * @return array with
507          *    string 'ident' => Notification identifier
508          *    array 'notifications' => Home notifications
509          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
510          */
511         public function homeNotifs($seen = 0, $start = 0, $limit = 80)
512         {
513                 $ident = 'home';
514                 $notifs = [];
515
516                 $condition = ['wall' => true, 'uid' => local_user()];
517
518                 if ($seen === 0) {
519                         $condition['unseen'] = true;
520                 }
521
522                 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
523                         'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
524                 $params = ['order' => ['created' => true], 'limit' => [$start, $limit]];
525                 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
526
527                 if (DBA::isResult($items)) {
528                         $notifs = $this->formatNotifs(Item::inArray($items), $ident);
529                 }
530
531                 $arr = [
532                         'notifications' => $notifs,
533                         'ident' => $ident,
534                 ];
535
536                 return $arr;
537         }
538
539         /**
540          * @brief Get introductions
541          *
542          * @param bool $all     If false only include introductions into the query
543          *                      which aren't marked as ignored
544          * @param int  $start   Start the query at this point
545          * @param int  $limit   Maximum number of query results
546          *
547          * @return array with
548          *    string 'ident' => Notification identifier
549          *    array 'notifications' => Introductions
550          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
551          * @throws \ImagickException
552          */
553         public function introNotifs($all = false, $start = 0, $limit = 80)
554         {
555                 $ident = 'introductions';
556                 $notifs = [];
557                 $sql_extra = "";
558
559                 if (!$all) {
560                         $sql_extra = " AND `ignore` = 0 ";
561                 }
562
563                 /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
564                 $r = DBA::p(
565                         "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
566                                 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
567                                 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
568                                 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
569                                 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
570                                 `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
571                         FROM `intro`
572                                 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
573                                 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
574                                 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
575                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0
576                         LIMIT %d, %d",
577                         intval($_SESSION['uid']),
578                         intval($start),
579                         intval($limit)
580                 );
581                 if (DBA::isResult($r)) {
582                         $notifs = $this->formatIntros(DBA::toArray($r));
583                 }
584
585                 $arr = [
586                         'ident' => $ident,
587                         'notifications' => $notifs,
588                 ];
589
590                 return $arr;
591         }
592
593         /**
594          * @brief Format the notification query in an usable array
595          *
596          * @param array $intros The array from the db query
597          * @return array with the introductions
598          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
599          * @throws \ImagickException
600          */
601         private function formatIntros($intros)
602         {
603                 $knowyou = '';
604
605                 $arr = [];
606
607                 foreach ($intros as $it) {
608                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
609                         // We have to distinguish between these two because they use different data.
610                         // Contact suggestions
611                         if ($it['fid']) {
612                                 $return_addr = bin2hex(self::getApp()->user['nickname'] . '@' . self::getApp()->getHostName() . ((self::getApp()->getURLPath()) ? '/' . self::getApp()->getURLPath() : ''));
613
614                                 $intro = [
615                                         'label' => 'friend_suggestion',
616                                         'notify_type' => L10n::t('Friend Suggestion'),
617                                         'intro_id' => $it['intro_id'],
618                                         'madeby' => $it['name'],
619                                         'madeby_url' => $it['url'],
620                                         'madeby_zrl' => Contact::magicLink($it['url']),
621                                         'madeby_addr' => $it['addr'],
622                                         'contact_id' => $it['contact-id'],
623                                         'photo' => (!empty($it['fphoto']) ? ProxyUtils::proxifyUrl($it['fphoto'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
624                                         'name' => $it['fname'],
625                                         'url' => $it['furl'],
626                                         'zrl' => Contact::magicLink($it['furl']),
627                                         'hidden' => $it['hidden'] == 1,
628                                         'post_newfriend' => (intval(PConfig::get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
629                                         'knowyou' => $knowyou,
630                                         'note' => $it['note'],
631                                         'request' => $it['frequest'] . '?addr=' . $return_addr,
632                                 ];
633
634                                 // Normal connection requests
635                         } else {
636                                 $it = $this->getMissingIntroData($it);
637
638                                 if (empty($it['url'])) {
639                                         continue;
640                                 }
641
642                                 // Don't show these data until you are connected. Diaspora is doing the same.
643                                 if ($it['gnetwork'] === Protocol::DIASPORA) {
644                                         $it['glocation'] = "";
645                                         $it['gabout'] = "";
646                                         $it['ggender'] = "";
647                                 }
648                                 $intro = [
649                                         'label' => (($it['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
650                                         'notify_type' => (($it['network'] !== Protocol::OSTATUS) ? L10n::t('Friend/Connect Request') : L10n::t('New Follower')),
651                                         'dfrn_id' => $it['issued-id'],
652                                         'uid' => $_SESSION['uid'],
653                                         'intro_id' => $it['intro_id'],
654                                         'contact_id' => $it['contact-id'],
655                                         'photo' => (!empty($it['photo']) ? ProxyUtils::proxifyUrl($it['photo'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
656                                         'name' => $it['name'],
657                                         'location' => BBCode::convert($it['glocation'], false),
658                                         'about' => BBCode::convert($it['gabout'], false),
659                                         'keywords' => $it['gkeywords'],
660                                         'gender' => $it['ggender'],
661                                         'hidden' => $it['hidden'] == 1,
662                                         'post_newfriend' => (intval(PConfig::get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
663                                         'url' => $it['url'],
664                                         'zrl' => Contact::magicLink($it['url']),
665                                         'addr' => $it['gaddr'],
666                                         'network' => $it['gnetwork'],
667                                         'knowyou' => $it['knowyou'],
668                                         'note' => $it['note'],
669                                 ];
670                         }
671
672                         $arr[] = $intro;
673                 }
674
675                 return $arr;
676         }
677
678         /**
679          * @brief Check for missing contact data and try to fetch the data from
680          *     from other sources
681          *
682          * @param array $arr The input array with the intro data
683          *
684          * @return array The array with the intro data
685          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
686          */
687         private function getMissingIntroData($arr)
688         {
689                 // If the network and the addr isn't available from the gcontact
690                 // table entry, take the one of the contact table entry
691                 if (empty($arr['gnetwork']) && !empty($arr['network'])) {
692                         $arr['gnetwork'] = $arr['network'];
693                 }
694                 if (empty($arr['gaddr']) && !empty($arr['addr'])) {
695                         $arr['gaddr'] = $arr['addr'];
696                 }
697
698                 // If the network and addr is still not available
699                 // get the missing data data from other sources
700                 if (empty($arr['gnetwork']) || empty($arr['gaddr'])) {
701                         $ret = Contact::getDetailsByURL($arr['url']);
702
703                         if (empty($arr['gnetwork']) && !empty($ret['network'])) {
704                                 $arr['gnetwork'] = $ret['network'];
705                         }
706                         if (empty($arr['gaddr']) && !empty($ret['addr'])) {
707                                 $arr['gaddr'] = $ret['addr'];
708                         }
709                 }
710
711                 return $arr;
712         }
713 }