1
0
Fork 0
mirror of synced 2024-05-10 07:42:34 +12:00
appwrite/app/init.php

318 lines
13 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
2020-03-25 17:34:06 +13:00
/**
* Init
*
2020-09-25 10:32:39 +12:00
* Initializes both Appwrite API entry point, queue workers, and CLI tasks.
2020-03-25 17:34:06 +13:00
* Set configuration, framework resources, app constants
*
*/
2020-06-20 23:20:49 +12:00
if (\file_exists(__DIR__.'/../vendor/autoload.php')) {
2019-10-01 17:57:41 +13:00
require_once __DIR__.'/../vendor/autoload.php';
2019-08-01 08:35:42 +12:00
}
2019-05-09 18:54:39 +12:00
use Utopia\App;
use Utopia\Request;
use Utopia\Response;
2020-03-29 01:42:16 +13:00
use Utopia\Config\Config;
2019-05-09 18:54:39 +12:00
use Utopia\Locale\Locale;
use Utopia\Registry\Registry;
use Appwrite\Auth\Auth;
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Database\Validator\Authorization;
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
2019-08-09 09:49:46 +12:00
use PHPMailer\PHPMailer\PHPMailer;
2019-05-09 18:54:39 +12:00
2019-10-01 17:57:41 +13:00
const APP_NAME = 'Appwrite';
const APP_DOMAIN = 'appwrite.io';
2020-03-02 11:10:52 +13:00
const APP_EMAIL_TEAM = 'team@localhost.test'; // Default email address
const APP_EMAIL_SECURITY = 'security@localhost.test'; // Default security email address
2020-03-02 06:05:51 +13:00
const APP_USERAGENT = APP_NAME.'-Server v%s. Please report abuse at %s';
2019-10-01 17:57:41 +13:00
const APP_MODE_ADMIN = 'admin';
const APP_PAGING_LIMIT = 15;
2020-05-03 22:49:42 +12:00
const APP_CACHE_BUSTER = 125;
2020-05-31 05:32:16 +12:00
const APP_VERSION_STABLE = '0.6.2';
2020-02-20 01:41:23 +13:00
const APP_STORAGE_UPLOADS = '/storage/uploads';
const APP_STORAGE_CACHE = '/storage/cache';
const APP_STORAGE_CERTIFICATES = '/storage/certificates';
2020-02-25 23:04:12 +13:00
const APP_STORAGE_CONFIG = '/storage/config';
2020-02-21 09:43:06 +13:00
const APP_SOCIAL_TWITTER = 'https://twitter.com/appwrite_io';
const APP_SOCIAL_TWITTER_HANDLE = 'appwrite_io';
const APP_SOCIAL_FACEBOOK = 'https://www.facebook.com/appwrite.io';
const APP_SOCIAL_LINKEDIN = 'https://www.linkedin.com/company/appwrite';
const APP_SOCIAL_INSTAGRAM = 'https://www.instagram.com/appwrite.io';
const APP_SOCIAL_GITHUB = 'https://github.com/appwrite';
const APP_SOCIAL_DISCORD = 'https://discord.gg/GSeTUeA';
2020-05-18 03:57:42 +12:00
const APP_SOCIAL_DEV = 'https://dev.to/appwrite';
2019-05-09 18:54:39 +12:00
2019-10-01 17:57:41 +13:00
$register = new Registry();
$request = new Request();
$response = new Response();
2020-06-19 12:04:09 +12:00
$utopia = new App('Asia/Tel_Aviv');
$utopia->setMode($utopia->getEnv('_APP_ENV', App::MODE_TYPE_PRODUCTION));
2019-05-09 18:54:39 +12:00
2019-10-01 17:57:41 +13:00
/*
2019-05-09 18:54:39 +12:00
* ENV vars
*/
2020-05-26 07:54:18 +12:00
Config::load('events', __DIR__.'/../app/config/events.php');
2020-03-29 01:42:16 +13:00
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');
2020-06-26 18:14:54 +12:00
Config::load('roles', __DIR__.'/../app/config/roles.php'); // User roles and scopes
Config::load('services', __DIR__.'/../app/config/services.php'); // List of services
2020-03-29 01:42:16 +13:00
2020-06-19 12:04:09 +12:00
Config::setParam('env', $utopia->getMode());
2020-03-29 01:42:16 +13:00
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')));
2020-06-20 23:20:49 +12:00
Config::setParam('port', (string) \parse_url(Config::getParam('protocol').'://'.$request->getServer('HTTP_HOST', ''), PHP_URL_PORT));
Config::setParam('hostname', \parse_url(Config::getParam('protocol').'://'.$request->getServer('HTTP_HOST', null), PHP_URL_HOST));
2020-03-29 01:42:16 +13:00
Resque::setBackend($request->getServer('_APP_REDIS_HOST', '')
.':'.$request->getServer('_APP_REDIS_PORT', ''));
2019-05-09 18:54:39 +12:00
2020-06-20 23:20:49 +12:00
\define('COOKIE_DOMAIN',
2019-11-22 07:28:15 +13:00
(
$request->getServer('HTTP_HOST', null) === 'localhost' ||
2020-03-29 01:42:16 +13:00
$request->getServer('HTTP_HOST', null) === 'localhost:'.Config::getParam('port') ||
2020-06-20 23:20:49 +12:00
(\filter_var(Config::getParam('hostname'), FILTER_VALIDATE_IP) !== false)
2019-11-22 07:28:15 +13:00
)
? null
: '.'.Config::getParam('hostname')
);
2020-06-20 23:20:49 +12:00
\define('COOKIE_SAMESITE', Response::COOKIE_SAMESITE_NONE);
2019-05-09 18:54:39 +12:00
2019-10-01 17:57:41 +13:00
/*
2019-05-09 18:54:39 +12:00
* Registry
*/
$register->set('db', function () use ($request) { // Register DB connection
2019-10-01 17:57:41 +13:00
$dbHost = $request->getServer('_APP_DB_HOST', '');
$dbUser = $request->getServer('_APP_DB_USER', '');
$dbPass = $request->getServer('_APP_DB_PASS', '');
$dbScheme = $request->getServer('_APP_DB_SCHEMA', '');
2019-05-09 18:54:39 +12:00
2019-07-10 02:01:08 +12:00
$pdo = new PDO("mysql:host={$dbHost};dbname={$dbScheme};charset=utf8mb4", $dbUser, $dbPass, array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
2019-10-01 17:57:41 +13:00
PDO::ATTR_TIMEOUT => 5, // Seconds
2019-05-09 18:54:39 +12:00
));
// Connection settings
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // Return arrays
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Handle all errors with exceptions
return $pdo;
});
$register->set('influxdb', function () use ($request) { // Register DB connection
$host = $request->getServer('_APP_INFLUXDB_HOST', '');
$port = $request->getServer('_APP_INFLUXDB_PORT', '');
if (empty($host) || empty($port)) {
2019-10-01 17:57:41 +13:00
return;
}
2019-05-09 18:54:39 +12:00
$client = new InfluxDB\Client($host, $port, '', '', false, false, 5);
return $client;
});
$register->set('statsd', function () use ($request) { // Register DB connection
2019-08-07 19:56:08 +12:00
$host = $request->getServer('_APP_STATSD_HOST', 'telegraf');
2019-05-09 18:54:39 +12:00
$port = $request->getServer('_APP_STATSD_PORT', 8125);
$connection = new \Domnikl\Statsd\Connection\UdpSocket($host, $port);
$statsd = new \Domnikl\Statsd\Client($connection);
return $statsd;
});
2020-03-29 01:42:16 +13:00
$register->set('cache', function () use ($request) { // Register cache connection
2019-05-09 18:54:39 +12:00
$redis = new Redis();
2020-03-29 01:42:16 +13:00
$redis->connect($request->getServer('_APP_REDIS_HOST', ''),
$request->getServer('_APP_REDIS_PORT', ''));
2019-10-01 17:57:41 +13:00
2019-05-09 18:54:39 +12:00
return $redis;
});
$register->set('smtp', function () use ($request) {
2019-08-09 09:49:46 +12:00
$mail = new PHPMailer(true);
2019-05-09 18:54:39 +12:00
2019-08-09 09:49:46 +12:00
$mail->isSMTP();
2019-05-09 18:54:39 +12:00
2020-01-12 02:58:02 +13:00
$username = $request->getServer('_APP_SMTP_USERNAME', null);
$password = $request->getServer('_APP_SMTP_PASSWORD', null);
2019-08-09 09:49:46 +12:00
2019-10-01 17:57:41 +13:00
$mail->XMailer = 'Appwrite Mailer';
$mail->Host = $request->getServer('_APP_SMTP_HOST', 'smtp');
$mail->Port = $request->getServer('_APP_SMTP_PORT', 25);
$mail->SMTPAuth = (!empty($username) && !empty($password));
$mail->Username = $username;
$mail->Password = $password;
2020-01-12 02:58:02 +13:00
$mail->SMTPSecure = $request->getServer('_APP_SMTP_SECURE', false);
$mail->SMTPAutoTLS = false;
2020-06-13 04:49:56 +12:00
$mail->CharSet = 'UTF-8';
2019-08-09 09:49:46 +12:00
2020-06-20 23:20:49 +12:00
$from = \urldecode($request->getServer('_APP_SYSTEM_EMAIL_NAME', APP_NAME.' Server'));
2020-02-08 12:41:45 +13:00
$email = $request->getServer('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM);
$mail->setFrom($email, $from);
$mail->addReplyTo($email, $from);
2019-05-09 18:54:39 +12:00
2019-08-09 09:49:46 +12:00
$mail->isHTML(true);
2019-09-28 13:48:50 +12:00
2019-08-09 09:49:46 +12:00
return $mail;
2019-05-09 18:54:39 +12:00
});
2019-10-01 17:57:41 +13:00
/*
2019-05-09 18:54:39 +12:00
* Localization
*/
$locale = $request->getParam('locale', $request->getHeader('X-Appwrite-Locale', ''));
2019-05-09 18:54:39 +12:00
Locale::$exceptions = false;
2019-10-25 08:20:47 +13:00
Locale::setLanguage('af', include __DIR__.'/config/locales/af.php');
Locale::setLanguage('ar', include __DIR__.'/config/locales/ar.php');
Locale::setLanguage('bn', include __DIR__.'/config/locales/bn.php');
Locale::setLanguage('cat', include __DIR__.'/config/locales/cat.php');
Locale::setLanguage('cz', include __DIR__.'/config/locales/cz.php');
Locale::setLanguage('de', include __DIR__.'/config/locales/de.php');
Locale::setLanguage('en', include __DIR__.'/config/locales/en.php');
Locale::setLanguage('es', include __DIR__.'/config/locales/es.php');
Locale::setLanguage('fi', include __DIR__.'/config/locales/fi.php');
2019-10-26 18:31:29 +13:00
Locale::setLanguage('fo', include __DIR__.'/config/locales/fo.php');
2019-10-25 08:20:47 +13:00
Locale::setLanguage('fr', include __DIR__.'/config/locales/fr.php');
Locale::setLanguage('gr', include __DIR__.'/config/locales/gr.php');
Locale::setLanguage('he', include __DIR__.'/config/locales/he.php');
Locale::setLanguage('hi', include __DIR__.'/config/locales/hi.php');
Locale::setLanguage('hu', include __DIR__.'/config/locales/hu.php');
Locale::setLanguage('hy', include __DIR__.'/config/locales/hy.php');
Locale::setLanguage('id', include __DIR__.'/config/locales/id.php');
Locale::setLanguage('is', include __DIR__.'/config/locales/is.php');
Locale::setLanguage('it', include __DIR__.'/config/locales/it.php');
2019-11-18 18:05:10 +13:00
Locale::setLanguage('ja', include __DIR__.'/config/locales/ja.php');
2019-10-25 08:20:47 +13:00
Locale::setLanguage('jv', include __DIR__.'/config/locales/jv.php');
2020-03-30 09:10:47 +13:00
Locale::setLanguage('km', include __DIR__.'/config/locales/km.php');
2019-10-25 08:20:47 +13:00
Locale::setLanguage('ko', include __DIR__.'/config/locales/ko.php');
Locale::setLanguage('lt', include __DIR__.'/config/locales/lt.php');
Locale::setLanguage('ml', include __DIR__.'/config/locales/ml.php');
Locale::setLanguage('ms', include __DIR__.'/config/locales/ms.php');
Locale::setLanguage('nl', include __DIR__.'/config/locales/nl.php');
Locale::setLanguage('no', include __DIR__.'/config/locales/no.php');
2019-10-28 08:00:23 +13:00
Locale::setLanguage('ph', include __DIR__.'/config/locales/ph.php');
2019-10-25 08:20:47 +13:00
Locale::setLanguage('pl', include __DIR__.'/config/locales/pl.php');
Locale::setLanguage('pt-br', include __DIR__.'/config/locales/pt-br.php');
Locale::setLanguage('pt-pt', include __DIR__.'/config/locales/pt-pt.php');
Locale::setLanguage('ro', include __DIR__.'/config/locales/ro.php');
Locale::setLanguage('ru', include __DIR__ . '/config/locales/ru.php');
Locale::setLanguage('si', include __DIR__ . '/config/locales/si.php');
Locale::setLanguage('sl', include __DIR__ . '/config/locales/sl.php');
Locale::setLanguage('sq', include __DIR__ . '/config/locales/sq.php');
Locale::setLanguage('sv', include __DIR__ . '/config/locales/sv.php');
Locale::setLanguage('ta', include __DIR__ . '/config/locales/ta.php');
Locale::setLanguage('th', include __DIR__.'/config/locales/th.php');
Locale::setLanguage('tr', include __DIR__.'/config/locales/tr.php');
Locale::setLanguage('ua', include __DIR__.'/config/locales/ua.php');
Locale::setLanguage('vi', include __DIR__.'/config/locales/vi.php');
Locale::setLanguage('zh-cn', include __DIR__.'/config/locales/zh-cn.php');
Locale::setLanguage('zh-tw', include __DIR__.'/config/locales/zh-tw.php');
2019-10-09 17:33:33 +13:00
Locale::setDefault('en');
2020-06-20 23:20:49 +12:00
if (\in_array($locale, Config::getParam('locales'))) {
2019-05-09 18:54:39 +12:00
Locale::setDefault($locale);
}
2020-06-20 23:20:49 +12:00
\stream_context_set_default([ // Set global user agent and http settings
2019-05-09 18:54:39 +12:00
'http' => [
'method' => 'GET',
2020-06-20 23:20:49 +12:00
'user_agent' => \sprintf(APP_USERAGENT,
2020-03-29 01:42:16 +13:00
Config::getParam('version'),
2020-03-02 06:05:51 +13:00
$request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)),
2019-10-01 17:57:41 +13:00
'timeout' => 2,
],
2019-05-09 18:54:39 +12:00
]);
2019-10-01 17:57:41 +13:00
/*
2019-05-09 18:54:39 +12:00
* Auth & Project Scope
*/
$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
2020-03-29 01:42:16 +13:00
$consoleDB->setMocks(Config::getParam('collections', []));
2019-05-09 18:54:39 +12:00
Authorization::disable();
$project = $consoleDB->getDocument($request->getParam('project', $request->getHeader('X-Appwrite-Project', '')));
2019-05-09 18:54:39 +12:00
Authorization::enable();
$console = $consoleDB->getDocument('console');
$mode = $request->getParam('mode', $request->getHeader('X-Appwrite-Mode', 'default'));
2020-02-17 20:16:11 +13:00
Auth::setCookieName('a_session_'.$project->getId());
2019-05-09 18:54:39 +12:00
if (APP_MODE_ADMIN === $mode) {
2020-02-17 20:16:11 +13:00
Auth::setCookieName('a_session_'.$console->getId());
2019-05-09 18:54:39 +12:00
}
$session = Auth::decodeSession(
$request->getCookie(Auth::$cookieName, // Get sessions
$request->getCookie(Auth::$cookieName.'_legacy', // Get fallback session from old clients (no SameSite support)
$request->getHeader('X-Appwrite-Key', '')))); // Get API Key
2020-02-27 22:17:09 +13:00
// Get fallback session from clients who block 3rd-party cookies
$response->addHeader('X-Debug-Fallback', 'false');
if(empty($session['id']) && empty($session['secret'])) {
$response->addHeader('X-Debug-Fallback', 'true');
$fallback = $request->getHeader('X-Fallback-Cookies', '');
2020-06-20 23:20:49 +12:00
$fallback = \json_decode($fallback, true);
2020-02-27 22:17:09 +13:00
$session = Auth::decodeSession(((isset($fallback[Auth::$cookieName])) ? $fallback[Auth::$cookieName] : ''));
}
2019-10-01 17:57:41 +13:00
Auth::$unique = $session['id'];
Auth::$secret = $session['secret'];
2019-05-09 18:54:39 +12:00
$projectDB = new Database();
$projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
2020-02-17 20:16:11 +13:00
$projectDB->setNamespace('app_'.$project->getId());
2020-03-29 01:42:16 +13:00
$projectDB->setMocks(Config::getParam('collections', []));
2019-05-09 18:54:39 +12:00
2020-06-16 02:06:27 +12:00
if (APP_MODE_ADMIN !== $mode) {
$user = $projectDB->getDocument(Auth::$unique);
}
else {
2019-05-09 18:54:39 +12:00
$user = $consoleDB->getDocument(Auth::$unique);
$user
2020-02-17 20:16:11 +13:00
->setAttribute('$id', 'admin-'.$user->getAttribute('$id'))
2019-05-09 18:54:39 +12:00
;
}
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) // Check a document has been found in the DB
2019-05-09 18:54:39 +12:00
|| Database::SYSTEM_COLLECTION_USERS !== $user->getCollection() // Validate returned document is really a user document
|| !Auth::tokenVerify($user->getAttribute('tokens', []), Auth::TOKEN_TYPE_LOGIN, Auth::$secret)) { // Validate user has valid login token
2020-02-17 20:16:11 +13:00
$user = new Document(['$id' => '', '$collection' => Database::SYSTEM_COLLECTION_USERS]);
2019-05-09 18:54:39 +12:00
}
if (APP_MODE_ADMIN === $mode) {
if (!empty($user->search('teamId', $project->getAttribute('teamId'), $user->getAttribute('memberships')))) {
2019-05-09 18:54:39 +12:00
Authorization::disable();
} else {
2020-02-17 20:16:11 +13:00
$user = new Document(['$id' => '', '$collection' => Database::SYSTEM_COLLECTION_USERS]);
2019-05-09 18:54:39 +12:00
}
}
// Set project mail
2020-02-08 12:41:45 +13:00
$register->get('smtp')
->setFrom(
$request->getServer('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM),
2020-02-17 20:16:11 +13:00
($project->getId() === 'console')
2020-06-20 23:20:49 +12:00
? \urldecode($request->getServer('_APP_SYSTEM_EMAIL_NAME', APP_NAME.' Server'))
: \sprintf(Locale::getText('account.emails.team'), $project->getAttribute('name')
2020-02-08 12:41:45 +13:00
)
);