4 use Friendica\Core\System;
6 require_once('include/datetime.php');
7 require_once('include/bbcode.php');
8 require_once('include/ForumManager.php');
9 require_once('include/group.php');
10 require_once('mod/proxy.php');
11 require_once('include/xml.php');
12 require_once('include/cache.php');
13 require_once('include/enotify.php');
16 * @brief Outputs the counts and the lists of various notifications
18 * The output format can be controlled via the GET parameter 'format'. It can be
19 * - xml (deprecated legacy default)
20 * - json (outputs JSONP with the 'callback' GET parameter)
22 * Expected JSON structure:
31 * "all-events-today": 0,
35 * "birthdays-today": 0,
39 * "notifications": [ ],
47 * @param App $a The Friendica App instance
49 function ping_init(App $a)
53 if (isset($_GET['format']) && $_GET['format'] == 'json') {
65 $notifications = array();
73 $groups_unseen = array();
74 $forums_unseen = array();
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 (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
101 $data = array('result' => array('invalid' => 1));
103 if ($format == 'json') {
104 if (isset($_GET['callback'])) {
106 header("Content-type: application/javascript");
107 echo $_GET['callback'] . '(' . json_encode($data) . ')';
109 header("Content-type: application/json");
110 echo json_encode($data);
113 header("Content-type: text/xml");
114 echo xml::from_array($data, $xml);
119 $notifs = ping_get_notifications(local_user());
121 $items_unseen = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
122 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
123 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
124 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
125 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
126 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
127 AND `item`.`contact-id` != %d
128 ORDER BY `item`.`created` DESC",
129 intval(local_user()), intval(local_user())
132 if (dbm::is_result($items_unseen)) {
133 $arr = array('items' => $items_unseen);
134 call_hooks('network_ping', $arr);
136 foreach ($items_unseen as $item) {
145 if ($network_count) {
146 if (intval(feature_enabled(local_user(), 'groups'))) {
147 // Find out how unseen network posts are spread across groups
148 $group_counts = groups_count_unseen();
149 if (dbm::is_result($group_counts)) {
150 foreach ($group_counts as $group_count) {
151 if ($group_count['count'] > 0) {
152 $groups_unseen[] = $group_count;
158 if (intval(feature_enabled(local_user(), 'forumlist_widget'))) {
159 $forum_counts = ForumManager::count_unseen_items();
160 if (dbm::is_result($forums_counts)) {
161 foreach ($forums_counts as $forum_count) {
162 if ($forum_count['count'] > 0) {
163 $forums_unseen[] = $forum_count;
170 $intros1 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
171 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
172 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
173 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
176 $intros2 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
177 `contact`.`name`, `contact`.`url`, `contact`.`photo`
178 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
179 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
183 $intro_count = count($intros1) + count($intros2);
184 $intros = $intros1 + $intros2;
186 $myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ;
187 $mails = qu("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
188 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
189 intval(local_user()),
192 $mail_count = count($mails);
194 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
195 $regs = qu("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total`
196 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
197 WHERE `contact`.`self` = 1");
199 if (dbm::is_result($regs)) {
200 $register_count = $regs[0]['total'];
204 $cachekey = "ping_init:".local_user();
205 $ev = Cache::get($cachekey);
207 $ev = qu("SELECT type, start, adjust FROM `event`
208 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
209 ORDER BY `start` ASC ",
210 intval(local_user()),
211 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
212 dbesc(datetime_convert('UTC', 'UTC', 'now'))
214 if (dbm::is_result($ev)) {
215 Cache::set($cachekey, $ev, CACHE_HOUR);
219 if (dbm::is_result($ev)) {
220 $all_events = count($ev);
223 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
226 if ($x['type'] === 'birthday') {
233 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
234 $all_events_today ++;
244 $data['intro'] = $intro_count;
245 $data['mail'] = $mail_count;
246 $data['net'] = $network_count;
247 $data['home'] = $home_count;
248 $data['register'] = $register_count;
250 $data['all-events'] = $all_events;
251 $data['all-events-today'] = $all_events_today;
252 $data['events'] = $events;
253 $data['events-today'] = $events_today;
254 $data['birthdays'] = $birthdays;
255 $data['birthdays-today'] = $birthdays_today;
257 if (dbm::is_result($notifs)) {
258 foreach ($notifs as $notif) {
259 if ($notif['seen'] == 0) {
265 // merge all notification types in one array
266 if (dbm::is_result($intros)) {
267 foreach ($intros as $intro) {
269 'href' => System::baseUrl() . '/notifications/intros/' . $intro['id'],
270 'name' => $intro['name'],
271 'url' => $intro['url'],
272 'photo' => $intro['photo'],
273 'date' => $intro['datetime'],
275 'message' => t('{0} wants to be your friend'),
281 if (dbm::is_result($mails)) {
282 foreach ($mails as $mail) {
284 'href' => System::baseUrl() . '/message/' . $mail['id'],
285 'name' => $mail['from-name'],
286 'url' => $mail['from-url'],
287 'photo' => $mail['from-photo'],
288 'date' => $mail['created'],
290 'message' => t('{0} sent you a message'),
296 if (dbm::is_result($regs)) {
297 foreach ($regs as $reg) {
299 'href' => System::baseUrl() . '/admin/users/',
300 'name' => $reg['name'],
301 'url' => $reg['url'],
302 'photo' => $reg['micro'],
303 'date' => $reg['created'],
305 'message' => t('{0} requested registration'),
311 // sort notifications by $[]['date']
312 $sort_function = function($a, $b) {
313 $adate = strtotime($a['date']);
314 $bdate = strtotime($b['date']);
316 // Unseen messages are kept at the top
317 // The value 31536000 means one year. This should be enough :-)
325 if ($adate == $bdate) {
328 return ($adate < $bdate) ? 1 : -1;
330 usort($notifs, $sort_function);
332 if (dbm::is_result($notifs)) {
333 // Are the nofications called from the regular process or via the friendica app?
334 $regularnotifications = (intval($_GET['uid']) && intval($_GET['_']));
336 foreach ($notifs as $notif) {
337 if ($a->is_friendica_app() || !$regularnotifications) {
338 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
341 $contact = get_contact_details_by_url($notif['url']);
342 if (isset($contact['micro'])) {
343 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
345 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
348 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
350 $notifications[] = array(
351 'id' => $notif['id'],
352 'href' => $notif['href'],
353 'name' => $notif['name'],
354 'url' => $notif['url'],
355 'photo' => $notif['photo'],
356 'date' => relative_date($notif['date']),
357 'message' => $notif['message'],
358 'seen' => $notif['seen'],
359 'timestamp' => strtotime($local_time)
366 $sysmsgs_info = array();
368 if (x($_SESSION, 'sysmsg')) {
369 $sysmsgs = $_SESSION['sysmsg'];
370 unset($_SESSION['sysmsg']);
373 if (x($_SESSION, 'sysmsg_info')) {
374 $sysmsgs_info = $_SESSION['sysmsg_info'];
375 unset($_SESSION['sysmsg_info']);
378 if ($format == 'json') {
379 $data['groups'] = $groups_unseen;
380 $data['forums'] = $forums_unseen;
381 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
382 $data['notifications'] = $notifications;
383 $data['sysmsgs'] = array(
384 'notice' => $sysmsgs,
385 'info' => $sysmsgs_info
388 $json_payload = json_encode(array("result" => $data));
390 if (isset($_GET['callback'])) {
392 header("Content-type: application/javascript");
393 echo $_GET['callback'] . '(' . $json_payload . ')';
395 header("Content-type: application/json");
399 // Legacy slower XML format output
400 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
402 header("Content-type: text/xml");
403 echo xml::from_array(array("result" => $data), $xml);
410 * @brief Retrieves the notifications array for the given user ID
412 * @param int $uid User id
413 * @return array Associative array of notifications
415 function ping_get_notifications($uid)
427 $r = qu("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
428 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
429 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
430 AND NOT (`notify`.`type` IN (%d, %d))
431 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
433 intval(NOTIFY_INTRO),
449 foreach ($r AS $notification) {
450 if (is_null($notification["visible"])) {
451 $notification["visible"] = true;
454 if (is_null($notification["spam"])) {
455 $notification["spam"] = 0;
458 if (is_null($notification["deleted"])) {
459 $notification["deleted"] = 0;
462 if ($notification["msg_cache"]) {
463 $notification["name"] = $notification["name_cache"];
464 $notification["message"] = $notification["msg_cache"];
466 $notification["name"] = strip_tags(bbcode($notification["name"]));
467 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
469 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
470 dbesc($notification["name"]),
471 dbesc($notification["message"]),
472 intval($notification["id"])
476 $notification["href"] = System::baseUrl() . "/notify/view/" . $notification["id"];
478 if ($notification["visible"] && !$notification["spam"] &&
479 !$notification["deleted"] && !is_array($result[$notification["parent"]])) {
480 $result[$notification["parent"]] = $notification;
483 } while ((count($result) < 50) && !$quit);
489 * @brief Backward-compatible XML formatting for ping.php output
492 * @param array $data The initial ping data array
493 * @param int $sysnotify_count Number of unseen system notifications
494 * @param array $notifs Complete list of notification
495 * @param array $sysmsgs List of system notice messages
496 * @param array $sysmsgs_info List of system info messages
497 * @param int $groups_unseen Number of unseen group items
498 * @param int $forums_unseen Number of unseen forum items
499 * @return array XML-transform ready data array
501 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
503 $notifications = array();
504 foreach($notifs as $key => $notif) {
505 $notifications[$key . ':note'] = $notif['message'];
507 $notifications[$key . ':@attributes'] = array(
508 'id' => $notif['id'],
509 'href' => $notif['href'],
510 'name' => $notif['name'],
511 'url' => $notif['url'],
512 'photo' => $notif['photo'],
513 'date' => $notif['date'],
514 'seen' => $notif['seen'],
515 'timestamp' => $notif['timestamp']
520 foreach ($sysmsgs as $key => $m){
521 $sysmsg[$key . ':notice'] = $m;
523 foreach ($sysmsgs_info as $key => $m){
524 $sysmsg[$key . ':info'] = $m;
527 $data['notif'] = $notifications;
528 $data['@attributes'] = array('count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']);
529 $data['sysmsgs'] = $sysmsg;
531 if ($data['register'] == 0) {
532 unset($data['register']);
536 if (count($groups_unseen)) {
537 foreach ($groups_unseen as $key => $item) {
538 $groups[$key . ':group'] = $item['count'];
539 $groups[$key . ':@attributes'] = array('id' => $item['id']);
541 $data['groups'] = $groups;
545 if (count($forums_unseen)) {
546 foreach ($forums_unseen as $key => $item) {
547 $forums[$count . ':forum'] = $item['count'];
548 $forums[$count . ':@attributes'] = array('id' => $item['id']);
550 $data['forums'] = $forums;