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