From bdb25315b6f01f1963b21f6e0c0bd5ec2959c62e Mon Sep 17 00:00:00 2001 From: Mario Vavti Date: Wed, 26 Apr 2023 23:01:56 +0200 Subject: initial commit for directory flags federation --- Zotlabs/Daemon/Onedirsync.php | 9 +++--- Zotlabs/Daemon/Poller.php | 3 +- Zotlabs/Lib/Libzot.php | 2 +- Zotlabs/Lib/Libzotdir.php | 66 ++++++++++++++++++++++++++++++------------- Zotlabs/Module/Dircensor.php | 19 ++++++++----- Zotlabs/Module/Dirsearch.php | 4 +-- boot.php | 17 ++++++----- 7 files changed, 78 insertions(+), 42 deletions(-) diff --git a/Zotlabs/Daemon/Onedirsync.php b/Zotlabs/Daemon/Onedirsync.php index 0d5ad22ea..b711b0c05 100644 --- a/Zotlabs/Daemon/Onedirsync.php +++ b/Zotlabs/Daemon/Onedirsync.php @@ -37,13 +37,14 @@ class Onedirsync { $h = Libzot::zot_record_preferred($h); if (($h) && (($h['hubloc_status'] & HUBLOC_OFFLINE) || $h['hubloc_deleted'] || $h['hubloc_error'])) { - q("update updates set ud_flags = 9 where ud_hash = '%s' and ud_flags != 9", - dbesc($r[0]['ud_hash']) - ); - // 2023-04-12: Flag the entry deleted but try to update anyway since the info is not always correct + // 2023-04-12: Try to update anyway since the info is not always correct // This might change after all directory servers run the new code. + // q("update updates set ud_flags = 9 where ud_hash = '%s' and ud_flags != 9", + // dbesc($r[0]['ud_hash']) + //); + // return; } diff --git a/Zotlabs/Daemon/Poller.php b/Zotlabs/Daemon/Poller.php index 77a428ec3..e5a1c6f39 100644 --- a/Zotlabs/Daemon/Poller.php +++ b/Zotlabs/Daemon/Poller.php @@ -170,7 +170,8 @@ class Poller { $dirmode = intval(get_config('system', 'directory_mode')); if ($dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY) { - $r = q("SELECT * FROM updates WHERE ud_update = 1 AND (ud_last = '%s' OR ud_last > %s - INTERVAL %s)", + $r = q("SELECT * FROM updates WHERE ud_update != %d AND (ud_last = '%s' OR ud_last > %s - INTERVAL %s)", + intval(DIRECTORY_UPDATE_OK), dbesc(NULL_DATE), db_utcnow(), db_quoteinterval('7 DAY') diff --git a/Zotlabs/Lib/Libzot.php b/Zotlabs/Lib/Libzot.php index 99b9e0f75..72cf892d0 100644 --- a/Zotlabs/Lib/Libzot.php +++ b/Zotlabs/Lib/Libzot.php @@ -965,7 +965,7 @@ class Libzot { } // update updates if anything changed bump the ud_date - Libzotdir::update($xchan_hash, $address, $changed); + Libzotdir::update($xchan_hash, $address, $changed, $r[0]['xchan_censored']); if (empty($ret['message'])) { $ret['success'] = true; diff --git a/Zotlabs/Lib/Libzotdir.php b/Zotlabs/Lib/Libzotdir.php index 548164baa..acac40d3b 100644 --- a/Zotlabs/Lib/Libzotdir.php +++ b/Zotlabs/Lib/Libzotdir.php @@ -233,6 +233,8 @@ class Libzotdir { if (! $r) return; + $dir_trusted_hosts = get_directory_fallback_servers(); + foreach ($r as $rr) { if (! $rr['site_directory']) continue; @@ -264,8 +266,7 @@ class Libzotdir { if (is_array($j['transactions']) && count($j['transactions'])) { foreach ($j['transactions'] as $t) { - - if (empty($t['hash'])) { + if (empty($t['hash']) || empty($t['host']) || empty($t['address'])) { continue; } @@ -274,23 +275,40 @@ class Libzotdir { ); if ($r) { + $update = 0; + // no need to look at updates that originated from our own site + if ($t['host'] === z_root()) { + continue; + } + + // there is more recent xchan information if ($r[0]['ud_date'] >= $t['timestamp']) { + $update = DIRECTORY_UPDATE_XCHAN; + } + + // the host is trusted and flags have changed + if (in_array($t['host'], $dir_trusted_hosts) && intval($r[0]['ud_flags']) !== intval($t['flags'])) { + $update = (($update) ? DIRECTORY_UPDATE_BOTH : DIRECTORY_UPDATE_FLAGS); + } + + if (!$update) { continue; } - q("UPDATE updates SET ud_update = 1 WHERE ud_id = %d", + q("UPDATE updates SET ud_update = %d WHERE ud_id = %d", + intval($update), dbesc($r[0]['ud_id']) ); } else { - $t['transaction_id'] = strpos($t['transaction_id'], '@') === false ? $t['transaction_id'] : substr($t['transaction_id'], strpos($t['transaction_id'], '@') + 1); - q("insert into updates ( ud_hash, ud_guid, ud_date, ud_addr, ud_update ) - values ( '%s', '%s', '%s', '%s', 1 ) ", + q("insert into updates ( ud_hash, ud_host, ud_date, ud_addr, ud_update, ud_flags ) + values ( '%s', '%s', '%s', '%s', 1, %d) ", dbesc($t['hash']), - dbesc($t['host'] ?? $t['transaction_id']), // 2023-04-12 transaction_id is deprecated + dbesc($t['host']), dbesc($t['timestamp']), - dbesc($t['address']) + dbesc($t['address']), + dbesc(in_array($t['host'], $dir_trusted_hosts) ? $t['flags'] : 0) ); } } @@ -318,15 +336,19 @@ class Libzotdir { logger('update_directory_entry: ' . print_r($ud,true), LOGGER_DATA); - if (empty($ud['ud_hash']) || empty($ud['ud_addr'])) { - q("DELETE FROM updates WHERE ud_id = %d", - dbesc($ud['ud_id']) + // set the flag if requested? + if (in_array($ud['ud_flags'], [DIRECTORY_UPDATE_FLAGS, DIRECTORY_UPDATE_BOTH])) { + q("UPDATE xchan SET xchan_censored = %d WHERE xchan_hash = '%s'", + intval($ud['ud_flags']), + dbesc($ud['ud_hash']) ); - return false; } - $href = ((strpos($ud['ud_addr'], '://') === false) ? Webfinger::zot_url(punify($ud['ud_addr'])) : punify($ud['ud_addr'])); + if (intval($ud['ud_flags']) === DIRECTORY_UPDATE_FLAGS) { + return true; + } + $href = ((strpos($ud['ud_addr'], '://') === false) ? Webfinger::zot_url(punify($ud['ud_addr'])) : punify($ud['ud_addr'])); if($href) { $zf = Zotfinger::exec($href); if($zf && array_path_exists('signature/signer',$zf) && $zf['signature']['signer'] === $href && intval($zf['signature']['header_valid'])) { @@ -643,9 +665,11 @@ class Libzotdir { * @param bool $bump_date (optional) default true */ - static function update($hash, $addr, $bump_date = true) { + static function update($hash, $addr, $bump_date = true, $flag = DIRECTORY_FLAG_OK) { +hz_syslog('flag: '. print_r($flag, true)); $dirmode = intval(get_config('system', 'directory_mode')); +hz_syslog('gothere'); if($dirmode == DIRECTORY_MODE_NORMAL) { return; @@ -655,7 +679,7 @@ class Libzotdir { return; } - $u = q("SELECT ud_id FROM updates WHERE ud_hash = '%s' LIMIT 1", + $u = q("SELECT * FROM updates WHERE ud_hash = '%s' LIMIT 1", dbesc($hash) ); @@ -665,21 +689,23 @@ class Libzotdir { } if ($u) { - $x = q("UPDATE updates SET $date_sql ud_last = '%s', ud_guid = '%s', ud_addr = '%s', ud_flags = 0 WHERE ud_id = %d", + $x = q("UPDATE updates SET $date_sql ud_last = '%s', ud_host = '%s', ud_addr = '%s', ud_update = 0, ud_flags = %d WHERE ud_id = %d", dbesc(NULL_DATE), - dbesc(\App::get_hostname()), + dbesc(z_root()), dbesc($addr), + intval($flag), intval($u[0]['ud_id']) ); return; } - q("INSERT INTO updates (ud_hash, ud_guid, ud_date, ud_addr ) VALUES ( '%s', '%s', '%s', '%s' )", + q("INSERT INTO updates (ud_hash, ud_host, ud_date, ud_addr, ud_flags) VALUES ( '%s', '%s', '%s', '%s', %d )", dbesc($hash), - dbesc(\App::get_hostname()), + dbesc(z_root()), dbesc(datetime_convert()), - dbesc($addr) + dbesc($addr), + intval($flag) ); return; diff --git a/Zotlabs/Module/Dircensor.php b/Zotlabs/Module/Dircensor.php index 309b14346..90759e58d 100644 --- a/Zotlabs/Module/Dircensor.php +++ b/Zotlabs/Module/Dircensor.php @@ -4,6 +4,7 @@ namespace Zotlabs\Module; use App; use Zotlabs\Web\Controller; +use Zotlabs\Lib\Libzotdir; class Dircensor extends Controller { @@ -20,6 +21,7 @@ class Dircensor extends Controller { } $xchan = argv(1); + if(! $xchan) { return; } @@ -33,25 +35,28 @@ class Dircensor extends Controller { } $severity = intval($_REQUEST['severity'] ?? 0); + $flag = DIRECTORY_FLAG_OK; - if ($severity < 0) { - $severity = 0; + if ($severity === 1) { + $flag = DIRECTORY_FLAG_UNSAFE; } - if ($severity > 2) { - $severity = 2; + if ($severity === 2) { + $flag = DIRECTORY_FLAG_HIDDEN; } +hz_syslog('flag: '. print_r($flag, true)); + Libzotdir::update($xchan, $r[0]['xchan_url'], true, $flag); q("update xchan set xchan_censored = %d where xchan_hash = '%s'", - intval($severity), + intval($flag), dbesc($xchan) ); - if($severity) { + if($flag) { info( t('Entry censored') . EOL); } else { - info( t('Entry uncensored') . EOL); + info( t('Entry OK') . EOL); } goaway(z_root() . '/directory'); diff --git a/Zotlabs/Module/Dirsearch.php b/Zotlabs/Module/Dirsearch.php index 22b030edf..d4af0f91a 100644 --- a/Zotlabs/Module/Dirsearch.php +++ b/Zotlabs/Module/Dirsearch.php @@ -228,8 +228,8 @@ class Dirsearch extends Controller { $spkt['transactions'][] = [ 'hash' => $rr['ud_hash'], 'address' => $rr['ud_addr'], - 'host' => $rr['ud_guid'], - 'transaction_id' => $rr['ud_guid'], // deprecated 2023-04-12 + 'host' => $rr['ud_host'], + 'transaction_id' => $rr['ud_host'], // deprecated 2023-04-12 'timestamp' => $rr['ud_date'], 'flags' => $rr['ud_flags'] ]; diff --git a/boot.php b/boot.php index 1f9d131d3..8666c5f4f 100644 --- a/boot.php +++ b/boot.php @@ -84,6 +84,15 @@ define('DIRECTORY_MODE_PRIMARY', 0x0001); // There can only be *one* primary dir define('DIRECTORY_MODE_SECONDARY', 0x0002); // All other mirror directory servers define('DIRECTORY_MODE_STANDALONE', 0x0100); // A detached (off the grid) hub with itself as directory server. +define('DIRECTORY_UPDATE_OK', 0); +define('DIRECTORY_UPDATE_XCHAN', 1); +define('DIRECTORY_UPDATE_FLAGS', 2); +define('DIRECTORY_UPDATE_BOTH', 3); + +define('DIRECTORY_FLAG_OK', 0); +define('DIRECTORY_FLAG_UNSAFE', 1); +define('DIRECTORY_FLAG_HIDDEN', 2); + // We will look for upstream directories whenever me make contact // with other sites, but if this is a new installation and isn't // a standalone hub, we need to seed the service with a starting @@ -96,8 +105,7 @@ define('DIRECTORY_FALLBACK_MASTER', 'https://hub.netzgemeinde.eu'); function get_directory_fallback_servers() { $ret = [ 'https://hub.netzgemeinde.eu', - 'https://zotsite.net', - 'https://hub.libranet.de' + 'https://zotsite.net' ]; return $ret; @@ -364,11 +372,6 @@ define('POLL_TENSCALE', 0x0002); // 1-10 define('POLL_MULTIPLE_CHOICE', 0x0004); define('POLL_OVERWRITE', 0x8000); // If you vote twice remove the prior entry - -define('UPDATE_FLAGS_UPDATED', 0x0001); -define('UPDATE_FLAGS_FORCED', 0x0002); -define('UPDATE_FLAGS_DELETED', 0x1000); - define('HUBLOC_OFFLINE', 0x0001); define('DROPITEM_NORMAL', 0); -- cgit v1.2.3