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