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

344 lines
12 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
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;
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-06-30 23:09:28 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
$response->redirect('/auth/signin');
}, ['response']);
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-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
$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-06-30 23:09:28 +12:00
}, ['layout']);
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-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
$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-06-30 23:09:28 +12:00
}, ['layout']);
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-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
$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-06-30 23:09:28 +12:00
}, ['layout']);
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-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-06-30 23:09:28 +12:00
}, ['layout']);
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-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-06-30 23:09:28 +12:00
}, ['layout']);
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-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-06-30 23:09:28 +12:00
}, ['layout']);
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-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-06-30 23:09:28 +12:00
}, ['layout']);
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-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-06-30 23:09:28 +12:00
}, ['layout']);
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-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);
2020-06-30 23:09:28 +12:00
}, ['layout']);
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-11-12 11:02:42 +13:00
->action(function ($format, $platform, $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
];
2020-11-12 11:02:42 +13:00
$routes = [];
$models = [];
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
],
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
],
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',
],
'Locale' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Locale',
'description' => '',
'in' => 'header',
],
'Mode' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Mode',
'description' => '',
'in' => 'header',
],
],
];
2020-11-12 11:02:42 +13:00
$security = [
APP_PLATFORM_CLIENT => ['Project' => []],
APP_PLATFORM_SERVER => ['Project' => [], 'Key' => []],
APP_PLATFORM_CONSOLE => ['Project' => [], 'Key' => []],
2020-06-30 23:09:28 +12:00
];
foreach ($utopia->getRoutes() as $key => $method) {
2020-11-12 11:02:42 +13:00
foreach ($method as $route) { /** @var \Utopia\Route $route */
2020-06-30 23:09:28 +12:00
if (!$route->getLabel('docs', true)) {
continue;
2020-07-15 04:29:49 +12:00
}
if ($route->getLabel('sdk.mock', false)) {
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
2020-06-30 23:09:28 +12:00
if ($platform !== APP_PLATFORM_CONSOLE && !\in_array($platforms[$platform], $route->getLabel('sdk.platform', []))) {
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
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, $routes, $models, $keys[$platform], $security[$platform]);
break;
2020-06-30 23:09:28 +12:00
2020-11-12 11:02:42 +13:00
case 'open-api3':
$format = new OpenAPI3($utopia, $routes, $models, $keys[$platform], $security[$platform]);
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());
2020-07-01 06:08:02 +12:00
}, ['utopia', 'request', 'response']);