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);
12 unset($db_host, $db_user, $db_pass, $db_data);
14 require_once("session.php");
15 require_once("datetime.php");
16 require_once('include/items.php');
21 $a->set_baseurl(get_config('system','url'));
29 $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 `contact`.*, `user`.`nickname`
74 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
75 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
86 require_once('include/group.php');
90 if($parent['type'] == 'remote') {
91 // local followup to remote post
93 $conversant_str = dbesc($parent['contact-id']);
98 $allow_people = expand_acl($parent['allow_cid']);
99 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
100 $deny_people = expand_acl($parent['deny_cid']);
101 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
103 $conversants = array();
105 foreach($items as $item) {
106 $recipients[] = $item['contact-id'];
107 $conversants[] = $item['contact-id'];
110 $conversants = array_unique($conversants,SORT_NUMERIC);
113 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
114 $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
115 $recipients = array_diff($recipients,$deny);
117 $conversant_str = dbesc(implode(', ',$conversants));
122 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
129 $tomb_template = load_view_file('view/atom_tomb.tpl');
130 $item_template = load_view_file('view/atom_item.tpl');
131 $cmnt_template = load_view_file('view/atom_cmnt.tpl');
134 $feed_template = load_view_file('view/atom_feed.tpl');
135 $mail_template = load_view_file('view/atom_mail.tpl');
140 $atom .= replace_macros($feed_template, array(
141 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
142 '$feed_title' => xmlify($owner['name']),
143 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
144 '$name' => xmlify($owner['name']),
145 '$profile_page' => xmlify($owner['url']),
146 '$photo' => xmlify($owner['photo']),
147 '$thumb' => xmlify($owner['thumb']),
148 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
149 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
150 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME))
154 $atom .= replace_macros($mail_template, array(
155 '$name' => xmlify($owner['name']),
156 '$profile_page' => xmlify($owner['url']),
157 '$thumb' => xmlify($owner['thumb']),
158 '$item_id' => xmlify($item['uri']),
159 '$subject' => xmlify($item['title']),
160 '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
161 '$content' => xmlify($item['body']),
162 '$parent_id' => xmlify($item['parent-uri'])
169 foreach($items as $item) {
171 $verb = construct_verb($item);
172 $actobj = construct_activity($item);
174 if($item['id'] == $item_id) {
175 $atom .= replace_macros($cmnt_template, array(
176 '$name' => xmlify($owner['name']),
177 '$profile_page' => xmlify($owner['url']),
178 '$thumb' => xmlify($owner['thumb']),
179 '$owner_name' => xmlify($item['owner-name']),
180 '$owner_profile_page' => xmlify($item['owner-link']),
181 '$owner_thumb' => xmlify($item['owner-avatar']),
182 '$item_id' => xmlify($item['uri']),
183 '$title' => xmlify($item['title']),
184 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
185 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
186 '$location' => xmlify($item['location']),
188 '$verb' => xmlify($verb),
189 '$actobj' => $actobj,
190 '$content' => xmlify($item['body']),
191 '$parent_id' => xmlify($item['parent-uri']),
192 '$comment_allow' => 0
198 foreach($items as $item) {
199 if($item['deleted']) {
200 $atom .= replace_macros($tomb_template, array(
201 '$id' => xmlify($item['uri']),
202 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
206 foreach($contacts as $contact) {
207 if($item['contact-id'] == $contact['id']) {
209 $verb = construct_verb($item);
210 $actobj = construct_activity($item);
212 if($item['parent'] == $item['id']) {
213 $atom .= replace_macros($item_template, array(
214 '$name' => xmlify($contact['name']),
215 '$profile_page' => xmlify($contact['url']),
216 '$thumb' => xmlify($contact['thumb']),
217 '$owner_name' => xmlify($item['owner-name']),
218 '$owner_profile_page' => xmlify($item['owner-link']),
219 '$owner_thumb' => xmlify($item['owner-avatar']),
220 '$item_id' => xmlify($item['uri']),
221 '$title' => xmlify($item['title']),
222 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
223 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
224 '$location' => xmlify($item['location']),
226 '$verb' => xmlify($verb),
227 '$actobj' => $actobj,
228 '$content' => xmlify($item['body']),
229 '$comment_allow' => (($item['last-child']) ? 1 : 0)
233 $atom .= replace_macros($cmnt_template, array(
234 '$name' => xmlify($contact['name']),
235 '$profile_page' => xmlify($contact['url']),
236 '$thumb' => xmlify($contact['thumb']),
237 '$item_id' => xmlify($item['uri']),
238 '$title' => xmlify($item['title']),
239 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
240 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
241 '$content' => xmlify($item['body']),
242 '$location' => xmlify($item['location']),
244 '$verb' => xmlify($verb),
245 '$actobj' => $actobj,
246 '$parent_id' => xmlify($item['parent-uri']),
247 '$comment_allow' => (($item['last-child']) ? 1 : 0)
256 $atom .= "</feed>\r\n";
261 // create a clone of this feed but with comments disabled to send to those who can't respond.
263 $atom_nowrite = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
267 $recip_str = $parent['contact-id'];
269 $recip_str = implode(', ', $recipients);
272 $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
284 if((! strlen($rr['dfrn-id'])) && (! $rr['duplex']))
288 $idtosend = $orig_id = (($rr['dfrn-id']) ? $rr['dfrn-id'] : $rr['issued-id']);
290 if($rr['duplex'] && $rr['dfrn-id'])
291 $idtosend = '0:' . $orig_id;
292 if($rr['duplex'] && $rr['issued-id'])
293 $idtosend = '1:' . $orig_id;
295 $url = $rr['notify'] . '?dfrn_id=' . $idtosend;
300 $xml = fetch_url($url);
308 $res = simplexml_load_string($xml);
310 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
315 $sent_dfrn_id = hex2bin($res->dfrn_id);
316 $challenge = hex2bin($res->challenge);
320 if($rr['duplex'] && strlen($rr['prvkey'])) {
321 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['prvkey']);
322 openssl_private_decrypt($challenge,$postvars['challenge'],$rr['prvkey']);
325 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['pubkey']);
326 openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
329 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
331 if(strpos($final_dfrn_id,':') == 1)
332 $final_dfrn_id = substr($final_dfrn_id,2);
334 if($final_dfrn_id != $orig_id) {
335 // did not decode properly - cannot trust this site
339 $postvars['dfrn_id'] = $idtosend;
341 if(($rr['rel']) && ($rr['rel'] != REL_FAN) && (! $rr['blocked']) && (! $rr['readonly'])) {
342 $postvars['data'] = $atom;
345 $postvars['data'] = $atom_nowrite;
348 $xml = post_url($rr['notify'],$postvars);
353 $res = simplexml_load_string($xml);
355 // Currently there is no retry attempt for failed mail delivery.
356 // We need to handle this in the UI, report the non-deliverables and try again
358 if(($cmd == 'mail') && (intval($res->status) == 0)) {
360 $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",