diff --git a/app/controllers/general.php b/app/controllers/general.php index 8cd4553e3..150fd9ce3 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -3,13 +3,16 @@ require_once __DIR__.'/../init.php'; use Utopia\App; +use Utopia\Locale\Locale; +use Utopia\Logger\Logger; use Utopia\Logger\Log; use Utopia\Logger\Log\User; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Appwrite\Utopia\View; -use Appwrite\Extend\Exception; +use Appwrite\Extend\Exception as AppwriteException; use Utopia\Config\Config; +use Utopia\Exception as UtopiaException; use Utopia\Domains\Domain; use Appwrite\Auth\Auth; use Appwrite\Event\Certificate; @@ -18,6 +21,7 @@ use Appwrite\Utopia\Response\Filters\V11 as ResponseV11; use Appwrite\Utopia\Response\Filters\V12 as ResponseV12; use Appwrite\Utopia\Response\Filters\V13 as ResponseV13; use Utopia\CLI\Console; +use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; @@ -31,16 +35,7 @@ Config::setParam('domainVerification', false); Config::setParam('cookieDomain', 'localhost'); Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE); -App::init(function ($utopia, $request, $response, $console, $project, $dbForConsole, $user, $locale, $clients) { - /** @var Utopia\App $utopia */ - /** @var Appwrite\Utopia\Request $request */ - /** @var Appwrite\Utopia\Response $response */ - /** @var Utopia\Database\Document $console */ - /** @var Utopia\Database\Document $project */ - /** @var Utopia\Database\Database $dbForConsole */ - /** @var Utopia\Database\Document $user */ - /** @var Utopia\Locale\Locale $locale */ - /** @var array $clients */ +App::init(function (App $utopia, Request $request, Response $response, Document $console, Document $project, Database $dbForConsole, Document $user, Locale $locale, array $clients) { /* * Request format @@ -127,11 +122,11 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons } if ($project->isEmpty()) { - throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); + throw new AppwriteException('Project not found', 404, AppwriteException::PROJECT_NOT_FOUND); } if (!empty($route->getLabel('sdk.auth', [])) && $project->isEmpty() && ($route->getLabel('scope', '') !== 'public')) { - throw new Exception('Missing or unknown project ID', 400, Exception::PROJECT_UNKNOWN); + throw new AppwriteException('Missing or unknown project ID', 400, AppwriteException::PROJECT_UNKNOWN); } $referrer = $request->getReferer(); @@ -198,7 +193,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons if (App::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'enabled') { // Force HTTPS if ($request->getProtocol() !== 'https') { if($request->getMethod() !== Request::METHOD_GET) { - throw new Exception('Method unsupported over HTTP.', 500, Exception::GENERAL_PROTOCOL_UNSUPPORTED); + throw new AppwriteException('Method unsupported over HTTP.', 500, AppwriteException::GENERAL_PROTOCOL_UNSUPPORTED); } return $response->redirect('https://'.$request->getHostname().$request->getURI()); @@ -229,7 +224,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons && \in_array($request->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_DELETE]) && $route->getLabel('origin', false) !== '*' && empty($request->getHeader('x-appwrite-key', ''))) { - throw new Exception($originValidator->getDescription(), 403, Exception::GENERAL_UNKNOWN_ORIGIN); + throw new AppwriteException($originValidator->getDescription(), 403, AppwriteException::GENERAL_UNKNOWN_ORIGIN); } /* @@ -298,31 +293,29 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons if(array_key_exists($service, $project->getAttribute('services',[])) && !$project->getAttribute('services',[])[$service] && !Auth::isPrivilegedUser(Authorization::getRoles())) { - throw new Exception('Service is disabled', 503, Exception::GENERAL_SERVICE_DISABLED); + throw new AppwriteException('Service is disabled', 503, AppwriteException::GENERAL_SERVICE_DISABLED); } } if (!\in_array($scope, $scopes)) { if ($project->isEmpty()) { // Check if permission is denied because project is missing - throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); + throw new AppwriteException('Project not found', 404, AppwriteException::PROJECT_NOT_FOUND); } - throw new Exception($user->getAttribute('email', 'User').' (role: '.\strtolower($roles[$role]['label']).') missing scope ('.$scope.')', 401, Exception::GENERAL_UNAUTHORIZED_SCOPE); + throw new AppwriteException($user->getAttribute('email', 'User').' (role: '.\strtolower($roles[$role]['label']).') missing scope ('.$scope.')', 401, AppwriteException::GENERAL_UNAUTHORIZED_SCOPE); } if (false === $user->getAttribute('status')) { // Account is blocked - throw new Exception('Invalid credentials. User is blocked', 401, Exception::USER_BLOCKED); + throw new AppwriteException('Invalid credentials. User is blocked', 401, AppwriteException::USER_BLOCKED); } if ($user->getAttribute('reset')) { - throw new Exception('Password reset is required', 412, Exception::USER_PASSWORD_RESET_REQUIRED); + throw new AppwriteException('Password reset is required', 412, AppwriteException::USER_PASSWORD_RESET_REQUIRED); } }, ['utopia', 'request', 'response', 'console', 'project', 'dbForConsole', 'user', 'locale', 'clients']); -App::options(function ($request, $response) { - /** @var Appwrite\Utopia\Request $request */ - /** @var Appwrite\Utopia\Response $response */ +App::options(function (Request $request, Response $response) { $origin = $request->getOrigin(); @@ -336,15 +329,7 @@ App::options(function ($request, $response) { ->noContent(); }, ['request', 'response']); -App::error(function ($error, $utopia, $request, $response, $layout, $project, $logger, $loggerBreadcrumbs) { - /** @var Exception $error */ - /** @var Utopia\App $utopia */ - /** @var Appwrite\Utopia\Request $request */ - /** @var Appwrite\Utopia\Response $response */ - /** @var Appwrite\Utopia\View $layout */ - /** @var Utopia\Database\Document $project */ - /** @var Utopia\Logger\Logger $logger */ - /** @var Utopia\Logger\Log\Breadcrumb[] $loggerBreadcrumbs */ +App::error(function (AppwriteException|UtopiaException $error, App $utopia, Request $request, Response $response, View $layout, Document $project, ?Logger $logger, array $loggerBreadcrumbs) { $version = App::getEnv('_APP_VERSION', 'UNKNOWN'); $route = $utopia->match($request); @@ -426,20 +411,20 @@ App::error(function ($error, $utopia, $request, $response, $layout, $project, $l /** Handle Utopia Errors */ if ($error instanceof Utopia\Exception) { - $error = new Exception($message, $code, Exception::GENERAL_UNKNOWN, $error); + $error = new AppwriteException($message, $code, AppwriteException::GENERAL_UNKNOWN, $error); switch($code) { case 400: - $error->setType(Exception::GENERAL_ARGUMENT_INVALID); + $error->setType(AppwriteException::GENERAL_ARGUMENT_INVALID); break; case 404: - $error->setType(Exception::GENERAL_ROUTE_NOT_FOUND); + $error->setType(AppwriteException::GENERAL_ROUTE_NOT_FOUND); break; } } /** Wrap all exceptions inside Appwrite\Extend\Exception */ - if (!($error instanceof Exception)) { - $error = new Exception($message, $code, Exception::GENERAL_UNKNOWN, $error); + if (!($error instanceof AppwriteException)) { + $error = new AppwriteException($message, $code, AppwriteException::GENERAL_UNKNOWN, $error); } switch ($code) { // Don't show 500 errors! @@ -520,8 +505,7 @@ App::get('/manifest.json') ->label('scope', 'public') ->label('docs', false) ->inject('response') - ->action(function ($response) { - /** @var Appwrite\Utopia\Response $response */ + ->action(function (Response $response) { $response->json([ 'name' => APP_NAME, @@ -547,7 +531,7 @@ App::get('/robots.txt') ->label('scope', 'public') ->label('docs', false) ->inject('response') - ->action(function ($response) { + ->action(function (Response $response) { $template = new View(__DIR__.'/../views/general/robots.phtml'); $response->text($template->render(false)); }); @@ -557,7 +541,7 @@ App::get('/humans.txt') ->label('scope', 'public') ->label('docs', false) ->inject('response') - ->action(function ($response) { + ->action(function (Response $response) { $template = new View(__DIR__.'/../views/general/humans.phtml'); $response->text($template->render(false)); }); @@ -568,7 +552,7 @@ App::get('/.well-known/acme-challenge') ->label('docs', false) ->inject('request') ->inject('response') - ->action(function ($request, $response) { + ->action(function (Request $request, Response $response) { $uriChunks = \explode('/', $request->getURI()); $token = $uriChunks[\count($uriChunks) - 1]; @@ -581,32 +565,32 @@ App::get('/.well-known/acme-challenge') ]); if (!$validator->isValid($token) || \count($uriChunks) !== 4) { - throw new Exception('Invalid challenge token.', 400); + throw new AppwriteException('Invalid challenge token.', 400); } $base = \realpath(APP_STORAGE_CERTIFICATES); $absolute = \realpath($base.'/.well-known/acme-challenge/'.$token); if (!$base) { - throw new Exception('Storage error', 500, Exception::GENERAL_SERVER_ERROR); + throw new AppwriteException('Storage error', 500, AppwriteException::GENERAL_SERVER_ERROR); } if (!$absolute) { - throw new Exception('Unknown path', 404); + throw new AppwriteException('Unknown path', 404); } if (!\substr($absolute, 0, \strlen($base)) === $base) { - throw new Exception('Invalid path', 401); + throw new AppwriteException('Invalid path', 401); } if (!\file_exists($absolute)) { - throw new Exception('Unknown path', 404); + throw new AppwriteException('Unknown path', 404); } $content = @\file_get_contents($absolute); if (!$content) { - throw new Exception('Failed to get contents', 500, Exception::GENERAL_SERVER_ERROR); + throw new AppwriteException('Failed to get contents', 500, AppwriteException::GENERAL_SERVER_ERROR); } $response->text($content);