]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
8442349cfdcfe83ebc574c5f43ea150be096f21a
[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                 if(count($r))
167                         $contacts = $r;
168         }
169
170         $feed_template = load_view_file('view/atom_feed.tpl');
171         $mail_template = load_view_file('view/atom_mail.tpl');
172
173         $atom = '';
174         $hubxml = '';
175         $slaps = array();
176
177         if(strlen($hub)) {
178                 $hubs = explode(',', $hub);
179                 if(count($hubs)) {
180                         foreach($hubs as $h) {
181                                 $h = trim($h);
182                                 if(! strlen($h))
183                                         continue;
184                                 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
185                         }
186                 }
187         }
188
189         $atom .= replace_macros($feed_template, array(
190                         '$version'      => xmlify(FRIENDIKA_VERSION),
191                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
192                         '$feed_title'   => xmlify($owner['name']),
193                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
194                         '$hub'          => $hubxml,
195                         '$salmon'       => '',  // private feed, we don't use salmon here
196                         '$name'         => xmlify($owner['name']),
197                         '$profile_page' => xmlify($owner['url']),
198                         '$photo'        => xmlify($owner['photo']),
199                         '$thumb'        => xmlify($owner['thumb']),
200                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
201                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
202                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
203                         '$birthday'     => ''
204         ));
205
206         if($cmd === 'mail') {
207                 $notify_hub = false;  // mail is  not public
208
209                 $atom .= replace_macros($mail_template, array(
210                         '$name'         => xmlify($owner['name']),
211                         '$profile_page' => xmlify($owner['url']),
212                         '$thumb'        => xmlify($owner['thumb']),
213                         '$item_id'      => xmlify($item['uri']),
214                         '$subject'      => xmlify($item['title']),
215                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
216                         '$content'      => xmlify($item['body']),
217                         '$parent_id'    => xmlify($item['parent-uri'])
218                 ));
219         }
220         else {
221                 if($followup) {
222                         foreach($items as $item) {  // there is only one item
223                                 if($item['id'] == $item_id) {
224                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
225                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
226                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
227                                 }
228                         }
229                 }
230                 else {
231                         foreach($items as $item) {
232                                 $contact = get_item_contact($item,$contacts);
233                                 if(! $contact)
234                                         continue;
235
236                                 $atom   .= atom_entry($item,'text',$contact,$owner,true);
237
238                                 // There's a problem here - we *were* going to use salmon to provide semi-authenticated
239                                 // communication to OStatus, but unless we're the item author they won't verify.
240                                 // commented out for now, though we'll still send local replies (and any mentions 
241                                 // that they contain) upstream. Rethinking the problem space.
242  
243 //                              $slaps[] = atom_entry($item,'html',$contact,$owner,true);
244                         }
245                 }
246         }
247         $atom .= '</feed>' . "\r\n";
248
249         logger('notifier: ' . $atom, LOGGER_DATA);
250
251 //      logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
252
253         if($followup)
254                 $recip_str = $parent['contact-id'];
255         else
256                 $recip_str = implode(', ', $recipients);
257
258
259         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
260                 dbesc($recip_str)
261         );
262
263         // delivery loop
264
265         require_once('include/salmon.php');
266
267         if(count($r)) {
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                 
346         // send additional slaps to mentioned remote tags (@foo@example.com)
347
348         if($slap && count($url_recipients) && $followup && $notify_hub) {
349                 foreach($url_recipients as $url) {
350                         if($url) {
351                                 logger('notifier: urldelivery: ' . $url);
352                                 $deliver_status = slapper($owner,$url,$slap);
353                                 // TODO: redeliver/queue these items on failure, though there is no contact record
354                         }
355                 }
356         }
357
358         if((strlen($hub)) && ($notify_hub)) {
359                 $hubs = explode(',', $hub);
360                 if(count($hubs)) {
361                         foreach($hubs as $h) {
362                                 $h = trim($h);
363                                 if(! strlen($h))
364                                         continue;
365                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
366                                 post_url($h,$params);
367                                 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
368                                 if(count($hubs) > 1)
369                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
370                         }
371                 }
372         }
373
374         if($notify_hub) {
375
376                 /**
377                  *
378                  * If you have less than 150 dfrn friends and it's a public message,
379                  * we'll just go ahead and push them out securely with dfrn/rino.
380                  * If you've got more than that, you'll have to rely on PuSH delivery.
381                  *
382                  */
383
384                 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxdeliver')));
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 == 0) || (count($r) < $max_allowed))) {
401
402                         foreach($r as $rr) {
403
404                                 /* Don't deliver to folks who have already been delivered to */
405
406                                 if(! in_array($rr['id'], $conversants)) {
407                                         $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
408                                                         intval($rr['id'])
409                                         );
410
411                                         if(count($n)) {
412                                         
413                                                 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
414                                                 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
415                                         }
416                                 }
417                                 else
418                                         logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
419                         }
420                 }
421         }
422
423         return;
424 }
425
426 if (array_search(__file__,get_included_files())===0){
427   echo "run!";
428   notifier_run($argv,$argc);
429   killme();
430 }