basic hub registration

This commit is contained in:
friendica 2012-08-21 23:11:27 -07:00
parent 72d38d75bf
commit 2140c2ea37
4 changed files with 49 additions and 35 deletions

View File

@ -204,6 +204,12 @@ define ( 'NOTIFY_POKE', 0x0200 );
define ( 'NOTIFY_SYSTEM', 0x8000 ); define ( 'NOTIFY_SYSTEM', 0x8000 );
define ( 'HUBLOC_FLAGS_PRIMARY', 0x0001);
define ( 'HUBLOC_FLAGS_UNVERIFIED', 0x0002);
/** /**
* Tag/term types * Tag/term types
*/ */

View File

@ -19,6 +19,7 @@ function zot_new_uid($entity_id) {
* Given an array of zot_uid(s), return all distinct hubs * Given an array of zot_uid(s), return all distinct hubs
* If primary is true, return only primary hubs * If primary is true, return only primary hubs
* Result is ordered by url to assist in batching. * Result is ordered by url to assist in batching.
* Return only the first primary hub as there should only be one.
* *
*/ */
@ -37,8 +38,9 @@ function zot_get_hubloc($arr,$primary = false) {
if(! strlen($tmp)) if(! strlen($tmp))
return array(); return array();
$sql_extra = (($primary) ? " and hubloc_primary = 1 " : "" ); $sql_extra = (($primary) ? " and hubloc_flags & " . intval(HUBLOC_FLAGS_PRIMARY) : "" );
return q("select * from hubloc where hubloc_guid in ( $tmp ) $sql_extra order by hubloc_url"); $limit = (($primary) ? " limit 1 " : "");
return q("select * from hubloc where hubloc_guid in ( $tmp ) $sql_extra order by hubloc_url $limit");
} }
@ -85,28 +87,31 @@ function zot_gethub($arr) {
} }
function zot_register_hub($arr) { function zot_register_hub($arr) {
$total = 0;
if((x($arr,'hub')) && (x($arr,'guid'))) { if((x($arr,'hub')) && (x($arr,'guid'))) {
$x = z_fetch_url($arr['hub'] . '/.well-known/zot-guid/' . $arr['guid']); $x = z_fetch_url($arr['hub'] . '/.well-known/zot-guid/' . $arr['guid']);
if($x['success']) { if($x['success']) {
$record = json_decode($x['body']); $record = json_decode($x['body']);
if($record->guid === $arr['guid'] && $record->url === $arr['hub']) { if($record->hub && count($record->hub)) {
$r = q("insert into hubloc (hubloc_guid, hubloc_primary, hubloc_url, foreach($record->hub as $h) {
// store any hubs we don't know about
if( ! zot_gethub(array('hub' => $h->url, 'guid' => $arr['guid']))) {
$r = q("insert into hubloc (hubloc_guid, hubloc_flags, hubloc_url,
hubloc_callback, hubloc_sitekey, hubloc_key) hubloc_callback, hubloc_sitekey, hubloc_key)
values ( '%s', %d, '%s', '%s', '%s', '%s' )", values ( '%s', %d, '%s', '%s', '%s', '%s' )",
dbesc($arr['guid']), dbesc($arr['guid']),
intval($record->primary), intval((($h->primary) ? HUBLOC_FLAGS_PRIMARY : 0) | HUBLOC_FLAGS_UNVERIFIED ),
dbesc($record->url), dbesc($h->url),
dbesc($record->callback), dbesc($h->callback),
dbesc($record->sitekey), dbesc($h->sitekey),
dbesc($record->key) dbesc($record->key)
); );
// return the discovery record so we can further process
if($r) if($r)
return $record; $total ++;
} }
} }
} }
return false; }
}
return $total;
} }

View File

@ -15,16 +15,16 @@ function post_post(&$a) {
if($msgtype === 'notify') { if($msgtype === 'notify') {
$hub = zot_gethub($_REQUEST['guid']); $hub = zot_gethub($_REQUEST);
if(! $hub) { if(! $hub) {
$result = zot_register_hub($_REQUEST); $result = zot_register_hub($_REQUEST);
if(! $result) { if((! $result) || (! zot_gethub($_REQUEST))) {
$ret['message'] = 'Hub not available.'; $ret['message'] = 'Hub not available.';
json_return_and_dir($ret); json_return_and_die($ret);
} }
} }
// check which hub is primary and take action if mismatched
// check which hub is primary and take action if mismatched
// add to receive queue // add to receive queue
// qreceive_add($_REQUEST); // qreceive_add($_REQUEST);

View File

@ -25,27 +25,30 @@ function zfinger_init(&$a) {
$ret['success'] = true; $ret['success'] = true;
// Communication details
$ret['guid'] = $e['entity_global_id']; $ret['guid'] = $e['entity_global_id'];
$ret['url'] = z_root();
$ret['primary'] = (bool) $e['entity_primary'];
$ret['callback'] = z_root() . '/' . 'post';
$ret['sitekey'] = get_config('system','pubkey');
$ret['key'] = $e['pubkey']; $ret['key'] = $e['pubkey'];
// array of (verified) hubs this entity uses
$ret['hubs'] = array(); $ret['hubs'] = array();
$x = zot_get_hubloc(array($e['entity_global_id'])); $x = zot_get_hubloc(array($e['entity_global_id']));
if($x && count($x)) { if($x && count($x)) {
foreach($x as $hub) { foreach($x as $hub) {
if(! ($hub['hubloc_flags'] & HUBLOC_FLAGS_UNVERIFIED)) {
$ret['hubs'][] = array( $ret['hubs'][] = array(
'primary' => (bool) $hub['hubloc_primary'], 'primary' => (($hub['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY) ? true : false),
'url' => $hub['hubloc_url'], 'url' => $hub['hubloc_url'],
'callback' => $hub['hubloc_callback'], 'callback' => $hub['hubloc_callback'],
'sitekey' => $hub['hubloc_sitekey'] 'sitekey' => $hub['hubloc_sitekey']
); );
} }
} }
}
// more stuff
// more stuff, e.g. the basic public profile
json_return_and_die($ret); json_return_and_die($ret);