From 3c0c8a2dc1bdc196c2fb94da52c07c44c9690fd3 Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Sun, 18 Jul 2010 20:49:54 -0700 Subject: [PATCH] poller --- include/poller.php | 167 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 include/poller.php diff --git a/include/poller.php b/include/poller.php new file mode 100644 index 000000000..8c3110746 --- /dev/null +++ b/include/poller.php @@ -0,0 +1,167 @@ +set_baseurl($argv[1]); + + $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' AND `self` = 0 ORDER BY RAND()"); + + if(! count($contacts)) + killme(); + + foreach($contacts as $contact) { + + $importer_uid = $contact['uid']; + + $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", + intval($importer_uid) + ); + if(! count($r)) + continue; + + $importer = $r[0]; + + $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') + ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z') + : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z')); + + $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ; + + $xml = fetch_url($url); + + if(! $xml) + continue; + + $res = simplexml_load_string($xml); + + if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $contact['dfrn-id'])) + continue; + + $postvars = array(); + + $postvars['dfrn_id'] = $contact['dfrn-id']; + $challenge = hex2bin($res->challenge); + + openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']); + + $xml = post_url($contact['poll'],$postvars); + if(! strlen($xml)) + continue; + +echo "XML response:" . $xml . "\r\n"; +echo "Length:" . strlen($xml) . "\r\n"; + + $feed = new SimplePie(); + $feed->set_raw_data($xml); + $feed->enable_order_by_date(false); + $feed->init(); + + foreach($feed->get_items() as $item) { + + $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry'); + print_r($rawdelete); + if($deleted) { + // pick out ref and when from attribs + // check hasn't happened already, verify ownership and then process it + + + continue; + } + + $is_reply = false; + $item_id = $item->get_id(); + $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to'); + if(isset($rawthread[0]['attribs']['']['ref'])) { + $is_reply = true; + $parent_urn = $rawthread[0]['attribs']['']['ref']; + } + + + if($is_reply) { + + // Have we seen it? If not, import it. + + $item_id = $item->get_id(); + + $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1", + dbesc($item_id), + intval($importer['uid']) + ); + // FIXME update content if 'updated' changes + if(count($r)) { + $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow'); + if($allow && $allow[0]['data'] != $r[0]['last-child']) { + $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1", + intval($allow[0]['data']), + dbesc($item_id), + intval($importer['uid']) + ); + } + continue; + } + $datarray = get_atom_elements($item); + $datarray['parent_urn'] = $parent_urn; + $datarray['uid'] = $importer['uid']; + $datarray['contact-id'] = $importer['id']; + $r = post_remote($a,$datarray); + continue; + } + + else { + // Head post of a conversation. Have we seen it? If not, import it. + + $item_id = $item->get_id(); + $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1", + dbesc($item_id), + intval($importer['uid']) + ); + if(count($r)) { + $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow'); + if($allow && $allow[0]['data'] != $r[0]['last-child']) { + $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1", + intval($allow[0]['data']), + dbesc($item_id), + intval($importer['uid']) + ); + } + continue; + } + + $datarray = get_atom_elements($item); + $datarray['parent_urn'] = $item_id; + $datarray['uid'] = $importer['uid']; + $datarray['contact-id'] = $importer['id']; + $r = post_remote($a,$datarray); + continue; + + } + + } + + $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", + dbesc(datetime_convert()), + intval($contact['id']) + ); + + } + + killme(); + + +