]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
40fdbcc5f720f543d8dccdf92bddb59dd31b61b1
[friendica.git] / include / notifier.php
1 <?php
2
3         $debugging = false;
4
5         require_once("boot.php");
6
7         $a = new App;
8
9         @include(".htconfig.php");
10         require_once("dba.php");
11         $db = new dba($db_host, $db_user, $db_pass, $db_data);
12                 unset($db_host, $db_user, $db_pass, $db_data);
13
14         require_once("session.php");
15         require_once("datetime.php");
16
17         if($argc < 3)
18                 exit;
19
20         $a->set_baseurl(get_config('system','url'));
21
22         $baseurl = $argv[1];
23         $a->set_baseurl($argv[1]);
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         if($debugging)
38                 dbg(3);
39
40         $recipients = array();
41
42         if($cmd == 'mail') {
43
44                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
45                                 intval($item_id)
46                 );
47                 if(! count($message))
48                         killme();
49                 $uid = $message[0]['uid'];
50                 $recipients[] = $message[0]['contact-id'];
51                 $item = $message[0];
52
53         }
54         else {
55                 // find ancestors
56
57                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
58                         intval($item_id)
59                 );
60                 if(! count($r))
61                         killme();
62
63                 $parent = $r[0]['parent'];
64                 $uid = $r[0]['uid'];
65                 $updated = $r[0]['edited'];
66
67                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
68                         intval($parent)
69                 );
70
71                 if(! count($items))
72                         killme();
73         }
74
75         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
76                 intval($uid)
77         );
78
79         if(count($r))
80                 $owner = $r[0];
81         else
82                 killme();
83
84         if($cmd != 'mail') {
85
86                 require_once('include/group.php');
87
88                 $parent = $items[0];
89
90                 if($parent['type'] == 'remote') {
91                         // local followup to remote post
92                         $followup = true;
93                         $conversant_str = dbesc($parent['contact-id']);
94                 }
95                 else {
96                         $followup = false;
97
98                         $allow_people = expand_acl($parent['allow_cid']);
99                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
100                         $deny_people = expand_acl($parent['deny_cid']);
101                         $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
102
103                         $conversants = array();
104
105                         foreach($items as $item) {
106                                 $recipients[] = $item['contact-id'];
107                                 $conversants[] = $item['contact-id'];
108                         }
109
110                         $conversants = array_unique($conversants,SORT_NUMERIC);
111
112
113                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
114                         $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
115                         $recipients = array_diff($recipients,$deny);
116         
117                         $conversant_str = dbesc(implode(', ',$conversants));
118                 }
119
120                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
121
122                 if( ! count($r))
123                         killme();
124
125                 $contacts = $r;
126
127                 $tomb_template = file_get_contents('view/atom_tomb.tpl');
128                 $item_template = file_get_contents('view/atom_item.tpl');
129                 $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
130         }
131
132         $feed_template = file_get_contents('view/atom_feed.tpl');
133         $mail_template = file_get_contents('view/atom_mail.tpl');
134
135         $atom = '';
136
137
138         $atom .= replace_macros($feed_template, array(
139                         '$feed_id' => xmlify($a->get_baseurl()),
140                         '$feed_title' => xmlify($owner['name']),
141                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 
142                                 $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
143                         '$name' => xmlify($owner['name']),
144                         '$profile_page' => xmlify($owner['url']),
145                         '$photo' => xmlify($owner['photo']),
146                         '$thumb' => xmlify($owner['thumb']),
147                         '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
148                         '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
149                         '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
150         ));
151
152         if($cmd == 'mail') {
153                 $atom .= replace_macros($mail_template, array(
154                         '$name' => xmlify($owner['name']),
155                         '$profile_page' => xmlify($owner['url']),
156                         '$thumb' => xmlify($owner['thumb']),
157                         '$item_id' => xmlify($item['uri']),
158                         '$subject' => xmlify($item['title']),
159                         '$created' => xmlify(datetime_convert('UTC', 'UTC', 
160                                 $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
161                         '$content' =>xmlify($item['body']),
162                         '$parent_id' => xmlify($item['parent-uri'])
163
164                 ));
165         }
166         else {
167
168                 if($followup) {
169                         foreach($items as $item) {
170                                 if($item['id'] == $item_id) {
171                                         $atom .= replace_macros($cmnt_template, array(
172                                                 '$name' => xmlify($owner['name']),
173                                                 '$profile_page' => xmlify($owner['url']),
174                                                 '$thumb' => xmlify($owner['thumb']),
175                                                 '$item_id' => xmlify($item['uri']),
176                                                 '$title' => xmlify($item['title']),
177                                                 '$published' => xmlify(datetime_convert('UTC', 'UTC', 
178                                                         $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
179                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
180                                                         $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
181                                                 '$content' => xmlify($item['body']),
182                                                 '$parent_id' => xmlify($item['parent-uri']),
183                                                 '$comment_allow' => 0
184                                         ));
185                                 }
186                         }
187                 }
188                 else {
189                         foreach($items as $item) {
190                                 if($item['deleted']) {
191                                         $atom .= replace_macros($tomb_template, array(
192                                                 '$id' => xmlify($item['uri']),
193                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
194                                                         $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
195                                         ));
196                                 }
197                                 else {
198                                         foreach($contacts as $contact) {
199                                                 if($item['contact-id'] == $contact['id']) {
200                                                         if($item['parent'] == $item['id']) {
201                                                                 $atom .= replace_macros($item_template, array(
202                                                                         '$name' => xmlify($contact['name']),
203                                                                         '$profile_page' => xmlify($contact['url']),
204                                                                         '$thumb' => xmlify($contact['thumb']),
205                                                                         '$owner_name' => xmlify($item['owner-name']),
206                                                                         '$owner_profile_page' => xmlify($item['owner-link']),
207                                                                         '$owner_thumb' => xmlify($item['owner-avatar']),
208                                                                         '$item_id' => xmlify($item['uri']),
209                                                                         '$title' => xmlify($item['title']),
210                                                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', 
211                                                                                 $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
212                                                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
213                                                                                 $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
214                                                                         '$location' => xmlify($item['location']),
215                                                                         '$content' =>xmlify($item['body']),
216                                                                         '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'])) ? 1 : 0)
217                                                                 ));
218                                                         }
219                                                         else {
220                                                                 $atom .= replace_macros($cmnt_template, array(
221                                                                         '$name' => xmlify($contact['name']),
222                                                                         '$profile_page' => xmlify($contact['url']),
223                                                                         '$thumb' => xmlify($contact['thumb']),
224                                                                         '$item_id' => xmlify($item['uri']),
225                                                                         '$title' => xmlify($item['title']),
226                                                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', 
227                                                                                 $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
228                                                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
229                                                                                 $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
230                                                                         '$content' =>xmlify($item['body']),
231                                                                         '$parent_id' => xmlify($item['parent-uri']),
232                                                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
233                                                                 ));
234                                                         }
235                                                 }
236                                         }
237                                 }
238                         }
239                 }
240         }
241         $atom .= "</feed>\r\n";
242
243         if($debugging)
244                 echo $atom;
245
246         // create a clone of this feed but with comments disabled to send to those who can't respond. 
247
248         $atom_nowrite = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
249
250
251         if($followup)
252                 $recip_str = $parent['contact-id'];
253         else
254                 $recip_str = implode(', ', $recipients);
255
256
257         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
258                 dbesc($recip_str)
259         );
260         if(! count($r))
261                 killme();
262
263         // delivery loop
264
265         foreach($r as $rr) {
266                 if($rr['self'])
267                         continue;
268
269                 if((! strlen($rr['dfrn-id'])) || ($rr['duplex'] && ! strlen($rr['issued-id'])))
270                         continue;
271
272                 $idtosend = (($rr['duplex']) ? $rr['issued-id'] : $rr['dfrn-id']);
273
274                 $url = $rr['notify'] . '?dfrn_id=' . $idtosend;
275
276                 $xml = fetch_url($url);
277
278                 if($debugging)
279                         echo $xml;
280
281                 if(! $xml)
282                         continue;
283
284                 $res = simplexml_load_string($xml);
285
286                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
287                         continue;
288
289                 $postvars = array();
290                 $sent_dfrn_id = hex2bin($res->dfrn_id);
291                 $challenge = hex2bin($res->challenge);
292                 $final_dfrn_id = '';
293
294                 if($rr['duplex']) {
295                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['prvkey']);
296                         openssl_private_decrypt($challenge,$postvars['challenge'],$rr['prvkey']);
297                 }
298                 else {
299                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['pubkey']);
300                         openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
301                 }
302
303                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
304                 if(($final_dfrn_id != $rr['dfrn-id']) || (($rr['duplex']) && ($final_dfrn_id != $rr['issued-id']))) {
305                         // did not decode properly - cannot trust this site 
306                         continue;
307                 }
308
309                 $postvars['dfrn_id'] = (($duplex) ? $rr['issued-id'] : $rr['dfrn-id']);
310
311                 if($cmd == 'mail') {
312                         $postvars['data'] = $atom;
313                 }
314                 elseif(((strlen($rr['dfrn-id'])) || (($rr['duplex']) && (strlen($rr['issued-id'])))) 
315                         && (! ($rr['blocked']) || ($rr['readonly']))) {
316                         $postvars['data'] = $atom;
317                 }
318                 else {
319                         $postvars['data'] = $atom_nowrite;
320                 }
321
322                 $xml = post_url($rr['notify'],$postvars);
323
324                 if($debugging)
325                         echo $xml;
326
327                 $res = simplexml_load_string($xml);
328
329                 // Currently there is no retry attempt for failed mail delivery.
330                 // We need to handle this in the UI, report the non-deliverables and try again
331  
332                 if(($cmd == 'mail') && (intval($res->status) == 0)) {
333
334                         $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",
335                                 intval($item_id)
336                         );
337                 }
338         }
339
340         killme();
341