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