]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
Also use is_filled_array() here, too. if $contacts is FALSE count() issues are E_WARNING
[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                 $regs = array();
36                 $mails = array();
37
38                 $home = 0;
39                 $network = 0;
40                 $groups_unseen = array();
41                 $forums_unseen = array();
42
43                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
44                                 `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
45                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
46                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
47                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
48                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
49                                 AND `item`.`contact-id` != %d
50                                 ORDER BY `item`.`created` DESC",
51                         intval(local_user()), intval(local_user())
52                 );
53
54                 if(is_filled_array($r)) {
55
56                         $arr = array('items' => $r);
57                         call_hooks('network_ping', $arr);
58
59                         foreach ($r as $it) {
60
61                                 if($it['wall'])
62                                         $home ++;
63                                 else
64                                         $network ++;
65
66                                 switch($it['verb']){
67                                         case ACTIVITY_TAG:
68                                                 $obj = parse_xml_string($xmlhead.$it['object']);
69                                                 $it['tname'] = $obj->content;
70                                                 $tags[] = $it;
71                                                 break;
72                                         case ACTIVITY_LIKE:
73                                                 $likes[] = $it;
74                                                 break;
75                                         case ACTIVITY_DISLIKE:
76                                                 $dislikes[] = $it;
77                                                 break;
78                                         case ACTIVITY_FRIEND:
79                                                 $obj = parse_xml_string($xmlhead.$it['object']);
80                                                 $it['fname'] = $obj->title;
81                                                 $friends[] = $it;
82                                                 break;
83                                         default:
84                                                 if ($it['parent']!=$it['id']) {
85                                                         $comments[] = $it;
86                                                 } else {
87                                                         if(! $it['wall'])
88                                                                 $posts[] = $it;
89                                                 }
90                                 }
91                         }
92                 }
93
94                 if($network) {
95                         if(intval(feature_enabled(local_user(),'groups'))) {
96                                 // Find out how unseen network posts are spread across groups
97                                 $groups_unseen = groups_count_unseen();
98                         }
99
100                         if(intval(feature_enabled(local_user(),'forumlist_widget'))) {
101                                 $forums_unseen = ForumManager::count_unseen_items();
102                         }
103                 }
104
105                 $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,
106                         `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
107                         FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
108                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
109                         intval(local_user())
110                 );
111                 $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
112                         `contact`.`name`, `contact`.`url`, `contact`.`photo`
113                         FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
114                         WHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
115                         intval(local_user())
116                 );
117
118                 $intro = count($intros1) + count($intros2);
119                 $intros = $intros1+$intros2;
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(is_filled_array($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 ( is_filled_array($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 ( is_filled_array($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 (is_filled_array($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 ( is_filled_array($intros) ) {
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 ( is_filled_array($mails) ) {
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 ( is_filled_array($regs) ) {
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
303                 // sort notifications by $[]['date']
304                 $sort_function = function($a, $b) {
305                         $adate = date($a['date']);
306                         $bdate = date($b['date']);
307                         if ($adate == $bdate) {
308                                 return 0;
309                         }
310                         return ($adate < $bdate) ? 1 : -1;
311                 };
312                 usort($notifs, $sort_function);
313
314                 if( is_filled_array($notifs) ) {
315                         foreach($notifs as $n) {
316                                 echo xmlize($n);
317                         }
318                 }
319
320
321                 echo "  </notif>";
322         }
323         echo " <sysmsgs>";
324
325         if(x($_SESSION,'sysmsg')){
326                 foreach ($_SESSION['sysmsg'] as $m){
327                         echo "<notice>".xmlify($m)."</notice>";
328                 }
329                 unset($_SESSION['sysmsg']);
330         }
331         if(x($_SESSION,'sysmsg_info')){
332                 foreach ($_SESSION['sysmsg_info'] as $m){
333                         echo "<info>".xmlify($m)."</info>";
334                 }
335                 unset($_SESSION['sysmsg_info']);
336         }
337
338         echo " </sysmsgs>";
339         echo"</result>
340         ";
341
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 }