use Friendica\Core\Worker;
use Friendica\Database\Database;
use Friendica\Database\DBA;
--use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Module\Register;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
$gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
if (DBA::isResult($gserver)) {
$next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']);
- DBA::update('gserver', ['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
- self::update(['failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
- 'next_contact' => $next_update, 'detection-method' => null],
++ self::update(['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
+ 'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null],
['nurl' => Strings::normaliseLink($url)]);
Logger::info('Set failed status for existing server', ['url' => $url]);
return;
*
* @return boolean 'true' if server could be detected
*/
- public static function detect(string $url, string $network = '', bool $only_nodeinfo = false)
+ public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
{
Logger::info('Detect server type', ['server' => $url]);
- $serverdata = ['detection-method' => self::DETECT_MANUAL];
$original_url = $url;
$serverdata['last_contact'] = DateTimeFormat::utcNow();
$serverdata['failed'] = false;
- // Limit the length on incoming fields
- $serverdata = DBStructure::getFieldsForTable('gserver', $serverdata);
-
- $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => $serverdata['nurl']]);
+ $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
if (!DBA::isResult($gserver)) {
$serverdata['created'] = DateTimeFormat::utcNow();
$ret = DBA::insert('gserver', $serverdata);
$contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 30 days')]);
if ($contacts > 0) {
Logger::info('Update monthly users', ['id' => $id, 'url' => $serverdata['nurl'], 'monthly-users' => $contacts]);
- DBA::update('gserver', ['active-month-users' => $contacts], ['id' => $id]);
+ self::update(['active-month-users' => $contacts], ['id' => $id]);
}
}
-
+
if (empty($serverdata['active-halfyear-users'])) {
$contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 180 days')]);
if ($contacts > 0) {
* Fetch server data from '/statistics.json' on the given server
*
* @param string $url URL of the given server
- *
* @return array server data
*/
- private static function fetchStatistics(string $url): array
+ private static function fetchStatistics(string $url, array $serverdata)
{
$curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON);
if (!$curlResult->isSuccess()) {
*
* @param object $curlResult result of curl execution
* @param array $serverdata array with server data
- * @param string $url Server URL
+ *
* @return array server data
*/
- private static function analyseRootBody($curlResult, array $serverdata)
- private static function analyseRootBody($curlResult, array $serverdata, string $url): array
++ private static function analyseRootBody($curlResult, array $serverdata): array
{
if (empty($curlResult->getBody())) {
return $serverdata;
* Fetch the content type of the given url
* @param string $url URL of the page
* @param string $accept content-type to accept
+ * @param int $timeout
* @return array content type
*/
- public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT, int $timeout = 0)
- public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT): array
++ public static function getContentType(string $url, string $accept = HttpClientAccept::DEFAULT, int $timeout = 0): array
{
- $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => $accept]);
+ if (!empty($timeout)) {
+ $options = [HttpClientOptions::TIMEOUT => $timeout];
+ } else {
+ $options = [];
+ }
+
+ $curlResult = DI::httpClient()->head($url, array_merge([HttpClientOptions::ACCEPT_CONTENT => $accept], $options));
- // Workaround for systems that can't handle a HEAD request
- if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) {
- $curlResult = DI::httpClient()->get($url, $accept, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
+ // Workaround for systems that can't handle a HEAD request. Don't retry on timeouts.
+ if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() >= 400) && !in_array($curlResult->getReturnCode(), [408, 504])) {
+ $curlResult = DI::httpClient()->get($url, $accept, array_merge([HttpClientOptions::CONTENT_LENGTH => 1000000], $options));
}
if (!$curlResult->isSuccess()) {