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