]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
Support relayable_retractions that are relayed to us by the top-level post owner
[friendica.git] / include / notifier.php
1 <?php
2
3 require_once("boot.php");
4 require_once('include/queue_fn.php');
5 require_once('include/html2plain.php');
6
7 /*
8  * This file was at one time responsible for doing all deliveries, but this caused
9  * big problems on shared hosting systems, where the process might get killed by the 
10  * hosting provider and nothing would get delivered. 
11  * It now only delivers one message under certain cases, and invokes a queued
12  * delivery mechanism (include/deliver.php) to deliver individual contacts at 
13  * controlled intervals.
14  * This has a much better chance of surviving random processes getting killed
15  * by the hosting provider. 
16  * A lot of this code is duplicated in include/deliver.php until we have time to go back
17  * and re-structure the delivery procedure based on the obstacles that have been thrown at 
18  * us by hosting providers. 
19  */
20
21 function notifier_run($argv, $argc){
22         global $a, $db;
23
24         if(is_null($a)){
25                 $a = new App;
26         }
27   
28         if(is_null($db)) {
29                 @include(".htconfig.php");
30                 require_once("dba.php");
31                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
32                         unset($db_host, $db_user, $db_pass, $db_data);
33         }
34
35         require_once("session.php");
36         require_once("datetime.php");
37         require_once('include/items.php');
38         require_once('include/bbcode.php');
39         require_once('include/email.php');
40         load_config('config');
41         load_config('system');
42
43         load_hooks();
44
45         if($argc < 3)
46                 return;
47
48         $a->set_baseurl(get_config('system','url'));
49
50         logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
51
52         $cmd = $argv[1];
53
54         switch($cmd) {
55                 case 'mail':
56                 default:
57                         $item_id = intval($argv[2]);
58                         if(! $item_id){
59                                 return;
60                         }
61                         break;
62         }
63
64         $expire = false;
65         $mail = false;
66         $fsuggest = false;
67         $top_level = false;
68         $recipients = array();
69         $url_recipients = array();
70
71         $normal_mode = true;
72
73         if($cmd === 'mail') {
74                 $normal_mode = false;
75                 $mail = true;
76                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
77                                 intval($item_id)
78                 );
79                 if(! count($message)){
80                         return;
81                 }
82                 $uid = $message[0]['uid'];
83                 $recipients[] = $message[0]['contact-id'];
84                 $item = $message[0];
85
86         }
87         elseif($cmd === 'expire') {
88                 $normal_mode = false;
89                 $expire = true;
90                 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1 
91                         AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
92                         intval($item_id)
93                 );
94                 $uid = $item_id;
95                 $item_id = 0;
96                 if(! count($items))
97                         return;
98         }
99         elseif($cmd === 'suggest') {
100                 $normal_mode = false;
101                 $fsuggest = true;
102
103                 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
104                         intval($item_id)
105                 );
106                 if(! count($suggest))
107                         return;
108                 $uid = $suggest[0]['uid'];
109                 $recipients[] = $suggest[0]['cid'];
110                 $item = $suggest[0];
111         }
112         else {
113
114                 // find ancestors
115                 $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
116                         intval($item_id)
117                 );
118
119                 if((! count($r)) || (! intval($r[0]['parent']))) {
120                         return;
121                 }
122
123                 $target_item = $r[0];
124                 $parent_id = intval($r[0]['parent']);
125                 $uid = $r[0]['uid'];
126                 $updated = $r[0]['edited'];
127
128                 // The following seems superfluous. We've already checked for "if (! intval($r[0]['parent']))" a few lines up
129                 if(! $parent_id)
130                         return;
131
132                 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` 
133                         FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
134                         intval($parent_id)
135                 );
136
137                 if(! count($items)) {
138                         return;
139                 }
140
141                 // avoid race condition with deleting entries
142
143                 if($items[0]['deleted']) {
144                         foreach($items as $item)
145                                 $item['deleted'] = 1;
146                 }
147
148                 if((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
149                         logger('notifier: top level post');
150                         $top_level = true;
151                 }
152
153         }
154
155         $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`, 
156                 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, 
157                 `user`.`page-flags`, `user`.`prvnets`
158                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
159                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
160                 intval($uid)
161         );
162
163         if(! count($r))
164                 return;
165
166         $owner = $r[0];
167
168         $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
169
170         $hub = get_config('system','huburl');
171
172         // If this is a public conversation, notify the feed hub
173         $public_message = true;
174
175         // fill this in with a single salmon slap if applicable
176         $slap = '';
177
178         if(! ($mail || $fsuggest)) {
179
180                 require_once('include/group.php');
181
182                 $parent = $items[0];
183
184                 // This is IMPORTANT!!!!
185
186                 // We will only send a "notify owner to relay" or followup message if the referenced post
187                 // originated on our system by virtue of having our hostname somewhere
188                 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
189
190                 // if $parent['wall'] == 1 we will already have the parent message in our array
191                 // and we will relay the whole lot.
192  
193                 // expire sends an entire group of expire messages and cannot be forwarded.
194                 // However the conversation owner will be a part of the conversation and will 
195                 // be notified during this run.
196                 // Other DFRN conversation members will be alerted during polled updates.
197
198
199
200                 // Diaspora members currently are not notified of expirations, and other networks have
201                 // either limited or no ability to process deletions. We should at least fix Diaspora 
202                 // by stringing togther an array of retractions and sending them onward.
203                  
204         
205                 $localhost = str_replace('www.','',$a->get_hostname());
206                 if(strpos($localhost,':'))
207                         $localhost = substr($localhost,0,strpos($localhost,':'));
208
209                 /**
210                  *
211                  * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes 
212                  * have been known to cause runaway conditions which affected several servers, along with 
213                  * permissions issues. 
214                  *
215                  */
216  
217                 $relay_to_owner = false;
218
219                 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
220                         $relay_to_owner = true;
221                 }
222
223
224                 if(($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && (! $top_level)) {
225                         $relay_to_owner = true;                 
226                 } 
227
228                 // until the 'origin' flag has been in use for several months
229                 // we will just use it as a fallback test
230                 // later we will be able to use it as the primary test of whether or not to relay.
231
232                 if(! $target_item['origin'])
233                         $relay_to_owner = false;
234
235                 if($parent['origin'])
236                         $relay_to_owner = false;
237
238
239
240                 if($relay_to_owner) {
241                         logger('notifier: followup', LOGGER_DEBUG);
242                         // local followup to remote post
243                         $followup = true;
244                         $public_message = false; // not public
245                         $conversant_str = dbesc($parent['contact-id']);
246                 }
247                 else {
248                         $followup = false;
249
250                         // don't send deletions onward for other people's stuff
251
252                         if($target_item['deleted'] && (! intval($target_item['wall']))) {
253                                 logger('notifier: ignoring delete notification for non-wall item');
254                                 return;
255                         }
256
257                         if((strlen($parent['allow_cid'])) 
258                                 || (strlen($parent['allow_gid'])) 
259                                 || (strlen($parent['deny_cid'])) 
260                                 || (strlen($parent['deny_gid']))) {
261                                 $public_message = false; // private recipients, not public
262                         }
263
264                         $allow_people = expand_acl($parent['allow_cid']);
265                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
266                         $deny_people  = expand_acl($parent['deny_cid']);
267                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
268
269                         // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
270                         // a delivery fork. private groups (forum_mode == 2) do not uplink
271
272                         if((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
273                                 proc_run('php','include/notifier','uplink',$item_id);
274                         }
275
276                         $conversants = array();
277
278                         foreach($items as $item) {
279                                 $recipients[] = $item['contact-id'];
280                                 $conversants[] = $item['contact-id'];
281                                 // pull out additional tagged people to notify (if public message)
282                                 if($public_message && strlen($item['inform'])) {
283                                         $people = explode(',',$item['inform']);
284                                         foreach($people as $person) {
285                                                 if(substr($person,0,4) === 'cid:') {
286                                                         $recipients[] = intval(substr($person,4));
287                                                         $conversants[] = intval(substr($person,4));
288                                                 }
289                                                 else {
290                                                         $url_recipients[] = substr($person,4);
291                                                 }
292                                         }
293                                 }
294                         }
295
296                         logger('notifier: url_recipients' . print_r($url_recipients,true));
297
298                         $conversants = array_unique($conversants);
299
300
301                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
302                         $deny = array_unique(array_merge($deny_people,$deny_groups));
303                         $recipients = array_diff($recipients,$deny);
304
305                         $conversant_str = dbesc(implode(', ',$conversants));
306                 }
307
308                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0");
309
310                 if(count($r))
311                         $contacts = $r;
312         }
313
314         $feed_template = get_markup_template('atom_feed.tpl');
315         $mail_template = get_markup_template('atom_mail.tpl');
316
317         $atom = '';
318         $slaps = array();
319
320         $hubxml = feed_hublinks();
321
322         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
323
324         if(strlen($birthday))
325                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
326
327         $atom .= replace_macros($feed_template, array(
328                         '$version'      => xmlify(FRIENDICA_VERSION),
329                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
330                         '$feed_title'   => xmlify($owner['name']),
331                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
332                         '$hub'          => $hubxml,
333                         '$salmon'       => '',  // private feed, we don't use salmon here
334                         '$name'         => xmlify($owner['name']),
335                         '$profile_page' => xmlify($owner['url']),
336                         '$photo'        => xmlify($owner['photo']),
337                         '$thumb'        => xmlify($owner['thumb']),
338                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
339                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
340                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
341                         '$birthday'     => $birthday,
342                         '$community'    => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
343
344         ));
345
346         if($mail) {
347                 $public_message = false;  // mail is  not public
348
349                 $body = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']);
350
351                 $atom .= replace_macros($mail_template, array(
352                         '$name'         => xmlify($owner['name']),
353                         '$profile_page' => xmlify($owner['url']),
354                         '$thumb'        => xmlify($owner['thumb']),
355                         '$item_id'      => xmlify($item['uri']),
356                         '$subject'      => xmlify($item['title']),
357                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
358                         '$content'      => xmlify($body),
359                         '$parent_id'    => xmlify($item['parent-uri'])
360                 ));
361         }
362         elseif($fsuggest) {
363                 $public_message = false;  // suggestions are not public
364
365                 $sugg_template = get_markup_template('atom_suggest.tpl');
366
367                 $atom .= replace_macros($sugg_template, array(
368                         '$name'         => xmlify($item['name']),
369                         '$url'          => xmlify($item['url']),
370                         '$photo'        => xmlify($item['photo']),
371                         '$request'      => xmlify($item['request']),
372                         '$note'         => xmlify($item['note'])
373                 ));
374
375                 // We don't need this any more
376
377                 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
378                         intval($item['id'])
379                 );
380
381         }
382         else {
383                 if($followup) {
384                         foreach($items as $item) {  // there is only one item
385                                 if(! $item['parent'])
386                                         continue;
387                                 if($item['id'] == $item_id) {
388                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
389                                         $slap  = atom_entry($item,'html',null,$owner,false);
390                                         $atom .= atom_entry($item,'text',null,$owner,false);
391                                 }
392                         }
393                 }
394                 else {
395                         foreach($items as $item) {
396
397                                 if(! $item['parent'])
398                                         continue;
399
400                                 // private emails may be in included in public conversations. Filter them.
401
402                                 if(($public_message) && $item['private'])
403                                         continue;
404
405
406                                 $contact = get_item_contact($item,$contacts);
407
408                                 if(! $contact)
409                                         continue;
410
411                                 if($normal_mode) {
412
413                                         // we only need the current item, but include the parent because without it
414                                         // older sites without a corresponding dfrn_notify change may do the wrong thing.
415
416                                     if($item_id == $item['id'] || $item['id'] == $item['parent'])
417                                                 $atom .= atom_entry($item,'text',null,$owner,true);
418                                 }
419                                 else
420                                         $atom .= atom_entry($item,'text',null,$owner,true);
421
422                                 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire)) 
423                                         $slaps[] = atom_entry($item,'html',null,$owner,true);
424                         }
425                 }
426         }
427         $atom .= '</feed>' . "\r\n";
428
429         logger('notifier: ' . $atom, LOGGER_DATA);
430
431         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
432
433         // If this is a public message and pubmail is set on the parent, include all your email contacts
434
435         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
436
437         if(! $mail_disabled) {
438                 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid'])) 
439                         && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid'])) 
440                         && (intval($target_item['pubmail']))) {
441                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
442                                 intval($uid),
443                                 dbesc(NETWORK_MAIL)
444                         );
445                         if(count($r)) {
446                                 foreach($r as $rr)
447                                         $recipients[] = $rr['id'];
448                         }
449                 }
450         }
451
452         if($followup)
453                 $recip_str = $parent['contact-id'];
454         else
455                 $recip_str = implode(', ', $recipients);
456
457         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
458                 dbesc($recip_str)
459         );
460
461
462         require_once('include/salmon.php');
463
464         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
465
466         // delivery loop
467
468         if(count($r)) {
469
470                 foreach($r as $contact) {
471                         if((! $mail) && (! $fsuggest) && (! $followup) && (! $contact['self'])) {
472                                 if(($contact['network'] === NETWORK_DIASPORA) && ($public_message))
473                                         continue;
474                                 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
475                                         dbesc($cmd),
476                                         intval($item_id),
477                                         intval($contact['id'])
478                                 );
479                         }
480                 }
481
482
483                 // This controls the number of deliveries to execute with each separate delivery process.
484                 // By default we'll perform one delivery per process. Assuming a hostile shared hosting
485                 // provider, this provides the greatest chance of deliveries if processes start getting 
486                 // killed. We can also space them out with the delivery_interval to also help avoid them 
487                 // getting whacked.
488
489                 // If $deliveries_per_process > 1, we will chain this number of multiple deliveries 
490                 // together into a single process. This will reduce the overall number of processes 
491                 // spawned for each delivery, but they will run longer. 
492
493                 $deliveries_per_process = intval(get_config('system','delivery_batch_count'));
494                 if($deliveries_per_process <= 0)
495                         $deliveries_per_process = 1;
496
497                 $this_batch = array();
498
499                 for($x = 0; $x < count($r); $x ++) {
500                         $contact = $r[$x];
501
502                         if($contact['self'])
503                                 continue;
504
505                         // potentially more than one recipient. Start a new process and space them out a bit.
506                         // we will deliver single recipient types of message and email recipients here. 
507                 
508                         if((! $mail) && (! $fsuggest) && (! $followup)) {
509
510                                 $this_batch[] = $contact['id'];
511
512                                 if(count($this_batch) == $deliveries_per_process) {
513                                         proc_run('php','include/delivery.php',$cmd,$item_id,$this_batch);
514                                         $this_batch = array();
515                                         if($interval)
516                                                 @time_sleep_until(microtime(true) + (float) $interval);
517                                 }
518                                 continue;
519                         }
520
521                         $deliver_status = 0;
522
523                         logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest");
524
525                         switch($contact['network']) {
526                                 case NETWORK_DFRN:
527
528                                         // perform local delivery if we are on the same site
529
530                                         $basepath =  implode('/', array_slice(explode('/',$contact['url']),0,3));
531
532                                         if(link_compare($basepath,$a->get_baseurl())) {
533
534                                                 $nickname = basename($contact['url']);
535                                                 if($contact['issued-id'])
536                                                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
537                                                 else
538                                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
539
540                                                 $x = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`, 
541                                                         `contact`.`pubkey` AS `cpubkey`, 
542                                                         `contact`.`prvkey` AS `cprvkey`, 
543                                                         `contact`.`thumb` AS `thumb`, 
544                                                         `contact`.`url` as `url`,
545                                                         `contact`.`name` as `senderName`,
546                                                         `user`.* 
547                                                         FROM `contact` 
548                                                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
549                                                         WHERE `contact`.`blocked` = 0 AND `contact`.`archive` = 0
550                                                         AND `contact`.`pending` = 0
551                                                         AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
552                                                         $sql_extra
553                                                         AND `user`.`account_expired` = 0 LIMIT 1",
554                                                         dbesc(NETWORK_DFRN),
555                                                         dbesc($nickname)
556                                                 );
557
558                                                 if(count($x)) {
559
560                                                         if($owner['page-flags'] == PAGE_COMMUNITY && ! $x[0]['writable']) {
561                                                                 q("update contact set writable = 1 where id = %d limit 1",
562                                                                         intval($x[0]['id'])
563                                                                 );
564                                                                 $x[0]['writable'] = 1;
565                                                         }
566
567                                                         // if contact's ssl policy changed, which we just determined
568                                                         // is on our own server, update our contact links
569                                                         
570                                                         $ssl_policy = get_config('system','ssl_policy');
571                                                         fix_contact_ssl_policy($x[0],$ssl_policy);
572
573                                                         // If we are setup as a soapbox we aren't accepting input from this person
574
575                                                         if($x[0]['page-flags'] == PAGE_SOAPBOX)
576                                                                 break;
577
578                                                         require_once('library/simplepie/simplepie.inc');
579                                                         logger('mod-delivery: local delivery');
580                                                         local_delivery($x[0],$atom);
581                                                         break;                                  
582                                                 }
583                                         }
584
585
586
587                                         logger('notifier: dfrndelivery: ' . $contact['name']);
588                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
589
590                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
591         
592                                         if($deliver_status == (-1)) {
593                                                 logger('notifier: delivery failed: queuing message');
594                                                 // queue message for redelivery
595                                                 add_to_queue($contact['id'],NETWORK_DFRN,$atom);
596                                         }
597                                         break;
598                                 case NETWORK_OSTATUS:
599
600                                         // Do not send to otatus if we are not configured to send to public networks
601                                         if($owner['prvnets'])
602                                                 break;
603                                         if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
604                                                 break;
605
606                                         if($followup && $contact['notify']) {
607                                                 logger('notifier: slapdelivery: ' . $contact['name']);
608                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
609
610                                                 if($deliver_status == (-1)) {
611                                                         // queue message for redelivery
612                                                         add_to_queue($contact['id'],NETWORK_OSTATUS,$slap);
613                                                 }
614                                         }
615                                         else {
616
617                                                 // only send salmon if public - e.g. if it's ok to notify
618                                                 // a public hub, it's ok to send a salmon
619
620                                                 if((count($slaps)) && ($public_message) && (! $expire)) {
621                                                         logger('notifier: slapdelivery: ' . $contact['name']);
622                                                         foreach($slaps as $slappy) {
623                                                                 if($contact['notify']) {
624                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
625                                                                         if($deliver_status == (-1)) {
626                                                                                 // queue message for redelivery
627                                                                                 add_to_queue($contact['id'],NETWORK_OSTATUS,$slappy);
628                                                                         }
629                                                                 }
630                                                         }
631                                                 }
632                                         }
633                                         break;
634
635                                 case NETWORK_MAIL:
636                                 case NETWORK_MAIL2:
637                                                 
638                                         if(get_config('system','dfrn_only'))
639                                                 break;
640
641                                         // WARNING: does not currently convert to RFC2047 header encodings, etc.
642
643                                         $addr = $contact['addr'];
644                                         if(! strlen($addr))
645                                                 break;
646
647                                         if($cmd === 'wall-new' || $cmd === 'comment-new') {
648
649                                                 $it = null;
650                                                 if($cmd === 'wall-new') 
651                                                         $it = $items[0];
652                                                 else {
653                                                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", 
654                                                                 intval($argv[2]),
655                                                                 intval($uid)
656                                                         );
657                                                         if(count($r))
658                                                                 $it = $r[0];
659                                                 }
660                                                 if(! $it)
661                                                         break;
662                                                 
663
664
665                                                 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
666                                                         intval($uid)
667                                                 );
668                                                 if(! count($local_user))
669                                                         break;
670                                                 
671                                                 $reply_to = '';
672                                                 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
673                                                         intval($uid)
674                                                 );
675                                                 if($r1 && $r1[0]['reply_to'])
676                                                         $reply_to = $r1[0]['reply_to'];
677
678                                                 $subject  = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
679
680                                                 // only expose our real email address to true friends
681
682                                                 if(($contact['rel'] == CONTACT_IS_FRIEND) && (! $contact['blocked']))
683                                                         $headers  = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $local_user[0]['email'] . '>' . "\n";
684                                                 else
685                                                         $headers  = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . t('noreply') . '@' . $a->get_hostname() . '>' . "\n";
686
687                                                 if($reply_to)
688                                                         $headers .= 'Reply-to: ' . $reply_to . "\n";
689
690                                                 // for testing purposes: Collect exported mails
691                                                 //$file = tempnam("/tmp/friendica/", "mail-out2-");
692                                                 //file_put_contents($file, json_encode($it));
693
694                                                 $headers .= 'Message-Id: <' . iri2msgid($it['uri']) . '>' . "\n";
695
696                                                 if($it['uri'] !== $it['parent-uri']) {
697                                                         $headers .= 'References: <' . iri2msgid($it['parent-uri']) . '>' . "\n";
698                                                         if(!strlen($it['title'])) {
699                                                                 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
700                                                                         dbesc($it['parent-uri']));
701
702                                                                 if(count($r) AND ($r[0]['title'] != ''))  
703                                                                         $subject = $r[0]['title'];
704                                                         }
705                                                         if(strncasecmp($subject,'RE:',3))
706                                                                 $subject = 'Re: '.$subject;
707                                                 }
708                                                 email_send($addr, $subject, $headers, $it);
709                                         }
710                                         break;
711                                 case NETWORK_DIASPORA:
712                                         require_once('include/diaspora.php');
713
714                                         if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')))
715                                                 break;
716
717                                         if($mail) {
718                                                 diaspora_send_mail($item,$owner,$contact);
719                                                 break;
720                                         }
721
722                                         if(! $normal_mode)
723                                                 break;
724
725                                         // special handling for followup to public post
726                                         // all other public posts processed as public batches further below
727
728                                         if($public_message) {
729                                                 if($followup)
730                                                         diaspora_send_followup($target_item,$owner,$contact, true);
731                                                 break;
732                                         }
733
734                                         if(! $contact['pubkey'])
735                                                 break;
736                                         
737                                         if($target_item['verb'] === ACTIVITY_DISLIKE) {
738                                                 // unsupported
739                                                 break;
740                                         }
741                                         elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
742                                                 // diaspora delete, 
743                                                 diaspora_send_retraction($target_item,$owner,$contact);
744                                                 break;
745                                         }
746                                         elseif($followup) {
747                                                 // send comments, likes and retractions of likes to owner to relay
748                                                 diaspora_send_followup($target_item,$owner,$contact);
749                                                 break;
750                                         }
751                                         elseif($target_item['parent'] != $target_item['id']) {
752                                                 // we are the relay - send comments, likes and unlikes to our conversants
753                                                 diaspora_send_relay($target_item,$owner,$contact);
754                                                 break;
755                                         }
756                                         elseif(($top_level) && (! $walltowall)) {
757                                                 // currently no workable solution for sending walltowall
758                                                 diaspora_send_status($target_item,$owner,$contact);
759                                                 break;
760                                         }
761
762                                         break;
763
764                                 case NETWORK_FEED:
765                                 case NETWORK_FACEBOOK:
766                                         if(get_config('system','dfrn_only'))
767                                                 break;
768                                 default:
769                                         break;
770                         }
771                 }
772         }
773                 
774         // send additional slaps to mentioned remote tags (@foo@example.com)
775
776         if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
777                 if(! get_config('system','dfrn_only')) {
778                         foreach($url_recipients as $url) {
779                                 if($url) {
780                                         logger('notifier: urldelivery: ' . $url);
781                                         $deliver_status = slapper($owner,$url,$slap);
782                                         // TODO: redeliver/queue these items on failure, though there is no contact record
783                                 }
784                         }
785                 }
786         }
787
788
789         if($public_message) {
790
791                 $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s' 
792                         AND `uid` = %d AND `rel` != %d group by `batch` ORDER BY rand() ",
793                         dbesc(NETWORK_DIASPORA),
794                         intval($owner['uid']),
795                         intval(CONTACT_IS_SHARING)
796                 );
797                         
798                 $r2 = q("SELECT `id`, `name`,`network` FROM `contact` 
799                         WHERE `network` in ( '%s', '%s')  AND `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
800                         AND `rel` != %d order by rand() ",
801                         dbesc(NETWORK_DFRN),
802                         dbesc(NETWORK_MAIL2),
803                         intval($owner['uid']),
804                         intval(CONTACT_IS_SHARING)
805                 );
806
807                 $r = array_merge($r2,$r1);
808
809                 if(count($r)) {
810                         logger('pubdeliver: ' . print_r($r,true), LOGGER_DEBUG);
811
812                         // throw everything into the queue in case we get killed
813
814                         foreach($r as $rr) {
815                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
816                                         q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
817                                                 dbesc($cmd),
818                                                 intval($item_id),
819                                                 intval($rr['id'])
820                                         );
821                                 }
822                         }
823
824                         foreach($r as $rr) {
825
826                                 // except for Diaspora batch jobs
827                                 // Don't deliver to folks who have already been delivered to
828
829                                 if(($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
830                                         logger('notifier: already delivered id=' . $rr['id']);
831                                         continue;
832                                 }
833
834                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
835                                         logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']); 
836                                         proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
837                                         if($interval)
838                                                 @time_sleep_until(microtime(true) + (float) $interval);
839                                 }
840                         }
841                 }
842
843
844                 if(strlen($hub)) {
845                         $hubs = explode(',', $hub);
846                         if(count($hubs)) {
847                                 foreach($hubs as $h) {
848                                         $h = trim($h);
849                                         if(! strlen($h))
850                                                 continue;
851                                         $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
852                                         post_url($h,$params);
853                                         logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
854                                         if(count($hubs) > 1)
855                                                 sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
856                                 }
857                         }
858                 }
859
860         }
861
862         logger('notifier: calling hooks', LOGGER_DEBUG);
863
864         if($normal_mode)
865                 call_hooks('notifier_normal',$target_item);
866
867         call_hooks('notifier_end',$target_item);
868
869         return;
870 }
871
872 if (array_search(__file__,get_included_files())===0){
873   notifier_run($argv,$argc);
874   killme();
875 }