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