From f854d7d5f7d20ce2192c0c6f3f4bf78570d6c2dc Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 12 Mar 2023 17:49:44 -0400 Subject: [PATCH] Fix several PHP messages - Add logging for exceptions --- src/classes/Controllers/Web/Servers.php | 4 +- src/classes/Pollers/Profile.php | 57 ++++++++++++++++--------- src/classes/Pollers/Server.php | 55 ++++++++++++++---------- 3 files changed, 72 insertions(+), 44 deletions(-) diff --git a/src/classes/Controllers/Web/Servers.php b/src/classes/Controllers/Web/Servers.php index a4c1d78..580edb9 100644 --- a/src/classes/Controllers/Web/Servers.php +++ b/src/classes/Controllers/Web/Servers.php @@ -66,7 +66,7 @@ class Servers extends BaseController $sql_where = ''; $values = []; - if ($args['language']) { + if (!empty($args['language'])) { $sql_where .= ' AND LEFT(`language`, 2) = LEFT(:language, 2)'; $values['language'] = $args['language']; @@ -104,7 +104,7 @@ AND NOT `hidden` $vars = [ 'title' => $this->l10n->gettext('Public Servers'), 'total' => $count, - 'language' => $args['language'], + 'language' => $args['language'] ?? null, 'servers' => $servers, 'pager' => $pager->renderFull($count), 'stable_version' => $stable_version, diff --git a/src/classes/Pollers/Profile.php b/src/classes/Pollers/Profile.php index b07bbd2..aa3c3dc 100644 --- a/src/classes/Pollers/Profile.php +++ b/src/classes/Pollers/Profile.php @@ -2,7 +2,7 @@ namespace Friendica\Directory\Pollers; -use Friendica\Directory\Utils\Network; +use GuzzleHttp\Exception\RequestException; /** * @author Hypolite Petovan @@ -136,13 +136,28 @@ class Profile //Skip the profile scrape? if ($server['noscrape_url']) { $this->logger->debug('Calling ' . $server['noscrape_url'] . '/' . $username); - $params = \Friendica\Directory\Utils\Scrape::retrieveNoScrapeData($this->http, $server['noscrape_url'] . '/' . $username); + try { + $params = \Friendica\Directory\Utils\Scrape::retrieveNoScrapeData($this->http, $server['noscrape_url'] . '/' . $username); + } catch (RequestException $e) { + $this->logger->info('Request failed with error code ' . $e->getCode()); + } catch (\Throwable $e) { + $this->logger->warning('Request failed with exception ' . get_class($e)); + $this->logger->warning(var_export($e, true)); + } + $available = !!$params; //If the result was false, do a scrape after all. } if (!$available) { - $this->logger->notice('Parsing profile page ' . $profile_uri); - $params = \Friendica\Directory\Utils\Scrape::retrieveProfileData($this->http, $profile_uri); + $this->logger->info('Parsing profile page ' . $profile_uri); + try { + $params = \Friendica\Directory\Utils\Scrape::retrieveProfileData($this->http, $profile_uri); + } catch (RequestException $e) { + $this->logger->info('Request failed with error code ' . $e->getCode()); + } catch (\Throwable $e) { + $this->logger->warning('Request failed with exception ' . get_class($e)); + $this->logger->warning(var_export($e, true)); + } $params['language'] = $server['language']; $available = !empty($params['fn']); @@ -306,23 +321,27 @@ class Profile $status = false; if ($profile_id) { - $img_str = $this->http->get($params['photo'])->getBody()->getContents(); - $img = new \Friendica\Directory\Utils\Photo($img_str); - if ($img->getImage()) { - $img->scaleImageSquare(80); + try { + $img_str = $this->http->get($params['photo'])->getBody()->getContents(); + $img = new \Friendica\Directory\Utils\Photo($img_str); + if ($img->getImage()) { + $img->scaleImageSquare(80); - $this->atlas->perform('INSERT INTO `photo` SET - `profile_id` = :profile_id, - `data` = :data - ON DUPLICATE KEY UPDATE - `data` = :data', - [ - 'profile_id' => $profile_id, - 'data' => $img->imageString() - ] - ); + $this->atlas->perform('INSERT INTO `photo` SET + `profile_id` = :profile_id, + `data` = :data + ON DUPLICATE KEY UPDATE + `data` = :data', + [ + 'profile_id' => $profile_id, + 'data' => $img->imageString() + ] + ); + } + $status = true; + } catch (RequestException $e) { + $this->logger->info('Photo retrieval unsuccessful', ['url' => $params['photo'], 'code' => $e->getCode()]); } - $status = true; } $submit_end = microtime(true); diff --git a/src/classes/Pollers/Server.php b/src/classes/Pollers/Server.php index 6da2a89..290b9b4 100644 --- a/src/classes/Pollers/Server.php +++ b/src/classes/Pollers/Server.php @@ -154,7 +154,7 @@ class Server $addons = $probe_result['data']['plugins']; } - if ($probe_result['data']['admin']['profile']) { + if (!empty($probe_result['data']['admin']['profile'])) { $subscribe = $this->getSubscribeUrl($probe_result['data']['url'], $probe_result['data']['admin']['profile']); } @@ -178,14 +178,14 @@ class Server [ 'server_id' => $server['id'], 'base_url' => strtolower($probe_result['data']['url']), - 'name' => $probe_result['data']['site_name'], + 'name' => substr($probe_result['data']['site_name'], 0, 255), 'language' => $probe_result['data']['language'] ?? null, 'version' => $probe_result['data']['version'], 'addons' => implode(',', $addons), 'reg_policy' => $probe_result['data']['register_policy'], 'info' => $probe_result['data']['info'], - 'admin_name' => $probe_result['data']['admin']['name'], - 'admin_profile' => $probe_result['data']['admin']['profile'], + 'admin_name' => $probe_result['data']['admin']['name'] ?? null, + 'admin_profile' => $probe_result['data']['admin']['profile'] ?? null, 'noscrape_url' => $probe_result['data']['no_scrape_url'] ?? null, 'subscribe_url' => $subscribe ?? null, 'ssl_state' => $probe_result['ssl_state'] @@ -265,29 +265,35 @@ class Server $sslcert_issues = false; + $probe_start = microtime(true); + $probe_data = null; try { //Probe the site. - $probe_start = microtime(true); $probe_data = $this->http->get($base_url . '/friendica/json', $options)->getBody()->getContents(); - $probe_end = microtime(true); } catch (RequestException $e) { - if (!in_array($e->getHandlerContext()['errno'], [ + if (in_array($e->getHandlerContext()['errno'] ?? 0, [ 60, //Could not authenticate certificate with known CA's 83 //Issuer check failed ])) { - throw $e; + $sslcert_issues = true; + + //When it's the certificate that doesn't work, we probe again without strict SSL. + $options['verify'] = false; + + $probe_start = microtime(true); + try { + $probe_data = $this->http->get($base_url . '/friendica/json', $options)->getBody()->getContents(); + } catch(RequestException $e) { + // Collects 404, 500 errors + $this->logger->info('SSL-non-verified URL probe failed with error code: ' . $e->getCode()); + } + } else { + $this->logger->info('SSL-verified URL probe failed with error code: ' . $e->getCode()); } - - $sslcert_issues = true; - - //When it's the certificate that doesn't work, we probe again without strict SSL. - $options['verify'] = false; - - $probe_start = microtime(true); - $probe_data = $this->http->get($base_url . '/friendica/json', $options)->getBody()->getContents(); - $probe_end = microtime(true); } + $probe_end = microtime(true); + $time = round(($probe_end - $probe_start) * 1000); try { @@ -404,11 +410,9 @@ class Server { $uri = Uri::withQueryValues(new Uri($base_url . '/poco'), ['fields' => 'urls', 'count' => 1000]); - $response = $this->http->request('GET', $uri); - - $this->logger->debug('WebRequest: ' . $uri . ' Status: ' . $response->getStatusCode()); - - if ($response->getStatusCode() != 200) { + try { + $response = $this->http->request('GET', $uri); + } catch (RequestException $e) { $this->logger->info('Unsuccessful poco request: ' . $uri); return; } @@ -445,7 +449,12 @@ class Server { $uri = Uri::withQueryValues(new Uri($base_url . '/xrd'), ['uri' => $profile]); - $response = $this->http->request('GET', $uri, ['headers' => ['Accept' => 'application/jrd+json']]); + try { + $response = $this->http->request('GET', $uri, ['headers' => ['Accept' => 'application/jrd+json']]); + } catch (RequestException $e) { + $this->logger->info('Unsuccessful xrd request: ' . $uri); + return null; + } $xrdJsonData = $response->getBody()->getContents();