2 require_once("include/datetime.php");
3 require_once('include/bbcode.php');
4 require_once('include/forums.php');
5 require_once("mod/proxy.php");
7 function ping_init(&$a) {
9 header("Content-type: text/xml");
11 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
14 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
18 // Different login session than the page that is calling us.
20 if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
21 echo '<invalid>1</invalid></result>';
25 $notifs = ping_get_notifications(local_user());
26 $sysnotify = 0; // we will update this in a moment
37 $network_group = array();
38 $forums_unseen = array();
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())
53 $arr = array('items' => $r);
54 call_hooks('network_ping', $arr);
65 $obj = parse_xml_string($xmlhead.$it['object']);
66 $it['tname'] = $obj->content;
72 case ACTIVITY_DISLIKE:
76 $obj = parse_xml_string($xmlhead.$it['object']);
77 $it['fname'] = $obj->title;
81 if ($it['parent']!=$it['id']) {
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 " .
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) . '"/>';
106 if(intval(feature_enabled(local_user(),'forumlist_widget'))) {
107 $forums_unseen = forums_count_unseen();
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",
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",
124 $intro = count($intros1) + count($intros2);
125 $intros = $intros1+$intros2;
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()),
135 $mail = count($mails);
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");
140 $register = $regs[0]['total'];
146 $all_events_today = 0;
150 $birthdays_today = 0;
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'))
161 if($ev && count($ev)) {
162 $all_events = intval($ev[0]['total']);
165 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
168 if($x['type'] === 'birthday') {
175 if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
176 $all_events_today ++;
188 * return xml from notification array
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
200 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
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");
205 // Are the nofications calles from the regular process or via the friendica app?
206 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
210 if ($a->is_friendica_app() OR !$regularnotifications)
211 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
213 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
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'])
224 echo "<intro>$intro</intro>
227 <home>$home</home>\r\n";
228 if ($register!=0) echo "<register>$register</register>";
230 if ( count($network_group) ) {
232 foreach ($network_group as $it) {
233 echo '<group id="' . $it['id'] . '">' . $it['count'] . "</group>";
238 if ( count($forums_unseen) ) {
240 foreach ($forums_unseen as $it) {
241 echo '<forum id="' . $it['id'] . '">' . $it['count'] . "</forum>";
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";
254 if(count($notifs) && (! $sysnotify)) {
255 foreach($notifs as $zz) {
261 echo ' <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
263 // merge all notification types in one array
265 foreach ($intros as $i) {
267 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
268 'name' => $i['name'],
270 'photo' => $i['photo'],
271 'date' => $i['datetime'],
273 'message' => t("{0} wants to be your friend"),
280 foreach ($mails as $i) {
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'],
288 'message' => t("{0} sent you a message"),
295 foreach ($regs as $i) {
297 'href' => $a->get_baseurl().'/admin/users/',
298 'name' => $i['name'],
300 'photo' => $i['micro'],
301 'date' => $i['created'],
303 'message' => t("{0} requested registration"),
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) {
315 return ($adate < $bdate) ? 1 : -1;
317 usort($notifs, $sort_function);
320 foreach($notifs as $n) {
330 if(x($_SESSION,'sysmsg')){
331 foreach ($_SESSION['sysmsg'] as $m){
332 echo "<notice>".xmlify($m)."</notice>";
334 unset($_SESSION['sysmsg']);
336 if(x($_SESSION,'sysmsg_info')){
337 foreach ($_SESSION['sysmsg_info'] as $m){
338 echo "<info>".xmlify($m)."</info>";
340 unset($_SESSION['sysmsg_info']);
350 function ping_get_notifications($uid) {
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",
368 intval(NOTIFY_INTRO),
373 if (!$r AND !$seen) {
384 foreach ($r AS $notification) {
385 if (is_null($notification["visible"]))
386 $notification["visible"] = true;
388 if (is_null($notification["spam"]))
389 $notification["spam"] = 0;
391 if (is_null($notification["deleted"]))
392 $notification["deleted"] = 0;
394 $notification["message"] = strip_tags(bbcode($notification["msg"]));
395 $notification["name"] = strip_tags(bbcode($notification["name"]));
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.
400 $pos = strpos($notification["message"],$notification['name']);
402 $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
404 $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
406 if ($notification["visible"] AND !$notification["spam"] AND
407 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
408 $result[$notification["parent"]] = $notification;
412 } while ((count($result) < 50) AND !$quit);