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');
10 * @brief Outputs the counts and the lists of various notifications
12 * The output format can be controlled via the GET parameter 'format'. It can be
13 * - xml (deprecated legacy default)
14 * - json (outputs JSONP with the 'callback' GET parameter)
16 * Expected JSON structure:
25 * "all-events-today": 0,
29 * "birthdays-today": 0,
33 * "notifications": [ ],
41 * @param App $a The Friendica App instance
43 function ping_init(App $a)
47 if (isset($_GET['format']) && $_GET['format'] == 'json') {
59 $notifications = array();
67 $groups_unseen = array();
68 $forums_unseen = array();
71 $all_events_today = 0;
78 $data['intro'] = $intro_count;
79 $data['mail'] = $mail_count;
80 $data['net'] = $network_count;
81 $data['home'] = $home_count;
82 $data['register'] = $register_count;
84 $data['all-events'] = $all_events;
85 $data['all-events-today'] = $all_events_today;
86 $data['events'] = $events;
87 $data['events-today'] = $events_today;
88 $data['birthdays'] = $birthdays;
89 $data['birthdays-today'] = $birthdays_today;
92 // Different login session than the page that is calling us.
93 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
95 $data = array('result' => array('invalid' => 1));
97 if ($format == 'json') {
98 if (isset($_GET['callback'])) {
100 header("Content-type: application/javascript");
101 echo $_GET['callback'] . '(' . json_encode($data) . ')';
103 header("Content-type: application/json");
104 echo json_encode($data);
107 header("Content-type: text/xml");
108 echo xml::from_array($data, $xml);
113 $notifs = ping_get_notifications(local_user());
115 $items_unseen = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
116 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
117 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
118 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
119 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
120 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
121 AND `item`.`contact-id` != %d
122 ORDER BY `item`.`created` DESC",
123 intval(local_user()), intval(local_user())
126 if (dbm::is_result($items_unseen)) {
127 $arr = array('items' => $items_unseen);
128 call_hooks('network_ping', $arr);
130 foreach ($items_unseen as $item) {
139 if ($network_count) {
140 if (intval(feature_enabled(local_user(), 'groups'))) {
141 // Find out how unseen network posts are spread across groups
142 $group_counts = groups_count_unseen();
143 if (dbm::is_result($group_counts)) {
144 foreach ($group_counts as $group_count) {
145 if ($group_count['count'] > 0) {
146 $groups_unseen[] = $group_count;
152 if (intval(feature_enabled(local_user(), 'forumlist_widget'))) {
153 $forum_counts = ForumManager::count_unseen_items();
154 if (dbm::is_result($forums_counts)) {
155 foreach ($forums_counts as $forum_count) {
156 if ($forum_count['count'] > 0) {
157 $forums_unseen[] = $forum_count;
164 $intros1 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
165 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
166 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
167 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
170 $intros2 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
171 `contact`.`name`, `contact`.`url`, `contact`.`photo`
172 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
173 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
177 $intro_count = count($intros1) + count($intros2);
178 $intros = $intros1 + $intros2;
180 $myurl = App::get_baseurl() . '/profile/' . $a->user['nickname'] ;
181 $mails = qu("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
182 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
183 intval(local_user()),
186 $mail_count = count($mails);
188 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
189 $regs = qu("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total`
190 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
191 WHERE `contact`.`self` = 1");
193 if (dbm::is_result($regs)) {
194 $register_count = $regs[0]['total'];
198 $ev = qu("SELECT count(`event`.`id`) AS total, type, start, adjust FROM `event`
199 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
200 ORDER BY `start` ASC ",
201 intval(local_user()),
202 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
203 dbesc(datetime_convert('UTC', 'UTC', 'now'))
206 if (dbm::is_result($ev)) {
207 $all_events = intval($ev[0]['total']);
210 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
213 if ($x['type'] === 'birthday') {
220 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
221 $all_events_today ++;
231 $data['intro'] = $intro_count;
232 $data['mail'] = $mail_count;
233 $data['net'] = $network_count;
234 $data['home'] = $home_count;
235 $data['register'] = $register_count;
237 $data['all-events'] = $all_events;
238 $data['all-events-today'] = $all_events_today;
239 $data['events'] = $events;
240 $data['events-today'] = $events_today;
241 $data['birthdays'] = $birthdays;
242 $data['birthdays-today'] = $birthdays_today;
244 if (dbm::is_result($notifs)) {
245 foreach ($notifs as $notif) {
246 if ($notif['seen'] == 0) {
252 // merge all notification types in one array
253 if (dbm::is_result($intros)) {
254 foreach ($intros as $intro) {
256 'href' => App::get_baseurl() . '/notifications/intros/' . $intro['id'],
257 'name' => $intro['name'],
258 'url' => $intro['url'],
259 'photo' => $intro['photo'],
260 'date' => $intro['datetime'],
262 'message' => t('{0} wants to be your friend'),
268 if (dbm::is_result($mails)) {
269 foreach ($mails as $mail) {
271 'href' => App::get_baseurl() . '/message/' . $mail['id'],
272 'name' => $mail['from-name'],
273 'url' => $mail['from-url'],
274 'photo' => $mail['from-photo'],
275 'date' => $mail['created'],
277 'message' => t('{0} sent you a message'),
283 if (dbm::is_result($regs)) {
284 foreach ($regs as $reg) {
286 'href' => App::get_baseurl() . '/admin/users/',
287 'name' => $reg['name'],
288 'url' => $reg['url'],
289 'photo' => $reg['micro'],
290 'date' => $reg['created'],
292 'message' => t('{0} requested registration'),
298 // sort notifications by $[]['date']
299 $sort_function = function($a, $b) {
300 $adate = date($a['date']);
301 $bdate = date($b['date']);
302 if ($adate == $bdate) {
305 return ($adate < $bdate) ? 1 : -1;
307 usort($notifs, $sort_function);
309 if (dbm::is_result($notifs)) {
310 // Are the nofications called from the regular process or via the friendica app?
311 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
313 foreach ($notifs as $notif) {
314 if ($a->is_friendica_app() OR !$regularnotifications) {
315 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
318 $contact = get_contact_details_by_url($notif['url']);
319 if (isset($contact['micro'])) {
320 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
322 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
325 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
327 $notifications[] = array(
328 'id' => $notif['id'],
329 'href' => $notif['href'],
330 'name' => $notif['name'],
331 'url' => $notif['url'],
332 'photo' => $notif['photo'],
333 'date' => relative_date($notif['date']),
334 'message' => $notif['message'],
335 'seen' => $notif['seen'],
336 'timestamp' => strtotime($local_time)
343 $sysmsgs_info = array();
345 if (x($_SESSION, 'sysmsg')) {
346 $sysmsgs = $_SESSION['sysmsg'];
347 unset($_SESSION['sysmsg']);
350 if (x($_SESSION, 'sysmsg_info')) {
351 $sysmsgs_info = $_SESSION['sysmsg_info'];
352 unset($_SESSION['sysmsg_info']);
355 if ($format == 'json') {
356 $data['groups'] = $groups_unseen;
357 $data['forums'] = $forums_unseen;
358 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
359 $data['notifications'] = $notifications;
360 $data['sysmsgs'] = array(
361 'notice' => $sysmsgs,
362 'info' => $sysmsgs_info
365 $json_payload = json_encode(array("result" => $data));
367 if (isset($_GET['callback'])) {
369 header("Content-type: application/javascript");
370 echo $_GET['callback'] . '(' . $json_payload . ')';
372 header("Content-type: application/json");
376 // Legacy slower XML format output
377 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
379 header("Content-type: text/xml");
380 echo xml::from_array(array("result" => $data), $xml);
387 * @brief Retrieves the notifications array for the given user ID
389 * @param int $uid User id
390 * @return array Associative array of notifications
392 function ping_get_notifications($uid)
404 $r = qu("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
405 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
406 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
407 AND NOT (`notify`.`type` IN (%d, %d))
408 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
410 intval(NOTIFY_INTRO),
415 if (!$r AND !$seen) {
426 foreach ($r AS $notification) {
427 if (is_null($notification["visible"])) {
428 $notification["visible"] = true;
431 if (is_null($notification["spam"])) {
432 $notification["spam"] = 0;
435 if (is_null($notification["deleted"])) {
436 $notification["deleted"] = 0;
439 if ($notification["msg_cache"]) {
440 $notification["name"] = $notification["name_cache"];
441 $notification["message"] = $notification["msg_cache"];
443 $notification["name"] = strip_tags(bbcode($notification["name"]));
444 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
446 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
447 dbesc($notification["name"]),
448 dbesc($notification["message"]),
449 intval($notification["id"])
453 $notification["href"] = App::get_baseurl() . "/notify/view/" . $notification["id"];
455 if ($notification["visible"] AND !$notification["spam"] AND
456 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
457 $result[$notification["parent"]] = $notification;
460 } while ((count($result) < 50) AND !$quit);
466 * @brief Backward-compatible XML formatting for ping.php output
469 * @param array $data The initial ping data array
470 * @param int $sysnotify_count Number of unseen system notifications
471 * @param array $notifs Complete list of notification
472 * @param array $sysmsgs List of system notice messages
473 * @param array $sysmsgs_info List of system info messages
474 * @param int $groups_unseen Number of unseen group items
475 * @param int $forums_unseen Number of unseen forum items
476 * @return array XML-transform ready data array
478 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
480 $notifications = array();
481 foreach($notifs as $key => $notif) {
482 $notifications[$key . ':note'] = $notif['message'];
484 $notifications[$key . ':@attributes'] = array(
485 'id' => $notif['id'],
486 'href' => $notif['href'],
487 'name' => $notif['name'],
488 'url' => $notif['url'],
489 'photo' => $notif['photo'],
490 'date' => $notif['date'],
491 'seen' => $notif['seen'],
492 'timestamp' => $notif['timestamp']
497 foreach ($sysmsgs as $key => $m){
498 $sysmsg[$key . ':notice'] = $m;
500 foreach ($sysmsgs_info as $key => $m){
501 $sysmsg[$key . ':info'] = $m;
504 $data['notif'] = $notifications;
505 $data['@attributes'] = array('count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']);
506 $data['sysmsgs'] = $sysmsg;
508 if ($data['register'] == 0) {
509 unset($data['register']);
513 if (count($groups_unseen)) {
514 foreach ($groups_unseen as $key => $item) {
515 $groups[$key . ':group'] = $item['count'];
516 $groups[$key . ':@attributes'] = array('id' => $item['id']);
518 $data['groups'] = $groups;
522 if (count($forums_unseen)) {
523 foreach ($forums_unseen as $key => $item) {
524 $forums[$count . ':forum'] = $item['count'];
525 $forums[$count . ':@attributes'] = array('id' => $item['id']);
527 $data['forums'] = $forums;