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