From cbd8c7787313f3c5dcfe6918bf740c944c2bf474 Mon Sep 17 00:00:00 2001 From: nobody Date: Sun, 23 Jan 2022 02:13:59 -0800 Subject: [PATCH] revert clearing primary on dead channels. Mark the hubloc deleted instead. Zotfinger will redirect to any living clones we know about. By default, Zotfinger requests will recurse, but this can be disabled if needed. --- Zotlabs/Lib/Zotfinger.php | 48 ++++++++++++++++----------------------- include/connections.php | 37 +++++++++++------------------- include/hubloc.php | 25 ++++++++++++-------- include/text.php | 38 +++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 62 deletions(-) diff --git a/Zotlabs/Lib/Zotfinger.php b/Zotlabs/Lib/Zotfinger.php index 0941372ff..4e37645f5 100644 --- a/Zotlabs/Lib/Zotfinger.php +++ b/Zotlabs/Lib/Zotfinger.php @@ -7,7 +7,7 @@ use Zotlabs\Web\HTTPSig; class Zotfinger { - public static function exec($resource, $channel = null, $verify = true) + public static function exec($resource, $channel = null, $verify = true, $recurse = true) { if (!$resource) { @@ -42,39 +42,29 @@ class Zotfinger $redirects = 0; $x = z_post_url($resource, $data, $redirects, ['headers' => $h]); - if (intval($x['return_code'] === 404)) { + if (in_array(intval($x['return_code']), [ 404, 410 ]) && $recurse) { - // if this resource returns "not found", mark any corresponding hubloc deleted and - // change the primary if needed. We need to catch it at this level because we - // can't really sync the locations if we've got no data to work with. - - $h = Activity::get_actor_hublocs($resource, 'zot6,not_deleted'); + // The resource has been deleted or doesn't exist at this location. + // Try to find another nomadic resource for this channel and return that. + + // First, see if there's a hubloc for this site. Fetch that record to + // obtain the nomadic identity hash. Then use that to find any additional + // nomadic locations. + + $h = Activity::get_actor_hublocs($resource, 'zot6'); if ($h) { - $primary = intval($h[0]['hubloc_primary']); - - q("update hubloc set hubloc_deleted = 1, hubloc_primary = 0 where hubloc_id = %d", - intval($h[0]['hubloc_id']) - ); - if ($primary) { - // find another hub that can act as primary since this one cannot. If this - // fails, it may be that there are no other instances of the channel known - // to this site. In that case, we'll just leave the entry without a primary - // until/id we hear from them again at a new location. - $a = q("select * from hubloc where hubloc_hash = '%s' and hubloc_deleted = 0", - dbesc($h[0]['hubloc_hash']) - ); - if ($a) { - $new_primary = array_shift($a); - q("update hubloc set hubloc_primary = 1 where hubloc_id = %d", - intval($new_primary['hubloc_id']) - ); - $new_primary['hubloc_primary'] = 1; - hubloc_change_primary($new_primary); + // mark this location deleted + hubloc_delete($h[0]); + $hubs = Activity::get_actor_hublocs($h[0]['hubloc_hash']); + if ($hubs) { + foreach ($hubs as $hub) { + if ($hub['hubloc_id_url'] !== $resource and !$hub['hubloc_deleted']) { + return $self::exec($hub['hubloc_id_url'],$channel,$verify); + } } - } + } } } - if ($x['success']) { if ($verify) { diff --git a/include/connections.php b/include/connections.php index 5817a79e9..89b221335 100644 --- a/include/connections.php +++ b/include/connections.php @@ -390,32 +390,21 @@ function remove_all_xchan_resources($xchan, $channel_id = 0) dbesc($xchan) ); - if ($dirmode === false || $dirmode == DIRECTORY_MODE_NORMAL) { - $r = q( - "delete from xchan where xchan_hash = '%s'", - dbesc($xchan) - ); - $r = q( - "delete from hubloc where hubloc_hash = '%s'", - dbesc($xchan) - ); - $r = q( - "delete from xprof where xprof_hash = '%s'", - dbesc($xchan) - ); - } else { - // directory servers need to keep the record around for sync purposes - mark it deleted + $r = q( + "update hubloc set hubloc_deleted = 1 where hubloc_hash = '%s'", + dbesc($xchan) + ); - $r = q( - "update hubloc set hubloc_deleted = 1 where hubloc_hash = '%s'", - dbesc($xchan) - ); + $r = q( + "update xchan set xchan_deleted = 1 where xchan_hash = '%s'", + dbesc($xchan) + ); + + $r = q( + "delete from xprof where xprof_hash = '%s'", + dbesc($xchan) + ); - $r = q( - "update xchan set xchan_deleted = 1 where xchan_hash = '%s'", - dbesc($xchan) - ); - } } } diff --git a/include/hubloc.php b/include/hubloc.php index 53ad35ccb..2192076cc 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -17,7 +17,8 @@ use Zotlabs\Daemon\Run; */ function hubloc_store_lowlevel($arr) { - + $update = array_key_exists('hubloc_id',$arr) && $arr['hubloc_id'] ? 'hubloc_id = ' . intval($arr['hubloc_id']) : false; + $store = [ 'hubloc_guid' => ((array_key_exists('hubloc_guid', $arr)) ? $arr['hubloc_guid'] : ''), 'hubloc_guid_sig' => ((array_key_exists('hubloc_guid_sig', $arr)) ? $arr['hubloc_guid_sig'] : ''), @@ -41,8 +42,8 @@ function hubloc_store_lowlevel($arr) 'hubloc_error' => ((array_key_exists('hubloc_error', $arr)) ? $arr['hubloc_error'] : 0), 'hubloc_deleted' => ((array_key_exists('hubloc_deleted', $arr)) ? $arr['hubloc_deleted'] : 0) ]; - - return create_table_from_array('hubloc', $store); + + return $update ? update_table_from_array('hubloc', $store, $update) : create_table_from_array('hubloc', $store); } function site_store_lowlevel($arr) @@ -292,7 +293,14 @@ function hubloc_mark_as_down($posturl) ); } - +function hubloc_delete($hubloc) { + if (is_array($hubloc) && array_key_exists('hubloc_id',$hubloc)) { + q("update hubloc set hubloc_deleted = 1 where hubloc_id = %d", + intval($hubloc['hubloc_id']) + ); + } +} + /** * @brief return comma separated string of non-dead clone locations (net addresses) for a given netid * @@ -316,18 +324,17 @@ function locations_by_netid($netid) function ping_site($url) { + $ret = [ 'success' => false ]; - $ret = array('success' => false); - - $r = Zotlabs\Lib\Zotfinger::exec($url); + $r = Zotlabs\Lib\Zotfinger::exec($url); if (! $r['data']) { $ret['message'] = 'no answer from ' . $url; return $ret; } - $ret['success'] = true; - return $ret; + $ret['success'] = true; + return $ret; } diff --git a/include/text.php b/include/text.php index 58533c7ca..5c41d3b53 100644 --- a/include/text.php +++ b/include/text.php @@ -3742,6 +3742,44 @@ function create_table_from_array($table, $arr, $binary_fields = []) return $r; } + + +function update_table_from_array($table, $arr, $where, $binary_fields = []) +{ + + if (! ($arr && $table)) { + return false; + } + + $columns = db_columns($table); + + $clean = []; + foreach ($arr as $k => $v) { + if (! in_array($k, $columns)) { + continue; + } + + $matches = false; + if (preg_match('/([^a-zA-Z0-9\-\_\.])/', $k, $matches)) { + return false; + } + if (in_array($k, $binary_fields)) { + $clean[$k] = dbescbin($v); + } else { + $clean[$k] = dbesc($v); + } + } + $sql = "UPDATE " . TQUOT . $table . TQUOT . " SET "; + foreach ($clean as $k => $v) { + $sql .= TQUOT . $k . TQUOT . ' = "' . $v . '",'; + } + $sql = rtrim($sql,','); + $r = dbq($sql . " WHERE " . $where); + + return $r; +} + + function share_shield($m) { return str_replace($m[1], '!=+=+=!' . base64url_encode($m[1]) . '=+!=+!=', $m[0]);