4 require_once("boot.php");
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);
13 require_once("session.php");
14 require_once("datetime.php");
20 $a->set_baseurl($argv[1]);
28 $item_id = intval($argv[3]);
35 $recipients = array();
39 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
44 $uid = $message[0]['uid'];
45 $recipients[] = $message[0]['contact-id'];
52 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
58 $parent = $r[0]['parent'];
60 $updated = $r[0]['edited'];
62 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
70 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
81 require_once('include/group.php');
85 if($parent['type'] == 'remote') {
86 // local followup to remote post
88 $conversant_str = dbesc($parent['contact-id']);
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']));
98 $conversants = array();
100 foreach($items as $item) {
101 $recipients[] = $item['contact-id'];
102 $conversants[] = $item['contact-id'];
105 $conversants = array_unique($conversants,SORT_NUMERIC);
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);
112 $conversant_str = dbesc(implode(', ',$conversants));
115 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
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');
127 $feed_template = file_get_contents('view/atom_feed.tpl');
128 $mail_template = file_get_contents('view/atom_mail.tpl');
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'))
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'])
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
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'))
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)
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)
235 $atom .= "</feed>\r\n";
237 // create a clone of this feed but with comments disabled to send to those who can't respond.
239 $atom_nowrite = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
243 $recip_str = $parent['contact-id'];
245 $recip_str = implode(', ', $recipients);
248 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
260 if(! strlen($rr['dfrn-id']))
265 $url = $rr['notify'] . '?dfrn_id=' . $rr['dfrn-id'];
267 $xml = fetch_url($url);
272 $res = simplexml_load_string($xml);
274 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
278 $sent_dfrn_id = hex2bin($res->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
288 $postvars['dfrn_id'] = $rr['dfrn-id'];
290 $challenge = hex2bin($res->challenge);
292 openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
295 $postvars['data'] = $atom;
297 elseif(strlen($rr['dfrn-id']) && (! ($rr['blocked']) || ($rr['readonly']))) {
298 $postvars['data'] = $atom;
301 $postvars['data'] = $atom_nowrite;
304 $xml = post_url($rr['notify'],$postvars);
307 $res = simplexml_load_string($xml);
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
312 if(($cmd == 'mail') && (intval($res->status) == 0)) {
314 $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",