]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
Bugfix: The user id wasn't fetched correctly.
[friendica.git] / mod / ping.php
1 <?php
2 require_once("include/datetime.php");
3 require_once('include/bbcode.php');
4
5
6 function ping_init(&$a) {
7
8         header("Content-type: text/xml");
9
10         echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
11                 <result>";
12
13         $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
14
15         if(local_user()){
16
17                 // Different login session than the page that is calling us.
18
19                 if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
20                         echo '<invalid>1</invalid></result>';
21                         killme();
22                 }
23
24                 $firehose = intval(get_pconfig(local_user(),'system','notify_full'));
25
26                 // Are the nofications calles from the regular process or via the friendica app?
27                 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
28
29                 $z = ping_get_notifications(local_user(), $regularnotifications);
30                 $sysnotify = 0; // we will update this in a moment
31
32                 $tags = array();
33                 $comments = array();
34                 $likes = array();
35                 $dislikes = array();
36                 $friends = array();
37                 $posts = array();
38
39                 $home = 0;
40                 $network = 0;
41
42                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
43                                 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
44                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
45                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
46                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
47                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
48                                 ORDER BY `item`.`created` DESC",
49                         intval(local_user())
50                 );
51
52                 if(count($r)) {
53
54                         $arr = array('items' => $r);
55                         call_hooks('network_ping', $arr);
56
57                         foreach ($r as $it) {
58
59                                 if($it['wall'])
60                                         $home ++;
61                                 else
62                                         $network ++;
63
64                                 switch($it['verb']){
65                                         case ACTIVITY_TAG:
66                                                 $obj = parse_xml_string($xmlhead.$it['object']);
67                                                 $it['tname'] = $obj->content;
68                                                 $tags[] = $it;
69                                                 break;
70                                         case ACTIVITY_LIKE:
71                                                 $likes[] = $it;
72                                                 break;
73                                         case ACTIVITY_DISLIKE:
74                                                 $dislikes[] = $it;
75                                                 break;
76                                         case ACTIVITY_FRIEND:
77                                                 $obj = parse_xml_string($xmlhead.$it['object']);
78                                                 $it['fname'] = $obj->title;
79                                                 $friends[] = $it;
80                                                 break;
81                                         default:
82                                                 if ($it['parent']!=$it['id']) {
83                                                         $comments[] = $it;
84                                                 } else {
85                                                         if(! $it['wall'])
86                                                                 $posts[] = $it;
87                                                 }
88                                 }
89                         }
90                 }
91
92                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,
93                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
94                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
95                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
96                         intval(local_user())
97                 );
98                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
99                         `contact`.`name`, `contact`.`url`, `contact`.`photo`
100                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
101                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
102                         intval(local_user())
103                 );
104
105                 $intro = count($intros1) + count($intros2);
106                 $intros = $intros1+$intros2;
107
108
109
110                 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
111                 $mails = q("SELECT * FROM `mail`
112                         WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
113                         intval(local_user()),
114                         dbesc($myurl)
115                 );
116                 $mail = count($mails);
117
118                 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
119                         $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");
120                         if($regs)
121                                 $register = $regs[0]['total'];
122                 } else {
123                         $register = "0";
124                 }
125
126                 $all_events = 0;
127                 $all_events_today = 0;
128                 $events = 0;
129                 $events_today = 0;
130                 $birthdays = 0;
131                 $birthdays_today = 0;
132
133
134                 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
135                         WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
136                         ORDER BY `start` ASC ",
137                         intval(local_user()),
138                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
139                         dbesc(datetime_convert('UTC','UTC','now'))
140                 );
141
142                 if($ev && count($ev)) {
143                         $all_events = intval($ev[0]['total']);
144
145                         if($all_events) {
146                                 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
147                                 foreach($ev as $x) {
148                                         $bd = false;
149                                         if($x['type'] === 'birthday') {
150                                                 $birthdays ++;
151                                                 $bd = true;
152                                         }
153                                         else {
154                                                 $events ++;
155                                         }
156                                         if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
157                                                 $all_events_today ++;
158                                                 if($bd)
159                                                         $birthdays_today ++;
160                                                 else
161                                                         $events_today ++;
162                                         }
163                                 }
164                         }
165                 }
166
167
168
169                 function xmlize($href, $name, $url, $photo, $date, $seen, $message){
170                         require_once("mod/proxy.php");
171                         $photo = proxy_url($photo);
172                         $data = array('href' => &$href, 'name' => &$name, 'url'=>&$url, 'photo'=>&$photo, 'date'=>&$date, 'seen'=>&$seen, 'messsage'=>&$message);
173                         call_hooks('ping_xmlize', $data);
174                         $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" >%s</note>';
175                         return sprintf ( $notsxml,
176                                 xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message)
177                         );
178                 }
179
180                 echo "<intro>$intro</intro>
181                                 <mail>$mail</mail>
182                                 <net>$network</net>
183                                 <home>$home</home>\r\n";
184                 if ($register!=0) echo "<register>$register</register>";
185
186                 echo "<all-events>$all_events</all-events>
187                         <all-events-today>$all_events_today</all-events-today>
188                         <events>$events</events>
189                         <events-today>$events_today</events-today>
190                         <birthdays>$birthdays</birthdays>
191                         <birthdays-today>$birthdays_today</birthdays-today>\r\n";
192
193                 $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends)+count($posts)+count($tags);
194
195                 if($firehose) {
196                         echo '  <notif count="'.$tot.'">';
197                 }
198                 else {
199                         if(count($z) && (! $sysnotify)) {
200                                 foreach($z as $zz) {
201                                         if($zz['seen'] == 0)
202                                                 $sysnotify ++;
203                                 }
204                         }
205
206                         echo '  <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
207
208                         if ($intro>0){
209                                 foreach ($intros as $i) {
210                                         echo xmlize($a->get_baseurl().'/notifications/intros/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen', "&rarr; ".t("{0} wants to be your friend"));
211                                 };
212                         }
213                         if ($mail>0){
214                                 foreach ($mails as $i) {
215                                         echo xmlize($a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen',"&rarr; ".t("{0} sent you a message"));
216                                 };
217                         }
218                         if ($register>0){
219                                 foreach ($regs as $i) {
220                                         echo xmlize($a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen', "&rarr; ".t("{0} requested registration"));
221                                 };
222                         }
223
224                         if(count($z)) {
225                                 foreach($z as $zz) {
226                                         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'])));
227                                 }
228                         }
229                 }
230
231                 if($firehose) {
232                         if ($intro>0){
233                                 foreach ($intros as $i) {
234                                         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") );
235                                 };
236                         }
237                         if ($mail>0){
238                                 foreach ($mails as $i) {
239                                         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") );
240                                 };
241                         }
242                         if ($register>0){
243                                 foreach ($regs as $i) {
244                                         echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") );
245                                 };
246                         }
247
248                         if (count($comments)){
249                                 foreach ($comments as $i) {
250                                         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'] ) );
251                                 };
252                         }
253                         if (count($likes)){
254                                 foreach ($likes as $i) {
255                                         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'] ) );
256                                 };
257                         }
258                         if (count($dislikes)){
259                                 foreach ($dislikes as $i) {
260                                         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'] ) );
261                                 };
262                         }
263                         if (count($friends)){
264                                 foreach ($friends as $i) {
265                                         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'] ) );
266                                 };
267                         }
268                         if (count($posts)){
269                                 foreach ($posts as $i) {
270                                         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") ) );
271                                 };
272                         }
273                         if (count($tags)){
274                                 foreach ($tags as $i) {
275                                         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'] ) );
276                                 };
277                         }
278
279                         if (count($cit)){
280                                 foreach ($cit 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',t("{0} mentioned you in a post") );
282                                 };
283                         }
284                 }
285
286                 echo "  </notif>";
287         }
288         echo " <sysmsgs>";
289
290         if(x($_SESSION,'sysmsg')){
291                 foreach ($_SESSION['sysmsg'] as $m){
292                         echo "<notice>".xmlify($m)."</notice>";
293                 }
294                 unset($_SESSION['sysmsg']);
295         }
296         if(x($_SESSION,'sysmsg_info')){
297                 foreach ($_SESSION['sysmsg_info'] as $m){
298                         echo "<info>".xmlify($m)."</info>";
299                 }
300                 unset($_SESSION['sysmsg_info']);
301         }
302
303         echo " </sysmsgs>";
304         echo"</result>
305         ";
306
307         killme();
308 }
309
310 function ping_get_notifications($uid, $regularnotifications) {
311
312         $result = array();
313         $offset = 0;
314         $seen = false;
315         $seensql = "NOT";
316         $order = "";
317         $quit = false;
318
319         do {
320                 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
321                         FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
322                         WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
323                         AND NOT (`notify`.`type` IN (%d, %d))
324                         AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
325                         intval($uid),
326                         intval(NOTIFY_INTRO),
327                         intval(NOTIFY_MAIL),
328                         intval($offset)
329                 );
330
331                 if (!$r AND !$seen) {
332                         $seen = true;
333                         $seensql = "";
334                         $order = "DESC";
335                         $offset = 0;
336                 } elseif (!$r)
337                         $quit = true;
338                 else
339                         $offset += 50;
340
341                 foreach ($r AS $notification) {
342                         if (is_null($notification["visible"]))
343                                 $notification["visible"] = true;
344
345                         if (is_null($notification["spam"]))
346                                 $notification["spam"] = 0;
347
348                         if (is_null($notification["deleted"]))
349                                 $notification["deleted"] = 0;
350
351                         $notification["msg"] = strip_tags(bbcode($notification["msg"]));
352                         $notification["name"] = strip_tags(bbcode($notification["name"]));
353
354                         // Replace the name with {0} but ensure to make that only once
355                         // The {0} is used later and prints the name in bold.
356                         // But don't do it for the android app.
357                         $pos = strpos($notification["msg"],$notification['name']);
358                         if (($pos !== false) AND $regularnotifications)
359                                 $notification["msg"] = substr_replace($notification["msg"],"{0}",$pos,strlen($notification["name"]));
360                         else
361                                 $notification["msg"] = str_replace("{0}", $notification["name"], $notification["msg"]);
362
363                         if ($notification["visible"] AND !$notification["spam"] AND
364                                 !$notification["deleted"] AND !is_array($result[$notification["parent"]]))
365                                 $result[$notification["parent"]] = $notification;
366                 }
367
368         } while ((count($result) < 50) AND !$quit);
369
370         return($result);
371 }