5 require_once("boot.php");
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);
14 require_once("session.php");
15 require_once("datetime.php");
20 $a->set_baseurl(get_config('system',url'));
23 $a->set_baseurl($argv[1]);
31 $item_id = intval($argv[2]);
38 $recipients = array();
42 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
47 $uid = $message[0]['uid'];
48 $recipients[] = $message[0]['contact-id'];
55 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
61 $parent = $r[0]['parent'];
63 $updated = $r[0]['edited'];
65 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
73 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
84 require_once('include/group.php');
88 if($parent['type'] == 'remote') {
89 // local followup to remote post
91 $conversant_str = dbesc($parent['contact-id']);
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']));
101 $conversants = array();
103 foreach($items as $item) {
104 $recipients[] = $item['contact-id'];
105 $conversants[] = $item['contact-id'];
108 $conversants = array_unique($conversants,SORT_NUMERIC);
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);
115 $conversant_str = dbesc(implode(', ',$conversants));
118 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
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');
130 $feed_template = file_get_contents('view/atom_feed.tpl');
131 $mail_template = file_get_contents('view/atom_mail.tpl');
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'))
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'])
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
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'))
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)
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)
238 $atom .= "</feed>\r\n";
243 // create a clone of this feed but with comments disabled to send to those who can't respond.
245 $atom_nowrite = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
249 $recip_str = $parent['contact-id'];
251 $recip_str = implode(', ', $recipients);
254 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
266 if(! strlen($rr['dfrn-id']))
271 $url = $rr['notify'] . '?dfrn_id=' . $rr['dfrn-id'];
273 $xml = fetch_url($url);
281 $res = simplexml_load_string($xml);
283 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
287 $sent_dfrn_id = hex2bin($res->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
297 $postvars['dfrn_id'] = $rr['dfrn-id'];
299 $challenge = hex2bin($res->challenge);
301 openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
304 $postvars['data'] = $atom;
306 elseif(strlen($rr['dfrn-id']) && (! ($rr['blocked']) || ($rr['readonly']))) {
307 $postvars['data'] = $atom;
310 $postvars['data'] = $atom_nowrite;
313 $xml = post_url($rr['notify'],$postvars);
318 $res = simplexml_load_string($xml);
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
323 if(($cmd == 'mail') && (intval($res->status) == 0)) {
325 $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",