Merge branch 'remove-globals' of github.com:appwrite/appwrite into preloader

This commit is contained in:
Eldad Fux
2020-07-01 01:20:13 +03:00
322 changed files with 5648 additions and 5372 deletions
+141 -115
View File
@@ -2,10 +2,9 @@
require_once __DIR__.'/init.php';
global $utopia, $request, $response, $register, $consoleDB, $project;
use Utopia\App;
use Utopia\Request;
use Utopia\Response;
use Utopia\View;
use Utopia\Exception;
use Utopia\Config\Config;
@@ -14,50 +13,81 @@ use Appwrite\Auth\Auth;
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Database\Validator\Authorization;
use Appwrite\Event\Event;
use Appwrite\Network\Validator\Origin;
/*
* Configuration files
*/
$webhook = new Event('v1-webhooks', 'WebhooksV1');
$audit = new Event('v1-audits', 'AuditsV1');
$usage = new Event('v1-usage', 'UsageV1');
$mail = new Event('v1-mails', 'MailsV1');
$deletes = new Event('v1-deletes', 'DeletesV1');
Config::setParam('domain', $_SERVER['HTTP_HOST']);
Config::setParam('domainVerification', false);
// Config::setParam('domain', $request->getServer('HTTP_HOST', ''));
// Config::setParam('domainVerification', false);
/**
* Get All verified client URLs for both console and current projects
* + Filter for duplicated entries
*/
$clientsConsole = \array_map(function ($node) {
return $node['hostname'];
}, \array_filter($console->getAttribute('platforms', []), function ($node) {
if (isset($node['type']) && $node['type'] === 'web' && isset($node['hostname']) && !empty($node['hostname'])) {
return true;
}
\define('COOKIE_DOMAIN',
(
$_SERVER['HTTP_HOST'] === 'localhost' ||
$_SERVER['HTTP_HOST'] === 'localhost:'.$request->getPort() ||
(\filter_var($request->getHostname(), FILTER_VALIDATE_IP) !== false)
)
? null
: '.'.$request->getHostname()
);
\define('COOKIE_SAMESITE', Response::COOKIE_SAMESITE_NONE);
return false;
}));
// \define('COOKIE_DOMAIN',
// (
// $request->getServer('HTTP_HOST', null) === 'localhost' ||
// $request->getServer('HTTP_HOST', null) === 'localhost:'.$request->getPort() ||
// (\filter_var($request->getHostname(), FILTER_VALIDATE_IP) !== false)
// )
// ? null
// : '.'.$request->getHostname()
// );
// \define('COOKIE_SAMESITE', Response::COOKIE_SAMESITE_NONE);
$clients = \array_unique(\array_merge($clientsConsole, \array_map(function ($node) {
return $node['hostname'];
}, \array_filter($project->getAttribute('platforms', []), function ($node) {
if (isset($node['type']) && $node['type'] === 'web' && isset($node['hostname']) && !empty($node['hostname'])) {
return true;
}
return false;
}))));
// // Set project mail
// $register->get('smtp')
// ->setFrom(
// App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM),
// ($project->getId() === 'console')
// ? \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME.' Server'))
// : \sprintf(Locale::getText('account.emails.team'), $project->getAttribute('name')
// )
// );
App::init(function ($utopia, $request, $response, $console, $project, $user, $locale, $webhooks, $audits, $usage, $clients) {
/** @var Utopia\Request $request */
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Document $console */
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Document $user */
/** @var Utopia\Locale\Locale $locale */
/** @var Appwrite\Event\Event $webhook */
/** @var Appwrite\Event\Event $audit */
/** @var Appwrite\Event\Event $usage */
/** @var Appwrite\Event\Event $mail */
/** @var Appwrite\Event\Event $deletes */
/** @var bool $mode */
/** @var array $clients */
$localeParam = (string)$request->getParam('locale', $request->getHeader('X-Appwrite-Locale', ''));
if (\in_array($localeParam, Config::getParam('locale-codes'))) {
$locale->setDefault($localeParam);
};
$utopia->init(function () use ($utopia, $request, $response, &$user, $project, $console, $webhook, $mail, $audit, $usage, $clients) {
$route = $utopia->match($request);
if(!empty($route->getLabel('sdk.platform', [])) && empty($project->getId()) && ($route->getLabel('scope', '') !== 'public')) {
throw new Exception('Missing or unknown project ID', 400);
}
$console->setAttribute('platforms', [ // Allways allow current host
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Current Host',
'type' => 'web',
'hostname' => \parse_url('https://'.$request->getServer('HTTP_HOST'), PHP_URL_HOST),
], Document::SET_TYPE_APPEND);
$referrer = $request->getServer('HTTP_REFERER', '');
$origin = \parse_url($request->getServer('HTTP_ORIGIN', $referrer), PHP_URL_HOST);
$protocol = \parse_url($request->getServer('HTTP_ORIGIN', $referrer), PHP_URL_SCHEME);
@@ -79,8 +109,8 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $
* As recommended at:
* @see https://www.owasp.org/index.php/List_of_useful_HTTP_headers
*/
if ($utopia->getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'enabled') { // Force HTTPS
if(Config::getParam('protocol') !== 'https') {
if (App::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'enabled') { // Force HTTPS
if($request->getProtocol() !== 'https') {
return $response->redirect('https://' . Config::getParam('domain').$request->getServer('REQUEST_URI'));
}
@@ -198,13 +228,13 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $
/*
* Background Jobs
*/
$webhook
$webhooks
->setParam('projectId', $project->getId())
->setParam('event', $route->getLabel('webhook', ''))
->setParam('payload', [])
;
$audit
$audits
->setParam('projectId', $project->getId())
->setParam('userId', $user->getId())
->setParam('event', '')
@@ -222,10 +252,9 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $
->setParam('response', 0)
->setParam('storage', 0)
;
});
$utopia->shutdown(function () use ($response, $request, $webhook, $audit, $usage, $deletes, $mode, $project, $utopia) {
}, ['utopia', 'request', 'response', 'console', 'project', 'user', 'locale', 'webhook', 'audit', 'usage', 'clients']);
App::shutdown(function ($utopia, $response, $request, $webhook, $audit, $usage, $deletes, $mode, $project) {
/*
* Trigger events for background workers
*/
@@ -252,9 +281,9 @@ $utopia->shutdown(function () use ($response, $request, $webhook, $audit, $usage
->trigger()
;
}
});
}, ['utopia', 'response', 'request', 'webhook', 'audit', 'usage', 'deletes', 'mode', 'project']);
$utopia->options(function () use ($request, $response) {
App::options(function ($request, $response) {
$origin = $request->getServer('HTTP_ORIGIN');
$response
@@ -264,11 +293,12 @@ $utopia->options(function () use ($request, $response) {
->addHeader('Access-Control-Allow-Origin', $origin)
->addHeader('Access-Control-Allow-Credentials', 'true')
->send();
});
}, ['request', 'response']);
$utopia->error(function ($error /* @var $error Exception */) use ($request, $response, $utopia, $project) {
$env = Config::getParam('env');
$version = Config::getParam('version');
App::error(function ($error, $utopia, $request, $response, $project) {
/** @var Exception $error */
$version = App::getEnv('_APP_VERSION', 'UNKNOWN');
switch ($error->getCode()) {
case 400: // Error allowed publicly
@@ -289,7 +319,7 @@ $utopia->error(function ($error /* @var $error Exception */) use ($request, $res
$_SERVER = []; // Reset before reporting to error log to avoid keys being compromised
$output = ((App::MODE_TYPE_DEVELOPMENT == $env)) ? [
$output = ((App::isDevelopment())) ? [
'message' => $error->getMessage(),
'code' => $error->getCode(),
'file' => $error->getFile(),
@@ -337,91 +367,85 @@ $utopia->error(function ($error /* @var $error Exception */) use ($request, $res
$response
->json($output)
;
});
}, ['error', 'utopia', 'request', 'response', 'project']);
$utopia->get('/manifest.json')
App::get('/manifest.json')
->desc('Progressive app manifest file')
->label('scope', 'public')
->label('docs', false)
->action(
function () use ($response) {
$response->json([
'name' => APP_NAME,
'short_name' => APP_NAME,
'start_url' => '.',
'url' => 'https://appwrite.io/',
'display' => 'standalone',
'background_color' => '#fff',
'theme_color' => '#f02e65',
'description' => 'End to end backend server for frontend and mobile apps. 👩‍💻👨‍💻',
'icons' => [
[
'src' => 'images/favicon.png',
'sizes' => '256x256',
'type' => 'image/png',
],
],
]);
}
);
->action(function ($response) {
/** @var Utopia\Response $response */
$utopia->get('/robots.txt')
$response->json([
'name' => APP_NAME,
'short_name' => APP_NAME,
'start_url' => '.',
'url' => 'https://appwrite.io/',
'display' => 'standalone',
'background_color' => '#fff',
'theme_color' => '#f02e65',
'description' => 'End to end backend server for frontend and mobile apps. 👩‍💻👨‍💻',
'icons' => [
[
'src' => 'images/favicon.png',
'sizes' => '256x256',
'type' => 'image/png',
],
],
]);
}, ['response']);
App::get('/robots.txt')
->desc('Robots.txt File')
->label('scope', 'public')
->label('docs', false)
->action(
function () use ($response) {
$template = new View(__DIR__.'/views/general/robots.phtml');
$response->text($template->render(false));
}
);
->action(function ($response) {
$template = new View(__DIR__.'/views/general/robots.phtml');
$response->text($template->render(false));
}, ['response']);
$utopia->get('/humans.txt')
App::get('/humans.txt')
->desc('Humans.txt File')
->label('scope', 'public')
->label('docs', false)
->action(
function () use ($response) {
$template = new View(__DIR__.'/views/general/humans.phtml');
$response->text($template->render(false));
}
);
->action(function ($response) {
$template = new View(__DIR__.'/views/general/humans.phtml');
$response->text($template->render(false));
}, ['response']);
$utopia->get('/.well-known/acme-challenge')
App::get('/.well-known/acme-challenge')
->desc('SSL Verification')
->label('scope', 'public')
->label('docs', false)
->action(
function () use ($request, $response) {
$base = \realpath(APP_STORAGE_CERTIFICATES);
$path = \str_replace('/.well-known/acme-challenge/', '', $request->getParam('q'));
$absolute = \realpath($base.'/.well-known/acme-challenge/'.$path);
->action(function ($request, $response) {
$base = \realpath(APP_STORAGE_CERTIFICATES);
$path = \str_replace('/.well-known/acme-challenge/', '', $request->getParam('q'));
$absolute = \realpath($base.'/.well-known/acme-challenge/'.$path);
if(!$base) {
throw new Exception('Storage error', 500);
}
if(!$absolute) {
throw new Exception('Unknown path', 404);
}
if(!\substr($absolute, 0, \strlen($base)) === $base) {
throw new Exception('Invalid path', 401);
}
if(!\file_exists($absolute)) {
throw new Exception('Unknown path', 404);
}
$content = @\file_get_contents($absolute);
if(!$content) {
throw new Exception('Failed to get contents', 500);
}
$response->text($content);
if(!$base) {
throw new Exception('Storage error', 500);
}
);
if(!$absolute) {
throw new Exception('Unknown path', 404);
}
if(!\substr($absolute, 0, \strlen($base)) === $base) {
throw new Exception('Invalid path', 401);
}
if(!\file_exists($absolute)) {
throw new Exception('Unknown path', 404);
}
$content = @\file_get_contents($absolute);
if(!$content) {
throw new Exception('Failed to get contents', 500);
}
$response->text($content);
}, ['request', 'response']);
include_once __DIR__ . '/controllers/shared/api.php';
include_once __DIR__ . '/controllers/shared/web.php';
@@ -430,4 +454,6 @@ foreach(Config::getParam('services', []) as $service) {
include_once $service['controller'];
}
$utopia->run($request, $response);
$app = new App('Asia/Tel_Aviv');
$app->run(new Request(), new Response());
+5 -12
View File
@@ -1,7 +1,6 @@
<?php
global $request;
use Utopia\App;
use Utopia\Config\Config;
use Appwrite\Database\Database;
@@ -36,13 +35,7 @@ $collections = [
'name' => 'Localhost',
'type' => 'web',
'hostname' => 'localhost',
],
[
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Current Host',
'type' => 'web',
'hostname' => \parse_url('https://'.$request->getServer('HTTP_HOST'), PHP_URL_HOST),
],
], // Current host is added on app init
],
'legalName' => '',
'legalCountry' => '',
@@ -50,9 +43,9 @@ $collections = [
'legalCity' => '',
'legalAddress' => '',
'legalTaxId' => '',
'authWhitelistEmails' => (!empty($request->getServer('_APP_CONSOLE_WHITELIST_EMAILS', null))) ? \explode(',', $request->getServer('_APP_CONSOLE_WHITELIST_EMAILS', null)) : [],
'authWhitelistIPs' => (!empty($request->getServer('_APP_CONSOLE_WHITELIST_IPS', null))) ? \explode(',', $request->getServer('_APP_CONSOLE_WHITELIST_IPS', null)) : [],
'authWhitelistDomains' => (!empty($request->getServer('_APP_CONSOLE_WHITELIST_DOMAINS', null))) ? \explode(',', $request->getServer('_APP_CONSOLE_WHITELIST_DOMAINS', null)) : [],
'authWhitelistEmails' => (!empty(App::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null))) ? \explode(',', App::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null)) : [],
'authWhitelistIPs' => (!empty(App::getEnv('_APP_CONSOLE_WHITELIST_IPS', null))) ? \explode(',', App::getEnv('_APP_CONSOLE_WHITELIST_IPS', null)) : [],
'authWhitelistDomains' => (!empty(App::getEnv('_APP_CONSOLE_WHITELIST_DOMAINS', null))) ? \explode(',', App::getEnv('_APP_CONSOLE_WHITELIST_DOMAINS', null)) : [],
],
Database::SYSTEM_COLLECTION_COLLECTIONS => [
'$collection' => Database::SYSTEM_COLLECTION_COLLECTIONS,

Some files were not shown because too many files have changed in this diff Show More