2 require_once("include/datetime.php");
3 require_once('include/bbcode.php');
4 require_once("mod/proxy.php");
6 function ping_init(&$a) {
8 header("Content-type: text/xml");
10 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
13 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
17 // Different login session than the page that is calling us.
19 if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
20 echo '<invalid>1</invalid></result>';
24 $notifs = ping_get_notifications(local_user());
25 $sysnotify = 0; // we will update this in a moment
36 $network_group = array();
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 ORDER BY `item`.`created` DESC",
50 $arr = array('items' => $r);
51 call_hooks('network_ping', $arr);
62 $obj = parse_xml_string($xmlhead.$it['object']);
63 $it['tname'] = $obj->content;
69 case ACTIVITY_DISLIKE:
73 $obj = parse_xml_string($xmlhead.$it['object']);
74 $it['fname'] = $obj->title;
78 if ($it['parent']!=$it['id']) {
90 # Find out how unseen network posts are spread across groups
91 $sql = "SELECT g.id, g.name, count(i.id) count " .
92 "FROM `group` g, group_member gm, item i " .
95 "AND i.unseen AND i.visible " .
96 "AND NOT i.deleted " .
97 "AND i.`contact-id` = gm.`contact-id` " .
98 "AND gm.gid = g.id GROUP BY g.id";
99 #echo '<SQL id="' . intval(local_user()) . '">' . $sql . '</SQL>';
100 $network_group = q($sql, intval(local_user()), intval(local_user()));
101 #echo '<COUNT R="' . count($network_group) . '"/>';
104 $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
105 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
106 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
107 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
110 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
111 `contact`.`name`, `contact`.`url`, `contact`.`photo`
112 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
113 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
117 $intro = count($intros1) + count($intros2);
118 $intros = $intros1+$intros2;
122 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
123 $mails = q("SELECT * FROM `mail`
124 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
125 intval(local_user()),
128 $mail = count($mails);
130 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
131 $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 $register = $regs[0]['total'];
139 $all_events_today = 0;
143 $birthdays_today = 0;
146 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
147 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
148 ORDER BY `start` ASC ",
149 intval(local_user()),
150 dbesc(datetime_convert('UTC','UTC','now + 7 days')),
151 dbesc(datetime_convert('UTC','UTC','now'))
154 if($ev && count($ev)) {
155 $all_events = intval($ev[0]['total']);
158 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
161 if($x['type'] === 'birthday') {
168 if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
169 $all_events_today ++;
181 * return xml from notification array
183 * @param array $n Notification array:
184 * 'href' => notification link
185 * 'name' => subject name
186 * 'url' => subject url
187 * 'photo' => subject photo
188 * 'date' => notification date
189 * 'seen' => bool true/false
190 * 'message' => notification message. "{0}" will be replaced by subject name
193 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
195 $n['message'] = html_entity_decode($n['message'], ENT_COMPAT | ENT_HTML401, "UTF-8");
196 $n['name'] = html_entity_decode($n['name'], ENT_COMPAT | ENT_HTML401, "UTF-8");
198 // Are the nofications calles from the regular process or via the friendica app?
199 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
203 if ($a->is_friendica_app() OR !$regularnotifications)
204 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
206 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
208 call_hooks('ping_xmlize', $n);
209 $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" timestamp="%s" >%s</note>'."\n";
210 return sprintf ( $notsxml,
211 xmlify($n['href']), xmlify($n['name']), xmlify($n['url']), xmlify($n['photo']),
212 xmlify(relative_date($n['date'])), xmlify($n['seen']), xmlify(strtotime($local_time)),
213 xmlify($n['message'])
217 echo "<intro>$intro</intro>
220 <home>$home</home>\r\n";
221 if ($register!=0) echo "<register>$register</register>";
222 if ( count($network_group) ) {
224 foreach ($network_group as $it) {
225 echo '<group id="' . $it['id'] . '">' . $it['count'] . "</group>";
230 echo "<all-events>$all_events</all-events>
231 <all-events-today>$all_events_today</all-events-today>
232 <events>$events</events>
233 <events-today>$events_today</events-today>
234 <birthdays>$birthdays</birthdays>
235 <birthdays-today>$birthdays_today</birthdays-today>\r\n";
238 if(count($notifs) && (! $sysnotify)) {
239 foreach($notifs as $zz) {
245 echo ' <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
247 // merge all notification types in one array
249 foreach ($intros as $i) {
251 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
252 'name' => $i['name'],
254 'photo' => $i['photo'],
255 'date' => $i['datetime'],
257 'message' => t("{0} wants to be your friend"),
264 foreach ($mails as $i) {
266 'href' => $a->get_baseurl().'/message/'.$i['id'],
267 'name' => $i['from-name'],
268 'url' => $i['from-url'],
269 'photo' => $i['from-photo'],
270 'date' => $i['created'],
272 'message' => t("{0} sent you a message"),
279 foreach ($regs as $i) {
281 'href' => $a->get_baseurl().'/admin/users/',
282 'name' => $i['name'],
284 'photo' => $i['micro'],
285 'date' => $i['created'],
287 'message' => t("{0} requested registration"),
292 // sort notifications by $[]['date']
293 $sort_function = function($a, $b) {
294 $adate = date($a['date']);
295 $bdate = date($b['date']);
296 if ($adate == $bdate) {
299 return ($adate < $bdate) ? 1 : -1;
301 usort($notifs, $sort_function);
304 foreach($notifs as $n) {
314 if(x($_SESSION,'sysmsg')){
315 foreach ($_SESSION['sysmsg'] as $m){
316 echo "<notice>".xmlify($m)."</notice>";
318 unset($_SESSION['sysmsg']);
320 if(x($_SESSION,'sysmsg_info')){
321 foreach ($_SESSION['sysmsg_info'] as $m){
322 echo "<info>".xmlify($m)."</info>";
324 unset($_SESSION['sysmsg_info']);
334 function ping_get_notifications($uid) {
346 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
347 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
348 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
349 AND NOT (`notify`.`type` IN (%d, %d))
350 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
352 intval(NOTIFY_INTRO),
357 if (!$r AND !$seen) {
368 foreach ($r AS $notification) {
369 if (is_null($notification["visible"]))
370 $notification["visible"] = true;
372 if (is_null($notification["spam"]))
373 $notification["spam"] = 0;
375 if (is_null($notification["deleted"]))
376 $notification["deleted"] = 0;
378 $notification["message"] = strip_tags(bbcode($notification["msg"]));
379 $notification["name"] = strip_tags(bbcode($notification["name"]));
381 // Replace the name with {0} but ensure to make that only once
382 // The {0} is used later and prints the name in bold.
384 $pos = strpos($notification["message"],$notification['name']);
386 $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
388 $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
390 if ($notification["visible"] AND !$notification["spam"] AND
391 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
392 $result[$notification["parent"]] = $notification;
396 } while ((count($result) < 50) AND !$quit);