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');
9 function ping_init(&$a) {
13 if (isset($_GET['format']) && $_GET['format'] == 'json') {
18 // Different login session than the page that is calling us.
19 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
21 $data = array('result' => array('invalid' => 1));
23 if ($format == 'json') {
24 if (isset($_GET['callback'])) {
26 header("Content-type: application/javascript");
27 echo $_GET['callback'] . '(' . json_encode($data) . ')';
29 header("Content-type: application/json");
30 echo json_encode($data);
33 header("Content-type: text/xml");
34 echo xml::from_array($data, $xml);
39 $notifs = ping_get_notifications(local_user());
40 $sysnotify_count = 0; // we will update this in a moment
54 $groups_unseen = array();
55 $forums_unseen = array();
57 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
58 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
59 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
60 FROM `item` INNER JOIN `item` as `pitem` ON `pitem`.`id`=`item`.`parent`
61 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
62 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
63 AND `item`.`contact-id` != %d
64 ORDER BY `item`.`created` DESC",
65 intval(local_user()), intval(local_user())
68 if (dbm::is_result($r)) {
70 $arr = array('items' => $r);
71 call_hooks('network_ping', $arr);
83 if (intval(feature_enabled(local_user(),'groups'))) {
84 // Find out how unseen network posts are spread across groups
85 $group_counts = groups_count_unseen();
86 if (dbm::is_result($group_counts)) {
87 foreach ($group_counts as $group_count) {
88 if ($group_count['count'] > 0) {
89 $groups_unseen[] = $group_count;
95 if (intval(feature_enabled(local_user(),'forumlist_widget'))) {
96 $forum_counts = ForumManager::count_unseen_items();
97 if (dbm::is_result($forums_counts)) {
98 foreach ($forums_counts as $forum_count) {
99 if ($forum_count['count'] > 0) {
100 $forums_unseen[] = $forum_count;
107 $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
108 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
109 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
110 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
113 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
114 `contact`.`name`, `contact`.`url`, `contact`.`photo`
115 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
116 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
120 $intro_count = count($intros1) + count($intros2);
121 $intros = $intros1+$intros2;
123 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
124 $mails = q("SELECT * FROM `mail`
125 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
126 intval(local_user()),
129 $mail_count = count($mails);
131 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
132 $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
134 $register_count = $regs[0]['total'];
139 $all_events_today = 0;
143 $birthdays_today = 0;
145 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
146 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
147 ORDER BY `start` ASC ",
148 intval(local_user()),
149 dbesc(datetime_convert('UTC','UTC','now + 7 days')),
150 dbesc(datetime_convert('UTC','UTC','now'))
153 if (dbm::is_result($ev)) {
154 $all_events = intval($ev[0]['total']);
157 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
160 if ($x['type'] === 'birthday') {
167 if (datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
168 $all_events_today ++;
179 $data['intro'] = $intro_count;
180 $data['mail'] = $mail_count;
181 $data['net'] = $network_count;
182 $data['home'] = $home_count;
183 $data['register'] = $register_count;
185 $data['all-events'] = $all_events;
186 $data['all-events-today'] = $all_events_today;
187 $data['events'] = $events;
188 $data['events-today'] = $events_today;
189 $data['birthdays'] = $birthdays;
190 $data['birthdays-today'] = $birthdays_today;
192 if (dbm::is_result($notifs)) {
193 foreach ($notifs as $notif) {
194 if ($notif['seen'] == 0) {
200 // merge all notification types in one array
201 if (dbm::is_result($intros)) {
202 foreach ($intros as $intro) {
204 'href' => $a->get_baseurl() . '/notifications/intros/' . $intro['id'],
205 'name' => $intro['name'],
206 'url' => $intro['url'],
207 'photo' => $intro['photo'],
208 'date' => $intro['datetime'],
210 'message' => t('{0} wants to be your friend'),
216 if (dbm::is_result($mails)) {
217 foreach ($mails as $mail) {
219 'href' => $a->get_baseurl() . '/message/' . $mail['id'],
220 'name' => $mail['from-name'],
221 'url' => $mail['from-url'],
222 'photo' => $mail['from-photo'],
223 'date' => $mail['created'],
225 'message' => t('{0} sent you a message'),
231 if (dbm::is_result($regs)) {
232 foreach ($regs as $reg) {
234 'href' => $a->get_baseurl() . '/admin/users/',
235 'name' => $reg['name'],
236 'url' => $reg['url'],
237 'photo' => $reg['micro'],
238 'date' => $reg['created'],
240 'message' => t('{0} requested registration'),
246 // sort notifications by $[]['date']
247 $sort_function = function($a, $b) {
248 $adate = date($a['date']);
249 $bdate = date($b['date']);
250 if ($adate == $bdate) {
253 return ($adate < $bdate) ? 1 : -1;
255 usort($notifs, $sort_function);
257 if (dbm::is_result($notifs)) {
258 // Are the nofications called from the regular process or via the friendica app?
259 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
261 foreach ($notifs as $notif) {
262 if ($a->is_friendica_app() OR !$regularnotifications) {
263 $notif['message'] = str_replace("{0}", $notif['name'], $notif['message']);
266 $contact = get_contact_details_by_url($notif['url']);
267 if (isset($contact['micro'])) {
268 $notif['photo'] = proxy_url($contact['micro'], false, PROXY_SIZE_MICRO);
270 $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO);
273 $local_time = datetime_convert('UTC', date_default_timezone_get(), $notif['date']);
275 $notifications[] = array(
276 'id' => $notif['id'],
277 'href' => $notif['href'],
278 'name' => $notif['name'],
279 'url' => $notif['url'],
280 'photo' => $notif['photo'],
281 'date' => relative_date($notif['date']),
282 'message' => $notif['message'],
283 'seen' => $notif['seen'],
284 'timestamp' => strtotime($local_time)
291 $sysmsgs_info = array();
293 if (x($_SESSION,'sysmsg')) {
294 $sysmsgs = $_SESSION['sysmsg'];
295 unset($_SESSION['sysmsg']);
298 if (x($_SESSION,'sysmsg_info')) {
299 $sysmsgs_info = $_SESSION['sysmsg_info'];
300 unset($_SESSION['sysmsg_info']);
303 if ($format == 'json') {
304 $data['groups'] = $groups_unseen;
305 $data['forums'] = $forums_unseen;
306 $data['notify'] = $sysnotify_count + $intro_count + $mail_count + $register_count;
307 $data['notifications'] = $notifications;
308 $data['sysmsgs'] = array(
309 'notice' => $sysmsgs,
310 'info' => $sysmsgs_info
313 $json_payload = json_encode(array("result" => $data));
315 if (isset($_GET['callback'])) {
317 header("Content-type: application/javascript");
318 echo $_GET['callback'] . '(' . $json_payload . ')';
320 header("Content-type: application/json");
324 // Legacy slower XML format output
325 $data = ping_format_xml_data($data, $sysnotify_count, $notifications, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen);
327 header("Content-type: text/xml");
328 echo xml::from_array(array("result" => $data), $xml);
335 * @brief Retrieves the notifications array for the given user ID
337 * @param int $uid User id
338 * @return array Associative array of notifications
340 function ping_get_notifications($uid) {
352 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
353 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
354 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
355 AND NOT (`notify`.`type` IN (%d, %d))
356 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
358 intval(NOTIFY_INTRO),
363 if (!$r AND !$seen) {
374 foreach ($r AS $notification) {
375 if (is_null($notification["visible"])) {
376 $notification["visible"] = true;
379 if (is_null($notification["spam"])) {
380 $notification["spam"] = 0;
383 if (is_null($notification["deleted"])) {
384 $notification["deleted"] = 0;
387 if ($notification["msg_cache"]) {
388 $notification["name"] = $notification["name_cache"];
389 $notification["message"] = $notification["msg_cache"];
391 $notification["name"] = strip_tags(bbcode($notification["name"]));
392 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
394 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
395 dbesc($notification["name"]),
396 dbesc($notification["message"]),
397 intval($notification["id"])
401 $notification["href"] = $a->get_baseurl() . "/notify/view/" . $notification["id"];
403 if ($notification["visible"] AND !$notification["spam"] AND
404 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
405 $result[$notification["parent"]] = $notification;
408 } while ((count($result) < 50) AND !$quit);
414 * @brief Backward-compatible XML formatting for ping.php output
417 * @param array $data The initial ping data array
418 * @param int $sysnotify_count Number of unseen system notifications
419 * @param array $notifs Complete list of notification
420 * @param array $sysmsgs List of system notice messages
421 * @param array $sysmsgs_info List of system info messages
422 * @return array XML-transform ready data array
424 function ping_format_xml_data($data, $sysnotify_count, $notifs, $sysmsgs, $sysmsgs_info, $groups_unseen, $forums_unseen) {
425 $notifications = array();
426 foreach($notifs as $key => $n) {
427 $notifications[$key . ":note"] = $n['message'];
429 $notifications[$key . ":@attributes"] = array(
431 "href" => $n['href'],
432 "name" => $n['name'],
434 "photo" => $n['photo'],
435 "date" => $n['date'],
436 "seen" => $n['seen'],
437 "timestamp" => $n['timestamp']
442 foreach ($sysmsgs as $key => $m){
443 $sysmsg[$key . ":notice"] = $m;
445 foreach ($sysmsgs_info as $key => $m){
446 $sysmsg[$key . ":info"] = $m;
449 $data["notif"] = $notifications;
450 $data["@attributes"] = array("count" => $sysnotify_count + $data["intro"] + $data["mail"] + $data["register"]);
451 $data["sysmsgs"] = $sysmsg;
453 if ($data["register"] == 0) {
454 unset($data["register"]);
458 if (count($groups_unseen)) {
459 foreach ($groups_unseen as $key => $item) {
460 $groups[$key . ':group'] = $item['count'];
461 $groups[$key . ':@attributes'] = array('id' => $item['id']);
463 $data['groups'] = $groups;
467 if (count($forums_unseen)) {
468 foreach ($forums_unseen as $key => $item) {
469 $forums[$count . ':forum'] = $item['count'];
470 $forums[$count . ':@attributes'] = array('id' => $item['id']);
472 $data['forums'] = $forums;