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\System;
11 use Friendica\Core\PConfig;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Group;
15 use Friendica\Util\XML;
17 require_once 'include/datetime.php';
18 require_once 'include/bbcode.php';
19 require_once 'mod/proxy.php';
20 require_once 'include/enotify.php';
23 * @brief Outputs the counts and the lists of various notifications
25 * The output format can be controlled via the GET parameter 'format'. It can be
26 * - xml (deprecated legacy default)
27 * - json (outputs JSONP with the 'callback' GET parameter)
29 * Expected JSON structure:
38 * "all-events-today": 0,
42 * "birthdays-today": 0,
46 * "notifications": [ ],
54 * @param App $a The Friendica App instance
56 function ping_init(App $a)
60 if (isset($_GET['format']) && $_GET['format'] == 'json') {
84 $all_events_today = 0;
91 $data['intro'] = $intro_count;
92 $data['mail'] = $mail_count;
93 $data['net'] = $network_count;
94 $data['home'] = $home_count;
95 $data['register'] = $register_count;
97 $data['all-events'] = $all_events;
98 $data['all-events-today'] = $all_events_today;
99 $data['events'] = $events;
100 $data['events-today'] = $events_today;
101 $data['birthdays'] = $birthdays;
102 $data['birthdays-today'] = $birthdays_today;
105 // Different login session than the page that is calling us.
106 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
107 $data = ['result' => ['invalid' => 1]];
109 if ($format == 'json') {
110 if (isset($_GET['callback'])) {
112 header("Content-type: application/javascript");
113 echo $_GET['callback'] . '(' . json_encode($data) . ')';
115 header("Content-type: application/json");
116 echo json_encode($data);
119 header("Content-type: text/xml");
120 echo XML::fromArray($data, $xml);
125 $notifs = ping_get_notifications(local_user());
128 "SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
129 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
130 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
131 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
132 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
133 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
134 AND `item`.`contact-id` != %d
135 ORDER BY `item`.`created` DESC",
136 intval(local_user()),
140 if (DBM::is_result($items_unseen)) {
141 $arr = ['items' => $items_unseen];
142 Addon::callHooks('network_ping', $arr);
144 foreach ($items_unseen as $item) {
153 if ($network_count) {
154 if (intval(Feature::isEnabled(local_user(), 'groups'))) {
155 // Find out how unseen network posts are spread across groups
156 $group_counts = Group::countUnseen();
157 if (DBM::is_result($group_counts)) {
158 foreach ($group_counts as $group_count) {
159 if ($group_count['count'] > 0) {
160 $groups_unseen[] = $group_count;
166 if (intval(Feature::isEnabled(local_user(), 'forumlist_widget'))) {
167 $forum_counts = ForumManager::countUnseenItems();
168 if (DBM::is_result($forum_counts)) {
169 foreach ($forum_counts as $forum_count) {
170 if ($forum_count['count'] > 0) {
171 $forums_unseen[] = $forum_count;
179 "SELECT `intro`.`id`, `intro`.`datetime`,
180 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
181 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
182 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
186 "SELECT `intro`.`id`, `intro`.`datetime`,
187 `contact`.`name`, `contact`.`url`, `contact`.`photo`
188 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
189 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
193 $intro_count = count($intros1) + count($intros2);
194 $intros = $intros1 + $intros2;
196 $myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ;
198 "SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
199 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
200 intval(local_user()),
203 $mail_count = count($mails);
205 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()) {
207 "SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`
208 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
209 WHERE `contact`.`self` = 1"
212 if (DBM::is_result($regs)) {
213 $register_count = count($regs);
217 $cachekey = "ping_init:".local_user();
218 $ev = Cache::get($cachekey);
221 "SELECT type, start, adjust FROM `event`
222 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
223 ORDER BY `start` ASC ",
224 intval(local_user()),
225 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
226 dbesc(datetime_convert('UTC', 'UTC', 'now'))
228 if (DBM::is_result($ev)) {
229 Cache::set($cachekey, $ev, CACHE_HOUR);
233 if (DBM::is_result($ev)) {
234 $all_events = count($ev);
237 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
238 foreach ($ev as $x) {
240 if ($x['type'] === 'birthday') {
246 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
247 $all_events_today ++;
258 $data['intro'] = $intro_count;
259 $data['mail'] = $mail_count;
260 $data['net'] = $network_count;
261 $data['home'] = $home_count;
262 $data['register'] = $register_count;
264 $data['all-events'] = $all_events;
265 $data['all-events-today'] = $all_events_today;
266 $data['events'] = $events;
267 $data['events-today'] = $events_today;
268 $data['birthdays'] = $birthdays;
269 $data['birthdays-today'] = $birthdays_today;
271 if (DBM::is_result($notifs)) {
272 foreach ($notifs as $notif) {
273 if ($notif['seen'] == 0) {
279 // merge all notification types in one array
280 if (DBM::is_result($intros)) {
281 foreach ($intros as $intro) {
283 'href' => System::baseUrl() . '/notifications/intros/' . $intro['id'],
284 'name' => $intro['name'],
285 'url' => $intro['url'],
286 'photo' => $intro['photo'],
287 'date' => $intro['datetime'],
289 'message' => t('{0} wants to be your friend'),
295 if (DBM::is_result($mails)) {
296 foreach ($mails as $mail) {
298 'href' => System::baseUrl() . '/message/' . $mail['id'],
299 'name' => $mail['from-name'],
300 'url' => $mail['from-url'],
301 'photo' => $mail['from-photo'],
302 'date' => $mail['created'],
304 'message' => t('{0} sent you a message'),
310 if (DBM::is_result($regs)) {
311 foreach ($regs as $reg) {
313 'href' => System::baseUrl() . '/admin/users/',
314 'name' => $reg['name'],
315 'url' => $reg['url'],
316 'photo' => $reg['micro'],
317 'date' => $reg['created'],
319 'message' => t('{0} requested registration'),
325 // sort notifications by $[]['date']
326 $sort_function = function ($a, $b) {
327 $adate = strtotime($a['date']);
328 $bdate = strtotime($b['date']);
330 // Unseen messages are kept at the top
331 // The value 31536000 means one year. This should be enough :-)
339 if ($adate == $bdate) {
342 return ($adate < $bdate) ? 1 : -1;
344 usort($notifs, $sort_function);
346 if (DBM::is_result($notifs)) {
347 // Are the nofications called from the regular process or via the friendica app?
348 $regularnotifications = (intval($_GET['uid']) && intval($_GET['_']));
350 foreach ($notifs as $notif) {
351 if ($a->is_friendica_app() || !$regularnotifications) {
352 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
355 $contact = Contact::getDetailsByURL($notif['url']);
356 if (isset($contact['micro'])) {
357 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
359 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
362 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
365 'id' => $notif['id'],
366 'href' => $notif['href'],
367 'name' => $notif['name'],
368 'url' => $notif['url'],
369 'photo' => $notif['photo'],
370 'date' => relative_date($notif['date']),
371 'message' => $notif['message'],
372 'seen' => $notif['seen'],
373 'timestamp' => strtotime($local_time)
382 if (x($_SESSION, 'sysmsg')) {
383 $sysmsgs = $_SESSION['sysmsg'];
384 unset($_SESSION['sysmsg']);
387 if (x($_SESSION, 'sysmsg_info')) {
388 $sysmsgs_info = $_SESSION['sysmsg_info'];
389 unset($_SESSION['sysmsg_info']);
392 if ($format == 'json') {
393 $data['groups'] = $groups_unseen;
394 $data['forums'] = $forums_unseen;
395 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
396 $data['notifications'] = $notifications;
398 'notice' => $sysmsgs,
399 'info' => $sysmsgs_info
402 $json_payload = json_encode(["result" => $data]);
404 if (isset($_GET['callback'])) {
406 header("Content-type: application/javascript");
407 echo $_GET['callback'] . '(' . $json_payload . ')';
409 header("Content-type: application/json");
413 // Legacy slower XML format output
414 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
416 header("Content-type: text/xml");
417 echo XML::fromArray(["result" => $data], $xml);
424 * @brief Retrieves the notifications array for the given user ID
426 * @param int $uid User id
427 * @return array Associative array of notifications
429 function ping_get_notifications($uid)
442 "SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
443 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
444 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
445 AND NOT (`notify`.`type` IN (%d, %d))
446 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
448 intval(NOTIFY_INTRO),
464 foreach ($r as $notification) {
465 if (is_null($notification["visible"])) {
466 $notification["visible"] = true;
469 if (is_null($notification["spam"])) {
470 $notification["spam"] = 0;
473 if (is_null($notification["deleted"])) {
474 $notification["deleted"] = 0;
477 if ($notification["msg_cache"]) {
478 $notification["name"] = $notification["name_cache"];
479 $notification["message"] = $notification["msg_cache"];
481 $notification["name"] = strip_tags(bbcode($notification["name"]));
482 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
485 "UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
486 dbesc($notification["name"]),
487 dbesc($notification["message"]),
488 intval($notification["id"])
492 $notification["href"] = System::baseUrl() . "/notify/view/" . $notification["id"];
494 if ($notification["visible"]
495 && !$notification["spam"]
496 && !$notification["deleted"]
497 && !(x($result, $notification["parent"]) && is_array($result[$notification["parent"]]))
499 // Should we condense the notifications or show them all?
500 if (PConfig::get(local_user(), 'system', 'detailed_notif')) {
501 $result[$notification["id"]] = $notification;
503 $result[$notification["parent"]] = $notification;
507 } while ((count($result) < 50) && !$quit);
513 * @brief Backward-compatible XML formatting for ping.php output
516 * @param array $data The initial ping data array
517 * @param int $sysnotify Number of unseen system notifications
518 * @param array $notifs Complete list of notification
519 * @param array $sysmsgs List of system notice messages
520 * @param array $sysmsgs_info List of system info messages
521 * @param int $groups_unseen Number of unseen group items
522 * @param int $forums_unseen Number of unseen forum items
523 * @return array XML-transform ready data array
525 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
528 foreach ($notifs as $key => $notif) {
529 $notifications[$key . ':note'] = $notif['message'];
531 $notifications[$key . ':@attributes'] = [
532 'id' => $notif['id'],
533 'href' => $notif['href'],
534 'name' => $notif['name'],
535 'url' => $notif['url'],
536 'photo' => $notif['photo'],
537 'date' => $notif['date'],
538 'seen' => $notif['seen'],
539 'timestamp' => $notif['timestamp']
544 foreach ($sysmsgs as $key => $m) {
545 $sysmsg[$key . ':notice'] = $m;
547 foreach ($sysmsgs_info as $key => $m) {
548 $sysmsg[$key . ':info'] = $m;
551 $data['notif'] = $notifications;
552 $data['@attributes'] = ['count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']];
553 $data['sysmsgs'] = $sysmsg;
555 if ($data['register'] == 0) {
556 unset($data['register']);
560 if (count($groups_unseen)) {
561 foreach ($groups_unseen as $key => $item) {
562 $groups[$key . ':group'] = $item['count'];
563 $groups[$key . ':@attributes'] = ['id' => $item['id']];
565 $data['groups'] = $groups;
569 if (count($forums_unseen)) {
570 foreach ($forums_unseen as $key => $item) {
571 $forums[$count . ':forum'] = $item['count'];
572 $forums[$count . ':@attributes'] = ['id' => $item['id']];
574 $data['forums'] = $forums;