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