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