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 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
25 if(! count($contacts))
28 foreach($contacts as $contact) {
30 if($contact['priority']) {
33 $t = $contact['last-update'];
35 switch ($contact['priority']) {
37 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month"))
41 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week"))
45 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day"))
49 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour"))
54 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour"))
63 $importer_uid = $contact['uid'];
65 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
73 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
74 ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
75 : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
77 $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
79 $xml = fetch_url($url);
85 $res = simplexml_load_string($xml);
87 if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $contact['dfrn-id']))
92 $postvars['dfrn_id'] = $contact['dfrn-id'];
93 $challenge = hex2bin($res->challenge);
95 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
97 $xml = post_url($contact['poll'],$postvars);
99 echo "XML response:" . $xml . "\r\n";
100 echo "Length:" . strlen($xml) . "\r\n";
103 // an empty response may mean there's nothing new - record the fact that we checked
104 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
105 dbesc(datetime_convert()),
106 intval($contact['id'])
111 $feed = new SimplePie();
112 $feed->set_raw_data($xml);
113 $feed->enable_order_by_date(false);
116 foreach($feed->get_items() as $item) {
120 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
121 if(isset($rawdelete[0]['attribs']['']['ref'])) {
122 $uri = $rawthread[0]['attribs']['']['ref'];
124 if(isset($rawdelete[0]['attribs']['']['when'])) {
125 $when = $rawthread[0]['attribs']['']['when'];
126 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
129 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
132 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
134 intval($importer['uid'])
137 if($r[0]['uri'] == $r[0]['parent-uri']) {
138 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
139 WHERE `parent-uri` = '%s'",
145 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
146 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
149 intval($importer['uid'])
158 $item_id = $item->get_id();
159 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
160 if(isset($rawthread[0]['attribs']['']['ref'])) {
162 $parent_uri = $rawthread[0]['attribs']['']['ref'];
168 // Have we seen it? If not, import it.
170 $item_id = $item->get_id();
172 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
174 intval($importer['uid'])
176 // FIXME update content if 'updated' changes
178 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
179 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
180 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
181 intval($allow[0]['data']),
183 intval($importer['uid'])
188 $datarray = get_atom_elements($item);
189 $datarray['parent-uri'] = $parent_uri;
190 $datarray['uid'] = $importer['uid'];
191 $datarray['contact-id'] = $contact['id'];
192 $r = post_remote($a,$datarray);
197 // Head post of a conversation. Have we seen it? If not, import it.
199 $item_id = $item->get_id();
200 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
202 intval($importer['uid'])
205 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
206 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
207 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
208 intval($allow[0]['data']),
210 intval($importer['uid'])
216 $datarray = get_atom_elements($item);
217 $datarray['parent-uri'] = $item_id;
218 $datarray['uid'] = $importer['uid'];
219 $datarray['contact-id'] = $contact['id'];
220 $r = post_remote($a,$datarray);
227 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
228 dbesc(datetime_convert()),
229 intval($contact['id'])