]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
b94c21e7154dafa55b2c9f302ccf308b797bdd09
[friendica.git] / include / notifier.php
1 <?php
2
3         $debugging = true;
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         $cmd = $argv[1];
23
24         switch($cmd) {
25
26                 case 'mail':
27                 default:
28                         $item_id = intval($argv[2]);
29                         if(! $item_id)
30                                 killme();
31                         break;
32         }
33
34         if($debugging)
35                 dbg(3);
36
37         $recipients = array();
38
39         if($cmd == 'mail') {
40
41                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
42                                 intval($item_id)
43                 );
44                 if(! count($message))
45                         killme();
46                 $uid = $message[0]['uid'];
47                 $recipients[] = $message[0]['contact-id'];
48                 $item = $message[0];
49
50         }
51         else {
52                 // find ancestors
53
54                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
55                         intval($item_id)
56                 );
57                 if(! count($r))
58                         killme();
59
60                 $parent = $r[0]['parent'];
61                 $uid = $r[0]['uid'];
62                 $updated = $r[0]['edited'];
63
64                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
65                         intval($parent)
66                 );
67
68                 if(! count($items))
69                         killme();
70         }
71
72         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
73                 intval($uid)
74         );
75
76         if(count($r))
77                 $owner = $r[0];
78         else
79                 killme();
80
81         if($cmd != 'mail') {
82
83                 require_once('include/group.php');
84
85                 $parent = $items[0];
86
87                 if($parent['type'] == 'remote') {
88                         // local followup to remote post
89                         $followup = true;
90                         $conversant_str = dbesc($parent['contact-id']);
91                 }
92                 else {
93                         $followup = false;
94
95                         $allow_people = expand_acl($parent['allow_cid']);
96                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
97                         $deny_people = expand_acl($parent['deny_cid']);
98                         $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
99
100                         $conversants = array();
101
102                         foreach($items as $item) {
103                                 $recipients[] = $item['contact-id'];
104                                 $conversants[] = $item['contact-id'];
105                         }
106
107                         $conversants = array_unique($conversants,SORT_NUMERIC);
108
109
110                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
111                         $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
112                         $recipients = array_diff($recipients,$deny);
113         
114                         $conversant_str = dbesc(implode(', ',$conversants));
115                 }
116
117                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
118
119                 if( ! count($r))
120                         killme();
121
122                 $contacts = $r;
123
124                 $tomb_template = file_get_contents('view/atom_tomb.tpl');
125                 $item_template = file_get_contents('view/atom_item.tpl');
126                 $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
127         }
128
129         $feed_template = file_get_contents('view/atom_feed.tpl');
130         $mail_template = file_get_contents('view/atom_mail.tpl');
131
132         $atom = '';
133
134
135         $atom .= replace_macros($feed_template, array(
136                         '$feed_id' => xmlify($a->get_baseurl()),
137                         '$feed_title' => xmlify($owner['name']),
138                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 
139                                 $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
140                         '$name' => xmlify($owner['name']),
141                         '$profile_page' => xmlify($owner['url']),
142                         '$photo' => xmlify($owner['photo']),
143                         '$thumb' => xmlify($owner['thumb']),
144                         '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
145                         '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
146                         '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
147         ));
148
149         if($cmd == 'mail') {
150                 $atom .= replace_macros($mail_template, array(
151                         '$name' => xmlify($owner['name']),
152                         '$profile_page' => xmlify($owner['url']),
153                         '$thumb' => xmlify($owner['thumb']),
154                         '$item_id' => xmlify($item['uri']),
155                         '$subject' => xmlify($item['title']),
156                         '$created' => xmlify(datetime_convert('UTC', 'UTC', 
157                                 $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
158                         '$content' =>xmlify($item['body']),
159                         '$parent_id' => xmlify($item['parent-uri'])
160
161                 ));
162         }
163         else {
164
165                 if($followup) {
166                         foreach($items as $item) {
167                                 if($item['id'] == $item_id) {
168                                         $atom .= replace_macros($cmnt_template, array(
169                                                 '$name' => xmlify($owner['name']),
170                                                 '$profile_page' => xmlify($owner['url']),
171                                                 '$thumb' => xmlify($owner['thumb']),
172                                                 '$item_id' => xmlify($item['uri']),
173                                                 '$title' => xmlify($item['title']),
174                                                 '$published' => xmlify(datetime_convert('UTC', 'UTC', 
175                                                         $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
176                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
177                                                         $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
178                                                 '$content' => xmlify($item['body']),
179                                                 '$parent_id' => xmlify($item['parent-uri']),
180                                                 '$comment_allow' => 0
181                                         ));
182                                 }
183                         }
184                 }
185                 else {
186                         foreach($items as $item) {
187                                 if($item['deleted']) {
188                                         $atom .= replace_macros($tomb_template, array(
189                                                 '$id' => xmlify($item['uri']),
190                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
191                                                         $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
192                                         ));
193                                 }
194                                 else {
195                                         foreach($contacts as $contact) {
196                                                 if($item['contact-id'] == $contact['id']) {
197                                                         if($item['parent'] == $item['id']) {
198                                                                 $atom .= replace_macros($item_template, array(
199                                                                         '$name' => xmlify($contact['name']),
200                                                                         '$profile_page' => xmlify($contact['url']),
201                                                                         '$thumb' => xmlify($contact['thumb']),
202                                                                         '$owner_name' => xmlify($item['owner-name']),
203                                                                         '$owner_profile_page' => xmlify($item['owner-link']),
204                                                                         '$owner_thumb' => xmlify($item['owner-avatar']),
205                                                                         '$item_id' => xmlify($item['uri']),
206                                                                         '$title' => xmlify($item['title']),
207                                                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', 
208                                                                                 $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
209                                                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
210                                                                                 $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
211                                                                         '$location' => xmlify($item['location']),
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'])) && (! $rr['duplex']))
267                         continue;
268
269                 $idtosend = (($rr['dfrn-id']) ? $rr['dfrn-id'] : $rr['issued-id']);
270
271                 $url = $rr['notify'] . '?dfrn_id=' . $idtosend;
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                 $challenge = hex2bin($res->challenge);
289                 $final_dfrn_id = '';
290
291                 if($rr['duplex'] && strlen($rr['prvkey'])) {
292                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['prvkey']);
293                         openssl_private_decrypt($challenge,$postvars['challenge'],$rr['prvkey']);
294                 }
295                 else {
296                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['pubkey']);
297                         openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
298                 }
299
300                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
301                 if($final_dfrn_id != $idtosend) {
302                         // did not decode properly - cannot trust this site 
303                         continue;
304                 }
305
306                 $postvars['dfrn_id'] = $idtosend;
307
308                 if((($rr['rel'] == DIRECTION_OUT) || ($rr['rel'] == DIRECTION_BOTH)) && (! $rr['blocked']) && (! $rr['readonly'])) {
309                         $postvars['data'] = $atom;
310                 }
311                 else {
312                         $postvars['data'] = $atom_nowrite;
313                 }
314
315                 $xml = post_url($rr['notify'],$postvars);
316
317                 if($debugging)
318                         echo $xml;
319
320                 $res = simplexml_load_string($xml);
321
322                 // Currently there is no retry attempt for failed mail delivery.
323                 // We need to handle this in the UI, report the non-deliverables and try again
324  
325                 if(($cmd == 'mail') && (intval($res->status) == 0)) {
326
327                         $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",
328                                 intval($item_id)
329                         );
330                 }
331         }
332
333         killme();
334