]> git.mxchange.org Git - friendica.git/blob - mod/ping.php
ping.php performance: Cache notification bbcode
[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`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
127                         if ($regs)
128                                 $register = $regs[0]['total'];
129                 } else {
130                         $register = "0";
131                 }
132
133                 $all_events = 0;
134                 $all_events_today = 0;
135                 $events = 0;
136                 $events_today = 0;
137                 $birthdays = 0;
138                 $birthdays_today = 0;
139
140
141                 $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
142                         WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
143                         ORDER BY `start` ASC ",
144                         intval(local_user()),
145                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
146                         dbesc(datetime_convert('UTC','UTC','now'))
147                 );
148
149                 if (dbm::is_result($ev)) {
150                         $all_events = intval($ev[0]['total']);
151
152                         if ($all_events) {
153                                 $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
154                                 foreach($ev as $x) {
155                                         $bd = false;
156                                         if ($x['type'] === 'birthday') {
157                                                 $birthdays ++;
158                                                 $bd = true;
159                                         }
160                                         else {
161                                                 $events ++;
162                                         }
163                                         if (datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
164                                                 $all_events_today ++;
165                                                 if ($bd)
166                                                         $birthdays_today ++;
167                                                 else
168                                                         $events_today ++;
169                                         }
170                                 }
171                         }
172                 }
173
174                 $data = array();
175                 $data["intro"] = $intro;
176                 $data["mail"] = $mail;
177                 $data["net"] = $network;
178                 $data["home"] = $home;
179
180                 if ($register!=0)
181                         $data["register"] = $register;
182
183                 $groups = array();
184
185                 if (dbm::is_result($groups_unseen)) {
186                         $count = 0;
187                         foreach ($groups_unseen as $it)
188                                 if ($it['count'] > 0) {
189                                         $count++;
190                                         $groups[$count.":group"] = $it['count'];
191                                         $groups[$count.":@attributes"] = array("id" => $it['id']);
192                                 }
193                         $data["groups"] = $groups;
194                 }
195
196                 $forums = array();
197
198                 if (dbm::is_result($forums_unseen)) {
199                         $count = 0;
200                         foreach ($forums_unseen as $it)
201                                 if ($it['count'] > 0) {
202                                         $count++;
203                                         $forums[$count.":forum"] = $it['count'];
204                                         $forums[$count.":@attributes"] = array("id" => $it['id']);
205                                 }
206                         $data["forums"] = $forums;
207                 }
208
209                 $data["all-events"] = $all_events;
210                 $data["all-events-today"] = $all_events_today;
211                 $data["events"] = $events;
212                 $data["events-today"] = $events_today;
213                 $data["birthdays"] = $birthdays;
214                 $data["birthdays-today"] = $birthdays_today;
215
216
217                 if (dbm::is_result($notifs) && !$sysnotify) {
218                         foreach ($notifs as $zz) {
219                                 if ($zz['seen'] == 0)
220                                         $sysnotify ++;
221                         }
222                 }
223
224                 // merge all notification types in one array
225                 if (dbm::is_result($intros)) {
226                         foreach ($intros as $i) {
227                                 $n = array(
228                                         'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
229                                         'name' => $i['name'],
230                                         'url' => $i['url'],
231                                         'photo' => $i['photo'],
232                                         'date' => $i['datetime'],
233                                         'seen' => false,
234                                         'message' => t("{0} wants to be your friend"),
235                                 );
236                                 $notifs[] = $n;
237                         }
238                 }
239
240                 if (dbm::is_result($mails)) {
241                         foreach ($mails as $i) {
242                                 $n = array(
243                                         'href' => $a->get_baseurl().'/message/'.$i['id'],
244                                         'name' => $i['from-name'],
245                                         'url' => $i['from-url'],
246                                         'photo' => $i['from-photo'],
247                                         'date' => $i['created'],
248                                         'seen' => false,
249                                         'message' => t("{0} sent you a message"),
250                                 );
251                                 $notifs[] = $n;
252                         }
253                 }
254
255                 if (dbm::is_result($regs)) {
256                         foreach ($regs as $i) {
257                                 $n = array(
258                                         'href' => $a->get_baseurl().'/admin/users/',
259                                         'name' => $i['name'],
260                                         'url' => $i['url'],
261                                         'photo' => $i['micro'],
262                                         'date' => $i['created'],
263                                         'seen' => false,
264                                         'message' => t("{0} requested registration"),
265                                 );
266                                 $notifs[] = $n;
267                         }
268                 }
269
270                 // sort notifications by $[]['date']
271                 $sort_function = function($a, $b) {
272                         $adate = date($a['date']);
273                         $bdate = date($b['date']);
274                         if ($adate == $bdate) {
275                                 return 0;
276                         }
277                         return ($adate < $bdate) ? 1 : -1;
278                 };
279                 usort($notifs, $sort_function);
280
281                 if (dbm::is_result($notifs)) {
282
283                         // Are the nofications calles from the regular process or via the friendica app?
284                         $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
285
286                         $count = 0;
287                         foreach($notifs as $n) {
288                                 $count++;
289                                 if ($a->is_friendica_app() OR !$regularnotifications)
290                                         $n['message'] = str_replace("{0}", $n['name'], $n['message']);
291
292                                 $notifications[$count.":note"] = $n['message'];
293
294                                 $contact = get_contact_details_by_url($n['url']);
295                                 if (isset($contact["micro"]))
296                                         $n['photo'] = proxy_url($contact["micro"], false, PROXY_SIZE_MICRO);
297                                 else
298                                         $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
299
300                                 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
301
302                                 call_hooks('ping_xmlize', $n);
303
304                                 $notifications[$count.":@attributes"] = array("id" => $n["id"],
305                                                                                 "href" => $n['href'],
306                                                                                 "name" => $n['name'],
307                                                                                 "url" => $n['url'],
308                                                                                 "photo" => $n['photo'],
309                                                                                 "date" => relative_date($n['date']),
310                                                                                 "seen" => $n['seen'],
311                                                                                 "timestamp" => strtotime($local_time));
312
313                         }
314                 }
315
316                 $data["notif"] = $notifications;
317                 $data["@attributes"] = array("count" => $sysnotify + $intro + $mail + $register);
318         }
319
320         $sysmsg = array();
321
322         if (x($_SESSION,'sysmsg')){
323                 $count = 0;
324                 foreach ($_SESSION['sysmsg'] as $m){
325                         $count++;
326                         $sysmsg[$count.":notice"] = $m;
327                 }
328                 unset($_SESSION['sysmsg']);
329         }
330
331         if (x($_SESSION,'sysmsg_info')){
332                 $count = 0;
333                 foreach ($_SESSION['sysmsg_info'] as $m){
334                         $count++;
335                         $sysmsg[$count.":info"] = $m;
336                 }
337                 unset($_SESSION['sysmsg_info']);
338         }
339
340         $data["sysmsgs"] = $sysmsg;
341
342         header("Content-type: text/xml");
343         echo xml::from_array(array("result" => $data), $xml);
344         killme();
345 }
346
347 /**
348  * @brief Retrieves the notifications array for the given user ID
349  *
350  * @param int $uid
351  * @return array
352  */
353 function ping_get_notifications($uid) {
354
355         $result = array();
356         $offset = 0;
357         $seen = false;
358         $seensql = "NOT";
359         $order = "DESC";
360         $quit = false;
361
362         $a = get_app();
363
364         do {
365                 $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
366                         FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
367                         WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
368                         AND NOT (`notify`.`type` IN (%d, %d))
369                         AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
370                         intval($uid),
371                         intval(NOTIFY_INTRO),
372                         intval(NOTIFY_MAIL),
373                         intval($offset)
374                 );
375
376                 if (!$r AND !$seen) {
377                         $seen = true;
378                         $seensql = "";
379                         $order = "DESC";
380                         $offset = 0;
381                 } elseif (!$r) {
382                         $quit = true;
383                 } else {
384                         $offset += 50;
385                 }
386
387                 foreach ($r AS $notification) {
388                         if (is_null($notification["visible"])) {
389                                 $notification["visible"] = true;
390                         }
391
392                         if (is_null($notification["spam"])) {
393                                 $notification["spam"] = 0;
394                         }
395
396                         if (is_null($notification["deleted"])) {
397                                 $notification["deleted"] = 0;
398                         }
399
400                         if ($notification["msg_cache"]) {
401                                 $notification["name"] = $notification["name_cache"];
402                                 $notification["message"] = $notification["msg_cache"];
403                         } else {
404                                 $notification["name"] = strip_tags(bbcode($notification["name"]));
405                                 $notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
406
407                                 q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
408                                         dbesc($notification["name"]),
409                                         dbesc($notification["message"]),
410                                         intval($notification["id"])
411                                 );
412                         }
413
414                         $notification["href"] = $a->get_baseurl() . "/notify/view/" . $notification["id"];
415
416                         if ($notification["visible"] AND !$notification["spam"] AND
417                                 !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
418                                 $result[$notification["parent"]] = $notification;
419                         }
420                 }
421         } while ((count($result) < 50) AND !$quit);
422
423         return($result);
424 }