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