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