1
0
Fork 0
mirror of synced 2024-05-20 04:32:37 +12:00
appwrite/app/controllers/web/home.php

499 lines
17 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
2021-02-24 00:29:12 +13:00
use Appwrite\Database\Database;
2020-11-12 11:02:42 +13:00
use Appwrite\Specification\Format\OpenAPI3;
use Appwrite\Specification\Format\Swagger2;
use Appwrite\Specification\Specification;
2020-06-29 05:31:21 +12:00
use Utopia\App;
2019-05-09 18:54:39 +12:00
use Utopia\View;
2020-03-29 01:42:16 +13:00
use Utopia\Config\Config;
2020-11-12 11:02:42 +13:00
use Utopia\Exception;
use Utopia\Validator\Boolean;
2020-12-23 19:09:16 +13:00
use Utopia\Validator\Range;
2020-05-17 17:27:10 +12:00
use Utopia\Validator\WhiteList;
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
App::init(function ($layout) {
2020-06-30 23:09:28 +12:00
/** @var Utopia\View $layout */
2020-06-25 09:16:03 +12:00
$header = new View(__DIR__.'/../../views/home/comps/header.phtml');
$footer = new View(__DIR__.'/../../views/home/comps/footer.phtml');
$footer
2020-06-30 23:09:28 +12:00
->setParam('version', App::getEnv('_APP_VERSION', 'UNKNOWN'))
2020-06-25 09:16:03 +12:00
;
$layout
->setParam('title', APP_NAME)
->setParam('description', '')
->setParam('class', 'home')
->setParam('platforms', Config::getParam('platforms'))
->setParam('header', [$header])
->setParam('footer', [$footer])
;
2020-06-30 09:43:34 +12:00
}, ['layout'], 'home');
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
App::shutdown(function ($response, $layout) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @var Utopia\View $layout */
2020-07-09 21:11:10 +12:00
$response->html($layout->render());
2020-06-30 09:43:34 +12:00
}, ['response', 'layout'], 'home');
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('response')
2021-05-13 17:59:00 +12:00
->inject('consoleDB')
2021-02-24 00:29:12 +13:00
->inject('project')
2021-05-13 17:59:00 +12:00
->action(function ($response, $consoleDB, $project) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-05-13 17:59:00 +12:00
/** @var Appwrite\Database\Database $consoleDB */
2021-02-24 00:29:12 +13:00
/** @var Appwrite\Database\Document $project */
$response
->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->addHeader('Expires', 0)
->addHeader('Pragma', 'no-cache')
;
2021-05-13 17:59:00 +12:00
if ('console' === $project->getId() || $project->isEmpty()) {
2021-05-13 02:53:25 +12:00
$whitlistRoot = App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled');
2021-02-24 00:29:12 +13:00
2021-05-13 02:53:25 +12:00
if($whitlistRoot !== 'disabled') {
2021-05-13 17:59:00 +12:00
$consoleDB->getCollection([ // Count users
2021-02-24 00:29:12 +13:00
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
],
]);
2021-02-24 02:03:43 +13:00
2021-05-13 17:59:00 +12:00
$sum = $consoleDB->getSum();
2021-02-24 00:29:12 +13:00
if($sum !== 0) {
return $response->redirect('/auth/signin');
}
}
}
$response->redirect('/auth/signup');
2020-12-27 06:02:50 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/signin')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2019-12-17 08:35:33 +13:00
$page = new View(__DIR__.'/../../views/home/auth/signin.phtml');
2019-05-09 18:54:39 +12:00
2021-02-24 00:29:12 +13:00
$page
2021-05-13 02:53:25 +12:00
->setParam('root', App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled'))
2021-02-24 00:29:12 +13:00
;
2019-05-09 18:54:39 +12:00
$layout
2019-09-27 06:47:48 +12:00
->setParam('title', 'Sign In - '.APP_NAME)
2019-05-09 18:54:39 +12:00
->setParam('body', $page);
2020-12-27 06:02:50 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/signup')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2019-12-17 08:35:33 +13:00
$page = new View(__DIR__.'/../../views/home/auth/signup.phtml');
2019-05-09 18:54:39 +12:00
2021-02-24 00:29:12 +13:00
$page
2021-05-13 02:53:25 +12:00
->setParam('root', App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled'))
2021-02-24 00:29:12 +13:00
;
2019-05-09 18:54:39 +12:00
$layout
2019-09-27 06:47:48 +12:00
->setParam('title', 'Sign Up - '.APP_NAME)
2019-05-09 18:54:39 +12:00
->setParam('body', $page);
2020-12-27 06:02:50 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/recovery')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2019-12-17 08:35:33 +13:00
$page = new View(__DIR__.'/../../views/home/auth/recovery.phtml');
2019-05-09 18:54:39 +12:00
2021-02-24 01:00:31 +13:00
$page
->setParam('smtpEnabled', (!empty(App::getEnv('_APP_SMTP_HOST'))))
;
2019-05-09 18:54:39 +12:00
$layout
2019-09-27 06:47:48 +12:00
->setParam('title', 'Password Recovery - '.APP_NAME)
2019-05-09 18:54:39 +12:00
->setParam('body', $page);
2020-12-27 06:02:50 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/confirm')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2019-12-17 08:35:33 +13:00
$page = new View(__DIR__.'/../../views/home/auth/confirm.phtml');
2019-05-09 18:54:39 +12:00
$layout
2019-09-27 06:47:48 +12:00
->setParam('title', 'Account Confirmation - '.APP_NAME)
2019-05-09 18:54:39 +12:00
->setParam('body', $page);
2020-12-27 06:02:50 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/join')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2019-12-17 08:35:33 +13:00
$page = new View(__DIR__.'/../../views/home/auth/join.phtml');
2019-05-09 18:54:39 +12:00
$layout
2019-09-27 06:47:48 +12:00
->setParam('title', 'Invitation - '.APP_NAME)
2019-05-09 18:54:39 +12:00
->setParam('body', $page);
2020-12-27 06:02:50 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/recovery/reset')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2019-12-17 08:35:33 +13:00
$page = new View(__DIR__.'/../../views/home/auth/recovery/reset.phtml');
2019-05-09 18:54:39 +12:00
$layout
2019-09-27 06:47:48 +12:00
->setParam('title', 'Password Reset - '.APP_NAME)
2019-05-09 18:54:39 +12:00
->setParam('body', $page);
2020-12-27 06:02:50 +13:00
});
2020-04-08 23:00:50 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/oauth2/success')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2020-04-08 23:00:50 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2020-04-09 01:14:52 +12:00
$page = new View(__DIR__.'/../../views/home/auth/oauth2.phtml');
2020-04-08 23:00:50 +12:00
$layout
->setParam('title', APP_NAME)
->setParam('body', $page)
->setParam('header', [])
->setParam('footer', [])
;
2020-12-27 06:02:50 +13:00
});
2020-04-08 23:00:50 +12:00
2020-06-29 05:31:21 +12:00
App::get('/auth/oauth2/failure')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2020-04-08 23:00:50 +12:00
->label('permission', 'public')
->label('scope', 'home')
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($layout) {
/** @var Utopia\View $layout */
2020-04-09 01:14:52 +12:00
$page = new View(__DIR__.'/../../views/home/auth/oauth2.phtml');
2020-04-08 23:00:50 +12:00
$layout
->setParam('title', APP_NAME)
->setParam('body', $page)
->setParam('header', [])
->setParam('footer', [])
;
2020-12-27 06:02:50 +13:00
});
2020-04-08 23:00:50 +12:00
2020-06-29 05:31:21 +12:00
App::get('/error/:code')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2019-05-09 18:54:39 +12:00
->label('permission', 'public')
->label('scope', 'home')
->param('code', null, new \Utopia\Validator\Numeric(), 'Valid status code number', false)
2020-12-27 06:02:50 +13:00
->inject('layout')
2020-06-30 23:09:28 +12:00
->action(function ($code, $layout) {
/** @var Utopia\View $layout */
2019-12-17 08:35:33 +13:00
$page = new View(__DIR__.'/../../views/error.phtml');
2019-05-09 18:54:39 +12:00
$page
->setParam('code', $code)
;
$layout
->setParam('title', 'Error'.' - '.APP_NAME)
2019-05-09 18:54:39 +12:00
->setParam('body', $page);
2021-01-16 19:22:35 +13:00
});
2020-05-17 17:27:10 +12:00
2020-11-12 11:02:42 +13:00
App::get('/specs/:format')
2020-06-26 06:32:12 +12:00
->groups(['web', 'home'])
2020-05-17 17:27:10 +12:00
->label('scope', 'public')
->label('docs', false)
2020-11-12 11:02:42 +13:00
->param('format', 'swagger2', new WhiteList(['swagger2', 'open-api3'], true), 'Spec format.', true)
2020-09-11 02:40:14 +12:00
->param('platform', APP_PLATFORM_CLIENT, new WhiteList([APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER, APP_PLATFORM_CONSOLE], true), 'Choose target platform.', true)
2020-12-23 19:09:16 +13:00
->param('tests', 0, function () {return new Range(0, 1);}, 'Include only test services.', true)
->param('allModels', false, new Boolean(true), 'Also include unused models.', true)
2020-12-27 06:02:50 +13:00
->inject('utopia')
->inject('request')
->inject('response')
->action(function ($format, $platform, $tests, $allModels, $utopia, $request, $response) {
2020-06-30 23:09:28 +12:00
/** @var Utopia\App $utopia */
/** @var Utopia\Swoole\Request $request */
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-05-17 17:27:10 +12:00
2020-06-30 23:09:28 +12:00
$platforms = [
'client' => APP_PLATFORM_CLIENT,
'server' => APP_PLATFORM_SERVER,
2020-11-12 11:02:42 +13:00
'console' => APP_PLATFORM_CONSOLE,
2020-06-30 23:09:28 +12:00
];
2021-05-20 02:26:06 +12:00
$authCounts = [
'client' => 1,
'server' => 2,
'console' => 1,
];
2020-11-12 11:02:42 +13:00
$routes = [];
$models = [];
$services = [];
2020-11-12 11:02:42 +13:00
2020-06-30 23:09:28 +12:00
$keys = [
APP_PLATFORM_CLIENT => [
'Project' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Project',
'description' => 'Your project ID',
'in' => 'header',
2020-05-17 17:27:10 +12:00
],
2021-04-16 19:59:41 +12:00
'JWT' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-JWT',
'description' => 'Your secret JSON Web Token',
'in' => 'header',
],
2020-06-30 23:09:28 +12:00
'Locale' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Locale',
'description' => '',
'in' => 'header',
2020-05-17 17:27:10 +12:00
],
2020-06-30 23:09:28 +12:00
],
APP_PLATFORM_SERVER => [
'Project' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Project',
'description' => 'Your project ID',
'in' => 'header',
2020-05-17 17:27:10 +12:00
],
2020-06-30 23:09:28 +12:00
'Key' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Key',
'description' => 'Your secret API key',
'in' => 'header',
2020-05-17 17:27:10 +12:00
],
2021-03-29 10:22:12 +13:00
'JWT' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-JWT',
'description' => 'Your secret JSON Web Token',
'in' => 'header',
],
2020-06-30 23:09:28 +12:00
'Locale' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Locale',
'description' => '',
'in' => 'header',
2020-05-17 17:27:10 +12:00
],
2020-06-30 23:09:28 +12:00
],
APP_PLATFORM_CONSOLE => [
'Project' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Project',
'description' => 'Your project ID',
'in' => 'header',
2020-05-17 17:27:10 +12:00
],
2020-06-30 23:09:28 +12:00
'Key' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Key',
'description' => 'Your secret API key',
'in' => 'header',
],
2021-03-29 10:22:12 +13:00
'JWT' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-JWT',
'description' => 'Your secret JSON Web Token',
'in' => 'header',
],
2020-06-30 23:09:28 +12:00
'Locale' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Locale',
'description' => '',
'in' => 'header',
],
'Mode' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Mode',
'description' => '',
'in' => 'header',
],
],
];
foreach ($utopia->getRoutes() as $key => $method) {
2020-11-12 11:02:42 +13:00
foreach ($method as $route) { /** @var \Utopia\Route $route */
2021-04-16 19:22:17 +12:00
$routeSecurity = $route->getLabel('sdk.auth', []);
2021-03-29 10:22:12 +13:00
$sdkPlatofrms = [];
foreach ($routeSecurity as $value) {
switch ($value) {
case APP_AUTH_TYPE_SESSION:
$sdkPlatofrms[] = APP_PLATFORM_CLIENT;
break;
case APP_AUTH_TYPE_KEY:
$sdkPlatofrms[] = APP_PLATFORM_SERVER;
break;
case APP_AUTH_TYPE_JWT:
$sdkPlatofrms[] = APP_PLATFORM_SERVER;
break;
case APP_AUTH_TYPE_ADMIN:
$sdkPlatofrms[] = APP_PLATFORM_CONSOLE;
break;
}
}
if(empty($routeSecurity)) {
$sdkPlatofrms[] = APP_PLATFORM_CLIENT;
}
2020-06-30 23:09:28 +12:00
if (!$route->getLabel('docs', true)) {
continue;
2020-07-15 04:29:49 +12:00
}
2020-12-23 19:09:16 +13:00
if ($route->getLabel('sdk.mock', false) && !$tests) {
continue;
}
if (!$route->getLabel('sdk.mock', false) && $tests) {
2020-07-15 04:29:49 +12:00
continue;
2020-06-30 23:09:28 +12:00
}
2020-05-17 17:27:10 +12:00
2020-06-30 23:09:28 +12:00
if (empty($route->getLabel('sdk.namespace', null))) {
continue;
}
2020-05-17 17:27:10 +12:00
2021-03-29 10:22:12 +13:00
if ($platform !== APP_PLATFORM_CONSOLE && !\in_array($platforms[$platform], $sdkPlatofrms)) {
2020-06-30 23:09:28 +12:00
continue;
}
2020-05-17 17:27:10 +12:00
2020-11-12 11:02:42 +13:00
$routes[] = $route;
$model = $response->getModel($route->getLabel('sdk.response.model', 'none'));
if($model) {
$models[$model->getType()] = $model;
2020-06-30 23:09:28 +12:00
}
2020-11-12 11:02:42 +13:00
}
}
2020-05-17 17:27:10 +12:00
foreach (Config::getParam('services', []) as $key => $service) {
if(!isset($service['docs']) // Skip service if not part of the public API
|| !isset($service['sdk'])
|| !$service['docs']
|| !$service['sdk']) {
continue;
}
$services[] = [
'name' => $service['key'] ?? '',
2021-02-11 04:04:50 +13:00
'description' => $service['subtitle'] ?? '',
];
}
2020-11-12 11:02:42 +13:00
$models = $response->getModels();
2020-06-30 23:09:28 +12:00
foreach ($models as $key => $value) {
if($platform !== APP_PLATFORM_CONSOLE && !$value->isPublic()) {
unset($models[$key]);
}
}
2020-11-12 11:02:42 +13:00
switch ($format) {
case 'swagger2':
$format = new Swagger2($utopia, $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0, $allModels);
2020-11-12 11:02:42 +13:00
break;
2020-06-30 23:09:28 +12:00
2020-11-12 11:02:42 +13:00
case 'open-api3':
$format = new OpenAPI3($utopia, $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0, $allModels);
2020-11-12 11:02:42 +13:00
break;
default:
throw new Exception('Format not found', 404);
break;
2020-06-30 23:09:28 +12:00
}
2020-05-17 17:27:10 +12:00
2020-11-12 11:02:42 +13:00
$specs = new Specification($format);
$format
->setParam('name', APP_NAME)
->setParam('description', 'Appwrite backend as a service cuts up to 70% of the time and costs required for building a modern application. We abstract and simplify common development tasks behind a REST APIs, to help you develop your app in a fast and secure way. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)')
->setParam('endpoint', App::getEnv('_APP_HOME', $request->getProtocol().'://'.$request->getHostname()).'/v1')
->setParam('version', APP_VERSION_STABLE)
->setParam('terms', App::getEnv('_APP_HOME', $request->getProtocol().'://'.$request->getHostname()).'/policy/terms')
->setParam('support.email', App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM))
->setParam('support.url', App::getEnv('_APP_HOME', $request->getProtocol().'://'.$request->getHostname()).'/support')
->setParam('contact.name', APP_NAME.' Team')
->setParam('contact.email', App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM))
->setParam('contact.url', App::getEnv('_APP_HOME', $request->getProtocol().'://'.$request->getHostname()).'/support')
->setParam('license.name', 'BSD-3-Clause')
->setParam('license.url', 'https://raw.githubusercontent.com/appwrite/appwrite/master/LICENSE')
->setParam('docs.description', 'Full API docs, specs and tutorials')
->setParam('docs.url', App::getEnv('_APP_HOME', $request->getProtocol().'://'.$request->getHostname()).'/docs')
;
2020-05-17 17:27:10 +12:00
2020-06-30 23:09:28 +12:00
$response
2020-11-12 11:02:42 +13:00
->json($specs->parse());
2021-02-25 07:31:43 +13:00
});
App::get('/versions')
->desc('Get Version')
->groups(['web', 'home'])
->label('scope', 'public')
->inject('response')
->action(function ($response) {
/** @var Appwrite\Utopia\Response $response */
$platforms = Config::getParam('platforms');
$versions = [
'server' => APP_VERSION_STABLE,
];
foreach($platforms as $platform) {
$languages = $platform['languages'] ?? [];
foreach ($languages as $key => $language) {
if(isset($language['dev']) && $language['dev']) {
continue;
}
if(isset($language['enabled']) && !$language['enabled']) {
continue;
}
$platformKey = $platform['key'] ?? '';
$languageKey = $language['key'] ?? '';
$version = $language['version'] ?? '';
$versions[$platformKey . '-' . $languageKey] = $version;
}
}
$response->json($versions);
2020-12-27 06:02:50 +13:00
});