]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
4f939eaa0cbe12a5217254f9ad73b72ce350f440
[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         if (local_user()){
12                 // Different login session than the page that is calling us.
13                 if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
14                         $data = array("invalid" => 1);
15                         header("Content-type: text/xml");
16                         echo xml::from_array(array("result" => $data));
17                         killme();
18                 }
19
20                 $notifs = ping_get_notifications(local_user());
21                 $sysnotify = 0; // we will update this in a moment
22
23                 $tags = array();
24                 $comments = array();
25                 $likes = array();
26                 $dislikes = array();
27                 $friends = array();
28                 $posts = array();
29                 $regs = array();
30                 $mails = array();
31
32                 $home = 0;
33                 $network = 0;
34                 $groups_unseen = array();
35                 $forums_unseen = array();
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                                 AND `item`.`contact-id` != %d
44                                 ORDER BY `item`.`created` DESC",
45                         intval(local_user()), intval(local_user())
46                 );
47
48                 if (dbm::is_result($r)) {
49
50                         $arr = array('items' => $r);
51                         call_hooks('network_ping', $arr);
52
53                         foreach ($r as $it) {
54
55                                 if ($it['wall'])
56                                         $home ++;
57                                 else
58                                         $network ++;
59
60                                 switch($it['verb']){
61                                         case ACTIVITY_TAG:
62                                                 $obj = parse_xml_string($xmlhead.$it['object']);
63                                                 $it['tname'] = $obj->content;
64                                                 $tags[] = $it;
65                                                 break;
66                                         case ACTIVITY_LIKE:
67                                                 $likes[] = $it;
68                                                 break;
69                                         case ACTIVITY_DISLIKE:
70                                                 $dislikes[] = $it;
71                                                 break;
72                                         case ACTIVITY_FRIEND:
73                                                 $obj = parse_xml_string($xmlhead.$it['object']);
74                                                 $it['fname'] = $obj->title;
75                                                 $friends[] = $it;
76                                                 break;
77                                         default:
78                                                 if ($it['parent']!=$it['id']) {
79                                                         $comments[] = $it;
80                                                 } else {
81                                                         if (!$it['wall'])
82                                                                 $posts[] = $it;
83                                                 }
84                                 }
85                         }
86                 }
87
88                 if ($network) {
89                         if (intval(feature_enabled(local_user(),'groups'))) {
90                                 // Find out how unseen network posts are spread across groups
91                                 $groups_unseen = groups_count_unseen();
92                         }
93
94                         if (intval(feature_enabled(local_user(),'forumlist_widget'))) {
95                                 $forums_unseen = ForumManager::count_unseen_items();
96                         }
97                 }
98
99                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,
100                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
101                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
102                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
103                         intval(local_user())
104                 );
105                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
106                         `contact`.`name`, `contact`.`url`, `contact`.`photo`
107                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
108                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
109                         intval(local_user())
110                 );
111
112                 $intro = count($intros1) + count($intros2);
113                 $intros = $intros1+$intros2;
114
115                 $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
116                 $mails = q("SELECT * FROM `mail`
117                         WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
118                         intval(local_user()),
119                         dbesc($myurl)
120                 );
121                 $mail = count($mails);
122
123                 if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
124                         $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");
125                         if ($regs)
126                                 $register = $regs[0]['total'];
127                 } else {
128                         $register = "0";
129                 }
130
131                 $all_events = 0;
132                 $all_events_today = 0;
133                 $events = 0;
134                 $events_today = 0;
135                 $birthdays = 0;
136                 $birthdays_today = 0;
137
138
139                 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
140                         WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
141                         ORDER BY `start` ASC ",
142                         intval(local_user()),
143                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
144                         dbesc(datetime_convert('UTC','UTC','now'))
145                 );
146
147                 if (dbm::is_result($ev)) {
148                         $all_events = intval($ev[0]['total']);
149
150                         if ($all_events) {
151                                 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
152                                 foreach($ev as $x) {
153                                         $bd = false;
154                                         if ($x['type'] === 'birthday') {
155                                                 $birthdays ++;
156                                                 $bd = true;
157                                         }
158                                         else {
159                                                 $events ++;
160                                         }
161                                         if (datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
162                                                 $all_events_today ++;
163                                                 if ($bd)
164                                                         $birthdays_today ++;
165                                                 else
166                                                         $events_today ++;
167                                         }
168                                 }
169                         }
170                 }
171
172                 $data = array();
173                 $data["intro"] = $intro;
174                 $data["mail"] = $mail;
175                 $data["net"] = $network;
176                 $data["home"] = $home;
177
178                 if ($register!=0)
179                         $data["register"] = $register;
180
181                 $groups = array();
182
183                 if (dbm::is_result($groups_unseen)) {
184                         $count = 0;
185                         foreach ($groups_unseen as $it)
186                                 if ($it['count'] > 0) {
187                                         $count++;
188                                         $groups[$count.":group"] = $it['count'];
189                                         $groups[$count.":@attributes"] = array("id" => $it['id']);
190                                 }
191                         $data["groups"] = $groups;
192                 }
193
194                 $forums = array();
195
196                 if (dbm::is_result($forums_unseen)) {
197                         $count = 0;
198                         foreach ($forums_unseen as $it)
199                                 if ($it['count'] > 0) {
200                                         $count++;
201                                         $forums[$count.":forum"] = $it['count'];
202                                         $forums[$count.":@attributes"] = array("id" => $it['id']);
203                                 }
204                         $data["forums"] = $forums;
205                 }
206
207                 $data["all-events"] = $all_events;
208                 $data["all-events-today"] = $all_events_today;
209                 $data["events"] = $events;
210                 $data["events-today"] = $events_today;
211                 $data["birthdays"] = $birthdays;
212                 $data["birthdays-today"] = $birthdays_today;
213
214
215                 if (dbm::is_result($notifs) && !$sysnotify) {
216                         foreach ($notifs as $zz) {
217                                 if ($zz['seen'] == 0)
218                                         $sysnotify ++;
219                         }
220                 }
221
222                 // merge all notification types in one array
223                 if (dbm::is_result($intros)) {
224                         foreach ($intros as $i) {
225                                 $n = array(
226                                         'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
227                                         'name' => $i['name'],
228                                         'url' => $i['url'],
229                                         'photo' => $i['photo'],
230                                         'date' => $i['datetime'],
231                                         'seen' => false,
232                                         'message' => t("{0} wants to be your friend"),
233                                 );
234                                 $notifs[] = $n;
235                         }
236                 }
237
238                 if (dbm::is_result($mails)) {
239                         foreach ($mails as $i) {
240                                 $n = array(
241                                         'href' => $a->get_baseurl().'/message/'.$i['id'],
242                                         'name' => $i['from-name'],
243                                         'url' => $i['from-url'],
244                                         'photo' => $i['from-photo'],
245                                         'date' => $i['created'],
246                                         'seen' => false,
247                                         'message' => t("{0} sent you a message"),
248                                 );
249                                 $notifs[] = $n;
250                         }
251                 }
252
253                 if (dbm::is_result($regs)) {
254                         foreach ($regs as $i) {
255                                 $n = array(
256                                         'href' => $a->get_baseurl().'/admin/users/',
257                                         'name' => $i['name'],
258                                         'url' => $i['url'],
259                                         'photo' => $i['micro'],
260                                         'date' => $i['created'],
261                                         'seen' => false,
262                                         'message' => t("{0} requested registration"),
263                                 );
264                                 $notifs[] = $n;
265                         }
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 (dbm::is_result($notifs)) {
280
281                         // Are the nofications calles from the regular process or via the friendica app?
282                         $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
283
284                         $count = 0;
285                         foreach($notifs as $n) {
286                                 $count++;
287                                 if ($a->is_friendica_app() OR !$regularnotifications)
288                                         $n['message'] = str_replace("{0}", $n['name'], $n['message']);
289
290                                 $notifications[$count.":note"] = $n['message'];
291
292                                 $contact = get_contact_details_by_url($n['url']);
293                                 if (isset($contact["micro"]))
294                                         $n['photo'] = proxy_url($contact["micro"], false, PROXY_SIZE_MICRO);
295                                 else
296                                         $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
297
298                                 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
299
300                                 call_hooks('ping_xmlize', $n);
301
302                                 $notifications[$count.":@attributes"] = array("id" => $n["id"],
303                                                                                 "href" => $n['href'],
304                                                                                 "name" => $n['name'],
305                                                                                 "url" => $n['url'],
306                                                                                 "photo" => $n['photo'],
307                                                                                 "date" => relative_date($n['date']),
308                                                                                 "seen" => $n['seen'],
309                                                                                 "timestamp" => strtotime($local_time));
310
311                         }
312                 }
313
314                 $data["notif"] = $notifications;
315                 $data["@attributes"] = array("count" => $sysnotify + $intro + $mail + $register);
316         }
317
318         $sysmsg = array();
319
320         if (x($_SESSION,'sysmsg')){
321                 $count = 0;
322                 foreach ($_SESSION['sysmsg'] as $m){
323                         $count++;
324                         $sysmsg[$count.":notice"] = $m;
325                 }
326                 unset($_SESSION['sysmsg']);
327         }
328
329         if (x($_SESSION,'sysmsg_info')){
330                 $count = 0;
331                 foreach ($_SESSION['sysmsg_info'] as $m){
332                         $count++;
333                         $sysmsg[$count.":info"] = $m;
334                 }
335                 unset($_SESSION['sysmsg_info']);
336         }
337
338         $data["sysmsgs"] = $sysmsg;
339
340         header("Content-type: text/xml");
341         echo xml::from_array(array("result" => $data));
342         killme();
343 }
344
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 }