3 require_once('simplepie/simplepie.inc');
6 function get_atom_elements($item) {
10 $author = $item->get_author();
11 $res['remote-name'] = $author->get_name();
12 $res['remote-link'] = $author->get_link();
13 $res['remote-avatar'] = $author->get_avatar();
14 $res['remote-id'] = $item->get_id();
15 $res['title'] = $item->get_title();
16 $res['body'] = $item->get_content();
18 if(strlen($res['body']) > 100000)
19 $res['body'] = substr($res['body'],0,10000) . "\r\n[Extremely large post truncated.]\r\n" ;
21 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
22 if($allow && $allow[0]['data'] == 1)
23 $res['last-child'] = 1;
25 $res['last-child'] = 0;
27 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
29 $res['created'] = $rawcreated[0]['data'];
31 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
33 $res['edited'] = $rawcreated[0]['data'];
35 $rawowner = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0", 'owner');
36 if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data'])
37 $res['owner-name'] = rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data'];
38 if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data'])
39 $res['owner-link'] = rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data'];
40 if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data'])
41 $res['owner-avatar'] = rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data'];
48 function post_remote($arr) {
50 $arr['hash'] = random_string();
51 $arr['type'] = 'remote';
52 $arr['remote-name'] = notags(trim($arr['remote-name']));
53 $arr['remote-link'] = notags(trim($arr['remote-link']));
54 $arr['remote-avatar'] = notags(trim($arr['remote-avatar']));
55 $arr['owner-name'] = notags(trim($arr['owner-name']));
56 $arr['owner-link'] = notags(trim($arr['owner-link']));
57 $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
58 if(! strlen($arr['remote-avatar']))
59 $arr['remote-avatar'] = $a->get_baseurl() . '/images/default-profile-sm.jpg';
60 if(! strlen($arr['owner-avatar']))
61 $arr['owner-avatar'] = $a->get_baseurl() . '/images/default-profile-sm.jpg';
62 $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
63 $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
64 $arr['title'] = notags(trim($arr['title']));
65 $arr['body'] = escape_tags(trim($arr['body']));
66 $arr['last-child'] = intval($arr['last-child']);
70 $parent = $arr['parent_urn'];
72 unset($arr['parent_urn']);
78 $r = q("INSERT INTO `item` (`"
79 . implode("`, `", array_keys($arr))
81 . implode("', '", array_values($arr))
85 $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
90 $parent_id = $r[0]['id'];
93 $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
98 $current_post = $r[0]['id'];
100 $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
102 intval($current_post)
107 function dfrn_notify_post(&$a) {
109 $dfrn_id = notags(trim($_POST['dfrn_id']));
110 $challenge = notags(trim($_POST['challenge']));
111 $data = $_POST['data'];
112 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
119 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
124 // find the local user who owns this relationship.
126 $r = q("SELECT `id`, `uid` FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
135 $feed = new SimplePie();
136 $feed->set_raw_data($data);
137 $feed->enable_order_by_date(false);
140 foreach($feed->get_items() as $item) {
142 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
145 // pick out ref and when from attribs
146 // check hasn't happened already, verify ownership and then process it
153 $item_id = $item->get_id();
154 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
155 if(isset($rawthread[0]['attribs']['']['ref'])) {
157 $parent_urn = $rawthread[0]['attribs']['']['ref'];
162 if($x == ($total_items - 1)) {
163 // remote reply to our post. Import and then notify everybody else.
166 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
168 $item_id = $item->get_id();
170 $r = q("SELECT `uid`, `last-child` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
172 intval($importer['uid'])
175 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
176 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
177 $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
178 intval($allow[0]['data']),
184 $datarray = get_atom_elements($item);
185 $datarray['parent_urn'] = $parent_urn;
186 $datarray['uid'] = $importer['uid'];
187 $datarray['contact-id'] = $importer['id'];
188 $r = post_remote($datarray);
193 // Head post of a conversation. Have we seen it? If not, import it.
195 $item_id = $item->get_id();
196 $r = q("SELECT `uid` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
198 intval($importer['uid'])
201 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
202 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
203 $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
204 intval($allow[0]['data']),
212 $datarray = get_atom_elements($item);
213 $datarray['parent_urn'] = $item_id;
214 $datarray['uid'] = $importer['uid'];
215 $datarray['contact-id'] = $importer['id'];
216 $r = post_remote($datarray);
237 function dfrn_notify_content(&$a) {
239 if(x($_GET,'dfrn_id')) {
240 // initial communication from external contact
241 $hash = random_string();
245 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
247 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
248 VALUES( '%s', '%s', '%s') ",
250 dbesc(notags(trim($_GET['dfrn_id']))),
254 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 LIMIT 1",
255 dbesc($_GET['dfrn_id']));
256 if((! count($r)) || (! strlen($r[0]['prvkey'])))
261 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
262 $challenge = bin2hex($challenge);
263 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $_GET['dfrn_id'] . '</dfrn_id>'
264 . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
265 session_write_close();