4 use Friendica\Core\System;
5 use Friendica\Core\PConfig;
7 require_once('include/datetime.php');
8 require_once('include/bbcode.php');
9 require_once('include/ForumManager.php');
10 require_once('include/group.php');
11 require_once('mod/proxy.php');
12 require_once('include/xml.php');
13 require_once('include/cache.php');
14 require_once('include/enotify.php');
17 * @brief Outputs the counts and the lists of various notifications
19 * The output format can be controlled via the GET parameter 'format'. It can be
20 * - xml (deprecated legacy default)
21 * - json (outputs JSONP with the 'callback' GET parameter)
23 * Expected JSON structure:
32 * "all-events-today": 0,
36 * "birthdays-today": 0,
40 * "notifications": [ ],
48 * @param App $a The Friendica App instance
50 function ping_init(App $a)
54 if (isset($_GET['format']) && $_GET['format'] == 'json') {
66 $notifications = array();
74 $groups_unseen = array();
75 $forums_unseen = array();
78 $all_events_today = 0;
85 $data['intro'] = $intro_count;
86 $data['mail'] = $mail_count;
87 $data['net'] = $network_count;
88 $data['home'] = $home_count;
89 $data['register'] = $register_count;
91 $data['all-events'] = $all_events;
92 $data['all-events-today'] = $all_events_today;
93 $data['events'] = $events;
94 $data['events-today'] = $events_today;
95 $data['birthdays'] = $birthdays;
96 $data['birthdays-today'] = $birthdays_today;
99 // Different login session than the page that is calling us.
100 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
102 $data = array('result' => array('invalid' => 1));
104 if ($format == 'json') {
105 if (isset($_GET['callback'])) {
107 header("Content-type: application/javascript");
108 echo $_GET['callback'] . '(' . json_encode($data) . ')';
110 header("Content-type: application/json");
111 echo json_encode($data);
114 header("Content-type: text/xml");
115 echo xml::from_array($data, $xml);
120 $notifs = ping_get_notifications(local_user());
122 $items_unseen = q("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
123 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
124 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
125 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
126 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
127 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
128 AND `item`.`contact-id` != %d
129 ORDER BY `item`.`created` DESC",
130 intval(local_user()), intval(local_user())
133 if (dbm::is_result($items_unseen)) {
134 $arr = array('items' => $items_unseen);
135 call_hooks('network_ping', $arr);
137 foreach ($items_unseen as $item) {
146 if ($network_count) {
147 if (intval(feature_enabled(local_user(), 'groups'))) {
148 // Find out how unseen network posts are spread across groups
149 $group_counts = groups_count_unseen();
150 if (dbm::is_result($group_counts)) {
151 foreach ($group_counts as $group_count) {
152 if ($group_count['count'] > 0) {
153 $groups_unseen[] = $group_count;
159 if (intval(feature_enabled(local_user(), 'forumlist_widget'))) {
160 $forum_counts = ForumManager::count_unseen_items();
161 if (dbm::is_result($forums_counts)) {
162 foreach ($forums_counts as $forum_count) {
163 if ($forum_count['count'] > 0) {
164 $forums_unseen[] = $forum_count;
171 $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
172 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
173 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
174 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
177 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
178 `contact`.`name`, `contact`.`url`, `contact`.`photo`
179 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
180 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
184 $intro_count = count($intros1) + count($intros2);
185 $intros = $intros1 + $intros2;
187 $myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ;
188 $mails = q("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
189 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
190 intval(local_user()),
193 $mail_count = count($mails);
195 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
196 $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total`
197 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
198 WHERE `contact`.`self` = 1");
200 if (dbm::is_result($regs)) {
201 $register_count = $regs[0]['total'];
205 $cachekey = "ping_init:".local_user();
206 $ev = Cache::get($cachekey);
208 $ev = q("SELECT type, start, adjust FROM `event`
209 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
210 ORDER BY `start` ASC ",
211 intval(local_user()),
212 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
213 dbesc(datetime_convert('UTC', 'UTC', 'now'))
215 if (dbm::is_result($ev)) {
216 Cache::set($cachekey, $ev, CACHE_HOUR);
220 if (dbm::is_result($ev)) {
221 $all_events = count($ev);
224 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
227 if ($x['type'] === 'birthday') {
234 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
235 $all_events_today ++;
245 $data['intro'] = $intro_count;
246 $data['mail'] = $mail_count;
247 $data['net'] = $network_count;
248 $data['home'] = $home_count;
249 $data['register'] = $register_count;
251 $data['all-events'] = $all_events;
252 $data['all-events-today'] = $all_events_today;
253 $data['events'] = $events;
254 $data['events-today'] = $events_today;
255 $data['birthdays'] = $birthdays;
256 $data['birthdays-today'] = $birthdays_today;
258 if (dbm::is_result($notifs)) {
259 foreach ($notifs as $notif) {
260 if ($notif['seen'] == 0) {
266 // merge all notification types in one array
267 if (dbm::is_result($intros)) {
268 foreach ($intros as $intro) {
270 'href' => System::baseUrl() . '/notifications/intros/' . $intro['id'],
271 'name' => $intro['name'],
272 'url' => $intro['url'],
273 'photo' => $intro['photo'],
274 'date' => $intro['datetime'],
276 'message' => t('{0} wants to be your friend'),
282 if (dbm::is_result($mails)) {
283 foreach ($mails as $mail) {
285 'href' => System::baseUrl() . '/message/' . $mail['id'],
286 'name' => $mail['from-name'],
287 'url' => $mail['from-url'],
288 'photo' => $mail['from-photo'],
289 'date' => $mail['created'],
291 'message' => t('{0} sent you a message'),
297 if (dbm::is_result($regs)) {
298 foreach ($regs as $reg) {
300 'href' => System::baseUrl() . '/admin/users/',
301 'name' => $reg['name'],
302 'url' => $reg['url'],
303 'photo' => $reg['micro'],
304 'date' => $reg['created'],
306 'message' => t('{0} requested registration'),
312 // sort notifications by $[]['date']
313 $sort_function = function($a, $b) {
314 $adate = strtotime($a['date']);
315 $bdate = strtotime($b['date']);
317 // Unseen messages are kept at the top
318 // The value 31536000 means one year. This should be enough :-)
326 if ($adate == $bdate) {
329 return ($adate < $bdate) ? 1 : -1;
331 usort($notifs, $sort_function);
333 if (dbm::is_result($notifs)) {
334 // Are the nofications called from the regular process or via the friendica app?
335 $regularnotifications = (intval($_GET['uid']) && intval($_GET['_']));
337 foreach ($notifs as $notif) {
338 if ($a->is_friendica_app() || !$regularnotifications) {
339 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
342 $contact = get_contact_details_by_url($notif['url']);
343 if (isset($contact['micro'])) {
344 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
346 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
349 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
351 $notifications[] = array(
352 'id' => $notif['id'],
353 'href' => $notif['href'],
354 'name' => $notif['name'],
355 'url' => $notif['url'],
356 'photo' => $notif['photo'],
357 'date' => relative_date($notif['date']),
358 'message' => $notif['message'],
359 'seen' => $notif['seen'],
360 'timestamp' => strtotime($local_time)
367 $sysmsgs_info = array();
369 if (x($_SESSION, 'sysmsg')) {
370 $sysmsgs = $_SESSION['sysmsg'];
371 unset($_SESSION['sysmsg']);
374 if (x($_SESSION, 'sysmsg_info')) {
375 $sysmsgs_info = $_SESSION['sysmsg_info'];
376 unset($_SESSION['sysmsg_info']);
379 if ($format == 'json') {
380 $data['groups'] = $groups_unseen;
381 $data['forums'] = $forums_unseen;
382 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
383 $data['notifications'] = $notifications;
384 $data['sysmsgs'] = array(
385 'notice' => $sysmsgs,
386 'info' => $sysmsgs_info
389 $json_payload = json_encode(array("result" => $data));
391 if (isset($_GET['callback'])) {
393 header("Content-type: application/javascript");
394 echo $_GET['callback'] . '(' . $json_payload . ')';
396 header("Content-type: application/json");
400 // Legacy slower XML format output
401 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
403 header("Content-type: text/xml");
404 echo xml::from_array(array("result" => $data), $xml);
411 * @brief Retrieves the notifications array for the given user ID
413 * @param int $uid User id
414 * @return array Associative array of notifications
416 function ping_get_notifications($uid)
428 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
429 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
430 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
431 AND NOT (`notify`.`type` IN (%d, %d))
432 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
434 intval(NOTIFY_INTRO),
450 foreach ($r AS $notification) {
451 if (is_null($notification["visible"])) {
452 $notification["visible"] = true;
455 if (is_null($notification["spam"])) {
456 $notification["spam"] = 0;
459 if (is_null($notification["deleted"])) {
460 $notification["deleted"] = 0;
463 if ($notification["msg_cache"]) {
464 $notification["name"] = $notification["name_cache"];
465 $notification["message"] = $notification["msg_cache"];
467 $notification["name"] = strip_tags(bbcode($notification["name"]));
468 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
470 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
471 dbesc($notification["name"]),
472 dbesc($notification["message"]),
473 intval($notification["id"])
477 $notification["href"] = System::baseUrl() . "/notify/view/" . $notification["id"];
479 if ($notification["visible"] && !$notification["spam"] &&
480 !$notification["deleted"] && !is_array($result[$notification["parent"]])) {
481 if (PConfig::get(local_user(), 'system', 'detailled_notif')) {
482 $result[$notification["id"]] = $notification;
484 $result[$notification["parent"]] = $notification;
488 } while ((count($result) < 50) && !$quit);
494 * @brief Backward-compatible XML formatting for ping.php output
497 * @param array $data The initial ping data array
498 * @param int $sysnotify_count Number of unseen system notifications
499 * @param array $notifs Complete list of notification
500 * @param array $sysmsgs List of system notice messages
501 * @param array $sysmsgs_info List of system info messages
502 * @param int $groups_unseen Number of unseen group items
503 * @param int $forums_unseen Number of unseen forum items
504 * @return array XML-transform ready data array
506 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
508 $notifications = array();
509 foreach($notifs as $key => $notif) {
510 $notifications[$key . ':note'] = $notif['message'];
512 $notifications[$key . ':@attributes'] = array(
513 'id' => $notif['id'],
514 'href' => $notif['href'],
515 'name' => $notif['name'],
516 'url' => $notif['url'],
517 'photo' => $notif['photo'],
518 'date' => $notif['date'],
519 'seen' => $notif['seen'],
520 'timestamp' => $notif['timestamp']
525 foreach ($sysmsgs as $key => $m){
526 $sysmsg[$key . ':notice'] = $m;
528 foreach ($sysmsgs_info as $key => $m){
529 $sysmsg[$key . ':info'] = $m;
532 $data['notif'] = $notifications;
533 $data['@attributes'] = array('count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']);
534 $data['sysmsgs'] = $sysmsg;
536 if ($data['register'] == 0) {
537 unset($data['register']);
541 if (count($groups_unseen)) {
542 foreach ($groups_unseen as $key => $item) {
543 $groups[$key . ':group'] = $item['count'];
544 $groups[$key . ':@attributes'] = array('id' => $item['id']);
546 $data['groups'] = $groups;
550 if (count($forums_unseen)) {
551 foreach ($forums_unseen as $key => $item) {
552 $forums[$count . ':forum'] = $item['count'];
553 $forums[$count . ':@attributes'] = array('id' => $item['id']);
555 $data['forums'] = $forums;