DATABASE: outq additions, delivery now getting the notify across to the other side

This commit is contained in:
friendica 2012-11-19 16:51:15 -08:00
parent db77309bc3
commit 194ef02c28
6 changed files with 140 additions and 67 deletions

40
include/deliver.php Normal file
View File

@ -0,0 +1,40 @@
<?php
require_once('include/cli_startup.php');
require_once('include/zot.php');
function deliver_run($argv, $argc) {
cli_startup();
$a = get_app();
if($argc < 2)
return;
logger('deliver: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
for($x = 1; $x < $argc; $x ++) {
$r = q("select * from outq where outq_hash = '%s' limit 1",
dbesc($argv[$x])
);
if($r) {
$result = zot_zot($r[0]['outq_posturl'],$r[0]['outq_notify']);
if($result['success']) {
zot_process_response($result, $r[0]);
}
else {
$y = q("update outq set outq_updated = '%s' where outq_hash = '%s' limit 1",
dbesc(datetime_convert()),
dbesc($argv[$x])
);
}
}
}
}
if (array_search(__file__,get_included_files())===0){
deliver_run($argv,$argc);
killme();
}

View File

@ -184,6 +184,8 @@ function notifier_run($argv, $argc){
$s = q("select * from channel where channel_id = %d limit 1", $s = q("select * from channel where channel_id = %d limit 1",
intval($target_item['uid']) intval($target_item['uid'])
); );
if($s)
$channel = $s[0];
if($target_item['id'] == $target_item['parent']) { if($target_item['id'] == $target_item['parent']) {
$parent_item = $target_item; $parent_item = $target_item;
@ -261,12 +263,13 @@ function notifier_run($argv, $argc){
foreach($hubs as $hub) { foreach($hubs as $hub) {
$hash = random_string(); $hash = random_string();
$n = zot_build_packet($channel,'notify',null,(($private) ? $hub['hubloc_sitekey'],$hash); $n = zot_build_packet($channel,'notify',null,(($private) ? $hub['hubloc_sitekey'] : null),$hash);
q("insert into outq ( outq_hash, outq_account, outq_channel, outq_posturl, outq_created, outq_updated, outq_notify, outq_msg ) values ( '%s', %d, %d, '%s', '%s', '%s', '%s', '%s' )", q("insert into outq ( outq_hash, outq_account, outq_channel, outq_posturl, outq_async, outq_created, outq_updated, outq_notify, outq_msg ) values ( '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s' )",
dbesc($hash), dbesc($hash),
intval($target_item['aid']), intval($target_item['aid']),
intval($target_item['uid']), intval($target_item['uid']),
dbesc($hub['hubloc_callback']), dbesc($hub['hubloc_callback']),
intval(1),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc($n), dbesc($n),

View File

@ -212,9 +212,9 @@ function zot_refresh($them,$channel = null) {
if($result['success']) { if($result['success']) {
$j = json_decode($result['body']); $j = json_decode($result['body'],true);
$x = import_xchan_from_json($j); $x = import_xchan($j);
if(! $x['success']) if(! $x['success'])
return $x; return $x;
@ -226,18 +226,18 @@ function zot_refresh($them,$channel = null) {
if($channel) { if($channel) {
$global_perms = get_perms(); $global_perms = get_perms();
if($j->permissions->data) { if($j['permissions']['data']) {
$permissions = aes_unencapsulate(array( $permissions = aes_unencapsulate(array(
'data' => $j->permissions->data, 'data' => $j['permissions']['data'],
'key' => $j->permissions->key, 'key' => $j['permissions']['key'],
'iv' => $j->permissions->iv), 'iv' => $j['permissions']['iv']),
$channel['channel_prvkey']); $channel['channel_prvkey']);
if($permissions) if($permissions)
$permissions = json_decode($permissions); $permissions = json_decode($permissions,true);
logger('decrypted permissions: ' . print_r($permissions,true), LOGGER_DATA); logger('decrypted permissions: ' . print_r($permissions,true), LOGGER_DATA);
} }
else else
$permissions = $j->permissions; $permissions = $j['permissions'];
foreach($permissions as $k => $v) { foreach($permissions as $k => $v) {
if($v) { if($v) {
@ -265,17 +265,17 @@ function zot_refresh($them,$channel = null) {
} }
function zot_gethub($jarr) { function zot_gethub($arr) {
if($jarr->guid && $jarr->guid_sig && $jarr->url && $jarr->url_sig) { if($arr['guid'] && $arr['guid_sig'] && $arr['url'] && $arr['url_sig']) {
$r = q("select * from hubloc $r = q("select * from hubloc
where hubloc_guid = '%s' and hubloc_guid_sig = '%s' where hubloc_guid = '%s' and hubloc_guid_sig = '%s'
and hubloc_url = '%s' and hubloc_url_sig = '%s' and hubloc_url = '%s' and hubloc_url_sig = '%s'
limit 1", limit 1",
dbesc($jarr->guid), dbesc($arr['guid']),
dbesc($jarr->guid_sig), dbesc($arr['guid_sig']),
dbesc($jarr->url), dbesc($arr['url']),
dbesc($jarr->url_sig) dbesc($arr['url_sig'])
); );
if($r && count($r)) if($r && count($r))
return $r[0]; return $r[0];
@ -287,15 +287,15 @@ function zot_register_hub($arr) {
$result = array('success' => false); $result = array('success' => false);
if($arr->hub && $arr->hub_sig && $arr->guid && $arr->guid_sig) { if($arr['hub'] && $arr['hub_sig'] && $arr['guid'] && $arr['guid_sig']) {
$guid_hash = base64url_encode(hash('whirlpool',$arr->guid . $arr->guid_sig, true)); $guid_hash = base64url_encode(hash('whirlpool',$arr['guid'] . $arr['guid_sig'], true));
$x = z_fetch_url($arr->hub . '/.well-known/zot-info/?f=&hash=' . $guid_hash); $x = z_fetch_url($arr['hub'] . '/.well-known/zot-info/?f=&hash=' . $guid_hash);
if($x['success']) { if($x['success']) {
$record = json_decode($x['body']); $record = json_decode($x['body'],true);
$c = import_xchan_from_json($record); $c = import_xchan($record);
if($c['success']) if($c['success'])
$result['success'] = true; $result['success'] = true;
} }
@ -427,15 +427,15 @@ function import_xchan_from_json($j) {
// //
function import_xchan($j) { function import_xchan($arr) {
$ret = array('success' => false); $ret = array('success' => false);
$xchan_hash = base64url_encode(hash('whirlpool',$j['guid'] . $j['guid_sig'], true)); $xchan_hash = base64url_encode(hash('whirlpool',$arr['guid'] . $arr['guid_sig'], true));
$import_photos = false; $import_photos = false;
if(! rsa_verify($j['guid'],base64url_decode($j['guid_sig']),$j['key'])) { if(! rsa_verify($arr['guid'],base64url_decode($arr['guid_sig']),$arr['key'])) {
logger('import_xchan_from_json: Unable to verify channel signature for ' . $j['address']); logger('import_xchan_from_json: Unable to verify channel signature for ' . $arr['address']);
$ret['message'] = t('Unable to verify channel signature'); $ret['message'] = t('Unable to verify channel signature');
return $ret; return $ret;
} }
@ -445,12 +445,12 @@ function import_xchan($j) {
); );
if($r) { if($r) {
if($r[0]['xchan_photo_date'] != $j['photo_updated']) if($r[0]['xchan_photo_date'] != $arr['photo_updated'])
$update_photos = true; $update_photos = true;
if($r[0]['xchan_name_date'] != $j['name_updated']) { if($r[0]['xchan_name_date'] != $arr['name_updated']) {
$r = q("update xchan set xchan_name = '%s', xchan_name_date = '%s' where xchan_hash = '%s' limit 1", $r = q("update xchan set xchan_name = '%s', xchan_name_date = '%s' where xchan_hash = '%s' limit 1",
dbesc($j['name']), dbesc($arr['name']),
dbesc($j['name_updated']), dbesc($arr['name_updated']),
dbesc($xchan_hash) dbesc($xchan_hash)
); );
} }
@ -461,17 +461,17 @@ function import_xchan($j) {
xchan_photo_l, xchan_addr, xchan_url, xchan_name, xchan_network, xchan_photo_date, xchan_name_date) xchan_photo_l, xchan_addr, xchan_url, xchan_name, xchan_network, xchan_photo_date, xchan_name_date)
values ( '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ", values ( '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ",
dbesc($xchan_hash), dbesc($xchan_hash),
dbesc($j['guid']), dbesc($arr['guid']),
dbesc($j['guid_sig']), dbesc($arr['guid_sig']),
dbesc($j['key']), dbesc($arr['key']),
dbesc($j['photo_mimetype']), dbesc($arr['photo_mimetype']),
dbesc($j['photo']), dbesc($arr['photo']),
dbesc($j['address']), dbesc($arr['address']),
dbesc($j['url']), dbesc($arr['url']),
dbesc($j['name']), dbesc($arr['name']),
dbesc('zot'), dbesc('zot'),
dbesc($j['photo_updated']), dbesc($arr['photo_updated']),
dbesc($j['name_updated']) dbesc($arr['name_updated'])
); );
} }
@ -481,10 +481,10 @@ function import_xchan($j) {
require_once("Photo.php"); require_once("Photo.php");
$photos = import_profile_photo($j['photo'],0,$xchan_hash); $photos = import_profile_photo($arr['photo'],0,$xchan_hash);
$r = q("update xchan set xchan_photo_date = '%s', xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s' $r = q("update xchan set xchan_photo_date = '%s', xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s'
where xchan_hash = '%s' limit 1", where xchan_hash = '%s' limit 1",
dbesc($j['photo_updated']), dbesc($arr['photo_updated']),
dbesc($photos[0]), dbesc($photos[0]),
dbesc($photos[1]), dbesc($photos[1]),
dbesc($photos[2]), dbesc($photos[2]),
@ -493,9 +493,9 @@ function import_xchan($j) {
); );
} }
if($j['locations']) { if($arr['locations']) {
foreach($j['locations'] as $location) { foreach($arr['locations'] as $location) {
if(! rsa_verify($location['url'],base64url_decode($location['url_sig']),$j['key'])) { if(! rsa_verify($location['url'],base64url_decode($location['url_sig']),$arr['key'])) {
logger('import_xchan_from_json: Unable to verify site signature for ' . $location['url']); logger('import_xchan_from_json: Unable to verify site signature for ' . $location['url']);
$ret['message'] .= sprintf( t('Unable to verify site signature for %s'), $location['url']) . EOL; $ret['message'] .= sprintf( t('Unable to verify site signature for %s'), $location['url']) . EOL;
continue; continue;
@ -517,8 +517,8 @@ function import_xchan($j) {
$r = q("insert into hubloc ( hubloc_guid, hubloc_guid_sig, hubloc_hash, hubloc_addr, hubloc_flags, hubloc_url, hubloc_url_sig, hubloc_host, hubloc_callback, hubloc_sitekey) $r = q("insert into hubloc ( hubloc_guid, hubloc_guid_sig, hubloc_hash, hubloc_addr, hubloc_flags, hubloc_url, hubloc_url_sig, hubloc_host, hubloc_callback, hubloc_sitekey)
values ( '%s','%s','%s','%s', %d ,'%s','%s','%s','%s','%s')", values ( '%s','%s','%s','%s', %d ,'%s','%s','%s','%s','%s')",
dbesc($j['guid']), dbesc($arr['guid']),
dbesc($j['guid_sig']), dbesc($arr['guid_sig']),
dbesc($xchan_hash), dbesc($xchan_hash),
dbesc($location['address']), dbesc($location['address']),
intval((intval($location['primary'])) ? HUBLOC_FLAGS_PRIMARY : 0), intval((intval($location['primary'])) ? HUBLOC_FLAGS_PRIMARY : 0),
@ -540,3 +540,28 @@ function import_xchan($j) {
return $ret; return $ret;
} }
function zot_process_response($arr,$outq) {
if(! $arr['success'])
return;
$x = json_decode($arr['body'],true);
// synchronous message types are handled immediately
// async messages remain in the queue until processed.
if(intval($outq['outq_async'])) {
$r = q("update outq set outq_delivered = 1, outq_updated = '%s' where outq_hash = '%s' and outq_channel = %d limit 1",
dbesc(datetime_convert()),
dbesc($outq['outq_hash']),
intval($outq['outq_channel'])
);
}
else {
$r = q("delete from outq where outq_hash = '%s' and outq_channel = %d limit 1",
dbesc($outq['outq_hash']),
intval($outq['outq_channel'])
);
}
logger('zot_process_response: ' . print_r($x,true), LOGGER_DATA);
}

View File

@ -675,6 +675,8 @@ CREATE TABLE IF NOT EXISTS `outq` (
`outq_account` int(10) unsigned NOT NULL DEFAULT '0', `outq_account` int(10) unsigned NOT NULL DEFAULT '0',
`outq_channel` int(10) unsigned NOT NULL DEFAULT '0', `outq_channel` int(10) unsigned NOT NULL DEFAULT '0',
`outq_posturl` char(255) NOT NULL DEFAULT '', `outq_posturl` char(255) NOT NULL DEFAULT '',
`outq_async` tinyint(1) NOT NULL DEFAULT '0',
`outq_delivered` tinyint(1) NOT NULL DEFAULT '0',
`outq_created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `outq_created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`outq_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `outq_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`outq_notify` mediumtext NOT NULL, `outq_notify` mediumtext NOT NULL,
@ -684,7 +686,9 @@ CREATE TABLE IF NOT EXISTS `outq` (
KEY `outq_channel` (`outq_channel`), KEY `outq_channel` (`outq_channel`),
KEY `outq_hub` (`outq_posturl`), KEY `outq_hub` (`outq_posturl`),
KEY `outq_created` (`outq_created`), KEY `outq_created` (`outq_created`),
KEY `outq_updated` (`outq_updated`) KEY `outq_updated` (`outq_updated`),
KEY `outq_async` (`outq_async`),
KEY `outq_delivered` (`outq_delivered`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `pconfig` ( CREATE TABLE IF NOT EXISTS `pconfig` (

View File

@ -15,23 +15,25 @@ function post_post(&$a) {
if(array_key_exists('iv',$_REQUEST)) { if(array_key_exists('iv',$_REQUEST)) {
// hush-hush ultra top secret mode // hush-hush ultra top secret mode
$data = aes_unencapsulate($_REQUEST,get_config('system','site_prvkey')); $data = json_decode(aes_unencapsulate($_REQUEST['data'],get_config('system','site_prvkey')),true);
} }
else { else {
$data = $_REQUEST; $data = json_decode($_REQUEST['data'],true);
} }
$msgtype = ((array_key_exists('type',$data)) ? $data['type'] : ''); $msgtype = ((array_key_exists('type',$data)) ? $data['type'] : '');
if(array_key_exists('sender',$data)) { if(array_key_exists('sender',$data)) {
$j_sender = json_decode($data['sender']); $sender = $data['sender'];
} }
$hub = zot_gethub($j_sender); $hub = zot_gethub($sender);
if(! $hub) { if(! $hub) {
// (!!) this will validate the sender // (!!) this will validate the sender
$result = zot_register_hub($j_sender); $result = zot_register_hub($sender);
if((! $result['success']) || (! zot_gethub($j_sender))) { if((! $result['success']) || (! zot_gethub($sender))) {
$ret['message'] = 'Hub not available.'; $ret['message'] = 'Hub not available.';
logger('mod_zot: no hub'); logger('mod_zot: no hub');
json_return_and_die($ret); json_return_and_die($ret);
@ -41,29 +43,29 @@ function post_post(&$a) {
// TODO: check which hub is primary and take action if mismatched // TODO: check which hub is primary and take action if mismatched
if(array_key_exists('recipients',$data)) if(array_key_exists('recipients',$data))
$j_recipients = json_decode($data['recipients']); $recipients = $data['recipients'];
if($msgtype === 'refresh') { if($msgtype === 'refresh') {
// remote channel info (such as permissions or photo or something) // remote channel info (such as permissions or photo or something)
// has been updated. Grab a fresh copy and sync it. // has been updated. Grab a fresh copy and sync it.
if($j_recipients) { if($recipients) {
// This would be a permissions update, typically for one connection // This would be a permissions update, typically for one connection
foreach($j_recipients as $recip) { foreach($recipients as $recip) {
$r = q("select channel.*,xchan.* from channel $r = q("select channel.*,xchan.* from channel
left join xchan on channel_hash = xchan_hash left join xchan on channel_hash = xchan_hash
where channel_guid = '%s' and channel_guid_sig = '%s' limit 1", where channel_guid = '%s' and channel_guid_sig = '%s' limit 1",
dbesc($recip->guid), dbesc($recip['guid']),
dbesc($recip->guid_sig) dbesc($recip['guid_sig'])
); );
$x = zot_refresh(array( $x = zot_refresh(array(
'xchan_guid' => $j_sender->guid, 'xchan_guid' => $sender['guid'],
'xchan_guid_sig' => $j_sender->guid_sig, 'xchan_guid_sig' => $sender['guid_sig'],
'hubloc_url' => $j_sender->url 'hubloc_url' => $sender['url']
),$r[0]); ),$r[0]);
} }
} }
@ -72,9 +74,9 @@ function post_post(&$a) {
// system wide refresh // system wide refresh
$x = zot_refresh(array( $x = zot_refresh(array(
'xchan_guid' => $j_sender->guid, 'xchan_guid' => $sender['guid'],
'xchan_guid_sig' => $j_sender->guid_sig, 'xchan_guid_sig' => $sender['guid_sig'],
'hubloc_url' => $j_sender->url 'hubloc_url' => $sender['url']
),null); ),null);
} }
$ret['result'] = true; $ret['result'] = true;
@ -82,13 +84,12 @@ function post_post(&$a) {
} }
if($msgtype === 'notify') { if($msgtype === 'notify') {
// add to receive queue // add to receive queue
// qreceive_add($data); // qreceive_add($data);
$ret['result'] = true; $ret['result'] = true;
json_return_and_die($ret); json_return_and_die($ret);
} }
} }

View File

@ -1 +1 @@
2012-11-18.143 2012-11-19.144