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