]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
queue logging
[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,SORT_NUMERIC);
142
143
144                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
145                         $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
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                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
180                         '$feed_title'   => xmlify($owner['name']),
181                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
182                         '$hub'          => $hubxml,
183                         '$salmon'       => '',   // private feed, we don't use salmon here
184                         '$name'         => xmlify($owner['name']),
185                         '$profile_page' => xmlify($owner['url']),
186                         '$photo'        => xmlify($owner['photo']),
187                         '$thumb'        => xmlify($owner['thumb']),
188                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
189                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
190                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME))
191         ));
192
193         if($cmd === 'mail') {
194                 $notify_hub = false;  // mail is  not public
195
196                 $atom .= replace_macros($mail_template, array(
197                         '$name'         => xmlify($owner['name']),
198                         '$profile_page' => xmlify($owner['url']),
199                         '$thumb'        => xmlify($owner['thumb']),
200                         '$item_id'      => xmlify($item['uri']),
201                         '$subject'      => xmlify($item['title']),
202                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
203                         '$content'      => xmlify($item['body']),
204                         '$parent_id'    => xmlify($item['parent-uri'])
205                 ));
206         }
207         else {
208
209                 if($followup) {
210                         foreach($items as $item) {  // there is only one item
211                                 if($item['id'] == $item_id) {
212                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
213                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
214                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
215                                 }
216                         }
217                 }
218                 else {
219                         foreach($items as $item) {
220                                 $contact = get_item_contact($item,$contacts);
221                                 if(! $contact)
222                                         continue;
223
224                                 $atom   .= atom_entry($item,'text',$contact,$owner,true);
225                                 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
226                         }
227                 }
228         }
229         $atom .= '</feed>' . "\r\n";
230
231         logger('notifier: ' . $atom, LOGGER_DATA);
232
233         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
234
235         if($followup)
236                 $recip_str = $parent['contact-id'];
237         else
238                 $recip_str = implode(', ', $recipients);
239
240
241         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 ",
242                 dbesc($recip_str)
243         );
244         if(! count($r))
245                 killme();
246
247         // delivery loop
248
249         require_once('include/salmon.php');
250
251         foreach($r as $contact) {
252                 if($contact['self'])
253                         continue;
254
255                 $deliver_status = 0;
256
257                 switch($contact['network']) {
258                         case 'dfrn':
259                                 logger('notifier: dfrndelivery: ' . $contact['name']);
260                                 $deliver_status = dfrn_deliver($owner,$contact,$atom);
261
262                                 logger('notifier: dfrn_delivery returns ' . $deliver_status);
263
264                                 if($deliver_status == (-1)) {
265                                         logger('notifier: delivery failed: queuing message');
266                                         // queue message for redelivery
267                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
268                                                 VALUES ( %d, '%s', '%s', '%s') ",
269                                                 intval($contact['id']),
270                                                 dbesc(datetime_convert()),
271                                                 dbesc(datetime_convert()),
272                                                 dbesc($atom)
273                                         );
274                                 }
275                                 break;
276                         default:
277                                 if($followup && $contact['notify']) {
278                                         logger('notifier: slapdelivery: ' . $contact['name']);
279                                         $deliver_status = slapper($owner,$contact['notify'],$slap);
280
281                                         if($deliver_status == (-1)) {
282                                                 // queue message for redelivery
283                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
284                                                         VALUES ( %d, '%s', '%s', '%s') ",
285                                                         intval($contact['id']),
286                                                         dbesc(datetime_convert()),
287                                                         dbesc(datetime_convert()),
288                                                         dbesc($slap)
289                                                 );
290
291                                         }
292
293
294                                 }
295                                 else {
296
297                                         // only send salmon if public - e.g. if it's ok to notify
298                                         // a public hub, it's ok to send a salmon
299
300                                         if(count($slaps) && $notify_hub) {
301                                                 logger('notifier: slapdelivery: ' . $contact['name']);
302                                                 foreach($slaps as $slappy) {
303                                                         if($contact['notify']) {
304                                                                 $deliver_status = slapper($owner,$contact['notify'],$slappy);
305                                                                 if($deliver_status == (-1)) {
306                                                                         // queue message for redelivery
307                                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
308                                                                                 VALUES ( %d, '%s', '%s', '%s') ",
309                                                                                 intval($contact['id']),
310                                                                                 dbesc(datetime_convert()),
311                                                                                 dbesc(datetime_convert()),
312                                                                                 dbesc($slappy)
313                                                                         );                                                              
314                                                                 }
315                                                         }
316                                                 }
317                                         }
318                                 }
319                                 break;
320                 }
321         }
322                 
323         // send additional slaps to mentioned remote tags (@foo@example.com)
324
325         if(count($slaps) && count($url_recipients) && $notify_hub) {
326                 foreach($url_recipients as $url) {
327                         logger('notifier: urldelivery: ' . $url);
328                         foreach($slaps as $slappy) {
329                                 if($url) {
330                                         $deliver_status = slapper($owner,$url,$slappy);
331                                         // TODO: redeliver/queue these items on failure, though there is no contact record
332                                 }
333                         }
334                 }
335         }
336
337         if((strlen($hub)) && ($notify_hub)) {
338                 $hubs = explode(',', $hub);
339                 if(count($hubs)) {
340                         foreach($hubs as $h) {
341                                 $h = trim($h);
342                                 if(! strlen($h))
343                                         continue;
344                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
345                                 post_url($h,$params);
346                                 logger('pubsub: publish: ' . $h . ' returned ' . $a->get_curl_code());
347                                 if(count($hubs) > 1)
348                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
349                         }
350                 }
351         }
352
353         killme();
354