]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
reinstate send slaps but only at the top level (and followup)
[friendica.git] / include / notifier.php
1 <?php
2 require_once("boot.php");
3
4 function notifier_run($argv, $argc){
5         global $a, $db;
6
7         if(is_null($a)){
8                 $a = new App;
9         }
10   
11         if(is_null($db)) {
12                 @include(".htconfig.php");
13                 require_once("dba.php");
14                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
15                         unset($db_host, $db_user, $db_pass, $db_data);
16         }
17
18         require_once("session.php");
19         require_once("datetime.php");
20         require_once('include/items.php');
21         require_once('include/bbcode.php');
22
23         if($argc < 3)
24                 return;
25
26         $a->set_baseurl(get_config('system','url'));
27
28         logger('notifier: invoked: ' . print_r($argv,true));
29
30         $cmd = $argv[1];
31
32         switch($cmd) {
33
34                 case 'mail':
35                 default:
36                         $item_id = intval($argv[2]);
37                         if(! $item_id){
38                                 return;
39                         }
40                         break;
41         }
42
43         $top_level = false;
44         $recipients = array();
45         $url_recipients = array();
46
47         if($cmd === 'mail') {
48
49                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
50                                 intval($item_id)
51                 );
52                 if(! count($message)){
53                         return;
54                 }
55                 $uid = $message[0]['uid'];
56                 $recipients[] = $message[0]['contact-id'];
57                 $item = $message[0];
58
59         }
60         else {
61
62                 // find ancestors
63                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
64                         intval($item_id)
65                 );
66
67                 if((! count($r)) || (! intval($r[0]['parent']))) {
68                         return;
69                 }
70
71                 $parent_id = intval($r[0]['parent']);
72                 $uid = $r[0]['uid'];
73                 $updated = $r[0]['edited'];
74
75                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
76                         intval($parent_id)
77                 );
78
79                 if(! count($items)){
80                         return;
81                 }
82
83
84                 // avoid race condition with deleting entries
85
86                 if($items[0]['deleted']) {
87                         foreach($items as $item)
88                                 $item['deleted'] = 1;
89                 }
90
91                 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
92                         $top_level = true;
93         }
94
95         $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags` 
96                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
97                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
98                 intval($uid)
99         );
100
101         if(count($r))
102                 $owner = $r[0];
103         else {
104                 return;
105         }
106         $hub = get_config('system','huburl');
107
108         // If this is a public conversation, notify the feed hub
109         $notify_hub = true;
110
111         // fill this in with a single salmon slap if applicable
112         $slap = '';
113
114         if($cmd != 'mail') {
115
116                 require_once('include/group.php');
117
118                 $parent = $items[0];
119
120                 if($parent['type'] === 'remote') {
121                         // local followup to remote post
122                         $followup = true;
123                         $notify_hub = false; // not public
124                         $conversant_str = dbesc($parent['contact-id']);
125                 }
126                 else {
127                         $followup = false;
128
129                         if((strlen($parent['allow_cid'])) 
130                                 || (strlen($parent['allow_gid'])) 
131                                 || (strlen($parent['deny_cid'])) 
132                                 || (strlen($parent['deny_gid']))) {
133                                 $notify_hub = false; // private recipients, not public
134                         }
135
136                         $allow_people = expand_acl($parent['allow_cid']);
137                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
138                         $deny_people  = expand_acl($parent['deny_cid']);
139                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
140
141                         $conversants = array();
142
143                         foreach($items as $item) {
144                                 $recipients[] = $item['contact-id'];
145                                 $conversants[] = $item['contact-id'];
146                                 // pull out additional tagged people to notify (if public message)
147                                 if($notify_hub && strlen($item['inform'])) {
148                                         $people = explode(',',$item['inform']);
149                                         foreach($people as $person) {
150                                                 if(substr($person,0,4) === 'cid:') {
151                                                         $recipients[] = intval(substr($person,4));
152                                                         $conversants[] = intval(substr($person,4));
153                                                 }
154                                                 else {
155                                                         $url_recipients[] = substr($person,4);
156                                                 }
157                                         }
158                                 }
159                         }
160
161                         logger('notifier: url_recipients' . print_r($url_recipients,true));
162
163                         $conversants = array_unique($conversants);
164
165
166                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
167                         $deny = array_unique(array_merge($deny_people,$deny_groups));
168                         $recipients = array_diff($recipients,$deny);
169
170                         $conversant_str = dbesc(implode(', ',$conversants));
171                 }
172
173                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
174
175
176                 if(count($r))
177                         $contacts = $r;
178         }
179
180         $feed_template = load_view_file('view/atom_feed.tpl');
181         $mail_template = load_view_file('view/atom_mail.tpl');
182
183         $atom = '';
184         $slaps = array();
185
186         $hubxml = feed_hublinks();
187
188         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
189
190         if(strlen($birthday))
191                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
192
193         $atom .= replace_macros($feed_template, array(
194                         '$version'      => xmlify(FRIENDIKA_VERSION),
195                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
196                         '$feed_title'   => xmlify($owner['name']),
197                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
198                         '$hub'          => $hubxml,
199                         '$salmon'       => '',  // private feed, we don't use salmon here
200                         '$name'         => xmlify($owner['name']),
201                         '$profile_page' => xmlify($owner['url']),
202                         '$photo'        => xmlify($owner['photo']),
203                         '$thumb'        => xmlify($owner['thumb']),
204                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
205                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
206                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
207                         '$birthday'     => $birthday
208         ));
209
210         if($cmd === 'mail') {
211                 $notify_hub = false;  // mail is  not public
212
213                 $atom .= replace_macros($mail_template, array(
214                         '$name'         => xmlify($owner['name']),
215                         '$profile_page' => xmlify($owner['url']),
216                         '$thumb'        => xmlify($owner['thumb']),
217                         '$item_id'      => xmlify($item['uri']),
218                         '$subject'      => xmlify($item['title']),
219                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
220                         '$content'      => xmlify($item['body']),
221                         '$parent_id'    => xmlify($item['parent-uri'])
222                 ));
223         }
224         else {
225                 if($followup) {
226                         foreach($items as $item) {  // there is only one item
227                                 if(! $item['parent'])
228                                         continue;
229                                 if($item['id'] == $item_id) {
230                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
231                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
232                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
233                                 }
234                         }
235                 }
236                 else {
237                         foreach($items as $item) {
238                                 if(! $item['parent'])
239                                         continue;
240
241                                 $contact = get_item_contact($item,$contacts);
242                                 if(! $contact)
243                                         continue;
244
245                                 $atom   .= atom_entry($item,'text',$contact,$owner,true);
246
247                                 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link'])) 
248                                         $slaps[] = atom_entry($item,'html',$contact,$owner,true);
249                         }
250                 }
251         }
252         $atom .= '</feed>' . "\r\n";
253
254         logger('notifier: ' . $atom, LOGGER_DATA);
255
256         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
257
258         if($followup)
259                 $recip_str = $parent['contact-id'];
260         else
261                 $recip_str = implode(', ', $recipients);
262
263
264         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
265                 dbesc($recip_str)
266         );
267
268         // delivery loop
269
270         require_once('include/salmon.php');
271
272         if(count($r)) {
273                 foreach($r as $contact) {
274                         if($contact['self'])
275                                 continue;
276
277                         $deliver_status = 0;
278
279                         switch($contact['network']) {
280                                 case 'dfrn':
281                                         logger('notifier: dfrndelivery: ' . $contact['name']);
282                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
283
284                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
285         
286                                         if($deliver_status == (-1)) {
287                                                 logger('notifier: delivery failed: queuing message');
288                                                 // queue message for redelivery
289                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
290                                                         VALUES ( %d, '%s', '%s', '%s') ",
291                                                         intval($contact['id']),
292                                                         dbesc(datetime_convert()),
293                                                         dbesc(datetime_convert()),
294                                                         dbesc($atom)
295                                                 );
296                                         }
297                                         break;
298                                 case 'stat':
299                                         if($followup && $contact['notify']) {
300                                                 logger('notifier: slapdelivery: ' . $contact['name']);
301                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
302
303                                                 if($deliver_status == (-1)) {
304                                                         // queue message for redelivery
305                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
306                                                                 VALUES ( %d, '%s', '%s', '%s') ",
307                                                                 intval($contact['id']),
308                                                                 dbesc(datetime_convert()),
309                                                                 dbesc(datetime_convert()),
310                                                                 dbesc($slap)
311                                                         );
312
313                                                 }
314         
315
316                                         }
317                                         else {
318
319                                                 // only send salmon if public - e.g. if it's ok to notify
320                                                 // a public hub, it's ok to send a salmon
321
322                                                 if(count($slaps) && $notify_hub) {
323                                                         logger('notifier: slapdelivery: ' . $contact['name']);
324                                                         foreach($slaps as $slappy) {
325                                                                 if($contact['notify']) {
326                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
327                                                                         if($deliver_status == (-1)) {
328                                                                                 // queue message for redelivery
329                                                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
330                                                                                         VALUES ( %d, '%s', '%s', '%s') ",
331                                                                                         intval($contact['id']),
332                                                                                         dbesc(datetime_convert()),
333                                                                                         dbesc(datetime_convert()),
334                                                                                         dbesc($slappy)
335                                                                                 );                                                              
336                                                                         }
337                                                                 }
338                                                         }
339                                                 }
340                                         }
341                                         break;
342                                 case 'mail':
343                                 case 'dspr':
344                                 case 'feed':
345                                 default:
346                                         break;
347                         }
348                 }
349         }
350                 
351         // send additional slaps to mentioned remote tags (@foo@example.com)
352
353         if($slap && count($url_recipients) && $followup && $notify_hub) {
354                 foreach($url_recipients as $url) {
355                         if($url) {
356                                 logger('notifier: urldelivery: ' . $url);
357                                 $deliver_status = slapper($owner,$url,$slap);
358                                 // TODO: redeliver/queue these items on failure, though there is no contact record
359                         }
360                 }
361         }
362
363         if((strlen($hub)) && ($notify_hub)) {
364                 $hubs = explode(',', $hub);
365                 if(count($hubs)) {
366                         foreach($hubs as $h) {
367                                 $h = trim($h);
368                                 if(! strlen($h))
369                                         continue;
370                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
371                                 post_url($h,$params);
372                                 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
373                                 if(count($hubs) > 1)
374                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
375                         }
376                 }
377         }
378
379         if($notify_hub) {
380
381                 /**
382                  *
383                  * If you have less than 150 dfrn friends and it's a public message,
384                  * we'll just go ahead and push them out securely with dfrn/rino.
385                  * If you've got more than that, you'll have to rely on PuSH delivery.
386                  *
387                  */
388
389                 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
390                                 
391                 /**
392                  *
393                  * Only get the bare essentials and go back for the full record. 
394                  * If you've got a lot of friends and we grab all the details at once it could exhaust memory. 
395                  *
396                  */
397
398                 $r = q("SELECT `id`, `name` FROM `contact` 
399                         WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
400                         AND `rel` != %d ",
401                         intval($owner['uid']),
402                         intval(REL_FAN)
403                 );
404
405                 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
406
407                         foreach($r as $rr) {
408
409                                 /* Don't deliver to folks who have already been delivered to */
410
411                                 if(! in_array($rr['id'], $conversants)) {
412                                         $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
413                                                         intval($rr['id'])
414                                         );
415
416                                         if(count($n)) {
417                                         
418                                                 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
419                                                 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
420                                         }
421                                 }
422                                 else
423                                         logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
424                         }
425                 }
426         }
427
428         return;
429 }
430
431 if (array_search(__file__,get_included_files())===0){
432   echo "run!";
433   notifier_run($argv,$argc);
434   killme();
435 }