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