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-12-27 00:56:29 +13:00
|
|
|
* Set configuration, framework resources & app constants
|
2020-03-25 17:34:06 +13:00
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
2021-03-10 19:53:49 +13:00
|
|
|
ini_set('memory_limit','512M');
|
|
|
|
ini_set('display_errors', 1);
|
|
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
ini_set('default_socket_timeout', -1);
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
2020-12-29 09:31:42 +13:00
|
|
|
use Ahc\Jwt\JWT;
|
|
|
|
use Ahc\Jwt\JWTException;
|
2020-07-01 06:08:02 +12:00
|
|
|
use Appwrite\Auth\Auth;
|
2020-03-25 06:56:32 +13:00
|
|
|
use Appwrite\Database\Database;
|
|
|
|
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
|
|
|
|
use Appwrite\Database\Adapter\Redis as RedisAdapter;
|
2020-06-30 16:34:13 +12:00
|
|
|
use Appwrite\Database\Document;
|
2020-07-01 06:08:02 +12:00
|
|
|
use Appwrite\Database\Validator\Authorization;
|
2020-06-28 07:42:38 +12:00
|
|
|
use Appwrite\Event\Event;
|
2021-11-17 00:45:24 +13:00
|
|
|
use Appwrite\GraphQL\Builder;
|
2020-05-13 10:00:00 +12:00
|
|
|
use Appwrite\OpenSSL\OpenSSL;
|
2020-07-02 10:34:05 +12:00
|
|
|
use Utopia\App;
|
2020-06-30 16:34:13 +12:00
|
|
|
use Utopia\View;
|
2020-07-02 10:34:05 +12:00
|
|
|
use Utopia\Config\Config;
|
2021-11-17 00:45:24 +13:00
|
|
|
use Utopia\CLI\Console;
|
2020-07-02 10:34:05 +12:00
|
|
|
use Utopia\Locale\Locale;
|
|
|
|
use Utopia\Registry\Registry;
|
2020-10-24 06:36:52 +13:00
|
|
|
use MaxMind\Db\Reader;
|
2020-07-02 10:34:05 +12:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
2021-06-23 07:51:14 +12:00
|
|
|
use Swoole\Database\PDOConfig;
|
|
|
|
use Swoole\Database\PDOPool;
|
|
|
|
use Swoole\Database\RedisConfig;
|
|
|
|
use Swoole\Database\RedisPool;
|
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
|
2021-02-19 05:48:11 +13:00
|
|
|
const APP_EMAIL_SECURITY = ''; // 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';
|
2020-11-19 11:08:45 +13:00
|
|
|
const APP_MODE_DEFAULT = 'default';
|
2019-10-01 17:57:41 +13:00
|
|
|
const APP_MODE_ADMIN = 'admin';
|
2020-07-20 02:43:34 +12:00
|
|
|
const APP_PAGING_LIMIT = 12;
|
2021-10-16 05:11:20 +13:00
|
|
|
const APP_CACHE_BUSTER = 170;
|
|
|
|
const APP_VERSION_STABLE = '0.11.0';
|
2020-02-20 01:41:23 +13:00
|
|
|
const APP_STORAGE_UPLOADS = '/storage/uploads';
|
2020-07-15 09:20:46 +12:00
|
|
|
const APP_STORAGE_FUNCTIONS = '/storage/functions';
|
2020-02-20 01:41:23 +13:00
|
|
|
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';
|
2020-07-20 15:59:04 +12:00
|
|
|
const APP_SOCIAL_DISCORD = 'https://appwrite.io/discord';
|
2021-04-11 04:14:15 +12:00
|
|
|
const APP_SOCIAL_DISCORD_CHANNEL = '564160730845151244';
|
2020-05-18 03:57:42 +12:00
|
|
|
const APP_SOCIAL_DEV = 'https://dev.to/appwrite';
|
2020-12-19 03:05:15 +13:00
|
|
|
const APP_SOCIAL_STACKSHARE = 'https://stackshare.io/appwrite';
|
2021-10-18 04:08:40 +13:00
|
|
|
const APP_SOCIAL_YOUTUBE = 'https://www.youtube.com/c/appwrite';
|
|
|
|
|
2020-12-19 03:05:15 +13:00
|
|
|
// Deletion Types
|
|
|
|
const DELETE_TYPE_DOCUMENT = 'document';
|
2020-12-28 06:57:35 +13:00
|
|
|
const DELETE_TYPE_EXECUTIONS = 'executions';
|
2020-12-19 03:05:15 +13:00
|
|
|
const DELETE_TYPE_AUDIT = 'audit';
|
|
|
|
const DELETE_TYPE_ABUSE = 'abuse';
|
2021-02-05 23:57:43 +13:00
|
|
|
const DELETE_TYPE_CERTIFICATES = 'certificates';
|
2021-06-16 21:35:37 +12:00
|
|
|
const DELETE_TYPE_REALTIME = 'realtime';
|
2021-07-28 08:31:19 +12:00
|
|
|
// Mail Types
|
|
|
|
const MAIL_TYPE_VERIFICATION = 'verification';
|
2021-08-30 22:44:52 +12:00
|
|
|
const MAIL_TYPE_MAGIC_SESSION = 'magicSession';
|
2021-07-28 08:31:19 +12:00
|
|
|
const MAIL_TYPE_RECOVERY = 'recovery';
|
|
|
|
const MAIL_TYPE_INVITATION = 'invitation';
|
2021-03-29 10:22:12 +13:00
|
|
|
// Auth Types
|
|
|
|
const APP_AUTH_TYPE_SESSION = 'Session';
|
|
|
|
const APP_AUTH_TYPE_JWT = 'JWT';
|
|
|
|
const APP_AUTH_TYPE_KEY = 'Key';
|
|
|
|
const APP_AUTH_TYPE_ADMIN = 'Admin';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-10-01 17:57:41 +13:00
|
|
|
$register = new Registry();
|
2020-06-19 12:04:09 +12:00
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::setMode(App::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-06-30 17:27:52 +12:00
|
|
|
Config::load('events', __DIR__.'/config/events.php');
|
2021-02-28 23:16:27 +13:00
|
|
|
Config::load('auth', __DIR__.'/config/auth.php');
|
2020-06-30 17:27:52 +12:00
|
|
|
Config::load('providers', __DIR__.'/config/providers.php');
|
|
|
|
Config::load('platforms', __DIR__.'/config/platforms.php');
|
|
|
|
Config::load('collections', __DIR__.'/config/collections.php');
|
2021-04-21 23:02:54 +12:00
|
|
|
Config::load('runtimes', __DIR__.'/config/runtimes.php');
|
2020-06-30 17:27:52 +12:00
|
|
|
Config::load('roles', __DIR__.'/config/roles.php'); // User roles and scopes
|
|
|
|
Config::load('scopes', __DIR__.'/config/scopes.php'); // User roles and scopes
|
|
|
|
Config::load('services', __DIR__.'/config/services.php'); // List of services
|
2020-07-29 16:03:28 +12:00
|
|
|
Config::load('variables', __DIR__.'/config/variables.php'); // List of env variables
|
2020-06-30 17:27:52 +12:00
|
|
|
Config::load('avatar-browsers', __DIR__.'/config/avatars/browsers.php');
|
|
|
|
Config::load('avatar-credit-cards', __DIR__.'/config/avatars/credit-cards.php');
|
|
|
|
Config::load('avatar-flags', __DIR__.'/config/avatars/flags.php');
|
|
|
|
Config::load('locale-codes', __DIR__.'/config/locale/codes.php');
|
|
|
|
Config::load('locale-currencies', __DIR__.'/config/locale/currencies.php');
|
|
|
|
Config::load('locale-eu', __DIR__.'/config/locale/eu.php');
|
|
|
|
Config::load('locale-languages', __DIR__.'/config/locale/languages.php');
|
|
|
|
Config::load('locale-phones', __DIR__.'/config/locale/phones.php');
|
2021-07-23 08:15:01 +12:00
|
|
|
Config::load('locale-countries', __DIR__.'/config/locale/countries.php');
|
|
|
|
Config::load('locale-continents', __DIR__.'/config/locale/continents.php');
|
2020-06-30 17:27:52 +12:00
|
|
|
Config::load('storage-logos', __DIR__.'/config/storage/logos.php');
|
|
|
|
Config::load('storage-mimes', __DIR__.'/config/storage/mimes.php');
|
|
|
|
Config::load('storage-inputs', __DIR__.'/config/storage/inputs.php');
|
|
|
|
Config::load('storage-outputs', __DIR__.'/config/storage/outputs.php');
|
2020-03-29 01:42:16 +13:00
|
|
|
|
2021-02-01 03:56:06 +13:00
|
|
|
$user = App::getEnv('_APP_REDIS_USER','');
|
|
|
|
$pass = App::getEnv('_APP_REDIS_PASS','');
|
2021-02-01 19:21:42 +13:00
|
|
|
if(!empty($user) || !empty($pass)) {
|
2021-02-01 03:56:06 +13:00
|
|
|
Resque::setBackend('redis://'.$user.':'.$pass.'@'.App::getEnv('_APP_REDIS_HOST', '').':'.App::getEnv('_APP_REDIS_PORT', ''));
|
2021-01-31 18:24:38 +13:00
|
|
|
} else {
|
|
|
|
Resque::setBackend(App::getEnv('_APP_REDIS_HOST', '').':'.App::getEnv('_APP_REDIS_PORT', ''));
|
|
|
|
}
|
2020-07-13 08:43:03 +12:00
|
|
|
/**
|
|
|
|
* DB Filters
|
|
|
|
*/
|
|
|
|
Database::addFilter('json',
|
|
|
|
function($value) {
|
|
|
|
if(!is_array($value)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
return json_encode($value);
|
|
|
|
},
|
|
|
|
function($value) {
|
|
|
|
return json_decode($value, true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Database::addFilter('encrypt',
|
2020-07-13 09:18:52 +12:00
|
|
|
function($value) {
|
|
|
|
$key = App::getEnv('_APP_OPENSSL_KEY_V1');
|
2020-07-13 08:43:03 +12:00
|
|
|
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
|
|
|
$tag = null;
|
|
|
|
|
|
|
|
return json_encode([
|
|
|
|
'data' => OpenSSL::encrypt($value, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
|
|
|
|
'method' => OpenSSL::CIPHER_AES_128_GCM,
|
|
|
|
'iv' => bin2hex($iv),
|
|
|
|
'tag' => bin2hex($tag),
|
|
|
|
'version' => '1',
|
|
|
|
]);
|
|
|
|
},
|
2020-07-13 09:18:52 +12:00
|
|
|
function($value) {
|
2020-07-13 08:43:03 +12:00
|
|
|
$value = json_decode($value, true);
|
2020-07-13 09:18:52 +12:00
|
|
|
$key = App::getEnv('_APP_OPENSSL_KEY_V'.$value['version']);
|
2020-07-13 08:43:03 +12:00
|
|
|
|
|
|
|
return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag']));
|
|
|
|
}
|
|
|
|
);
|
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
|
|
|
|
*/
|
2021-03-25 00:37:57 +13:00
|
|
|
$register->set('dbPool', function () { // Register DB connection
|
2020-06-29 05:31:21 +12:00
|
|
|
$dbHost = App::getEnv('_APP_DB_HOST', '');
|
2021-06-23 07:51:14 +12:00
|
|
|
$dbPort = App::getEnv('_APP_DB_PORT', '');
|
2020-06-29 05:31:21 +12:00
|
|
|
$dbUser = App::getEnv('_APP_DB_USER', '');
|
|
|
|
$dbPass = App::getEnv('_APP_DB_PASS', '');
|
|
|
|
$dbScheme = App::getEnv('_APP_DB_SCHEMA', '');
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2021-06-23 07:51:14 +12:00
|
|
|
$pool = new PDOPool((new PDOConfig())
|
|
|
|
->withHost($dbHost)
|
|
|
|
->withPort($dbPort)
|
|
|
|
->withDbName($dbScheme)
|
|
|
|
->withCharset('utf8mb4')
|
|
|
|
->withUsername($dbUser)
|
|
|
|
->withPassword($dbPass)
|
2021-08-25 03:45:09 +12:00
|
|
|
->withOptions([
|
|
|
|
PDO::ATTR_ERRMODE => App::isDevelopment() ? PDO::ERRMODE_WARNING : PDO::ERRMODE_SILENT, // If in production mode, warnings are not displayed
|
|
|
|
])
|
2021-07-13 04:15:21 +12:00
|
|
|
, 16);
|
2021-03-25 00:37:57 +13:00
|
|
|
|
|
|
|
return $pool;
|
|
|
|
});
|
|
|
|
$register->set('redisPool', function () {
|
2021-06-12 04:09:46 +12:00
|
|
|
$redisHost = App::getEnv('_APP_REDIS_HOST', '');
|
|
|
|
$redisPort = App::getEnv('_APP_REDIS_PORT', '');
|
|
|
|
$redisUser = App::getEnv('_APP_REDIS_USER', '');
|
|
|
|
$redisPass = App::getEnv('_APP_REDIS_PASS', '');
|
2021-06-23 07:51:14 +12:00
|
|
|
$redisAuth = '';
|
2021-06-12 04:09:46 +12:00
|
|
|
|
2021-06-23 07:51:14 +12:00
|
|
|
if ($redisUser && $redisPass) {
|
|
|
|
$redisAuth = $redisUser.':'.$redisPass;
|
2021-03-25 00:37:57 +13:00
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2021-06-23 07:51:14 +12:00
|
|
|
$pool = new RedisPool((new RedisConfig)
|
|
|
|
->withHost($redisHost)
|
|
|
|
->withPort($redisPort)
|
|
|
|
->withAuth($redisAuth)
|
|
|
|
->withDbIndex(0)
|
2021-07-13 04:15:21 +12:00
|
|
|
, 16);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2021-03-25 00:37:57 +13:00
|
|
|
return $pool;
|
2019-05-09 18:54:39 +12:00
|
|
|
});
|
2020-06-29 08:45:36 +12:00
|
|
|
$register->set('influxdb', function () { // Register DB connection
|
2020-06-29 05:31:21 +12:00
|
|
|
$host = App::getEnv('_APP_INFLUXDB_HOST', '');
|
|
|
|
$port = App::getEnv('_APP_INFLUXDB_PORT', '');
|
2019-07-30 05:37:25 +12:00
|
|
|
|
2019-09-30 19:13:40 +13:00
|
|
|
if (empty($host) || empty($port)) {
|
2019-10-01 17:57:41 +13:00
|
|
|
return;
|
2019-07-30 05:37:25 +12:00
|
|
|
}
|
2021-05-19 07:05:44 +12:00
|
|
|
$driver = new InfluxDB\Driver\Curl("http://{$host}:{$port}");
|
2019-05-09 18:54:39 +12:00
|
|
|
$client = new InfluxDB\Client($host, $port, '', '', false, false, 5);
|
2021-05-05 00:32:20 +12:00
|
|
|
$client->setDriver($driver);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
return $client;
|
|
|
|
});
|
2020-06-29 08:45:36 +12:00
|
|
|
$register->set('statsd', function () { // Register DB connection
|
2020-06-29 05:31:21 +12:00
|
|
|
$host = App::getEnv('_APP_STATSD_HOST', 'telegraf');
|
|
|
|
$port = App::getEnv('_APP_STATSD_PORT', 8125);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
$connection = new \Domnikl\Statsd\Connection\UdpSocket($host, $port);
|
|
|
|
$statsd = new \Domnikl\Statsd\Client($connection);
|
|
|
|
|
|
|
|
return $statsd;
|
|
|
|
});
|
2020-06-29 08:45:36 +12:00
|
|
|
$register->set('smtp', function () {
|
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-06-29 05:31:21 +12:00
|
|
|
$username = App::getEnv('_APP_SMTP_USERNAME', null);
|
|
|
|
$password = App::getEnv('_APP_SMTP_PASSWORD', null);
|
2019-08-09 09:49:46 +12:00
|
|
|
|
2019-10-01 17:57:41 +13:00
|
|
|
$mail->XMailer = 'Appwrite Mailer';
|
2020-06-29 05:31:21 +12:00
|
|
|
$mail->Host = App::getEnv('_APP_SMTP_HOST', 'smtp');
|
|
|
|
$mail->Port = App::getEnv('_APP_SMTP_PORT', 25);
|
2019-10-01 17:57:41 +13:00
|
|
|
$mail->SMTPAuth = (!empty($username) && !empty($password));
|
|
|
|
$mail->Username = $username;
|
|
|
|
$mail->Password = $password;
|
2020-06-29 05:31:21 +12:00
|
|
|
$mail->SMTPSecure = App::getEnv('_APP_SMTP_SECURE', false);
|
2020-01-12 02:58:02 +13:00
|
|
|
$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-29 05:31:21 +12:00
|
|
|
$from = \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME.' Server'));
|
|
|
|
$email = App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM);
|
2020-02-08 12:19:13 +13:00
|
|
|
|
|
|
|
$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
|
|
|
});
|
2020-10-30 03:08:09 +13:00
|
|
|
$register->set('geodb', function () {
|
2021-10-15 23:34:53 +13:00
|
|
|
return new Reader(__DIR__.'/db/DBIP/dbip-country-lite-2021-10.mmdb');
|
2020-10-30 03:08:09 +13:00
|
|
|
});
|
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::$exceptions = false;
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('af', __DIR__.'/config/locale/translations/af.json');
|
|
|
|
Locale::setLanguageFromJSON('ar', __DIR__.'/config/locale/translations/ar.json');
|
2021-10-07 13:24:12 +13:00
|
|
|
Locale::setLanguageFromJSON('as', __DIR__.'/config/locale/translations/as.json');
|
2021-10-08 11:12:21 +13:00
|
|
|
Locale::setLanguageFromJSON('az', __DIR__.'/config/locale/translations/az.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('be', __DIR__.'/config/locale/translations/be.json');
|
|
|
|
Locale::setLanguageFromJSON('bg', __DIR__.'/config/locale/translations/bg.json');
|
2021-10-02 08:14:46 +13:00
|
|
|
Locale::setLanguageFromJSON('bh', __DIR__.'/config/locale/translations/bh.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('bn', __DIR__.'/config/locale/translations/bn.json');
|
2021-07-29 08:03:14 +12:00
|
|
|
Locale::setLanguageFromJSON('bs', __DIR__.'/config/locale/translations/bs.json');
|
2021-07-28 18:28:00 +12:00
|
|
|
Locale::setLanguageFromJSON('ca', __DIR__.'/config/locale/translations/ca.json');
|
|
|
|
Locale::setLanguageFromJSON('cs', __DIR__.'/config/locale/translations/cs.json');
|
2021-10-02 04:45:47 +13:00
|
|
|
Locale::setLanguageFromJSON('da', __DIR__.'/config/locale/translations/da.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('de', __DIR__.'/config/locale/translations/de.json');
|
2021-07-30 19:25:07 +12:00
|
|
|
Locale::setLanguageFromJSON('el', __DIR__.'/config/locale/translations/el.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('en', __DIR__.'/config/locale/translations/en.json');
|
2021-10-08 03:24:20 +13:00
|
|
|
Locale::setLanguageFromJSON('eo', __DIR__.'/config/locale/translations/eo.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('es', __DIR__.'/config/locale/translations/es.json');
|
|
|
|
Locale::setLanguageFromJSON('fa', __DIR__.'/config/locale/translations/fa.json');
|
|
|
|
Locale::setLanguageFromJSON('fi', __DIR__.'/config/locale/translations/fi.json');
|
|
|
|
Locale::setLanguageFromJSON('fo', __DIR__.'/config/locale/translations/fo.json');
|
|
|
|
Locale::setLanguageFromJSON('fr', __DIR__.'/config/locale/translations/fr.json');
|
2021-10-11 00:55:45 +13:00
|
|
|
Locale::setLanguageFromJSON('ga', __DIR__.'/config/locale/translations/ga.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('gu', __DIR__.'/config/locale/translations/gu.json');
|
|
|
|
Locale::setLanguageFromJSON('he', __DIR__.'/config/locale/translations/he.json');
|
|
|
|
Locale::setLanguageFromJSON('hi', __DIR__.'/config/locale/translations/hi.json');
|
2021-10-02 02:49:31 +13:00
|
|
|
Locale::setLanguageFromJSON('hr', __DIR__.'/config/locale/translations/hr.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('hu', __DIR__.'/config/locale/translations/hu.json');
|
|
|
|
Locale::setLanguageFromJSON('hy', __DIR__.'/config/locale/translations/hy.json');
|
|
|
|
Locale::setLanguageFromJSON('id', __DIR__.'/config/locale/translations/id.json');
|
|
|
|
Locale::setLanguageFromJSON('is', __DIR__.'/config/locale/translations/is.json');
|
|
|
|
Locale::setLanguageFromJSON('it', __DIR__.'/config/locale/translations/it.json');
|
|
|
|
Locale::setLanguageFromJSON('ja', __DIR__.'/config/locale/translations/ja.json');
|
|
|
|
Locale::setLanguageFromJSON('jv', __DIR__.'/config/locale/translations/jv.json');
|
2021-07-30 19:51:31 +12:00
|
|
|
Locale::setLanguageFromJSON('kn', __DIR__.'/config/locale/translations/kn.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('km', __DIR__.'/config/locale/translations/km.json');
|
|
|
|
Locale::setLanguageFromJSON('ko', __DIR__.'/config/locale/translations/ko.json');
|
2021-10-03 20:26:03 +13:00
|
|
|
Locale::setLanguageFromJSON('lb', __DIR__.'/config/locale/translations/lb.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('lt', __DIR__.'/config/locale/translations/lt.json');
|
2021-10-07 11:04:57 +13:00
|
|
|
Locale::setLanguageFromJSON('lv', __DIR__.'/config/locale/translations/lv.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('ml', __DIR__.'/config/locale/translations/ml.json');
|
|
|
|
Locale::setLanguageFromJSON('mr', __DIR__.'/config/locale/translations/mr.json');
|
|
|
|
Locale::setLanguageFromJSON('ms', __DIR__.'/config/locale/translations/ms.json');
|
2021-10-06 06:50:20 +13:00
|
|
|
Locale::setLanguageFromJSON('nb', __DIR__.'/config/locale/translations/nb.json');
|
2021-07-30 19:25:07 +12:00
|
|
|
Locale::setLanguageFromJSON('ne', __DIR__.'/config/locale/translations/ne.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('nl', __DIR__.'/config/locale/translations/nl.json');
|
2021-10-07 09:53:25 +13:00
|
|
|
Locale::setLanguageFromJSON('nn', __DIR__.'/config/locale/translations/nn.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('no', __DIR__.'/config/locale/translations/no.json');
|
2021-07-29 07:25:34 +12:00
|
|
|
Locale::setLanguageFromJSON('or', __DIR__.'/config/locale/translations/or.json');
|
2021-07-30 19:25:07 +12:00
|
|
|
Locale::setLanguageFromJSON('pa', __DIR__.'/config/locale/translations/pa.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('pl', __DIR__.'/config/locale/translations/pl.json');
|
|
|
|
Locale::setLanguageFromJSON('pt-br', __DIR__.'/config/locale/translations/pt-br.json');
|
|
|
|
Locale::setLanguageFromJSON('pt-pt', __DIR__.'/config/locale/translations/pt-pt.json');
|
|
|
|
Locale::setLanguageFromJSON('ro', __DIR__.'/config/locale/translations/ro.json');
|
|
|
|
Locale::setLanguageFromJSON('ru', __DIR__ . '/config/locale/translations/ru.json');
|
2021-10-06 01:20:20 +13:00
|
|
|
Locale::setLanguageFromJSON('sa', __DIR__ . '/config/locale/translations/sa.json');
|
2021-10-08 06:18:47 +13:00
|
|
|
Locale::setLanguageFromJSON('sd', __DIR__ . '/config/locale/translations/sd.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('si', __DIR__ . '/config/locale/translations/si.json');
|
2021-10-06 09:28:01 +13:00
|
|
|
Locale::setLanguageFromJSON('sk', __DIR__ . '/config/locale/translations/sk.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('sl', __DIR__ . '/config/locale/translations/sl.json');
|
|
|
|
Locale::setLanguageFromJSON('sq', __DIR__ . '/config/locale/translations/sq.json');
|
|
|
|
Locale::setLanguageFromJSON('sv', __DIR__ . '/config/locale/translations/sv.json');
|
|
|
|
Locale::setLanguageFromJSON('ta', __DIR__ . '/config/locale/translations/ta.json');
|
|
|
|
Locale::setLanguageFromJSON('th', __DIR__.'/config/locale/translations/th.json');
|
2021-07-29 07:55:25 +12:00
|
|
|
Locale::setLanguageFromJSON('tl', __DIR__.'/config/locale/translations/tl.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('tr', __DIR__.'/config/locale/translations/tr.json');
|
2021-07-29 07:49:52 +12:00
|
|
|
Locale::setLanguageFromJSON('uk', __DIR__.'/config/locale/translations/uk.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
Locale::setLanguageFromJSON('ur', __DIR__.'/config/locale/translations/ur.json');
|
|
|
|
Locale::setLanguageFromJSON('vi', __DIR__.'/config/locale/translations/vi.json');
|
|
|
|
Locale::setLanguageFromJSON('zh-cn', __DIR__.'/config/locale/translations/zh-cn.json');
|
|
|
|
Locale::setLanguageFromJSON('zh-tw', __DIR__.'/config/locale/translations/zh-tw.json');
|
2019-10-09 17:33:33 +13:00
|
|
|
|
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-06-30 23:09:28 +12:00
|
|
|
App::getEnv('_APP_VERSION', 'UNKNOWN'),
|
2020-06-29 05:31:21 +12:00
|
|
|
App::getEnv('_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
|
|
|
]);
|
|
|
|
|
2020-06-30 16:34:13 +12:00
|
|
|
// Runtime Execution
|
|
|
|
|
|
|
|
App::setResource('register', function() use ($register) {
|
|
|
|
return $register;
|
|
|
|
});
|
|
|
|
|
|
|
|
App::setResource('layout', function($locale) {
|
|
|
|
$layout = new View(__DIR__.'/views/layouts/default.phtml');
|
|
|
|
$layout->setParam('locale', $locale);
|
|
|
|
|
|
|
|
return $layout;
|
|
|
|
}, ['locale']);
|
|
|
|
|
|
|
|
App::setResource('locale', function() {
|
2021-04-01 19:14:23 +13:00
|
|
|
return new Locale(App::getEnv('_APP_LOCALE', 'en'));
|
2020-06-30 16:34:13 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
// Queues
|
2020-12-07 11:14:57 +13:00
|
|
|
App::setResource('events', function($register) {
|
2020-12-07 21:51:12 +13:00
|
|
|
return new Event('', '');
|
2020-06-30 16:34:13 +12:00
|
|
|
}, ['register']);
|
|
|
|
|
2020-07-06 02:19:59 +12:00
|
|
|
App::setResource('audits', function($register) {
|
2020-12-23 01:51:46 +13:00
|
|
|
return new Event(Event::AUDITS_QUEUE_NAME, Event::AUDITS_CLASS_NAME);
|
2020-06-30 16:34:13 +12:00
|
|
|
}, ['register']);
|
|
|
|
|
|
|
|
App::setResource('usage', function($register) {
|
2020-12-23 01:51:46 +13:00
|
|
|
return new Event(Event::USAGE_QUEUE_NAME, Event::USAGE_CLASS_NAME);
|
2020-06-30 16:34:13 +12:00
|
|
|
}, ['register']);
|
|
|
|
|
2020-07-06 02:19:59 +12:00
|
|
|
App::setResource('mails', function($register) {
|
2020-12-23 01:51:46 +13:00
|
|
|
return new Event(Event::MAILS_QUEUE_NAME, Event::MAILS_CLASS_NAME);
|
2020-06-30 16:34:13 +12:00
|
|
|
}, ['register']);
|
|
|
|
|
|
|
|
App::setResource('deletes', function($register) {
|
2020-12-23 01:51:46 +13:00
|
|
|
return new Event(Event::DELETE_QUEUE_NAME, Event::DELETE_CLASS_NAME);
|
2020-06-30 16:34:13 +12:00
|
|
|
}, ['register']);
|
|
|
|
|
|
|
|
// Test Mock
|
2021-01-13 05:36:21 +13:00
|
|
|
App::setResource('clients', function($request, $console, $project) {
|
|
|
|
$console->setAttribute('platforms', [ // Allways allow current host
|
|
|
|
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
|
|
|
|
'name' => 'Current Host',
|
|
|
|
'type' => 'web',
|
|
|
|
'hostname' => $request->getHostname(),
|
|
|
|
], Document::SET_TYPE_APPEND);
|
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}))));
|
|
|
|
|
|
|
|
return $clients;
|
2021-01-13 05:36:21 +13:00
|
|
|
}, ['request', 'console', 'project']);
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
App::setResource('user', function($mode, $project, $console, $request, $response, $projectDB, $consoleDB) {
|
2020-10-30 02:07:56 +13:00
|
|
|
/** @var Utopia\Swoole\Request $request */
|
2020-10-30 02:50:49 +13:00
|
|
|
/** @var Appwrite\Utopia\Response $response */
|
2020-07-01 20:55:14 +12:00
|
|
|
/** @var Appwrite\Database\Document $project */
|
|
|
|
/** @var Appwrite\Database\Database $consoleDB */
|
|
|
|
/** @var Appwrite\Database\Database $projectDB */
|
2021-04-01 23:59:11 +13:00
|
|
|
/** @var string $mode */
|
2020-07-01 06:08:02 +12:00
|
|
|
|
2020-07-03 18:08:37 +12:00
|
|
|
Authorization::setDefaultStatus(true);
|
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
Auth::setCookieName('a_session_'.$project->getId());
|
|
|
|
|
|
|
|
if (APP_MODE_ADMIN === $mode) {
|
|
|
|
Auth::setCookieName('a_session_'.$console->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
$session = Auth::decodeSession(
|
|
|
|
$request->getCookie(Auth::$cookieName, // Get sessions
|
2020-11-20 19:48:15 +13:00
|
|
|
$request->getCookie(Auth::$cookieName.'_legacy', '')));// Get fallback session from old clients (no SameSite support)
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
// Get fallback session from clients who block 3rd-party cookies
|
2020-10-21 23:50:11 +13:00
|
|
|
if($response) $response->addHeader('X-Debug-Fallback', 'false');
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
if(empty($session['id']) && empty($session['secret'])) {
|
2020-10-21 23:50:11 +13:00
|
|
|
if($response) $response->addHeader('X-Debug-Fallback', 'true');
|
2020-07-05 10:22:22 +12:00
|
|
|
$fallback = $request->getHeader('x-fallback-cookies', '');
|
2020-07-01 06:08:02 +12:00
|
|
|
$fallback = \json_decode($fallback, true);
|
|
|
|
$session = Auth::decodeSession(((isset($fallback[Auth::$cookieName])) ? $fallback[Auth::$cookieName] : ''));
|
|
|
|
}
|
2020-07-03 17:56:28 +12:00
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
Auth::$unique = $session['id'];
|
|
|
|
Auth::$secret = $session['secret'];
|
|
|
|
|
|
|
|
if (APP_MODE_ADMIN !== $mode) {
|
|
|
|
$user = $projectDB->getDocument(Auth::$unique);
|
2021-06-28 22:18:00 +12:00
|
|
|
} else {
|
2020-07-01 06:08:02 +12:00
|
|
|
$user = $consoleDB->getDocument(Auth::$unique);
|
|
|
|
|
|
|
|
$user
|
|
|
|
->setAttribute('$id', 'admin-'.$user->getAttribute('$id'))
|
|
|
|
;
|
|
|
|
}
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
if (empty($user->getId()) // Check a document has been found in the DB
|
|
|
|
|| Database::SYSTEM_COLLECTION_USERS !== $user->getCollection() // Validate returned document is really a user document
|
2021-02-20 01:12:47 +13:00
|
|
|
|| !Auth::sessionVerify($user->getAttribute('sessions', []), Auth::$secret)) { // Validate user has valid login token
|
2020-07-01 06:08:02 +12:00
|
|
|
$user = new Document(['$id' => '', '$collection' => Database::SYSTEM_COLLECTION_USERS]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (APP_MODE_ADMIN === $mode) {
|
|
|
|
if (!empty($user->search('teamId', $project->getAttribute('teamId'), $user->getAttribute('memberships')))) {
|
2020-07-03 17:56:28 +12:00
|
|
|
Authorization::setDefaultStatus(false); // Cancel security segmentation for admin users.
|
2020-07-01 06:08:02 +12:00
|
|
|
} else {
|
|
|
|
$user = new Document(['$id' => '', '$collection' => Database::SYSTEM_COLLECTION_USERS]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 09:31:42 +13:00
|
|
|
$authJWT = $request->getHeader('x-appwrite-jwt', '');
|
|
|
|
|
|
|
|
if (!empty($authJWT)) { // JWT authentication
|
2020-12-29 10:23:09 +13:00
|
|
|
$jwt = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10); // Instantiate with key, algo, maxAge and leeway.
|
|
|
|
|
2020-12-29 09:31:42 +13:00
|
|
|
try {
|
|
|
|
$payload = $jwt->decode($authJWT);
|
|
|
|
} catch (JWTException $error) {
|
|
|
|
throw new Exception('Failed to verify JWT. '.$error->getMessage(), 401);
|
|
|
|
}
|
2021-08-17 20:58:33 +12:00
|
|
|
|
2020-12-29 09:31:42 +13:00
|
|
|
$jwtUserId = $payload['userId'] ?? '';
|
|
|
|
$jwtSessionId = $payload['sessionId'] ?? '';
|
|
|
|
|
|
|
|
if($jwtUserId && $jwtSessionId) {
|
|
|
|
$user = $projectDB->getDocument($jwtUserId);
|
|
|
|
}
|
|
|
|
|
2021-07-20 03:09:39 +12:00
|
|
|
if (empty($user->search('$id', $jwtSessionId, $user->getAttribute('sessions')))) { // Match JWT to active token
|
2020-12-29 09:31:42 +13:00
|
|
|
$user = new Document(['$id' => '', '$collection' => Database::SYSTEM_COLLECTION_USERS]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
return $user;
|
|
|
|
}, ['mode', 'project', 'console', 'request', 'response', 'projectDB', 'consoleDB']);
|
|
|
|
|
|
|
|
App::setResource('project', function($consoleDB, $request) {
|
2020-10-30 02:07:56 +13:00
|
|
|
/** @var Utopia\Swoole\Request $request */
|
2020-07-01 20:55:14 +12:00
|
|
|
/** @var Appwrite\Database\Database $consoleDB */
|
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
Authorization::disable();
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
$project = $consoleDB->getDocument($request->getParam('project',
|
2020-07-05 10:22:22 +12:00
|
|
|
$request->getHeader('x-appwrite-project', '')));
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2020-07-03 17:56:28 +12:00
|
|
|
Authorization::reset();
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
return $project;
|
|
|
|
}, ['consoleDB', 'request']);
|
|
|
|
|
|
|
|
App::setResource('console', function($consoleDB) {
|
|
|
|
return $consoleDB->getDocument('console');
|
|
|
|
}, ['consoleDB']);
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2021-06-23 07:51:14 +12:00
|
|
|
App::setResource('consoleDB', function($db, $cache) {
|
2020-06-30 16:34:13 +12:00
|
|
|
$consoleDB = new Database();
|
2021-06-23 07:51:14 +12:00
|
|
|
$consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache));
|
2020-06-30 16:34:13 +12:00
|
|
|
$consoleDB->setNamespace('app_console'); // Should be replaced with param if we want to have parent projects
|
|
|
|
$consoleDB->setMocks(Config::getParam('collections', []));
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
return $consoleDB;
|
2021-06-23 07:51:14 +12:00
|
|
|
}, ['db', 'cache']);
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2021-06-23 07:51:14 +12:00
|
|
|
App::setResource('projectDB', function($db, $cache, $project) {
|
2020-07-01 06:08:02 +12:00
|
|
|
$projectDB = new Database();
|
2021-06-23 07:51:14 +12:00
|
|
|
$projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache));
|
2020-07-01 06:08:02 +12:00
|
|
|
$projectDB->setNamespace('app_'.$project->getId());
|
|
|
|
$projectDB->setMocks(Config::getParam('collections', []));
|
|
|
|
|
|
|
|
return $projectDB;
|
2021-06-23 07:51:14 +12:00
|
|
|
}, ['db', 'cache', 'project']);
|
2020-06-27 00:27:58 +12:00
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
App::setResource('mode', function($request) {
|
2020-10-30 02:07:56 +13:00
|
|
|
/** @var Utopia\Swoole\Request $request */
|
2020-11-19 11:08:45 +13:00
|
|
|
return $request->getParam('mode', $request->getHeader('x-appwrite-mode', APP_MODE_DEFAULT));
|
2020-07-01 06:08:02 +12:00
|
|
|
}, ['request']);
|
2020-07-03 08:24:14 +12:00
|
|
|
|
2020-10-30 04:44:21 +13:00
|
|
|
App::setResource('geodb', function($register) {
|
2020-10-30 04:54:36 +13:00
|
|
|
/** @var Utopia\Registry\Registry $register */
|
2020-10-30 03:08:09 +13:00
|
|
|
return $register->get('geodb');
|
2020-10-30 04:54:36 +13:00
|
|
|
}, ['register']);
|
2021-03-10 20:42:45 +13:00
|
|
|
|
|
|
|
App::setResource('schema', function($utopia, $response, $request, $register) {
|
|
|
|
$schema = null;
|
|
|
|
try {
|
|
|
|
/*
|
2021-03-19 08:26:45 +13:00
|
|
|
* Try to get the schema from the register.
|
|
|
|
* If there is no schema catch the exception and generate it.
|
2021-03-10 20:42:45 +13:00
|
|
|
*/
|
2021-03-19 09:43:49 +13:00
|
|
|
Console::log('Getting Schema from register...');
|
2021-03-10 20:42:45 +13:00
|
|
|
$schema = $register->get('_schema');
|
|
|
|
} catch (Exception $e) {
|
2021-03-19 09:43:49 +13:00
|
|
|
Console::error('Schema not present. Generating Schema...');
|
2021-03-12 04:44:04 +13:00
|
|
|
$schema = Builder::buildSchema($utopia, $response, $register);
|
2021-03-19 09:43:49 +13:00
|
|
|
$register->set('_schema', function () use ($schema){
|
2021-03-10 20:42:45 +13:00
|
|
|
return $schema;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $schema;
|
|
|
|
}, ['utopia', 'response', 'request', 'register']);
|