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