]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
Merge acl-var into develop
[friendica.git] / mod / ping.php
1 <?php
2 require_once("include/datetime.php");
3 require_once('include/bbcode.php');
4 require_once("mod/proxy.php");
5
6 function ping_init(&$a) {
7
8         header("Content-type: text/xml");
9
10         echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
11                 <result>";
12
13         $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
14
15         if(local_user()){
16
17                 // Different login session than the page that is calling us.
18
19                 if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
20                         echo '<invalid>1</invalid></result>';
21                         killme();
22                 }
23
24                 $notifs = ping_get_notifications(local_user());
25                 $sysnotify = 0; // we will update this in a moment
26
27                 $tags = array();
28                 $comments = array();
29                 $likes = array();
30                 $dislikes = array();
31                 $friends = array();
32                 $posts = array();
33
34                 $home = 0;
35                 $network = 0;
36
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                                 ORDER BY `item`.`created` DESC",
44                         intval(local_user())
45                 );
46
47                 if(count($r)) {
48
49                         $arr = array('items' => $r);
50                         call_hooks('network_ping', $arr);
51
52                         foreach ($r as $it) {
53
54                                 if($it['wall'])
55                                         $home ++;
56                                 else
57                                         $network ++;
58
59                                 switch($it['verb']){
60                                         case ACTIVITY_TAG:
61                                                 $obj = parse_xml_string($xmlhead.$it['object']);
62                                                 $it['tname'] = $obj->content;
63                                                 $tags[] = $it;
64                                                 break;
65                                         case ACTIVITY_LIKE:
66                                                 $likes[] = $it;
67                                                 break;
68                                         case ACTIVITY_DISLIKE:
69                                                 $dislikes[] = $it;
70                                                 break;
71                                         case ACTIVITY_FRIEND:
72                                                 $obj = parse_xml_string($xmlhead.$it['object']);
73                                                 $it['fname'] = $obj->title;
74                                                 $friends[] = $it;
75                                                 break;
76                                         default:
77                                                 if ($it['parent']!=$it['id']) {
78                                                         $comments[] = $it;
79                                                 } else {
80                                                         if(! $it['wall'])
81                                                                 $posts[] = $it;
82                                                 }
83                                 }
84                         }
85                 }
86
87                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,
88                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
89                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
90                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
91                         intval(local_user())
92                 );
93                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
94                         `contact`.`name`, `contact`.`url`, `contact`.`photo`
95                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
96                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
97                         intval(local_user())
98                 );
99
100                 $intro = count($intros1) + count($intros2);
101                 $intros = $intros1+$intros2;
102
103
104
105                 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
106                 $mails = q("SELECT * FROM `mail`
107                         WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
108                         intval(local_user()),
109                         dbesc($myurl)
110                 );
111                 $mail = count($mails);
112
113                 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
114                         $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");
115                         if($regs)
116                                 $register = $regs[0]['total'];
117                 } else {
118                         $register = "0";
119                 }
120
121                 $all_events = 0;
122                 $all_events_today = 0;
123                 $events = 0;
124                 $events_today = 0;
125                 $birthdays = 0;
126                 $birthdays_today = 0;
127
128
129                 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
130                         WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
131                         ORDER BY `start` ASC ",
132                         intval(local_user()),
133                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
134                         dbesc(datetime_convert('UTC','UTC','now'))
135                 );
136
137                 if($ev && count($ev)) {
138                         $all_events = intval($ev[0]['total']);
139
140                         if($all_events) {
141                                 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
142                                 foreach($ev as $x) {
143                                         $bd = false;
144                                         if($x['type'] === 'birthday') {
145                                                 $birthdays ++;
146                                                 $bd = true;
147                                         }
148                                         else {
149                                                 $events ++;
150                                         }
151                                         if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
152                                                 $all_events_today ++;
153                                                 if($bd)
154                                                         $birthdays_today ++;
155                                                 else
156                                                         $events_today ++;
157                                         }
158                                 }
159                         }
160                 }
161
162
163                 /**
164                  * return xml from notification array
165                  *
166                  * @param array $n Notification array:
167                  *              'href' => notification link
168                  *              'name' => subject name
169                  *              'url' => subject url
170                  *              'photo' => subject photo
171                  *              'date' => notification date
172                  *              'seen' => bool true/false
173                  *              'message' => notification message. "{0}" will be replaced by subject name
174                  **/
175                 function xmlize($n){
176                         $n['photo'] = proxy_url($n['photo']);
177
178                         $n['message'] = html_entity_decode($n['message'], ENT_COMPAT | ENT_HTML401, "UTF-8");
179                         $n['name'] = html_entity_decode($n['name'], ENT_COMPAT | ENT_HTML401, "UTF-8");
180
181                         // Are the nofications calles from the regular process or via the friendica app?
182                         $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
183
184                         $a = get_app();
185
186                         if ($a->is_friendica_app() OR !$regularnotifications)
187                                 $n['message'] = str_replace("{0}", $n['name'], $n['message']);
188
189                         $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']); 
190                                 
191                         call_hooks('ping_xmlize', $n);
192                         $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" timestamp="%s" >%s</note>'."\n";
193                         return sprintf ( $notsxml,
194                                 xmlify($n['href']), xmlify($n['name']), xmlify($n['url']), xmlify($n['photo']),
195                                 xmlify(relative_date($n['date'])), xmlify($n['seen']), xmlify(strtotime($local_time)),
196                                 xmlify($n['message'])
197                         );
198                 }
199
200                 echo "<intro>$intro</intro>
201                                 <mail>$mail</mail>
202                                 <net>$network</net>
203                                 <home>$home</home>\r\n";
204                 if ($register!=0) echo "<register>$register</register>";
205
206                 echo "<all-events>$all_events</all-events>
207                         <all-events-today>$all_events_today</all-events-today>
208                         <events>$events</events>
209                         <events-today>$events_today</events-today>
210                         <birthdays>$birthdays</birthdays>
211                         <birthdays-today>$birthdays_today</birthdays-today>\r\n";
212
213
214                 if(count($notifs) && (! $sysnotify)) {
215                         foreach($notifs as $zz) {
216                                 if($zz['seen'] == 0)
217                                         $sysnotify ++;
218                         }
219                 }
220
221                 echo '  <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
222
223                 // merge all notification types in one array
224                 if ($intro>0){
225                         foreach ($intros as $i) {
226                                 $n = array(
227                                         'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
228                                         'name' => $i['name'],
229                                         'url' => $i['url'], 
230                                         'photo' => $i['photo'],
231                                         'date' => $i['datetime'],
232                                         'seen' => false,
233                                         'message' => t("{0} wants to be your friend"),                          
234                                 );
235                                 $notifs[] = $n;
236                         }
237                 }
238                 
239                 if ($mail>0){
240                         foreach ($mails as $i) {
241                                 $n = array(
242                                         'href' => $a->get_baseurl().'/message/'.$i['id'],
243                                         'name' => $i['from-name'],
244                                         'url' => $i['from-url'], 
245                                         'photo' => $i['from-photo'],
246                                         'date' => $i['created'],
247                                         'seen' => false,
248                                         'message' => t("{0} sent you a message"),                               
249                                 );
250                                 $notifs[] = $n;
251                         }
252                 }
253                 
254                 if ($register>0){
255                         foreach ($regs as $i) {
256                                 $n = array(
257                                         'href' => $a->get_baseurl().'/admin/users/',
258                                         'name' => $i['name'],
259                                         'url' => $i['url'], 
260                                         'photo' => $i['micro'],
261                                         'date' => $i['created'],
262                                         'seen' => false,
263                                         'message' => t("{0} requested registration"),                           
264                                 );
265                                 $notifs[] = $n;
266                         }
267                 }
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) {
273                                 return 0;
274                         }
275                         return ($adate < $bdate) ? 1 : -1;
276                 };
277                 usort($notifs, $sort_function);
278
279                 if(count($notifs)) {
280                         foreach($notifs as $n) {
281                                 echo xmlize($n);
282                         }
283                 }
284
285
286                 echo "  </notif>";
287         }
288         echo " <sysmsgs>";
289
290         if(x($_SESSION,'sysmsg')){
291                 foreach ($_SESSION['sysmsg'] as $m){
292                         echo "<notice>".xmlify($m)."</notice>";
293                 }
294                 unset($_SESSION['sysmsg']);
295         }
296         if(x($_SESSION,'sysmsg_info')){
297                 foreach ($_SESSION['sysmsg_info'] as $m){
298                         echo "<info>".xmlify($m)."</info>";
299                 }
300                 unset($_SESSION['sysmsg_info']);
301         }
302
303         echo " </sysmsgs>";
304         echo"</result>
305         ";
306
307         killme();
308 }
309
310 function ping_get_notifications($uid) {
311
312         $result = array();
313         $offset = 0;
314         $seen = false;
315         $seensql = "NOT";
316         $order = "DESC";
317         $quit = false;
318
319         $a = get_app();
320
321         do {
322                 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
323                         FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
324                         WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
325                         AND NOT (`notify`.`type` IN (%d, %d))
326                         AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
327                         intval($uid),
328                         intval(NOTIFY_INTRO),
329                         intval(NOTIFY_MAIL),
330                         intval($offset)
331                 );
332
333                 if (!$r AND !$seen) {
334                         $seen = true;
335                         $seensql = "";
336                         $order = "DESC";
337                         $offset = 0;
338                 } elseif (!$r)
339                         $quit = true;
340                 else
341                         $offset += 50;
342                         
343
344                 foreach ($r AS $notification) {
345                         if (is_null($notification["visible"]))
346                                 $notification["visible"] = true;
347
348                         if (is_null($notification["spam"]))
349                                 $notification["spam"] = 0;
350
351                         if (is_null($notification["deleted"]))
352                                 $notification["deleted"] = 0;
353
354                         $notification["message"] = strip_tags(bbcode($notification["msg"]));
355                         $notification["name"] = strip_tags(bbcode($notification["name"]));
356
357                         // Replace the name with {0} but ensure to make that only once
358                         // The {0} is used later and prints the name in bold.
359
360                         $pos = strpos($notification["message"],$notification['name']);
361                         if ($pos !== false)
362                                 $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
363
364                         $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
365                                 
366                         if ($notification["visible"] AND !$notification["spam"] AND
367                                 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
368                                 $result[$notification["parent"]] = $notification;
369                         }
370                 }
371
372         } while ((count($result) < 50) AND !$quit);
373
374         
375         return($result);
376 }