]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
Merge branch 'master' of github.com:annando/friendica
[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         if(local_user()){
14
15                 $firehose = intval(get_pconfig(local_user(),'system','notify_full'));
16
17                 $t = q("select count(*) as total from notify where uid = %d and seen = 0",
18                         intval(local_user())
19                 );
20                 if($t && intval($t[0]['total']) > 49) {
21                         $z = q("select * from notify where uid = %d
22                                 and seen = 0 order by date desc limit 0, 50",
23                                 intval(local_user())
24                         );
25                         $sysnotify = $t[0]['total'];
26                 }
27                 else {
28                         $z1 = q("select * from notify where uid = %d
29                                 and seen = 0 order by date desc limit 0, 50",
30                                 intval(local_user())
31                         );
32
33                         $z2 = q("select * from notify where uid = %d
34                                 and seen = 1 order by date desc limit 0, %d",
35                                 intval(local_user()),
36                                 intval(50 - intval($t[0]['total']))
37                         );
38                         $z = array_merge($z1,$z2);
39                         $sysnotify = 0; // we will update this in a moment
40                 }
41
42
43
44                 $tags = array();
45                 $comments = array();
46                 $likes = array();
47                 $dislikes = array();
48                 $friends = array();
49                 $posts = array();
50                 $home = 0;
51                 $network = 0;
52
53                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`, 
54                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`, 
55                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
56                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
57                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
58                                  `item`.`deleted` = 0 AND `item`.`uid` = %d 
59                                 ORDER BY `item`.`created` DESC",
60                         intval(local_user())
61                 );
62
63                 if(count($r)) {         
64
65                         foreach ($r as $it) {
66
67                                 if($it['wall'])
68                                         $home ++;
69                                 else
70                                         $network ++;
71
72                                 switch($it['verb']){
73                                         case ACTIVITY_TAG:
74                                                 $obj = parse_xml_string($xmlhead.$it['object']);
75                                                 $it['tname'] = $obj->content;
76                                                 $tags[] = $it;
77                                                 break;
78                                         case ACTIVITY_LIKE:
79                                                 $likes[] = $it;
80                                                 break;
81                                         case ACTIVITY_DISLIKE:
82                                                 $dislikes[] = $it;
83                                                 break;
84                                         case ACTIVITY_FRIEND:
85                                                 $obj = parse_xml_string($xmlhead.$it['object']);
86                                                 $it['fname'] = $obj->title;                     
87                                                 $friends[] = $it;
88                                                 break;
89                                         default:
90                                                 if ($it['parent']!=$it['id']) { 
91                                                         $comments[] = $it;
92                                                 } else {
93                                                         if(! $it['wall'])
94                                                                 $posts[] = $it;
95                                                 }
96                                 }
97                         }
98                 }
99
100                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`, 
101                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` 
102                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
103                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
104                         intval(local_user())
105                 );
106                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`, 
107                         `contact`.`name`, `contact`.`url`, `contact`.`photo` 
108                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
109                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
110                         intval(local_user())
111                 );
112                 
113                 $intro = count($intros1) + count($intros2);
114                 $intros = $intros1+$intros2;
115
116
117
118                 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
119                 $mails = q("SELECT *,  COUNT(*) AS `total` FROM `mail`
120                         WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
121                         intval(local_user()),
122                         dbesc($myurl)
123                 );
124                 if($mails)
125                         $mail = $mails[0]['total'];
126                 
127                 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
128                         $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");
129                         if($regs)
130                                 $register = $regs[0]['total'];
131                 } else {
132                         $register = "0";
133                 }
134
135
136                 function xmlize($href, $name, $url, $photo, $date, $seen, $message){
137                         $data = array('href' => &$href, 'name' => &$name, 'url'=>&$url, 'photo'=>&$photo, 'date'=>&$date, 'seen'=>&$seen, 'messsage'=>&$message);
138                         call_hooks('ping_xmlize', $data);
139                         $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" >%s</note>';
140                         return sprintf ( $notsxml,
141                                 xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message)
142                         );
143                 }
144                 
145                 echo "<intro>$intro</intro>
146                                 <mail>$mail</mail>
147                                 <net>$network</net>
148                                 <home>$home</home>";
149                 if ($register!=0) echo "<register>$register</register>";
150                 
151                 $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends)+count($posts)+count($tags);
152
153                 require_once('include/bbcode.php');
154
155                 if($firehose) {
156                         echo '  <notif count="'.$tot.'">';
157                 }
158                 else {
159                         if(count($z) && (! $sysnotify)) {
160                                 foreach($z as $zz) {
161                                         if($zz['seen'] == 0)
162                                                 $sysnotify ++;
163                                 }
164                         }                                               
165
166                         echo '  <notif count="'. $sysnotify .'">';
167                         if(count($z)) {
168                                 foreach($z as $zz) {
169                                         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'])));
170                                 }
171                         }
172                 }
173
174                 if($firehose) {
175                         if ($intro>0){
176                                 foreach ($intros as $i) { 
177                                         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") );
178                                 };
179                         }
180                         if ($mail>0){
181                                 foreach ($mails as $i) { 
182                                         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") );
183                                 };
184                         }
185                         if ($register>0){
186                                 foreach ($regs as $i) { 
187                                         echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") );
188                                 };
189                         }
190
191                         if (count($comments)){
192                                 foreach ($comments as $i) {
193                                         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'] ) );
194                                 };
195                         }
196                         if (count($likes)){
197                                 foreach ($likes as $i) {
198                                         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'] ) );
199                                 };
200                         }
201                         if (count($dislikes)){
202                                 foreach ($dislikes as $i) {
203                                         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'] ) );
204                                 };
205                         }
206                         if (count($friends)){
207                                 foreach ($friends as $i) {
208                                         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'] ) );
209                                 };
210                         }
211                         if (count($posts)){
212                                 foreach ($posts as $i) {
213                                         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") ) );
214                                 };
215                         }
216                         if (count($tags)){
217                                 foreach ($tags as $i) {
218                                         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'] ) );
219                                 };
220                         }
221
222                         if (count($cit)){
223                                 foreach ($cit as $i) {
224                                         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") );
225                                 };
226                         }
227                 }
228
229                 echo "  </notif>";
230         }
231         echo " <sysmsgs>";
232
233         if(x($_SESSION,'sysmsg')){
234                 foreach ($_SESSION['sysmsg'] as $m){
235                         echo "<notice>".xmlify($m)."</notice>";
236                 }
237                 unset($_SESSION['sysmsg']);
238         }
239         if(x($_SESSION,'sysmsg_info')){
240                 foreach ($_SESSION['sysmsg_info'] as $m){
241                         echo "<info>".xmlify($m)."</info>";
242                 }
243                 unset($_SESSION['sysmsg_info']);
244         }
245         
246         echo " </sysmsgs>";
247         echo"</result>
248         ";
249
250         killme();
251 }
252