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) {
11 $xmlhead = "<"."?xml version='1.0' encoding='UTF-8' ?".">";
14 // Different login session than the page that is calling us.
15 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
16 $data = array("invalid" => 1);
17 header("Content-type: text/xml");
18 echo xml::from_array(array("result" => $data), $xml);
22 $notifs = ping_get_notifications(local_user());
23 $sysnotify = 0; // we will update this in a moment
36 $groups_unseen = array();
37 $forums_unseen = array();
39 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
40 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
41 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
42 FROM `item` INNER JOIN `item` as `pitem` ON `pitem`.`id`=`item`.`parent`
43 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
44 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
45 AND `item`.`contact-id` != %d
46 ORDER BY `item`.`created` DESC",
47 intval(local_user()), intval(local_user())
50 if (dbm::is_result($r)) {
52 $arr = array('items' => $r);
53 call_hooks('network_ping', $arr);
64 $obj = parse_xml_string($xmlhead.$it['object']);
65 $it['tname'] = $obj->content;
71 case ACTIVITY_DISLIKE:
75 $obj = parse_xml_string($xmlhead.$it['object']);
76 $it['fname'] = $obj->title;
80 if ($it['parent']!=$it['id']) {
91 if (intval(feature_enabled(local_user(),'groups'))) {
92 // Find out how unseen network posts are spread across groups
93 $groups_unseen = groups_count_unseen();
96 if (intval(feature_enabled(local_user(),'forumlist_widget'))) {
97 $forums_unseen = ForumManager::count_unseen_items();
101 $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
102 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
103 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
104 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
107 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
108 `contact`.`name`, `contact`.`url`, `contact`.`photo`
109 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
110 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
114 $intro = count($intros1) + count($intros2);
115 $intros = $intros1+$intros2;
117 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
118 $mails = q("SELECT * FROM `mail`
119 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
120 intval(local_user()),
123 $mail = count($mails);
125 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
126 $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");
128 $register = $regs[0]['total'];
134 $all_events_today = 0;
138 $birthdays_today = 0;
141 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
142 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
143 ORDER BY `start` ASC ",
144 intval(local_user()),
145 dbesc(datetime_convert('UTC','UTC','now + 7 days')),
146 dbesc(datetime_convert('UTC','UTC','now'))
149 if (dbm::is_result($ev)) {
150 $all_events = intval($ev[0]['total']);
153 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
156 if ($x['type'] === 'birthday') {
163 if (datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
164 $all_events_today ++;
175 $data["intro"] = $intro;
176 $data["mail"] = $mail;
177 $data["net"] = $network;
178 $data["home"] = $home;
181 $data["register"] = $register;
185 if (dbm::is_result($groups_unseen)) {
187 foreach ($groups_unseen as $it)
188 if ($it['count'] > 0) {
190 $groups[$count.":group"] = $it['count'];
191 $groups[$count.":@attributes"] = array("id" => $it['id']);
193 $data["groups"] = $groups;
198 if (dbm::is_result($forums_unseen)) {
200 foreach ($forums_unseen as $it)
201 if ($it['count'] > 0) {
203 $forums[$count.":forum"] = $it['count'];
204 $forums[$count.":@attributes"] = array("id" => $it['id']);
206 $data["forums"] = $forums;
209 $data["all-events"] = $all_events;
210 $data["all-events-today"] = $all_events_today;
211 $data["events"] = $events;
212 $data["events-today"] = $events_today;
213 $data["birthdays"] = $birthdays;
214 $data["birthdays-today"] = $birthdays_today;
217 if (dbm::is_result($notifs) && !$sysnotify) {
218 foreach ($notifs as $zz) {
219 if ($zz['seen'] == 0)
224 // merge all notification types in one array
225 if (dbm::is_result($intros)) {
226 foreach ($intros as $i) {
228 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
229 'name' => $i['name'],
231 'photo' => $i['photo'],
232 'date' => $i['datetime'],
234 'message' => t("{0} wants to be your friend"),
240 if (dbm::is_result($mails)) {
241 foreach ($mails as $i) {
243 'href' => $a->get_baseurl().'/message/'.$i['id'],
244 'name' => $i['from-name'],
245 'url' => $i['from-url'],
246 'photo' => $i['from-photo'],
247 'date' => $i['created'],
249 'message' => t("{0} sent you a message"),
255 if (dbm::is_result($regs)) {
256 foreach ($regs as $i) {
258 'href' => $a->get_baseurl().'/admin/users/',
259 'name' => $i['name'],
261 'photo' => $i['micro'],
262 'date' => $i['created'],
264 'message' => t("{0} requested registration"),
270 // sort notifications by $[]['date']
271 $sort_function = function($a, $b) {
272 $adate = date($a['date']);
273 $bdate = date($b['date']);
274 if ($adate == $bdate) {
277 return ($adate < $bdate) ? 1 : -1;
279 usort($notifs, $sort_function);
281 if (dbm::is_result($notifs)) {
283 // Are the nofications calles from the regular process or via the friendica app?
284 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
287 foreach($notifs as $n) {
289 if ($a->is_friendica_app() OR !$regularnotifications)
290 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
292 $notifications[$count.":note"] = $n['message'];
294 $contact = get_contact_details_by_url($n['url']);
295 if (isset($contact["micro"]))
296 $n['photo'] = proxy_url($contact["micro"], false, PROXY_SIZE_MICRO);
298 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
300 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
302 call_hooks('ping_xmlize', $n);
304 $notifications[$count.":@attributes"] = array("id" => $n["id"],
305 "href" => $n['href'],
306 "name" => $n['name'],
308 "photo" => $n['photo'],
309 "date" => relative_date($n['date']),
310 "seen" => $n['seen'],
311 "timestamp" => strtotime($local_time));
316 $data["notif"] = $notifications;
317 $data["@attributes"] = array("count" => $sysnotify + $intro + $mail + $register);
322 if (x($_SESSION,'sysmsg')){
324 foreach ($_SESSION['sysmsg'] as $m){
326 $sysmsg[$count.":notice"] = $m;
328 unset($_SESSION['sysmsg']);
331 if (x($_SESSION,'sysmsg_info')){
333 foreach ($_SESSION['sysmsg_info'] as $m){
335 $sysmsg[$count.":info"] = $m;
337 unset($_SESSION['sysmsg_info']);
340 $data["sysmsgs"] = $sysmsg;
342 header("Content-type: text/xml");
343 echo xml::from_array(array("result" => $data), $xml);
348 * @brief Retrieves the notifications array for the given user ID
350 * @param int $uid User id
351 * @return array Associative array of notifications
353 function ping_get_notifications($uid) {
365 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
366 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
367 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
368 AND NOT (`notify`.`type` IN (%d, %d))
369 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
371 intval(NOTIFY_INTRO),
376 if (!$r AND !$seen) {
387 foreach ($r AS $notification) {
388 if (is_null($notification["visible"])) {
389 $notification["visible"] = true;
392 if (is_null($notification["spam"])) {
393 $notification["spam"] = 0;
396 if (is_null($notification["deleted"])) {
397 $notification["deleted"] = 0;
400 if ($notification["msg_cache"]) {
401 $notification["name"] = $notification["name_cache"];
402 $notification["message"] = $notification["msg_cache"];
404 $notification["name"] = strip_tags(bbcode($notification["name"]));
405 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
407 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
408 dbesc($notification["name"]),
409 dbesc($notification["message"]),
410 intval($notification["id"])
414 $notification["href"] = $a->get_baseurl() . "/notify/view/" . $notification["id"];
416 if ($notification["visible"] AND !$notification["spam"] AND
417 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
418 $result[$notification["parent"]] = $notification;
421 } while ((count($result) < 50) AND !$quit);