]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
Merge branch 'master', remote-tracking branch 'remotes/upstream/master'
[friendica.git] / mod / notifications.php
1 <?php
2
3 function notifications_post(&$a) {
4
5         if(! local_user()) {
6                 goaway(z_root());
7         }
8         
9         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
10         
11         if($request_id === "all")
12                 return;
13
14         if($request_id) {
15
16                 $r = q("SELECT * FROM `intro` WHERE `id` = %d  AND `uid` = %d LIMIT 1",
17                         intval($request_id),
18                         intval(local_user())
19                 );
20         
21                 if(count($r)) {
22                         $intro_id = $r[0]['id'];
23                         $contact_id = $r[0]['contact-id'];
24                 }
25                 else {
26                         notice( t('Invalid request identifier.') . EOL);
27                         return;
28                 }
29
30                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
31                 // that should not be deleted.
32
33                 $fid = $r[0]['fid'];
34
35                 if($_POST['submit'] == t('Discard')) {
36                         $r = q("DELETE FROM `intro` WHERE `id` = %d LIMIT 1", 
37                                 intval($intro_id)
38                         );      
39                         if(! $fid) {
40                                 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1", 
41                                         intval($contact_id),
42                                         intval(local_user())
43                                 );
44                         }
45                         goaway($a->get_baseurl() . '/notifications/intros');
46                 }
47                 if($_POST['submit'] == t('Ignore')) {
48                         $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d LIMIT 1",
49                                 intval($intro_id));
50                         goaway($a->get_baseurl() . '/notifications/intros');
51                 }
52         }
53 }
54
55
56
57
58
59 function notifications_content(&$a) {
60
61         if(! local_user()) {
62                 notice( t('Permission denied.') . EOL);
63                 return;
64         }
65
66         nav_set_selected('notifications');              
67
68         $o = '';
69         $tabs = array(
70                 array(
71                         'label' => t('System'),
72                         'url'=>$a->get_baseurl() . '/notifications/system',
73                         'sel'=> (($a->argv[1] == 'system') ? 'active' : ''),
74                 ),
75                 array(
76                         'label' => t('Network'),
77                         'url'=>$a->get_baseurl() . '/notifications/network',
78                         'sel'=> (($a->argv[1] == 'network') ? 'active' : ''),
79                 ),
80                 array(
81                         'label' => t('Personal'),
82                         'url'=>$a->get_baseurl() . '/notifications/personal',
83                         'sel'=> (($a->argv[1] == 'personal') ? 'active' : ''),
84                 ),
85                 array(
86                         'label' => t('Home'),
87                         'url' => $a->get_baseurl() . '/notifications/home',
88                         'sel'=> (($a->argv[1] == 'home') ? 'active' : ''),
89                 ),
90                 array(
91                         'label' => t('Introductions'),
92                         'url' => $a->get_baseurl() . '/notifications/intros',
93                         'sel'=> (($a->argv[1] == 'intros') ? 'active' : ''),
94                 ),
95                 array(
96                         'label' => t('Messages'),
97                         'url' => $a->get_baseurl() . '/message',
98                         'sel'=> '',
99                 ),
100         );
101         
102         $o = "";
103
104         
105         if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
106                 nav_set_selected('introductions');
107                 if(($a->argc > 2) && ($a->argv[2] == 'all'))
108                         $sql_extra = '';
109                 else
110                         $sql_extra = " AND `ignore` = 0 ";
111                 
112                 $notif_tpl = get_markup_template('notifications.tpl');
113                 
114                 $notif_content .= '<a href="' . ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ) . '" id="notifications-show-hide-link" >'
115                         . ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '</a></div>' . "\r\n";
116
117                 $r = q("SELECT COUNT(*) AS `total` FROM `intro` 
118                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
119                                 intval($_SESSION['uid'])
120                 );
121                 if($r && count($r)) {
122                         $a->set_pager_total($r[0]['total']);
123                         $a->set_pager_itemspage(20);
124                 }
125
126                 $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`
127                         FROM `intro` LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
128                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
129                                 intval($_SESSION['uid']));
130
131                 if(($r !== false) && (count($r))) {
132
133                         $sugg = get_markup_template('suggestions.tpl');
134                         $tpl = get_markup_template("intros.tpl");
135
136                         foreach($r as $rr) {
137                                 if($rr['fid']) {
138
139                                         $return_addr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname() . (($a->path) ? '/' . $a->path : ''));
140                                         $notif_content .= replace_macros($sugg,array(
141                                                 '$str_notifytype' => t('Notification type: '),
142                                                 '$notify_type' => t('Friend Suggestion'),
143                                                 '$intro_id' => $rr['intro_id'],
144                                                 '$madeby' => sprintf( t('suggested by %s'),$rr['name']),
145                                                 '$contact_id' => $rr['contact-id'],
146                                                 '$photo' => ((x($rr,'fphoto')) ? $rr['fphoto'] : "images/default-profile.jpg"),
147                                                 '$fullname' => $rr['fname'],
148                                                 '$url' => $rr['furl'],
149                                                 '$hidden' => array('hidden', t('Hide this contact from others'), ($rr['hidden'] == 1), ''),
150                                                 '$activity' => array('activity', t('Post a new friend activity'), 1, t('if applicable')),
151
152                                                 '$knowyou' => $knowyou,
153                                                 '$approve' => t('Approve'),
154                                                 '$note' => $rr['note'],
155                                                 '$request' => $rr['frequest'] . '?addr=' . $return_addr,
156                                                 '$ignore' => t('Ignore'),
157                                                 '$discard' => t('Discard')
158
159                                         ));
160
161                                         continue;
162
163                                 }
164                                 $friend_selected = (($rr['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
165                                 $fan_selected = (($rr['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
166                                 $dfrn_tpl = get_markup_template('netfriend.tpl');
167
168                                 $knowyou   = '';
169                                 $dfrn_text = '';
170
171                                 if($rr['network'] === NETWORK_DFRN || $rr['network'] === NETWORK_DIASPORA) {
172                                         if($rr['network'] === NETWORK_DFRN)
173                                                 $knowyou = t('Claims to be known to you: ') . (($rr['knowyou']) ? t('yes') : t('no'));
174                                         else
175                                                 $knowyou = '';
176                                         $dfrn_text = replace_macros($dfrn_tpl,array(
177                                                 '$intro_id' => $rr['intro_id'],
178                                                 '$friend_selected' => $friend_selected,
179                                                 '$fan_selected' => $fan_selected,
180                                                 '$approve_as' => t('Approve as: '),
181                                                 '$as_friend' => t('Friend'),
182                                                 '$as_fan' => (($rr['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Fan/Admirer'))
183                                         ));
184                                 }                       
185
186                                 $notif_content .= replace_macros($tpl,array(
187                                         '$str_notifytype' => t('Notification type: '),
188                                         '$notify_type' => (($rr['network'] !== NETWORK_OSTATUS) ? t('Friend/Connect Request') : t('New Follower')),
189                                         '$dfrn_text' => $dfrn_text,     
190                                         '$dfrn_id' => $rr['issued-id'],
191                                         '$uid' => $_SESSION['uid'],
192                                         '$intro_id' => $rr['intro_id'],
193                                         '$contact_id' => $rr['contact-id'],
194                                         '$photo' => ((x($rr,'photo')) ? $rr['photo'] : "images/default-profile.jpg"),
195                                         '$fullname' => $rr['name'],
196                                         '$hidden' => array('hidden', t('Hide this contact from others'), ($rr['hidden'] == 1), ''),
197                                         '$activity' => array('activity', t('Post a new friend activity'), 1, t('if applicable')),
198                                         '$url' => $rr['url'],
199                                         '$knowyou' => $knowyou,
200                                         '$approve' => t('Approve'),
201                                         '$note' => $rr['note'],
202                                         '$ignore' => t('Ignore'),
203                                         '$discard' => t('Discard')
204
205                                 ));
206                         }
207                 }
208                 else
209                         info( t('No introductions.') . EOL);
210                 
211                 $o .= replace_macros($notif_tpl,array(
212                         '$notif_header' => t('Notifications'),
213                         '$tabs' => $tabs,
214                         '$notif_content' => $notif_content,
215                 ));
216                 
217                 $o .= paginate($a);
218                 return $o;
219                                 
220         } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
221                 
222                 $notif_tpl = get_markup_template('notifications.tpl');
223                 
224                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
225                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
226                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
227                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
228                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
229                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
230                         intval(local_user())
231                 );
232                 
233                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
234                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
235                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
236                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
237                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
238                 
239                 $notif_content = '';
240                 
241                 if (count($r) > 0) {
242                         
243                         foreach ($r as $it) {
244                                 switch($it['verb']){
245                                         case ACTIVITY_LIKE:
246                                                 $notif_content .= replace_macros($tpl_item_likes,array(
247                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
248                                                         '$item_image' => $it['author-avatar'],
249                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
250                                                         '$item_when' => relative_date($it['created'])
251                                                 ));
252                                                 break;
253                                                 
254                                         case ACTIVITY_DISLIKE:
255                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
256                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
257                                                         '$item_image' => $it['author-avatar'],
258                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
259                                                         '$item_when' => relative_date($it['created'])
260                                                 ));
261                                                 break;
262                                                 
263                                         case ACTIVITY_FRIEND:
264                                         
265                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
266                                                 $obj = parse_xml_string($xmlhead.$it['object']);
267                                                 $it['fname'] = $obj->title;
268                                                 
269                                                 $notif_content .= replace_macros($tpl_item_friends,array(
270                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
271                                                         '$item_image' => $it['author-avatar'],
272                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
273                                                         '$item_when' => relative_date($it['created'])
274                                                 ));
275                                                 break;
276                                                 
277                                         default:
278                                                 $item_text = (($it['id'] == $it['parent'])
279                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
280                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
281                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
282
283                                                 $notif_content .= replace_macros($tpl,array(
284                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
285                                                         '$item_image' => $it['author-avatar'],
286                                                         '$item_text' => $item_text,
287                                                         '$item_when' => relative_date($it['created'])
288                                                 ));
289                                 }
290                         }
291                         
292                 } else {
293                         
294                         $notif_content = t('No more network notifications.');
295                 }
296                 
297                 $o .= replace_macros($notif_tpl,array(
298                         '$notif_header' => t('Network Notifications'),
299                         '$tabs' => $tabs,
300                         '$notif_content' => $notif_content,
301                 ));
302                 
303         } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
304                 
305                 $notif_tpl = get_markup_template('notifications.tpl');
306                 
307                 $not_tpl = get_markup_template('notify.tpl');
308                 require_once('include/bbcode.php');
309
310                 $r = q("SELECT * from notify where uid = %d and seen = 0 order by date desc",
311                         intval(local_user())
312                 );
313                 
314                 if (count($r) > 0) {
315                         foreach ($r as $it) {
316                                 $notif_content .= replace_macros($not_tpl,array(
317                                         '$item_link' => $a->get_baseurl().'/notify/view/'. $it['id'],
318                                         '$item_image' => $it['photo'],
319                                         '$item_text' => strip_tags(bbcode($it['msg'])),
320                                         '$item_when' => relative_date($it['date'])
321                                 ));
322                         }
323                 } else {
324                         $notif_content .= t('No more system notifications.');
325                 }
326                 
327                 $o .= replace_macros($notif_tpl,array(
328                         '$notif_header' => t('System Notifications'),
329                         '$tabs' => $tabs,
330                         '$notif_content' => $notif_content,
331                 ));
332
333         } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
334                 
335                 $notif_tpl = get_markup_template('notifications.tpl');
336                 
337                 $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
338                 $myurl = substr($myurl,strpos($myurl,'://')+3);
339                 $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
340                 $diasp_url = str_replace('/profile/','/u/',$myurl);
341                 $sql_extra .= sprintf(" AND ( `item`.`author-link` regexp '%s' or `item`.`tag` regexp '%s' or `item`.`tag` regexp '%s' ) ",
342                         dbesc($myurl . '$'),
343                         dbesc($myurl . '\\]'),
344                         dbesc($diasp_url . '\\]')
345                 );
346
347
348                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
349                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
350                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
351                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
352                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 
353                                 $sql_extra
354                                 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
355                         intval(local_user())
356                 );
357                 
358                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
359                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
360                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
361                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
362                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
363                 
364                 $notif_content = '';
365                 
366                 if (count($r) > 0) {
367                         
368                         foreach ($r as $it) {
369                                 switch($it['verb']){
370                                         case ACTIVITY_LIKE:
371                                                 $notif_content .= replace_macros($tpl_item_likes,array(
372                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
373                                                         '$item_image' => $it['author-avatar'],
374                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
375                                                         '$item_when' => relative_date($it['created'])
376                                                 ));
377                                                 break;
378                                                 
379                                         case ACTIVITY_DISLIKE:
380                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
381                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
382                                                         '$item_image' => $it['author-avatar'],
383                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
384                                                         '$item_when' => relative_date($it['created'])
385                                                 ));
386                                                 break;
387                                                 
388                                         case ACTIVITY_FRIEND:
389                                         
390                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
391                                                 $obj = parse_xml_string($xmlhead.$it['object']);
392                                                 $it['fname'] = $obj->title;
393                                                 
394                                                 $notif_content .= replace_macros($tpl_item_friends,array(
395                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
396                                                         '$item_image' => $it['author-avatar'],
397                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
398                                                         '$item_when' => relative_date($it['created'])
399                                                 ));
400                                                 break;
401                                                 
402                                         default:
403                                                 $item_text = (($it['id'] == $it['parent'])
404                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
405                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
406                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
407
408                                                 $notif_content .= replace_macros($tpl,array(
409                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
410                                                         '$item_image' => $it['author-avatar'],
411                                                         '$item_text' => $item_text,
412                                                         '$item_when' => relative_date($it['created'])
413                                                 ));
414                                 }
415                         }
416                         
417                 } else {
418                         
419                         $notif_content = t('No more personal notifications.');
420                 }
421                 
422                 $o .= replace_macros($notif_tpl,array(
423                         '$notif_header' => t('Personal Notifications'),
424                         '$tabs' => $tabs,
425                         '$notif_content' => $notif_content,
426                 ));
427                 
428
429
430
431
432
433         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
434                 
435                 $notif_tpl = get_markup_template('notifications.tpl');
436                 
437                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
438                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
439                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
440                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
441                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
442                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 ORDER BY `item`.`created` DESC",
443                         intval(local_user())
444                 );
445                 
446                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
447                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
448                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
449                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
450                 
451                 $notif_content = '';
452                 
453                 if (count($r) > 0) {
454                         
455                         foreach ($r as $it) {
456                                 switch($it['verb']){
457                                         case ACTIVITY_LIKE:
458                                                 $notif_content .= replace_macros($tpl_item_likes,array(
459                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
460                                                         '$item_image' => $it['author-avatar'],
461                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
462                                                         '$item_when' => relative_date($it['created'])
463                                                 ));
464
465                                                 break;
466                                         case ACTIVITY_DISLIKE:
467                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
468                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
469                                                         '$item_image' => $it['author-avatar'],
470                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
471                                                         '$item_when' => relative_date($it['created'])
472                                                 ));
473
474                                                 break;
475                                         case ACTIVITY_FRIEND:
476                                         
477                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
478                                                 $obj = parse_xml_string($xmlhead.$it['object']);
479                                                 $it['fname'] = $obj->title;
480                                                 
481                                                 $notif_content .= replace_macros($tpl_item_friends,array(
482                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
483                                                         '$item_image' => $it['author-avatar'],
484                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
485                                                         '$item_when' => relative_date($it['created'])
486                                                 ));
487
488                                                 break;
489                                         default:
490                                                 $notif_content .= replace_macros($tpl_item_comments,array(
491                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
492                                                         '$item_image' => $it['author-avatar'],
493                                                         '$item_text' => sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']),
494                                                         '$item_when' => relative_date($it['created'])
495                                                 ));
496                                 }
497                         }
498                                 
499                 } else {
500                         $notif_content = t('No more home notifications.');
501                 }
502                 
503                 $o .= replace_macros($notif_tpl,array(
504                         '$notif_header' => t('Home Notifications'),
505                         '$tabs' => $tabs,
506                         '$notif_content' => $notif_content,
507                 ));
508         }
509
510         $o .= paginate($a);
511         return $o;
512 }