3 * @file include/ping.php
7 use Friendica\Content\ForumManager;
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Core\Cache\Duration;
10 use Friendica\Core\Hook;
11 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Group;
15 use Friendica\Model\Item;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Temporal;
18 use Friendica\Util\Proxy as ProxyUtils;
19 use Friendica\Util\XML;
22 * 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
54 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
56 function ping_init(App $a)
60 if (isset($_GET['format']) && $_GET['format'] == 'json') {
77 $all_events_today = 0;
84 $data['intro'] = $intro_count;
85 $data['mail'] = $mail_count;
86 $data['net'] = $network_count;
87 $data['home'] = $home_count;
88 $data['register'] = $register_count;
90 $data['all-events'] = $all_events;
91 $data['all-events-today'] = $all_events_today;
92 $data['events'] = $events;
93 $data['events-today'] = $events_today;
94 $data['birthdays'] = $birthdays;
95 $data['birthdays-today'] = $birthdays_today;
98 // Different login session than the page that is calling us.
99 if (!empty($_GET['uid']) && intval($_GET['uid']) != local_user()) {
100 $data = ['result' => ['invalid' => 1]];
102 if ($format == 'json') {
103 if (isset($_GET['callback'])) {
105 header("Content-type: application/javascript");
106 echo $_GET['callback'] . '(' . json_encode($data) . ')';
108 header("Content-type: application/json");
109 echo json_encode($data);
112 header("Content-type: text/xml");
113 echo XML::fromArray($data, $xml);
118 $notifs = ping_get_notifications(local_user());
120 $condition = ["`unseen` AND `uid` = ? AND `contact-id` != ?", local_user(), local_user()];
121 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
122 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid', 'wall'];
123 $params = ['order' => ['received' => true]];
124 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
126 if (DBA::isResult($items)) {
127 $items_unseen = Item::inArray($items);
128 $arr = ['items' => $items_unseen];
129 Hook::callAll('network_ping', $arr);
131 foreach ($items_unseen as $item) {
140 if ($network_count) {
141 // Find out how unseen network posts are spread across groups
142 $group_counts = Group::countUnseen();
143 if (DBA::isResult($group_counts)) {
144 foreach ($group_counts as $group_count) {
145 if ($group_count['count'] > 0) {
146 $groups_unseen[] = $group_count;
151 $forum_counts = ForumManager::countUnseenItems();
152 if (DBA::isResult($forum_counts)) {
153 foreach ($forum_counts as $forum_count) {
154 if ($forum_count['count'] > 0) {
155 $forums_unseen[] = $forum_count;
162 "SELECT `intro`.`id`, `intro`.`datetime`,
163 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
164 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
165 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
169 "SELECT `intro`.`id`, `intro`.`datetime`,
170 `contact`.`name`, `contact`.`url`, `contact`.`photo`
171 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
172 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
176 $intro_count = count($intros1) + count($intros2);
177 $intros = $intros1 + $intros2;
179 $myurl = DI::baseUrl() . '/profile/' . $a->user['nickname'];
181 "SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
182 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
183 intval(local_user()),
186 $mail_count = count($mails);
188 if (intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::APPROVE && is_site_admin()) {
189 $regs = Friendica\Model\Register::getPending();
191 if (DBA::isResult($regs)) {
192 $register_count = count($regs);
196 $cachekey = "ping_init:".local_user();
197 $ev = DI::cache()->get($cachekey);
200 "SELECT type, start, adjust FROM `event`
201 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
202 ORDER BY `start` ASC ",
203 intval(local_user()),
204 DBA::escape(DateTimeFormat::utc('now + 7 days')),
205 DBA::escape(DateTimeFormat::utcNow())
207 if (DBA::isResult($ev)) {
208 DI::cache()->set($cachekey, $ev, Duration::HOUR);
212 if (DBA::isResult($ev)) {
213 $all_events = count($ev);
216 $str_now = DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d');
217 foreach ($ev as $x) {
219 if ($x['type'] === 'birthday') {
225 if (DateTimeFormat::convert($x['start'], ((intval($x['adjust'])) ? $a->timezone : 'UTC'), 'UTC', 'Y-m-d') === $str_now) {
226 $all_events_today ++;
237 $data['intro'] = $intro_count;
238 $data['mail'] = $mail_count;
239 $data['net'] = $network_count;
240 $data['home'] = $home_count;
241 $data['register'] = $register_count;
243 $data['all-events'] = $all_events;
244 $data['all-events-today'] = $all_events_today;
245 $data['events'] = $events;
246 $data['events-today'] = $events_today;
247 $data['birthdays'] = $birthdays;
248 $data['birthdays-today'] = $birthdays_today;
250 if (DBA::isResult($notifs)) {
251 foreach ($notifs as $notif) {
252 if ($notif['seen'] == 0) {
258 // merge all notification types in one array
259 if (DBA::isResult($intros)) {
260 foreach ($intros as $intro) {
263 'href' => DI::baseUrl() . '/notifications/intros/' . $intro['id'],
264 'name' => $intro['name'],
265 'url' => $intro['url'],
266 'photo' => $intro['photo'],
267 'date' => $intro['datetime'],
269 'message' => DI::l10n()->t('{0} wants to be your friend'),
275 if (DBA::isResult($regs)) {
276 foreach ($regs as $reg) {
279 'href' => DI::baseUrl() . '/admin/users/',
280 'name' => $reg['name'],
281 'url' => $reg['url'],
282 'photo' => $reg['micro'],
283 'date' => $reg['created'],
285 'message' => DI::l10n()->t('{0} requested registration'),
291 // sort notifications by $[]['date']
292 $sort_function = function ($a, $b) {
293 $adate = strtotime($a['date']);
294 $bdate = strtotime($b['date']);
296 // Unseen messages are kept at the top
297 // The value 31536000 means one year. This should be enough :-)
305 if ($adate == $bdate) {
308 return ($adate < $bdate) ? 1 : -1;
310 usort($notifs, $sort_function);
312 if (DBA::isResult($notifs)) {
313 foreach ($notifs as $notif) {
314 $contact = Contact::getDetailsByURL($notif['url']);
315 if (isset($contact['micro'])) {
316 $notif['photo'] = ProxyUtils::proxifyUrl($contact['micro'], false, ProxyUtils::SIZE_MICRO);
318 $notif['photo'] = ProxyUtils::proxifyUrl($notif['photo'], false, ProxyUtils::SIZE_MICRO);
321 $local_time = DateTimeFormat::local($notif['date']);
324 'id' => $notif['id'],
325 'href' => $notif['href'],
326 'name' => $notif['name'],
327 'url' => $notif['url'],
328 'photo' => $notif['photo'],
329 'date' => Temporal::getRelativeDate($notif['date']),
330 'message' => $notif['message'],
331 'seen' => $notif['seen'],
332 'timestamp' => strtotime($local_time)
341 if (!empty($_SESSION['sysmsg'])) {
342 $sysmsgs = $_SESSION['sysmsg'];
343 unset($_SESSION['sysmsg']);
346 if (!empty($_SESSION['sysmsg_info'])) {
347 $sysmsgs_info = $_SESSION['sysmsg_info'];
348 unset($_SESSION['sysmsg_info']);
351 if ($format == 'json') {
352 $data['groups'] = $groups_unseen;
353 $data['forums'] = $forums_unseen;
354 $data['notification'] = $sysnotify_count + $intro_count + $register_count;
355 $data['notifications'] = $notifications;
357 'notice' => $sysmsgs,
358 'info' => $sysmsgs_info
361 $json_payload = json_encode(["result" => $data]);
363 if (isset($_GET['callback'])) {
365 header("Content-type: application/javascript");
366 echo $_GET['callback'] . '(' . $json_payload . ')';
368 header("Content-type: application/json");
372 // Legacy slower XML format output
373 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
375 header("Content-type: text/xml");
376 echo XML::fromArray(["result" => $data], $xml);
383 * Retrieves the notifications array for the given user ID
385 * @param int $uid User id
386 * @return array Associative array of notifications
387 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
389 function ping_get_notifications($uid)
400 "SELECT `notify`.*, `item`.`visible`, `item`.`deleted`
401 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
402 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
403 AND NOT (`notify`.`type` IN (%d, %d))
404 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
406 intval(NOTIFY_INTRO),
422 foreach ($r as $notification) {
423 if (is_null($notification["visible"])) {
424 $notification["visible"] = true;
427 if (is_null($notification["deleted"])) {
428 $notification["deleted"] = 0;
431 if ($notification["msg_cache"]) {
432 $notification["name"] = $notification["name_cache"];
433 $notification["message"] = $notification["msg_cache"];
435 $notification["name"] = strip_tags(BBCode::convert($notification["name"]));
436 $notification["message"] = Friendica\Model\Notify::formatMessage($notification["name"], strip_tags(BBCode::convert($notification["msg"])));
439 "UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
440 DBA::escape($notification["name"]),
441 DBA::escape($notification["message"]),
442 intval($notification["id"])
446 $notification["href"] = DI::baseUrl() . "/notification/" . $notification["id"];
448 if ($notification["visible"]
449 && !$notification["deleted"]
450 && empty($result[$notification["parent"]])
452 // Should we condense the notifications or show them all?
453 if (DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
454 $result[$notification["id"]] = $notification;
456 $result[$notification["parent"]] = $notification;
460 } while ((count($result) < 50) && !$quit);
466 * Backward-compatible XML formatting for ping.php output
469 * @param array $data The initial ping data array
470 * @param int $sysnotify_count Number of unseen system notifications
471 * @param array $notifs Complete list of notification
472 * @param array $sysmsgs List of system notice messages
473 * @param array $sysmsgs_info List of system info messages
474 * @param array $groups_unseen List of unseen group items
475 * @param array $forums_unseen List of unseen forum items
477 * @return array XML-transform ready data array
479 function ping_format_xml_data($data, $sysnotify_count, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
482 foreach ($notifs as $key => $notif) {
483 $notifications[$key . ':note'] = $notif['message'];
485 $notifications[$key . ':@attributes'] = [
486 'id' => $notif['id'],
487 'href' => $notif['href'],
488 'name' => $notif['name'],
489 'url' => $notif['url'],
490 'photo' => $notif['photo'],
491 'date' => $notif['date'],
492 'seen' => $notif['seen'],
493 'timestamp' => $notif['timestamp']
498 foreach ($sysmsgs as $key => $m) {
499 $sysmsg[$key . ':notice'] = $m;
501 foreach ($sysmsgs_info as $key => $m) {
502 $sysmsg[$key . ':info'] = $m;
505 $data['notif'] = $notifications;
506 $data['@attributes'] = ['count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']];
507 $data['sysmsgs'] = $sysmsg;
509 if ($data['register'] == 0) {
510 unset($data['register']);
514 if (count($groups_unseen)) {
515 foreach ($groups_unseen as $key => $item) {
516 $groups[$key . ':group'] = $item['count'];
517 $groups[$key . ':@attributes'] = ['id' => $item['id']];
519 $data['groups'] = $groups;
523 if (count($forums_unseen)) {
524 foreach ($forums_unseen as $key => $item) {
525 $forums[$key . ':forum'] = $item['count'];
526 $forums[$key . ':@attributes'] = ['id' => $item['id']];
528 $data['forums'] = $forums;