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