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