3 * @file include/ping.php
6 use Friendica\Content\Feature;
7 use Friendica\Content\ForumManager;
8 use Friendica\Core\Addon;
9 use Friendica\Core\Cache;
10 use Friendica\Core\L10n;
11 use Friendica\Core\System;
12 use Friendica\Core\PConfig;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Group;
16 use Friendica\Util\XML;
18 require_once 'include/datetime.php';
19 require_once 'include/bbcode.php';
20 require_once 'mod/proxy.php';
21 require_once 'include/enotify.php';
24 * @brief Outputs the counts and the lists of various notifications
26 * The output format can be controlled via the GET parameter 'format'. It can be
27 * - xml (deprecated legacy default)
28 * - json (outputs JSONP with the 'callback' GET parameter)
30 * Expected JSON structure:
39 * "all-events-today": 0,
43 * "birthdays-today": 0,
47 * "notifications": [ ],
55 * @param App $a The Friendica App instance
57 function ping_init(App $a)
61 if (isset($_GET['format']) && $_GET['format'] == 'json') {
85 $all_events_today = 0;
92 $data['intro'] = $intro_count;
93 $data['mail'] = $mail_count;
94 $data['net'] = $network_count;
95 $data['home'] = $home_count;
96 $data['register'] = $register_count;
98 $data['all-events'] = $all_events;
99 $data['all-events-today'] = $all_events_today;
100 $data['events'] = $events;
101 $data['events-today'] = $events_today;
102 $data['birthdays'] = $birthdays;
103 $data['birthdays-today'] = $birthdays_today;
106 // Different login session than the page that is calling us.
107 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
108 $data = ['result' => ['invalid' => 1]];
110 if ($format == 'json') {
111 if (isset($_GET['callback'])) {
113 header("Content-type: application/javascript");
114 echo $_GET['callback'] . '(' . json_encode($data) . ')';
116 header("Content-type: application/json");
117 echo json_encode($data);
120 header("Content-type: text/xml");
121 echo XML::fromArray($data, $xml);
126 $notifs = ping_get_notifications(local_user());
129 "SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
130 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
131 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
132 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
133 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
134 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
135 AND `item`.`contact-id` != %d
136 ORDER BY `item`.`created` DESC",
137 intval(local_user()),
141 if (DBM::is_result($items_unseen)) {
142 $arr = ['items' => $items_unseen];
143 Addon::callHooks('network_ping', $arr);
145 foreach ($items_unseen as $item) {
154 if ($network_count) {
155 if (intval(Feature::isEnabled(local_user(), 'groups'))) {
156 // Find out how unseen network posts are spread across groups
157 $group_counts = Group::countUnseen();
158 if (DBM::is_result($group_counts)) {
159 foreach ($group_counts as $group_count) {
160 if ($group_count['count'] > 0) {
161 $groups_unseen[] = $group_count;
167 if (intval(Feature::isEnabled(local_user(), 'forumlist_widget'))) {
168 $forum_counts = ForumManager::countUnseenItems();
169 if (DBM::is_result($forum_counts)) {
170 foreach ($forum_counts as $forum_count) {
171 if ($forum_count['count'] > 0) {
172 $forums_unseen[] = $forum_count;
180 "SELECT `intro`.`id`, `intro`.`datetime`,
181 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
182 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
183 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
187 "SELECT `intro`.`id`, `intro`.`datetime`,
188 `contact`.`name`, `contact`.`url`, `contact`.`photo`
189 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
190 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
194 $intro_count = count($intros1) + count($intros2);
195 $intros = $intros1 + $intros2;
197 $myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ;
199 "SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
200 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
201 intval(local_user()),
204 $mail_count = count($mails);
206 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()) {
208 "SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`
209 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
210 WHERE `contact`.`self` = 1"
213 if (DBM::is_result($regs)) {
214 $register_count = count($regs);
218 $cachekey = "ping_init:".local_user();
219 $ev = Cache::get($cachekey);
222 "SELECT type, start, adjust FROM `event`
223 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
224 ORDER BY `start` ASC ",
225 intval(local_user()),
226 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
227 dbesc(datetime_convert('UTC', 'UTC', 'now'))
229 if (DBM::is_result($ev)) {
230 Cache::set($cachekey, $ev, CACHE_HOUR);
234 if (DBM::is_result($ev)) {
235 $all_events = count($ev);
238 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
239 foreach ($ev as $x) {
241 if ($x['type'] === 'birthday') {
247 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
248 $all_events_today ++;
259 $data['intro'] = $intro_count;
260 $data['mail'] = $mail_count;
261 $data['net'] = $network_count;
262 $data['home'] = $home_count;
263 $data['register'] = $register_count;
265 $data['all-events'] = $all_events;
266 $data['all-events-today'] = $all_events_today;
267 $data['events'] = $events;
268 $data['events-today'] = $events_today;
269 $data['birthdays'] = $birthdays;
270 $data['birthdays-today'] = $birthdays_today;
272 if (DBM::is_result($notifs)) {
273 foreach ($notifs as $notif) {
274 if ($notif['seen'] == 0) {
280 // merge all notification types in one array
281 if (DBM::is_result($intros)) {
282 foreach ($intros as $intro) {
284 'href' => System::baseUrl() . '/notifications/intros/' . $intro['id'],
285 'name' => $intro['name'],
286 'url' => $intro['url'],
287 'photo' => $intro['photo'],
288 'date' => $intro['datetime'],
290 'message' => L10n::t('{0} wants to be your friend'),
296 if (DBM::is_result($mails)) {
297 foreach ($mails as $mail) {
299 'href' => System::baseUrl() . '/message/' . $mail['id'],
300 'name' => $mail['from-name'],
301 'url' => $mail['from-url'],
302 'photo' => $mail['from-photo'],
303 'date' => $mail['created'],
305 'message' => L10n::t('{0} sent you a message'),
311 if (DBM::is_result($regs)) {
312 foreach ($regs as $reg) {
314 'href' => System::baseUrl() . '/admin/users/',
315 'name' => $reg['name'],
316 'url' => $reg['url'],
317 'photo' => $reg['micro'],
318 'date' => $reg['created'],
320 'message' => L10n::t('{0} requested registration'),
326 // sort notifications by $[]['date']
327 $sort_function = function ($a, $b) {
328 $adate = strtotime($a['date']);
329 $bdate = strtotime($b['date']);
331 // Unseen messages are kept at the top
332 // The value 31536000 means one year. This should be enough :-)
340 if ($adate == $bdate) {
343 return ($adate < $bdate) ? 1 : -1;
345 usort($notifs, $sort_function);
347 if (DBM::is_result($notifs)) {
348 // Are the nofications called from the regular process or via the friendica app?
349 $regularnotifications = (intval($_GET['uid']) && intval($_GET['_']));
351 foreach ($notifs as $notif) {
352 if ($a->is_friendica_app() || !$regularnotifications) {
353 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
356 $contact = Contact::getDetailsByURL($notif['url']);
357 if (isset($contact['micro'])) {
358 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
360 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
363 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
366 'id' => $notif['id'],
367 'href' => $notif['href'],
368 'name' => $notif['name'],
369 'url' => $notif['url'],
370 'photo' => $notif['photo'],
371 'date' => relative_date($notif['date']),
372 'message' => $notif['message'],
373 'seen' => $notif['seen'],
374 'timestamp' => strtotime($local_time)
383 if (x($_SESSION, 'sysmsg')) {
384 $sysmsgs = $_SESSION['sysmsg'];
385 unset($_SESSION['sysmsg']);
388 if (x($_SESSION, 'sysmsg_info')) {
389 $sysmsgs_info = $_SESSION['sysmsg_info'];
390 unset($_SESSION['sysmsg_info']);
393 if ($format == 'json') {
394 $data['groups'] = $groups_unseen;
395 $data['forums'] = $forums_unseen;
396 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
397 $data['notifications'] = $notifications;
399 'notice' => $sysmsgs,
400 'info' => $sysmsgs_info
403 $json_payload = json_encode(["result" => $data]);
405 if (isset($_GET['callback'])) {
407 header("Content-type: application/javascript");
408 echo $_GET['callback'] . '(' . $json_payload . ')';
410 header("Content-type: application/json");
414 // Legacy slower XML format output
415 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
417 header("Content-type: text/xml");
418 echo XML::fromArray(["result" => $data], $xml);
425 * @brief Retrieves the notifications array for the given user ID
427 * @param int $uid User id
428 * @return array Associative array of notifications
430 function ping_get_notifications($uid)
443 "SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
444 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
445 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
446 AND NOT (`notify`.`type` IN (%d, %d))
447 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
449 intval(NOTIFY_INTRO),
465 foreach ($r as $notification) {
466 if (is_null($notification["visible"])) {
467 $notification["visible"] = true;
470 if (is_null($notification["spam"])) {
471 $notification["spam"] = 0;
474 if (is_null($notification["deleted"])) {
475 $notification["deleted"] = 0;
478 if ($notification["msg_cache"]) {
479 $notification["name"] = $notification["name_cache"];
480 $notification["message"] = $notification["msg_cache"];
482 $notification["name"] = strip_tags(bbcode($notification["name"]));
483 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
486 "UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
487 dbesc($notification["name"]),
488 dbesc($notification["message"]),
489 intval($notification["id"])
493 $notification["href"] = System::baseUrl() . "/notify/view/" . $notification["id"];
495 if ($notification["visible"]
496 && !$notification["spam"]
497 && !$notification["deleted"]
498 && !(x($result, $notification["parent"]) && is_array($result[$notification["parent"]]))
500 // Should we condense the notifications or show them all?
501 if (PConfig::get(local_user(), 'system', 'detailed_notif')) {
502 $result[$notification["id"]] = $notification;
504 $result[$notification["parent"]] = $notification;
508 } while ((count($result) < 50) && !$quit);
514 * @brief Backward-compatible XML formatting for ping.php output
517 * @param array $data The initial ping data array
518 * @param int $sysnotify Number of unseen system notifications
519 * @param array $notifs Complete list of notification
520 * @param array $sysmsgs List of system notice messages
521 * @param array $sysmsgs_info List of system info messages
522 * @param int $groups_unseen Number of unseen group items
523 * @param int $forums_unseen Number of unseen forum items
524 * @return array XML-transform ready data array
526 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
529 foreach ($notifs as $key => $notif) {
530 $notifications[$key . ':note'] = $notif['message'];
532 $notifications[$key . ':@attributes'] = [
533 'id' => $notif['id'],
534 'href' => $notif['href'],
535 'name' => $notif['name'],
536 'url' => $notif['url'],
537 'photo' => $notif['photo'],
538 'date' => $notif['date'],
539 'seen' => $notif['seen'],
540 'timestamp' => $notif['timestamp']
545 foreach ($sysmsgs as $key => $m) {
546 $sysmsg[$key . ':notice'] = $m;
548 foreach ($sysmsgs_info as $key => $m) {
549 $sysmsg[$key . ':info'] = $m;
552 $data['notif'] = $notifications;
553 $data['@attributes'] = ['count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']];
554 $data['sysmsgs'] = $sysmsg;
556 if ($data['register'] == 0) {
557 unset($data['register']);
561 if (count($groups_unseen)) {
562 foreach ($groups_unseen as $key => $item) {
563 $groups[$key . ':group'] = $item['count'];
564 $groups[$key . ':@attributes'] = ['id' => $item['id']];
566 $data['groups'] = $groups;
570 if (count($forums_unseen)) {
571 foreach ($forums_unseen as $key => $item) {
572 $forums[$count . ':forum'] = $item['count'];
573 $forums[$count . ':@attributes'] = ['id' => $item['id']];
575 $data['forums'] = $forums;