From 5aa0017e91cac135ef8b84c138becb09decb1d58 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Sat, 28 Jan 2017 15:01:19 -0800 Subject: provide lowlevel xchan storage function to ensure that all non-null rows are initialised --- boot.php | 1 + include/channel.php | 36 ++++++------- include/hubloc.php | 104 -------------------------------------- include/items.php | 34 +++++++------ include/network.php | 35 +++++++++---- include/security.php | 23 ++++----- include/xchan.php | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/zot.php | 44 ++++++++-------- 8 files changed, 236 insertions(+), 180 deletions(-) create mode 100644 include/xchan.php diff --git a/boot.php b/boot.php index 21862a227..adc5dc13d 100755 --- a/boot.php +++ b/boot.php @@ -45,6 +45,7 @@ require_once('include/channel.php'); require_once('include/connections.php'); require_once('include/account.php'); require_once('include/zid.php'); +require_once('include/xchan.php'); define ( 'PLATFORM_NAME', 'hubzilla' ); diff --git a/include/channel.php b/include/channel.php index d66ed7abe..d2ef89cc8 100644 --- a/include/channel.php +++ b/include/channel.php @@ -311,23 +311,25 @@ function create_identity($arr) { $newuid = $ret['channel']['channel_id']; - $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_guid_sig, xchan_pubkey, xchan_photo_l, xchan_photo_m, xchan_photo_s, xchan_addr, xchan_url, xchan_follow, xchan_connurl, xchan_name, xchan_network, xchan_photo_date, xchan_name_date, xchan_system ) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)", - dbesc($hash), - dbesc($guid), - dbesc($sig), - dbesc($key['pubkey']), - dbesc(z_root() . "/photo/profile/l/{$newuid}"), - dbesc(z_root() . "/photo/profile/m/{$newuid}"), - dbesc(z_root() . "/photo/profile/s/{$newuid}"), - dbesc(channel_reddress($ret['channel'])), - dbesc(z_root() . '/channel/' . $ret['channel']['channel_address']), - dbesc(z_root() . '/follow?f=&url=%s'), - dbesc(z_root() . '/poco/' . $ret['channel']['channel_address']), - dbesc($ret['channel']['channel_name']), - dbesc('zot'), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - intval($system) + $r = xchan_store_lowlevel( + [ + 'xchan_hash' => $hash, + 'xchan_guid' => $guid, + 'xchan_guid_sig' => $sig, + 'xchan_pubkey' => $key['pubkey'], + 'xchan_photo_l' => z_root() . "/photo/profile/l/{$newuid}", + 'xchan_photo_m' => z_root() . "/photo/profile/m/{$newuid}", + 'xchan_photo_s' => z_root() . "/photo/profile/s/{$newuid}", + 'xchan_addr' => channel_reddress($ret['channel']), + 'xchan_url' => z_root() . '/channel/' . $ret['channel']['channel_address'], + 'xchan_follow' => z_root() . '/follow?f=&url=%s', + 'xchan_connurl' => z_root() . '/poco/' . $ret['channel']['channel_address'], + 'xchan_name' => $ret['channel']['channel_name'], + 'xchan_network' => 'zot', + 'xchan_photo_date' => datetime_convert(), + 'xchan_name_date' => datetime_convert(), + 'xchan_system' => $system + ] ); // Not checking return value. diff --git a/include/hubloc.php b/include/hubloc.php index 6f81ea31f..4da92ae72 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -176,110 +176,6 @@ function hubloc_mark_as_down($posturl) { } -function xchan_store($arr) { - - logger('xchan_store: ' . print_r($arr,true)); - - if(! $arr['hash']) - $arr['hash'] = $arr['guid']; - if(! $arr['hash']) - return false; - - $r = q("select * from xchan where xchan_hash = '%s' limit 1", - dbesc($arr['hash']) - ); - if($r) - return true; - - if(! $arr['network']) - $arr['network'] = 'unknown'; - if(! $arr['name']) - $arr['name'] = 'unknown'; - if(! $arr['url']) - $arr['url'] = z_root(); - if(! $arr['photo']) - $arr['photo'] = z_root() . '/' . get_default_profile_photo(); - - - if($arr['network'] === 'zot') { - if((! $arr['key']) || (! rsa_verify($arr['guid'],base64url_decode($arr['guid_sig']),$arr['key']))) { - logger('Unable to verify signature for ' . $arr['hash']); - return false; - } - } - - $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_guid_sig, xchan_pubkey, xchan_addr, xchan_url, xchan_connurl, xchan_follow, xchan_connpage, xchan_name, xchan_network, xchan_instance_url, xchan_hidden, xchan_orphan, xchan_censored, xchan_selfcensored, xchan_system, xchan_pubforum, xchan_deleted, xchan_name_date ) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s',%d, %d, %d, %d, %d, %d, %d, '%s') ", - dbesc($arr['hash']), - dbesc($arr['guid']), - dbesc($arr['guid_sig']), - dbesc($arr['pubkey']), - dbesc($arr['address']), - dbesc($arr['url']), - dbesc($arr['connurl']), - dbesc($arr['follow']), - dbesc($arr['connpage']), - dbesc($arr['name']), - dbesc($arr['network']), - dbesc($arr['instance_url']), - intval($arr['hidden']), - intval($arr['orphan']), - intval($arr['censored']), - intval($arr['selfcensored']), - intval($arr['system']), - intval($arr['pubforum']), - intval($arr['deleted']), - dbesc(datetime_convert()) - ); - if(! $r) - return $r; - - $photos = import_xchan_photo($arr['photo'],$arr['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' where xchan_hash = '%s'", - dbesc(datetime_convert()), - dbesc($photos[0]), - dbesc($photos[1]), - dbesc($photos[2]), - dbesc($photos[3]), - dbesc($arr['hash']) - ); - return $r; - -} - - -function xchan_fetch($arr) { - - $key = ''; - if($arr['hash']) { - $key = 'xchan_hash'; - $v = $arr['hash']; - } - elseif($arr['guid']) { - $key = 'xchan_guid'; - $v = $arr['guid']; - } - elseif($arr['address']) { - $key = 'xchan_addr'; - $v = $arr['address']; - } - - if(! $key) - return false; - - $r = q("select * from xchan where $key = '$v' limit 1"); - if(! $r) - return false; - - $ret = array(); - foreach($r[0] as $k => $v) { - if($k === 'xchan_addr') - $ret['address'] = $v; - else - $ret[str_replace('xchan_','',$k)] = $v; - } - return $ret; -} - function ping_site($url) { diff --git a/include/items.php b/include/items.php index 0d9fb02a4..559992b7f 100755 --- a/include/items.php +++ b/include/items.php @@ -833,13 +833,15 @@ function import_author_rss($x) { } $name = trim($x['name']); - $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_url, xchan_name, xchan_network ) - values ( '%s', '%s', '%s', '%s', '%s' )", - dbesc($x['guid']), - dbesc($x['guid']), - dbesc($x['url']), - dbesc(($name) ? $name : t('(Unknown)')), - dbesc('rss') + $r = xchan_store_lowlevel( + [ + 'xchan_hash' => $x['guid'], + 'xchan_guid' => $x['guid'], + 'xchan_url' => $x['url'], + 'xchan_name' => (($name) ? $name : t('(Unknown)')), + 'xchan_name_date' => datetime_convert(), + 'xchan_network' => 'rss' + ] ); if($r && $x['photo']) { @@ -878,14 +880,17 @@ function import_author_unknown($x) { $name = trim($x['name']); - $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_url, xchan_name, xchan_network ) - values ( '%s', '%s', '%s', '%s', '%s' )", - dbesc($x['url']), - dbesc($x['url']), - dbesc($x['url']), - dbesc(($name) ? $name : t('(Unknown)')), - dbesc('unknown') + $r = xchan_store_lowlevel( + [ + 'xchan_hash' => $x['url'], + 'xchan_guid' => $x['url'], + 'xchan_url' => $x['url'], + 'xchan_name' => (($name) ? $name : t('(Unknown)')), + 'xchan_name_date' => datetime_convert(), + 'xchan_network' => 'unknown' + ] ); + if($r && $x['photo']) { $photos = import_xchan_photo($x['photo']['src'],$x['url']); @@ -1516,6 +1521,7 @@ function item_store($arr, $allow_exec = false, $deliver = true) { $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : ''); $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : ''); $arr['postopts'] = ((x($arr,'postopts')) ? trim($arr['postopts']) : ''); + $arr['route'] = ((x($arr,'route')) ? trim($arr['route']) : ''); $arr['item_private'] = ((x($arr,'item_private')) ? intval($arr['item_private']) : 0 ); $arr['item_wall'] = ((x($arr,'item_wall')) ? intval($arr['item_wall']) : 0 ); $arr['item_type'] = ((x($arr,'item_type')) ? intval($arr['item_type']) : 0 ); diff --git a/include/network.php b/include/network.php index 4391f1bfe..73fd417ea 100644 --- a/include/network.php +++ b/include/network.php @@ -1121,16 +1121,17 @@ function discover_by_url($url,$arr = null) { if(! $photo) $photo = z_root() . '/images/rss_icon.png'; - $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_pubkey, xchan_addr, xchan_url, xchan_name, xchan_network, xchan_instance_url, xchan_name_date ) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ", - dbesc($guid), - dbesc($guid), - dbesc($pubkey), - dbesc($addr), - dbesc($profile), - dbesc($name), - dbesc($network), - dbesc(z_root()), - dbesc(datetime_convert()) + $r = xchan_store_lowlevel( + [ + 'xchan_hash' => $guid, + 'xchan_guid' => $guid, + 'xchan_pubkey' => $pubkey, + 'xchan_addr' => $addr, + 'xchan_url' => $profile, + 'xchan_name' => $name, + 'xchan_name_date' => datetime_convert(), + 'xchan_network' => $network + ] ); $photos = import_xchan_photo($photo,$guid); @@ -1449,6 +1450,20 @@ function discover_by_webbie($webbie) { ); } else { + + $r = xchan_store_lowlevel( + [ + 'xchan_hash' => $address, + 'xchan_guid' => (($diaspora_guid) ? $diaspora_guid : $location), + 'xchan_pubkey' => $pubkey, + 'xchan_addr' => $address, + 'xchan_url' => $location, + 'xchan_name' => $fullname, + 'xchan_name_date' => datetime_convert(), + 'xchan_network' => $network + ] + ); + $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_pubkey, xchan_addr, xchan_url, xchan_name, xchan_network, xchan_name_date ) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ", dbesc($address), dbesc(($diaspora_guid) ? $diaspora_guid : $location), diff --git a/include/security.php b/include/security.php index 2f5de54c7..57b33251f 100644 --- a/include/security.php +++ b/include/security.php @@ -171,19 +171,16 @@ function atoken_create_xchan($xchan) { if($r) return; - $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_addr, xchan_url, xchan_name, xchan_network, xchan_photo_mimetype, xchan_photo_l, xchan_photo_m, xchan_photo_s ) - values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ", - dbesc($xchan['xchan_hash']), - dbesc($xchan['xchan_hash']), - dbesc($xchan['xchan_addr']), - dbesc($xchan['xchan_url']), - dbesc($xchan['xchan_name']), - dbesc($xchan['xchan_network']), - dbesc($xchan['xchan_photo_mimetype']), - dbesc($xchan['xchan_photo_l']), - dbesc($xchan['xchan_photo_m']), - dbesc($xchan['xchan_photo_s']) - ); + $xchan['xchan_guid'] = $xchan['xchan_hash']; + + $store = []; + foreach($xchan as $k => $v) { + if(strpos($k,'xchan_') === 0) { + $store[$k] = $v; + } + } + + $r = xchan_store_lowlevel($store); return true; } diff --git a/include/xchan.php b/include/xchan.php new file mode 100644 index 000000000..12eb674fa --- /dev/null +++ b/include/xchan.php @@ -0,0 +1,139 @@ + ((array_key_exists('xchan_hash',$arr)) ? $arr['xchan_hash'] : ''), + 'xchan_guid' => ((array_key_exists('xchan_guid',$arr)) ? $arr['xchan_guid'] : ''), + 'xchan_guid_sig' => ((array_key_exists('xchan_guid_sig',$arr)) ? $arr['xchan_guid_sig'] : ''), + 'xchan_pubkey' => ((array_key_exists('xchan_pubkey',$arr)) ? $arr['xchan_pubkey'] : ''), + 'xchan_photo_mimetype' => ((array_key_exists('xchan_photo_mimetype',$arr)) ? $arr['xchan_photo_mimetype'] : ''), + 'xchan_photo_l' => ((array_key_exists('xchan_photo_l',$arr)) ? $arr['xchan_photo_l'] : ''), + 'xchan_photo_m' => ((array_key_exists('xchan_photo_m',$arr)) ? $arr['xchan_photo_m'] : ''), + 'xchan_photo_s' => ((array_key_exists('xchan_photo_s',$arr)) ? $arr['xchan_photo_s'] : ''), + 'xchan_addr' => ((array_key_exists('xchan_addr',$arr)) ? $arr['xchan_addr'] : ''), + 'xchan_url' => ((array_key_exists('xchan_url',$arr)) ? $arr['xchan_url'] : ''), + 'xchan_connurl' => ((array_key_exists('xchan_connurl',$arr)) ? $arr['xchan_connurl'] : ''), + 'xchan_follow' => ((array_key_exists('xchan_follow',$arr)) ? $arr['xchan_follow'] : ''), + 'xchan_connpage' => ((array_key_exists('xchan_connpage',$arr)) ? $arr['xchan_connpage'] : ''), + 'xchan_name' => ((array_key_exists('xchan_name',$arr)) ? $arr['xchan_name'] : ''), + 'xchan_network' => ((array_key_exists('xchan_network',$arr)) ? $arr['xchan_network'] : ''), + 'xchan_instance_url' => ((array_key_exists('xchan_instance_url',$arr)) ? $arr['xchan_instance_url'] : ''), + 'xchan_flags' => ((array_key_exists('xchan_flags',$arr)) ? intval($arr['xchan_flags']) : 0), + 'xchan_photo_date' => ((array_key_exists('xchan_photo_date',$arr)) ? datetime_convert('UTC','UTC',$arr['xchan_photo_date']) : NULL_DATE), + 'xchan_name_date' => ((array_key_exists('xchan_name_date',$arr)) ? datetime_convert('UTC','UTC',$arr['xchan_name_date']) : NULL_DATE), + 'xchan_hidden' => ((array_key_exists('xchan_hidden',$arr)) ? intval($arr['xchan_hidden']) : 0), + 'xchan_orphan' => ((array_key_exists('xchan_orphan',$arr)) ? intval($arr['xchan_orphan']) : 0), + 'xchan_censored' => ((array_key_exists('xchan_censored',$arr)) ? intval($arr['xchan_censored']) : 0), + 'xchan_selfcensored' => ((array_key_exists('xchan_selfcensored',$arr)) ? intval($arr['xchan_selfcensored']) : 0), + 'xchan_system' => ((array_key_exists('xchan_system',$arr)) ? intval($arr['xchan_system']) : 0), + 'xchan_pubforum' => ((array_key_exists('xchan_pubforum',$arr)) ? intval($arr['xchan_pubforum']) : 0), + 'xchan_deleted' => ((array_key_exists('xchan_deleted',$arr)) ? intval($arr['xchan_deleted']) : 0) + ]; + + return create_table_from_array('xchan',$store); +} + + + +function xchan_store($arr) { + + logger('xchan_store: ' . print_r($arr,true)); + + if(! $arr['hash']) + $arr['hash'] = $arr['guid']; + if(! $arr['hash']) + return false; + + $r = q("select * from xchan where xchan_hash = '%s' limit 1", + dbesc($arr['hash']) + ); + if($r) + return true; + + if(! $arr['network']) + $arr['network'] = 'unknown'; + if(! $arr['name']) + $arr['name'] = 'unknown'; + if(! $arr['url']) + $arr['url'] = z_root(); + if(! $arr['photo']) + $arr['photo'] = z_root() . '/' . get_default_profile_photo(); + + + if($arr['network'] === 'zot') { + if((! $arr['key']) || (! rsa_verify($arr['guid'],base64url_decode($arr['guid_sig']),$arr['key']))) { + logger('Unable to verify signature for ' . $arr['hash']); + return false; + } + } + + $x = []; + foreach($arr as $k => $v) { + if($k === 'key') { + $x['xchan_pubkey'] = $v; + continue; + } + if($k === 'photo') { + continue; + } + + $x['xchan_' . $k] = $v; + } + + $x['xchan_name_date'] = datetime_convert(); + + $r = xchan_store_lowlevel($x); + + if(! $r) + return $r; + + $photos = import_xchan_photo($arr['photo'],$arr['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' where xchan_hash = '%s'", + dbesc(datetime_convert()), + dbesc($photos[0]), + dbesc($photos[1]), + dbesc($photos[2]), + dbesc($photos[3]), + dbesc($arr['hash']) + ); + return $r; + +} + + +function xchan_fetch($arr) { + + $key = ''; + if($arr['hash']) { + $key = 'xchan_hash'; + $v = $arr['hash']; + } + elseif($arr['guid']) { + $key = 'xchan_guid'; + $v = $arr['guid']; + } + elseif($arr['address']) { + $key = 'xchan_addr'; + $v = $arr['address']; + } + + if(! $key) + return false; + + $r = q("select * from xchan where $key = '$v' limit 1"); + if(! $r) + return false; + + $ret = array(); + foreach($r[0] as $k => $v) { + if($k === 'xchan_addr') + $ret['address'] = $v; + else + $ret[str_replace('xchan_','',$k)] = $v; + } + return $ret; +} + + diff --git a/include/zot.php b/include/zot.php index 24b50cc12..1538e956e 100644 --- a/include/zot.php +++ b/include/zot.php @@ -759,28 +759,28 @@ function import_xchan($arr,$ud_flags = UPDATE_FLAGS_UPDATED, $ud_arr = null) { && ($arr['site']['url'] != z_root())) $arr['searchable'] = false; - $x = q("insert into xchan ( xchan_hash, xchan_guid, xchan_guid_sig, xchan_pubkey, xchan_photo_mimetype, - xchan_photo_l, xchan_addr, xchan_url, xchan_connurl, xchan_follow, xchan_connpage, xchan_name, xchan_network, xchan_photo_date, xchan_name_date, xchan_hidden, xchan_selfcensored, xchan_deleted, xchan_pubforum ) - values ( '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d) ", - dbesc($xchan_hash), - dbesc($arr['guid']), - dbesc($arr['guid_sig']), - dbesc($arr['key']), - dbesc($arr['photo_mimetype']), - dbesc($arr['photo']), - dbesc($arr['address']), - dbesc($arr['url']), - dbesc($arr['connections_url']), - dbesc($arr['follow_url']), - dbesc($arr['connect_url']), - dbesc(($arr['name']) ? $arr['name'] : '-'), - dbesc('zot'), - dbescdate($arr['photo_updated']), - dbescdate($arr['name_updated']), - intval(1 - intval($arr['searchable'])), - intval($arr['adult_content']), - intval($arr['deleted']), - intval($arr['public_forum']) + $x = xchan_store_lowlevel( + [ + 'xchan_hash' => $xchan_hash, + 'xchan_guid' => $arr['guid'], + 'xchan_guid_sig' => $arr['guid_sig'], + 'xchan_pubkey' => $arr['key'], + 'xchan_photo_mimetype' => $arr['photo_mimetype'], + 'xchan_photo_l' => $arr['photo'], + 'xchan_addr' => $arr['address'], + 'xchan_url' => $arr['url'], + 'xchan_connurl' => $arr['connections_url'], + 'xchan_follow' => $arr['follow_url'], + 'xchan_connpage' => $arr['connect_url'], + 'xchan_name' => (($arr['name']) ? $arr['name'] : '-'), + 'xchan_network' => 'zot', + 'xchan_photo_date' => $arr['photo_updated'], + 'xchan_name_date' => $arr['name_updated'], + 'xchan_hidden' => intval(1 - intval($arr['searchable'])), + 'xchan_selfcensored' => $arr['adult_content'], + 'xchan_deleted' => $arr['deleted'], + 'xchan_pubforum' => $arr['public_forum'] + ] ); $what .= 'new_xchan'; -- cgit v1.2.3