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