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