2 require_once("include/datetime.php");
3 require_once('include/bbcode.php');
4 require_once('include/ForumManager.php');
5 require_once('include/group.php');
6 require_once("mod/proxy.php");
7 require_once('include/xml.php');
9 function ping_init(&$a) {
11 $xmlhead = "<"."?xml version='1.0' encoding='UTF-8' ?".">";
14 // Different login session than the page that is calling us.
15 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
16 $data = array("invalid" => 1);
17 header("Content-type: text/xml");
18 echo xml::from_array(array("result" => $data), $xml);
22 $notifs = ping_get_notifications(local_user());
23 $sysnotify = 0; // we will update this in a moment
36 $groups_unseen = array();
37 $forums_unseen = array();
39 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
40 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
41 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
42 FROM `item` INNER JOIN `item` as `pitem` ON `pitem`.`id`=`item`.`parent`
43 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
44 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
45 AND `item`.`contact-id` != %d
46 ORDER BY `item`.`created` DESC",
47 intval(local_user()), intval(local_user())
50 if (dbm::is_result($r)) {
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']) {
91 if (intval(feature_enabled(local_user(),'groups'))) {
92 // Find out how unseen network posts are spread across groups
93 $groups_unseen = groups_count_unseen();
96 if (intval(feature_enabled(local_user(),'forumlist_widget'))) {
97 $forums_unseen = ForumManager::count_unseen_items();
101 $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
102 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
103 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
104 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
107 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
108 `contact`.`name`, `contact`.`url`, `contact`.`photo`
109 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
110 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
114 $intro = count($intros1) + count($intros2);
115 $intros = $intros1+$intros2;
117 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
118 $mails = q("SELECT * FROM `mail`
119 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
120 intval(local_user()),
123 $mail = count($mails);
125 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
126 $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
127 $register = count($regs);
133 $all_events_today = 0;
137 $birthdays_today = 0;
140 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
141 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
142 ORDER BY `start` ASC ",
143 intval(local_user()),
144 dbesc(datetime_convert('UTC','UTC','now + 7 days')),
145 dbesc(datetime_convert('UTC','UTC','now'))
148 if (dbm::is_result($ev)) {
149 $all_events = intval($ev[0]['total']);
152 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
155 if ($x['type'] === 'birthday') {
162 if (datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
163 $all_events_today ++;
174 $data["intro"] = $intro;
175 $data["mail"] = $mail;
176 $data["net"] = $network;
177 $data["home"] = $home;
180 $data["register"] = $register;
184 if (dbm::is_result($groups_unseen)) {
186 foreach ($groups_unseen as $it)
187 if ($it['count'] > 0) {
189 $groups[$count.":group"] = $it['count'];
190 $groups[$count.":@attributes"] = array("id" => $it['id']);
192 $data["groups"] = $groups;
197 if (dbm::is_result($forums_unseen)) {
199 foreach ($forums_unseen as $it)
200 if ($it['count'] > 0) {
202 $forums[$count.":forum"] = $it['count'];
203 $forums[$count.":@attributes"] = array("id" => $it['id']);
205 $data["forums"] = $forums;
208 $data["all-events"] = $all_events;
209 $data["all-events-today"] = $all_events_today;
210 $data["events"] = $events;
211 $data["events-today"] = $events_today;
212 $data["birthdays"] = $birthdays;
213 $data["birthdays-today"] = $birthdays_today;
216 if (dbm::is_result($notifs) && !$sysnotify) {
217 foreach ($notifs as $zz) {
218 if ($zz['seen'] == 0)
223 // merge all notification types in one array
224 if (dbm::is_result($intros)) {
225 foreach ($intros as $i) {
227 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
228 'name' => $i['name'],
230 'photo' => $i['photo'],
231 'date' => $i['datetime'],
233 'message' => t("{0} wants to be your friend"),
239 if (dbm::is_result($mails)) {
240 foreach ($mails as $i) {
242 'href' => $a->get_baseurl().'/message/'.$i['id'],
243 'name' => $i['from-name'],
244 'url' => $i['from-url'],
245 'photo' => $i['from-photo'],
246 'date' => $i['created'],
248 'message' => t("{0} sent you a message"),
254 if (dbm::is_result($regs)) {
255 foreach ($regs as $i) {
257 'href' => $a->get_baseurl().'/admin/users/',
258 'name' => $i['name'],
260 'photo' => $i['micro'],
261 'date' => $i['created'],
263 'message' => t("{0} requested registration"),
269 // sort notifications by $[]['date']
270 $sort_function = function($a, $b) {
271 $adate = date($a['date']);
272 $bdate = date($b['date']);
273 if ($adate == $bdate) {
276 return ($adate < $bdate) ? 1 : -1;
278 usort($notifs, $sort_function);
280 if (dbm::is_result($notifs)) {
282 // Are the nofications calles from the regular process or via the friendica app?
283 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
286 foreach($notifs as $n) {
288 if ($a->is_friendica_app() OR !$regularnotifications)
289 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
291 $notifications[$count.":note"] = $n['message'];
293 $contact = get_contact_details_by_url($n['url']);
294 if (isset($contact["micro"]))
295 $n['photo'] = proxy_url($contact["micro"], false, PROXY_SIZE_MICRO);
297 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
299 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
301 call_hooks('ping_xmlize', $n);
303 $notifications[$count.":@attributes"] = array("id" => $n["id"],
304 "href" => $n['href'],
305 "name" => $n['name'],
307 "photo" => $n['photo'],
308 "date" => relative_date($n['date']),
309 "seen" => $n['seen'],
310 "timestamp" => strtotime($local_time));
315 $data["notif"] = $notifications;
316 $data["@attributes"] = array("count" => $sysnotify + $intro + $mail + $register);
321 if (x($_SESSION,'sysmsg')){
323 foreach ($_SESSION['sysmsg'] as $m){
325 $sysmsg[$count.":notice"] = $m;
327 unset($_SESSION['sysmsg']);
330 if (x($_SESSION,'sysmsg_info')){
332 foreach ($_SESSION['sysmsg_info'] as $m){
334 $sysmsg[$count.":info"] = $m;
336 unset($_SESSION['sysmsg_info']);
339 $data["sysmsgs"] = $sysmsg;
341 header("Content-type: text/xml");
342 echo xml::from_array(array("result" => $data), $xml);
346 function ping_get_notifications($uid) {
358 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
359 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
360 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
361 AND NOT (`notify`.`type` IN (%d, %d))
362 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
364 intval(NOTIFY_INTRO),
369 if (!$r AND !$seen) {
380 foreach ($r AS $notification) {
381 if (is_null($notification["visible"]))
382 $notification["visible"] = true;
384 if (is_null($notification["spam"]))
385 $notification["spam"] = 0;
387 if (is_null($notification["deleted"]))
388 $notification["deleted"] = 0;
390 $notification["message"] = strip_tags(bbcode($notification["msg"]));
391 $notification["name"] = strip_tags(bbcode($notification["name"]));
393 // Replace the name with {0} but ensure to make that only once
394 // The {0} is used later and prints the name in bold.
396 if ($notification['name'] != "")
397 $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);