]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
683cff360e03da31b873ef62b05fd4b70802bb61
[friendica.git] / include / notifier.php
1 <?php
2
3         require_once("boot.php");
4
5         $a = new App;
6
7         @include(".htconfig.php");
8         require_once("dba.php");
9         $db = new dba($db_host, $db_user, $db_pass, $db_data);
10                 unset($db_host, $db_user, $db_pass, $db_data);
11
12
13         require_once("session.php");
14         require_once("datetime.php");
15         require_once('include/items.php');
16         require_once('include/bbcode.php');
17
18         if($argc < 3)
19                 exit;
20
21         $a->set_baseurl(get_config('system','url'));
22
23         logger('notifier: invoked: ' . print_r($argv,true));
24
25         $cmd = $argv[1];
26
27         switch($cmd) {
28
29                 case 'mail':
30                 default:
31                         $item_id = intval($argv[2]);
32                         if(! $item_id)
33                                 killme();
34                         break;
35         }
36
37         $recipients = array();
38         $url_recipients = array();
39
40         if($cmd === 'mail') {
41
42                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
43                                 intval($item_id)
44                 );
45                 if(! count($message))
46                         killme();
47                 $uid = $message[0]['uid'];
48                 $recipients[] = $message[0]['contact-id'];
49                 $item = $message[0];
50
51         }
52         else {
53                 // find ancestors
54
55                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
56                         intval($item_id)
57                 );
58                 if(! count($r))
59                         killme();
60
61                 $parent_id = $r[0]['parent'];
62                 $uid = $r[0]['uid'];
63                 $updated = $r[0]['edited'];
64
65                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
66                         intval($parent_id)
67                 );
68
69                 if(! count($items))
70                         killme();
71         }
72
73         $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags` 
74                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
75                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
76                 intval($uid)
77         );
78
79         if(count($r))
80                 $owner = $r[0];
81         else
82                 killme();
83
84         $hub = get_config('system','huburl');
85
86         // If this is a public conversation, notify the feed hub
87         $notify_hub = true;
88
89         // fill this in with a single salmon slap if applicable
90         $slap = '';
91
92         if($cmd != 'mail') {
93
94                 require_once('include/group.php');
95
96                 $parent = $items[0];
97
98                 if($parent['type'] === 'remote') {
99                         // local followup to remote post
100                         $followup = true;
101                         $notify_hub = false; // not public
102                         $conversant_str = dbesc($parent['contact-id']);
103                 }
104                 else {
105                         $followup = false;
106
107                         if((strlen($parent['allow_cid'])) 
108                                 || (strlen($parent['allow_gid'])) 
109                                 || (strlen($parent['deny_cid'])) 
110                                 || (strlen($parent['deny_gid']))) {
111                                 $notify_hub = false; // private recipients, not public
112                         }
113
114                         $allow_people = expand_acl($parent['allow_cid']);
115                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
116                         $deny_people = expand_acl($parent['deny_cid']);
117                         $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
118
119                         $conversants = array();
120
121                         foreach($items as $item) {
122                                 $recipients[] = $item['contact-id'];
123                                 $conversants[] = $item['contact-id'];
124                                 // pull out additional tagged people to notify (if public message)
125                                 if($notify_hub && strlen($item['inform'])) {
126                                         $people = explode(',',$item['inform']);
127                                         foreach($people as $person) {
128                                                 if(substr($person,0,4) === 'cid:') {
129                                                         $recipients[] = intval(substr($person,4));
130                                                         $conversants[] = intval(substr($person,4));
131                                                 }
132                                                 else {
133                                                         $url_recipients[] = substr($person,4);
134                                                 }
135                                         }
136                                 }
137                         }
138
139                         logger('notifier: url_recipients' . print_r($url_recipients,true));
140
141                         $conversants = array_unique($conversants);
142
143
144                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
145                         $deny = array_unique(array_merge($deny_people,$deny_groups));
146                         $recipients = array_diff($recipients,$deny);
147
148                         $conversant_str = dbesc(implode(', ',$conversants));
149                 }
150
151                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
152
153                 if( ! count($r))
154                         killme();
155
156                 $contacts = $r;
157         }
158
159         $feed_template = load_view_file('view/atom_feed.tpl');
160         $mail_template = load_view_file('view/atom_mail.tpl');
161
162         $atom = '';
163         $hubxml = '';
164         $slaps = array();
165
166         if(strlen($hub)) {
167                 $hubs = explode(',', $hub);
168                 if(count($hubs)) {
169                         foreach($hubs as $h) {
170                                 $h = trim($h);
171                                 if(! strlen($h))
172                                         continue;
173                                 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
174                         }
175                 }
176         }
177
178         $atom .= replace_macros($feed_template, array(
179                         '$version'      => xmlify(FRIENDIKA_VERSION),
180                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
181                         '$feed_title'   => xmlify($owner['name']),
182                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
183                         '$hub'          => $hubxml,
184                         '$salmon'       => '',   // private feed, we don't use salmon here
185                         '$name'         => xmlify($owner['name']),
186                         '$profile_page' => xmlify($owner['url']),
187                         '$photo'        => xmlify($owner['photo']),
188                         '$thumb'        => xmlify($owner['thumb']),
189                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
190                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
191                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
192                         '$birthday'     => ''
193         ));
194
195         if($cmd === 'mail') {
196                 $notify_hub = false;  // mail is  not public
197
198                 $atom .= replace_macros($mail_template, array(
199                         '$name'         => xmlify($owner['name']),
200                         '$profile_page' => xmlify($owner['url']),
201                         '$thumb'        => xmlify($owner['thumb']),
202                         '$item_id'      => xmlify($item['uri']),
203                         '$subject'      => xmlify($item['title']),
204                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
205                         '$content'      => xmlify($item['body']),
206                         '$parent_id'    => xmlify($item['parent-uri'])
207                 ));
208         }
209         else {
210                 if($followup) {
211                         foreach($items as $item) {  // there is only one item
212                                 if($item['id'] == $item_id) {
213                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
214                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
215                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
216                                 }
217                         }
218                 }
219                 else {
220                         foreach($items as $item) {
221                                 $contact = get_item_contact($item,$contacts);
222                                 if(! $contact)
223                                         continue;
224
225                                 $atom   .= atom_entry($item,'text',$contact,$owner,true);
226
227                                 // There's a problem here - we *were* going to use salmon to provide semi-authenticated
228                                 // communication to OStatus, but unless we're the item author they won't verify.
229                                 // commented out for now, though we'll still send local replies (and any mentions 
230                                 // that they contain) upstream. Rethinking the problem space.
231  
232 //                              $slaps[] = atom_entry($item,'html',$contact,$owner,true);
233                         }
234                 }
235         }
236         $atom .= '</feed>' . "\r\n";
237
238         logger('notifier: ' . $atom, LOGGER_DATA);
239
240 //      logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
241
242         if($followup)
243                 $recip_str = $parent['contact-id'];
244         else
245                 $recip_str = implode(', ', $recipients);
246
247
248         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 ",
249                 dbesc($recip_str)
250         );
251         if(! count($r))
252                 killme();
253
254         // delivery loop
255
256         require_once('include/salmon.php');
257
258         foreach($r as $contact) {
259                 if($contact['self'])
260                         continue;
261
262                 $deliver_status = 0;
263
264                 switch($contact['network']) {
265                         case 'dfrn':
266                                 logger('notifier: dfrndelivery: ' . $contact['name']);
267                                 $deliver_status = dfrn_deliver($owner,$contact,$atom);
268
269                                 logger('notifier: dfrn_delivery returns ' . $deliver_status);
270
271                                 if($deliver_status == (-1)) {
272                                         logger('notifier: delivery failed: queuing message');
273                                         // queue message for redelivery
274                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
275                                                 VALUES ( %d, '%s', '%s', '%s') ",
276                                                 intval($contact['id']),
277                                                 dbesc(datetime_convert()),
278                                                 dbesc(datetime_convert()),
279                                                 dbesc($atom)
280                                         );
281                                 }
282                                 break;
283                         default:
284                                 if($followup && $contact['notify']) {
285                                         logger('notifier: slapdelivery: ' . $contact['name']);
286                                         $deliver_status = slapper($owner,$contact['notify'],$slap);
287
288                                         if($deliver_status == (-1)) {
289                                                 // queue message for redelivery
290                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
291                                                         VALUES ( %d, '%s', '%s', '%s') ",
292                                                         intval($contact['id']),
293                                                         dbesc(datetime_convert()),
294                                                         dbesc(datetime_convert()),
295                                                         dbesc($slap)
296                                                 );
297
298                                         }
299
300
301                                 }
302                                 else {
303
304                                         // only send salmon if public - e.g. if it's ok to notify
305                                         // a public hub, it's ok to send a salmon
306
307                                         if(count($slaps) && $notify_hub) {
308                                                 logger('notifier: slapdelivery: ' . $contact['name']);
309                                                 foreach($slaps as $slappy) {
310                                                         if($contact['notify']) {
311                                                                 $deliver_status = slapper($owner,$contact['notify'],$slappy);
312                                                                 if($deliver_status == (-1)) {
313                                                                         // queue message for redelivery
314                                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
315                                                                                 VALUES ( %d, '%s', '%s', '%s') ",
316                                                                                 intval($contact['id']),
317                                                                                 dbesc(datetime_convert()),
318                                                                                 dbesc(datetime_convert()),
319                                                                                 dbesc($slappy)
320                                                                         );                                                              
321                                                                 }
322                                                         }
323                                                 }
324                                         }
325                                 }
326                                 break;
327                 }
328         }
329                 
330         // send additional slaps to mentioned remote tags (@foo@example.com)
331
332         if($slap && count($url_recipients) && $followup && $notify_hub) {
333                 foreach($url_recipients as $url) {
334                         if($url) {
335                                 logger('notifier: urldelivery: ' . $url);
336                                 $deliver_status = slapper($owner,$url,$slap);
337                                 // TODO: redeliver/queue these items on failure, though there is no contact record
338                         }
339                 }
340         }
341
342         if((strlen($hub)) && ($notify_hub)) {
343                 $hubs = explode(',', $hub);
344                 if(count($hubs)) {
345                         foreach($hubs as $h) {
346                                 $h = trim($h);
347                                 if(! strlen($h))
348                                         continue;
349                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
350                                 post_url($h,$params);
351                                 logger('pubsub: publish: ' . $h . ' returned ' . $a->get_curl_code());
352                                 if(count($hubs) > 1)
353                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
354                         }
355                 }
356         }
357
358         killme();
359