1
0
Fork 0
mirror of synced 2024-09-20 11:37:45 +12:00

get project db key by region

This commit is contained in:
shimon 2024-08-05 15:26:20 +03:00
parent 3c749153f0
commit 6b3051f131
5 changed files with 346 additions and 444 deletions

View file

@ -1,7 +1,7 @@
<?php <?php
require_once __DIR__ . '/init.php'; require_once __DIR__.'/init.php';
require_once __DIR__ . '/controllers/general.php'; require_once __DIR__.'/controllers/general.php';
use Appwrite\Event\Certificate; use Appwrite\Event\Certificate;
use Appwrite\Event\Delete; use Appwrite\Event\Delete;
@ -35,8 +35,7 @@ CLI::setResource('cache', function ($pools) {
$adapters[] = $pools $adapters[] = $pools
->get($value) ->get($value)
->pop() ->pop()
->getResource() ->getResource();
;
} }
return new Cache(new Sharding($adapters)); return new Cache(new Sharding($adapters));
@ -72,7 +71,7 @@ CLI::setResource('dbForConsole', function ($pools, $cache) {
$collections = Config::getParam('collections', [])['console']; $collections = Config::getParam('collections', [])['console'];
$last = \array_key_last($collections); $last = \array_key_last($collections);
if (!($dbForConsole->exists($dbForConsole->getDatabase(), $last))) { /** TODO cache ready variable using registry */ if (! ($dbForConsole->exists($dbForConsole->getDatabase(), $last))) { /** TODO cache ready variable using registry */
throw new Exception('Tables not ready yet.'); throw new Exception('Tables not ready yet.');
} }
@ -82,10 +81,10 @@ CLI::setResource('dbForConsole', function ($pools, $cache) {
$pools->get('console')->reclaim(); $pools->get('console')->reclaim();
sleep($sleep); sleep($sleep);
} }
} while ($attempts < $maxAttempts && !$ready); } while ($attempts < $maxAttempts && ! $ready);
if (!$ready) { if (! $ready) {
throw new Exception("Console is not ready yet. Please try again later."); throw new Exception('Console is not ready yet. Please try again later.');
} }
return $dbForConsole; return $dbForConsole;
@ -103,13 +102,13 @@ CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole,
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://'.$project->getAttribute('database'));
} }
if (isset($databases[$dsn->getHost()])) { if (isset($databases[$dsn->getHost()])) {
$database = $databases[$dsn->getHost()]; $database = $databases[$dsn->getHost()];
$sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$database $database
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())
@ -118,7 +117,7 @@ CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole,
$database $database
->setSharedTables(false) ->setSharedTables(false)
->setTenant(null) ->setTenant(null)
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_'.$project->getInternalId());
} }
return $database; return $database;
@ -133,7 +132,8 @@ CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole,
$databases[$dsn->getHost()] = $database; $databases[$dsn->getHost()] = $database;
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$database $database
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())
@ -142,7 +142,7 @@ CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole,
$database $database
->setSharedTables(false) ->setSharedTables(false)
->setTenant(null) ->setTenant(null)
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_'.$project->getInternalId());
} }
$database $database
@ -192,7 +192,7 @@ CLI::setResource('logError', function (Registry $register) {
$log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING); $log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING);
$responseCode = $logger->addLog($log); $responseCode = $logger->addLog($log);
Console::info('Usage stats log pushed with status code: ' . $responseCode); Console::info('Usage stats log pushed with status code: '.$responseCode);
} }
Console::warning("Failed: {$error->getMessage()}"); Console::warning("Failed: {$error->getMessage()}");

View file

@ -135,7 +135,7 @@ App::post('/v1/projects')
// TODO: Temporary until all projects are using shared tables. // TODO: Temporary until all projects are using shared tables.
$sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn, $sharedTablesKeys)) { if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$schema = 'appwrite'; $schema = 'appwrite';
$database = 'appwrite'; $database = 'appwrite';
$namespace = System::getEnv('_APP_DATABASE_SHARED_NAMESPACE', ''); $namespace = System::getEnv('_APP_DATABASE_SHARED_NAMESPACE', '');
@ -199,7 +199,8 @@ App::post('/v1/projects')
$adapter = $pools->get($dsn->getHost())->pop()->getResource(); $adapter = $pools->get($dsn->getHost())->pop()->getResource();
$dbForProject = new Database($adapter, $cache); $dbForProject = new Database($adapter, $cache);
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$dbForProject $dbForProject
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())

View file

