X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FGServer.php;h=1878befa9f2282440c44a3b0ad19351555cb047c;hb=3f309805919671ce0ae22f82f5a751050a07dce4;hp=98acc9b67d22bf96090c75d85a90606c41c4a900;hpb=259b99e6e94cd6714e65bb128af9cbe776fd3325;p=friendica.git diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 98acc9b67d..1878befa9f 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -1,6 +1,6 @@ toArray($stmt); } + /** + * Checks if the given server array is unreachable for a long time now + * + * @param integer $gsid + * @return boolean + */ + private static function isDefunct(array $gserver): bool + { + return ($gserver['failed'] || in_array($gserver['network'], Protocol::FEDERATED)) && + ($gserver['last_contact'] >= $gserver['created']) && + ($gserver['last_contact'] < $gserver['last_failure']) && + ($gserver['last_contact'] < DateTimeFormat::utc('now - 90 days')); + } + + /** + * Checks if the given server id is unreachable for a long time now + * + * @param integer $gsid + * @return boolean + */ + public static function isDefunctById(int $gsid): bool + { + $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'last_contact', 'last_failure', 'created', 'failed', 'network'], ['id' => $gsid]); + if (empty($gserver)) { + return false; + } else { + if (strtotime($gserver['next_contact']) < time()) { + Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $gserver['url'], false); + } + return self::isDefunct($gserver); + } + } + /** * Checks if the given server id is reachable * * @param integer $gsid * @return boolean */ - public static function reachableById(int $gsid): bool + public static function isReachableById(int $gsid): bool { $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], ['id' => $gsid]); if (empty($gserver)) { @@ -327,13 +360,18 @@ class GServer /** * Reset failed server status by gserver id * - * @param int $gsid + * @param int $gsid + * @param string $network */ - public static function setReachableById(int $gsid) + public static function setReachableById(int $gsid, string $network) { - $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact'], ['id' => $gsid]); + $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact', 'network'], ['id' => $gsid]); if (DBA::isResult($gserver) && $gserver['failed']) { - self::update(['failed' => false, 'last_contact' => DateTimeFormat::utcNow()], ['id' => $gsid]); + $fields = ['failed' => false, 'last_contact' => DateTimeFormat::utcNow()]; + if (!empty($network) && !in_array($gserver['network'], Protocol::FEDERATED)) { + $fields['network'] = $network; + } + self::update($fields, ['id' => $gsid]); Logger::info('Reset failed status for server', ['url' => $gserver['url']]); if (strtotime($gserver['next_contact']) < time()) { @@ -365,7 +403,7 @@ class GServer * * @param string $url */ - public static function setFailure(string $url) + public static function setFailureByUrl(string $url) { $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]); if (DBA::isResult($gserver)) { @@ -374,6 +412,9 @@ class GServer 'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null], ['nurl' => Strings::normaliseLink($url)]); Logger::info('Set failed status for existing server', ['url' => $url]); + if (self::isDefunct($gserver)) { + self::archiveContacts($gserver['id']); + } return; } self::insert(['url' => $url, 'nurl' => Strings::normaliseLink($url), @@ -382,6 +423,18 @@ class GServer Logger::info('Set failed status for new server', ['url' => $url]); } + /** + * Archive server related contacts and inboxes + * + * @param integer $gsid + * @return void + */ + private static function archiveContacts(int $gsid) + { + Contact::update(['archive' => true], ['gsid' => $gsid]); + DBA::update('inbox-status', ['archive' => true], ['gsid' => $gsid]); + } + /** * Remove unwanted content from the given URL * @@ -426,7 +479,7 @@ class GServer // If the URL missmatches, then we mark the old entry as failure if (!Strings::compareLink($url, $original_url)) { - self::setFailure($original_url); + self::setFailureByUrl($original_url); if (!self::getID($url, true)) { self::detect($url, $network, $only_nodeinfo); } @@ -435,7 +488,7 @@ class GServer $valid_url = Network::isUrlValid($url); if (!$valid_url) { - self::setFailure($url); + self::setFailureByUrl($url); return false; } else { $valid_url = rtrim($valid_url, '/'); @@ -447,7 +500,7 @@ class GServer if (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) == parse_url($valid_url, PHP_URL_PATH))) || (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) { Logger::debug('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $valid_url]); - self::setFailure($url); + self::setFailureByUrl($url); if (!self::getID($valid_url, true)) { self::detect($valid_url, $network, $only_nodeinfo); } @@ -461,7 +514,7 @@ class GServer unset($parts['path']); $valid_url = (string)Uri::fromParts($parts); - self::setFailure($url); + self::setFailureByUrl($url); if (!self::getID($valid_url, true)) { self::detect($valid_url, $network, $only_nodeinfo); } @@ -481,7 +534,7 @@ class GServer // When a nodeinfo is present, we don't need to dig further $curlResult = DI::httpClient()->get($url . '/.well-known/x-nodeinfo2', HttpClientAccept::JSON); if ($curlResult->isTimeout()) { - self::setFailure($url); + self::setFailureByUrl($url); return false; } @@ -493,7 +546,7 @@ class GServer if ($only_nodeinfo && empty($serverdata)) { Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]); - self::setFailure($url); + self::setFailureByUrl($url); return false; } elseif (empty($serverdata)) { $serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => Protocol::PHANTOM, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => '']; @@ -532,7 +585,7 @@ class GServer } if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { - self::setFailure($url); + self::setFailureByUrl($url); return false; } @@ -601,7 +654,7 @@ class GServer // Most servers aren't installed in a subdirectory, so we declare this entry as failed if (($serverdata['network'] == Protocol::PHANTOM) && !empty(parse_url($url, PHP_URL_PATH)) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL])) { - self::setFailure($url); + self::setFailureByUrl($url); return false; } @@ -617,6 +670,11 @@ class GServer $serverdata = self::detectNetworkViaContacts($url, $serverdata); } + if (($serverdata['network'] == Protocol::PHANTOM) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_BODY])) { + self::setFailureByUrl($url); + return false; + } + // Detect the directory type $serverdata['directory-type'] = self::DT_NONE;