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');
14 * @brief Outputs the counts and the lists of various notifications
16 * The output format can be controlled via the GET parameter 'format'. It can be
17 * - xml (deprecated legacy default)
18 * - json (outputs JSONP with the 'callback' GET parameter)
20 * Expected JSON structure:
29 * "all-events-today": 0,
33 * "birthdays-today": 0,
37 * "notifications": [ ],
45 * @param App $a The Friendica App instance
47 function ping_init(App $a)
51 if (isset($_GET['format']) && $_GET['format'] == 'json') {
63 $notifications = array();
71 $groups_unseen = array();
72 $forums_unseen = array();
75 $all_events_today = 0;
82 $data['intro'] = $intro_count;
83 $data['mail'] = $mail_count;
84 $data['net'] = $network_count;
85 $data['home'] = $home_count;
86 $data['register'] = $register_count;
88 $data['all-events'] = $all_events;
89 $data['all-events-today'] = $all_events_today;
90 $data['events'] = $events;
91 $data['events-today'] = $events_today;
92 $data['birthdays'] = $birthdays;
93 $data['birthdays-today'] = $birthdays_today;
96 // Different login session than the page that is calling us.
97 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
99 $data = array('result' => array('invalid' => 1));
101 if ($format == 'json') {
102 if (isset($_GET['callback'])) {
104 header("Content-type: application/javascript");
105 echo $_GET['callback'] . '(' . json_encode($data) . ')';
107 header("Content-type: application/json");
108 echo json_encode($data);
111 header("Content-type: text/xml");
112 echo xml::from_array($data, $xml);
117 $notifs = ping_get_notifications(local_user());
119 $items_unseen = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
120 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
121 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
122 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
123 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
124 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
125 AND `item`.`contact-id` != %d
126 ORDER BY `item`.`created` DESC",
127 intval(local_user()), intval(local_user())
130 if (dbm::is_result($items_unseen)) {
131 $arr = array('items' => $items_unseen);
132 call_hooks('network_ping', $arr);
134 foreach ($items_unseen as $item) {
143 if ($network_count) {
144 if (intval(feature_enabled(local_user(), 'groups'))) {
145 // Find out how unseen network posts are spread across groups
146 $group_counts = groups_count_unseen();
147 if (dbm::is_result($group_counts)) {
148 foreach ($group_counts as $group_count) {
149 if ($group_count['count'] > 0) {
150 $groups_unseen[] = $group_count;
156 if (intval(feature_enabled(local_user(), 'forumlist_widget'))) {
157 $forum_counts = ForumManager::count_unseen_items();
158 if (dbm::is_result($forums_counts)) {
159 foreach ($forums_counts as $forum_count) {
160 if ($forum_count['count'] > 0) {
161 $forums_unseen[] = $forum_count;
168 $intros1 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
169 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
170 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
171 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
174 $intros2 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
175 `contact`.`name`, `contact`.`url`, `contact`.`photo`
176 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
177 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
181 $intro_count = count($intros1) + count($intros2);
182 $intros = $intros1 + $intros2;
184 $myurl = App::get_baseurl() . '/profile/' . $a->user['nickname'] ;
185 $mails = qu("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
186 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
187 intval(local_user()),
190 $mail_count = count($mails);
192 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
193 $regs = qu("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total`
194 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
195 WHERE `contact`.`self` = 1");
197 if (dbm::is_result($regs)) {
198 $register_count = $regs[0]['total'];
202 $cachekey = "ping_init:".local_user();
203 $ev = Cache::get($cachekey);
205 $ev = qu("SELECT type, start, adjust FROM `event`
206 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
207 ORDER BY `start` ASC ",
208 intval(local_user()),
209 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
210 dbesc(datetime_convert('UTC', 'UTC', 'now'))
212 if (dbm::is_result($ev)) {
213 Cache::set($cachekey, $ev, CACHE_HOUR);
217 if (dbm::is_result($ev)) {
218 $all_events = count($ev);
221 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
224 if ($x['type'] === 'birthday') {
231 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
232 $all_events_today ++;
242 $data['intro'] = $intro_count;
243 $data['mail'] = $mail_count;
244 $data['net'] = $network_count;
245 $data['home'] = $home_count;
246 $data['register'] = $register_count;
248 $data['all-events'] = $all_events;
249 $data['all-events-today'] = $all_events_today;
250 $data['events'] = $events;
251 $data['events-today'] = $events_today;
252 $data['birthdays'] = $birthdays;
253 $data['birthdays-today'] = $birthdays_today;
255 if (dbm::is_result($notifs)) {
256 foreach ($notifs as $notif) {
257 if ($notif['seen'] == 0) {
263 // merge all notification types in one array
264 if (dbm::is_result($intros)) {
265 foreach ($intros as $intro) {
267 'href' => App::get_baseurl() . '/notifications/intros/' . $intro['id'],
268 'name' => $intro['name'],
269 'url' => $intro['url'],
270 'photo' => $intro['photo'],
271 'date' => $intro['datetime'],
273 'message' => t('{0} wants to be your friend'),
279 if (dbm::is_result($mails)) {
280 foreach ($mails as $mail) {
282 'href' => App::get_baseurl() . '/message/' . $mail['id'],
283 'name' => $mail['from-name'],
284 'url' => $mail['from-url'],
285 'photo' => $mail['from-photo'],
286 'date' => $mail['created'],
288 'message' => t('{0} sent you a message'),
294 if (dbm::is_result($regs)) {
295 foreach ($regs as $reg) {
297 'href' => App::get_baseurl() . '/admin/users/',
298 'name' => $reg['name'],
299 'url' => $reg['url'],
300 'photo' => $reg['micro'],
301 'date' => $reg['created'],
303 'message' => t('{0} requested registration'),
309 // sort notifications by $[]['date']
310 $sort_function = function($a, $b) {
311 $adate = strtotime($a['date']);
312 $bdate = strtotime($b['date']);
314 // Unseen messages are kept at the top
315 // The value 31536000 means one year. This should be enough :-)
323 if ($adate == $bdate) {
326 return ($adate < $bdate) ? 1 : -1;
328 usort($notifs, $sort_function);
330 if (dbm::is_result($notifs)) {
331 // Are the nofications called from the regular process or via the friendica app?
332 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
334 foreach ($notifs as $notif) {
335 if ($a->is_friendica_app() OR !$regularnotifications) {
336 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
339 $contact = get_contact_details_by_url($notif['url']);
340 if (isset($contact['micro'])) {
341 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
343 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
346 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
348 $notifications[] = array(
349 'id' => $notif['id'],
350 'href' => $notif['href'],
351 'name' => $notif['name'],
352 'url' => $notif['url'],
353 'photo' => $notif['photo'],
354 'date' => relative_date($notif['date']),
355 'message' => $notif['message'],
356 'seen' => $notif['seen'],
357 'timestamp' => strtotime($local_time)
364 $sysmsgs_info = array();
366 if (x($_SESSION, 'sysmsg')) {
367 $sysmsgs = $_SESSION['sysmsg'];
368 unset($_SESSION['sysmsg']);
371 if (x($_SESSION, 'sysmsg_info')) {
372 $sysmsgs_info = $_SESSION['sysmsg_info'];
373 unset($_SESSION['sysmsg_info']);
376 if ($format == 'json') {
377 $data['groups'] = $groups_unseen;
378 $data['forums'] = $forums_unseen;
379 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
380 $data['notifications'] = $notifications;
381 $data['sysmsgs'] = array(
382 'notice' => $sysmsgs,
383 'info' => $sysmsgs_info
386 $json_payload = json_encode(array("result" => $data));
388 if (isset($_GET['callback'])) {
390 header("Content-type: application/javascript");
391 echo $_GET['callback'] . '(' . $json_payload . ')';
393 header("Content-type: application/json");
397 // Legacy slower XML format output
398 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
400 header("Content-type: text/xml");
401 echo xml::from_array(array("result" => $data), $xml);
408 * @brief Retrieves the notifications array for the given user ID
410 * @param int $uid User id
411 * @return array Associative array of notifications
413 function ping_get_notifications($uid)
425 $r = qu("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
426 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
427 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
428 AND NOT (`notify`.`type` IN (%d, %d))
429 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
431 intval(NOTIFY_INTRO),
436 if (!$r AND !$seen) {
447 foreach ($r AS $notification) {
448 if (is_null($notification["visible"])) {
449 $notification["visible"] = true;
452 if (is_null($notification["spam"])) {
453 $notification["spam"] = 0;
456 if (is_null($notification["deleted"])) {
457 $notification["deleted"] = 0;
460 if ($notification["msg_cache"]) {
461 $notification["name"] = $notification["name_cache"];
462 $notification["message"] = $notification["msg_cache"];
464 $notification["name"] = strip_tags(bbcode($notification["name"]));
465 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
467 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
468 dbesc($notification["name"]),
469 dbesc($notification["message"]),
470 intval($notification["id"])
474 $notification["href"] = App::get_baseurl() . "/notify/view/" . $notification["id"];
476 if ($notification["visible"] AND !$notification["spam"] AND
477 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
478 $result[$notification["parent"]] = $notification;
481 } while ((count($result) < 50) AND !$quit);
487 * @brief Backward-compatible XML formatting for ping.php output
490 * @param array $data The initial ping data array
491 * @param int $sysnotify_count Number of unseen system notifications
492 * @param array $notifs Complete list of notification
493 * @param array $sysmsgs List of system notice messages
494 * @param array $sysmsgs_info List of system info messages
495 * @param int $groups_unseen Number of unseen group items
496 * @param int $forums_unseen Number of unseen forum items
497 * @return array XML-transform ready data array
499 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
501 $notifications = array();
502 foreach($notifs as $key => $notif) {
503 $notifications[$key . ':note'] = $notif['message'];
505 $notifications[$key . ':@attributes'] = array(
506 'id' => $notif['id'],
507 'href' => $notif['href'],
508 'name' => $notif['name'],
509 'url' => $notif['url'],
510 'photo' => $notif['photo'],
511 'date' => $notif['date'],
512 'seen' => $notif['seen'],
513 'timestamp' => $notif['timestamp']
518 foreach ($sysmsgs as $key => $m){
519 $sysmsg[$key . ':notice'] = $m;
521 foreach ($sysmsgs_info as $key => $m){
522 $sysmsg[$key . ':info'] = $m;
525 $data['notif'] = $notifications;
526 $data['@attributes'] = array('count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']);
527 $data['sysmsgs'] = $sysmsg;
529 if ($data['register'] == 0) {
530 unset($data['register']);
534 if (count($groups_unseen)) {
535 foreach ($groups_unseen as $key => $item) {
536 $groups[$key . ':group'] = $item['count'];
537 $groups[$key . ':@attributes'] = array('id' => $item['id']);
539 $data['groups'] = $groups;
543 if (count($forums_unseen)) {
544 foreach ($forums_unseen as $key => $item) {
545 $forums[$count . ':forum'] = $item['count'];
546 $forums[$count . ':@attributes'] = array('id' => $item['id']);
548 $data['forums'] = $forums;