@ -5,11 +5,9 @@
* *
* Initializes both Appwrite API entry point, queue workers, and CLI tasks. * Initializes both Appwrite API entry point, queue workers, and CLI tasks.
* Set configuration, framework resources & app constants * Set configuration, framework resources & app constants
*
*/ */
if (\file_exists(__DIR__.'/../vendor/autoload.php')) {
if (\file_exists(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';
} }
\ini_set('memory_limit', '512M'); \ini_set('memory_limit', '512M');
@ -89,7 +87,7 @@ const APP_NAME = 'Appwrite';
const APP_DOMAIN = 'appwrite.io'; const APP_DOMAIN = 'appwrite.io';
const APP_EMAIL_TEAM = 'team@localhost.test'; // Default email address const APP_EMAIL_TEAM = 'team@localhost.test'; // Default email address
const APP_EMAIL_SECURITY = ''; // Default security email address const APP_EMAIL_SECURITY = ''; // Default security email address
const APP_USERAGENT = APP_NAME . '-Server v%s. Please report abuse at %s'; const APP_USERAGENT = APP_NAME.'-Server v%s. Please report abuse at %s';
const APP_MODE_DEFAULT = 'default'; const APP_MODE_DEFAULT = 'default';
const APP_MODE_ADMIN = 'admin'; const APP_MODE_ADMIN = 'admin';
const APP_PAGING_LIMIT = 12; const APP_PAGING_LIMIT = 12;
@ -178,7 +176,7 @@ const DELETE_TYPE_INSTALLATIONS = 'installations';
const DELETE_TYPE_RULES = 'rules'; const DELETE_TYPE_RULES = 'rules';
const DELETE_TYPE_SESSIONS = 'sessions'; const DELETE_TYPE_SESSIONS = 'sessions';
const DELETE_TYPE_CACHE_BY_TIMESTAMP = 'cacheByTimeStamp'; const DELETE_TYPE_CACHE_BY_TIMESTAMP = 'cacheByTimeStamp';
const DELETE_TYPE_CACHE_BY_RESOURCE = 'cacheByResource'; const DELETE_TYPE_CACHE_BY_RESOURCE = 'cacheByResource';
const DELETE_TYPE_SCHEDULES = 'schedules'; const DELETE_TYPE_SCHEDULES = 'schedules';
const DELETE_TYPE_TOPIC = 'topic'; const DELETE_TYPE_TOPIC = 'topic';
const DELETE_TYPE_TARGET = 'target'; const DELETE_TYPE_TARGET = 'target';
@ -211,9 +209,9 @@ const MESSAGE_TYPE_PUSH = 'push';
// Usage metrics // Usage metrics
const METRIC_TEAMS = 'teams'; const METRIC_TEAMS = 'teams';
const METRIC_USERS = 'users'; const METRIC_USERS = 'users';
const METRIC_MESSAGES = 'messages'; const METRIC_MESSAGES = 'messages';
const METRIC_MESSAGES_COUNTRY_CODE = '{countryCode}.messages'; const METRIC_MESSAGES_COUNTRY_CODE = '{countryCode}.messages';
const METRIC_SESSIONS = 'sessions'; const METRIC_SESSIONS = 'sessions';
const METRIC_DATABASES = 'databases'; const METRIC_DATABASES = 'databases';
const METRIC_COLLECTIONS = 'collections'; const METRIC_COLLECTIONS = 'collections';
const METRIC_DATABASE_ID_COLLECTIONS = '{databaseInternalId}.collections'; const METRIC_DATABASE_ID_COLLECTIONS = '{databaseInternalId}.collections';
@ -221,38 +219,38 @@ const METRIC_DOCUMENTS = 'documents';
const METRIC_DATABASE_ID_DOCUMENTS = '{databaseInternalId}.documents'; const METRIC_DATABASE_ID_DOCUMENTS = '{databaseInternalId}.documents';
const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS = '{databaseInternalId}.{collectionInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS = '{databaseInternalId}.{collectionInternalId}.documents';
const METRIC_BUCKETS = 'buckets'; const METRIC_BUCKETS = 'buckets';
const METRIC_FILES = 'files'; const METRIC_FILES = 'files';
const METRIC_FILES_STORAGE = 'files.storage'; const METRIC_FILES_STORAGE = 'files.storage';
const METRIC_BUCKET_ID_FILES = '{bucketInternalId}.files'; const METRIC_BUCKET_ID_FILES = '{bucketInternalId}.files';
const METRIC_BUCKET_ID_FILES_STORAGE = '{bucketInternalId}.files.storage'; const METRIC_BUCKET_ID_FILES_STORAGE = '{bucketInternalId}.files.storage';
const METRIC_FUNCTIONS = 'functions'; const METRIC_FUNCTIONS = 'functions';
const METRIC_DEPLOYMENTS = 'deployments'; const METRIC_DEPLOYMENTS = 'deployments';
const METRIC_DEPLOYMENTS_STORAGE = 'deployments.storage'; const METRIC_DEPLOYMENTS_STORAGE = 'deployments.storage';
const METRIC_BUILDS = 'builds'; const METRIC_BUILDS = 'builds';
const METRIC_BUILDS_STORAGE = 'builds.storage'; const METRIC_BUILDS_STORAGE = 'builds.storage';
const METRIC_BUILDS_COMPUTE = 'builds.compute'; const METRIC_BUILDS_COMPUTE = 'builds.compute';
const METRIC_BUILDS_MB_SECONDS = 'builds.mbSeconds'; const METRIC_BUILDS_MB_SECONDS = 'builds.mbSeconds';
const METRIC_FUNCTION_ID_BUILDS = '{functionInternalId}.builds'; const METRIC_FUNCTION_ID_BUILDS = '{functionInternalId}.builds';
const METRIC_FUNCTION_ID_BUILDS_STORAGE = '{functionInternalId}.builds.storage'; const METRIC_FUNCTION_ID_BUILDS_STORAGE = '{functionInternalId}.builds.storage';
const METRIC_FUNCTION_ID_BUILDS_COMPUTE = '{functionInternalId}.builds.compute'; const METRIC_FUNCTION_ID_BUILDS_COMPUTE = '{functionInternalId}.builds.compute';
const METRIC_FUNCTION_ID_DEPLOYMENTS = '{resourceType}.{resourceInternalId}.deployments'; const METRIC_FUNCTION_ID_DEPLOYMENTS = '{resourceType}.{resourceInternalId}.deployments';
const METRIC_FUNCTION_ID_DEPLOYMENTS_STORAGE = '{resourceType}.{resourceInternalId}.deployments.storage'; const METRIC_FUNCTION_ID_DEPLOYMENTS_STORAGE = '{resourceType}.{resourceInternalId}.deployments.storage';
const METRIC_FUNCTION_ID_BUILDS_MB_SECONDS = '{functionInternalId}.builds.mbSeconds'; const METRIC_FUNCTION_ID_BUILDS_MB_SECONDS = '{functionInternalId}.builds.mbSeconds';
const METRIC_EXECUTIONS = 'executions'; const METRIC_EXECUTIONS = 'executions';
const METRIC_EXECUTIONS_COMPUTE = 'executions.compute'; const METRIC_EXECUTIONS_COMPUTE = 'executions.compute';
const METRIC_EXECUTIONS_MB_SECONDS = 'executions.mbSeconds'; const METRIC_EXECUTIONS_MB_SECONDS = 'executions.mbSeconds';
const METRIC_FUNCTION_ID_EXECUTIONS = '{functionInternalId}.executions'; const METRIC_FUNCTION_ID_EXECUTIONS = '{functionInternalId}.executions';
const METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE = '{functionInternalId}.executions.compute'; const METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE = '{functionInternalId}.executions.compute';
const METRIC_FUNCTION_ID_EXECUTIONS_MB_SECONDS = '{functionInternalId}.executions.mbSeconds'; const METRIC_FUNCTION_ID_EXECUTIONS_MB_SECONDS = '{functionInternalId}.executions.mbSeconds';
const METRIC_NETWORK_REQUESTS = 'network.requests'; const METRIC_NETWORK_REQUESTS = 'network.requests';
const METRIC_NETWORK_INBOUND = 'network.inbound'; const METRIC_NETWORK_INBOUND = 'network.inbound';
const METRIC_NETWORK_OUTBOUND = 'network.outbound'; const METRIC_NETWORK_OUTBOUND = 'network.outbound';
$register = new Registry(); $register = new Registry();
App::setMode(System::getEnv('_APP_ENV', App::MODE_TYPE_PRODUCTION)); App::setMode(System::getEnv('_APP_ENV', App::MODE_TYPE_PRODUCTION));
if (!App::isProduction()) { if (! App::isProduction()) {
// Allow specific domains to skip public domain validation in dev environment // Allow specific domains to skip public domain validation in dev environment
// Useful for existing tests involving webhooks // Useful for existing tests involving webhooks
PublicDomain::allow(['request-catcher']); PublicDomain::allow(['request-catcher']);
@ -261,36 +259,36 @@ if (!App::isProduction()) {
/* /*
* ENV vars * ENV vars
*/ */
Config::load('events', __DIR__ . '/config/events.php'); Config::load('events', __DIR__.'/config/events.php');
Config::load('auth', __DIR__ . '/config/auth.php'); Config::load('auth', __DIR__.'/config/auth.php');
Config::load('apis', __DIR__ . '/config/apis.php'); // List of APIs Config::load('apis', __DIR__.'/config/apis.php'); // List of APIs
Config::load('errors', __DIR__ . '/config/errors.php'); Config::load('errors', __DIR__.'/config/errors.php');
Config::load('oAuthProviders', __DIR__ . '/config/oAuthProviders.php'); Config::load('oAuthProviders', __DIR__.'/config/oAuthProviders.php');
Config::load('platforms', __DIR__ . '/config/platforms.php'); Config::load('platforms', __DIR__.'/config/platforms.php');
Config::load('collections', __DIR__ . '/config/collections.php'); Config::load('collections', __DIR__.'/config/collections.php');
Config::load('runtimes', __DIR__ . '/config/runtimes.php'); Config::load('runtimes', __DIR__.'/config/runtimes.php');
Config::load('runtimes-v2', __DIR__ . '/config/runtimes-v2.php'); Config::load('runtimes-v2', __DIR__.'/config/runtimes-v2.php');
Config::load('usage', __DIR__ . '/config/usage.php'); Config::load('usage', __DIR__.'/config/usage.php');
Config::load('roles', __DIR__ . '/config/roles.php'); // User roles and scopes Config::load('roles', __DIR__.'/config/roles.php'); // User roles and scopes
Config::load('scopes', __DIR__ . '/config/scopes.php'); // User roles and scopes Config::load('scopes', __DIR__.'/config/scopes.php'); // User roles and scopes
Config::load('services', __DIR__ . '/config/services.php'); // List of services Config::load('services', __DIR__.'/config/services.php'); // List of services
Config::load('variables', __DIR__ . '/config/variables.php'); // List of env variables Config::load('variables', __DIR__.'/config/variables.php'); // List of env variables
Config::load('regions', __DIR__ . '/config/regions.php'); // List of available regions Config::load('regions', __DIR__.'/config/regions.php'); // List of available regions
Config::load('avatar-browsers', __DIR__ . '/config/avatars/browsers.php'); Config::load('avatar-browsers', __DIR__.'/config/avatars/browsers.php');
Config::load('avatar-credit-cards', __DIR__ . '/config/avatars/credit-cards.php'); Config::load('avatar-credit-cards', __DIR__.'/config/avatars/credit-cards.php');
Config::load('avatar-flags', __DIR__ . '/config/avatars/flags.php'); Config::load('avatar-flags', __DIR__.'/config/avatars/flags.php');
Config::load('locale-codes', __DIR__ . '/config/locale/codes.php'); Config::load('locale-codes', __DIR__.'/config/locale/codes.php');
Config::load('locale-currencies', __DIR__ . '/config/locale/currencies.php'); Config::load('locale-currencies', __DIR__.'/config/locale/currencies.php');
Config::load('locale-eu', __DIR__ . '/config/locale/eu.php'); Config::load('locale-eu', __DIR__.'/config/locale/eu.php');
Config::load('locale-languages', __DIR__ . '/config/locale/languages.php'); Config::load('locale-languages', __DIR__.'/config/locale/languages.php');
Config::load('locale-phones', __DIR__ . '/config/locale/phones.php'); Config::load('locale-phones', __DIR__.'/config/locale/phones.php');
Config::load('locale-countries', __DIR__ . '/config/locale/countries.php'); Config::load('locale-countries', __DIR__.'/config/locale/countries.php');
Config::load('locale-continents', __DIR__ . '/config/locale/continents.php'); Config::load('locale-continents', __DIR__.'/config/locale/continents.php');
Config::load('locale-templates', __DIR__ . '/config/locale/templates.php'); Config::load('locale-templates', __DIR__.'/config/locale/templates.php');
Config::load('storage-logos', __DIR__ . '/config/storage/logos.php'); Config::load('storage-logos', __DIR__.'/config/storage/logos.php');
Config::load('storage-mimes', __DIR__ . '/config/storage/mimes.php'); Config::load('storage-mimes', __DIR__.'/config/storage/mimes.php');
Config::load('storage-inputs', __DIR__ . '/config/storage/inputs.php'); Config::load('storage-inputs', __DIR__.'/config/storage/inputs.php');
Config::load('storage-outputs', __DIR__ . '/config/storage/outputs.php'); Config::load('storage-outputs', __DIR__.'/config/storage/outputs.php');
/** /**
* New DB Filters * New DB Filters
@ -345,8 +343,7 @@ Database::addFilter(
if (isset($formatOptions['min']) || isset($formatOptions['max'])) { if (isset($formatOptions['min']) || isset($formatOptions['max'])) {
$attribute $attribute
->setAttribute('min', $formatOptions['min']) ->setAttribute('min', $formatOptions['min'])
->setAttribute('max', $formatOptions['max']) ->setAttribute('max', $formatOptions['max']);
;
} }
return $value; return $value;
@ -355,9 +352,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryAttributes', 'subQueryAttributes',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
$attributes = $database->find('attributes', [ $attributes = $database->find('attributes', [
Query::equal('collectionInternalId', [$document->getInternalId()]), Query::equal('collectionInternalId', [$document->getInternalId()]),
@ -381,9 +376,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryIndexes', 'subQueryIndexes',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('indexes', [ ->find('indexes', [
@ -396,9 +389,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryPlatforms', 'subQueryPlatforms',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('platforms', [ ->find('platforms', [
@ -410,9 +401,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryKeys', 'subQueryKeys',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('keys', [ ->find('keys', [
@ -424,9 +413,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryWebhooks', 'subQueryWebhooks',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('webhooks', [ ->find('webhooks', [
@ -438,9 +425,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQuerySessions', 'subQuerySessions',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return Authorization::skip(fn () => $database->find('sessions', [ return Authorization::skip(fn () => $database->find('sessions', [
Query::equal('userInternalId', [$document->getInternalId()]), Query::equal('userInternalId', [$document->getInternalId()]),
@ -451,9 +436,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryTokens', 'subQueryTokens',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return Authorization::skip(fn () => $database return Authorization::skip(fn () => $database
->find('tokens', [ ->find('tokens', [
@ -465,9 +448,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryChallenges', 'subQueryChallenges',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return Authorization::skip(fn () => $database return Authorization::skip(fn () => $database
->find('challenges', [ ->find('challenges', [
@ -479,9 +460,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryAuthenticators', 'subQueryAuthenticators',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return Authorization::skip(fn () => $database return Authorization::skip(fn () => $database
->find('authenticators', [ ->find('authenticators', [
@ -493,9 +472,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryMemberships', 'subQueryMemberships',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return Authorization::skip(fn () => $database return Authorization::skip(fn () => $database
->find('memberships', [ ->find('memberships', [
@ -507,9 +484,7 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryVariables', 'subQueryVariables',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('variables', [ ->find('variables', [
@ -540,7 +515,7 @@ Database::addFilter(
return; return;
} }
$value = json_decode($value, true); $value = json_decode($value, true);
$key = System::getEnv('_APP_OPENSSL_KEY_V' . $value['version']); $key = System::getEnv('_APP_OPENSSL_KEY_V'.$value['version']);
return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag'])); return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag']));
} }
@ -548,14 +523,12 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryProjectVariables', 'subQueryProjectVariables',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('variables', [ ->find('variables', [
Query::equal('resourceType', ['project']), Query::equal('resourceType', ['project']),
Query::limit(APP_LIMIT_SUBQUERY) Query::limit(APP_LIMIT_SUBQUERY),
]); ]);
} }
); );
@ -567,11 +540,11 @@ Database::addFilter(
$user->getId(), $user->getId(),
$user->getAttribute('email', ''), $user->getAttribute('email', ''),
$user->getAttribute('name', ''), $user->getAttribute('name', ''),
$user->getAttribute('phone', '') $user->getAttribute('phone', ''),
]; ];
foreach ($user->getAttribute('labels', []) as $label) { foreach ($user->getAttribute('labels', []) as $label) {
$searchValues[] = 'label:' . $label; $searchValues[] = 'label:'.$label;
} }
$search = implode(' ', \array_filter($searchValues)); $search = implode(' ', \array_filter($searchValues));
@ -585,36 +558,33 @@ Database::addFilter(
Database::addFilter( Database::addFilter(
'subQueryTargets', 'subQueryTargets',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return Authorization::skip(fn () => $database return Authorization::skip(fn () => $database
->find('targets', [ ->find('targets', [
Query::equal('userInternalId', [$document->getInternalId()]), Query::equal('userInternalId', [$document->getInternalId()]),
Query::limit(APP_LIMIT_SUBQUERY) Query::limit(APP_LIMIT_SUBQUERY),
])); ]));
} }
); );
Database::addFilter( Database::addFilter(
'subQueryTopicTargets', 'subQueryTopicTargets',
function (mixed $value) { function (mixed $value) {},
return;
},
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
$targetIds = Authorization::skip(fn () => \array_map( $targetIds = Authorization::skip(fn () => \array_map(
fn ($document) => $document->getAttribute('targetInternalId'), fn ($document) => $document->getAttribute('targetInternalId'),
$database->find('subscribers', [ $database->find('subscribers', [
Query::equal('topicInternalId', [$document->getInternalId()]), Query::equal('topicInternalId', [$document->getInternalId()]),
Query::limit(APP_LIMIT_SUBSCRIBERS_SUBQUERY) Query::limit(APP_LIMIT_SUBSCRIBERS_SUBQUERY),
]) ])
)); ));
if (\count($targetIds) > 0) { if (\count($targetIds) > 0) {
return $database->skipValidation(fn () => $database->find('targets', [ return $database->skipValidation(fn () => $database->find('targets', [
Query::equal('$internalId', $targetIds) Query::equal('$internalId', $targetIds),
])); ]));
} }
return []; return [];
} }
); );
@ -626,7 +596,7 @@ Database::addFilter(
$provider->getId(), $provider->getId(),
$provider->getAttribute('name', ''), $provider->getAttribute('name', ''),
$provider->getAttribute('provider', ''), $provider->getAttribute('provider', ''),
$provider->getAttribute('type', '') $provider->getAttribute('type', ''),
]; ];
$search = \implode(' ', \array_filter($searchValues)); $search = \implode(' ', \array_filter($searchValues));
@ -698,6 +668,7 @@ Structure::addFormat(APP_DATABASE_ATTRIBUTE_DATETIME, function () {
Structure::addFormat(APP_DATABASE_ATTRIBUTE_ENUM, function ($attribute) { Structure::addFormat(APP_DATABASE_ATTRIBUTE_ENUM, function ($attribute) {
$elements = $attribute['formatOptions']['elements']; $elements = $attribute['formatOptions']['elements'];
return new WhiteList($elements, true); return new WhiteList($elements, true);
}, Database::VAR_STRING); }, Database::VAR_STRING);
@ -712,12 +683,14 @@ Structure::addFormat(APP_DATABASE_ATTRIBUTE_URL, function () {
Structure::addFormat(APP_DATABASE_ATTRIBUTE_INT_RANGE, function ($attribute) { Structure::addFormat(APP_DATABASE_ATTRIBUTE_INT_RANGE, function ($attribute) {
$min = $attribute['formatOptions']['min'] ?? -INF; $min = $attribute['formatOptions']['min'] ?? -INF;
$max = $attribute['formatOptions']['max'] ?? INF; $max = $attribute['formatOptions']['max'] ?? INF;
return new Range($min, $max, Range::TYPE_INTEGER); return new Range($min, $max, Range::TYPE_INTEGER);
}, Database::VAR_INTEGER); }, Database::VAR_INTEGER);
Structure::addFormat(APP_DATABASE_ATTRIBUTE_FLOAT_RANGE, function ($attribute) { Structure::addFormat(APP_DATABASE_ATTRIBUTE_FLOAT_RANGE, function ($attribute) {
$min = $attribute['formatOptions']['min'] ?? -INF; $min = $attribute['formatOptions']['min'] ?? -INF;
$max = $attribute['formatOptions']['max'] ?? INF; $max = $attribute['formatOptions']['max'] ?? INF;
return new Range($min, $max, Range::TYPE_FLOAT); return new Range($min, $max, Range::TYPE_FLOAT);
}, Database::VAR_FLOAT); }, Database::VAR_FLOAT);
@ -733,28 +706,29 @@ $register->set('logger', function () {
return; return;
} }
if (!Logger::hasProvider($providerName)) { if (! Logger::hasProvider($providerName)) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, "Logging provider not supported. Logging is disabled"); throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Logging provider not supported. Logging is disabled');
} }
// Old Sentry Format conversion. Fallback until the old syntax is completely deprecated. // Old Sentry Format conversion. Fallback until the old syntax is completely deprecated.
if (str_contains($providerConfig, ';') && strtolower($providerName) == 'sentry') { if (str_contains($providerConfig, ';') && strtolower($providerName) == 'sentry') {
$configChunks = \explode(";", $providerConfig); $configChunks = \explode(';', $providerConfig);
$sentryKey = $configChunks[0]; $sentryKey = $configChunks[0];
$projectId = $configChunks[1]; $projectId = $configChunks[1];
$providerConfig = 'https://' . $sentryKey . '@sentry.io/' . $projectId; $providerConfig = 'https://'.$sentryKey.'@sentry.io/'.$projectId;
} }
$classname = '\\Utopia\\Logger\\Adapter\\' . \ucfirst($providerName); $classname = '\\Utopia\\Logger\\Adapter\\'.\ucfirst($providerName);
$adapter = new $classname($providerConfig); $adapter = new $classname($providerConfig);
return new Logger($adapter); return new Logger($adapter);
}); });
$register->set('pools', function () { $register->set('pools', function () {
$group = new Group(); $group = new Group();
$fallbackForDB = 'db_main=' . AppwriteURL::unparse([ $fallbackForDB = 'db_main='.AppwriteURL::unparse([
'scheme' => 'mariadb', 'scheme' => 'mariadb',
'host' => System::getEnv('_APP_DB_HOST', 'mariadb'), 'host' => System::getEnv('_APP_DB_HOST', 'mariadb'),
'port' => System::getEnv('_APP_DB_PORT', '3306'), 'port' => System::getEnv('_APP_DB_PORT', '3306'),
@ -762,7 +736,7 @@ $register->set('pools', function () {
'pass' => System::getEnv('_APP_DB_PASS', ''), 'pass' => System::getEnv('_APP_DB_PASS', ''),
'path' => System::getEnv('_APP_DB_SCHEMA', ''), 'path' => System::getEnv('_APP_DB_SCHEMA', ''),
]); ]);
$fallbackForRedis = 'redis_main=' . AppwriteURL::unparse([ $fallbackForRedis = 'redis_main='.AppwriteURL::unparse([
'scheme' => 'redis', 'scheme' => 'redis',
'host' => System::getEnv('_APP_REDIS_HOST', 'redis'), 'host' => System::getEnv('_APP_REDIS_HOST', 'redis'),
'port' => System::getEnv('_APP_REDIS_PORT', '6379'), 'port' => System::getEnv('_APP_REDIS_PORT', '6379'),
@ -818,7 +792,7 @@ $register->set('pools', function () {
throw new \Exception('Pool size is too small. Increase the number of allowed database connections or decrease the number of workers.', 500); throw new \Exception('Pool size is too small. Increase the number of allowed database connections or decrease the number of workers.', 500);
} }
$poolSize = (int)($instanceConnections / $workerCount); $poolSize = (int) ($instanceConnections / $workerCount);
foreach ($connections as $key => $connection) { foreach ($connections as $key => $connection) {
$type = $connection['type'] ?? ''; $type = $connection['type'] ?? '';
@ -828,7 +802,7 @@ $register->set('pools', function () {
$dsns = explode(',', $connection['dsns'] ?? ''); $dsns = explode(',', $connection['dsns'] ?? '');
foreach ($dsns as &$dsn) { foreach ($dsns as &$dsn) {
$dsn = explode('=', $dsn); $dsn = explode('=', $dsn);
$name = ($multiple) ? $key . '_' . $dsn[0] : $key; $name = ($multiple) ? $key.'_'.$dsn[0] : $key;
$dsn = $dsn[1] ?? ''; $dsn = $dsn[1] ?? '';
$config[] = $name; $config[] = $name;
if (empty($dsn)) { if (empty($dsn)) {
@ -844,8 +818,8 @@ $register->set('pools', function () {
$dsnScheme = $dsn->getScheme(); $dsnScheme = $dsn->getScheme();
$dsnDatabase = $dsn->getPath(); $dsnDatabase = $dsn->getPath();
if (!in_array($dsnScheme, $schemes)) { if (! in_array($dsnScheme, $schemes)) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, "Invalid console database scheme"); throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Invalid console database scheme');
} }
/** /**
@ -859,18 +833,18 @@ $register->set('pools', function () {
'mysql', 'mysql',
'mariadb' => function () use ($dsnHost, $dsnPort, $dsnUser, $dsnPass, $dsnDatabase) { 'mariadb' => function () use ($dsnHost, $dsnPort, $dsnUser, $dsnPass, $dsnDatabase) {
return new PDOProxy(function () use ($dsnHost, $dsnPort, $dsnUser, $dsnPass, $dsnDatabase) { return new PDOProxy(function () use ($dsnHost, $dsnPort, $dsnUser, $dsnPass, $dsnDatabase) {
return new PDO("mysql:host={$dsnHost};port={$dsnPort};dbname={$dsnDatabase};charset=utf8mb4", $dsnUser, $dsnPass, array( return new PDO("mysql:host={$dsnHost};port={$dsnPort};dbname={$dsnDatabase};charset=utf8mb4", $dsnUser, $dsnPass, [
PDO::ATTR_TIMEOUT => 3, // Seconds PDO::ATTR_TIMEOUT => 3, // Seconds
PDO::ATTR_PERSISTENT => true, PDO::ATTR_PERSISTENT => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_STRINGIFY_FETCHES => true PDO::ATTR_STRINGIFY_FETCHES => true,
)); ]);
}); });
}, },
'redis' => function () use ($dsnHost, $dsnPort, $dsnPass) { 'redis' => function () use ($dsnHost, $dsnPort, $dsnPass) {
$redis = new Redis(); $redis = new Redis();
@$redis->pconnect($dsnHost, (int)$dsnPort); @$redis->pconnect($dsnHost, (int) $dsnPort);
if ($dsnPass) { if ($dsnPass) {
$redis->auth($dsnPass); $redis->auth($dsnPass);
} }
@ -910,7 +884,7 @@ $register->set('pools', function () {
break; break;
default: default:
throw new Exception(Exception::GENERAL_SERVER_ERROR, "Server error: Missing adapter implementation."); throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Server error: Missing adapter implementation.');
} }
return $adapter; return $adapter;
@ -919,7 +893,7 @@ $register->set('pools', function () {
$group->add($pool); $group->add($pool);
} }
Config::setParam('pools-' . $key, $config); Config::setParam('pools-'.$key, $config);
} }
return $group; return $group;
@ -952,14 +926,14 @@ $register->set('smtp', function () {
$mail->XMailer = 'Appwrite Mailer'; $mail->XMailer = 'Appwrite Mailer';
$mail->Host = System::getEnv('_APP_SMTP_HOST', 'smtp'); $mail->Host = System::getEnv('_APP_SMTP_HOST', 'smtp');
$mail->Port = System::getEnv('_APP_SMTP_PORT', 25); $mail->Port = System::getEnv('_APP_SMTP_PORT', 25);
$mail->SMTPAuth = !empty($username) && !empty($password); $mail->SMTPAuth = ! empty($username) && ! empty($password);
$mail->Username = $username; $mail->Username = $username;
$mail->Password = $password; $mail->Password = $password;
$mail->SMTPSecure = System::getEnv('_APP_SMTP_SECURE', ''); $mail->SMTPSecure = System::getEnv('_APP_SMTP_SECURE', '');
$mail->SMTPAutoTLS = false; $mail->SMTPAutoTLS = false;
$mail->CharSet = 'UTF-8'; $mail->CharSet = 'UTF-8';
$from = \urldecode(System::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')); $from = \urldecode(System::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME.' Server'));
$email = System::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM); $email = System::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM);
$mail->setFrom($email, $from); $mail->setFrom($email, $from);
@ -970,12 +944,13 @@ $register->set('smtp', function () {
return $mail; return $mail;
}); });
$register->set('geodb', function () { $register->set('geodb', function () {
return new Reader(__DIR__ . '/assets/dbip/dbip-country-lite-2024-02.mmdb'); return new Reader(__DIR__.'/assets/dbip/dbip-country-lite-2024-02.mmdb');
}); });
$register->set('passwordsDictionary', function () { $register->set('passwordsDictionary', function () {
$content = \file_get_contents(__DIR__ . '/assets/security/10k-common-passwords'); $content = \file_get_contents(__DIR__.'/assets/security/10k-common-passwords');
$content = explode("\n", $content); $content = explode("\n", $content);
$content = array_flip($content); $content = array_flip($content);
return $content; return $content;
}); });
$register->set('promiseAdapter', function () { $register->set('promiseAdapter', function () {
@ -994,12 +969,12 @@ $locales = Config::getParam('locale-codes', []);
foreach ($locales as $locale) { foreach ($locales as $locale) {
$code = $locale['code']; $code = $locale['code'];
$path = __DIR__ . '/config/locale/translations/' . $code . '.json'; $path = __DIR__.'/config/locale/translations/'.$code.'.json';
if (!\file_exists($path)) { if (! \file_exists($path)) {
$path = __DIR__ . '/config/locale/translations/' . \substr($code, 0, 2) . '.json'; // if `ar-ae` doesn't exist, look for `ar` $path = __DIR__.'/config/locale/translations/'.\substr($code, 0, 2).'.json'; // if `ar-ae` doesn't exist, look for `ar`
if (!\file_exists($path)) { if (! \file_exists($path)) {
$path = __DIR__ . '/config/locale/translations/en.json'; // if none translation exists, use default from `en.json` $path = __DIR__.'/config/locale/translations/en.json'; // if none translation exists, use default from `en.json`
} }
} }
@ -1084,7 +1059,7 @@ App::setResource('clients', function ($request, $console, $project) {
$validator = new Hostname(); $validator = new Hostname();
foreach ($hostnames as $hostname) { foreach ($hostnames as $hostname) {
$hostname = trim($hostname); $hostname = trim($hostname);
if (!$validator->isValid($hostname)) { if (! $validator->isValid($hostname)) {
continue; continue;
} }
$console->setAttribute('platforms', [ $console->setAttribute('platforms', [
@ -1103,7 +1078,7 @@ App::setResource('clients', function ($request, $console, $project) {
fn ($node) => $node['hostname'], fn ($node) => $node['hostname'],
\array_filter( \array_filter(
$console->getAttribute('platforms', []), $console->getAttribute('platforms', []),
fn ($node) => (isset($node['type']) && ($node['type'] === Origin::CLIENT_TYPE_WEB) && !empty($node['hostname'])) fn ($node) => (isset($node['type']) && ($node['type'] === Origin::CLIENT_TYPE_WEB) && ! empty($node['hostname']))
) )
); );
@ -1115,7 +1090,7 @@ App::setResource('clients', function ($request, $console, $project) {
isset($node['type']) && isset($node['type']) &&
($node['type'] === Origin::CLIENT_TYPE_WEB || ($node['type'] === Origin::CLIENT_TYPE_WEB ||
$node['type'] === Origin::CLIENT_TYPE_FLUTTER_WEB) && $node['type'] === Origin::CLIENT_TYPE_FLUTTER_WEB) &&
!empty($node['hostname']) ! empty($node['hostname'])
) { ) {
$clients[] = $node['hostname']; $clients[] = $node['hostname'];
} }
@ -1131,19 +1106,18 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons
/** @var Utopia\Database\Database $dbForProject */ /** @var Utopia\Database\Database $dbForProject */
/** @var Utopia\Database\Database $dbForConsole */ /** @var Utopia\Database\Database $dbForConsole */
/** @var string $mode */ /** @var string $mode */
Authorization::setDefaultStatus(true); Authorization::setDefaultStatus(true);
Auth::setCookieName('a_session_' . $project->getId()); Auth::setCookieName('a_session_'.$project->getId());
if (APP_MODE_ADMIN === $mode) { if ($mode === APP_MODE_ADMIN) {
Auth::setCookieName('a_session_' . $console->getId()); Auth::setCookieName('a_session_'.$console->getId());
} }
$session = Auth::decodeSession( $session = Auth::decodeSession(
$request->getCookie( $request->getCookie(
Auth::$cookieName, // Get sessions Auth::$cookieName, // Get sessions
$request->getCookie(Auth::$cookieName . '_legacy', '') $request->getCookie(Auth::$cookieName.'_legacy', '')
) )
); );
@ -1151,7 +1125,7 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons
if (empty($session['id']) && empty($session['secret'])) { if (empty($session['id']) && empty($session['secret'])) {
$sessionHeader = $request->getHeader('x-appwrite-session', ''); $sessionHeader = $request->getHeader('x-appwrite-session', '');
if (!empty($sessionHeader)) { if (! empty($sessionHeader)) {
$session = Auth::decodeSession($sessionHeader); $session = Auth::decodeSession($sessionHeader);
} }
} }
@ -1173,7 +1147,7 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons
Auth::$unique = $session['id'] ?? ''; Auth::$unique = $session['id'] ?? '';
Auth::$secret = $session['secret'] ?? ''; Auth::$secret = $session['secret'] ?? '';
if (APP_MODE_ADMIN !== $mode) { if ($mode !== APP_MODE_ADMIN) {
if ($project->isEmpty()) { if ($project->isEmpty()) {
$user = new Document([]); $user = new Document([]);
} else { } else {
@ -1189,12 +1163,12 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons
if ( if (
$user->isEmpty() // Check a document has been found in the DB $user->isEmpty() // Check a document has been found in the DB
|| !Auth::sessionVerify($user->getAttribute('sessions', []), Auth::$secret) || ! Auth::sessionVerify($user->getAttribute('sessions', []), Auth::$secret)
) { // Validate user has valid login token ) { // Validate user has valid login token
$user = new Document([]); $user = new Document([]);
} }
if (APP_MODE_ADMIN === $mode) { if ($mode === APP_MODE_ADMIN) {
if ($user->find('teamInternalId', $project->getAttribute('teamInternalId'), 'memberships')) { if ($user->find('teamInternalId', $project->getAttribute('teamInternalId'), 'memberships')) {
Authorization::setDefaultStatus(false); // Cancel security segmentation for admin users. Authorization::setDefaultStatus(false); // Cancel security segmentation for admin users.
} else { } else {
@ -1204,13 +1178,13 @@ App::setResource('user', function ($mode, $project, $console, $request, $respons
$authJWT = $request->getHeader('x-appwrite-jwt', ''); $authJWT = $request->getHeader('x-appwrite-jwt', '');
if (!empty($authJWT) && !$project->isEmpty()) { // JWT authentication if (! empty($authJWT) && ! $project->isEmpty()) { // JWT authentication
$jwt = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway. $jwt = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway.
try { try {
$payload = $jwt->decode($authJWT); $payload = $jwt->decode($authJWT);
} catch (JWTException $error) { } catch (JWTException $error) {
throw new Exception(Exception::USER_JWT_INVALID, 'Failed to verify JWT. ' . $error->getMessage()); throw new Exception(Exception::USER_JWT_INVALID, 'Failed to verify JWT. '.$error->getMessage());
} }
$jwtUserId = $payload['userId'] ?? ''; $jwtUserId = $payload['userId'] ?? '';
@ -1235,7 +1209,6 @@ App::setResource('project', function ($dbForConsole, $request, $console) {
/** @var Appwrite\Utopia\Request $request */ /** @var Appwrite\Utopia\Request $request */
/** @var Utopia\Database\Database $dbForConsole */ /** @var Utopia\Database\Database $dbForConsole */
/** @var Utopia\Database\Document $console */ /** @var Utopia\Database\Document $console */
$projectId = $request->getParam('project', $request->getHeader('x-appwrite-project', '')); $projectId = $request->getParam('project', $request->getHeader('x-appwrite-project', ''));
if (empty($projectId) || $projectId === 'console') { if (empty($projectId) || $projectId === 'console') {
@ -1255,7 +1228,7 @@ App::setResource('session', function (Document $user) {
$sessions = $user->getAttribute('sessions', []); $sessions = $user->getAttribute('sessions', []);
$sessionId = Auth::sessionVerify($user->getAttribute('sessions'), Auth::$secret); $sessionId = Auth::sessionVerify($user->getAttribute('sessions'), Auth::$secret);
if (!$sessionId) { if (! $sessionId) {
return; return;
} }
@ -1265,8 +1238,7 @@ App::setResource('session', function (Document $user) {
} }
} }
return; }, ['user']);
}, ['user']);
App::setResource('console', function () { App::setResource('console', function () {
return new Document([ return new Document([
@ -1298,12 +1270,12 @@ App::setResource('console', function () {
'limit' => (System::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled') === 'enabled') ? 1 : 0, // limit signup to 1 user 'limit' => (System::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled') === 'enabled') ? 1 : 0, // limit signup to 1 user
'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG, // 1 Year in seconds 'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG, // 1 Year in seconds
], ],
'authWhitelistEmails' => (!empty(System::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null))) ? \explode(',', System::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null)) : [], 'authWhitelistEmails' => (! empty(System::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null))) ? \explode(',', System::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null)) : [],
'authWhitelistIPs' => (!empty(System::getEnv('_APP_CONSOLE_WHITELIST_IPS', null))) ? \explode(',', System::getEnv('_APP_CONSOLE_WHITELIST_IPS', null)) : [], 'authWhitelistIPs' => (! empty(System::getEnv('_APP_CONSOLE_WHITELIST_IPS', null))) ? \explode(',', System::getEnv('_APP_CONSOLE_WHITELIST_IPS', null)) : [],
'oAuthProviders' => [ 'oAuthProviders' => [
'githubEnabled' => true, 'githubEnabled' => true,
'githubSecret' => System::getEnv('_APP_CONSOLE_GITHUB_SECRET', ''), 'githubSecret' => System::getEnv('_APP_CONSOLE_GITHUB_SECRET', ''),
'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', '') 'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', ''),
], ],
]); ]);
}, []); }, []);
@ -1317,7 +1289,7 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForConsole,
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://'.$project->getAttribute('database'));
} }
$dbAdapter = $pools $dbAdapter = $pools
@ -1336,10 +1308,11 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForConsole,
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://'.$project->getAttribute('database'));
} }
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$database $database
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())
@ -1348,7 +1321,7 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForConsole,
$database $database
->setSharedTables(false) ->setSharedTables(false)
->setTenant(null) ->setTenant(null)
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_'.$project->getInternalId());
} }
return $database; return $database;
@ -1383,7 +1356,7 @@ App::setResource('getProjectDB', function (Group $pools, Database $dbForConsole,
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://'.$project->getAttribute('database'));
} }
$configure = (function (Database $database) use ($project, $dsn) { $configure = (function (Database $database) use ($project, $dsn) {
@ -1392,7 +1365,8 @@ App::setResource('getProjectDB', function (Group $pools, Database $dbForConsole,
->setMetadata('project', $project->getId()) ->setMetadata('project', $project->getId())
->setTimeout(APP_DATABASE_TIMEOUT_MILLISECONDS); ->setTimeout(APP_DATABASE_TIMEOUT_MILLISECONDS);
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$database $database
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())
@ -1401,13 +1375,14 @@ App::setResource('getProjectDB', function (Group $pools, Database $dbForConsole,
$database $database
->setSharedTables(false) ->setSharedTables(false)
->setTenant(null) ->setTenant(null)
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_'.$project->getInternalId());
} }
}); });
if (isset($databases[$dsn->getHost()])) { if (isset($databases[$dsn->getHost()])) {
$database = $databases[$dsn->getHost()]; $database = $databases[$dsn->getHost()];
$configure($database); $configure($database);
return $database; return $database;
} }
@ -1432,8 +1407,7 @@ App::setResource('cache', function (Group $pools) {
$adapters[] = $pools $adapters[] = $pools
->get($value) ->get($value)
->pop() ->pop()
->getResource() ->getResource();
;
} }
return new Cache(new Sharding($adapters)); return new Cache(new Sharding($adapters));
@ -1444,22 +1418,22 @@ App::setResource('deviceForLocal', function () {
}); });
App::setResource('deviceForFiles', function ($project) { App::setResource('deviceForFiles', function ($project) {
return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); return getDevice(APP_STORAGE_UPLOADS.'/app-'.$project->getId());
}, ['project']); }, ['project']);
App::setResource('deviceForFunctions', function ($project) { App::setResource('deviceForFunctions', function ($project) {
return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); return getDevice(APP_STORAGE_FUNCTIONS.'/app-'.$project->getId());
}, ['project']); }, ['project']);
App::setResource('deviceForBuilds', function ($project) { App::setResource('deviceForBuilds', function ($project) {
return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); return getDevice(APP_STORAGE_BUILDS.'/app-'.$project->getId());
}, ['project']); }, ['project']);
function getDevice($root): Device function getDevice($root): Device
{ {
$connection = System::getEnv('_APP_CONNECTIONS_STORAGE', ''); $connection = System::getEnv('_APP_CONNECTIONS_STORAGE', '');
if (!empty($connection)) { if (! empty($connection)) {
$acl = 'private'; $acl = 'private';
$device = Storage::DEVICE_LOCAL; $device = Storage::DEVICE_LOCAL;
$accessKey = ''; $accessKey = '';
@ -1475,7 +1449,7 @@ function getDevice($root): Device
$bucket = $dsn->getPath() ?? ''; $bucket = $dsn->getPath() ?? '';
$region = $dsn->getParam('region'); $region = $dsn->getParam('region');
} catch (\Throwable $e) { } catch (\Throwable $e) {
Console::warning($e->getMessage() . 'Invalid DSN. Defaulting to Local device.'); Console::warning($e->getMessage().'Invalid DSN. Defaulting to Local device.');
} }
switch ($device) { switch ($device) {
@ -1484,6 +1458,7 @@ function getDevice($root): Device
case STORAGE::DEVICE_DO_SPACES: case STORAGE::DEVICE_DO_SPACES:
$device = new DOSpaces($root, $accessKey, $accessSecret, $bucket, $region, $acl); $device = new DOSpaces($root, $accessKey, $accessSecret, $bucket, $region, $acl);
$device->setHttpVersion(S3::HTTP_VERSION_1_1); $device->setHttpVersion(S3::HTTP_VERSION_1_1);
return $device; return $device;
case Storage::DEVICE_BACKBLAZE: case Storage::DEVICE_BACKBLAZE:
return new Backblaze($root, $accessKey, $accessSecret, $bucket, $region, $acl); return new Backblaze($root, $accessKey, $accessSecret, $bucket, $region, $acl);
@ -1506,6 +1481,7 @@ function getDevice($root): Device
$s3Region = System::getEnv('_APP_STORAGE_S3_REGION', ''); $s3Region = System::getEnv('_APP_STORAGE_S3_REGION', '');
$s3Bucket = System::getEnv('_APP_STORAGE_S3_BUCKET', ''); $s3Bucket = System::getEnv('_APP_STORAGE_S3_BUCKET', '');
$s3Acl = 'private'; $s3Acl = 'private';
return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl); return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl);
case Storage::DEVICE_DO_SPACES: case Storage::DEVICE_DO_SPACES:
$doSpacesAccessKey = System::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', ''); $doSpacesAccessKey = System::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', '');
@ -1515,6 +1491,7 @@ function getDevice($root): Device
$doSpacesAcl = 'private'; $doSpacesAcl = 'private';
$device = new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl); $device = new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl);
$device->setHttpVersion(S3::HTTP_VERSION_1_1); $device->setHttpVersion(S3::HTTP_VERSION_1_1);
return $device; return $device;
case Storage::DEVICE_BACKBLAZE: case Storage::DEVICE_BACKBLAZE:
$backblazeAccessKey = System::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', ''); $backblazeAccessKey = System::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', '');
@ -1522,6 +1499,7 @@ function getDevice($root): Device
$backblazeRegion = System::getEnv('_APP_STORAGE_BACKBLAZE_REGION', ''); $backblazeRegion = System::getEnv('_APP_STORAGE_BACKBLAZE_REGION', '');
$backblazeBucket = System::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', ''); $backblazeBucket = System::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', '');
$backblazeAcl = 'private'; $backblazeAcl = 'private';
return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl); return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl);
case Storage::DEVICE_LINODE: case Storage::DEVICE_LINODE:
$linodeAccessKey = System::getEnv('_APP_STORAGE_LINODE_ACCESS_KEY', ''); $linodeAccessKey = System::getEnv('_APP_STORAGE_LINODE_ACCESS_KEY', '');
@ -1529,6 +1507,7 @@ function getDevice($root): Device
$linodeRegion = System::getEnv('_APP_STORAGE_LINODE_REGION', ''); $linodeRegion = System::getEnv('_APP_STORAGE_LINODE_REGION', '');
$linodeBucket = System::getEnv('_APP_STORAGE_LINODE_BUCKET', ''); $linodeBucket = System::getEnv('_APP_STORAGE_LINODE_BUCKET', '');
$linodeAcl = 'private'; $linodeAcl = 'private';
return new Linode($root, $linodeAccessKey, $linodeSecretKey, $linodeBucket, $linodeRegion, $linodeAcl); return new Linode($root, $linodeAccessKey, $linodeSecretKey, $linodeBucket, $linodeRegion, $linodeAcl);
case Storage::DEVICE_WASABI: case Storage::DEVICE_WASABI:
$wasabiAccessKey = System::getEnv('_APP_STORAGE_WASABI_ACCESS_KEY', ''); $wasabiAccessKey = System::getEnv('_APP_STORAGE_WASABI_ACCESS_KEY', '');
@ -1536,6 +1515,7 @@ function getDevice($root): Device
$wasabiRegion = System::getEnv('_APP_STORAGE_WASABI_REGION', ''); $wasabiRegion = System::getEnv('_APP_STORAGE_WASABI_REGION', '');
$wasabiBucket = System::getEnv('_APP_STORAGE_WASABI_BUCKET', ''); $wasabiBucket = System::getEnv('_APP_STORAGE_WASABI_BUCKET', '');
$wasabiAcl = 'private'; $wasabiAcl = 'private';
return new Wasabi($root, $wasabiAccessKey, $wasabiSecretKey, $wasabiBucket, $wasabiRegion, $wasabiAcl); return new Wasabi($root, $wasabiAccessKey, $wasabiSecretKey, $wasabiBucket, $wasabiRegion, $wasabiAcl);
} }
} }
@ -1562,7 +1542,6 @@ App::setResource('passwordsDictionary', function ($register) {
return $register->get('passwordsDictionary'); return $register->get('passwordsDictionary');
}, ['register']); }, ['register']);
App::setResource('servers', function () { App::setResource('servers', function () {
$platforms = Config::getParam('platforms'); $platforms = Config::getParam('platforms');
$server = $platforms[APP_PLATFORM_SERVER]; $server = $platforms[APP_PLATFORM_SERVER];
@ -1619,7 +1598,7 @@ App::setResource('schema', function ($utopia, $dbForProject) {
$params = [ $params = [
'list' => function (string $databaseId, string $collectionId, array $args) { 'list' => function (string $databaseId, string $collectionId, array $args) {
return [ 'queries' => $args['queries']]; return ['queries' => $args['queries']];
}, },
'create' => function (string $databaseId, string $collectionId, array $args) { 'create' => function (string $databaseId, string $collectionId, array $args) {
$id = $args['id'] ?? 'unique()'; $id = $args['id'] ?? 'unique()';
@ -1667,18 +1646,21 @@ App::setResource('schema', function ($utopia, $dbForProject) {
App::setResource('contributors', function () { App::setResource('contributors', function () {
$path = 'app/config/contributors.json'; $path = 'app/config/contributors.json';
$list = (file_exists($path)) ? json_decode(file_get_contents($path), true) : []; $list = (file_exists($path)) ? json_decode(file_get_contents($path), true) : [];
return $list; return $list;
}); });
App::setResource('employees', function () { App::setResource('employees', function () {
$path = 'app/config/employees.json'; $path = 'app/config/employees.json';
$list = (file_exists($path)) ? json_decode(file_get_contents($path), true) : []; $list = (file_exists($path)) ? json_decode(file_get_contents($path), true) : [];
return $list; return $list;
}); });
App::setResource('heroes', function () { App::setResource('heroes', function () {
$path = 'app/config/heroes.json'; $path = 'app/config/heroes.json';
$list = (file_exists($path)) ? json_decode(file_get_contents($path), true) : []; $list = (file_exists($path)) ? json_decode(file_get_contents($path), true) : [];
return $list; return $list;
}); });
@ -1690,13 +1672,14 @@ App::setResource('requestTimestamp', function ($request) {
//TODO: Move this to the Request class itself //TODO: Move this to the Request class itself
$timestampHeader = $request->getHeader('x-appwrite-timestamp'); $timestampHeader = $request->getHeader('x-appwrite-timestamp');
$requestTimestamp = null; $requestTimestamp = null;
if (!empty($timestampHeader)) { if (! empty($timestampHeader)) {
try { try {
$requestTimestamp = new \DateTime($timestampHeader); $requestTimestamp = new \DateTime($timestampHeader);
} catch (\Throwable $e) { } catch (\Throwable $e) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid X-Appwrite-Timestamp header value'); throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid X-Appwrite-Timestamp header value');
} }
} }
return $requestTimestamp; return $requestTimestamp;
}, ['request']); }, ['request']);
App::setResource('plan', function (array $plan = []) { App::setResource('plan', function (array $plan = []) {

View file

@ -1,6 +1,6 @@
<?php <?php
require_once __DIR__ . '/init.php'; require_once __DIR__.'/init.php';
use Appwrite\Event\Audit; use Appwrite\Event\Audit;
use Appwrite\Event\Build; use Appwrite\Event\Build;
@ -76,7 +76,7 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register,
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://'.$project->getAttribute('database'));
} }
$adapter = $pools $adapter = $pools
@ -90,10 +90,11 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register,
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://'.$project->getAttribute('database'));
} }
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$database $database
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())
@ -102,7 +103,7 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register,
$database $database
->setSharedTables(false) ->setSharedTables(false)
->setTenant(null) ->setTenant(null)
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_'.$project->getInternalId());
} }
return $database; return $database;
@ -120,13 +121,14 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://'.$project->getAttribute('database'));
} }
if (isset($databases[$dsn->getHost()])) { if (isset($databases[$dsn->getHost()])) {
$database = $databases[$dsn->getHost()]; $database = $databases[$dsn->getHost()];
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$database $database
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())
@ -135,7 +137,7 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso
$database $database
->setSharedTables(false) ->setSharedTables(false)
->setTenant(null) ->setTenant(null)
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_'.$project->getInternalId());
} }
return $database; return $database;
@ -150,7 +152,8 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso
$databases[$dsn->getHost()] = $database; $databases[$dsn->getHost()] = $database;
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$database $database
->setSharedTables(true) ->setSharedTables(true)
->setTenant($project->getInternalId()) ->setTenant($project->getInternalId())
@ -159,7 +162,7 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso
$database $database
->setSharedTables(false) ->setSharedTables(false)
->setTenant(null) ->setTenant(null)
->setNamespace('_' . $project->getInternalId()); ->setNamespace('_'.$project->getInternalId());
} }
return $database; return $database;
@ -187,8 +190,7 @@ Server::setResource('cache', function (Registry $register) {
$adapters[] = $pools $adapters[] = $pools
->get($value) ->get($value)
->pop() ->pop()
->getResource() ->getResource();
;
} }
return new Cache(new Sharding($adapters)); return new Cache(new Sharding($adapters));
@ -257,27 +259,26 @@ Server::setResource('pools', function (Registry $register) {
}, ['register']); }, ['register']);
Server::setResource('deviceForFunctions', function (Document $project) { Server::setResource('deviceForFunctions', function (Document $project) {
return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); return getDevice(APP_STORAGE_FUNCTIONS.'/app-'.$project->getId());
}, ['project']); }, ['project']);
Server::setResource('deviceForFiles', function (Document $project) { Server::setResource('deviceForFiles', function (Document $project) {
return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); return getDevice(APP_STORAGE_UPLOADS.'/app-'.$project->getId());
}, ['project']); }, ['project']);
Server::setResource('deviceForBuilds', function (Document $project) { Server::setResource('deviceForBuilds', function (Document $project) {
return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); return getDevice(APP_STORAGE_BUILDS.'/app-'.$project->getId());
}, ['project']); }, ['project']);
Server::setResource('deviceForCache', function (Document $project) { Server::setResource('deviceForCache', function (Document $project) {
return getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId()); return getDevice(APP_STORAGE_CACHE.'/app-'.$project->getId());
}, ['project']); }, ['project']);
$pools = $register->get('pools'); $pools = $register->get('pools');
$platform = new Appwrite(); $platform = new Appwrite();
$args = $platform->getEnv('argv'); $args = $platform->getEnv('argv');
if (!isset($args[1])) { if (! isset($args[1])) {
Console::error('Missing worker name'); Console::error('Missing worker name');
Console::exit(1); Console::exit(1);
} }
@ -288,7 +289,7 @@ $workerName = $args[0];
if (\str_starts_with($workerName, 'databases')) { if (\str_starts_with($workerName, 'databases')) {
$queueName = System::getEnv('_APP_QUEUE_NAME', 'database_db_main'); $queueName = System::getEnv('_APP_QUEUE_NAME', 'database_db_main');
} else { } else {
$queueName = System::getEnv('_APP_QUEUE_NAME', 'v1-' . strtolower($workerName)); $queueName = System::getEnv('_APP_QUEUE_NAME', 'v1-'.strtolower($workerName));
} }
try { try {
@ -302,10 +303,10 @@ try {
'workersNum' => System::getEnv('_APP_WORKERS_NUM', 1), 'workersNum' => System::getEnv('_APP_WORKERS_NUM', 1),
'connection' => $pools->get('queue')->pop()->getResource(), 'connection' => $pools->get('queue')->pop()->getResource(),
'workerName' => strtolower($workerName) ?? null, 'workerName' => strtolower($workerName) ?? null,
'queueName' => $queueName 'queueName' => $queueName,
]); ]);
} catch (\Throwable $e) { } catch (\Throwable $e) {
Console::error($e->getMessage() . ', File: ' . $e->getFile() . ', Line: ' . $e->getLine()); Console::error($e->getMessage().', File: '.$e->getFile().', Line: '.$e->getLine());
} }
$worker = $platform->getWorker(); $worker = $platform->getWorker();
@ -329,12 +330,12 @@ $worker
$version = System::getEnv('_APP_VERSION', 'UNKNOWN'); $version = System::getEnv('_APP_VERSION', 'UNKNOWN');
if ($logger) { if ($logger) {
$log->setNamespace("appwrite-worker"); $log->setNamespace('appwrite-worker');
$log->setServer(\gethostname()); $log->setServer(\gethostname());
$log->setVersion($version); $log->setVersion($version);
$log->setType(Log::TYPE_ERROR); $log->setType(Log::TYPE_ERROR);
$log->setMessage($error->getMessage()); $log->setMessage($error->getMessage());
$log->setAction('appwrite-queue-' . $queueName); $log->setAction('appwrite-queue-'.$queueName);
$log->addTag('verboseType', get_class($error)); $log->addTag('verboseType', get_class($error));
$log->addTag('code', $error->getCode()); $log->addTag('code', $error->getCode());
$log->addTag('projectId', $project->getId() ?? 'n/a'); $log->addTag('projectId', $project->getId() ?? 'n/a');
@ -347,13 +348,13 @@ $worker
$log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING); $log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING);
$responseCode = $logger->addLog($log); $responseCode = $logger->addLog($log);
Console::info('Usage stats log pushed with status code: ' . $responseCode); Console::info('Usage stats log pushed with status code: '.$responseCode);
} }
Console::error('[Error] Type: ' . get_class($error)); Console::error('[Error] Type: '.get_class($error));
Console::error('[Error] Message: ' . $error->getMessage()); Console::error('[Error] Message: '.$error->getMessage());
Console::error('[Error] File: ' . $error->getFile()); Console::error('[Error] File: '.$error->getFile());
Console::error('[Error] Line: ' . $error->getLine()); Console::error('[Error] Line: '.$error->getLine());
}); });
$worker->workerStart() $worker->workerStart()

