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');
15 require_once('simplepie/simplepie.inc');
16 require_once('include/items.php');
21 $a->set_baseurl($argv[1]);
23 $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' AND `self` = 0 ORDER BY RAND()");
25 if(! count($contacts))
28 foreach($contacts as $contact) {
30 $importer_uid = $contact['uid'];
32 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
40 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
41 ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
42 : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
44 $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
46 $xml = fetch_url($url);
51 $res = simplexml_load_string($xml);
53 if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $contact['dfrn-id']))
58 $postvars['dfrn_id'] = $contact['dfrn-id'];
59 $challenge = hex2bin($res->challenge);
61 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
63 $xml = post_url($contact['poll'],$postvars);
67 echo "XML response:" . $xml . "\r\n";
68 echo "Length:" . strlen($xml) . "\r\n";
70 $feed = new SimplePie();
71 $feed->set_raw_data($xml);
72 $feed->enable_order_by_date(false);
75 foreach($feed->get_items() as $item) {
77 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
80 // pick out ref and when from attribs
81 // check hasn't happened already, verify ownership and then process it
88 $item_id = $item->get_id();
89 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
90 if(isset($rawthread[0]['attribs']['']['ref'])) {
92 $parent_uri = $rawthread[0]['attribs']['']['ref'];
98 // Have we seen it? If not, import it.
100 $item_id = $item->get_id();
102 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
104 intval($importer['uid'])
106 // FIXME update content if 'updated' changes
108 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
109 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
110 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
111 intval($allow[0]['data']),
113 intval($importer['uid'])
118 $datarray = get_atom_elements($item);
119 $datarray['parent-uri'] = $parent_uri;
120 $datarray['uid'] = $importer['uid'];
121 $datarray['contact-id'] = $contact['id'];
122 $r = post_remote($a,$datarray);
127 // Head post of a conversation. Have we seen it? If not, import it.
129 $item_id = $item->get_id();
130 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
132 intval($importer['uid'])
135 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
136 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
137 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
138 intval($allow[0]['data']),
140 intval($importer['uid'])
146 $datarray = get_atom_elements($item);
147 $datarray['parent-uri'] = $item_id;
148 $datarray['uid'] = $importer['uid'];
149 $datarray['contact-id'] = $contact['id'];
150 $r = post_remote($a,$datarray);
157 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
158 dbesc(datetime_convert()),
159 intval($contact['id'])