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