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");
8 function ping_init(&$a) {
10 header("Content-type: text/xml");
12 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
15 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
19 // Different login session than the page that is calling us.
21 if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
22 echo '<invalid>1</invalid></result>';
26 $notifs = ping_get_notifications(local_user());
27 $sysnotify = 0; // we will update this in a moment
38 $groups_unseen = array();
39 $forums_unseen = array();
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())
54 $arr = array('items' => $r);
55 call_hooks('network_ping', $arr);
66 $obj = parse_xml_string($xmlhead.$it['object']);
67 $it['tname'] = $obj->content;
73 case ACTIVITY_DISLIKE:
77 $obj = parse_xml_string($xmlhead.$it['object']);
78 $it['fname'] = $obj->title;
82 if ($it['parent']!=$it['id']) {
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();
98 if(intval(feature_enabled(local_user(),'forumlist_widget'))) {
99 $forums_unseen = forums_count_unseen();
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",
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",
116 $intro = count($intros1) + count($intros2);
117 $intros = $intros1+$intros2;
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()),
127 $mail = count($mails);
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");
132 $register = $regs[0]['total'];
138 $all_events_today = 0;
142 $birthdays_today = 0;
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'))
153 if($ev && count($ev)) {
154 $all_events = intval($ev[0]['total']);
157 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
160 if($x['type'] === 'birthday') {
167 if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
168 $all_events_today ++;
180 * return xml from notification array
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
192 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
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");
197 // Are the nofications calles from the regular process or via the friendica app?
198 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
202 if ($a->is_friendica_app() OR !$regularnotifications)
203 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
205 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
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'])
216 echo "<intro>$intro</intro>
219 <home>$home</home>\r\n";
220 if ($register!=0) echo "<register>$register</register>";
222 if ( count($groups_unseen) ) {
224 foreach ($groups_unseen as $it) {
225 echo '<group id="' . $it['id'] . '">' . $it['count'] . "</group>";
230 if ( count($forums_unseen) ) {
232 foreach ($forums_unseen as $it) {
233 echo '<forum id="' . $it['id'] . '">' . $it['count'] . "</forum>";
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";
246 if(count($notifs) && (! $sysnotify)) {
247 foreach($notifs as $zz) {
253 echo ' <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
255 // merge all notification types in one array
257 foreach ($intros as $i) {
259 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
260 'name' => $i['name'],
262 'photo' => $i['photo'],
263 'date' => $i['datetime'],
265 'message' => t("{0} wants to be your friend"),
272 foreach ($mails as $i) {
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'],
280 'message' => t("{0} sent you a message"),
287 foreach ($regs as $i) {
289 'href' => $a->get_baseurl().'/admin/users/',
290 'name' => $i['name'],
292 'photo' => $i['micro'],
293 'date' => $i['created'],
295 'message' => t("{0} requested registration"),
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) {
307 return ($adate < $bdate) ? 1 : -1;
309 usort($notifs, $sort_function);
312 foreach($notifs as $n) {
322 if(x($_SESSION,'sysmsg')){
323 foreach ($_SESSION['sysmsg'] as $m){
324 echo "<notice>".xmlify($m)."</notice>";
326 unset($_SESSION['sysmsg']);
328 if(x($_SESSION,'sysmsg_info')){
329 foreach ($_SESSION['sysmsg_info'] as $m){
330 echo "<info>".xmlify($m)."</info>";
332 unset($_SESSION['sysmsg_info']);
342 function ping_get_notifications($uid) {
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",
360 intval(NOTIFY_INTRO),
365 if (!$r AND !$seen) {
376 foreach ($r AS $notification) {
377 if (is_null($notification["visible"]))
378 $notification["visible"] = true;
380 if (is_null($notification["spam"]))
381 $notification["spam"] = 0;
383 if (is_null($notification["deleted"]))
384 $notification["deleted"] = 0;
386 $notification["message"] = strip_tags(bbcode($notification["msg"]));
387 $notification["name"] = strip_tags(bbcode($notification["name"]));
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.
392 $pos = strpos($notification["message"],$notification['name']);
394 $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
396 $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
398 if ($notification["visible"] AND !$notification["spam"] AND
399 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
400 $result[$notification["parent"]] = $notification;
404 } while ((count($result) < 50) AND !$quit);