]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
Merge pull request #3 from fabrixxm/master
[friendica.git] / mod / ping.php
1 <?php
2 require_once("include/datetime.php");
3
4
5 function ping_init(&$a) {
6
7         if(! local_user())
8                 xml_status(0);
9
10         
11         $comments = array();
12         $likes = array();
13         $dislikes = array();
14         $friends = array();
15         
16         $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
17                         `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`, 
18                         `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
19                         FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
20                         WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
21                          `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0",
22                 intval(local_user())
23         );
24         
25         $network = count($r);
26         foreach ($r as $it) {
27                 switch($it['verb']){
28                         case ACTIVITY_LIKE:
29                                 $likes[] = $it;
30                                 break;
31                         case ACTIVITY_DISLIKE:
32                                 $dislikes[] = $it;
33                                 break;
34                         case ACTIVITY_FRIEND:
35                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
36                                 $obj = parse_xml_string($xmlhead.$it['object']);
37                                 $it['fname'] = $obj->title;                     
38                                 $friends[] = $it;
39                                 break;
40                         default:
41                                 if ($it['parent']!=$it['id']) $comments[] = $it;
42                 }
43         }
44
45
46         $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
47                         `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`, 
48                         `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
49                         FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
50                         WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
51                          `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1",
52                 intval(local_user())
53         );      
54         $home = count($r);
55         foreach ($r as $it) {
56                 switch($it['verb']){
57                         case ACTIVITY_LIKE:
58                                 $likes[] = $it;
59                                 break;
60                         case ACTIVITY_DISLIKE:
61                                 $dislikes[] = $it;
62                                 break;
63                         case ACTIVITY_FRIEND:
64                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
65                                 $obj = parse_xml_string($xmlhead.$it['object']);
66                                 $it['fname'] = $obj->title;
67                                 $friends[] = $it;
68                                 break;
69                         default:
70                                 if ($it['parent']!=$it['id']) $comments[] = $it;
71                 }
72         }
73
74
75         $intros1 = q("SELECT COUNT(`intro`.`id`) AS `total`, `intro`.`id`, `intro`.`datetime`, 
76                 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` 
77                 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
78                 WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
79                 intval(local_user())
80         );
81         $intros2 = q("SELECT COUNT(`intro`.`id`) AS `total`, `intro`.`id`, `intro`.`datetime`, 
82                 `contact`.`name`, `contact`.`url`, `contact`.`photo` 
83                 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
84                 WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
85                 intval(local_user())
86         );
87         
88         $intro = $intros1[0]['total'] + $intros2[0]['total'];
89         if ($intros1[0]['total']==0) $intros1=Array();
90         if ($intros2[0]['total']==0) $intros2=Array();
91         $intros = $intros1+$intros2;
92
93
94
95         $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
96         $mails = q("SELECT *,  COUNT(*) AS `total` FROM `mail`
97                 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
98                 intval(local_user()),
99                 dbesc($myurl)
100         );
101         $mail = $mails[0]['total'];
102         
103         if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
104                 $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");
105                 $register = $regs[0]['total'];
106         } else {
107                 $register = "0";
108         }
109
110
111         function xmlize($href, $name, $url, $photo, $date, $message){
112                 $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s">%s</note>';
113                 return sprintf ( $notsxml,
114                                 $href, $name, $url, $photo, $date, $message
115                         );
116         }
117         
118         header("Content-type: text/xml");
119         echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
120                 <result>
121                         <intro>$intro</intro>
122                         <mail>$mail</mail>
123                         <net>$network</net>
124                         <home>$home</home>";
125         if ($register!=0) echo "<register>$register</register>";
126         
127         $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends);
128         
129         echo '  <notif count="'.$tot.'">';
130         if ($intro>0){
131                 foreach ($intros as $i) { 
132                         echo xmlize( $a->get_baseurl().'/notifications/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), t("{0} wants to be your friend") );
133                 };
134         }
135         if ($mail>0){
136                 foreach ($mails as $i) { 
137                         echo xmlize( $a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), t("{0} sent you a message") );
138                 };
139         }
140         if ($register>0){
141                 foreach ($regs as $i) { 
142                         echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), t("{0} requested registration") );
143                 };
144         }
145
146         if (count($comments)){
147                 foreach ($comments as $i) {
148                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf( t("{0} commented %s's post"), $i['pname'] ) );
149                 };
150         }
151         if (count($likes)){
152                 foreach ($likes as $i) {
153                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf( t("{0} like %s's post"), $i['pname'] ) );
154                 };
155         }
156         if (count($dislikes)){
157                 foreach ($dislikes as $i) {
158                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf( t("{0} dislike %s's post"), $i['pname'] ) );
159                 };
160         }
161         if (count($friends)){
162                 foreach ($friends as $i) {
163                         echo xmlize( $a->get_baseurl().'/display/'.$a->user['nickname']."/".$i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf( t("{0} is now friend with %s"), $i['fname'] ) );
164                 };
165         }
166
167
168         echo "  </notif>";
169         
170         echo " <sysmsgs>";
171                 if(x($_SESSION,'sysmsg')){
172                         foreach ($_SESSION['sysmsg'] as $m){
173                                 echo "<notice>".($m)."</notice>";
174                         }
175                         $_SESSION['sysmsg']=array();
176                         unset($_SESSION['sysmsg']);
177                 }
178                 if(x($_SESSION,'sysmsg_info')){
179                         foreach ($_SESSION['sysmsg_info'] as $m){
180                                 echo "<info>".($m)."</info>";
181                         }
182                         $_SESSION['sysmsg_info']=array();
183                         unset($_SESSION['sysmsg_info']);
184                 }
185         
186         echo " </sysmsgs>";
187         echo"</result>
188         ";
189
190         killme();
191 }
192