]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
over-ride comment box
[friendica.git] / include / notifier.php
1 <?php
2 require_once("boot.php");
3
4 function notifier_run($argv, $argc){
5         global $a, $db;
6
7         if(is_null($a)){
8                 $a = new App;
9         }
10   
11         if(is_null($db)) {
12                 @include(".htconfig.php");
13                 require_once("dba.php");
14                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
15                         unset($db_host, $db_user, $db_pass, $db_data);
16         }
17
18         require_once("session.php");
19         require_once("datetime.php");
20         require_once('include/items.php');
21         require_once('include/bbcode.php');
22
23         load_hooks();
24
25         if($argc < 3)
26                 return;
27
28         $a->set_baseurl(get_config('system','url'));
29
30         logger('notifier: invoked: ' . print_r($argv,true));
31
32         $cmd = $argv[1];
33
34         switch($cmd) {
35
36                 case 'mail':
37                 default:
38                         $item_id = intval($argv[2]);
39                         if(! $item_id){
40                                 return;
41                         }
42                         break;
43         }
44
45         $expire = false;
46         $top_level = false;
47         $recipients = array();
48         $url_recipients = array();
49
50         if($cmd === 'mail') {
51
52                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
53                                 intval($item_id)
54                 );
55                 if(! count($message)){
56                         return;
57                 }
58                 $uid = $message[0]['uid'];
59                 $recipients[] = $message[0]['contact-id'];
60                 $item = $message[0];
61
62         }
63         elseif($cmd === 'expire') {
64                 $expire = true;
65                 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1 
66                         AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
67                         intval($item_id)
68                 );
69                 $uid = $item_id;
70                 $item_id = 0;
71                 if(! count($items))
72                         return;
73         }
74         else {
75
76                 // find ancestors
77                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
78                         intval($item_id)
79                 );
80
81                 if((! count($r)) || (! intval($r[0]['parent']))) {
82                         return;
83                 }
84
85                 $parent_item = $r[0];
86                 $parent_id = intval($r[0]['parent']);
87                 $uid = $r[0]['uid'];
88                 $updated = $r[0]['edited'];
89
90                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
91                         intval($parent_id)
92                 );
93
94                 if(! count($items)) {
95                         return;
96                 }
97
98                 // avoid race condition with deleting entries
99
100                 if($items[0]['deleted']) {
101                         foreach($items as $item)
102                                 $item['deleted'] = 1;
103                 }
104
105                 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
106                         $top_level = true;
107         }
108
109         $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags` 
110                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
111                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
112                 intval($uid)
113         );
114
115         if(! count($r))
116                 return;
117
118         $owner = $r[0];
119
120         $hub = get_config('system','huburl');
121
122         // If this is a public conversation, notify the feed hub
123         $notify_hub = true;
124
125         // fill this in with a single salmon slap if applicable
126         $slap = '';
127
128         if($cmd != 'mail') {
129
130                 require_once('include/group.php');
131
132                 $parent = $items[0];
133
134                 if($parent['type'] === 'remote' && (! $expire)) {
135                         // local followup to remote post
136                         $followup = true;
137                         $notify_hub = false; // not public
138                         $conversant_str = dbesc($parent['contact-id']);
139                 }
140                 else {
141                         $followup = false;
142
143                         if((strlen($parent['allow_cid'])) 
144                                 || (strlen($parent['allow_gid'])) 
145                                 || (strlen($parent['deny_cid'])) 
146                                 || (strlen($parent['deny_gid']))) {
147                                 $notify_hub = false; // private recipients, not public
148                         }
149
150                         $allow_people = expand_acl($parent['allow_cid']);
151                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
152                         $deny_people  = expand_acl($parent['deny_cid']);
153                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
154
155                         $conversants = array();
156
157                         foreach($items as $item) {
158                                 $recipients[] = $item['contact-id'];
159                                 $conversants[] = $item['contact-id'];
160                                 // pull out additional tagged people to notify (if public message)
161                                 if($notify_hub && strlen($item['inform'])) {
162                                         $people = explode(',',$item['inform']);
163                                         foreach($people as $person) {
164                                                 if(substr($person,0,4) === 'cid:') {
165                                                         $recipients[] = intval(substr($person,4));
166                                                         $conversants[] = intval(substr($person,4));
167                                                 }
168                                                 else {
169                                                         $url_recipients[] = substr($person,4);
170                                                 }
171                                         }
172                                 }
173                         }
174
175                         logger('notifier: url_recipients' . print_r($url_recipients,true));
176
177                         $conversants = array_unique($conversants);
178
179
180                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
181                         $deny = array_unique(array_merge($deny_people,$deny_groups));
182                         $recipients = array_diff($recipients,$deny);
183
184                         $conversant_str = dbesc(implode(', ',$conversants));
185                 }
186
187                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
188
189
190                 if(count($r))
191                         $contacts = $r;
192         }
193
194         $feed_template = load_view_file('view/atom_feed.tpl');
195         $mail_template = load_view_file('view/atom_mail.tpl');
196
197         $atom = '';
198         $slaps = array();
199
200         $hubxml = feed_hublinks();
201
202         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
203
204         if(strlen($birthday))
205                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
206
207         $atom .= replace_macros($feed_template, array(
208                         '$version'      => xmlify(FRIENDIKA_VERSION),
209                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
210                         '$feed_title'   => xmlify($owner['name']),
211                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
212                         '$hub'          => $hubxml,
213                         '$salmon'       => '',  // private feed, we don't use salmon here
214                         '$name'         => xmlify($owner['name']),
215                         '$profile_page' => xmlify($owner['url']),
216                         '$photo'        => xmlify($owner['photo']),
217                         '$thumb'        => xmlify($owner['thumb']),
218                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
219                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
220                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
221                         '$birthday'     => $birthday
222         ));
223
224         if($cmd === 'mail') {
225                 $notify_hub = false;  // mail is  not public
226
227                 $atom .= replace_macros($mail_template, array(
228                         '$name'         => xmlify($owner['name']),
229                         '$profile_page' => xmlify($owner['url']),
230                         '$thumb'        => xmlify($owner['thumb']),
231                         '$item_id'      => xmlify($item['uri']),
232                         '$subject'      => xmlify($item['title']),
233                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
234                         '$content'      => xmlify($item['body']),
235                         '$parent_id'    => xmlify($item['parent-uri'])
236                 ));
237         }
238         else {
239                 if($followup) {
240                         foreach($items as $item) {  // there is only one item
241                                 if(! $item['parent'])
242                                         continue;
243                                 if($item['id'] == $item_id) {
244                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
245                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
246                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
247                                 }
248                         }
249                 }
250                 else {
251                         foreach($items as $item) {
252
253                                 if(! $item['parent'])
254                                         continue;
255
256                                 $contact = get_item_contact($item,$contacts);
257                                 if(! $contact)
258                                         continue;
259
260                                 $atom .= atom_entry($item,'text',$contact,$owner,true);
261
262                                 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire)) 
263                                         $slaps[] = atom_entry($item,'html',$contact,$owner,true);
264                         }
265                 }
266         }
267         $atom .= '</feed>' . "\r\n";
268
269         logger('notifier: ' . $atom, LOGGER_DATA);
270
271         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
272
273         // If this is a public message and pubmail is set on the parent, include all your email contacts
274
275         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
276
277         if(! $mail_disabled) {
278                 if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid'])) 
279                         && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid'])) 
280                         && (intval($parent_item['pubmail']))) {
281                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
282                                 intval($uid),
283                                 dbesc(NETWORK_MAIL)
284                         );
285                         if(count($r)) {
286                                 foreach($r as $rr)
287                                         $recipients[] = $rr['id'];
288                         }
289                 }
290         }
291
292         if($followup)
293                 $recip_str = $parent['contact-id'];
294         else
295                 $recip_str = implode(', ', $recipients);
296
297         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
298                 dbesc($recip_str)
299         );
300
301         // delivery loop
302
303         require_once('include/salmon.php');
304
305         if(count($r)) {
306                 foreach($r as $contact) {
307                         if($contact['self'])
308                                 continue;
309
310                         $deliver_status = 0;
311
312                         switch($contact['network']) {
313                                 case 'dfrn':
314                                         logger('notifier: dfrndelivery: ' . $contact['name']);
315                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
316
317                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
318         
319                                         if($deliver_status == (-1)) {
320                                                 logger('notifier: delivery failed: queuing message');
321                                                 // queue message for redelivery
322                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
323                                                         VALUES ( %d, '%s', '%s', '%s') ",
324                                                         intval($contact['id']),
325                                                         dbesc(datetime_convert()),
326                                                         dbesc(datetime_convert()),
327                                                         dbesc($atom)
328                                                 );
329                                         }
330                                         break;
331                                 case 'stat':
332                                         if($followup && $contact['notify']) {
333                                                 logger('notifier: slapdelivery: ' . $contact['name']);
334                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
335
336                                                 if($deliver_status == (-1)) {
337                                                         // queue message for redelivery
338                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
339                                                                 VALUES ( %d, '%s', '%s', '%s') ",
340                                                                 intval($contact['id']),
341                                                                 dbesc(datetime_convert()),
342                                                                 dbesc(datetime_convert()),
343                                                                 dbesc($slap)
344                                                         );
345
346                                                 }
347         
348
349                                         }
350                                         else {
351
352                                                 // only send salmon if public - e.g. if it's ok to notify
353                                                 // a public hub, it's ok to send a salmon
354
355                                                 if((count($slaps)) && ($notify_hub) && (! $expire)) {
356                                                         logger('notifier: slapdelivery: ' . $contact['name']);
357                                                         foreach($slaps as $slappy) {
358                                                                 if($contact['notify']) {
359                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
360                                                                         if($deliver_status == (-1)) {
361                                                                                 // queue message for redelivery
362                                                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
363                                                                                         VALUES ( %d, '%s', '%s', '%s') ",
364                                                                                         intval($contact['id']),
365                                                                                         dbesc(datetime_convert()),
366                                                                                         dbesc(datetime_convert()),
367                                                                                         dbesc($slappy)
368                                                                                 );                                                              
369                                                                         }
370                                                                 }
371                                                         }
372                                                 }
373                                         }
374                                         break;
375                                 case 'mail':
376
377                                         // WARNING: does not currently convert to RFC2047 header encodings, etc.
378
379                                         $addr = $contact['addr'];
380                                         if(! strlen($addr))
381                                                 break;
382
383                                         if($cmd === 'wall-new' || $cmd === 'comment-new') {
384
385                                                 $it = null;
386                                                 if($cmd === 'wall-new') 
387                                                         $it = $items[0];
388                                                 else {
389                                                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", 
390                                                                 intval($argv[2]),
391                                                                 intval($uid)
392                                                         );
393                                                         if(count($r))
394                                                                 $it = $r[0];
395                                                 }
396                                                 if(! $it)
397                                                         break;
398                                                 
399
400
401                                                 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
402                                                         intval($uid)
403                                                 );
404                                                 if(! count($local_user))
405                                                         break;
406                                                 
407                                                 $reply_to = '';
408                                                 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
409                                                         intval($uid)
410                                                 );
411                                                 if($r1 && $r1[0]['reply_to'])
412                                                         $reply_to = $r1[0]['reply_to'];
413         
414                                                 $subject  = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
415                                                 $headers  = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
416
417                                                 if($reply_to)
418                                                         $headers .= 'Reply-to: ' . $reply_to . "\n";
419
420                                                 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
421
422                                                 if($it['uri'] !== $it['parent-uri']) {
423                                                         $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
424                                                         if(! strlen($it['title'])) {
425                                                                 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
426                                                                         dbesc($it['parent-uri'])
427                                                                 );
428                                                                 if(count($r)) {
429                                                                         $subtitle = $r[0]['title'];
430                                                                         if($subtitle) {
431                                                                                 if(strncasecmp($subtitle,'RE:',3))
432                                                                                         $subject = $subtitle;
433                                                                                 else
434                                                                                         $subject = 'Re: ' . $subtitle;
435                                                                         }
436                                                                 }
437                                                         }
438                                                 }
439
440                                                 $headers .= 'MIME-Version: 1.0' . "\n";
441                                                 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
442                                                 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
443                                                 $html    = prepare_body($it);
444                                                 $message = '<html><body>' . $html . '</body></html>';
445                                                 logger('notifier: email delivery to ' . $addr);
446                                                 mail($addr, $subject, $message, $headers);
447                                         }
448                                         break;
449                                 case 'dspr':
450                                 case 'feed':
451                                 case 'face':
452                                 default:
453                                         break;
454                         }
455                 }
456         }
457                 
458         // send additional slaps to mentioned remote tags (@foo@example.com)
459
460         if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
461                 foreach($url_recipients as $url) {
462                         if($url) {
463                                 logger('notifier: urldelivery: ' . $url);
464                                 $deliver_status = slapper($owner,$url,$slap);
465                                 // TODO: redeliver/queue these items on failure, though there is no contact record
466                         }
467                 }
468         }
469
470         if((strlen($hub)) && ($notify_hub)) {
471                 $hubs = explode(',', $hub);
472                 if(count($hubs)) {
473                         foreach($hubs as $h) {
474                                 $h = trim($h);
475                                 if(! strlen($h))
476                                         continue;
477                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
478                                 post_url($h,$params);
479                                 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
480                                 if(count($hubs) > 1)
481                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
482                         }
483                 }
484         }
485
486         if($notify_hub) {
487
488                 /**
489                  *
490                  * If you have less than 150 dfrn friends and it's a public message,
491                  * we'll just go ahead and push them out securely with dfrn/rino.
492                  * If you've got more than that, you'll have to rely on PuSH delivery.
493                  *
494                  */
495
496                 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
497                                 
498                 /**
499                  *
500                  * Only get the bare essentials and go back for the full record. 
501                  * If you've got a lot of friends and we grab all the details at once it could exhaust memory. 
502                  *
503                  */
504
505                 $r = q("SELECT `id`, `name` FROM `contact` 
506                         WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
507                         AND `rel` != %d ",
508                         intval($owner['uid']),
509                         intval(REL_FAN)
510                 );
511
512                 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
513
514                         foreach($r as $rr) {
515
516                                 /* Don't deliver to folks who have already been delivered to */
517
518                                 if(! in_array($rr['id'], $conversants)) {
519                                         $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
520                                                         intval($rr['id'])
521                                         );
522
523                                         if(count($n)) {
524                                         
525                                                 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
526                                                 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
527                                         }
528                                 }
529                                 else
530                                         logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
531                         }
532                 }
533         }
534
535         return;
536 }
537
538 if (array_search(__file__,get_included_files())===0){
539   echo "run!";
540   notifier_run($argv,$argc);
541   killme();
542 }