2 require_once("include/datetime.php");
3 require_once('include/bbcode.php');
4 require_once('include/ForumManager.php');
5 require_once('include/group.php');
6 require_once('mod/proxy.php');
7 require_once('include/xml.php');
8 require_once('include/cache.php');
11 * @brief Outputs the counts and the lists of various notifications
13 * The output format can be controlled via the GET parameter 'format'. It can be
14 * - xml (deprecated legacy default)
15 * - json (outputs JSONP with the 'callback' GET parameter)
17 * Expected JSON structure:
26 * "all-events-today": 0,
30 * "birthdays-today": 0,
34 * "notifications": [ ],
42 * @param App $a The Friendica App instance
44 function ping_init(App $a)
48 if (isset($_GET['format']) && $_GET['format'] == 'json') {
60 $notifications = array();
68 $groups_unseen = array();
69 $forums_unseen = array();
72 $all_events_today = 0;
79 $data['intro'] = $intro_count;
80 $data['mail'] = $mail_count;
81 $data['net'] = $network_count;
82 $data['home'] = $home_count;
83 $data['register'] = $register_count;
85 $data['all-events'] = $all_events;
86 $data['all-events-today'] = $all_events_today;
87 $data['events'] = $events;
88 $data['events-today'] = $events_today;
89 $data['birthdays'] = $birthdays;
90 $data['birthdays-today'] = $birthdays_today;
93 // Different login session than the page that is calling us.
94 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
96 $data = array('result' => array('invalid' => 1));
98 if ($format == 'json') {
99 if (isset($_GET['callback'])) {
101 header("Content-type: application/javascript");
102 echo $_GET['callback'] . '(' . json_encode($data) . ')';
104 header("Content-type: application/json");
105 echo json_encode($data);
108 header("Content-type: text/xml");
109 echo xml::from_array($data, $xml);
114 $notifs = ping_get_notifications(local_user());
116 $items_unseen = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
117 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
118 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
119 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
120 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
121 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
122 AND `item`.`contact-id` != %d
123 ORDER BY `item`.`created` DESC",
124 intval(local_user()), intval(local_user())
127 if (dbm::is_result($items_unseen)) {
128 $arr = array('items' => $items_unseen);
129 call_hooks('network_ping', $arr);
131 foreach ($items_unseen as $item) {
140 if ($network_count) {
141 if (intval(feature_enabled(local_user(), 'groups'))) {
142 // Find out how unseen network posts are spread across groups
143 $group_counts = groups_count_unseen();
144 if (dbm::is_result($group_counts)) {
145 foreach ($group_counts as $group_count) {
146 if ($group_count['count'] > 0) {
147 $groups_unseen[] = $group_count;
153 if (intval(feature_enabled(local_user(), 'forumlist_widget'))) {
154 $forum_counts = ForumManager::count_unseen_items();
155 if (dbm::is_result($forums_counts)) {
156 foreach ($forums_counts as $forum_count) {
157 if ($forum_count['count'] > 0) {
158 $forums_unseen[] = $forum_count;
165 $intros1 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
166 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
167 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
168 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
171 $intros2 = qu("SELECT `intro`.`id`, `intro`.`datetime`,
172 `contact`.`name`, `contact`.`url`, `contact`.`photo`
173 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
174 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
178 $intro_count = count($intros1) + count($intros2);
179 $intros = $intros1 + $intros2;
181 $myurl = App::get_baseurl() . '/profile/' . $a->user['nickname'] ;
182 $mails = qu("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
183 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
184 intval(local_user()),
187 $mail_count = count($mails);
189 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
190 $regs = qu("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total`
191 FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
192 WHERE `contact`.`self` = 1");
194 if (dbm::is_result($regs)) {
195 $register_count = $regs[0]['total'];
199 $cachekey = "ping_init:".local_user();
200 $ev = Cache::get($cachekey);
202 $ev = qu("SELECT type, start, adjust FROM `event`
203 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
204 ORDER BY `start` ASC ",
205 intval(local_user()),
206 dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')),
207 dbesc(datetime_convert('UTC', 'UTC', 'now'))
209 if (dbm::is_result($ev)) {
210 Cache::set($cachekey, $ev, CACHE_HOUR);
214 if (dbm::is_result($ev)) {
215 $all_events = count($ev);
218 $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
221 if ($x['type'] === 'birthday') {
228 if (datetime_convert('UTC', ((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
229 $all_events_today ++;
239 $data['intro'] = $intro_count;
240 $data['mail'] = $mail_count;
241 $data['net'] = $network_count;
242 $data['home'] = $home_count;
243 $data['register'] = $register_count;
245 $data['all-events'] = $all_events;
246 $data['all-events-today'] = $all_events_today;
247 $data['events'] = $events;
248 $data['events-today'] = $events_today;
249 $data['birthdays'] = $birthdays;
250 $data['birthdays-today'] = $birthdays_today;
252 if (dbm::is_result($notifs)) {
253 foreach ($notifs as $notif) {
254 if ($notif['seen'] == 0) {
260 // merge all notification types in one array
261 if (dbm::is_result($intros)) {
262 foreach ($intros as $intro) {
264 'href' => App::get_baseurl() . '/notifications/intros/' . $intro['id'],
265 'name' => $intro['name'],
266 'url' => $intro['url'],
267 'photo' => $intro['photo'],
268 'date' => $intro['datetime'],
270 'message' => t('{0} wants to be your friend'),
276 if (dbm::is_result($mails)) {
277 foreach ($mails as $mail) {
279 'href' => App::get_baseurl() . '/message/' . $mail['id'],
280 'name' => $mail['from-name'],
281 'url' => $mail['from-url'],
282 'photo' => $mail['from-photo'],
283 'date' => $mail['created'],
285 'message' => t('{0} sent you a message'),
291 if (dbm::is_result($regs)) {
292 foreach ($regs as $reg) {
294 'href' => App::get_baseurl() . '/admin/users/',
295 'name' => $reg['name'],
296 'url' => $reg['url'],
297 'photo' => $reg['micro'],
298 'date' => $reg['created'],
300 'message' => t('{0} requested registration'),
306 // sort notifications by $[]['date']
307 $sort_function = function($a, $b) {
308 $adate = strtotime($a['date']);
309 $bdate = strtotime($b['date']);
311 // Unseen messages are kept at the top
312 // The value 31536000 means one year. This should be enough :-)
320 if ($adate == $bdate) {
323 return ($adate < $bdate) ? 1 : -1;
325 usort($notifs, $sort_function);
327 if (dbm::is_result($notifs)) {
328 // Are the nofications called from the regular process or via the friendica app?
329 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
331 foreach ($notifs as $notif) {
332 if ($a->is_friendica_app() OR !$regularnotifications) {
333 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
336 $contact = get_contact_details_by_url($notif['url']);
337 if (isset($contact['micro'])) {
338 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
340 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
343 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
345 $notifications[] = array(
346 'id' => $notif['id'],
347 'href' => $notif['href'],
348 'name' => $notif['name'],
349 'url' => $notif['url'],
350 'photo' => $notif['photo'],
351 'date' => relative_date($notif['date']),
352 'message' => $notif['message'],
353 'seen' => $notif['seen'],
354 'timestamp' => strtotime($local_time)
361 $sysmsgs_info = array();
363 if (x($_SESSION, 'sysmsg')) {
364 $sysmsgs = $_SESSION['sysmsg'];
365 unset($_SESSION['sysmsg']);
368 if (x($_SESSION, 'sysmsg_info')) {
369 $sysmsgs_info = $_SESSION['sysmsg_info'];
370 unset($_SESSION['sysmsg_info']);
373 if ($format == 'json') {
374 $data['groups'] = $groups_unseen;
375 $data['forums'] = $forums_unseen;
376 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
377 $data['notifications'] = $notifications;
378 $data['sysmsgs'] = array(
379 'notice' => $sysmsgs,
380 'info' => $sysmsgs_info
383 $json_payload = json_encode(array("result" => $data));
385 if (isset($_GET['callback'])) {
387 header("Content-type: application/javascript");
388 echo $_GET['callback'] . '(' . $json_payload . ')';
390 header("Content-type: application/json");
394 // Legacy slower XML format output
395 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
397 header("Content-type: text/xml");
398 echo xml::from_array(array("result" => $data), $xml);
405 * @brief Retrieves the notifications array for the given user ID
407 * @param int $uid User id
408 * @return array Associative array of notifications
410 function ping_get_notifications($uid)
422 $r = qu("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
423 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
424 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
425 AND NOT (`notify`.`type` IN (%d, %d))
426 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
428 intval(NOTIFY_INTRO),
433 if (!$r AND !$seen) {
444 foreach ($r AS $notification) {
445 if (is_null($notification["visible"])) {
446 $notification["visible"] = true;
449 if (is_null($notification["spam"])) {
450 $notification["spam"] = 0;
453 if (is_null($notification["deleted"])) {
454 $notification["deleted"] = 0;
457 if ($notification["msg_cache"]) {
458 $notification["name"] = $notification["name_cache"];
459 $notification["message"] = $notification["msg_cache"];
461 $notification["name"] = strip_tags(bbcode($notification["name"]));
462 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
464 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
465 dbesc($notification["name"]),
466 dbesc($notification["message"]),
467 intval($notification["id"])
471 $notification["href"] = App::get_baseurl() . "/notify/view/" . $notification["id"];
473 if ($notification["visible"] AND !$notification["spam"] AND
474 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
475 $result[$notification["parent"]] = $notification;
478 } while ((count($result) < 50) AND !$quit);
484 * @brief Backward-compatible XML formatting for ping.php output
487 * @param array $data The initial ping data array
488 * @param int $sysnotify_count Number of unseen system notifications
489 * @param array $notifs Complete list of notification
490 * @param array $sysmsgs List of system notice messages
491 * @param array $sysmsgs_info List of system info messages
492 * @param int $groups_unseen Number of unseen group items
493 * @param int $forums_unseen Number of unseen forum items
494 * @return array XML-transform ready data array
496 function ping_format_xml_data($data, $sysnotify, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen)
498 $notifications = array();
499 foreach($notifs as $key => $notif) {
500 $notifications[$key . ':note'] = $notif['message'];
502 $notifications[$key . ':@attributes'] = array(
503 'id' => $notif['id'],
504 'href' => $notif['href'],
505 'name' => $notif['name'],
506 'url' => $notif['url'],
507 'photo' => $notif['photo'],
508 'date' => $notif['date'],
509 'seen' => $notif['seen'],
510 'timestamp' => $notif['timestamp']
515 foreach ($sysmsgs as $key => $m){
516 $sysmsg[$key . ':notice'] = $m;
518 foreach ($sysmsgs_info as $key => $m){
519 $sysmsg[$key . ':info'] = $m;
522 $data['notif'] = $notifications;
523 $data['@attributes'] = array('count' => $sysnotify_count + $data['intro'] + $data['mail'] + $data['register']);
524 $data['sysmsgs'] = $sysmsg;
526 if ($data['register'] == 0) {
527 unset($data['register']);
531 if (count($groups_unseen)) {
532 foreach ($groups_unseen as $key => $item) {
533 $groups[$key . ':group'] = $item['count'];
534 $groups[$key . ':@attributes'] = array('id' => $item['id']);
536 $data['groups'] = $groups;
540 if (count($forums_unseen)) {
541 foreach ($forums_unseen as $key => $item) {
542 $forums[$count . ':forum'] = $item['count'];
543 $forums[$count . ':@attributes'] = array('id' => $item['id']);
545 $data['forums'] = $forums;