]> git.mxchange.org Git - friendica.git/blob - include/delivery.php
implement max load average before queuing/deferring delivery and poller processes
[friendica.git] / include / delivery.php
1 <?php
2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4 require_once('include/html2plain.php');
5
6 function delivery_run($argv, $argc){
7         global $a, $db;
8
9         if(is_null($a)){
10                 $a = new App;
11         }
12
13         if(is_null($db)) {
14                 @include(".htconfig.php");
15                 require_once("dba.php");
16                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
17                         unset($db_host, $db_user, $db_pass, $db_data);
18         }
19
20         require_once("session.php");
21         require_once("datetime.php");
22         require_once('include/items.php');
23         require_once('include/bbcode.php');
24         require_once('include/diaspora.php');
25         require_once('include/email.php');
26
27         load_config('config');
28         load_config('system');
29
30         load_hooks();
31
32         if($argc < 3)
33                 return;
34
35         $a->set_baseurl(get_config('system','url'));
36
37         logger('delivery: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
38
39         $cmd        = $argv[1];
40         $item_id    = intval($argv[2]);
41         $contact_id = intval($argv[3]);
42
43         // Some other process may have delivered this item already.
44
45         $r = q("select * from deliverq where cmd = '%s' and item = %d and contact = %d limit 1",
46                 dbesc($cmd),
47                 dbesc($item_id),
48                 dbesc($contact_id)
49         );
50         if(! count($r)) {
51                 return;
52         }       
53
54         $maxsysload = intval(get_config('system','maxloadavg'));
55         if($maxsysload < 1)
56                 $maxsysload = 50;
57         if(function_exists('sys_getloadavg')) {
58                 $load = sys_getloadavg();
59                 if(intval($load[0]) > $maxsysload) {
60                         logger('system: load ' . $load . ' too high. Delivery deferred to next queue run.');
61                         return;
62                 }
63         }
64
65         // It's ours to deliver. Remove it from the queue.
66
67         q("delete from deliverq where cmd = '%s' and item = %d and contact = %d limit 1",
68                 dbesc($cmd),
69                 dbesc($item_id),
70                 dbesc($contact_id)
71         );
72
73         if((! $item_id) || (! $contact_id))
74                 return;
75
76         $expire = false;
77         $top_level = false;
78         $recipients = array();
79         $url_recipients = array();
80
81         $normal_mode = true;
82
83         $recipients[] = $contact_id;
84
85         if($cmd === 'expire') {
86                 $normal_mode = false;
87                 $expire = true;
88                 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1 
89                         AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 30 MINUTE",
90                         intval($item_id)
91                 );
92                 $uid = $item_id;
93                 $item_id = 0;
94                 if(! count($items))
95                         return;
96         }
97         else {
98
99                 // find ancestors
100                 $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
101                         intval($item_id)
102                 );
103
104                 if((! count($r)) || (! intval($r[0]['parent']))) {
105                         return;
106                 }
107
108                 $target_item = $r[0];
109                 $parent_id = intval($r[0]['parent']);
110                 $uid = $r[0]['uid'];
111                 $updated = $r[0]['edited'];
112
113                 if(! $parent_id)
114                         return;
115
116
117                 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` 
118                         FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
119                         intval($parent_id)
120                 );
121
122                 if(! count($items)) {
123                         return;
124                 }
125
126                 $icontacts = null;
127                 $contacts_arr = array();
128                 foreach($items as $item)
129                         if(! in_array($item['contact-id'],$contacts_arr))
130                                 $contacts_arr[] = intval($item['contact-id']);
131                 if(count($contacts_arr)) {
132                         $str_contacts = implode(',',$contacts_arr); 
133                         $icontacts = q("SELECT * FROM `contact` 
134                                 WHERE `id` IN ( $str_contacts ) "
135                         );
136                 }
137                 if( ! ($icontacts && count($icontacts)))
138                         return;
139
140                 // avoid race condition with deleting entries
141
142                 if($items[0]['deleted']) {
143                         foreach($items as $item)
144                                 $item['deleted'] = 1;
145                 }
146
147                 if((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
148                         logger('delivery: top level post');
149                         $top_level = true;
150                 }
151         }
152
153         $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`, 
154                 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, 
155                 `user`.`page-flags`, `user`.`prvnets`
156                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
157                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
158                 intval($uid)
159         );
160
161         if(! count($r))
162                 return;
163
164         $owner = $r[0];
165
166         $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
167
168         $public_message = true;
169
170         // fill this in with a single salmon slap if applicable
171
172         $slap = '';
173
174         require_once('include/group.php');
175
176         $parent = $items[0];
177
178                 // This is IMPORTANT!!!!
179
180                 // We will only send a "notify owner to relay" or followup message if the referenced post
181                 // originated on our system by virtue of having our hostname somewhere
182                 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
183                 // if $parent['wall'] == 1 we will already have the parent message in our array
184                 // and we will relay the whole lot.
185  
186                 // expire sends an entire group of expire messages and cannot be forwarded.
187                 // However the conversation owner will be a part of the conversation and will 
188                 // be notified during this run.
189                 // Other DFRN conversation members will be alerted during polled updates.
190
191                 // Diaspora members currently are not notified of expirations, and other networks have
192                 // either limited or no ability to process deletions. We should at least fix Diaspora 
193                 // by stringing togther an array of retractions and sending them onward.
194                  
195         
196         $localhost = $a->get_hostname();
197         if(strpos($localhost,':'))
198                 $localhost = substr($localhost,0,strpos($localhost,':'));
199
200                 /**
201                  *
202                  * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes 
203                  * have been known to cause runaway conditions which affected several servers, along with 
204                  * permissions issues. 
205                  *
206                  */
207  
208         if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
209                 logger('relay denied for delivery agent.');
210
211                 /* no relay allowed for direct contact delivery */
212                 return;
213         }
214
215         if((strlen($parent['allow_cid'])) 
216                 || (strlen($parent['allow_gid'])) 
217                 || (strlen($parent['deny_cid'])) 
218                 || (strlen($parent['deny_gid']))) {
219                 $public_message = false; // private recipients, not public
220         }
221
222         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `blocked` = 0 AND `pending` = 0",
223                 intval($contact_id)
224         );
225
226         if(count($r))
227                 $contact = $r[0];
228         
229         $hubxml = feed_hublinks();
230
231         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
232
233         require_once('include/salmon.php');
234
235         if($contact['self'])
236                 return;
237
238         $deliver_status = 0;
239
240         switch($contact['network']) {
241
242                 case NETWORK_DFRN :
243                         logger('notifier: dfrndelivery: ' . $contact['name']);
244
245                         $feed_template = get_markup_template('atom_feed.tpl');
246                         $mail_template = get_markup_template('atom_mail.tpl');
247
248                         $atom = '';
249
250
251                         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
252
253                         if(strlen($birthday))
254                                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
255
256                         $atom .= replace_macros($feed_template, array(
257                                         '$version'      => xmlify(FRIENDICA_VERSION),
258                                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
259                                         '$feed_title'   => xmlify($owner['name']),
260                                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
261                                         '$hub'          => $hubxml,
262                                         '$salmon'       => '',  // private feed, we don't use salmon here
263                                         '$name'         => xmlify($owner['name']),
264                                         '$profile_page' => xmlify($owner['url']),
265                                         '$photo'        => xmlify($owner['photo']),
266                                         '$thumb'        => xmlify($owner['thumb']),
267                                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
268                                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
269                                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
270                                         '$birthday'     => $birthday,
271                                         '$community'    => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
272                         ));
273
274                         foreach($items as $item) {
275                                 if(! $item['parent'])
276                                         continue;
277
278                                 // private emails may be in included in public conversations. Filter them.
279                                 if(($public_message) && $item['private'])
280                                         continue;
281
282                                 $item_contact = get_item_contact($item,$icontacts);
283                                 if(! $item_contact)
284                                         continue;
285
286                                 if($normal_mode) {
287                                         if($item_id == $item['id'] || $item['id'] == $item['parent'])
288                                                 $atom .= atom_entry($item,'text',null,$owner,true);
289                                 }
290                                 else
291                                         $atom .= atom_entry($item,'text',null,$owner,true);
292
293                         }
294
295                         $atom .= '</feed>' . "\r\n";
296
297                         logger('notifier: ' . $atom, LOGGER_DATA);
298                         $basepath =  implode('/', array_slice(explode('/',$contact['url']),0,3));
299
300                         // perform local delivery if we are on the same site
301
302                         if(link_compare($basepath,$a->get_baseurl())) {
303
304                                 $nickname = basename($contact['url']);
305                                 if($contact['issued-id'])
306                                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
307                                 else
308                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
309
310                                 $x = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`, 
311                                         `contact`.`pubkey` AS `cpubkey`, 
312                                         `contact`.`prvkey` AS `cprvkey`, 
313                                         `contact`.`thumb` AS `thumb`, 
314                                         `contact`.`url` as `url`,
315                                         `contact`.`name` as `senderName`,
316                                         `user`.* 
317                                         FROM `contact` 
318                                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
319                                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
320                                         AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
321                                         $sql_extra
322                                         AND `user`.`account_expired` = 0 LIMIT 1",
323                                         dbesc(NETWORK_DFRN),
324                                         dbesc($nickname)
325                                 );
326
327                                 if(count($x)) {
328                                         if($owner['page-flags'] == PAGE_COMMUNITY && ! $x[0]['writable']) {
329                                                 q("update contact set writable = 1 where id = %d limit 1",
330                                                         intval($x[0]['id'])
331                                                 );
332                                                 $x[0]['writable'] = 1;
333                                         }
334
335                                         $ssl_policy = get_config('system','ssl_policy');
336                                         fix_contact_ssl_policy($x[0],$ssl_policy);
337
338                                         // If we are setup as a soapbox we aren't accepting input from this person
339
340                                         if($x[0]['page-flags'] == PAGE_SOAPBOX)
341                                                 break;
342
343                                         require_once('library/simplepie/simplepie.inc');
344                                         logger('mod-delivery: local delivery');
345                                         local_delivery($x[0],$atom);
346                                         break;
347                                 }
348                         }
349
350                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
351
352                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
353
354                         if($deliver_status == (-1)) {
355                                 logger('notifier: delivery failed: queuing message');
356                                 add_to_queue($contact['id'],NETWORK_DFRN,$atom);
357                         }
358                         break;
359
360                 case NETWORK_OSTATUS :
361
362                         // Do not send to otatus if we are not configured to send to public networks
363                         if($owner['prvnets'])
364                                 break;
365                         if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
366                                 break;
367
368                         // only send salmon if public - e.g. if it's ok to notify
369                         // a public hub, it's ok to send a salmon
370
371                         if(($public_message) && (! $expire)) {
372                                 $slaps = array();
373
374                                 foreach($items as $item) {
375                                         if(! $item['parent'])
376                                                 continue;
377
378                                         // private emails may be in included in public conversations. Filter them.
379                                         if(($public_message) && $item['private'])
380                                                 continue;
381
382                                         $item_contact = get_item_contact($item,$icontacts);
383                                         if(! $item_contact)
384                                                 continue;
385
386                                         if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire)) 
387                                                 $slaps[] = atom_entry($item,'html',null,$owner,true);
388                                 }
389
390                                 logger('notifier: slapdelivery: ' . $contact['name']);
391                                 foreach($slaps as $slappy) {
392                                         if($contact['notify']) {
393                                                 $deliver_status = slapper($owner,$contact['notify'],$slappy);
394                                                 if($deliver_status == (-1)) {
395                                                         // queue message for redelivery
396                                                         add_to_queue($contact['id'],NETWORK_OSTATUS,$slappy);
397                                                 }
398                                         }
399                                 }
400                         }
401
402                         break;
403
404                 case NETWORK_MAIL :
405                 case NETWORK_MAIL2:
406
407                         if(get_config('system','dfrn_only'))
408                                 break;
409                         // WARNING: does not currently convert to RFC2047 header encodings, etc.
410
411                         $addr = $contact['addr'];
412                         if(! strlen($addr))
413                                 break;
414
415                         if($cmd === 'wall-new' || $cmd === 'comment-new') {
416
417                                 $it = null;
418                                 if($cmd === 'wall-new') 
419                                         $it = $items[0];
420                                 else {
421                                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", 
422                                                 intval($argv[2]),
423                                                 intval($uid)
424                                         );
425                                         if(count($r))
426                                                 $it = $r[0];
427                                 }
428                                 if(! $it)
429                                         break;
430                                         
431
432                                 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
433                                         intval($uid)
434                                 );
435                                 if(! count($local_user))
436                                         break;
437                                         
438                                 $reply_to = '';
439                                 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
440                                         intval($uid)
441                                 );
442                                 if($r1 && $r1[0]['reply_to'])
443                                         $reply_to = $r1[0]['reply_to'];
444
445                                 $subject  = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
446
447                                 // only expose our real email address to true friends
448
449                                 if(($contact['rel'] == CONTACT_IS_FRIEND) && (! $contact['blocked']))
450                                         $headers  = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $local_user[0]['email'] . '>' . "\n";
451                                 else
452                                         $headers  = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . t('noreply') . '@' . $a->get_hostname() . '>' . "\n";
453
454                                 if($reply_to)
455                                         $headers .= 'Reply-to: ' . $reply_to . "\n";
456
457                                 // for testing purposes: Collect exported mails
458                                 // $file = tempnam("/tmp/friendica/", "mail-out-");
459                                 // file_put_contents($file, json_encode($it));
460
461                                 $headers .= 'Message-Id: <' . iri2msgid($it['uri']). '>' . "\n";
462
463                                 //logger("Mail: uri: ".$it['uri']." parent-uri ".$it['parent-uri'], LOGGER_DEBUG);
464                                 //logger("Mail: Data: ".print_r($it, true), LOGGER_DEBUG);
465                                 //logger("Mail: Data: ".print_r($it, true), LOGGER_DATA);
466
467                                 if($it['uri'] !== $it['parent-uri']) {
468                                         $headers .= 'References: <' . iri2msgid($it['parent-uri']) . '>' . "\n";
469                                         if(!strlen($it['title'])) {
470                                                 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
471                                                         dbesc($it['parent-uri']));
472
473                                                 if(count($r) AND ($r[0]['title'] != ''))
474                                                         $subject = $r[0]['title'];
475                                         }
476                                         if(strncasecmp($subject,'RE:',3))
477                                                 $subject = 'Re: '.$subject;
478                                 }
479                                 email_send($addr, $subject, $headers, $it);
480                         }
481                         break;
482
483                 case NETWORK_DIASPORA :
484                         if($public_message)
485                                 $loc = 'public batch ' . $contact['batch'];
486                         else 
487                                 $loc = $contact['name'];
488
489                         logger('delivery: diaspora batch deliver: ' . $loc);
490
491                         if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
492                                 break;
493
494                         if((! $contact['pubkey']) && (! $public_message))
495                                 break;
496
497                         if($target_item['verb'] === ACTIVITY_DISLIKE) {
498                                 // unsupported
499                                 break;
500                         }
501                         elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
502                         logger('delivery: diaspora retract: ' . $loc);
503                                 // diaspora delete, 
504                                 diaspora_send_retraction($target_item,$owner,$contact,$public_message);
505                                 break;
506                         }
507                         elseif($target_item['parent'] != $target_item['id']) {
508
509                         logger('delivery: diaspora relay: ' . $loc);
510
511                                 // we are the relay - send comments, likes and unlikes to our conversants
512                                 diaspora_send_relay($target_item,$owner,$contact,$public_message);
513                                 break;
514                         }               
515                         elseif(($top_level) && (! $walltowall)) {
516                                 // currently no workable solution for sending walltowall
517                                 logger('delivery: diaspora status: ' . $loc);
518                                 diaspora_send_status($target_item,$owner,$contact,$public_message);
519                                 break;
520                         }
521
522                         logger('delivery: diaspora unknown mode: ' . $contact['name']);
523
524                         break;
525
526                 case NETWORK_FEED :
527                 case NETWORK_FACEBOOK :
528                         if(get_config('system','dfrn_only'))
529                                 break;
530                 default:
531                         break;
532         }
533
534         return;
535 }
536
537 if (array_search(__file__,get_included_files())===0){
538   delivery_run($argv,$argc);
539   killme();
540 }