]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
The ID field will be stored with notifications as well.
[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                 if($t && intval($t[0]['total']) > 49) {
33                         $z = q("select * from notify where uid = %d AND `msg` != ''
34                                 and seen = 0 GROUP BY `parent` order by date desc limit 0, 50",
35                                 intval(local_user())
36                         );
37                         $sysnotify = $t[0]['total'];
38                 }
39                 else {
40                         $z1 = q("select * from notify where uid = %d AND `msg` != ''
41                                 and seen = 0 GROUP BY `parent` order by date desc limit 0, 50",
42                                 intval(local_user())
43                         );
44
45                         $z2 = q("select * from notify where uid = %d AND `msg` != ''
46                                 and seen = 1 GROUP BY `parent` order by date desc limit 0, %d",
47                                 intval(local_user()),
48                                 intval(50 - intval($t[0]['total']))
49                         );
50                         $z = array_merge($z1,$z2);
51                         $sysnotify = 0; // we will update this in a moment
52                 }
53
54
55
56                 $tags = array();
57                 $comments = array();
58                 $likes = array();
59                 $dislikes = array();
60                 $friends = array();
61                 $posts = array();
62
63                 $home = 0;
64                 $network = 0;
65
66                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
67                                 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
68                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
69                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
70                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
71                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
72                                 ORDER BY `item`.`created` DESC",
73                         intval(local_user())
74                 );
75
76                 if(count($r)) {
77
78                         $arr = array('items' => $r);
79                         call_hooks('network_ping', $arr);
80
81                         foreach ($r as $it) {
82
83                                 if($it['wall'])
84                                         $home ++;
85                                 else
86                                         $network ++;
87
88                                 switch($it['verb']){
89                                         case ACTIVITY_TAG:
90                                                 $obj = parse_xml_string($xmlhead.$it['object']);
91                                                 $it['tname'] = $obj->content;
92                                                 $tags[] = $it;
93                                                 break;
94                                         case ACTIVITY_LIKE:
95                                                 $likes[] = $it;
96                                                 break;
97                                         case ACTIVITY_DISLIKE:
98                                                 $dislikes[] = $it;
99                                                 break;
100                                         case ACTIVITY_FRIEND:
101                                                 $obj = parse_xml_string($xmlhead.$it['object']);
102                                                 $it['fname'] = $obj->title;
103                                                 $friends[] = $it;
104                                                 break;
105                                         default:
106                                                 if ($it['parent']!=$it['id']) {
107                                                         $comments[] = $it;
108                                                 } else {
109                                                         if(! $it['wall'])
110                                                                 $posts[] = $it;
111                                                 }
112                                 }
113                         }
114                 }
115
116                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,
117                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
118                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
119                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
120                         intval(local_user())
121                 );
122                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
123                         `contact`.`name`, `contact`.`url`, `contact`.`photo`
124                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
125                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
126                         intval(local_user())
127                 );
128
129                 $intro = count($intros1) + count($intros2);
130                 $intros = $intros1+$intros2;
131
132
133
134                 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
135                 $mails = q("SELECT *,  COUNT(*) AS `total` FROM `mail`
136                         WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
137                         intval(local_user()),
138                         dbesc($myurl)
139                 );
140                 if($mails)
141                         $mail = $mails[0]['total'];
142
143                 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
144                         $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");
145                         if($regs)
146                                 $register = $regs[0]['total'];
147                 } else {
148                         $register = "0";
149                 }
150
151                 $all_events = 0;
152                 $all_events_today = 0;
153                 $events = 0;
154                 $events_today = 0;
155                 $birthdays = 0;
156                 $birthdays_today = 0;
157
158
159                 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
160                         WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
161                         ORDER BY `start` ASC ",
162                         intval(local_user()),
163                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
164                         dbesc(datetime_convert('UTC','UTC','now'))
165                 );
166
167                 if($ev && count($ev)) {
168                         $all_events = intval($ev[0]['total']);
169
170                         if($all_events) {
171                                 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
172                                 foreach($ev as $x) {
173                                         $bd = false;
174                                         if($x['type'] === 'birthday') {
175                                                 $birthdays ++;
176                                                 $bd = true;
177                                         }
178                                         else {
179                                                 $events ++;
180                                         }
181                                         if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
182                                                 $all_events_today ++;
183                                                 if($bd)
184                                                         $birthdays_today ++;
185                                                 else
186                                                         $events_today ++;
187                                         }
188                                 }
189                         }
190                 }
191
192
193
194                 function xmlize($href, $name, $url, $photo, $date, $seen, $message){
195                         require_once("mod/proxy.php");
196                         $photo = proxy_url($photo);
197                         $data = array('href' => &$href, 'name' => &$name, 'url'=>&$url, 'photo'=>&$photo, 'date'=>&$date, 'seen'=>&$seen, 'messsage'=>&$message);
198                         call_hooks('ping_xmlize', $data);
199                         $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" >%s</note>';
200                         return sprintf ( $notsxml,
201                                 xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message)
202                         );
203                 }
204
205                 echo "<intro>$intro</intro>
206                                 <mail>$mail</mail>
207                                 <net>$network</net>
208                                 <home>$home</home>\r\n";
209                 if ($register!=0) echo "<register>$register</register>";
210
211                 echo "<all-events>$all_events</all-events>
212                         <all-events-today>$all_events_today</all-events-today>
213                         <events>$events</events>
214                         <events-today>$events_today</events-today>
215                         <birthdays>$birthdays</birthdays>
216                         <birthdays-today>$birthdays_today</birthdays-today>\r\n";
217
218                 $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends)+count($posts)+count($tags);
219
220                 require_once('include/bbcode.php');
221
222                 if($firehose) {
223                         echo '  <notif count="'.$tot.'">';
224                 }
225                 else {
226                         if(count($z) && (! $sysnotify)) {
227                                 foreach($z as $zz) {
228                                         if($zz['seen'] == 0)
229                                                 $sysnotify ++;
230                                 }
231                         }
232
233                         echo '  <notif count="'. $sysnotify .'">';
234                         if(count($z)) {
235                                 foreach($z as $zz) {
236                                         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'])));
237                                 }
238                         }
239                 }
240
241                 if($firehose) {
242                         if ($intro>0){
243                                 foreach ($intros as $i) {
244                                         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") );
245                                 };
246                         }
247                         if ($mail>0){
248                                 foreach ($mails as $i) { 
249                                         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") );
250                                 };
251                         }
252                         if ($register>0){
253                                 foreach ($regs as $i) { 
254                                         echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") );
255                                 };
256                         }
257
258                         if (count($comments)){
259                                 foreach ($comments as $i) {
260                                         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'] ) );
261                                 };
262                         }
263                         if (count($likes)){
264                                 foreach ($likes 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} liked %s's post"), $i['pname'] ) );
266                                 };
267                         }
268                         if (count($dislikes)){
269                                 foreach ($dislikes 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} disliked %s's post"), $i['pname'] ) );
271                                 };
272                         }
273                         if (count($friends)){
274                                 foreach ($friends 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} is now friends with %s"), $i['fname'] ) );
276                                 };
277                         }
278                         if (count($posts)){
279                                 foreach ($posts 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} posted") ) );
281                                 };
282                         }
283                         if (count($tags)){
284                                 foreach ($tags 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} tagged %s's post with #%s"), $i['pname'], $i['tname'] ) );
286                                 };
287                         }
288
289                         if (count($cit)){
290                                 foreach ($cit as $i) {
291                                         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") );
292                                 };
293                         }
294                 }
295
296                 echo "  </notif>";
297         }
298         echo " <sysmsgs>";
299
300         if(x($_SESSION,'sysmsg')){
301                 foreach ($_SESSION['sysmsg'] as $m){
302                         echo "<notice>".xmlify($m)."</notice>";
303                 }
304                 unset($_SESSION['sysmsg']);
305         }
306         if(x($_SESSION,'sysmsg_info')){
307                 foreach ($_SESSION['sysmsg_info'] as $m){
308                         echo "<info>".xmlify($m)."</info>";
309                 }
310                 unset($_SESSION['sysmsg_info']);
311         }
312         
313         echo " </sysmsgs>";
314         echo"</result>
315         ";
316
317         killme();
318 }
319