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) {
79 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
80 if(isset($rawdelete[0]['attribs']['']['ref'])) {
81 $uri = $rawthread[0]['attribs']['']['ref'];
83 if(isset($rawdelete[0]['attribs']['']['when'])) {
84 $when = $rawthread[0]['attribs']['']['when'];
85 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
88 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
91 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
93 intval($importer['uid'])
96 if($r[0]['uri'] == $r[0]['parent-uri']) {
97 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
98 WHERE `parent-uri` = '%s'"
104 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
105 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
108 intval($importer['uid'])
117 $item_id = $item->get_id();
118 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
119 if(isset($rawthread[0]['attribs']['']['ref'])) {
121 $parent_uri = $rawthread[0]['attribs']['']['ref'];
127 // Have we seen it? If not, import it.
129 $item_id = $item->get_id();
131 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
133 intval($importer['uid'])
135 // FIXME update content if 'updated' changes
137 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
138 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
139 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
140 intval($allow[0]['data']),
142 intval($importer['uid'])
147 $datarray = get_atom_elements($item);
148 $datarray['parent-uri'] = $parent_uri;
149 $datarray['uid'] = $importer['uid'];
150 $datarray['contact-id'] = $contact['id'];
151 $r = post_remote($a,$datarray);
156 // Head post of a conversation. Have we seen it? If not, import it.
158 $item_id = $item->get_id();
159 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
161 intval($importer['uid'])
164 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
165 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
166 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
167 intval($allow[0]['data']),
169 intval($importer['uid'])
175 $datarray = get_atom_elements($item);
176 $datarray['parent-uri'] = $item_id;
177 $datarray['uid'] = $importer['uid'];
178 $datarray['contact-id'] = $contact['id'];
179 $r = post_remote($a,$datarray);
186 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
187 dbesc(datetime_convert()),
188 intval($contact['id'])