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) {
12 // Different login session than the page that is calling us.
13 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
14 $data = array("invalid" => 1);
15 header("Content-type: text/xml");
16 echo xml::from_array(array("result" => $data));
20 $notifs = ping_get_notifications(local_user());
21 $sysnotify = 0; // we will update this in a moment
34 $groups_unseen = array();
35 $forums_unseen = array();
37 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
38 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
39 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
40 FROM `item` INNER JOIN `item` as `pitem` ON `pitem`.`id`=`item`.`parent`
41 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
42 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
43 AND `item`.`contact-id` != %d
44 ORDER BY `item`.`created` DESC",
45 intval(local_user()), intval(local_user())
48 if (dbm::is_result($r)) {
50 $arr = array('items' => $r);
51 call_hooks('network_ping', $arr);
62 $obj = parse_xml_string($xmlhead.$it['object']);
63 $it['tname'] = $obj->content;
69 case ACTIVITY_DISLIKE:
73 $obj = parse_xml_string($xmlhead.$it['object']);
74 $it['fname'] = $obj->title;
78 if ($it['parent']!=$it['id']) {
89 if (intval(feature_enabled(local_user(),'groups'))) {
90 // Find out how unseen network posts are spread across groups
91 $groups_unseen = groups_count_unseen();
94 if (intval(feature_enabled(local_user(),'forumlist_widget'))) {
95 $forums_unseen = ForumManager::count_unseen_items();
99 $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
100 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
101 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
102 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
105 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
106 `contact`.`name`, `contact`.`url`, `contact`.`photo`
107 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
108 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
112 $intro = count($intros1) + count($intros2);
113 $intros = $intros1+$intros2;
115 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
116 $mails = q("SELECT * FROM `mail`
117 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
118 intval(local_user()),
121 $mail = count($mails);
123 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
124 $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");
126 $register = $regs[0]['total'];
132 $all_events_today = 0;
136 $birthdays_today = 0;
139 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
140 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
141 ORDER BY `start` ASC ",
142 intval(local_user()),
143 dbesc(datetime_convert('UTC','UTC','now + 7 days')),
144 dbesc(datetime_convert('UTC','UTC','now'))
147 if (dbm::is_result($ev)) {
148 $all_events = intval($ev[0]['total']);
151 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
154 if ($x['type'] === 'birthday') {
161 if (datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
162 $all_events_today ++;
173 $data["intro"] = $intro;
174 $data["mail"] = $mail;
175 $data["net"] = $network;
176 $data["home"] = $home;
179 $data["register"] = $register;
183 if (dbm::is_result($groups_unseen)) {
185 foreach ($groups_unseen as $it)
186 if ($it['count'] > 0) {
188 $groups[$count.":group"] = $it['count'];
189 $groups[$count.":@attributes"] = array("id" => $it['id']);
191 $data["groups"] = $groups;
196 if (dbm::is_result($forums_unseen)) {
198 foreach ($forums_unseen as $it)
199 if ($it['count'] > 0) {
201 $forums[$count.":forum"] = $it['count'];
202 $forums[$count.":@attributes"] = array("id" => $it['id']);
204 $data["forums"] = $forums;
207 $data["all-events"] = $all_events;
208 $data["all-events-today"] = $all_events_today;
209 $data["events"] = $events;
210 $data["events-today"] = $events_today;
211 $data["birthdays"] = $birthdays;
212 $data["birthdays-today"] = $birthdays_today;
215 if (dbm::is_result($notifs) && !$sysnotify) {
216 foreach ($notifs as $zz) {
217 if ($zz['seen'] == 0)
222 // merge all notification types in one array
223 if (dbm::is_result($intros)) {
224 foreach ($intros as $i) {
226 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
227 'name' => $i['name'],
229 'photo' => $i['photo'],
230 'date' => $i['datetime'],
232 'message' => t("{0} wants to be your friend"),
238 if (dbm::is_result($mails)) {
239 foreach ($mails as $i) {
241 'href' => $a->get_baseurl().'/message/'.$i['id'],
242 'name' => $i['from-name'],
243 'url' => $i['from-url'],
244 'photo' => $i['from-photo'],
245 'date' => $i['created'],
247 'message' => t("{0} sent you a message"),
253 if (dbm::is_result($regs)) {
254 foreach ($regs as $i) {
256 'href' => $a->get_baseurl().'/admin/users/',
257 'name' => $i['name'],
259 'photo' => $i['micro'],
260 'date' => $i['created'],
262 'message' => t("{0} requested registration"),
268 // sort notifications by $[]['date']
269 $sort_function = function($a, $b) {
270 $adate = date($a['date']);
271 $bdate = date($b['date']);
272 if ($adate == $bdate) {
275 return ($adate < $bdate) ? 1 : -1;
277 usort($notifs, $sort_function);
279 if (dbm::is_result($notifs)) {
281 // Are the nofications calles from the regular process or via the friendica app?
282 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
285 foreach($notifs as $n) {
287 if ($a->is_friendica_app() OR !$regularnotifications)
288 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
290 $notifications[$count.":note"] = $n['message'];
292 $contact = get_contact_details_by_url($n['url']);
293 if (isset($contact["micro"]))
294 $n['photo'] = proxy_url($contact["micro"], false, PROXY_SIZE_MICRO);
296 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
298 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
300 call_hooks('ping_xmlize', $n);
302 $notifications[$count.":@attributes"] = array("id" => $n["id"],
303 "href" => $n['href'],
304 "name" => $n['name'],
306 "photo" => $n['photo'],
307 "date" => relative_date($n['date']),
308 "seen" => $n['seen'],
309 "timestamp" => strtotime($local_time));
314 $data["notif"] = $notifications;
315 $data["@attributes"] = array("count" => $sysnotify + $intro + $mail + $register);
320 if (x($_SESSION,'sysmsg')){
322 foreach ($_SESSION['sysmsg'] as $m){
324 $sysmsg[$count.":notice"] = $m;
326 unset($_SESSION['sysmsg']);
329 if (x($_SESSION,'sysmsg_info')){
331 foreach ($_SESSION['sysmsg_info'] as $m){
333 $sysmsg[$count.":info"] = $m;
335 unset($_SESSION['sysmsg_info']);
338 $data["sysmsgs"] = $sysmsg;
340 header("Content-type: text/xml");
341 echo xml::from_array(array("result" => $data));
345 function ping_get_notifications($uid) {
357 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
358 FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
359 WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
360 AND NOT (`notify`.`type` IN (%d, %d))
361 AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
363 intval(NOTIFY_INTRO),
368 if (!$r AND !$seen) {
379 foreach ($r AS $notification) {
380 if (is_null($notification["visible"]))
381 $notification["visible"] = true;
383 if (is_null($notification["spam"]))
384 $notification["spam"] = 0;
386 if (is_null($notification["deleted"]))
387 $notification["deleted"] = 0;
389 $notification["message"] = strip_tags(bbcode($notification["msg"]));
390 $notification["name"] = strip_tags(bbcode($notification["name"]));
392 // Replace the name with {0} but ensure to make that only once
393 // The {0} is used later and prints the name in bold.
395 if ($notification['name'] != "")
396 $pos = strpos($notification["message"],$notification['name']);
401 $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
403 $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
405 if ($notification["visible"] AND !$notification["spam"] AND
406 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
407 $result[$notification["parent"]] = $notification;
411 } while ((count($result) < 50) AND !$quit);