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