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