3 * @file include/ping.php
7 use Friendica\Content\Feature;
8 use Friendica\Content\ForumManager;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Cache;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\System;
15 use Friendica\Database\DBM;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Group;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Temporal;
20 use Friendica\Util\XML;
22 require_once 'mod/proxy.php';
23 require_once 'include/enotify.php';
26 * @brief Outputs the counts and the lists of various notifications
28 * The output format can be controlled via the GET parameter 'format'. It can be
29 * - xml (deprecated legacy default)
30 * - json (outputs JSONP with the 'callback' GET parameter)
32 * Expected JSON structure:
41 * "all-events-today": 0,
45 * "birthdays-today": 0,
49 * "notifications": [ ],
57 * @param App $a The Friendica App instance
59 function ping_init(App $a)
63 if (isset($_GET['format']) && $_GET['format'] == 'json') {
87 $all_events_today = 0;
94 $data['intro'] = $intro_count;
95 $data['mail'] = $mail_count;
96 $data['net'] = $network_count;
97 $data['home'] = $home_count;
98 $data['register'] = $register_count;
100 $data['all-events'] = $all_events;
101 $data['all-events-today'] = $all_events_today;
102 $data['events'] = $events;
103 $data['events-today'] = $events_today;
104 $data['birthdays'] = $birthdays;
105 $data['birthdays-today'] = $birthdays_today;
108 // Different login session than the page that is calling us.
109 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
110 $data = ['result' => ['invalid' => 1]];
112 if ($format == 'json') {
113 if (isset($_GET['callback'])) {
115 header("Content-type: application/javascript");
116 echo $_GET['callback'] . '(' . json_encode($data) . ')';
118 header("Content-type: application/json");
119 echo json_encode($data);
122 header("Content-type: text/xml");
123 echo XML::fromArray($data, $xml);
128 $notifs = ping_get_notifications(local_user());
131 "SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
132 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
133 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
134 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
135 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
136 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
137 AND `item`.`contact-id` != %d
138 ORDER BY `item`.`created` DESC",
139 intval(local_user()),
143 if (DBM::is_result($items_unseen)) {
144 $arr = ['items' => $items_unseen];
145 Addon::callHooks('network_ping', $arr);
147 foreach ($items_unseen as $item) {
156 if ($network_count) {
157 if (intval(Feature::isEnabled(local_user(), 'groups'))) {
158 // Find out how unseen network posts are spread across groups
159 $group_counts = Group::countUnseen();
160 if (DBM::is_result($group_counts)) {
161 foreach ($group_counts as $group_count) {
162 if ($group_count['count'] > 0) {
163 $groups_unseen[] = $group_count;
169 if (intval(Feature::isEnabled(local_user(), 'forumlist_widget'))) {
170 $forum_counts = ForumManager::countUnseenItems();
171 if (DBM::is_result($forum_counts)) {
172 foreach ($forum_counts as $forum_count) {
173 if ($forum_count['count'] > 0) {
174 $forums_unseen[] = $forum_count;
182 "SELECT `intro`.`id`, `intro`.`datetime`,
183 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
184 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
185 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
189 "SELECT `intro`.`id`, `intro`.`datetime`,
190 `contact`.`name`, `contact`.`url`, `contact`.`photo`
191 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
192 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
196 $intro_count = count($intros1) + count($intros2);
197 $intros = $intros1 + $intros2;
199 $myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ;
201 "SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
202 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
203 intval(local_user()),
206 $mail_count = count($mails);
208 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()) {
210 "SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`
211 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
212 WHERE `contact`.`self` = 1"
215 if (DBM::is_result($regs)) {
216 $register_count = count($regs);
220 $cachekey = "ping_init:".local_user();
221 $ev = Cache::get($cachekey);
224 "SELECT type, start, adjust FROM `event`
225 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
226 ORDER BY `start` ASC ",
227 intval(local_user()),
228 dbesc(DateTimeFormat::utc('now + 7 days')),
229 dbesc(DateTimeFormat::utcNow())
231 if (DBM::is_result($ev)) {
232 Cache::set($cachekey, $ev, CACHE_HOUR);
236 if (DBM::is_result($ev)) {
237 $all_events = count($ev);
240 $str_now = DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d');
241 foreach ($ev as $x) {
243 if ($x['type'] === 'birthday') {
249 if (DateTimeFormat::convert($x['start'], ((intval($x['adjust'])) ? $a->timezone : 'UTC'), 'UTC', 'Y-m-d') === $str_now) {
250 $all_events_today ++;
261 $data['intro'] = $intro_count;
262 $data['mail'] = $mail_count;
263 $data['net'] = $network_count;
264 $data['home'] = $home_count;
265 $data['register'] = $register_count;
267 $data['all-events'] = $all_events;
268 $data['all-events-today'] = $all_events_today;
269 $data['events'] = $events;
270 $data['events-today'] = $events_today;
271 $data['birthdays'] = $birthdays;
272 $data['birthdays-today'] = $birthdays_today;
274 if (DBM::is_result($notifs)) {
275 foreach ($notifs as $notif) {
276 if ($notif['seen'] == 0) {
282 // merge all notification types in one array
283 if (DBM::is_result($intros)) {
284 foreach ($intros as $intro) {
286 'href' => System::baseUrl() . '/notifications/intros/' . $intro['id'],
287 'name' => $intro['name'],
288 'url' => $intro['url'],
289 'photo' => $intro['photo'],
290 'date' => $intro['datetime'],
292 'message' => L10n::t('{0} wants to be your friend'),
298 if (DBM::is_result($mails)) {
299 foreach ($mails as $mail) {
301 'href' => System::baseUrl() . '/message/' . $mail['id'],
302 'name' => $mail['from-name'],
303 'url' => $mail['from-url'],
304 'photo' => $mail['from-photo'],
305 'date' => $mail['created'],
307 'message' => L10n::t('{0} sent you a message'),
313 if (DBM::is_result($regs)) {
314 foreach ($regs as $reg) {
316 'href' => System::baseUrl() . '/admin/users/',
317 'name' => $reg['name'],
318 'url' => $reg['url'],
319 'photo' => $reg['micro'],
320 'date' => $reg['created'],
322 'message' => L10n::t('{0} requested registration'),
328 // sort notifications by $[]['date']
329 $sort_function = function ($a, $b) {
330 $adate = strtotime($a['date']);
331 $bdate = strtotime($b['date']);
333 // Unseen messages are kept at the top
334 // The value 31536000 means one year. This should be enough :-)
342 if ($adate == $bdate) {
345 return ($adate < $bdate) ? 1 : -1;
347 usort($notifs, $sort_function);
349 if (DBM::is_result($notifs)) {
350 // Are the nofications called from the regular process or via the friendica app?
351 $regularnotifications = (intval($_GET['uid']) && intval($_GET['_']));
353 foreach ($notifs as $notif) {
354 if ($a->is_friendica_app() || !$regularnotifications) {
355 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
358 $contact = Contact::getDetailsByURL($notif['url']);
359 if (isset($contact['micro'])) {
360 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
362 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
365 $local_time = DateTimeFormat::local($notif['date']);
368 'id' => $notif['id'],
369 'href' => $notif['href'],
370 'name' => $notif['name'],
371 'url' => $notif['url'],
372 'photo' => $notif['photo'],
373 'date' => Temporal::getRelativeDate($notif['date']),
374 'message' => $notif['message'],
375 'seen' => $notif['seen'],
376 'timestamp' => strtotime($local_time)
385 if (x($_SESSION, 'sysmsg')) {
386 $sysmsgs = $_SESSION['sysmsg'];
387 unset($_SESSION['sysmsg']);
390 if (x($_SESSION, 'sysmsg_info')) {
391 $sysmsgs_info = $_SESSION['sysmsg_info'];
392 unset($_SESSION['sysmsg_info']);
395 if ($format == 'json') {
396 $data['groups'] = $groups_unseen;
397 $data['forums'] = $forums_unseen;
398 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
399 $data['notifications'] = $notifications;
401 'notice' => $sysmsgs,
402 'info' => $sysmsgs_info
405 $json_payload = json_encode(["result" => $data]);
407 if (isset($_GET['callback'])) {
409 header("Content-type: application/javascript");
410 echo $_GET['callback'] . '(' . $json_payload . ')';
412 header("Content-type: application/json");
416 // Legacy slower XML format output
417 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
419 header("Content-type: text/xml");
420 echo XML::fromArray(["result" => $data], $xml);
427 * @brief Retrieves the notifications array for the given user ID
429 * @param int $uid User id
430 * @return array Associative array of notifications
432 function ping_get_notifications($uid)
445 "SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
446 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
447 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
448 AND NOT (`notify`.`type` IN (%d, %d))
449 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
451 intval(NOTIFY_INTRO),
467 foreach ($r as $notification) {
468 if (is_null($notification["visible"])) {
469 $notification["visible"] = true;
472 if (is_null($notification["spam"])) {
473 $notification["spam"] = 0;
476 if (is_null($notification["deleted"])) {
477 $notification["deleted"] = 0;
480 if ($notification["msg_cache"]) {
481 $notification["name"] = $notification["name_cache"];
482 $notification["message"] = $notification["msg_cache"];
484 $notification["name"] = strip_tags(BBCode::convert($notification["name"]));
485 $notification["message"] = format_notification_message($notification["name"], strip_tags(BBCode::convert($notification["msg"])));
488 "UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
489 dbesc($notification["name"]),
490 dbesc($notification["message"]),
491 intval($notification["id"])
495 $notification["href"] = System::baseUrl() . "/notify/view/" . $notification["id"];
497 if ($notification["visible"]
498 && !$notification["spam"]
499 && !$notification["deleted"]
500 && !(x($result, $notification["parent"]) && is_array($result[$notification["parent"]]))
502 // Should we condense the notifications or show them all?
503 if (PConfig::get(local_user(), 'system', 'detailed_notif')) {
504 $result[$notification["id"]] = $notification;
506 $result[$notification["parent"]] = $notification;
510 } while ((count($result) < 50) && !$quit);
516 * @brief Backward-compatible XML formatting for ping.php output
519 * @param array $data The initial ping data array
520 * @param int $sysnotify Number of unseen system notifications
521 * @param array $notifs Complete list of notification
522 * @param array $sysmsgs List of system notice messages
523 * @param array $sysmsgs_info List of system info messages
524 * @param int $groups_unseen Number of unseen group items
525 * @param int $forums_unseen Number of unseen forum items
526 * @return array XML-transform ready data array
528 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
531 foreach ($notifs as $key => $notif) {
532 $notifications[$key . ':note'] = $notif['message'];
534 $notifications[$key . ':@attributes'] = [
535 'id' => $notif['id'],
536 'href' => $notif['href'],
537 'name' => $notif['name'],
538 'url' => $notif['url'],
539 'photo' => $notif['photo'],
540 'date' => $notif['date'],
541 'seen' => $notif['seen'],
542 'timestamp' => $notif['timestamp']
547 foreach ($sysmsgs as $key => $m) {
548 $sysmsg[$key . ':notice'] = $m;
550 foreach ($sysmsgs_info as $key => $m) {
551 $sysmsg[$key . ':info'] = $m;
554 $data['notif'] = $notifications;
555 $data['@attributes'] = ['count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']];
556 $data['sysmsgs'] = $sysmsg;
558 if ($data['register'] == 0) {
559 unset($data['register']);
563 if (count($groups_unseen)) {
564 foreach ($groups_unseen as $key => $item) {
565 $groups[$key . ':group'] = $item['count'];
566 $groups[$key . ':@attributes'] = ['id' => $item['id']];
568 $data['groups'] = $groups;
572 if (count($forums_unseen)) {
573 foreach ($forums_unseen as $key => $item) {
574 $forums[$key . ':forum'] = $item['count'];
575 $forums[$key . ':@attributes'] = ['id' => $item['id']];
577 $data['forums'] = $forums;