From dd00fcc7bb3a3b50bd453fddb9a88b7f74d24140 Mon Sep 17 00:00:00 2001 From: friendica Date: Sat, 10 Nov 2012 20:08:07 -0800 Subject: starting to get into the hairy parts of zot - identity, location, and permission synchronisation. After this, messaging should be a piece of cake. --- include/zot.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- mod/post.php | 35 +++++++++++++++++------- mod/zfinger.php | 6 ++--- version.inc | 2 +- 4 files changed, 111 insertions(+), 16 deletions(-) diff --git a/include/zot.php b/include/zot.php index 3d1669c0f..1b8b8713a 100644 --- a/include/zot.php +++ b/include/zot.php @@ -59,9 +59,9 @@ function zot_verify(&$item,$identity) { -function zot_notify($channel,$url) { +function zot_notify($channel,$url,$type = 'notify') { $x = z_post_url($url, array( - 'type' => 'notify', + 'type' => $type, 'guid' => $channel['channel_guid'], 'guid_sig' => base64url_encode($guid,$channel['prvkey']), 'hub' => z_root(), @@ -125,6 +125,86 @@ function zot_finger($webbie,$channel) { } +function zot_refresh($them,$channel) { + + if($them['hubloc_url']) + $url = $them['hubloc_url']; + else { + $r = q("select hubloc_url from hubloc where hubloc_hash = '%s' and hubloc_flags & %d limit 1", + dbesc($them['xchan_hash']), + intval(HUBLOC_FLAGS_PRIMARY) + ); + if($r) + $url = $r[0]['hubloc_url']; + } + if(! $url) + return; + + if($them['xchan_hash']) + $guid_hash = $them['xchan_hash']; + + if(! $guid_hash) + return; + + $rhs = '/.well-known/zot-guid'; + + $postvars = array( + 'guid_hash' => $guid_hash, + 'target' => $channel['channel_guid'], + 'target_sig' => $channel['channel_guid_sig'], + 'key' => $channel['channel_pubkey'] + ); + $result = z_post_url($url . $rhs,$postvars); + + if($result['success']) { + + $j = json_decode($result['body']); + + $x = import_xchan_from_json($j); + + if(! $x['success']) + return $x; + + $xchan_hash = $x['hash']; + + $their_perms = 0; + + $global_perms = get_perms(); + + if($j->permissions->data) { + $permissions = aes_unencapsulate(array( + 'data' => $j->permissions->data, + 'key' => $j->permissions->key, + 'iv' => $j->permissions->iv), + $channel['channel_prvkey']); + if($permissions) + $permissions = json_decode($permissions); + logger('decrypted permissions: ' . print_r($permissions,true), LOGGER_DATA); + } + else + $permissions = $j->permissions; + + foreach($permissions as $k => $v) { + if($v) { + $their_perms = $their_perms | intval($global_perms[$k][1]); + } + } + + $r = q("update abook set their_perms = %d where abook_xchan = '%s' and abook_channel = %d limit 1", + intval($their_perms), + dbesc($channel['channel_hash']), + intval($channel['channel_id']) + ); + if(! $r) + logger('abook update failed'); + + return true; + + } + return false; + +} + function zot_gethub($arr) { diff --git a/mod/post.php b/mod/post.php index a33c44e57..ef94c9b5d 100644 --- a/mod/post.php +++ b/mod/post.php @@ -13,18 +13,33 @@ function post_post(&$a) { $msgtype = ((x($_REQUEST,'type')) ? $_REQUEST['type'] : ''); - if($msgtype === 'notify') { - - $hub = zot_gethub($_REQUEST); - if(! $hub) { - $result = zot_register_hub($_REQUEST); - if((! $result) || (! zot_gethub($_REQUEST))) { - $ret['message'] = 'Hub not available.'; - json_return_and_die($ret); - } + $hub = zot_gethub($_REQUEST); + if(! $hub) { + $result = zot_register_hub($_REQUEST); + if((! $result) || (! zot_gethub($_REQUEST))) { + $ret['message'] = 'Hub not available.'; + json_return_and_die($ret); } + } + + // check which hub is primary and take action if mismatched + + + if($msgtype === 'refresh') { + + // Need to pass the recipient in the message + + // look up recipient + + // format args + // $r = zot_refresh($them,$channel); + + return; + + } + + if($msgtype === 'notify') { - // check which hub is primary and take action if mismatched // add to receive queue // qreceive_add($_REQUEST); diff --git a/mod/zfinger.php b/mod/zfinger.php index 80411d16c..cc4b8cdd3 100644 --- a/mod/zfinger.php +++ b/mod/zfinger.php @@ -7,7 +7,7 @@ function zfinger_init(&$a) { $ret = array('success' => false); - $zguid = ((x($_REQUEST,'guid')) ? $_REQUEST['guid'] : ''); + $zhash = ((x($_REQUEST,'guid_hash')) ? $_REQUEST['guid_hash'] : ''); $zaddr = ((x($_REQUEST,'address')) ? $_REQUEST['address'] : ''); $ztarget = ((x($_REQUEST,'target')) ? $_REQUEST['target'] : ''); $zsig = ((x($_REQUEST,'target_sig')) ? $_REQUEST['target_sig'] : ''); @@ -25,8 +25,8 @@ function zfinger_init(&$a) { if(strlen($zguid)) { $r = q("select channel.*, xchan.* from channel left join xchan on channel_hash = xchan_hash - where channel_guid = '%s' limit 1", - dbesc($zguid) + where channel_hash = '%s' limit 1", + dbesc($zhash) ); } elseif(strlen($zaddr)) { diff --git a/version.inc b/version.inc index 6b7181576..f4b5b2086 100644 --- a/version.inc +++ b/version.inc @@ -1 +1 @@ -2012-11-09.133 +2012-11-10.134 -- cgit v1.2.3