]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
compatibility: version discovery
[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
211                 if($followup) {
212                         foreach($items as $item) {  // there is only one item
213                                 if($item['id'] == $item_id) {
214                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
215                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
216                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
217                                 }
218                         }
219                 }
220                 else {
221                         foreach($items as $item) {
222                                 $contact = get_item_contact($item,$contacts);
223                                 if(! $contact)
224                                         continue;
225
226                                 $atom   .= atom_entry($item,'text',$contact,$owner,true);
227                                 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
228                         }
229                 }
230         }
231         $atom .= '</feed>' . "\r\n";
232
233         logger('notifier: ' . $atom, LOGGER_DATA);
234
235         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
236
237         if($followup)
238                 $recip_str = $parent['contact-id'];
239         else
240                 $recip_str = implode(', ', $recipients);
241
242
243         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 ",
244                 dbesc($recip_str)
245         );
246         if(! count($r))
247                 killme();
248
249         // delivery loop
250
251         require_once('include/salmon.php');
252
253         foreach($r as $contact) {
254                 if($contact['self'])
255                         continue;
256
257                 $deliver_status = 0;
258
259                 switch($contact['network']) {
260                         case 'dfrn':
261                                 logger('notifier: dfrndelivery: ' . $contact['name']);
262                                 $deliver_status = dfrn_deliver($owner,$contact,$atom);
263
264                                 logger('notifier: dfrn_delivery returns ' . $deliver_status);
265
266                                 if($deliver_status == (-1)) {
267                                         logger('notifier: delivery failed: queuing message');
268                                         // queue message for redelivery
269                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
270                                                 VALUES ( %d, '%s', '%s', '%s') ",
271                                                 intval($contact['id']),
272                                                 dbesc(datetime_convert()),
273                                                 dbesc(datetime_convert()),
274                                                 dbesc($atom)
275                                         );
276                                 }
277                                 break;
278                         default:
279                                 if($followup && $contact['notify']) {
280                                         logger('notifier: slapdelivery: ' . $contact['name']);
281                                         $deliver_status = slapper($owner,$contact['notify'],$slap);
282
283                                         if($deliver_status == (-1)) {
284                                                 // queue message for redelivery
285                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
286                                                         VALUES ( %d, '%s', '%s', '%s') ",
287                                                         intval($contact['id']),
288                                                         dbesc(datetime_convert()),
289                                                         dbesc(datetime_convert()),
290                                                         dbesc($slap)
291                                                 );
292
293                                         }
294
295
296                                 }
297                                 else {
298
299                                         // only send salmon if public - e.g. if it's ok to notify
300                                         // a public hub, it's ok to send a salmon
301
302                                         if(count($slaps) && $notify_hub) {
303                                                 logger('notifier: slapdelivery: ' . $contact['name']);
304                                                 foreach($slaps as $slappy) {
305                                                         if($contact['notify']) {
306                                                                 $deliver_status = slapper($owner,$contact['notify'],$slappy);
307                                                                 if($deliver_status == (-1)) {
308                                                                         // queue message for redelivery
309                                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
310                                                                                 VALUES ( %d, '%s', '%s', '%s') ",
311                                                                                 intval($contact['id']),
312                                                                                 dbesc(datetime_convert()),
313                                                                                 dbesc(datetime_convert()),
314                                                                                 dbesc($slappy)
315                                                                         );                                                              
316                                                                 }
317                                                         }
318                                                 }
319                                         }
320                                 }
321                                 break;
322                 }
323         }
324                 
325         // send additional slaps to mentioned remote tags (@foo@example.com)
326
327         if(count($slaps) && count($url_recipients) && $notify_hub) {
328                 foreach($url_recipients as $url) {
329                         logger('notifier: urldelivery: ' . $url);
330                         foreach($slaps as $slappy) {
331                                 if($url) {
332                                         $deliver_status = slapper($owner,$url,$slappy);
333                                         // TODO: redeliver/queue these items on failure, though there is no contact record
334                                 }
335                         }
336                 }
337         }
338
339         if((strlen($hub)) && ($notify_hub)) {
340                 $hubs = explode(',', $hub);
341                 if(count($hubs)) {
342                         foreach($hubs as $h) {
343                                 $h = trim($h);
344                                 if(! strlen($h))
345                                         continue;
346                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
347                                 post_url($h,$params);
348                                 logger('pubsub: publish: ' . $h . ' returned ' . $a->get_curl_code());
349                                 if(count($hubs) > 1)
350                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
351                         }
352                 }
353         }
354
355         killme();
356