View file

@ -69,13 +69,13 @@ class Deletes extends Action
throw new Exception('Missing payload'); throw new Exception('Missing payload');
} }
$type = $payload['type'] ?? ''; $type = $payload['type'] ?? '';
$datetime = $payload['datetime'] ?? null; $datetime = $payload['datetime'] ?? null;
$hourlyUsageRetentionDatetime = $payload['hourlyUsageRetentionDatetime'] ?? null; $hourlyUsageRetentionDatetime = $payload['hourlyUsageRetentionDatetime'] ?? null;
$resource = $payload['resource'] ?? null; $resource = $payload['resource'] ?? null;
$resourceType = $payload['resourceType'] ?? null; $resourceType = $payload['resourceType'] ?? null;
$document = new Document($payload['document'] ?? []); $document = new Document($payload['document'] ?? []);
$project = new Document($payload['project'] ?? []); $project = new Document($payload['project'] ?? []);
$log->addTag('projectId', $project->getId()); $log->addTag('projectId', $project->getId());
$log->addTag('type', $type); $log->addTag('type', $type);
@ -111,7 +111,7 @@ class Deletes extends Action
$this->deleteRule($dbForConsole, $document); $this->deleteRule($dbForConsole, $document);
break; break;
default: default:
Console::error('No lazy delete operation available for document of type: ' . $document->getCollection()); Console::error('No lazy delete operation available for document of type: '.$document->getCollection());
break; break;
} }
break; break;
@ -119,12 +119,12 @@ class Deletes extends Action
$this->deleteExecutionLogs($project, $getProjectDB, $executionRetention); $this->deleteExecutionLogs($project, $getProjectDB, $executionRetention);
break; break;
case DELETE_TYPE_AUDIT: case DELETE_TYPE_AUDIT:
if (!$project->isEmpty()) { if (! $project->isEmpty()) {
$this->deleteAuditLogs($project, $getProjectDB, $auditRetention); $this->deleteAuditLogs($project, $getProjectDB, $auditRetention);
} }
if (!$document->isEmpty()) { if (! $document->isEmpty()) {
$this->deleteAuditLogsByResource($getProjectDB, 'document/' . $document->getId(), $project); $this->deleteAuditLogsByResource($getProjectDB, 'document/'.$document->getId(), $project);
} }
break; break;
case DELETE_TYPE_ABUSE: case DELETE_TYPE_ABUSE:
@ -161,16 +161,13 @@ class Deletes extends Action
$this->deleteSessionTargets($project, $getProjectDB, $document); $this->deleteSessionTargets($project, $getProjectDB, $document);
break; break;
default: default:
throw new \Exception('No delete operation for type: ' . \strval($type)); throw new \Exception('No delete operation for type: '.\strval($type));
} }
} }
/** /**
* @param Database $dbForConsole * @param Document|null $document
* @param callable $getProjectDB *
* @param string $datetime
* @param Document|null $document
* @return void
* @throws Authorization * @throws Authorization
* @throws Conflict * @throws Conflict
* @throws Restricted * @throws Restricted
@ -192,7 +189,8 @@ class Deletes extends Action
if ($project->isEmpty()) { if ($project->isEmpty()) {
$dbForConsole->deleteDocument('schedules', $document->getId()); $dbForConsole->deleteDocument('schedules', $document->getId());
Console::success('Deleted schedule for deleted project ' . $document->getAttribute('projectId')); Console::success('Deleted schedule for deleted project '.$document->getAttribute('projectId'));
return; return;
} }
@ -216,38 +214,33 @@ class Deletes extends Action
if ($delete) { if ($delete) {
$dbForConsole->deleteDocument('schedules', $document->getId()); $dbForConsole->deleteDocument('schedules', $document->getId());
Console::success('Deleting schedule for ' . $document->getAttribute('resourceType') . ' ' . $document->getAttribute('resourceId')); Console::success('Deleting schedule for '.$document->getAttribute('resourceType').' '.$document->getAttribute('resourceId'));
} }
} }
); );
} }
/** /**
* @param Document $project
* @param callable $getProjectDB
* @param Document $topic
* @throws Exception * @throws Exception
*/ */
private function deleteTopic(Document $project, callable $getProjectDB, Document $topic) private function deleteTopic(Document $project, callable $getProjectDB, Document $topic)
{ {
if ($topic->isEmpty()) { if ($topic->isEmpty()) {
Console::error('Failed to delete subscribers. Topic not found'); Console::error('Failed to delete subscribers. Topic not found');
return; return;
} }
$this->deleteByGroup( $this->deleteByGroup(
'subscribers', 'subscribers',
[ [
Query::equal('topicInternalId', [$topic->getInternalId()]) Query::equal('topicInternalId', [$topic->getInternalId()]),
], ],
$getProjectDB($project) $getProjectDB($project)
); );
} }
/** /**
* @param Document $project
* @param callable $getProjectDB
* @param Document $target
* @throws Exception * @throws Exception
*/ */
private function deleteTargetSubscribers(Document $project, callable $getProjectDB, Document $target): void private function deleteTargetSubscribers(Document $project, callable $getProjectDB, Document $target): void
@ -259,14 +252,14 @@ class Deletes extends Action
$this->deleteByGroup( $this->deleteByGroup(
'subscribers', 'subscribers',
[ [
Query::equal('targetInternalId', [$target->getInternalId()]) Query::equal('targetInternalId', [$target->getInternalId()]),
], ],
$dbForProject, $dbForProject,
function (Document $subscriber) use ($dbForProject, $target) { function (Document $subscriber) use ($dbForProject, $target) {
$topicId = $subscriber->getAttribute('topicId'); $topicId = $subscriber->getAttribute('topicId');
$topicInternalId = $subscriber->getAttribute('topicInternalId'); $topicInternalId = $subscriber->getAttribute('topicInternalId');
$topic = $dbForProject->getDocument('topics', $topicId); $topic = $dbForProject->getDocument('topics', $topicId);
if (!$topic->isEmpty() && $topic->getInternalId() === $topicInternalId) { if (! $topic->isEmpty() && $topic->getInternalId() === $topicInternalId) {
$totalAttribute = match ($target->getAttribute('providerType')) { $totalAttribute = match ($target->getAttribute('providerType')) {
MESSAGE_TYPE_EMAIL => 'emailTotal', MESSAGE_TYPE_EMAIL => 'emailTotal',
MESSAGE_TYPE_SMS => 'smsTotal', MESSAGE_TYPE_SMS => 'smsTotal',
@ -285,10 +278,8 @@ class Deletes extends Action
} }
/** /**
* @param Document $project * @param Document $target
* @param callable $getProjectDB *
* @param Document $target
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteExpiredTargets(Document $project, callable $getProjectDB): void private function deleteExpiredTargets(Document $project, callable $getProjectDB): void
@ -296,7 +287,7 @@ class Deletes extends Action
$this->deleteByGroup( $this->deleteByGroup(
'targets', 'targets',
[ [
Query::equal('expired', [true]) Query::equal('expired', [true]),
], ],
$getProjectDB($project), $getProjectDB($project),
function (Document $target) use ($getProjectDB, $project) { function (Document $target) use ($getProjectDB, $project) {
@ -310,7 +301,7 @@ class Deletes extends Action
$this->deleteByGroup( $this->deleteByGroup(
'targets', 'targets',
[ [
Query::equal('sessionInternalId', [$session->getInternalId()]) Query::equal('sessionInternalId', [$session->getInternalId()]),
], ],
$getProjectDB($project), $getProjectDB($project),
function (Document $target) use ($getProjectDB, $project) { function (Document $target) use ($getProjectDB, $project) {
@ -320,25 +311,20 @@ class Deletes extends Action
} }
/** /**
* @param Document $project
* @param callable $getProjectDB
* @param string $resource
* @return void
* @throws Authorization * @throws Authorization
* @param string|null $resourceType
* @throws Exception * @throws Exception
*/ */
private function deleteCacheByResource(Document $project, callable $getProjectDB, string $resource, string $resourceType = null): void private function deleteCacheByResource(Document $project, callable $getProjectDB, string $resource, ?string $resourceType = null): void
{ {
$projectId = $project->getId(); $projectId = $project->getId();
$dbForProject = $getProjectDB($project); $dbForProject = $getProjectDB($project);
$cache = new Cache( $cache = new Cache(
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId) new Filesystem(APP_STORAGE_CACHE.DIRECTORY_SEPARATOR.'app-'.$projectId)
); );
$query[] = Query::equal('resource', [$resource]); $query[] = Query::equal('resource', [$resource]);
if (!empty($resourceType)) { if (! empty($resourceType)) {
$query[] = Query::equal('resourceType', [$resourceType]); $query[] = Query::equal('resourceType', [$resourceType]);
} }
@ -347,12 +333,12 @@ class Deletes extends Action
$query, $query,
$dbForProject, $dbForProject,
function (Document $document) use ($cache, $projectId) { function (Document $document) use ($cache, $projectId) {
$path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId(); $path = APP_STORAGE_CACHE.DIRECTORY_SEPARATOR.'app-'.$projectId.DIRECTORY_SEPARATOR.$document->getId();
if ($cache->purge($document->getId())) { if ($cache->purge($document->getId())) {
Console::success('Deleting cache file: ' . $path); Console::success('Deleting cache file: '.$path);
} else { } else {
Console::error('Failed to delete cache file: ' . $path); Console::error('Failed to delete cache file: '.$path);
} }
} }
); );
@ -360,10 +346,7 @@ class Deletes extends Action
/** /**
* Document $project * Document $project
* @param Document $project *
* @param callable $getProjectDB
* @param string $datetime
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteCacheByDate(Document $project, callable $getProjectDB, string $datetime): void private function deleteCacheByDate(Document $project, callable $getProjectDB, string $datetime): void
@ -372,7 +355,7 @@ class Deletes extends Action
$dbForProject = $getProjectDB($project); $dbForProject = $getProjectDB($project);
$cache = new Cache( $cache = new Cache(
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId) new Filesystem(APP_STORAGE_CACHE.DIRECTORY_SEPARATOR.'app-'.$projectId)
); );
$query = [ $query = [
@ -384,22 +367,20 @@ class Deletes extends Action
$query, $query,
$dbForProject, $dbForProject,
function (Document $document) use ($cache, $projectId) { function (Document $document) use ($cache, $projectId) {
$path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId(); $path = APP_STORAGE_CACHE.DIRECTORY_SEPARATOR.'app-'.$projectId.DIRECTORY_SEPARATOR.$document->getId();
if ($cache->purge($document->getId())) { if ($cache->purge($document->getId())) {
Console::success('Deleting cache file: ' . $path); Console::success('Deleting cache file: '.$path);
} else { } else {
Console::error('Failed to delete cache file: ' . $path); Console::error('Failed to delete cache file: '.$path);
} }
} }
); );
} }
/** /**
* @param Database $dbForConsole * @param Database $dbForConsole
* @param callable $getProjectDB *
* @param string $hourlyUsageRetentionDatetime
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteUsageStats(Document $project, callable $getProjectDB, string $hourlyUsageRetentionDatetime): void private function deleteUsageStats(Document $project, callable $getProjectDB, string $hourlyUsageRetentionDatetime): void
@ -413,10 +394,8 @@ class Deletes extends Action
} }
/** /**
* @param callable $getProjectDB * @param Document $document teams document
* @param Document $document teams document *
* @param Document $project
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteMemberships(callable $getProjectDB, Document $document, Document $project): void private function deleteMemberships(callable $getProjectDB, Document $document, Document $project): void
@ -428,7 +407,7 @@ class Deletes extends Action
$this->deleteByGroup( $this->deleteByGroup(
'memberships', 'memberships',
[ [
Query::equal('teamInternalId', [$teamInternalId]) Query::equal('teamInternalId', [$teamInternalId]),
], ],
$dbForProject, $dbForProject,
function (Document $membership) use ($dbForProject) { function (Document $membership) use ($dbForProject) {
@ -439,9 +418,6 @@ class Deletes extends Action
} }
/** /**
* @param Database $dbForConsole
* @param Document $document
* @return void
* @throws Authorization * @throws Authorization
* @throws DatabaseException * @throws DatabaseException
* @throws Conflict * @throws Conflict
@ -452,7 +428,7 @@ class Deletes extends Action
{ {
$projects = $dbForConsole->find('projects', [ $projects = $dbForConsole->find('projects', [
Query::equal('teamInternalId', [$document->getInternalId()]) Query::equal('teamInternalId', [$document->getInternalId()]),
]); ]);
foreach ($projects as $project) { foreach ($projects as $project) {
$this->deleteProject($dbForConsole, $getProjectDB, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $project); $this->deleteProject($dbForConsole, $getProjectDB, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $project);
@ -461,14 +437,6 @@ class Deletes extends Action
} }
/** /**
* @param Database $dbForConsole
* @param callable $getProjectDB
* @param Device $deviceForFiles
* @param Device $deviceForFunctions
* @param Device $deviceForBuilds
* @param Device $deviceForCache
* @param Document $document
* @return void
* @throws Exception * @throws Exception
* @throws Authorization * @throws Authorization
* @throws DatabaseException * @throws DatabaseException
@ -481,7 +449,7 @@ class Deletes extends Action
$dsn = new DSN($document->getAttribute('database', 'console')); $dsn = new DSN($document->getAttribute('database', 'console'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables // TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $document->getAttribute('database', 'console')); $dsn = new DSN('mysql://'.$document->getAttribute('database', 'console'));
} }
$dbForProject = $getProjectDB($document); $dbForProject = $getProjectDB($document);
@ -498,14 +466,15 @@ class Deletes extends Action
$collections = $dbForProject->listCollections($limit); $collections = $dbForProject->listCollections($limit);
foreach ($collections as $collection) { foreach ($collections as $collection) {
if ($dsn->getHost() !== System::getEnv('_APP_DATABASE_SHARED_TABLES', '') || !\in_array($collection->getId(), $projectCollectionIds)) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (! in_array($dsn->getHost(), $sharedTablesKeys) || ! \in_array($collection->getId(), $projectCollectionIds)) {
$dbForProject->deleteCollection($collection->getId()); $dbForProject->deleteCollection($collection->getId());
} else { } else {
$this->deleteByGroup($collection->getId(), [], database: $dbForProject); $this->deleteByGroup($collection->getId(), [], database: $dbForProject);
} }
} }
$sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { if (in_array($dsn->getHost(), $sharedTablesKeys)) {
$collectionsIds = \array_map(fn ($collection) => $collection->getId(), $collections); $collectionsIds = \array_map(fn ($collection) => $collection->getId(), $collections);
if (empty(\array_diff($collectionsIds, $projectCollectionIds))) { if (empty(\array_diff($collectionsIds, $projectCollectionIds))) {
@ -518,29 +487,29 @@ class Deletes extends Action
// Delete Platforms // Delete Platforms
$this->deleteByGroup('platforms', [ $this->deleteByGroup('platforms', [
Query::equal('projectInternalId', [$projectInternalId]) Query::equal('projectInternalId', [$projectInternalId]),
], $dbForConsole); ], $dbForConsole);
// Delete project and function rules // Delete project and function rules
$this->deleteByGroup('rules', [ $this->deleteByGroup('rules', [
Query::equal('projectInternalId', [$projectInternalId]) Query::equal('projectInternalId', [$projectInternalId]),
], $dbForConsole, function (Document $document) use ($dbForConsole) { ], $dbForConsole, function (Document $document) use ($dbForConsole) {
$this->deleteRule($dbForConsole, $document); $this->deleteRule($dbForConsole, $document);
}); });
// Delete Keys // Delete Keys
$this->deleteByGroup('keys', [ $this->deleteByGroup('keys', [
Query::equal('projectInternalId', [$projectInternalId]) Query::equal('projectInternalId', [$projectInternalId]),
], $dbForConsole); ], $dbForConsole);
// Delete Webhooks // Delete Webhooks
$this->deleteByGroup('webhooks', [ $this->deleteByGroup('webhooks', [
Query::equal('projectInternalId', [$projectInternalId]) Query::equal('projectInternalId', [$projectInternalId]),
], $dbForConsole); ], $dbForConsole);
// Delete VCS Installations // Delete VCS Installations
$this->deleteByGroup('installations', [ $this->deleteByGroup('installations', [
Query::equal('projectInternalId', [$projectInternalId]) Query::equal('projectInternalId', [$projectInternalId]),
], $dbForConsole); ], $dbForConsole);
// Delete VCS Repositories // Delete VCS Repositories
@ -554,7 +523,8 @@ class Deletes extends Action
], $dbForConsole); ], $dbForConsole);
// Delete metadata table // Delete metadata table
if ($dsn->getHost() !== System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));
if (! in_array($dsn, $sharedTablesKeys)) {
$dbForProject->deleteCollection('_metadata'); $dbForProject->deleteCollection('_metadata');
} else { } else {
$this->deleteByGroup('_metadata', [], $dbForProject); $this->deleteByGroup('_metadata', [], $dbForProject);
@ -568,10 +538,8 @@ class Deletes extends Action
} }
/** /**
* @param callable $getProjectDB * @param Document $document user document
* @param Document $document user document *
* @param Document $project
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteUser(callable $getProjectDB, Document $document, Document $project): void private function deleteUser(callable $getProjectDB, Document $document, Document $project): void
@ -582,19 +550,19 @@ class Deletes extends Action
// Delete all sessions of this user from the sessions table and update the sessions field of the user record // Delete all sessions of this user from the sessions table and update the sessions field of the user record
$this->deleteByGroup('sessions', [ $this->deleteByGroup('sessions', [
Query::equal('userInternalId', [$userInternalId]) Query::equal('userInternalId', [$userInternalId]),
], $dbForProject); ], $dbForProject);
$dbForProject->purgeCachedDocument('users', $userId); $dbForProject->purgeCachedDocument('users', $userId);
// Delete Memberships and decrement team membership counts // Delete Memberships and decrement team membership counts
$this->deleteByGroup('memberships', [ $this->deleteByGroup('memberships', [
Query::equal('userInternalId', [$userInternalId]) Query::equal('userInternalId', [$userInternalId]),
], $dbForProject, function (Document $document) use ($dbForProject) { ], $dbForProject, function (Document $document) use ($dbForProject) {
if ($document->getAttribute('confirm')) { // Count only confirmed members if ($document->getAttribute('confirm')) { // Count only confirmed members
$teamId = $document->getAttribute('teamId'); $teamId = $document->getAttribute('teamId');
$team = $dbForProject->getDocument('teams', $teamId); $team = $dbForProject->getDocument('teams', $teamId);
if (!$team->isEmpty()) { if (! $team->isEmpty()) {
$dbForProject->decreaseDocumentAttribute('teams', $teamId, 'total', 1, 0); $dbForProject->decreaseDocumentAttribute('teams', $teamId, 'total', 1, 0);
} }
} }
@ -602,19 +570,19 @@ class Deletes extends Action
// Delete tokens // Delete tokens
$this->deleteByGroup('tokens', [ $this->deleteByGroup('tokens', [
Query::equal('userInternalId', [$userInternalId]) Query::equal('userInternalId', [$userInternalId]),
], $dbForProject); ], $dbForProject);
// Delete identities // Delete identities
$this->deleteByGroup('identities', [ $this->deleteByGroup('identities', [
Query::equal('userInternalId', [$userInternalId]) Query::equal('userInternalId', [$userInternalId]),
], $dbForProject); ], $dbForProject);
// Delete targets // Delete targets
$this->deleteByGroup( $this->deleteByGroup(
'targets', 'targets',
[ [
Query::equal('userInternalId', [$userInternalId]) Query::equal('userInternalId', [$userInternalId]),
], ],
$dbForProject, $dbForProject,
function (Document $target) use ($getProjectDB, $project) { function (Document $target) use ($getProjectDB, $project) {
@ -624,10 +592,8 @@ class Deletes extends Action
} }
/** /**
* @param database $dbForConsole * @param database $dbForConsole
* @param callable $getProjectDB *
* @param string $datetime
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteExecutionLogs(Document $project, callable $getProjectDB, string $datetime): void private function deleteExecutionLogs(Document $project, callable $getProjectDB, string $datetime): void
@ -635,14 +601,13 @@ class Deletes extends Action
$dbForProject = $getProjectDB($project); $dbForProject = $getProjectDB($project);
// Delete Executions // Delete Executions
$this->deleteByGroup('executions', [ $this->deleteByGroup('executions', [
Query::lessThan('$createdAt', $datetime) Query::lessThan('$createdAt', $datetime),
], $dbForProject); ], $dbForProject);
} }
/** /**
* @param Database $dbForConsole * @param Database $dbForConsole
* @param callable $getProjectDB *
* @return void
* @throws Exception|Throwable * @throws Exception|Throwable
*/ */
private function deleteExpiredSessions(Document $project, callable $getProjectDB): void private function deleteExpiredSessions(Document $project, callable $getProjectDB): void
@ -653,50 +618,45 @@ class Deletes extends Action
// Delete Sessions // Delete Sessions
$this->deleteByGroup('sessions', [ $this->deleteByGroup('sessions', [
Query::lessThan('$createdAt', $expired) Query::lessThan('$createdAt', $expired),
], $dbForProject); ], $dbForProject);
} }
/** /**
* @param Database $dbForConsole
* @param string $datetime
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteRealtimeUsage(Database $dbForConsole, string $datetime): void private function deleteRealtimeUsage(Database $dbForConsole, string $datetime): void
{ {
// Delete Dead Realtime Logs // Delete Dead Realtime Logs
$this->deleteByGroup('realtime', [ $this->deleteByGroup('realtime', [
Query::lessThan('timestamp', $datetime) Query::lessThan('timestamp', $datetime),
], $dbForConsole); ], $dbForConsole);
} }
/** /**
* @param Database $dbForConsole * @param Database $dbForConsole
* @param callable $getProjectDB * @param string $datetime
* @param string $datetime *
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteAbuseLogs(Document $project, callable $getProjectDB, string $abuseRetention): void private function deleteAbuseLogs(Document $project, callable $getProjectDB, string $abuseRetention): void
{ {
$projectId = $project->getId(); $projectId = $project->getId();
$dbForProject = $getProjectDB($project); $dbForProject = $getProjectDB($project);
$timeLimit = new TimeLimit("", 0, 1, $dbForProject); $timeLimit = new TimeLimit('', 0, 1, $dbForProject);
$abuse = new Abuse($timeLimit); $abuse = new Abuse($timeLimit);
try { try {
$abuse->cleanup($abuseRetention); $abuse->cleanup($abuseRetention);
} catch (DatabaseException $e) { } catch (DatabaseException $e) {
Console::error('Failed to delete abuse logs for project ' . $projectId . ': ' . $e->getMessage()); Console::error('Failed to delete abuse logs for project '.$projectId.': '.$e->getMessage());
} }
} }
/** /**
* @param Database $dbForConsole * @param Database $dbForConsole
* @param callable $getProjectDB * @param string $datetime
* @param string $datetime *
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void private function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void
@ -708,15 +668,11 @@ class Deletes extends Action
try { try {
$audit->cleanup($auditRetention); $audit->cleanup($auditRetention);
} catch (DatabaseException $e) { } catch (DatabaseException $e) {
Console::error('Failed to delete audit logs for project ' . $projectId . ': ' . $e->getMessage()); Console::error('Failed to delete audit logs for project '.$projectId.': '.$e->getMessage());
} }
} }
/** /**
* @param callable $getProjectDB
* @param string $resource
* @param Document $project
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteAuditLogsByResource(callable $getProjectDB, string $resource, Document $project): void private function deleteAuditLogsByResource(callable $getProjectDB, string $resource, Document $project): void
@ -724,17 +680,13 @@ class Deletes extends Action
$dbForProject = $getProjectDB($project); $dbForProject = $getProjectDB($project);
$this->deleteByGroup(Audit::COLLECTION, [ $this->deleteByGroup(Audit::COLLECTION, [
Query::equal('resource', [$resource]) Query::equal('resource', [$resource]),
], $dbForProject); ], $dbForProject);
} }
/** /**
* @param callable $getProjectDB * @param Document $document function document
* @param Device $deviceForFunctions *
* @param Device $deviceForBuilds
* @param Document $document function document
* @param Document $project
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteFunction(Database $dbForConsole, callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void private function deleteFunction(Database $dbForConsole, callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
@ -747,32 +699,32 @@ class Deletes extends Action
/** /**
* Delete rules * Delete rules
*/ */
Console::info("Deleting rules for function " . $functionId); Console::info('Deleting rules for function '.$functionId);
$this->deleteByGroup('rules', [ $this->deleteByGroup('rules', [
Query::equal('resourceType', ['function']), Query::equal('resourceType', ['function']),
Query::equal('resourceInternalId', [$functionInternalId]), Query::equal('resourceInternalId', [$functionInternalId]),
Query::equal('projectInternalId', [$project->getInternalId()]) Query::equal('projectInternalId', [$project->getInternalId()]),
], $dbForConsole, function (Document $document) use ($project, $dbForConsole) { ], $dbForConsole, function (Document $document) use ($dbForConsole) {
$this->deleteRule($dbForConsole, $document); $this->deleteRule($dbForConsole, $document);
}); });
/** /**
* Delete Variables * Delete Variables
*/ */
Console::info("Deleting variables for function " . $functionId); Console::info('Deleting variables for function '.$functionId);
$this->deleteByGroup('variables', [ $this->deleteByGroup('variables', [
Query::equal('resourceType', ['function']), Query::equal('resourceType', ['function']),
Query::equal('resourceInternalId', [$functionInternalId]) Query::equal('resourceInternalId', [$functionInternalId]),
], $dbForProject); ], $dbForProject);
/** /**
* Delete Deployments * Delete Deployments
*/ */
Console::info("Deleting deployments for function " . $functionId); Console::info('Deleting deployments for function '.$functionId);
$deploymentInternalIds = []; $deploymentInternalIds = [];
$this->deleteByGroup('deployments', [ $this->deleteByGroup('deployments', [
Query::equal('resourceInternalId', [$functionInternalId]) Query::equal('resourceInternalId', [$functionInternalId]),
], $dbForProject, function (Document $document) use ($deviceForFunctions, &$deploymentInternalIds) { ], $dbForProject, function (Document $document) use ($deviceForFunctions, &$deploymentInternalIds) {
$deploymentInternalIds[] = $document->getInternalId(); $deploymentInternalIds[] = $document->getInternalId();
$this->deleteDeploymentFiles($deviceForFunctions, $document); $this->deleteDeploymentFiles($deviceForFunctions, $document);
@ -781,11 +733,11 @@ class Deletes extends Action
/** /**
* Delete builds * Delete builds
*/ */
Console::info("Deleting builds for function " . $functionId); Console::info('Deleting builds for function '.$functionId);
foreach ($deploymentInternalIds as $deploymentInternalId) { foreach ($deploymentInternalIds as $deploymentInternalId) {
$this->deleteByGroup('builds', [ $this->deleteByGroup('builds', [
Query::equal('deploymentInternalId', [$deploymentInternalId]) Query::equal('deploymentInternalId', [$deploymentInternalId]),
], $dbForProject, function (Document $document) use ($deviceForBuilds) { ], $dbForProject, function (Document $document) use ($deviceForBuilds) {
$this->deleteBuildFiles($deviceForBuilds, $document); $this->deleteBuildFiles($deviceForBuilds, $document);
}); });
@ -794,15 +746,15 @@ class Deletes extends Action
/** /**
* Delete Executions * Delete Executions
*/ */
Console::info("Deleting executions for function " . $functionId); Console::info('Deleting executions for function '.$functionId);
$this->deleteByGroup('executions', [ $this->deleteByGroup('executions', [
Query::equal('functionInternalId', [$functionInternalId]) Query::equal('functionInternalId', [$functionInternalId]),
], $dbForProject); ], $dbForProject);
/** /**
* Delete VCS Repositories and VCS Comments * Delete VCS Repositories and VCS Comments
*/ */
Console::info("Deleting VCS repositories and comments linked to function " . $functionId); Console::info('Deleting VCS repositories and comments linked to function '.$functionId);
$this->deleteByGroup('repositories', [ $this->deleteByGroup('repositories', [
Query::equal('projectInternalId', [$project->getInternalId()]), Query::equal('projectInternalId', [$project->getInternalId()]),
Query::equal('resourceInternalId', [$functionInternalId]), Query::equal('resourceInternalId', [$functionInternalId]),
@ -819,79 +771,65 @@ class Deletes extends Action
/** /**
* Request executor to delete all deployment containers * Request executor to delete all deployment containers
*/ */
Console::info("Requesting executor to delete all deployment containers for function " . $functionId); Console::info('Requesting executor to delete all deployment containers for function '.$functionId);
$this->deleteRuntimes($getProjectDB, $document, $project); $this->deleteRuntimes($getProjectDB, $document, $project);
} }
/**
* @param Device $device
* @param Document $deployment
* @return void
*/
private function deleteDeploymentFiles(Device $device, Document $deployment): void private function deleteDeploymentFiles(Device $device, Document $deployment): void
{ {
$deploymentId = $deployment->getId(); $deploymentId = $deployment->getId();
$deploymentPath = $deployment->getAttribute('path', ''); $deploymentPath = $deployment->getAttribute('path', '');
if (empty($deploymentPath)) { if (empty($deploymentPath)) {
Console::info("No deployment files for deployment " . $deploymentId); Console::info('No deployment files for deployment '.$deploymentId);
return; return;
} }
Console::info("Deleting deployment files for deployment " . $deploymentId); Console::info('Deleting deployment files for deployment '.$deploymentId);
try { try {
if ($device->delete($deploymentPath, true)) { if ($device->delete($deploymentPath, true)) {
Console::success('Deleted deployment files: ' . $deploymentPath); Console::success('Deleted deployment files: '.$deploymentPath);
} else { } else {
Console::error('Failed to delete deployment files: ' . $deploymentPath); Console::error('Failed to delete deployment files: '.$deploymentPath);
} }
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::error('Failed to delete deployment files: ' . $deploymentPath); Console::error('Failed to delete deployment files: '.$deploymentPath);
Console::error('[Error] Type: ' . get_class($th)); Console::error('[Error] Type: '.get_class($th));
Console::error('[Error] Message: ' . $th->getMessage()); Console::error('[Error] Message: '.$th->getMessage());
Console::error('[Error] File: ' . $th->getFile()); Console::error('[Error] File: '.$th->getFile());
Console::error('[Error] Line: ' . $th->getLine()); Console::error('[Error] Line: '.$th->getLine());
} }
} }
/**
* @param Device $device
* @param Document $build
* @return void
*/
private function deleteBuildFiles(Device $device, Document $build): void private function deleteBuildFiles(Device $device, Document $build): void
{ {
$buildId = $build->getId(); $buildId = $build->getId();
$buildPath = $build->getAttribute('path', ''); $buildPath = $build->getAttribute('path', '');
if (empty($buildPath)) { if (empty($buildPath)) {
Console::info("No build files for build " . $buildId); Console::info('No build files for build '.$buildId);
return; return;
} }
try { try {
if ($device->delete($buildPath, true)) { if ($device->delete($buildPath, true)) {
Console::success('Deleted build files: ' . $buildPath); Console::success('Deleted build files: '.$buildPath);
} else { } else {
Console::error('Failed to delete build files: ' . $buildPath); Console::error('Failed to delete build files: '.$buildPath);
} }
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::error('Failed to delete deployment files: ' . $buildPath); Console::error('Failed to delete deployment files: '.$buildPath);
Console::error('[Error] Type: ' . get_class($th)); Console::error('[Error] Type: '.get_class($th));
Console::error('[Error] Message: ' . $th->getMessage()); Console::error('[Error] Message: '.$th->getMessage());
Console::error('[Error] File: ' . $th->getFile()); Console::error('[Error] File: '.$th->getFile());
Console::error('[Error] Line: ' . $th->getLine()); Console::error('[Error] Line: '.$th->getLine());
} }
} }
/** /**
* @param callable $getProjectDB
* @param Device $deviceForFunctions
* @param Device $deviceForBuilds
* @param Document $document
* @param Document $project
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteDeployment(callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void private function deleteDeployment(callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
@ -909,10 +847,10 @@ class Deletes extends Action
/** /**
* Delete builds * Delete builds
*/ */
Console::info("Deleting builds for deployment " . $deploymentId); Console::info('Deleting builds for deployment '.$deploymentId);
$this->deleteByGroup('builds', [ $this->deleteByGroup('builds', [
Query::equal('deploymentInternalId', [$deploymentInternalId]) Query::equal('deploymentInternalId', [$deploymentInternalId]),
], $dbForProject, function (Document $document) use ($deviceForBuilds) { ], $dbForProject, function (Document $document) use ($deviceForBuilds) {
$this->deleteBuildFiles($deviceForBuilds, $document); $this->deleteBuildFiles($deviceForBuilds, $document);
}); });
@ -920,38 +858,34 @@ class Deletes extends Action
/** /**
* Request executor to delete all deployment containers * Request executor to delete all deployment containers
*/ */
Console::info("Requesting executor to delete deployment container for deployment " . $deploymentId); Console::info('Requesting executor to delete deployment container for deployment '.$deploymentId);
$this->deleteRuntimes($getProjectDB, $document, $project); $this->deleteRuntimes($getProjectDB, $document, $project);
} }
/** /**
* @param Document $document to be deleted * @param Document $document to be deleted
* @param Database $database to delete it from * @param Database $database to delete it from
* @param callable|null $callback to perform after document is deleted * @param callable|null $callback to perform after document is deleted
* @return void
*/ */
private function deleteById(Document $document, Database $database, callable $callback = null): void private function deleteById(Document $document, Database $database, ?callable $callback = null): void
{ {
if ($database->deleteDocument($document->getCollection(), $document->getId())) { if ($database->deleteDocument($document->getCollection(), $document->getId())) {
Console::success('Deleted document "' . $document->getId() . '" successfully'); Console::success('Deleted document "'.$document->getId().'" successfully');
if (is_callable($callback)) { if (is_callable($callback)) {
$callback($document); $callback($document);
} }
} else { } else {
Console::error('Failed to delete document: ' . $document->getId()); Console::error('Failed to delete document: '.$document->getId());
} }
} }
/** /**
* @param string $collection collectionID * @param string $collection collectionID
* @param array $queries *
* @param Database $database
* @param callable|null $callback
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteByGroup(string $collection, array $queries, Database $database, callable $callback = null): void private function deleteByGroup(string $collection, array $queries, Database $database, ?callable $callback = null): void
{ {
$count = 0; $count = 0;
$chunk = 0; $chunk = 0;
@ -966,13 +900,14 @@ class Deletes extends Action
try { try {
$results = $database->find($collection, [Query::limit($limit), ...$queries]); $results = $database->find($collection, [Query::limit($limit), ...$queries]);
} catch (DatabaseException $e) { } catch (DatabaseException $e) {
Console::error('Failed to find documents for collection ' . $collection . ': ' . $e->getMessage()); Console::error('Failed to find documents for collection '.$collection.': '.$e->getMessage());
return; return;
} }
$sum = count($results); $sum = count($results);
Console::info('Deleting chunk #' . $chunk . '. Found ' . $sum . ' documents'); Console::info('Deleting chunk #'.$chunk.'. Found '.$sum.' documents');
foreach ($results as $document) { foreach ($results as $document) {
$this->deleteById($document, $database, $callback); $this->deleteById($document, $database, $callback);
@ -982,18 +917,16 @@ class Deletes extends Action
$executionEnd = \microtime(true); $executionEnd = \microtime(true);
Console::info("Deleted {$count} document by group in " . ($executionEnd - $executionStart) . " seconds"); Console::info("Deleted {$count} document by group in ".($executionEnd - $executionStart).' seconds');
} }
/** /**
* @param string $collection collectionID * @param string $collection collectionID
* @param Query[] $queries * @param Query[] $queries
* @param Database $database *
* @param callable|null $callback
* @return void
* @throws Exception * @throws Exception
*/ */
private function listByGroup(string $collection, array $queries, Database $database, callable $callback = null): void private function listByGroup(string $collection, array $queries, Database $database, ?callable $callback = null): void
{ {
$count = 0; $count = 0;
$chunk = 0; $chunk = 0;
@ -1031,24 +964,22 @@ class Deletes extends Action
$executionEnd = \microtime(true); $executionEnd = \microtime(true);
Console::info("Listed {$count} document by group in " . ($executionEnd - $executionStart) . " seconds"); Console::info("Listed {$count} document by group in ".($executionEnd - $executionStart).' seconds');
} }
/** /**
* @param Database $dbForConsole * @param Document $document rule document
* @param Document $document rule document
* @return void
*/ */
private function deleteRule(Database $dbForConsole, Document $document): void private function deleteRule(Database $dbForConsole, Document $document): void
{ {
$domain = $document->getAttribute('domain'); $domain = $document->getAttribute('domain');
$directory = APP_STORAGE_CERTIFICATES . '/' . $domain; $directory = APP_STORAGE_CERTIFICATES.'/'.$domain;
$checkTraversal = realpath($directory) === $directory; $checkTraversal = realpath($directory) === $directory;
if ($checkTraversal && is_dir($directory)) { if ($checkTraversal && is_dir($directory)) {
// Delete files, so Traefik is aware of change // Delete files, so Traefik is aware of change
array_map('unlink', glob($directory . '/*.*')); array_map('unlink', glob($directory.'/*.*'));
rmdir($directory); rmdir($directory);
Console::info("Deleted certificate files for {$domain}"); Console::info("Deleted certificate files for {$domain}");
} else { } else {
@ -1061,28 +992,16 @@ class Deletes extends Action
} }
} }
/**
* @param callable $getProjectDB
* @param Device $deviceForFiles
* @param Document $document
* @param Document $project
* @return void
*/
private function deleteBucket(callable $getProjectDB, Device $deviceForFiles, Document $document, Document $project): void private function deleteBucket(callable $getProjectDB, Device $deviceForFiles, Document $document, Document $project): void
{ {
$dbForProject = $getProjectDB($project); $dbForProject = $getProjectDB($project);
$dbForProject->deleteCollection('bucket_' . $document->getInternalId()); $dbForProject->deleteCollection('bucket_'.$document->getInternalId());
$deviceForFiles->deletePath($document->getId()); $deviceForFiles->deletePath($document->getId());
} }
/** /**
* @param Database $dbForConsole
* @param callable $getProjectDB
* @param Document $document
* @param Document $project
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteInstallation(Database $dbForConsole, callable $getProjectDB, Document $document, Document $project): void private function deleteInstallation(Database $dbForConsole, callable $getProjectDB, Document $document, Document $project): void
@ -1090,7 +1009,7 @@ class Deletes extends Action
$dbForProject = $getProjectDB($project); $dbForProject = $getProjectDB($project);
$this->listByGroup('functions', [ $this->listByGroup('functions', [
Query::equal('installationInternalId', [$document->getInternalId()]) Query::equal('installationInternalId', [$document->getInternalId()]),
], $dbForProject, function ($function) use ($dbForProject, $dbForConsole) { ], $dbForProject, function ($function) use ($dbForProject, $dbForConsole) {
$dbForConsole->deleteDocument('repositories', $function->getAttribute('repositoryId')); $dbForConsole->deleteDocument('repositories', $function->getAttribute('repositoryId'));
@ -1108,10 +1027,8 @@ class Deletes extends Action
} }
/** /**
* @param callable $getProjectDB * @param ?Document $function
* @param ?Document $function *
* @param Document $project
* @return void
* @throws Exception * @throws Exception
*/ */
private function deleteRuntimes(callable $getProjectDB, ?Document $function, Document $project): void private function deleteRuntimes(callable $getProjectDB, ?Document $function, Document $project): void
@ -1134,10 +1051,10 @@ class Deletes extends Action
Console::info("Runtime for deployment {$deploymentId} deleted."); Console::info("Runtime for deployment {$deploymentId} deleted.");
} catch (Throwable $th) { } catch (Throwable $th) {
Console::warning("Runtime for deployment {$deploymentId} skipped:"); Console::warning("Runtime for deployment {$deploymentId} skipped:");
Console::error('[Error] Type: ' . get_class($th)); Console::error('[Error] Type: '.get_class($th));
Console::error('[Error] Message: ' . $th->getMessage()); Console::error('[Error] Message: '.$th->getMessage());
Console::error('[Error] File: ' . $th->getFile()); Console::error('[Error] File: '.$th->getFile());
Console::error('[Error] Line: ' . $th->getLine()); Console::error('[Error] Line: '.$th->getLine());
} }
} }
); );