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