]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
Merge branch 'fabrixxm-master'
[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         if($argc < 3)
24                 return;
25
26         $a->set_baseurl(get_config('system','url'));
27
28         logger('notifier: invoked: ' . print_r($argv,true));
29
30         $cmd = $argv[1];
31
32         switch($cmd) {
33
34                 case 'mail':
35                 default:
36                         $item_id = intval($argv[2]);
37                         if(! $item_id){
38                                 return;
39                         }
40                         break;
41         }
42
43         $recipients = array();
44         $url_recipients = array();
45
46         if($cmd === 'mail') {
47
48                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
49                                 intval($item_id)
50                 );
51                 if(! count($message)){
52                         return;
53                 }
54                 $uid = $message[0]['uid'];
55                 $recipients[] = $message[0]['contact-id'];
56                 $item = $message[0];
57
58         }
59         else {
60
61                 // find ancestors
62                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
63                         intval($item_id)
64                 );
65
66                 if((! count($r)) || (! intval($r[0]['parent']))) {
67                         return;
68                 }
69
70                 $parent_id = intval($r[0]['parent']);
71                 $uid = $r[0]['uid'];
72                 $updated = $r[0]['edited'];
73
74                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
75                         intval($parent_id)
76                 );
77
78                 if(! count($items)){
79                         return;
80                 }
81
82                 // avoid race condition with deleting entries
83
84                 if($items[0]['deleted']) {
85                         foreach($items as $item)
86                                 $item['deleted'] = 1;
87                 }
88         }
89
90         $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags` 
91                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
92                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
93                 intval($uid)
94         );
95
96         if(count($r))
97                 $owner = $r[0];
98         else {
99                 return;
100         }
101         $hub = get_config('system','huburl');
102
103         // If this is a public conversation, notify the feed hub
104         $notify_hub = true;
105
106         // fill this in with a single salmon slap if applicable
107         $slap = '';
108
109         if($cmd != 'mail') {
110
111                 require_once('include/group.php');
112
113                 $parent = $items[0];
114
115                 if($parent['type'] === 'remote') {
116                         // local followup to remote post
117                         $followup = true;
118                         $notify_hub = false; // not public
119                         $conversant_str = dbesc($parent['contact-id']);
120                 }
121                 else {
122                         $followup = false;
123
124                         if((strlen($parent['allow_cid'])) 
125                                 || (strlen($parent['allow_gid'])) 
126                                 || (strlen($parent['deny_cid'])) 
127                                 || (strlen($parent['deny_gid']))) {
128                                 $notify_hub = false; // private recipients, not public
129                         }
130
131                         $allow_people = expand_acl($parent['allow_cid']);
132                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
133                         $deny_people  = expand_acl($parent['deny_cid']);
134                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
135
136                         $conversants = array();
137
138                         foreach($items as $item) {
139                                 $recipients[] = $item['contact-id'];
140                                 $conversants[] = $item['contact-id'];
141                                 // pull out additional tagged people to notify (if public message)
142                                 if($notify_hub && strlen($item['inform'])) {
143                                         $people = explode(',',$item['inform']);
144                                         foreach($people as $person) {
145                                                 if(substr($person,0,4) === 'cid:') {
146                                                         $recipients[] = intval(substr($person,4));
147                                                         $conversants[] = intval(substr($person,4));
148                                                 }
149                                                 else {
150                                                         $url_recipients[] = substr($person,4);
151                                                 }
152                                         }
153                                 }
154                         }
155
156                         logger('notifier: url_recipients' . print_r($url_recipients,true));
157
158                         $conversants = array_unique($conversants);
159
160
161                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
162                         $deny = array_unique(array_merge($deny_people,$deny_groups));
163                         $recipients = array_diff($recipients,$deny);
164
165                         $conversant_str = dbesc(implode(', ',$conversants));
166                 }
167
168                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
169
170 //              if( ! count($r)){
171 //                      return;
172 //              }
173
174                 if(count($r))
175                         $contacts = $r;
176         }
177
178         $feed_template = load_view_file('view/atom_feed.tpl');
179         $mail_template = load_view_file('view/atom_mail.tpl');
180
181         $atom = '';
182         $hubxml = '';
183         $slaps = array();
184
185         if(strlen($hub)) {
186                 $hubs = explode(',', $hub);
187                 if(count($hubs)) {
188                         foreach($hubs as $h) {
189                                 $h = trim($h);
190                                 if(! strlen($h))
191                                         continue;
192                                 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
193                         }
194                 }
195         }
196
197         $atom .= replace_macros($feed_template, array(
198                         '$version'      => xmlify(FRIENDIKA_VERSION),
199                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
200                         '$feed_title'   => xmlify($owner['name']),
201                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
202                         '$hub'          => $hubxml,
203                         '$salmon'       => '',  // private feed, we don't use salmon here
204                         '$name'         => xmlify($owner['name']),
205                         '$profile_page' => xmlify($owner['url']),
206                         '$photo'        => xmlify($owner['photo']),
207                         '$thumb'        => xmlify($owner['thumb']),
208                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
209                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
210                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
211                         '$birthday'     => ''
212         ));
213
214         if($cmd === 'mail') {
215                 $notify_hub = false;  // mail is  not public
216
217                 $atom .= replace_macros($mail_template, array(
218                         '$name'         => xmlify($owner['name']),
219                         '$profile_page' => xmlify($owner['url']),
220                         '$thumb'        => xmlify($owner['thumb']),
221                         '$item_id'      => xmlify($item['uri']),
222                         '$subject'      => xmlify($item['title']),
223                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
224                         '$content'      => xmlify($item['body']),
225                         '$parent_id'    => xmlify($item['parent-uri'])
226                 ));
227         }
228         else {
229                 if($followup) {
230                         foreach($items as $item) {  // there is only one item
231                                 if(! $item['parent'])
232                                         continue;
233                                 if($item['id'] == $item_id) {
234                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
235                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
236                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
237                                 }
238                         }
239                 }
240                 else {
241                         foreach($items as $item) {
242                                 if(! $item['parent'])
243                                         continue;
244
245                                 $contact = get_item_contact($item,$contacts);
246                                 if(! $contact)
247                                         continue;
248
249                                 $atom   .= atom_entry($item,'text',$contact,$owner,true);
250
251                                 // There's a problem here - we *were* going to use salmon to provide semi-authenticated
252                                 // communication to OStatus, but unless we're the item author they won't verify.
253                                 // commented out for now, though we'll still send local replies (and any mentions 
254                                 // that they contain) upstream. Rethinking the problem space.
255  
256 //                              $slaps[] = atom_entry($item,'html',$contact,$owner,true);
257                         }
258                 }
259         }
260         $atom .= '</feed>' . "\r\n";
261
262         logger('notifier: ' . $atom, LOGGER_DATA);
263
264 //      logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
265
266         if($followup)
267                 $recip_str = $parent['contact-id'];
268         else
269                 $recip_str = implode(', ', $recipients);
270
271
272         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
273                 dbesc($recip_str)
274         );
275
276         // delivery loop
277
278         require_once('include/salmon.php');
279
280         if(count($r)) {
281                 foreach($r as $contact) {
282                         if($contact['self'])
283                                 continue;
284
285                         $deliver_status = 0;
286
287                         switch($contact['network']) {
288                                 case 'dfrn':
289                                         logger('notifier: dfrndelivery: ' . $contact['name']);
290                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
291
292                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
293         
294                                         if($deliver_status == (-1)) {
295                                                 logger('notifier: delivery failed: queuing message');
296                                                 // queue message for redelivery
297                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
298                                                         VALUES ( %d, '%s', '%s', '%s') ",
299                                                         intval($contact['id']),
300                                                         dbesc(datetime_convert()),
301                                                         dbesc(datetime_convert()),
302                                                         dbesc($atom)
303                                                 );
304                                         }
305                                         break;
306                                 case 'stat':
307                                         if($followup && $contact['notify']) {
308                                                 logger('notifier: slapdelivery: ' . $contact['name']);
309                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
310
311                                                 if($deliver_status == (-1)) {
312                                                         // queue message for redelivery
313                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
314                                                                 VALUES ( %d, '%s', '%s', '%s') ",
315                                                                 intval($contact['id']),
316                                                                 dbesc(datetime_convert()),
317                                                                 dbesc(datetime_convert()),
318                                                                 dbesc($slap)
319                                                         );
320
321                                                 }
322         
323
324                                         }
325                                         else {
326
327                                                 // only send salmon if public - e.g. if it's ok to notify
328                                                 // a public hub, it's ok to send a salmon
329
330                                                 if(count($slaps) && $notify_hub) {
331                                                         logger('notifier: slapdelivery: ' . $contact['name']);
332                                                         foreach($slaps as $slappy) {
333                                                                 if($contact['notify']) {
334                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
335                                                                         if($deliver_status == (-1)) {
336                                                                                 // queue message for redelivery
337                                                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
338                                                                                         VALUES ( %d, '%s', '%s', '%s') ",
339                                                                                         intval($contact['id']),
340                                                                                         dbesc(datetime_convert()),
341                                                                                         dbesc(datetime_convert()),
342                                                                                         dbesc($slappy)
343                                                                                 );                                                              
344                                                                         }
345                                                                 }
346                                                         }
347                                                 }
348                                         }
349                                         break;
350                                 case 'mail':
351                                 case 'dspr':
352                                 case 'feed':
353                                 default:
354                                         break;
355                         }
356                 }
357         }
358                 
359         // send additional slaps to mentioned remote tags (@foo@example.com)
360
361         if($slap && count($url_recipients) && $followup && $notify_hub) {
362                 foreach($url_recipients as $url) {
363                         if($url) {
364                                 logger('notifier: urldelivery: ' . $url);
365                                 $deliver_status = slapper($owner,$url,$slap);
366                                 // TODO: redeliver/queue these items on failure, though there is no contact record
367                         }
368                 }
369         }
370
371         if((strlen($hub)) && ($notify_hub)) {
372                 $hubs = explode(',', $hub);
373                 if(count($hubs)) {
374                         foreach($hubs as $h) {
375                                 $h = trim($h);
376                                 if(! strlen($h))
377                                         continue;
378                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
379                                 post_url($h,$params);
380                                 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
381                                 if(count($hubs) > 1)
382                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
383                         }
384                 }
385         }
386
387         if($notify_hub) {
388
389                 /**
390                  *
391                  * If you have less than 150 dfrn friends and it's a public message,
392                  * we'll just go ahead and push them out securely with dfrn/rino.
393                  * If you've got more than that, you'll have to rely on PuSH delivery.
394                  *
395                  */
396
397                 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxdeliver')));
398                                 
399                 /**
400                  *
401                  * Only get the bare essentials and go back for the full record. 
402                  * If you've got a lot of friends and we grab all the details at once it could exhaust memory. 
403                  *
404                  */
405
406                 $r = q("SELECT `id`, `name` FROM `contact` 
407                         WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
408                         AND `rel` != %d ",
409                         intval($owner['uid']),
410                         intval(REL_FAN)
411                 );
412
413                 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
414
415                         foreach($r as $rr) {
416
417                                 /* Don't deliver to folks who have already been delivered to */
418
419                                 if(! in_array($rr['id'], $conversants)) {
420                                         $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
421                                                         intval($rr['id'])
422                                         );
423
424                                         if(count($n)) {
425                                         
426                                                 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
427                                                 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
428                                         }
429                                 }
430                                 else
431                                         logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
432                         }
433                 }
434         }
435
436         return;
437 }
438
439 if (array_search(__file__,get_included_files())===0){
440   echo "run!";
441   notifier_run($argv,$argc);
442   killme();
443 }