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