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