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");
20 $a->set_baseurl(get_config('system','url'));
28 $item_id = intval($argv[2]);
37 $recipients = array();
41 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
46 $uid = $message[0]['uid'];
47 $recipients[] = $message[0]['contact-id'];
54 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
60 $parent = $r[0]['parent'];
62 $updated = $r[0]['edited'];
64 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
72 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
83 require_once('include/group.php');
87 if($parent['type'] == 'remote') {
88 // local followup to remote post
90 $conversant_str = dbesc($parent['contact-id']);
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']));
100 $conversants = array();
102 foreach($items as $item) {
103 $recipients[] = $item['contact-id'];
104 $conversants[] = $item['contact-id'];
107 $conversants = array_unique($conversants,SORT_NUMERIC);
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);
114 $conversant_str = dbesc(implode(', ',$conversants));
117 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
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');
129 $feed_template = file_get_contents('view/atom_feed.tpl');
130 $mail_template = file_get_contents('view/atom_mail.tpl');
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'))
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'])
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
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'))
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)
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'])) && (! $rr['duplex']))
269 $idtosend = (($rr['dfrn-id']) ? $rr['dfrn-id'] : $rr['issued-id']);
271 $url = $rr['notify'] . '?dfrn_id=' . $idtosend;
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);
288 $challenge = hex2bin($res->challenge);
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']);
296 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['pubkey']);
297 openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
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
306 $postvars['dfrn_id'] = $idtosend;
308 if((($rr['rel'] == DIRECTION_OUT) || ($rr['rel'] == DIRECTION_BOTH)) && (! $rr['blocked']) && (! $rr['readonly'])) {
309 $postvars['data'] = $atom;
312 $postvars['data'] = $atom_nowrite;
315 $xml = post_url($rr['notify'],$postvars);
320 $res = simplexml_load_string($xml);
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
325 if(($cmd == 'mail') && (intval($res->status) == 0)) {
327 $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",