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