diff options
-rw-r--r-- | Zotlabs/Lib/Zotfinger.php | 26 | ||||
-rw-r--r-- | include/hubloc.php | 11 | ||||
-rw-r--r-- | include/text.php | 39 |
3 files changed, 74 insertions, 2 deletions
diff --git a/Zotlabs/Lib/Zotfinger.php b/Zotlabs/Lib/Zotfinger.php index 840d91403..58050609c 100644 --- a/Zotlabs/Lib/Zotfinger.php +++ b/Zotlabs/Lib/Zotfinger.php @@ -6,7 +6,7 @@ use Zotlabs\Web\HTTPSig; class Zotfinger { - static function exec($resource,$channel = null, $verify = true) { + static function exec($resource, $channel = null, $verify = true, $recurse = true) { if(! $resource) { return false; @@ -39,6 +39,30 @@ class Zotfinger { logger('fetch: ' . print_r($x,true)); + if (in_array(intval($x['return_code']), [ 404, 410 ]) && $recurse) { + + // 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) { + // 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) { $result['signature'] = HTTPSig::verify($x, EMPTY_STR, 'zot6'); diff --git a/include/hubloc.php b/include/hubloc.php index 2cce7a725..6401d1f0d 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -16,6 +16,8 @@ use Zotlabs\Daemon\Master; */ 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'] : ''), @@ -40,7 +42,7 @@ function hubloc_store_lowlevel($arr) { '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) { @@ -283,6 +285,13 @@ function hubloc_change_primary($hubloc) { return true; } +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 Mark a hubloc as down. diff --git a/include/text.php b/include/text.php index b76175a06..3b6039c31 100644 --- a/include/text.php +++ b/include/text.php @@ -3592,6 +3592,45 @@ 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]); } |