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 ORDER BY `item`.`created` DESC",
52 $arr = array('items' => $r);
53 call_hooks('network_ping', $arr);
64 $obj = parse_xml_string($xmlhead.$it['object']);
65 $it['tname'] = $obj->content;
71 case ACTIVITY_DISLIKE:
75 $obj = parse_xml_string($xmlhead.$it['object']);
76 $it['fname'] = $obj->title;
80 if ($it['parent']!=$it['id']) {
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 " .
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) . '"/>';
105 if(intval(feature_enabled(local_user(),'forumlist_widget'))) {
106 $forums_unseen = forums_count_unseen();
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",
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",
123 $intro = count($intros1) + count($intros2);
124 $intros = $intros1+$intros2;
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()),
134 $mail = count($mails);
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");
139 $register = $regs[0]['total'];
145 $all_events_today = 0;
149 $birthdays_today = 0;
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'))
160 if($ev && count($ev)) {
161 $all_events = intval($ev[0]['total']);
164 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
167 if($x['type'] === 'birthday') {
174 if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
175 $all_events_today ++;
187 * return xml from notification array
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
199 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
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");
204 // Are the nofications calles from the regular process or via the friendica app?
205 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
209 if ($a->is_friendica_app() OR !$regularnotifications)
210 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
212 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
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'])
223 echo "<intro>$intro</intro>
226 <home>$home</home>\r\n";
227 if ($register!=0) echo "<register>$register</register>";
229 if ( count($network_group) ) {
231 foreach ($network_group as $it) {
232 echo '<group id="' . $it['id'] . '">' . $it['count'] . "</group>";
237 if ( count($forums_unseen) ) {
239 foreach ($forums_unseen as $it) {
240 echo '<forum id="' . $it['id'] . '">' . $it['count'] . "</forum>";
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";
253 if(count($notifs) && (! $sysnotify)) {
254 foreach($notifs as $zz) {
260 echo ' <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
262 // merge all notification types in one array
264 foreach ($intros as $i) {
266 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
267 'name' => $i['name'],
269 'photo' => $i['photo'],
270 'date' => $i['datetime'],
272 'message' => t("{0} wants to be your friend"),
279 foreach ($mails as $i) {
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'],
287 'message' => t("{0} sent you a message"),
294 foreach ($regs as $i) {
296 'href' => $a->get_baseurl().'/admin/users/',
297 'name' => $i['name'],
299 'photo' => $i['micro'],
300 'date' => $i['created'],
302 'message' => t("{0} requested registration"),
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) {
314 return ($adate < $bdate) ? 1 : -1;
316 usort($notifs, $sort_function);
319 foreach($notifs as $n) {
329 if(x($_SESSION,'sysmsg')){
330 foreach ($_SESSION['sysmsg'] as $m){
331 echo "<notice>".xmlify($m)."</notice>";
333 unset($_SESSION['sysmsg']);
335 if(x($_SESSION,'sysmsg_info')){
336 foreach ($_SESSION['sysmsg_info'] as $m){
337 echo "<info>".xmlify($m)."</info>";
339 unset($_SESSION['sysmsg_info']);
349 function ping_get_notifications($uid) {
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",
367 intval(NOTIFY_INTRO),
372 if (!$r AND !$seen) {
383 foreach ($r AS $notification) {
384 if (is_null($notification["visible"]))
385 $notification["visible"] = true;
387 if (is_null($notification["spam"]))
388 $notification["spam"] = 0;
390 if (is_null($notification["deleted"]))
391 $notification["deleted"] = 0;
393 $notification["message"] = strip_tags(bbcode($notification["msg"]));
394 $notification["name"] = strip_tags(bbcode($notification["name"]));
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.
399 $pos = strpos($notification["message"],$notification['name']);
401 $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
403 $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
405 if ($notification["visible"] AND !$notification["spam"] AND
406 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
407 $result[$notification["parent"]] = $notification;
411 } while ((count($result) < 50) AND !$quit);