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