]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
Merge pull request #1693 from rabuzarus/events-reminder
[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                         $message = html_entity_decode($message, ENT_COMPAT | ENT_HTML401, "UTF-8");
171                         $name = html_entity_decode($name, ENT_COMPAT | ENT_HTML401, "UTF-8");
172
173                         // Are the nofications calles from the regular process or via the friendica app?
174                         $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
175
176                         $a = get_app();
177
178                         if ($a->is_friendica_app() OR !$regularnotifications)
179                                 $message = str_replace("{0}", $name, $message);
180
181                         $data = array('href' => &$href, 'name' => &$name, 'url'=>&$url, 'photo'=>&$photo, 'date'=>&$date, 'seen'=>&$seen, 'messsage'=>&$message);
182                         call_hooks('ping_xmlize', $data);
183                         $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" >%s</note>'."\n";
184                         return sprintf ( $notsxml,
185                                 xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message)
186                         );
187                 }
188
189                 echo "<intro>$intro</intro>
190                                 <mail>$mail</mail>
191                                 <net>$network</net>
192                                 <home>$home</home>\r\n";
193                 if ($register!=0) echo "<register>$register</register>";
194
195                 echo "<all-events>$all_events</all-events>
196                         <all-events-today>$all_events_today</all-events-today>
197                         <events>$events</events>
198                         <events-today>$events_today</events-today>
199                         <birthdays>$birthdays</birthdays>
200                         <birthdays-today>$birthdays_today</birthdays-today>\r\n";
201
202                 $tot = $mail+$intro+$register+count($comments)+count($likes)+count($dislikes)+count($friends)+count($posts)+count($tags);
203
204                 if($firehose) {
205                         echo '  <notif count="'.$tot.'">';
206                 }
207                 else {
208                         if(count($z) && (! $sysnotify)) {
209                                 foreach($z as $zz) {
210                                         if($zz['seen'] == 0)
211                                                 $sysnotify ++;
212                                 }
213                         }
214
215                         echo '  <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
216
217                         if ($intro>0){
218                                 foreach ($intros as $i) {
219                                         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"));
220                                 };
221                         }
222                         if ($mail>0){
223                                 foreach ($mails as $i) {
224                                         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"));
225                                 };
226                         }
227                         if ($register>0){
228                                 foreach ($regs as $i) {
229                                         echo xmlize($a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen', "&rarr; ".t("{0} requested registration"));
230                                 };
231                         }
232
233                         if(count($z)) {
234                                 foreach($z as $zz) {
235                                         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'])));
236                                 }
237                         }
238                 }
239
240                 if($firehose) {
241                         if ($intro>0){
242                                 foreach ($intros as $i) {
243                                         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") );
244                                 };
245                         }
246                         if ($mail>0){
247                                 foreach ($mails as $i) {
248                                         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") );
249                                 };
250                         }
251                         if ($register>0){
252                                 foreach ($regs as $i) {
253                                         echo xmlize( $a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen',t("{0} requested registration") );
254                                 };
255                         }
256
257                         if (count($comments)){
258                                 foreach ($comments as $i) {
259                                         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'] ) );
260                                 };
261                         }
262                         if (count($likes)){
263                                 foreach ($likes as $i) {
264                                         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'] ) );
265                                 };
266                         }
267                         if (count($dislikes)){
268                                 foreach ($dislikes as $i) {
269                                         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'] ) );
270                                 };
271                         }
272                         if (count($friends)){
273                                 foreach ($friends as $i) {
274                                         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'] ) );
275                                 };
276                         }
277                         if (count($posts)){
278                                 foreach ($posts as $i) {
279                                         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") ) );
280                                 };
281                         }
282                         if (count($tags)){
283                                 foreach ($tags as $i) {
284                                         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'] ) );
285                                 };
286                         }
287
288                         if (count($cit)){
289                                 foreach ($cit as $i) {
290                                         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") );
291                                 };
292                         }
293                 }
294
295                 echo "  </notif>";
296         }
297         echo " <sysmsgs>";
298
299         if(x($_SESSION,'sysmsg')){
300                 foreach ($_SESSION['sysmsg'] as $m){
301                         echo "<notice>".xmlify($m)."</notice>";
302                 }
303                 unset($_SESSION['sysmsg']);
304         }
305         if(x($_SESSION,'sysmsg_info')){
306                 foreach ($_SESSION['sysmsg_info'] as $m){
307                         echo "<info>".xmlify($m)."</info>";
308                 }
309                 unset($_SESSION['sysmsg_info']);
310         }
311
312         echo " </sysmsgs>";
313         echo"</result>
314         ";
315
316         killme();
317 }
318
319 function ping_get_notifications($uid) {
320
321         $result = array();
322         $offset = 0;
323         $seen = false;
324         $seensql = "NOT";
325         $order = "";
326         $quit = false;
327
328         $a = get_app();
329
330         do {
331                 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
332                         FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
333                         WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
334                         AND NOT (`notify`.`type` IN (%d, %d))
335                         AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
336                         intval($uid),
337                         intval(NOTIFY_INTRO),
338                         intval(NOTIFY_MAIL),
339                         intval($offset)
340                 );
341
342                 if (!$r AND !$seen) {
343                         $seen = true;
344                         $seensql = "";
345                         $order = "DESC";
346                         $offset = 0;
347                 } elseif (!$r)
348                         $quit = true;
349                 else
350                         $offset += 50;
351
352                 foreach ($r AS $notification) {
353                         if (is_null($notification["visible"]))
354                                 $notification["visible"] = true;
355
356                         if (is_null($notification["spam"]))
357                                 $notification["spam"] = 0;
358
359                         if (is_null($notification["deleted"]))
360                                 $notification["deleted"] = 0;
361
362                         $notification["msg"] = strip_tags(bbcode($notification["msg"]));
363                         $notification["name"] = strip_tags(bbcode($notification["name"]));
364
365                         // Replace the name with {0} but ensure to make that only once
366                         // The {0} is used later and prints the name in bold.
367
368                         $pos = strpos($notification["msg"],$notification['name']);
369                         if ($pos !== false)
370                                 $notification["msg"] = substr_replace($notification["msg"],"{0}",$pos,strlen($notification["name"]));
371
372                         if ($notification["visible"] AND !$notification["spam"] AND
373                                 !$notification["deleted"] AND !is_array($result[$notification["parent"]]))
374                                 $result[$notification["parent"]] = $notification;
375                 }
376
377         } while ((count($result) < 50) AND !$quit);
378
379         return($result);
380 }