X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FBaseApi.php;h=dc75b0dd262fc0da5a3ad206fc80e563a45323f2;hb=3bca4fe2a64671d09e08346456cdfa6c12f996e9;hp=b5fc8c8499254b97e74f2ab670cc8a6b984be71a;hpb=180b81c6e74bdc8d677f447fe796fb10783c96dd;p=friendica.git diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index b5fc8c8499..dc75b0dd26 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -1,6 +1,6 @@ app = $app; + $this->app = $app; + $this->errorFactory = $errorFactory; } /** @@ -93,7 +97,7 @@ class BaseApi extends BaseModule case Router::PATCH: case Router::POST: case Router::PUT: - self::checkAllowedScope(self::SCOPE_WRITE); + $this->checkAllowedScope(self::SCOPE_WRITE); if (!self::getCurrentUserID()) { throw new HTTPException\ForbiddenException($this->t('Permission denied.')); @@ -372,7 +376,7 @@ class BaseApi extends BaseModule */ public static function appSupportsQuotes(): bool { - $token = self::getCurrentApplication(); + $token = OAuth::getCurrentApplicationToken(); return (!empty($token['name']) && in_array($token['name'], ['Fedilab'])); } @@ -414,27 +418,27 @@ class BaseApi extends BaseModule * * @param string $scope the requested scope (read, write, follow, push) */ - public static function checkAllowedScope(string $scope) + public function checkAllowedScope(string $scope) { $token = self::getCurrentApplication(); if (empty($token)) { - Logger::notice('Empty application token'); - DI::mstdnError()->Forbidden(); + $this->logger->notice('Empty application token'); + $this->logAndJsonError(403, $this->errorFactory->Forbidden()); } if (!isset($token[$scope])) { - Logger::warning('The requested scope does not exist', ['scope' => $scope, 'application' => $token]); - DI::mstdnError()->Forbidden(); + $this->logger->warning('The requested scope does not exist', ['scope' => $scope, 'application' => $token]); + $this->logAndJsonError(403, $this->errorFactory->Forbidden()); } if (empty($token[$scope])) { - Logger::warning('The requested scope is not allowed', ['scope' => $scope, 'application' => $token]); - DI::mstdnError()->Forbidden(); + $this->logger->warning('The requested scope is not allowed', ['scope' => $scope, 'application' => $token]); + $this->logAndJsonError(403, $this->errorFactory->Forbidden()); } } - public static function checkThrottleLimit() + public function checkThrottleLimit() { $uid = self::getCurrentUserID(); @@ -447,11 +451,11 @@ class BaseApi extends BaseModule $posts_day = Post::countThread($condition); if ($posts_day > $throttle_day) { - Logger::notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]); - $error = DI::l10n()->t('Too Many Requests'); - $error_description = DI::l10n()->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day); + $this->logger->notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]); + $error = $this->t('Too Many Requests'); + $error_description = $this->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day); $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); - System::jsonError(429, $errorobj->toArray()); + $this->jsonError(429, $errorobj->toArray()); } } @@ -464,10 +468,10 @@ class BaseApi extends BaseModule if ($posts_week > $throttle_week) { Logger::notice('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]); - $error = DI::l10n()->t('Too Many Requests'); - $error_description = DI::l10n()->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week); + $error = $this->t('Too Many Requests'); + $error_description = $this->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week); $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); - System::jsonError(429, $errorobj->toArray()); + $this->jsonError(429, $errorobj->toArray()); } } @@ -480,10 +484,10 @@ class BaseApi extends BaseModule if ($posts_month > $throttle_month) { Logger::notice('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]); - $error = DI::l10n()->t('Too Many Requests'); - $error_description = DI::l10n()->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month); + $error = $this->t('Too Many Requests'); + $error_description = $this->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month); $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); - System::jsonError(429, $errorobj->toArray()); + $this->jsonError(429, $errorobj->toArray()); } } } @@ -515,4 +519,16 @@ class BaseApi extends BaseModule return null; } + + /** + * @param int $errorno + * @param Error $error + * @return void + * @throws HTTPException\InternalServerErrorException + */ + protected function logAndJsonError(int $errorno, Error $error) + { + $this->logger->info('API Error', ['no' => $errorno, 'error' => $error->toArray(), 'method' => $this->args->getMethod(), 'command' => $this->args->getQueryString(), 'user-agent' => $this->server['HTTP_USER_AGENT'] ?? '']); + $this->jsonError(403, $error->toArray()); + } }