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");
8 function ping_init(&$a) {
10 header("Content-type: text/xml");
12 echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
15 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
19 // Different login session than the page that is calling us.
21 if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
22 echo '<invalid>1</invalid></result>';
26 $notifs = ping_get_notifications(local_user());
27 $sysnotify = 0; // we will update this in a moment
40 $groups_unseen = array();
41 $forums_unseen = array();
43 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
44 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
45 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
46 FROM `item` INNER JOIN `item` as `pitem` ON `pitem`.`id`=`item`.`parent`
47 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
48 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
49 AND `item`.`contact-id` != %d
50 ORDER BY `item`.`created` DESC",
51 intval(local_user()), intval(local_user())
54 if(dba::is_result($r)) {
56 $arr = array('items' => $r);
57 call_hooks('network_ping', $arr);
68 $obj = parse_xml_string($xmlhead.$it['object']);
69 $it['tname'] = $obj->content;
75 case ACTIVITY_DISLIKE:
79 $obj = parse_xml_string($xmlhead.$it['object']);
80 $it['fname'] = $obj->title;
84 if ($it['parent']!=$it['id']) {
95 if(intval(feature_enabled(local_user(),'groups'))) {
96 // Find out how unseen network posts are spread across groups
97 $groups_unseen = groups_count_unseen();
100 if(intval(feature_enabled(local_user(),'forumlist_widget'))) {
101 $forums_unseen = ForumManager::count_unseen_items();
105 $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
106 `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
107 FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
108 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
111 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
112 `contact`.`name`, `contact`.`url`, `contact`.`photo`
113 FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
114 WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
118 $intro = count($intros1) + count($intros2);
119 $intros = $intros1+$intros2;
121 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
122 $mails = q("SELECT * FROM `mail`
123 WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
124 intval(local_user()),
127 $mail = count($mails);
129 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
130 $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");
132 $register = $regs[0]['total'];
138 $all_events_today = 0;
142 $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(dba::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 ++;
180 * return xml from notification array
182 * @param array $n Notification array:
183 * 'href' => notification link
184 * 'name' => subject name
185 * 'url' => subject url
186 * 'photo' => subject photo
187 * 'date' => notification date
188 * 'seen' => bool true/false
189 * 'message' => notification message. "{0}" will be replaced by subject name
192 $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
194 $n['message'] = html_entity_decode($n['message'], ENT_COMPAT | ENT_HTML401, "UTF-8");
195 $n['name'] = html_entity_decode($n['name'], ENT_COMPAT | ENT_HTML401, "UTF-8");
197 // Are the nofications calles from the regular process or via the friendica app?
198 $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
202 if ($a->is_friendica_app() OR !$regularnotifications)
203 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
205 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
207 call_hooks('ping_xmlize', $n);
208 $notsxml = '<note id="%d" href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" timestamp="%s" >%s</note>'."\n";
209 return sprintf ( $notsxml, intval($n['id']),
210 xmlify($n['href']), xmlify(xmlify($n['name'])), xmlify($n['url']), xmlify($n['photo']),
211 xmlify(relative_date($n['date'])), xmlify($n['seen']), xmlify(strtotime($local_time)),
212 xmlify($n['message'])
216 echo "<intro>$intro</intro>
219 <home>$home</home>\r\n";
220 if ($register!=0) echo "<register>$register</register>";
222 if ( dba::is_result($groups_unseen) ) {
224 foreach ($groups_unseen as $it)
225 if ($it['count'] > 0)
226 echo '<group id="'.$it['id'].'">'.$it['count']."</group>";
231 if ( dba::is_result($forums_unseen) ) {
233 foreach ($forums_unseen as $it)
234 if ($it['count'] > 0)
235 echo '<forum id="'.$it['id'].'">'.$it['count']."</forum>";
240 echo "<all-events>$all_events</all-events>
241 <all-events-today>$all_events_today</all-events-today>
242 <events>$events</events>
243 <events-today>$events_today</events-today>
244 <birthdays>$birthdays</birthdays>
245 <birthdays-today>$birthdays_today</birthdays-today>\r\n";
248 if (dba::is_result($notifs) && (! $sysnotify)) {
249 foreach ($notifs as $zz) {
255 echo ' <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
257 // merge all notification types in one array
258 if ( dba::is_result($intros) ) {
259 foreach ($intros as $i) {
261 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
262 'name' => $i['name'],
264 'photo' => $i['photo'],
265 'date' => $i['datetime'],
267 'message' => t("{0} wants to be your friend"),
273 if ( dba::is_result($mails) ) {
274 foreach ($mails as $i) {
276 'href' => $a->get_baseurl().'/message/'.$i['id'],
277 'name' => $i['from-name'],
278 'url' => $i['from-url'],
279 'photo' => $i['from-photo'],
280 'date' => $i['created'],
282 'message' => t("{0} sent you a message"),
288 if ( dba::is_result($regs) ) {
289 foreach ($regs as $i) {
291 'href' => $a->get_baseurl().'/admin/users/',
292 'name' => $i['name'],
294 'photo' => $i['micro'],
295 'date' => $i['created'],
297 'message' => t("{0} requested registration"),
303 // sort notifications by $[]['date']
304 $sort_function = function($a, $b) {
305 $adate = date($a['date']);
306 $bdate = date($b['date']);
307 if ($adate == $bdate) {
310 return ($adate < $bdate) ? 1 : -1;
312 usort($notifs, $sort_function);
314 if( dba::is_result($notifs) ) {
315 foreach($notifs as $n) {
325 if(x($_SESSION,'sysmsg')){
326 foreach ($_SESSION['sysmsg'] as $m){
327 echo "<notice>".xmlify($m)."</notice>";
329 unset($_SESSION['sysmsg']);
331 if(x($_SESSION,'sysmsg_info')){
332 foreach ($_SESSION['sysmsg_info'] as $m){
333 echo "<info>".xmlify($m)."</info>";
335 unset($_SESSION['sysmsg_info']);
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);