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