]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
give themes more control over events/birthdays and provide them as 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                 $t = q("select count(*) as total from notify where uid = %d and seen = 0",
26                         intval(local_user())
27                 );
28                 if($t && intval($t[0]['total']) > 49) {
29                         $z = q("select * from notify where uid = %d
30                                 and seen = 0 order by date desc limit 0, 50",
31                                 intval(local_user())
32                         );
33                         $sysnotify = $t[0]['total'];
34                 }
35                 else {
36                         $z1 = q("select * from notify where uid = %d
37                                 and seen = 0 order by date desc limit 0, 50",
38                                 intval(local_user())
39                         );
40
41                         $z2 = q("select * from notify where uid = %d
42                                 and seen = 1 order by date desc limit 0, %d",
43                                 intval(local_user()),
44                                 intval(50 - intval($t[0]['total']))
45                         );
46                         $z = array_merge($z1,$z2);
47                         $sysnotify = 0; // we will update this in a moment
48                 }
49
50
51
52                 $tags = array();
53                 $comments = array();
54                 $likes = array();
55                 $dislikes = array();
56                 $friends = array();
57                 $posts = array();
58
59                 $home = 0;
60                 $network = 0;
61
62                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`, 
63                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`, 
64                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
65                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
66                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
67                                  `item`.`deleted` = 0 AND `item`.`uid` = %d 
68                                 ORDER BY `item`.`created` DESC",
69                         intval(local_user())
70                 );
71
72                 if(count($r)) {         
73
74                         foreach ($r as $it) {
75
76                                 if($it['wall'])
77                                         $home ++;
78                                 else
79                                         $network ++;
80
81                                 switch($it['verb']){
82                                         case ACTIVITY_TAG:
83                                                 $obj = parse_xml_string($xmlhead.$it['object']);
84                                                 $it['tname'] = $obj->content;
85                                                 $tags[] = $it;
86                                                 break;
87                                         case ACTIVITY_LIKE:
88                                                 $likes[] = $it;
89                                                 break;
90                                         case ACTIVITY_DISLIKE:
91                                                 $dislikes[] = $it;
92                                                 break;
93                                         case ACTIVITY_FRIEND:
94                                                 $obj = parse_xml_string($xmlhead.$it['object']);
95                                                 $it['fname'] = $obj->title;                     
96                                                 $friends[] = $it;
97                                                 break;
98                                         default:
99                                                 if ($it['parent']!=$it['id']) { 
100                                                         $comments[] = $it;
101                                                 } else {
102                                                         if(! $it['wall'])
103                                                                 $posts[] = $it;
104                                                 }
105                                 }
106                         }
107                 }
108
109                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`, 
110                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` 
111                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
112                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
113                         intval(local_user())
114                 );
115                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`, 
116                         `contact`.`name`, `contact`.`url`, `contact`.`photo` 
117                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
118                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
119                         intval(local_user())
120                 );
121                 
122                 $intro = count($intros1) + count($intros2);
123                 $intros = $intros1+$intros2;
124
125
126
127                 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
128                 $mails = q("SELECT *,  COUNT(*) AS `total` FROM `mail`
129                         WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
130                         intval(local_user()),
131                         dbesc($myurl)
132                 );
133                 if($mails)
134                         $mail = $mails[0]['total'];
135                 
136                 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
137                         $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");
138                         if($regs)
139                                 $register = $regs[0]['total'];
140                 } else {
141                         $register = "0";
142                 }
143
144                 $all_events = 0;
145                 $all_events_today = 0;
146                 $events = 0;
147                 $events_today = 0;
148                 $birthdays = 0;
149                 $birthdays_today = 0;
150
151
152                 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
153                         WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
154                         ORDER BY `start` ASC ",
155                         intval(local_user()),
156                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
157                         dbesc(datetime_convert('UTC','UTC','now'))
158                 );
159
160                 if($ev && count($ev)) {
161                         $all_events = intval($ev[0]['total']);
162
163                         if($all_events) {
164                                 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
165                                 foreach($ev as $x) {
166                                         $bd = false;
167                                         if($x['type'] === 'birthday') {
168                                                 $birthdays ++;
169                                                 $bd = true;
170                                         }
171                                         else {
172                                                 $events ++;
173                                         }
174                                         if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
175                                                 $all_events_today ++;
176                                                 if($bd)
177                                                         $birthdays_today ++;
178                                                 else
179                                                         $events_today ++;
180                                         }
181                                 }
182                         }
183                 }
184
185
186
187                 function xmlize($href, $name, $url, $photo, $date, $seen, $message){
188                         $data = array('href' => &$href, 'name' => &$name, 'url'=>&$url, 'photo'=>&$photo, 'date'=>&$date, 'seen'=>&$seen, 'messsage'=>&$message);
189                         call_hooks('ping_xmlize', $data);
190                         $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" >%s</note>';
191                         return sprintf ( $notsxml,
192                                 xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message)
193                         );
194                 }
195                 
196                 echo "<intro>$intro</intro>
197                                 <mail>$mail</mail>
198                                 <net>$network</net>
199                                 <home>$home</home>\r\n";
200                 if ($register!=0) echo "<register>$register</register>";
201
202                 echo "<all-events>$all_events</all-events>
203                         <all-events-today>$all_events_today</all-events-today>
204                         <events>$events</events>
205                         <events-today>$events_today</events-today>
206                         <birthdays>$birthdays</birthdays>
207                         <birthdays-today>$birthdays_today</birthdays-today>\r\n";
208                 
209                 $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends)+count($posts)+count($tags);
210
211                 require_once('include/bbcode.php');
212
213                 if($firehose) {
214                         echo '  <notif count="'.$tot.'">';
215                 }
216                 else {
217                         if(count($z) && (! $sysnotify)) {
218                                 foreach($z as $zz) {
219                                         if($zz['seen'] == 0)
220                                                 $sysnotify ++;
221                                 }
222                         }                                               
223
224                         echo '  <notif count="'. $sysnotify .'">';
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