diff --git a/app/app.php b/app/app.php index 16f8421a7..8a91cd49a 100644 --- a/app/app.php +++ b/app/app.php @@ -3,12 +3,13 @@ // Init require_once __DIR__.'/init.php'; -global $env, $utopia, $request, $response, $register, $consoleDB, $project, $domain, $version, $service, $protocol, $domainVerification; +global $utopia, $request, $response, $register, $consoleDB, $project, $service; use Utopia\App; use Utopia\Request; use Utopia\View; use Utopia\Exception; +use Utopia\Config\Config; use Utopia\Domains\Domain; use Appwrite\Auth\Auth; use Appwrite\Database\Database; @@ -50,7 +51,7 @@ $clients = array_unique(array_merge($clientsConsole, array_map(function ($node) return false; })))); -$utopia->init(function () use ($utopia, $request, $response, &$user, $project, $roles, $webhook, $audit, $usage, $domain, $clients, &$domainVerification) { +$utopia->init(function () use ($utopia, $request, $response, &$user, $project, $roles, $webhook, $audit, $usage, $clients) { $route = $utopia->match($request); @@ -62,10 +63,10 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $ $refDomain = $protocol.'://'.((in_array($origin, $clients)) ? $origin : 'localhost') . (!empty($port) ? ':'.$port : ''); - $selfDomain = new Domain($domain); + $selfDomain = new Domain(Config::getParam('domain')); $endDomain = new Domain($origin); - $domainVerification = ($selfDomain->getRegisterable() === $endDomain->getRegisterable()); + Config::setParam('domainVerification', ($selfDomain->getRegisterable() === $endDomain->getRegisterable())); /* * Security Headers @@ -138,7 +139,7 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $ $user = new Document([ '$id' => 0, 'status' => Auth::USER_STATUS_ACTIVATED, - 'email' => 'app.'.$project->getId().'@service.'.$domain, + 'email' => 'app.'.$project->getId().'@service.'.Config::getParam('domain'), 'password' => '', 'name' => $project->getAttribute('name', 'Untitled'), ]); @@ -247,7 +248,10 @@ $utopia->options(function () use ($request, $response) { ->send(); }); -$utopia->error(function ($error /* @var $error Exception */) use ($request, $response, $utopia, $project, $env, $version) { +$utopia->error(function ($error /* @var $error Exception */) use ($request, $response, $utopia, $project) { + $env = Config::getParam('env'); + $version = Config::getParam('version'); + switch ($error->getCode()) { case 400: // Error allowed publicly case 401: // Error allowed publicly diff --git a/app/config/collections.php b/app/config/collections.php index 1314db689..4b23a1edd 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1,9 +1,12 @@ [ '$id' => 'console', @@ -1189,15 +1192,15 @@ $collections = [ /* * Add enabled OAuth2 providers to default data rules */ -foreach ($providers as $key => $provider) { +foreach ($providers as $index => $provider) { if (!$provider['enabled']) { continue; } $collections[Database::SYSTEM_COLLECTION_PROJECTS]['rules'][] = [ '$collection' => Database::SYSTEM_COLLECTION_RULES, - 'label' => 'OAuth2 '.ucfirst($key).' ID', - 'key' => 'usersOauth2'.ucfirst($key).'Appid', + 'label' => 'OAuth2 '.ucfirst($index).' ID', + 'key' => 'usersOauth2'.ucfirst($index).'Appid', 'type' => 'text', 'default' => '', 'required' => false, @@ -1206,8 +1209,8 @@ foreach ($providers as $key => $provider) { $collections[Database::SYSTEM_COLLECTION_PROJECTS]['rules'][] = [ '$collection' => Database::SYSTEM_COLLECTION_RULES, - 'label' => 'OAuth2 '.ucfirst($key).' Secret', - 'key' => 'usersOauth2'.ucfirst($key).'Secret', + 'label' => 'OAuth2 '.ucfirst($index).' Secret', + 'key' => 'usersOauth2'.ucfirst($index).'Secret', 'type' => 'text', 'default' => '', 'required' => false, @@ -1216,8 +1219,8 @@ foreach ($providers as $key => $provider) { $collections[Database::SYSTEM_COLLECTION_USERS]['rules'][] = [ '$collection' => Database::SYSTEM_COLLECTION_RULES, - 'label' => 'OAuth2 '.ucfirst($key).' ID', - 'key' => 'oauth2'.ucfirst($key), + 'label' => 'OAuth2 '.ucfirst($index).' ID', + 'key' => 'oauth2'.ucfirst($index), 'type' => 'text', 'default' => '', 'required' => false, @@ -1226,8 +1229,8 @@ foreach ($providers as $key => $provider) { $collections[Database::SYSTEM_COLLECTION_USERS]['rules'][] = [ '$collection' => Database::SYSTEM_COLLECTION_RULES, - 'label' => 'OAuth2 '.ucfirst($key).' Access Token', - 'key' => 'oauth2'.ucfirst($key).'AccessToken', + 'label' => 'OAuth2 '.ucfirst($index).' Access Token', + 'key' => 'oauth2'.ucfirst($index).'AccessToken', 'type' => 'text', 'default' => '', 'required' => false, diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 7c319d296..af59d8ebd 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1,10 +1,11 @@ init(function() use ($providers, &$oauth2Keys) { - foreach ($providers as $key => $provider) { +$utopia->init(function() use (&$oauth2Keys) { + foreach (Config::getParam('providers') as $key => $provider) { if (!$provider['enabled']) { continue; } @@ -155,7 +156,8 @@ $utopia->post('/v1/account/sessions') ->param('email', '', function () { return new Email(); }, 'User email.') ->param('password', '', function () { return new Password(); }, 'User password.') ->action( - function ($email, $password) use ($response, $request, $projectDB, $audit, $webhook, $protocol, $domainVerification) { + function ($email, $password) use ($response, $request, $projectDB, $audit, $webhook) { + $protocol = Config::getParam('protocol'); $profile = $projectDB->getCollection([ // Get user by email address 'limit' => 1, 'first' => true, @@ -216,7 +218,7 @@ $utopia->post('/v1/account/sessions') ->setParam('resource', 'users/'.$profile->getId()) ; - if(!$domainVerification) { + if(!Config::getParam('domainVerification')) { $response ->addHeader('X-Fallback-Cookies', json_encode([Auth::$cookieName => Auth::encodeSession($profile->getId(), $secret)])) ; @@ -244,11 +246,12 @@ $utopia->get('/v1/account/sessions/oauth2/:provider') ->label('sdk.location', true) ->label('abuse-limit', 50) ->label('abuse-key', 'ip:{ip}') - ->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth2 Provider. Currently, supported providers are: ' . implode(', ', array_keys(array_filter($providers, function($node) {return (!$node['mock']);}))).'.') + ->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'OAuth2 Provider. Currently, supported providers are: ' . implode(', ', array_keys(array_filter(Config::getParam('providers'), function($node) {return (!$node['mock']);}))).'.') ->param('success', '', function () use ($clients) { return new Host($clients); }, 'URL to redirect back to your app after a successful login attempt.') ->param('failure', '', function () use ($clients) { return new Host($clients); }, 'URL to redirect back to your app after a failed login attempt.') ->action( - function ($provider, $success, $failure) use ($response, $request, $project, $protocol) { + function ($provider, $success, $failure) use ($response, $request, $project) { + $protocol = Config::getParam('protocol'); $callback = $protocol.'://'.$request->getServer('HTTP_HOST').'/v1/account/sessions/oauth2/callback/'.$provider.'/'.$project->getId(); $appId = $project->getAttribute('usersOauth2'.ucfirst($provider).'Appid', ''); $appSecret = $project->getAttribute('usersOauth2'.ucfirst($provider).'Secret', '{}'); @@ -282,11 +285,13 @@ $utopia->get('/v1/account/sessions/oauth2/callback/:provider/:projectId') ->label('scope', 'public') ->label('docs', false) ->param('projectId', '', function () { return new Text(1024); }, 'Project unique ID.') - ->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth2 provider.') + ->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'OAuth2 provider.') ->param('code', '', function () { return new Text(1024); }, 'OAuth2 code.') ->param('state', '', function () { return new Text(2048); }, 'Login state params.', true) ->action( - function ($projectId, $provider, $code, $state) use ($response, $request, $domain, $protocol) { + function ($projectId, $provider, $code, $state) use ($response) { + $domain = Config::getParam('domain'); + $protocol = Config::getParam('protocol'); $response->redirect($protocol.'://'.$domain.'/v1/account/sessions/oauth2/'.$provider.'/redirect?' .http_build_query(['project' => $projectId, 'code' => $code, 'state' => $state])); } @@ -300,11 +305,12 @@ $utopia->get('/v1/account/sessions/oauth2/:provider/redirect') ->label('abuse-limit', 50) ->label('abuse-key', 'ip:{ip}') ->label('docs', false) - ->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth2 provider.') + ->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'OAuth2 provider.') ->param('code', '', function () { return new Text(1024); }, 'OAuth2 code.') ->param('state', '', function () { return new Text(2048); }, 'OAuth2 state params.', true) ->action( - function ($provider, $code, $state) use ($response, $request, $user, $projectDB, $project, $audit, $protocol, $domainVerification) { + function ($provider, $code, $state) use ($response, $request, $user, $projectDB, $project, $audit) { + $protocol = Config::getParam('protocol'); $callback = $protocol.'://'.$request->getServer('HTTP_HOST').'/v1/account/sessions/oauth2/callback/'.$provider.'/'.$project->getId(); $defaultState = ['success' => $project->getAttribute('url', ''), 'failure' => '']; $validateURL = new URL(); @@ -457,7 +463,7 @@ $utopia->get('/v1/account/sessions/oauth2/:provider/redirect') ->setParam('data', ['provider' => $provider]) ; - if(!$domainVerification) { + if(!Config::getParam('domainVerification')) { $response ->addHeader('X-Fallback-Cookies', json_encode([Auth::$cookieName => Auth::encodeSession($user->getId(), $secret)])) ; @@ -832,7 +838,8 @@ $utopia->delete('/v1/account') ->label('sdk.method', 'delete') ->label('sdk.description', '/docs/references/account/delete.md') ->action( - function () use ($response, $user, $projectDB, $audit, $webhook, $protocol, $domainVerification) { + function () use ($response, $user, $projectDB, $audit, $webhook) { + $protocol = Config::getParam('protocol'); $user = $projectDB->updateDocument(array_merge($user->getArrayCopy(), [ 'status' => Auth::USER_STATUS_BLOCKED, ])); @@ -863,7 +870,7 @@ $utopia->delete('/v1/account') ]) ; - if(!$domainVerification) { + if(!Config::getParam('domainVerification')) { $response ->addHeader('X-Fallback-Cookies', json_encode([])) ; @@ -888,7 +895,8 @@ $utopia->delete('/v1/account/sessions/:sessionId') ->label('abuse-limit', 100) ->param('sessionId', null, function () { return new UID(); }, 'Session unique ID. Use the string \'current\' to delete the current device session.') ->action( - function ($sessionId) use ($response, $user, $projectDB, $webhook, $audit, $protocol, $domainVerification) { + function ($sessionId) use ($response, $user, $projectDB, $webhook, $audit) { + $protocol = Config::getParam('protocol'); $sessionId = ($sessionId === 'current') ? Auth::tokenVerify($user->getAttribute('tokens'), Auth::TOKEN_TYPE_LOGIN, Auth::$secret) : $sessionId; @@ -914,7 +922,7 @@ $utopia->delete('/v1/account/sessions/:sessionId') ]) ; - if(!$domainVerification) { + if(!Config::getParam('domainVerification')) { $response ->addHeader('X-Fallback-Cookies', json_encode([])) ; @@ -945,7 +953,8 @@ $utopia->delete('/v1/account/sessions') ->label('sdk.description', '/docs/references/account/delete-sessions.md') ->label('abuse-limit', 100) ->action( - function () use ($response, $user, $projectDB, $audit, $webhook, $protocol, $domainVerification) { + function () use ($response, $user, $projectDB, $audit, $webhook) { + $protocol = Config::getParam('protocol'); $tokens = $user->getAttribute('tokens', []); foreach ($tokens as $token) { /* @var $token Document */ @@ -966,7 +975,7 @@ $utopia->delete('/v1/account/sessions') ]) ; - if(!$domainVerification) { + if(!Config::getParam('domainVerification')) { $response ->addHeader('X-Fallback-Cookies', json_encode([])) ; diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index fb00725c4..6f26f0eea 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -1,6 +1,6 @@ get('/v1/avatars/favicon') ->label('sdk.method', 'getFavicon') ->label('sdk.description', '/docs/references/avatars/get-favicon.md') ->action( - function ($url) use ($response, $request, $version) { + function ($url) use ($response, $request) { $width = 56; $height = 56; $quality = 80; @@ -238,7 +239,7 @@ $utopia->get('/v1/avatars/favicon') CURLOPT_MAXREDIRS => 3, CURLOPT_URL => $url, CURLOPT_USERAGENT => sprintf(APP_USERAGENT, - $version, + Config::getParam('version'), $request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY) ), ]); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 43dbf5877..fe1fcc386 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -1,6 +1,6 @@ get('/v1/projects') ->label('sdk.namespace', 'projects') ->label('sdk.method', 'list') ->action( - function () use ($request, $response, $providers, $consoleDB) { + function () use ($request, $response, $consoleDB) { $results = $consoleDB->getCollection([ 'limit' => 20, 'offset' => 0, @@ -105,7 +106,7 @@ $utopia->get('/v1/projects') ]); foreach ($results as $project) { - foreach ($providers as $provider => $node) { + foreach (Config::getParam('providers') as $provider => $node) { $secret = json_decode($project->getAttribute('usersOauth2'.ucfirst($provider).'Secret', '{}'), true); if (!empty($secret) && isset($secret['version'])) { @@ -126,14 +127,14 @@ $utopia->get('/v1/projects/:projectId') ->label('sdk.method', 'get') ->param('projectId', '', function () { return new UID(); }, 'Project unique ID.') ->action( - function ($projectId) use ($request, $response, $providers, $consoleDB) { + function ($projectId) use ($request, $response, $consoleDB) { $project = $consoleDB->getDocument($projectId); if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) { throw new Exception('Project not found', 404); } - foreach ($providers as $provider => $node) { + foreach (Config::getParam('providers') as $provider => $node) { $secret = json_decode($project->getAttribute('usersOauth2'.ucfirst($provider).'Secret', '{}'), true); if (!empty($secret) && isset($secret['version'])) { @@ -331,7 +332,7 @@ $utopia->patch('/v1/projects/:projectId/oauth2') ->label('sdk.namespace', 'projects') ->label('sdk.method', 'updateOAuth2') ->param('projectId', '', function () { return new UID(); }, 'Project unique ID.') - ->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'Provider Name', false) + ->param('provider', '', function () { return new WhiteList(array_keys(Config::getParam('providers'))); }, 'Provider Name', false) ->param('appId', '', function () { return new Text(256); }, 'Provider app ID.', true) ->param('secret', '', function () { return new text(256); }, 'Provider secret key.', true) ->action( @@ -1201,8 +1202,9 @@ $utopia->post('/v1/projects/:projectId/domains') ->param('projectId', null, function () { return new UID(); }, 'Project unique ID.') ->param('domain', null, function () { return new DomainValidator(); }, 'Domain name.') ->action( - function ($projectId, $domain) use ($request, $response, $consoleDB) { + function ($projectId) use ($request, $response, $consoleDB) { $project = $consoleDB->getDocument($projectId); + $domain = Config::getParam('domain'); if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) { throw new Exception('Project not found', 404); @@ -1283,7 +1285,7 @@ $utopia->get('/v1/projects/:projectId/domains/:domainId') ->param('projectId', null, function () { return new UID(); }, 'Project unique ID.') ->param('domainId', null, function () { return new UID(); }, 'Domain unique ID.') ->action( - function ($projectId, $domainId) use ($request, $response, $consoleDB) { + function ($projectId, $domainId) use ($response, $consoleDB) { $project = $consoleDB->getDocument($projectId); if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) { diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 2f0000beb..4a84dbf8d 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1,6 +1,6 @@ get('/v1/storage/files/:fileId/preview') //->param('storage', 'local', function () {return new WhiteList(array('local'));}, 'Selected storage device. defaults to local') //->param('token', '', function () {return new Text(128);}, 'Preview token', true) ->action( - function ($fileId, $width, $height, $quality, $background, $output) use ($request, $response, $projectDB, $project, $inputs, $outputs, $fileLogos, $version) { + function ($fileId, $width, $height, $quality, $background, $output) use ($request, $response, $projectDB, $project, $inputs, $outputs, $fileLogos) { $storage = 'local'; if (!extension_loaded('imagick')) { @@ -354,7 +354,7 @@ $utopia->get('/v1/storage/files/:fileId/preview') } $date = date('D, d M Y H:i:s', time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache - $key = md5($version.$fileId.$width.$height.$quality.$background.$storage.$output); + $key = md5($fileId.$width.$height.$quality.$background.$storage.$output); $file = $projectDB->getDocument($fileId); @@ -374,7 +374,7 @@ $utopia->get('/v1/storage/files/:fileId/preview') $cipher = null; $background = (empty($background)) ? 'eceff1' : $background; $type = strtolower(pathinfo($path, PATHINFO_EXTENSION)); - $key = md5($version.$path.$width.$height.$quality.$background.$storage.$output); + $key = md5($path.$width.$height.$quality.$background.$storage.$output); } $compressor = new GZIP(); diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index a410bcc9a..ccea69483 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -4,6 +4,7 @@ global $utopia, $register, $request, $response, $projectDB, $project, $user, $au use Utopia\Exception; use Utopia\Response; +use Utopia\Config\Config; use Utopia\Validator\Email; use Utopia\Validator\Text; use Utopia\Validator\Host; @@ -431,7 +432,8 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status') ->param('userId', '', function () { return new UID(); }, 'User unique ID.') ->param('secret', '', function () { return new Text(256); }, 'Secret key.') ->action( - function ($teamId, $inviteId, $userId, $secret) use ($response, $request, $user, $audit, $projectDB, $protocol, $domainVerification) { + function ($teamId, $inviteId, $userId, $secret) use ($response, $request, $user, $audit, $projectDB) { + $protocol = Config::getParam('protocol'); $membership = $projectDB->getDocument($inviteId); if (empty($membership->getId()) || Database::SYSTEM_COLLECTION_MEMBERSHIPS != $membership->getCollection()) { @@ -525,7 +527,7 @@ $utopia->patch('/v1/teams/:teamId/memberships/:inviteId/status') ->setParam('resource', 'teams/'.$teamId) ; - if(!$domainVerification) { + if(!Config::getParam('domainVerification')) { $response ->addHeader('X-Fallback-Cookies', json_encode([Auth::$cookieName => Auth::encodeSession($user->getId(), $secret)])) ; diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 7563264ec..e1153a7af 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1,6 +1,6 @@ post('/v1/users') ->param('password', '', function () { return new Password(); }, 'User password.') ->param('name', '', function () { return new Text(100); }, 'User name.', true) ->action( - function ($email, $password, $name) use ($response, $register, $projectDB, $providers) { + function ($email, $password, $name) use ($response, $projectDB) { $profile = $projectDB->getCollection([ // Get user by email address 'limit' => 1, 'first' => true, @@ -69,7 +70,7 @@ $utopia->post('/v1/users') $oauth2Keys = []; - foreach ($providers as $key => $provider) { + foreach (Config::getParam('providers') as $key => $provider) { if (!$provider['enabled']) { continue; } @@ -103,7 +104,7 @@ $utopia->get('/v1/users') ->param('offset', 0, function () { return new Range(0, 2000); }, 'Results offset. The default value is 0. Use this param to manage pagination.', true) ->param('orderType', 'ASC', function () { return new WhiteList(['ASC', 'DESC']); }, 'Order result by ASC or DESC order.', true) ->action( - function ($search, $limit, $offset, $orderType) use ($response, $projectDB, $providers) { + function ($search, $limit, $offset, $orderType) use ($response, $projectDB) { $results = $projectDB->getCollection([ 'limit' => $limit, 'offset' => $offset, @@ -118,7 +119,7 @@ $utopia->get('/v1/users') $oauth2Keys = []; - foreach ($providers as $key => $provider) { + foreach (Config::getParam('providers') as $key => $provider) { if (!$provider['enabled']) { continue; } @@ -154,7 +155,7 @@ $utopia->get('/v1/users/:userId') ->label('sdk.description', '/docs/references/users/get-user.md') ->param('userId', '', function () { return new UID(); }, 'User unique ID.') ->action( - function ($userId) use ($response, $projectDB, $providers) { + function ($userId) use ($response, $projectDB) { $user = $projectDB->getDocument($userId); if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { @@ -163,7 +164,7 @@ $utopia->get('/v1/users/:userId') $oauth2Keys = []; - foreach ($providers as $key => $provider) { + foreach (Config::getParam('providers') as $key => $provider) { if (!$provider['enabled']) { continue; } @@ -352,7 +353,7 @@ $utopia->patch('/v1/users/:userId/status') ->param('userId', '', function () { return new UID(); }, 'User unique ID.') ->param('status', '', function () { return new WhiteList([Auth::USER_STATUS_ACTIVATED, Auth::USER_STATUS_BLOCKED, Auth::USER_STATUS_UNACTIVATED]); }, 'User Status code. To activate the user pass '.Auth::USER_STATUS_ACTIVATED.', to block the user pass '.Auth::USER_STATUS_BLOCKED.' and for disabling the user pass '.Auth::USER_STATUS_UNACTIVATED) ->action( - function ($userId, $status) use ($response, $projectDB, $providers) { + function ($userId, $status) use ($response, $projectDB) { $user = $projectDB->getDocument($userId); if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { @@ -369,7 +370,7 @@ $utopia->patch('/v1/users/:userId/status') $oauth2Keys = []; - foreach ($providers as $key => $provider) { + foreach (Config::getParam('providers') as $key => $provider) { if (!$provider['enabled']) { continue; } @@ -400,7 +401,7 @@ $utopia->patch('/v1/users/:userId/prefs') ->param('userId', '', function () { return new UID(); }, 'User unique ID.') ->param('prefs', '', function () { return new Assoc();}, 'Prefs key-value JSON object.') ->action( - function ($userId, $prefs) use ($response, $projectDB, $providers) { + function ($userId, $prefs) use ($response, $projectDB) { $user = $projectDB->getDocument($userId); if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { diff --git a/app/controllers/shared/web.php b/app/controllers/shared/web.php index e2ef34104..9f148817a 100644 --- a/app/controllers/shared/web.php +++ b/app/controllers/shared/web.php @@ -1,10 +1,9 @@ getQuery('version', ''))) { $layout ->setParam('title', APP_NAME) - ->setParam('protocol', $protocol) + ->setParam('protocol', Config::getParam('protocol')) ->setParam('domain', $domain) ->setParam('home', $request->getServer('_APP_HOME')) ->setParam('setup', $request->getServer('_APP_SETUP')) @@ -32,9 +31,9 @@ $layout ->setParam('env', $utopia->getEnv()) ; -$utopia->shutdown(function () use ($utopia, $response, $request, $layout, $version, $env) { +$utopia->shutdown(function () use ($utopia, $response, $request, $layout) { $time = (60 * 60 * 24 * 45); // 45 days cache - $isDev = (\Utopia\App::ENV_TYPE_DEVELOPMENT == $env); + $isDev = (\Utopia\App::ENV_TYPE_DEVELOPMENT == Config::getParam('env')); $response ->addHeader('Cache-Control', 'public, max-age='.$time) @@ -44,7 +43,7 @@ $utopia->shutdown(function () use ($utopia, $response, $request, $layout, $versi $route = $utopia->match($request); $scope = $route->getLabel('scope', ''); $layout - ->setParam('version', $version) + ->setParam('version', Config::getParam('version')) ->setParam('isDev', $isDev) ->setParam('class', $scope) ; diff --git a/app/controllers/web/console.php b/app/controllers/web/console.php index 4a7f2c800..8aea50664 100644 --- a/app/controllers/web/console.php +++ b/app/controllers/web/console.php @@ -2,9 +2,10 @@ include_once __DIR__ . '/../shared/web.php'; -global $utopia, $response, $request, $layout, $version, $providers, $projectDB; +global $utopia, $response, $request, $layout, $projectDB; use Utopia\View; +use Utopia\Config\Config; use Utopia\Domains\Domain; use Appwrite\Database\Database; use Appwrite\Database\Validator\UID; @@ -16,25 +17,18 @@ $utopia->init(function () use ($layout, $utopia) { ; }); -$utopia->shutdown(function () use ($utopia, $response, $request, $layout, $version) { +$utopia->shutdown(function () use ($response, $request, $layout) { $header = new View(__DIR__.'/../../views/console/comps/header.phtml'); $footer = new View(__DIR__.'/../../views/console/comps/footer.phtml'); $footer ->setParam('home', $request->getServer('_APP_HOME', '')) - ->setParam('version', $version) + ->setParam('version', Config::getParam('version')) ; $layout ->setParam('header', [$header]) ->setParam('footer', [$footer]) - ->setParam('prefetch', [ - //'/console/database?version=' . $version, - //'/console/storage?version=' . $version, - //'/console/users?version=' . $version, - //'/console/settings?version=' . $version, - //'/console/account?version=' . $version, - ]) ; $response->send($layout->render()); @@ -228,10 +222,10 @@ $utopia->get('/console/users') ->desc('Platform console project settings') ->label('permission', 'public') ->label('scope', 'console') - ->action(function () use ($layout, $providers) { + ->action(function () use ($layout) { $page = new View(__DIR__.'/../../views/console/users/index.phtml'); - $page->setParam('providers', $providers); + $page->setParam('providers', Config::getParam('providers')); $layout ->setParam('title', APP_NAME.' - Users') @@ -242,7 +236,7 @@ $utopia->get('/console/users/view') ->desc('Platform console project user') ->label('permission', 'public') ->label('scope', 'console') - ->action(function () use ($layout, $providers) { + ->action(function () use ($layout) { $page = new View(__DIR__.'/../../views/console/users/view.phtml'); $layout diff --git a/app/controllers/web/home.php b/app/controllers/web/home.php index 1a758aac2..c53fff887 100644 --- a/app/controllers/web/home.php +++ b/app/controllers/web/home.php @@ -2,22 +2,23 @@ include_once __DIR__ . '/../shared/web.php'; -global $utopia, $response, $request, $layout, $version, $providers, $platforms; +global $utopia, $response, $request, $layout; use Utopia\View; +use Utopia\Config\Config; $header = new View(__DIR__.'/../../views/home/comps/header.phtml'); $footer = new View(__DIR__.'/../../views/home/comps/footer.phtml'); $footer - ->setParam('version', $version) + ->setParam('version', Config::getParam('version')) ; $layout ->setParam('title', APP_NAME) ->setParam('description', '') ->setParam('class', 'home') - ->setParam('platforms', $platforms) + ->setParam('platforms', Config::getParam('platforms')) ->setParam('header', [$header]) ->setParam('footer', [$footer]) ; diff --git a/app/init.php b/app/init.php index af7257394..18c57d88e 100644 --- a/app/init.php +++ b/app/init.php @@ -14,6 +14,7 @@ if (file_exists(__DIR__.'/../vendor/autoload.php')) { use Utopia\App; use Utopia\Request; use Utopia\Response; +use Utopia\Config\Config; use Utopia\Locale\Locale; use Utopia\Registry\Registry; use Appwrite\Auth\Auth; @@ -52,30 +53,31 @@ $response = new Response(); /* * ENV vars */ -$env = $request->getServer('_APP_ENV', App::ENV_TYPE_PRODUCTION); -$domain = $request->getServer('HTTP_HOST', ''); -$domainVerification = false; -$version = $request->getServer('_APP_VERSION', 'UNKNOWN'); -$providers = include __DIR__.'/../app/config/providers.php'; // OAuth2 providers list -$platforms = include __DIR__.'/../app/config/platforms.php'; -$locales = include __DIR__.'/../app/config/locales.php'; // Locales list -$collections = include __DIR__.'/../app/config/collections.php'; // Collections list -$redisHost = $request->getServer('_APP_REDIS_HOST', ''); -$redisPort = $request->getServer('_APP_REDIS_PORT', ''); -$utopia = new App('Asia/Tel_Aviv', $env); -$protocol = $request->getServer('HTTP_X_FORWARDED_PROTO', $request->getServer('REQUEST_SCHEME', 'https')); -$port = (string) parse_url($protocol.'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_PORT); +Config::load('providers', __DIR__.'/../app/config/providers.php'); +Config::load('platforms', __DIR__.'/../app/config/platforms.php'); +Config::load('locales', __DIR__.'/../app/config/locales.php'); +Config::load('collections', __DIR__.'/../app/config/collections.php'); -Resque::setBackend($redisHost.':'.$redisPort); +Config::setParam('env', $request->getServer('_APP_ENV', App::ENV_TYPE_PRODUCTION)); +Config::setParam('domain', $request->getServer('HTTP_HOST', '')); +Config::setParam('domainVerification', false); +Config::setParam('version', $request->getServer('_APP_VERSION', 'UNKNOWN')); +Config::setParam('protocol', $request->getServer('HTTP_X_FORWARDED_PROTO', $request->getServer('REQUEST_SCHEME', 'https'))); +Config::setParam('port', (string) parse_url(Config::getParam('protocol').'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_PORT)); + +$utopia = new App('Asia/Tel_Aviv', Config::getParam('env')); + +Resque::setBackend($request->getServer('_APP_REDIS_HOST', '') + .':'.$request->getServer('_APP_REDIS_PORT', '')); define('COOKIE_DOMAIN', ( $request->getServer('HTTP_HOST', null) === 'localhost' || - $request->getServer('HTTP_HOST', null) === 'localhost:'.$port || + $request->getServer('HTTP_HOST', null) === 'localhost:'.Config::getParam('port') || (filter_var($request->getServer('HTTP_HOST', null), FILTER_VALIDATE_IP) !== false) ) ? null - : '.'.parse_url($protocol.'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_HOST)); + : '.'.parse_url(Config::getParam('protocol').'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_HOST)); define('COOKIE_SAMESITE', Response::COOKIE_SAMESITE_NONE); /* @@ -119,10 +121,11 @@ $register->set('statsd', function () use ($request) { // Register DB connection return $statsd; }); -$register->set('cache', function () use ($redisHost, $redisPort) { // Register cache connection +$register->set('cache', function () use ($request) { // Register cache connection $redis = new Redis(); - $redis->connect($redisHost, $redisPort); + $redis->connect($request->getServer('_APP_REDIS_HOST', ''), + $request->getServer('_APP_REDIS_PORT', '')); return $redis; }); @@ -209,7 +212,7 @@ Locale::setLanguage('zh-tw', include __DIR__.'/config/locales/zh-tw.php'); Locale::setDefault('en'); -if (in_array($locale, $locales)) { +if (in_array($locale, Config::getParam('locales'))) { Locale::setDefault($locale); } @@ -217,7 +220,7 @@ stream_context_set_default([ // Set global user agent and http settings 'http' => [ 'method' => 'GET', 'user_agent' => sprintf(APP_USERAGENT, - $version, + Config::getParam('version'), $request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)), 'timeout' => 2, ], @@ -229,8 +232,8 @@ stream_context_set_default([ // Set global user agent and http settings $consoleDB = new Database(); $consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)); $consoleDB->setNamespace('app_console'); // Should be replaced with param if we want to have parent projects -$consoleDB->setMocks($collections); +$consoleDB->setMocks(Config::getParam('collections', [])); Authorization::disable(); $project = $consoleDB->getDocument($request->getParam('project', $request->getHeader('X-Appwrite-Project', null))); @@ -268,7 +271,7 @@ Auth::$secret = $session['secret']; $projectDB = new Database(); $projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)); $projectDB->setNamespace('app_'.$project->getId()); -$projectDB->setMocks($collections); +$projectDB->setMocks(Config::getParam('collections', [])); $user = $projectDB->getDocument(Auth::$unique); diff --git a/app/tasks/sdks.php b/app/tasks/sdks.php index c768bf48c..334e92a93 100644 --- a/app/tasks/sdks.php +++ b/app/tasks/sdks.php @@ -4,6 +4,7 @@ require_once __DIR__.'/../../vendor/autoload.php'; require_once __DIR__.'/../../app/init.php'; +use Utopia\Config\Config; use Utopia\CLI\CLI; use Utopia\CLI\Console; use Appwrite\Spec\Swagger2; @@ -39,7 +40,7 @@ $cli return $result; } - $platforms = include __DIR__ . '/../config/platforms.php'; + $platforms = Config::getParam('platforms'); $message = Console::confirm('Please enter your commit message:'); $production = (Console::confirm('Type "Appwrite" to deploy for production') == 'Appwrite'); diff --git a/app/tasks/upgrade.php b/app/tasks/upgrade.php index 80c3b96b9..c2a8723f9 100644 --- a/app/tasks/upgrade.php +++ b/app/tasks/upgrade.php @@ -3,8 +3,9 @@ require_once __DIR__.'/../init.php'; -global $register, $projectDB, $console, $providers, $request; +global $register, $projectDB, $console, $request; +use Utopia\Config\Config; use Utopia\CLI\CLI; use Utopia\CLI\Console; use Appwrite\Database\Database; @@ -93,7 +94,7 @@ $callbacks = [ ]; function fixDocument(Document $document) { - global $providers; + $providers = Config::getParam('providers'); if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_PROJECTS){ foreach($providers as $key => $provider) { diff --git a/app/workers/certificates.php b/app/workers/certificates.php index c087a59e3..73a59ca17 100644 --- a/app/workers/certificates.php +++ b/app/workers/certificates.php @@ -1,6 +1,7 @@ getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY) )); curl_setopt( diff --git a/app/workers/usage.php b/app/workers/usage.php index 18e7d5042..e5f673361 100644 --- a/app/workers/usage.php +++ b/app/workers/usage.php @@ -1,5 +1,7 @@ args['projectId']; $method = $this->args['method']; @@ -29,7 +31,7 @@ class UsageV1 $statsd = $register->get('statsd', true); - $tags = ",project={$projectId},version=".$version.''; + $tags = ",project={$projectId},version=".Config::getParam('version').''; // the global namespace is prepended to every key (optional) $statsd->setNamespace('appwrite.usage'); diff --git a/app/workers/webhooks.php b/app/workers/webhooks.php index 1eb0ec728..e7c6157a8 100644 --- a/app/workers/webhooks.php +++ b/app/workers/webhooks.php @@ -6,6 +6,7 @@ cli_set_process_title('Webhooks V1 Worker'); echo APP_NAME.' webhooks worker v1 has started'; +use Utopia\Config\Config; use Appwrite\Database\Database; use Appwrite\Database\Validator\Authorization; @@ -19,7 +20,7 @@ class WebhooksV1 public function perform() { - global $consoleDB, $version, $request; + global $consoleDB, $request; $errors = []; @@ -59,7 +60,7 @@ class WebhooksV1 curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, sprintf(APP_USERAGENT, - $version, + Config::getParam('version'), $request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY) )); curl_setopt( diff --git a/composer.json b/composer.json index 4adc670cf..5b158063e 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "utopia-php/audit": "0.2.*", "utopia-php/cache": "0.2.*", "utopia-php/cli": "0.4.*", + "utopia-php/config": "0.2.*", "utopia-php/locale": "0.2.*", "utopia-php/registry": "0.2.*", "utopia-php/domains": "0.2.*", diff --git a/composer.lock b/composer.lock index 313eb984a..62b26899d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "54701e5067d9b94ae765f3f0e560b531", + "content-hash": "d19ba7d1c39970839f6f191664e9498d", "packages": [ { "name": "appwrite/php-clamav", @@ -1381,6 +1381,52 @@ ], "time": "2020-03-25T04:46:43+00:00" }, + { + "name": "utopia-php/config", + "version": "0.2.0", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/config.git", + "reference": "f1f41d3863eb00bd2837b45c2e17e8b5da1cf46d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/config/zipball/f1f41d3863eb00bd2837b45c2e17e8b5da1cf46d", + "reference": "f1f41d3863eb00bd2837b45c2e17e8b5da1cf46d", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Config\\": "src/Config" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Eldad Fux", + "email": "eldad@appwrite.io" + } + ], + "description": "A simple Config library to managing application config variables", + "keywords": [ + "config", + "framework", + "php", + "upf", + "utopia" + ], + "time": "2020-03-28T10:24:18+00:00" + }, { "name": "utopia-php/domains", "version": "0.2.0", @@ -1576,7 +1622,7 @@ "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator", - "reference": "7a43413e650705843672ebe930fe6ecd77693393" + "reference": "97cb12c9c187a42a545060c324b0960c99582893" }, "require": { "ext-curl": "*", @@ -1606,7 +1652,7 @@ } ], "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", - "time": "2020-03-01T12:19:20+00:00" + "time": "2020-03-27T15:12:28+00:00" }, { "name": "doctrine/instantiator", @@ -2395,16 +2441,16 @@ }, { "name": "phpunit/phpunit", - "version": "7.5.20", + "version": "7.5.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "9467db479d1b0487c99733bb1e7944d32deded2c" + "reference": "6ef9ac810e494e7793013f8f5004d5d268852412" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9467db479d1b0487c99733bb1e7944d32deded2c", - "reference": "9467db479d1b0487c99733bb1e7944d32deded2c", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6ef9ac810e494e7793013f8f5004d5d268852412", + "reference": "6ef9ac810e494e7793013f8f5004d5d268852412", "shasum": "" }, "require": { @@ -2475,7 +2521,7 @@ "testing", "xunit" ], - "time": "2020-01-08T08:45:45+00:00" + "time": "2019-11-27T08:45:03+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -3206,12 +3252,12 @@ "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "27af7d9cd1b77fcb13b4ed452c291d9defad088b" + "reference": "6df2e2c3181e6d0b961fc341fbe8269fefc933c6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/27af7d9cd1b77fcb13b4ed452c291d9defad088b", - "reference": "27af7d9cd1b77fcb13b4ed452c291d9defad088b", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/6df2e2c3181e6d0b961fc341fbe8269fefc933c6", + "reference": "6df2e2c3181e6d0b961fc341fbe8269fefc933c6", "shasum": "" }, "require": { @@ -3263,7 +3309,7 @@ "keywords": [ "templating" ], - "time": "2020-03-24T12:35:30+00:00" + "time": "2020-03-27T16:41:01+00:00" }, { "name": "webmozart/assert", diff --git a/docker-compose.yml b/docker-compose.yml index 6bb22a303..14ffa887a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -148,18 +148,18 @@ services: # ports: # - "8081:8081" - # resque: - # image: registry.gitlab.com/appwrite/appwrite/resque-web:v1.0.2 - # restart: unless-stopped - # networks: - # - appwrite - # ports: - # - "5678:5678" - # environment: - # - RESQUE_WEB_HOST=redis - # - RESQUE_WEB_PORT=6379 - # - RESQUE_WEB_HTTP_BASIC_AUTH_USER=user - # - RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD=password + resque: + image: registry.gitlab.com/appwrite/appwrite/resque-web:v1.0.2 + restart: unless-stopped + networks: + - appwrite + ports: + - "5678:5678" + environment: + - RESQUE_WEB_HOST=redis + - RESQUE_WEB_PORT=6379 + - RESQUE_WEB_HTTP_BASIC_AUTH_USER=user + - RESQUE_WEB_HTTP_BASIC_AUTH_PASSWORD=password maildev: image: djfarrelly/maildev