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