]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
New function that will prevent notifications from deleted posts
[friendica.git] / mod / ping.php
1 <?php
2 require_once("include/datetime.php");
3
4
5 function ping_init(&$a) {
6
7         header("Content-type: text/xml");
8
9         echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
10                 <result>";
11
12         $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
13
14         if(local_user()){
15
16                 // Different login session than the page that is calling us. 
17
18                 if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
19                         echo '<invalid>1</invalid></result>';
20                         killme();
21                 }
22
23                 $firehose = intval(get_pconfig(local_user(),'system','notify_full'));
24
25 /*
26 select notify.id, notify.type, iid, visible, deleted, CASE notify.iid WHEN 0 THEN 1 ELSE item.visible END as vis, CASE notify.iid WHEN 0 THEN 0 ELSE item.deleted END as del from notify left join item on item.id = notify.iid where notify.uid=1 group by notify.parent order by notify.id desc limit 10;
27 */
28
29                 $t = q("select count(*) as `total` from `notify` where `uid` = %d and `seen` = 0 AND `msg` != '' GROUP BY `parent`",
30                         intval(local_user())
31                 );
32
33                 $z = ping_get_notifications(local_user());
34                 $sysnotify = 0; // we will update this in a moment
35
36 /*
37                 if($t && intval($t[0]['total']) > 49) {
38                         $z = q("select * from notify where uid = %d AND `msg` != ''
39                                 and seen = 0 GROUP BY `parent` order by date desc limit 0, 50",
40                                 intval(local_user())
41                         );
42                         $sysnotify = $t[0]['total'];
43                 }
44                 else {
45                         $z1 = q("select * from notify where uid = %d AND `msg` != ''
46                                 and seen = 0 GROUP BY `parent` order by date desc limit 0, 50",
47                                 intval(local_user())
48                         );
49
50                         $z2 = q("select * from notify where uid = %d AND `msg` != ''
51                                 and seen = 1 GROUP BY `parent` order by date desc limit 0, %d",
52                                 intval(local_user()),
53                                 intval(50 - intval($t[0]['total']))
54                         );
55                         $z = array_merge($z1,$z2);
56                         $sysnotify = 0; // we will update this in a moment
57                 }
58 */
59
60
61                 $tags = array();
62                 $comments = array();
63                 $likes = array();
64                 $dislikes = array();
65                 $friends = array();
66                 $posts = array();
67
68                 $home = 0;
69                 $network = 0;
70
71                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
72                                 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
73                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
74                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
75                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
76                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
77                                 ORDER BY `item`.`created` DESC",
78                         intval(local_user())
79                 );
80
81                 if(count($r)) {
82
83                         $arr = array('items' => $r);
84                         call_hooks('network_ping', $arr);
85
86                         foreach ($r as $it) {
87
88                                 if($it['wall'])
89                                         $home ++;
90                                 else
91                                         $network ++;
92
93                                 switch($it['verb']){
94                                         case ACTIVITY_TAG:
95                                                 $obj = parse_xml_string($xmlhead.$it['object']);
96                                                 $it['tname'] = $obj->content;
97                                                 $tags[] = $it;
98                                                 break;
99                                         case ACTIVITY_LIKE:
100                                                 $likes[] = $it;
101                                                 break;
102                                         case ACTIVITY_DISLIKE:
103                                                 $dislikes[] = $it;
104                                                 break;
105                                         case ACTIVITY_FRIEND:
106                                                 $obj = parse_xml_string($xmlhead.$it['object']);
107                                                 $it['fname'] = $obj->title;
108                                                 $friends[] = $it;
109                                                 break;
110                                         default:
111                                                 if ($it['parent']!=$it['id']) {
112                                                         $comments[] = $it;
113                                                 } else {
114                                                         if(! $it['wall'])
115                                                                 $posts[] = $it;
116                                                 }
117                                 }
118                         }
119                 }
120
121                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,
122                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
123                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
124                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
125                         intval(local_user())
126                 );
127                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
128                         `contact`.`name`, `contact`.`url`, `contact`.`photo`
129                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
130                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
131                         intval(local_user())
132                 );
133
134                 $intro = count($intros1) + count($intros2);
135                 $intros = $intros1+$intros2;
136
137
138
139                 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
140                 $mails = q("SELECT *,  COUNT(*) AS `total` FROM `mail`
141                         WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
142                         intval(local_user()),
143                         dbesc($myurl)
144                 );
145                 if($mails)
146                         $mail = $mails[0]['total'];
147
148                 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
149                         $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
150                         if($regs)
151                                 $register = $regs[0]['total'];
152                 } else {
153                         $register = "0";
154                 }
155
156                 $all_events = 0;
157                 $all_events_today = 0;
158                 $events = 0;
159                 $events_today = 0;
160                 $birthdays = 0;
161                 $birthdays_today = 0;
162
163
164                 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
165                         WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
166                         ORDER BY `start` ASC ",
167                         intval(local_user()),
168                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
169                         dbesc(datetime_convert('UTC','UTC','now'))
170                 );
171
172                 if($ev && count($ev)) {
173                         $all_events = intval($ev[0]['total']);
174
175                         if($all_events) {
176                                 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
177                                 foreach($ev as $x) {
178                                         $bd = false;
179                                         if($x['type'] === 'birthday') {
180                                                 $birthdays ++;
181                                                 $bd = true;
182                                         }
183                                         else {
184                                                 $events ++;
185                                         }
186                                         if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
187                                                 $all_events_today ++;
188                                                 if($bd)
189                                                         $birthdays_today ++;
190                                                 else
191                                                         $events_today ++;
192                                         }
193                                 }
194                         }
195                 }
196
197
198
199                 function xmlize($href, $name, $url, $photo, $date, $seen, $message){
200                         require_once("mod/proxy.php");
201                         $photo = proxy_url($photo);
202                         $data = array('href' => &$href, 'name' => &$name, 'url'=>&$url, 'photo'=>&$photo, 'date'=>&$date, 'seen'=>&$seen, 'messsage'=>&$message);
203                         call_hooks('ping_xmlize', $data);
204                         $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" >%s</note>';
205                         return sprintf ( $notsxml,
206                                 xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message)
207                         );
208                 }
209
210                 echo "<intro>$intro</intro>
211                                 <mail>$mail</mail>
212                                 <net>$network</net>
213                                 <home>$home</home>\r\n";
214                 if ($register!=0) echo "<register>$register</register>";
215
216                 echo "<all-events>$all_events</all-events>
217                         <all-events-today>$all_events_today</all-events-today>
218                         <events>$events</events>
219                         <events-today>$events_today</events-today>
220                         <birthdays>$birthdays</birthdays>
221                         <birthdays-today>$birthdays_today</birthdays-today>\r\n";
222
223                 $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends)+count($posts)+count($tags);
224
225                 require_once('include/bbcode.php');
226
227                 if($firehose) {
228                         echo '  <notif count="'.$tot.'">';
229                 }
230                 else {
231                         if(count($z) && (! $sysnotify)) {
232                                 foreach($z as $zz) {
233                                         if($zz['seen'] == 0)
234                                                 $sysnotify ++;
235                                 }
236                         }
237
238                         echo '  <notif count="'. $sysnotify .'">';
239                         if(count($z)) {
240                                 foreach($z as $zz) {
241                                         echo xmlize($a->get_baseurl() . '/notify/view/' . $zz['id'], $zz['name'],$zz['url'],$zz['photo'],relative_date($zz['date']), ($zz['seen'] ? 'notify-seen' : 'notify-unseen'), ($zz['seen'] ? '' : '&rarr; ') .strip_tags(bbcode($zz['msg'])));
242                                 }
243                         }
244                 }
245
246                 if($firehose) {
247                         if ($intro>0){
248                                 foreach ($intros as $i) {
249                                         echo xmlize( $a->get_baseurl().'/notifications/intros/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen',t("{0} wants to be your friend") );
250                                 };
251                         }
252                         if ($mail>0){
253                                 foreach ($mails as $i) { 
254                                         echo xmlize( $a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen',t("{0} sent you a message") );
255                                 };
256                         }
257                         if ($register>0){
258                                 foreach ($regs as $i) { 
259                                         echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") );
260                                 };
261                         }
262
263                         if (count($comments)){
264                                 foreach ($comments as $i) {
265                                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} commented %s's post"), $i['pname'] ) );
266                                 };
267                         }
268                         if (count($likes)){
269                                 foreach ($likes as $i) {
270                                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} liked %s's post"), $i['pname'] ) );
271                                 };
272                         }
273                         if (count($dislikes)){
274                                 foreach ($dislikes as $i) {
275                                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} disliked %s's post"), $i['pname'] ) );
276                                 };
277                         }
278                         if (count($friends)){
279                                 foreach ($friends as $i) {
280                                         echo xmlize($a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'],$i['author-name'],$i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} is now friends with %s"), $i['fname'] ) );
281                                 };
282                         }
283                         if (count($posts)){
284                                 foreach ($posts as $i) {
285                                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} posted") ) );
286                                 };
287                         }
288                         if (count($tags)){
289                                 foreach ($tags as $i) {
290                                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',sprintf( t("{0} tagged %s's post with #%s"), $i['pname'], $i['tname'] ) );
291                                 };
292                         }
293
294                         if (count($cit)){
295                                 foreach ($cit as $i) {
296                                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen',t("{0} mentioned you in a post") );
297                                 };
298                         }
299                 }
300
301                 echo "  </notif>";
302         }
303         echo " <sysmsgs>";
304
305         if(x($_SESSION,'sysmsg')){
306                 foreach ($_SESSION['sysmsg'] as $m){
307                         echo "<notice>".xmlify($m)."</notice>";
308                 }
309                 unset($_SESSION['sysmsg']);
310         }
311         if(x($_SESSION,'sysmsg_info')){
312                 foreach ($_SESSION['sysmsg_info'] as $m){
313                         echo "<info>".xmlify($m)."</info>";
314                 }
315                 unset($_SESSION['sysmsg_info']);
316         }
317
318         echo " </sysmsgs>";
319         echo"</result>
320         ";
321
322         killme();
323 }
324
325 function ping_get_notifications($uid) {
326
327         $result = array();
328         $offset = 0;
329         $seen = false;
330         $seensql = "NOT";
331         $quit = false;
332
333         do {
334                 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
335                         FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
336                         WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
337                         AND $seensql `notify`.`seen` ORDER BY `notify`.`date` DESC LIMIT %d, 50",
338                         intval($uid), intval($offset)
339                 );
340
341                 if (!$r AND !$seen) {
342                         $seen = true;
343                         $seensql = "";
344                         $offset = 0;
345                 } elseif (!$r)
346                         $quit = true;
347                 else
348                         $offset += 50;
349
350                 foreach ($r AS $notification) {
351                         if (is_null($notification["visible"]))
352                                 $notification["visible"] = true;
353
354                         if (is_null($notification["spam"]))
355                                 $notification["spam"] = 0;
356
357                         if (is_null($notification["deleted"]))
358                                 $notification["deleted"] = 0;
359
360                         if ($notification["visible"] AND !$notification["spam"] AND
361                                 !$notification["deleted"] AND !is_array($result[$notification["parent"]]))
362                                 $result[$notification["parent"]] = $notification;
363                 }
364
365         } while ((count($result) < 50) AND !$quit);
366
367         return($result);
368 }
369