3 * @file include/ping.php
6 use Friendica\Content\Feature;
7 use Friendica\Content\ForumManager;
8 use Friendica\Core\Cache;
9 use Friendica\Core\System;
10 use Friendica\Core\PConfig;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Group;
14 use Friendica\Util\XML;
16 require_once 'include/datetime.php';
17 require_once 'include/bbcode.php';
18 require_once 'mod/proxy.php';
19 require_once 'include/enotify.php';
22 * @brief Outputs the counts and the lists of various notifications
24 * The output format can be controlled via the GET parameter 'format'. It can be
25 * - xml (deprecated legacy default)
26 * - json (outputs JSONP with the 'callback' GET parameter)
28 * Expected JSON structure:
37 * "all-events-today": 0,
41 * "birthdays-today": 0,
45 * "notifications": [ ],
53 * @param App $a The Friendica App instance
55 function ping_init(App $a)
59 if (isset($_GET['format']) && $_GET['format'] == 'json') {
71 $notifications = array();
79 $groups_unseen = array();
80 $forums_unseen = array();
83 $all_events_today = 0;
90 $data['intro'] = $intro_count;
91 $data['mail'] = $mail_count;
92 $data['net'] = $network_count;
93 $data['home'] = $home_count;
94 $data['register'] = $register_count;
96 $data['all-events'] = $all_events;
97 $data['all-events-today'] = $all_events_today;
98 $data['events'] = $events;
99 $data['events-today'] = $events_today;
100 $data['birthdays'] = $birthdays;
101 $data['birthdays-today'] = $birthdays_today;
104 // Different login session than the page that is calling us.
105 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
106 $data = array('result' => array('invalid' => 1));
108 if ($format == 'json') {
109 if (isset($_GET['callback'])) {
111 header("Content-type: application/javascript");
112 echo $_GET['callback'] . '(' . json_encode($data) . ')';
114 header("Content-type: application/json");
115 echo json_encode($data);
118 header("Content-type: text/xml");
119 echo XML::fromArray($data, $xml);
124 $notifs = ping_get_notifications(local_user());
127 "SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
128 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
129 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
130 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
131 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
132 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
133 AND `item`.`contact-id` != %d
134 ORDER BY `item`.`created` DESC",
135 intval(local_user()),
139 if (DBM::is_result($items_unseen)) {
140 $arr = array('items' => $items_unseen);
141 call_hooks('network_ping', $arr);
143 foreach ($items_unseen as $item) {
152 if ($network_count) {
153 if (intval(Feature::isEnabled(local_user(), 'groups'))) {
154 // Find out how unseen network posts are spread across groups
155 $group_counts = Group::countUnseen();
156 if (DBM::is_result($group_counts)) {
157 foreach ($group_counts as $group_count) {
158 if ($group_count['count'] > 0) {
159 $groups_unseen[] = $group_count;
165 if (intval(Feature::isEnabled(local_user(), 'forumlist_widget'))) {
166 $forum_counts = ForumManager::countUnseenItems();
167 if (DBM::is_result($forum_counts)) {
168 foreach ($forum_counts as $forum_count) {
169 if ($forum_count['count'] > 0) {
170 $forums_unseen[] = $forum_count;
178 "SELECT `intro`.`id`, `intro`.`datetime`,
179 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
180 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
181 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
185 "SELECT `intro`.`id`, `intro`.`datetime`,
186 `contact`.`name`, `contact`.`url`, `contact`.`photo`
187 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
188 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
192 $intro_count = count($intros1) + count($intros2);
193 $intros = $intros1 + $intros2;
195 $myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ;
197 "SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
198 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
199 intval(local_user()),
202 $mail_count = count($mails);
204 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()) {
206 "SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`
207 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
208 WHERE `contact`.`self` = 1"
211 if (DBM::is_result($regs)) {
212 $register_count = count($regs);
216 $cachekey = "ping_init:".local_user();
217 $ev = Cache::get($cachekey);
220 "SELECT type, start, adjust FROM `event`
221 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
222 ORDER BY `start` ASC ",
223 intval(local_user()),
224 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
225 dbesc(datetime_convert('UTC', 'UTC', 'now'))
227 if (DBM::is_result($ev)) {
228 Cache::set($cachekey, $ev, CACHE_HOUR);
232 if (DBM::is_result($ev)) {
233 $all_events = count($ev);
236 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
237 foreach ($ev as $x) {
239 if ($x['type'] === 'birthday') {
245 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
246 $all_events_today ++;
257 $data['intro'] = $intro_count;
258 $data['mail'] = $mail_count;
259 $data['net'] = $network_count;
260 $data['home'] = $home_count;
261 $data['register'] = $register_count;
263 $data['all-events'] = $all_events;
264 $data['all-events-today'] = $all_events_today;
265 $data['events'] = $events;
266 $data['events-today'] = $events_today;
267 $data['birthdays'] = $birthdays;
268 $data['birthdays-today'] = $birthdays_today;
270 if (DBM::is_result($notifs)) {
271 foreach ($notifs as $notif) {
272 if ($notif['seen'] == 0) {
278 // merge all notification types in one array
279 if (DBM::is_result($intros)) {
280 foreach ($intros as $intro) {
282 'href' => System::baseUrl() . '/notifications/intros/' . $intro['id'],
283 'name' => $intro['name'],
284 'url' => $intro['url'],
285 'photo' => $intro['photo'],
286 'date' => $intro['datetime'],
288 'message' => t('{0} wants to be your friend'),
294 if (DBM::is_result($mails)) {
295 foreach ($mails as $mail) {
297 'href' => System::baseUrl() . '/message/' . $mail['id'],
298 'name' => $mail['from-name'],
299 'url' => $mail['from-url'],
300 'photo' => $mail['from-photo'],
301 'date' => $mail['created'],
303 'message' => t('{0} sent you a message'),
309 if (DBM::is_result($regs)) {
310 foreach ($regs as $reg) {
312 'href' => System::baseUrl() . '/admin/users/',
313 'name' => $reg['name'],
314 'url' => $reg['url'],
315 'photo' => $reg['micro'],
316 'date' => $reg['created'],
318 'message' => t('{0} requested registration'),
324 // sort notifications by $[]['date']
325 $sort_function = function ($a, $b) {
326 $adate = strtotime($a['date']);
327 $bdate = strtotime($b['date']);
329 // Unseen messages are kept at the top
330 // The value 31536000 means one year. This should be enough :-)
338 if ($adate == $bdate) {
341 return ($adate < $bdate) ? 1 : -1;
343 usort($notifs, $sort_function);
345 if (DBM::is_result($notifs)) {
346 // Are the nofications called from the regular process or via the friendica app?
347 $regularnotifications = (intval($_GET['uid']) && intval($_GET['_']));
349 foreach ($notifs as $notif) {
350 if ($a->is_friendica_app() || !$regularnotifications) {
351 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
354 $contact = Contact::getDetailsByURL($notif['url']);
355 if (isset($contact['micro'])) {
356 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
358 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
361 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
363 $notifications[] = array(
364 'id' => $notif['id'],
365 'href' => $notif['href'],
366 'name' => $notif['name'],
367 'url' => $notif['url'],
368 'photo' => $notif['photo'],
369 'date' => relative_date($notif['date']),
370 'message' => $notif['message'],
371 'seen' => $notif['seen'],
372 'timestamp' => strtotime($local_time)
379 $sysmsgs_info = array();
381 if (x($_SESSION, 'sysmsg')) {
382 $sysmsgs = $_SESSION['sysmsg'];
383 unset($_SESSION['sysmsg']);
386 if (x($_SESSION, 'sysmsg_info')) {
387 $sysmsgs_info = $_SESSION['sysmsg_info'];
388 unset($_SESSION['sysmsg_info']);
391 if ($format == 'json') {
392 $data['groups'] = $groups_unseen;
393 $data['forums'] = $forums_unseen;
394 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
395 $data['notifications'] = $notifications;
396 $data['sysmsgs'] = array(
397 'notice' => $sysmsgs,
398 'info' => $sysmsgs_info
401 $json_payload = json_encode(array("result" => $data));
403 if (isset($_GET['callback'])) {
405 header("Content-type: application/javascript");
406 echo $_GET['callback'] . '(' . $json_payload . ')';
408 header("Content-type: application/json");
412 // Legacy slower XML format output
413 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
415 header("Content-type: text/xml");
416 echo XML::fromArray(array("result" => $data), $xml);
423 * @brief Retrieves the notifications array for the given user ID
425 * @param int $uid User id
426 * @return array Associative array of notifications
428 function ping_get_notifications($uid)
441 "SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
442 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
443 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
444 AND NOT (`notify`.`type` IN (%d, %d))
445 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
447 intval(NOTIFY_INTRO),
463 foreach ($r as $notification) {
464 if (is_null($notification["visible"])) {
465 $notification["visible"] = true;
468 if (is_null($notification["spam"])) {
469 $notification["spam"] = 0;
472 if (is_null($notification["deleted"])) {
473 $notification["deleted"] = 0;
476 if ($notification["msg_cache"]) {
477 $notification["name"] = $notification["name_cache"];
478 $notification["message"] = $notification["msg_cache"];
480 $notification["name"] = strip_tags(bbcode($notification["name"]));
481 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
484 "UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
485 dbesc($notification["name"]),
486 dbesc($notification["message"]),
487 intval($notification["id"])
491 $notification["href"] = System::baseUrl() . "/notify/view/" . $notification["id"];
493 if ($notification["visible"]
494 && !$notification["spam"]
495 && !$notification["deleted"]
496 && !(x($result, $notification["parent"]) && is_array($result[$notification["parent"]]))
498 // Should we condense the notifications or show them all?
499 if (PConfig::get(local_user(), 'system', 'detailed_notif')) {
500 $result[$notification["id"]] = $notification;
502 $result[$notification["parent"]] = $notification;
506 } while ((count($result) < 50) && !$quit);
512 * @brief Backward-compatible XML formatting for ping.php output
515 * @param array $data The initial ping data array
516 * @param int $sysnotify Number of unseen system notifications
517 * @param array $notifs Complete list of notification
518 * @param array $sysmsgs List of system notice messages
519 * @param array $sysmsgs_info List of system info messages
520 * @param int $groups_unseen Number of unseen group items
521 * @param int $forums_unseen Number of unseen forum items
522 * @return array XML-transform ready data array
524 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
526 $notifications = array();
527 foreach ($notifs as $key => $notif) {
528 $notifications[$key . ':note'] = $notif['message'];
530 $notifications[$key . ':@attributes'] = array(
531 'id' => $notif['id'],
532 'href' => $notif['href'],
533 'name' => $notif['name'],
534 'url' => $notif['url'],
535 'photo' => $notif['photo'],
536 'date' => $notif['date'],
537 'seen' => $notif['seen'],
538 'timestamp' => $notif['timestamp']
543 foreach ($sysmsgs as $key => $m) {
544 $sysmsg[$key . ':notice'] = $m;
546 foreach ($sysmsgs_info as $key => $m) {
547 $sysmsg[$key . ':info'] = $m;
550 $data['notif'] = $notifications;
551 $data['@attributes'] = array('count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']);
552 $data['sysmsgs'] = $sysmsg;
554 if ($data['register'] == 0) {
555 unset($data['register']);
559 if (count($groups_unseen)) {
560 foreach ($groups_unseen as $key => $item) {
561 $groups[$key . ':group'] = $item['count'];
562 $groups[$key . ':@attributes'] = array('id' => $item['id']);
564 $data['groups'] = $groups;
568 if (count($forums_unseen)) {
569 foreach ($forums_unseen as $key => $item) {
570 $forums[$count . ':forum'] = $item['count'];
571 $forums[$count . ':@attributes'] = array('id' => $item['id']);
573 $data['forums'] = $forums;