5 require_once('include/datetime.php');
6 require_once('include/bbcode.php');
7 require_once('include/ForumManager.php');
8 require_once('include/group.php');
9 require_once('mod/proxy.php');
10 require_once('include/xml.php');
11 require_once('include/cache.php');
12 require_once('include/enotify.php');
15 * @brief Outputs the counts and the lists of various notifications
17 * The output format can be controlled via the GET parameter 'format'. It can be
18 * - xml (deprecated legacy default)
19 * - json (outputs JSONP with the 'callback' GET parameter)
21 * Expected JSON structure:
30 * "all-events-today": 0,
34 * "birthdays-today": 0,
38 * "notifications": [ ],
46 * @param App $a The Friendica App instance
48 function ping_init(App $a)
52 if (isset($_GET['format']) && $_GET['format'] == 'json') {
64 $notifications = array();
72 $groups_unseen = array();
73 $forums_unseen = array();
76 $all_events_today = 0;
83 $data['intro'] = $intro_count;
84 $data['mail'] = $mail_count;
85 $data['net'] = $network_count;
86 $data['home'] = $home_count;
87 $data['register'] = $register_count;
89 $data['all-events'] = $all_events;
90 $data['all-events-today'] = $all_events_today;
91 $data['events'] = $events;
92 $data['events-today'] = $events_today;
93 $data['birthdays'] = $birthdays;
94 $data['birthdays-today'] = $birthdays_today;
97 // Different login session than the page that is calling us.
98 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
100 $data = array('result' => array('invalid' => 1));
102 if ($format == 'json') {
103 if (isset($_GET['callback'])) {
105 header("Content-type: application/javascript");
106 echo $_GET['callback'] . '(' . json_encode($data) . ')';
108 header("Content-type: application/json");
109 echo json_encode($data);
112 header("Content-type: text/xml");
113 echo xml::from_array($data, $xml);
118 $notifs = ping_get_notifications(local_user());
120 $items_unseen = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
121 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
122 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
123 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
124 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
125 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
126 AND `item`.`contact-id` != %d
127 ORDER BY `item`.`created` DESC",
128 intval(local_user()), intval(local_user())
131 if (dbm::is_result($items_unseen)) {
132 $arr = array('items' => $items_unseen);
133 call_hooks('network_ping', $arr);
135 foreach ($items_unseen as $item) {
144 if ($network_count) {
145 if (intval(feature_enabled(local_user(), 'groups'))) {
146 // Find out how unseen network posts are spread across groups
147 $group_counts = groups_count_unseen();
148 if (dbm::is_result($group_counts)) {
149 foreach ($group_counts as $group_count) {
150 if ($group_count['count'] > 0) {
151 $groups_unseen[] = $group_count;
157 if (intval(feature_enabled(local_user(), 'forumlist_widget'))) {
158 $forum_counts = ForumManager::count_unseen_items();
159 if (dbm::is_result($forums_counts)) {
160 foreach ($forums_counts as $forum_count) {
161 if ($forum_count['count'] > 0) {
162 $forums_unseen[] = $forum_count;
169 $intros1 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
170 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
171 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
172 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
175 $intros2 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
176 `contact`.`name`, `contact`.`url`, `contact`.`photo`
177 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
178 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
182 $intro_count = count($intros1) + count($intros2);
183 $intros = $intros1 + $intros2;
185 $myurl = App::get_baseurl() . '/profile/' . $a->user['nickname'] ;
186 $mails = qu("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
187 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
188 intval(local_user()),
191 $mail_count = count($mails);
193 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
194 $regs = qu("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total`
195 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
196 WHERE `contact`.`self` = 1");
198 if (dbm::is_result($regs)) {
199 $register_count = $regs[0]['total'];
203 $cachekey = "ping_init:".local_user();
204 $ev = Cache::get($cachekey);
206 $ev = qu("SELECT type, start, adjust FROM `event`
207 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
208 ORDER BY `start` ASC ",
209 intval(local_user()),
210 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
211 dbesc(datetime_convert('UTC', 'UTC', 'now'))
213 if (dbm::is_result($ev)) {
214 Cache::set($cachekey, $ev, CACHE_HOUR);
218 if (dbm::is_result($ev)) {
219 $all_events = count($ev);
222 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
225 if ($x['type'] === 'birthday') {
232 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
233 $all_events_today ++;
243 $data['intro'] = $intro_count;
244 $data['mail'] = $mail_count;
245 $data['net'] = $network_count;
246 $data['home'] = $home_count;
247 $data['register'] = $register_count;
249 $data['all-events'] = $all_events;
250 $data['all-events-today'] = $all_events_today;
251 $data['events'] = $events;
252 $data['events-today'] = $events_today;
253 $data['birthdays'] = $birthdays;
254 $data['birthdays-today'] = $birthdays_today;
256 if (dbm::is_result($notifs)) {
257 foreach ($notifs as $notif) {
258 if ($notif['seen'] == 0) {
264 // merge all notification types in one array
265 if (dbm::is_result($intros)) {
266 foreach ($intros as $intro) {
268 'href' => App::get_baseurl() . '/notifications/intros/' . $intro['id'],
269 'name' => $intro['name'],
270 'url' => $intro['url'],
271 'photo' => $intro['photo'],
272 'date' => $intro['datetime'],
274 'message' => t('{0} wants to be your friend'),
280 if (dbm::is_result($mails)) {
281 foreach ($mails as $mail) {
283 'href' => App::get_baseurl() . '/message/' . $mail['id'],
284 'name' => $mail['from-name'],
285 'url' => $mail['from-url'],
286 'photo' => $mail['from-photo'],
287 'date' => $mail['created'],
289 'message' => t('{0} sent you a message'),
295 if (dbm::is_result($regs)) {
296 foreach ($regs as $reg) {
298 'href' => App::get_baseurl() . '/admin/users/',
299 'name' => $reg['name'],
300 'url' => $reg['url'],
301 'photo' => $reg['micro'],
302 'date' => $reg['created'],
304 'message' => t('{0} requested registration'),
310 // sort notifications by $[]['date']
311 $sort_function = function($a, $b) {
312 $adate = strtotime($a['date']);
313 $bdate = strtotime($b['date']);
315 // Unseen messages are kept at the top
316 // The value 31536000 means one year. This should be enough :-)
324 if ($adate == $bdate) {
327 return ($adate < $bdate) ? 1 : -1;
329 usort($notifs, $sort_function);
331 if (dbm::is_result($notifs)) {
332 // Are the nofications called from the regular process or via the friendica app?
333 $regularnotifications = (intval($_GET['uid']) && intval($_GET['_']));
335 foreach ($notifs as $notif) {
336 if ($a->is_friendica_app() || !$regularnotifications) {
337 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
340 $contact = get_contact_details_by_url($notif['url']);
341 if (isset($contact['micro'])) {
342 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
344 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
347 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
349 $notifications[] = array(
350 'id' => $notif['id'],
351 'href' => $notif['href'],
352 'name' => $notif['name'],
353 'url' => $notif['url'],
354 'photo' => $notif['photo'],
355 'date' => relative_date($notif['date']),
356 'message' => $notif['message'],
357 'seen' => $notif['seen'],
358 'timestamp' => strtotime($local_time)
365 $sysmsgs_info = array();
367 if (x($_SESSION, 'sysmsg')) {
368 $sysmsgs = $_SESSION['sysmsg'];
369 unset($_SESSION['sysmsg']);
372 if (x($_SESSION, 'sysmsg_info')) {
373 $sysmsgs_info = $_SESSION['sysmsg_info'];
374 unset($_SESSION['sysmsg_info']);
377 if ($format == 'json') {
378 $data['groups'] = $groups_unseen;
379 $data['forums'] = $forums_unseen;
380 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
381 $data['notifications'] = $notifications;
382 $data['sysmsgs'] = array(
383 'notice' => $sysmsgs,
384 'info' => $sysmsgs_info
387 $json_payload = json_encode(array("result" => $data));
389 if (isset($_GET['callback'])) {
391 header("Content-type: application/javascript");
392 echo $_GET['callback'] . '(' . $json_payload . ')';
394 header("Content-type: application/json");
398 // Legacy slower XML format output
399 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
401 header("Content-type: text/xml");
402 echo xml::from_array(array("result" => $data), $xml);
409 * @brief Retrieves the notifications array for the given user ID
411 * @param int $uid User id
412 * @return array Associative array of notifications
414 function ping_get_notifications($uid)
426 $r = qu("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
427 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
428 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
429 AND NOT (`notify`.`type` IN (%d, %d))
430 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
432 intval(NOTIFY_INTRO),
448 foreach ($r AS $notification) {
449 if (is_null($notification["visible"])) {
450 $notification["visible"] = true;
453 if (is_null($notification["spam"])) {
454 $notification["spam"] = 0;
457 if (is_null($notification["deleted"])) {
458 $notification["deleted"] = 0;
461 if ($notification["msg_cache"]) {
462 $notification["name"] = $notification["name_cache"];
463 $notification["message"] = $notification["msg_cache"];
465 $notification["name"] = strip_tags(bbcode($notification["name"]));
466 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
468 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
469 dbesc($notification["name"]),
470 dbesc($notification["message"]),
471 intval($notification["id"])
475 $notification["href"] = App::get_baseurl() . "/notify/view/" . $notification["id"];
477 if ($notification["visible"] && !$notification["spam"] &&
478 !$notification["deleted"] && !is_array($result[$notification["parent"]])) {
479 $result[$notification["parent"]] = $notification;
482 } while ((count($result) < 50) && !$quit);
488 * @brief Backward-compatible XML formatting for ping.php output
491 * @param array $data The initial ping data array
492 * @param int $sysnotify_count Number of unseen system notifications
493 * @param array $notifs Complete list of notification
494 * @param array $sysmsgs List of system notice messages
495 * @param array $sysmsgs_info List of system info messages
496 * @param int $groups_unseen Number of unseen group items
497 * @param int $forums_unseen Number of unseen forum items
498 * @return array XML-transform ready data array
500 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
502 $notifications = array();
503 foreach($notifs as $key => $notif) {
504 $notifications[$key . ':note'] = $notif['message'];
506 $notifications[$key . ':@attributes'] = array(
507 'id' => $notif['id'],
508 'href' => $notif['href'],
509 'name' => $notif['name'],
510 'url' => $notif['url'],
511 'photo' => $notif['photo'],
512 'date' => $notif['date'],
513 'seen' => $notif['seen'],
514 'timestamp' => $notif['timestamp']
519 foreach ($sysmsgs as $key => $m){
520 $sysmsg[$key . ':notice'] = $m;
522 foreach ($sysmsgs_info as $key => $m){
523 $sysmsg[$key . ':info'] = $m;
526 $data['notif'] = $notifications;
527 $data['@attributes'] = array('count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']);
528 $data['sysmsgs'] = $sysmsg;
530 if ($data['register'] == 0) {
531 unset($data['register']);
535 if (count($groups_unseen)) {
536 foreach ($groups_unseen as $key => $item) {
537 $groups[$key . ':group'] = $item['count'];
538 $groups[$key . ':@attributes'] = array('id' => $item['id']);
540 $data['groups'] = $groups;
544 if (count($forums_unseen)) {
545 foreach ($forums_unseen as $key => $item) {
546 $forums[$count . ':forum'] = $item['count'];
547 $forums[$count . ':@attributes'] = array('id' => $item['id']);
549 $data['forums'] = $forums;