2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 17:34:06 +13:00
|
|
|
/**
|
|
|
|
* Init
|
2022-05-24 02:54:50 +12:00
|
|
|
*
|
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
|
2022-05-24 02:54:50 +12:00
|
|
|
*
|
2020-03-25 17:34:06 +13:00
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
|
|
|
|
if (\file_exists(__DIR__ . '/../vendor/autoload.php')) {
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
2019-08-01 08:35:42 +12:00
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
ini_set('memory_limit', '512M');
|
2021-06-12 04:09:46 +12:00
|
|
|
ini_set('display_errors', 1);
|
|
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
ini_set('default_socket_timeout', -1);
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
2021-08-13 20:39:46 +12:00
|
|
|
use Appwrite\Extend\PDO;
|
2020-12-29 09:31:42 +13:00
|
|
|
use Ahc\Jwt\JWT;
|
|
|
|
use Ahc\Jwt\JWTException;
|
2022-02-07 10:27:16 +13:00
|
|
|
use Appwrite\Extend\Exception;
|
2020-07-01 06:08:02 +12:00
|
|
|
use Appwrite\Auth\Auth;
|
2022-08-15 01:43:41 +12:00
|
|
|
use Appwrite\SMS\Adapter\Mock;
|
|
|
|
use Appwrite\SMS\Adapter\Telesign;
|
|
|
|
use Appwrite\SMS\Adapter\TextMagic;
|
|
|
|
use Appwrite\SMS\Adapter\Twilio;
|
|
|
|
use Appwrite\SMS\Adapter\Msg91;
|
|
|
|
use Appwrite\SMS\Adapter\Vonage;
|
2022-06-21 00:22:35 +12:00
|
|
|
use Appwrite\DSN\DSN;
|
2022-04-14 00:39:31 +12:00
|
|
|
use Appwrite\Event\Audit;
|
|
|
|
use Appwrite\Event\Database as EventDatabase;
|
|
|
|
use Appwrite\Event\Delete;
|
2020-06-28 07:42:38 +12:00
|
|
|
use Appwrite\Event\Event;
|
2022-04-14 00:39:31 +12:00
|
|
|
use Appwrite\Event\Mail;
|
2022-06-09 01:57:34 +12:00
|
|
|
use Appwrite\Event\Phone;
|
2021-07-24 02:59:55 +12:00
|
|
|
use Appwrite\Network\Validator\Email;
|
|
|
|
use Appwrite\Network\Validator\IP;
|
|
|
|
use Appwrite\Network\Validator\URL;
|
2020-05-13 10:00:00 +12:00
|
|
|
use Appwrite\OpenSSL\OpenSSL;
|
2022-08-09 18:28:38 +12:00
|
|
|
use Appwrite\Usage\Stats;
|
2021-12-14 22:11:34 +13:00
|
|
|
use Appwrite\Utopia\View;
|
2020-07-02 10:34:05 +12:00
|
|
|
use Utopia\App;
|
2022-08-14 22:33:36 +12:00
|
|
|
use Utopia\Database\ID;
|
2021-11-23 05:12:18 +13:00
|
|
|
use Utopia\Logger\Logger;
|
2020-07-02 10:34:05 +12:00
|
|
|
use Utopia\Config\Config;
|
|
|
|
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-05-04 07:22:39 +12:00
|
|
|
use Utopia\Cache\Adapter\Redis as RedisCache;
|
2021-05-03 20:28:31 +12:00
|
|
|
use Utopia\Cache\Cache;
|
|
|
|
use Utopia\Database\Adapter\MariaDB;
|
2021-08-20 23:15:17 +12:00
|
|
|
use Utopia\Database\Document;
|
2021-08-22 03:09:08 +12:00
|
|
|
use Utopia\Database\Database;
|
2021-07-24 02:59:55 +12:00
|
|
|
use Utopia\Database\Validator\Structure;
|
2021-07-26 02:51:04 +12:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2021-07-27 11:10:39 +12:00
|
|
|
use Utopia\Validator\Range;
|
2021-09-11 08:12:22 +12:00
|
|
|
use Utopia\Validator\WhiteList;
|
2021-06-28 19:19:33 +12:00
|
|
|
use Swoole\Database\PDOConfig;
|
|
|
|
use Swoole\Database\PDOPool;
|
|
|
|
use Swoole\Database\RedisConfig;
|
|
|
|
use Swoole\Database\RedisPool;
|
2021-08-20 23:15:17 +12:00
|
|
|
use Utopia\Database\Query;
|
2022-07-29 00:38:54 +12:00
|
|
|
use Utopia\Database\Validator\DatetimeValidator;
|
2022-02-17 22:13:39 +13:00
|
|
|
use Utopia\Storage\Device;
|
2021-12-13 20:34:15 +13:00
|
|
|
use Utopia\Storage\Storage;
|
2022-05-10 23:15:56 +12:00
|
|
|
use Utopia\Storage\Device\Backblaze;
|
2022-02-09 04:40:56 +13:00
|
|
|
use Utopia\Storage\Device\DOSpaces;
|
2021-12-13 20:34:15 +13:00
|
|
|
use Utopia\Storage\Device\Local;
|
|
|
|
use Utopia\Storage\Device\S3;
|
2022-03-19 05:03:04 +13:00
|
|
|
use Utopia\Storage\Device\Linode;
|
2022-03-19 06:17:43 +13:00
|
|
|
use Utopia\Storage\Device\Wasabi;
|
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
|
2022-05-24 02:54:50 +12: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-05-10 06:37:47 +12:00
|
|
|
const APP_LIMIT_COUNT = 5000;
|
|
|
|
const APP_LIMIT_USERS = 10000;
|
2022-01-31 21:37:45 +13:00
|
|
|
const APP_LIMIT_ANTIVIRUS = 20000000; //20MB
|
|
|
|
const APP_LIMIT_ENCRYPTION = 20000000; //20MB
|
|
|
|
const APP_LIMIT_COMPRESSION = 20000000; //20MB
|
2022-05-01 19:54:58 +12:00
|
|
|
const APP_LIMIT_ARRAY_PARAMS_SIZE = 100; // Default maximum of how many elements can there be in API parameter that expects array value
|
2022-06-22 21:08:11 +12:00
|
|
|
const APP_LIMIT_ARRAY_ELEMENT_SIZE = 4096; // Default maximum length of element in array parameter represented by maximum URL length.
|
2022-05-31 00:01:39 +12:00
|
|
|
const APP_LIMIT_SUBQUERY = 1000;
|
2022-08-31 11:34:17 +12:00
|
|
|
const APP_LIMIT_WRITE_RATE_DEFAULT = 60; // Default maximum write rate per rate period
|
|
|
|
const APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT = 60; // Default maximum write rate period in seconds
|
2022-08-09 17:56:53 +12:00
|
|
|
const APP_KEY_ACCCESS = 24 * 60 * 60; // 24 hours
|
2022-09-01 01:11:23 +12:00
|
|
|
const APP_CACHE_UPDATE = 24 * 60 * 60; // 24 hours
|
2022-09-30 23:27:26 +13:00
|
|
|
const APP_CACHE_BUSTER = 501;
|
2022-10-12 23:03:55 +13:00
|
|
|
const APP_VERSION_STABLE = '1.0.3';
|
2021-08-17 10:05:52 +12:00
|
|
|
const APP_DATABASE_ATTRIBUTE_EMAIL = 'email';
|
2021-09-11 08:12:22 +12:00
|
|
|
const APP_DATABASE_ATTRIBUTE_ENUM = 'enum';
|
2021-08-17 10:05:52 +12:00
|
|
|
const APP_DATABASE_ATTRIBUTE_IP = 'ip';
|
2022-07-25 20:53:41 +12:00
|
|
|
const APP_DATABASE_ATTRIBUTE_DATETIME = 'datetime';
|
2021-08-17 10:05:52 +12:00
|
|
|
const APP_DATABASE_ATTRIBUTE_URL = 'url';
|
|
|
|
const APP_DATABASE_ATTRIBUTE_INT_RANGE = 'intRange';
|
|
|
|
const APP_DATABASE_ATTRIBUTE_FLOAT_RANGE = 'floatRange';
|
2021-10-27 09:28:16 +13:00
|
|
|
const APP_DATABASE_ATTRIBUTE_STRING_MAX_LENGTH = 1073741824; // 2^32 bits / 4 bits per char
|
2022-11-09 16:21:27 +13:00
|
|
|
const APP_DATABASE_DEFAULT_POOL_SIZE = 64;
|
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';
|
2021-12-09 04:08:53 +13:00
|
|
|
const APP_STORAGE_BUILDS = '/storage/builds';
|
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';
|
2022-01-31 21:37:45 +13:00
|
|
|
const APP_STORAGE_READ_BUFFER = 20 * (1000 * 1000); //20MB other names `APP_STORAGE_MEMORY_LIMIT`, `APP_STORAGE_MEMORY_BUFFER`, `APP_STORAGE_READ_LIMIT`, `APP_STORAGE_BUFFER_LIMIT`
|
2021-12-12 09:17:33 +13:00
|
|
|
const APP_SOCIAL_TWITTER = 'https://twitter.com/appwrite';
|
|
|
|
const APP_SOCIAL_TWITTER_HANDLE = 'appwrite';
|
2020-02-21 09:43:06 +13:00
|
|
|
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';
|
2022-05-24 02:54:50 +12:00
|
|
|
const APP_SOCIAL_STACKSHARE = 'https://stackshare.io/appwrite';
|
2021-12-11 13:17:28 +13:00
|
|
|
const APP_SOCIAL_YOUTUBE = 'https://www.youtube.com/c/appwrite?sub_confirmation=1';
|
2022-01-22 06:32:27 +13:00
|
|
|
// Database Reconnect
|
|
|
|
const DATABASE_RECONNECT_SLEEP = 2;
|
|
|
|
const DATABASE_RECONNECT_MAX_ATTEMPTS = 10;
|
2021-08-09 11:42:08 +12:00
|
|
|
// Database Worker Types
|
|
|
|
const DATABASE_TYPE_CREATE_ATTRIBUTE = 'createAttribute';
|
|
|
|
const DATABASE_TYPE_CREATE_INDEX = 'createIndex';
|
|
|
|
const DATABASE_TYPE_DELETE_ATTRIBUTE = 'deleteAttribute';
|
|
|
|
const DATABASE_TYPE_DELETE_INDEX = 'deleteIndex';
|
2022-01-24 12:01:42 +13:00
|
|
|
// Build Worker Types
|
2022-01-27 02:09:32 +13:00
|
|
|
const BUILD_TYPE_DEPLOYMENT = 'deployment';
|
2022-01-24 12:01:42 +13:00
|
|
|
const BUILD_TYPE_RETRY = 'retry';
|
2020-12-19 03:05:15 +13:00
|
|
|
// Deletion Types
|
2022-06-22 22:51:49 +12:00
|
|
|
const DELETE_TYPE_DATABASES = 'databases';
|
2020-12-19 03:05:15 +13:00
|
|
|
const DELETE_TYPE_DOCUMENT = 'document';
|
2021-10-26 13:14:55 +13:00
|
|
|
const DELETE_TYPE_COLLECTIONS = 'collections';
|
|
|
|
const DELETE_TYPE_PROJECTS = 'projects';
|
|
|
|
const DELETE_TYPE_FUNCTIONS = 'functions';
|
2022-01-28 12:36:30 +13:00
|
|
|
const DELETE_TYPE_DEPLOYMENTS = 'deployments';
|
2021-10-26 13:14:55 +13:00
|
|
|
const DELETE_TYPE_USERS = 'users';
|
2022-05-24 02:54:50 +12:00
|
|
|
const DELETE_TYPE_TEAMS = 'teams';
|
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-09-08 18:55:48 +12:00
|
|
|
const DELETE_TYPE_USAGE = 'usage';
|
2021-06-16 21:35:37 +12:00
|
|
|
const DELETE_TYPE_REALTIME = 'realtime';
|
2021-11-07 18:54:28 +13:00
|
|
|
const DELETE_TYPE_BUCKETS = 'buckets';
|
2022-06-02 22:20:03 +12:00
|
|
|
const DELETE_TYPE_SESSIONS = 'sessions';
|
2022-08-15 21:05:41 +12:00
|
|
|
const DELETE_TYPE_CACHE_BY_TIMESTAMP = 'cacheByTimeStamp';
|
|
|
|
const DELETE_TYPE_CACHE_BY_RESOURCE = 'cacheByResource';
|
2022-08-31 21:51:20 +12:00
|
|
|
// Compression type
|
|
|
|
const COMPRESSION_TYPE_NONE = 'none';
|
|
|
|
const COMPRESSION_TYPE_GZIP = 'gzip';
|
|
|
|
const COMPRESSION_TYPE_ZSTD = 'zstd';
|
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';
|
2022-04-14 00:26:07 +12:00
|
|
|
const MAIL_TYPE_CERTIFICATE = 'certificate';
|
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';
|
2021-09-30 21:41:29 +13:00
|
|
|
// Response related
|
2022-05-24 02:54:50 +12:00
|
|
|
const MAX_OUTPUT_CHUNK_SIZE = 2 * 1024 * 1024; // 2MB
|
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
|
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
Config::load('events', __DIR__ . '/config/events.php');
|
|
|
|
Config::load('auth', __DIR__ . '/config/auth.php');
|
|
|
|
Config::load('errors', __DIR__ . '/config/errors.php');
|
|
|
|
Config::load('providers', __DIR__ . '/config/providers.php');
|
|
|
|
Config::load('platforms', __DIR__ . '/config/platforms.php');
|
|
|
|
Config::load('collections', __DIR__ . '/config/collections.php');
|
|
|
|
Config::load('runtimes', __DIR__ . '/config/runtimes.php');
|
|
|
|
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
|
|
|
|
Config::load('variables', __DIR__ . '/config/variables.php'); // List of env variables
|
|
|
|
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');
|
|
|
|
Config::load('locale-countries', __DIR__ . '/config/locale/countries.php');
|
|
|
|
Config::load('locale-continents', __DIR__ . '/config/locale/continents.php');
|
|
|
|
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');
|
|
|
|
|
|
|
|
$user = App::getEnv('_APP_REDIS_USER', '');
|
|
|
|
$pass = App::getEnv('_APP_REDIS_PASS', '');
|
|
|
|
if (!empty($user) || !empty($pass)) {
|
|
|
|
Resque::setBackend('redis://' . $user . ':' . $pass . '@' . App::getEnv('_APP_REDIS_HOST', '') . ':' . App::getEnv('_APP_REDIS_PORT', ''));
|
2021-01-31 18:24:38 +13:00
|
|
|
} else {
|
2022-05-24 02:54:50 +12:00
|
|
|
Resque::setBackend(App::getEnv('_APP_REDIS_HOST', '') . ':' . App::getEnv('_APP_REDIS_PORT', ''));
|
2021-01-31 18:24:38 +13:00
|
|
|
}
|
2021-08-20 23:15:17 +12:00
|
|
|
|
2021-08-28 05:12:16 +12:00
|
|
|
/**
|
|
|
|
* New DB Filters
|
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'casting',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2022-04-22 01:14:01 +12:00
|
|
|
return json_encode(['value' => $value], JSON_PRESERVE_ZERO_FRACTION);
|
2021-08-28 05:12:16 +12:00
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-08-28 11:42:53 +12:00
|
|
|
if (is_null($value)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-31 00:01:39 +12:00
|
|
|
|
2021-08-28 08:27:48 +12:00
|
|
|
return json_decode($value, true)['value'];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'enum',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $attribute) {
|
2021-09-11 08:14:12 +12:00
|
|
|
if ($attribute->isSet('elements')) {
|
|
|
|
$attribute->removeAttribute('elements');
|
|
|
|
}
|
2022-05-31 00:01:39 +12:00
|
|
|
|
2021-09-11 08:14:12 +12:00
|
|
|
return $value;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $attribute) {
|
2021-12-09 06:49:44 +13:00
|
|
|
$formatOptions = json_decode($attribute->getAttribute('formatOptions', '[]'), true);
|
2021-09-11 08:14:12 +12:00
|
|
|
if (isset($formatOptions['elements'])) {
|
|
|
|
$attribute->setAttribute('elements', $formatOptions['elements']);
|
|
|
|
}
|
2022-05-31 00:01:39 +12:00
|
|
|
|
2021-09-11 08:14:12 +12:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'range',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $attribute) {
|
2021-08-28 08:27:48 +12:00
|
|
|
if ($attribute->isSet('min')) {
|
|
|
|
$attribute->removeAttribute('min');
|
|
|
|
}
|
|
|
|
if ($attribute->isSet('max')) {
|
|
|
|
$attribute->removeAttribute('max');
|
|
|
|
}
|
2022-05-31 00:01:39 +12:00
|
|
|
|
2021-08-28 08:27:48 +12:00
|
|
|
return $value;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $attribute) {
|
2021-12-09 06:49:44 +13:00
|
|
|
$formatOptions = json_decode($attribute->getAttribute('formatOptions', '[]'), true);
|
2021-08-28 08:27:48 +12:00
|
|
|
if (isset($formatOptions['min']) || isset($formatOptions['max'])) {
|
|
|
|
$attribute
|
|
|
|
->setAttribute('min', $formatOptions['min'])
|
|
|
|
->setAttribute('max', $formatOptions['max'])
|
|
|
|
;
|
|
|
|
}
|
2022-05-31 00:01:39 +12:00
|
|
|
|
2021-08-28 08:27:48 +12:00
|
|
|
return $value;
|
2021-08-28 05:12:16 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryAttributes',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-08-21 05:02:44 +12:00
|
|
|
return null;
|
2021-08-20 23:15:17 +12:00
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2021-08-20 23:15:17 +12:00
|
|
|
return $database
|
|
|
|
->find('attributes', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('collectionInternalId', [$document->getInternalId()]),
|
|
|
|
Query::equal('databaseInternalId', [$document->getAttribute('databaseInternalId')]),
|
2022-10-15 00:24:37 +13:00
|
|
|
Query::limit($database->getLimitForAttributes()),
|
2022-08-12 11:53:52 +12:00
|
|
|
]);
|
2021-08-20 23:15:17 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryIndexes',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-08-23 03:00:00 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2021-08-23 03:00:00 +12:00
|
|
|
return $database
|
|
|
|
->find('indexes', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('collectionInternalId', [$document->getInternalId()]),
|
|
|
|
Query::equal('databaseInternalId', [$document->getAttribute('databaseInternalId')]),
|
|
|
|
Query::limit(64),
|
|
|
|
]);
|
2021-08-23 03:00:00 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryPlatforms',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-08-30 19:33:45 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2021-08-30 19:33:45 +12:00
|
|
|
return $database
|
2021-09-01 20:43:35 +12:00
|
|
|
->find('platforms', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('projectInternalId', [$document->getInternalId()]),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]);
|
2021-08-31 02:24:50 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryDomains',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-08-31 02:24:50 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2021-08-31 02:24:50 +12:00
|
|
|
return $database
|
2021-09-01 20:43:35 +12:00
|
|
|
->find('domains', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('projectInternalId', [$document->getInternalId()]),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]);
|
2021-08-31 02:24:50 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryKeys',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-08-31 02:24:50 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2021-08-31 02:24:50 +12:00
|
|
|
return $database
|
2021-09-01 20:43:35 +12:00
|
|
|
->find('keys', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('projectInternalId', [$document->getInternalId()]),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]);
|
2021-08-31 02:24:50 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryWebhooks',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-08-31 02:24:50 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2021-08-31 02:24:50 +12:00
|
|
|
return $database
|
2021-09-01 20:43:35 +12:00
|
|
|
->find('webhooks', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('projectInternalId', [$document->getInternalId()]),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]);
|
2021-08-23 03:00:00 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQuerySessions',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2022-04-04 21:59:32 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2022-06-02 05:13:15 +12:00
|
|
|
return Authorization::skip(fn () => $database->find('sessions', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('userInternalId', [$document->getInternalId()]),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]));
|
2022-04-04 21:59:32 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryTokens',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2022-04-27 23:06:53 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2022-04-28 02:11:12 +12:00
|
|
|
return Authorization::skip(fn() => $database
|
2022-04-27 23:06:53 +12:00
|
|
|
->find('tokens', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('userInternalId', [$document->getInternalId()]),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]));
|
2022-05-09 02:39:24 +12:00
|
|
|
}
|
|
|
|
);
|
2022-05-31 00:01:39 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryMemberships',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2022-04-28 00:44:47 +12:00
|
|
|
return null;
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2022-04-28 00:44:47 +12:00
|
|
|
return Authorization::skip(fn() => $database
|
|
|
|
->find('memberships', [
|
2022-08-12 11:53:52 +12:00
|
|
|
Query::equal('userInternalId', [$document->getInternalId()]),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]));
|
2021-08-23 03:00:00 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-07-20 19:18:49 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'subQueryVariables',
|
|
|
|
function (mixed $value) {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function (mixed $value, Document $document, Database $database) {
|
2022-08-10 00:32:33 +12:00
|
|
|
return $database
|
2022-07-20 19:18:49 +12:00
|
|
|
->find('variables', [
|
2022-08-25 02:10:56 +12:00
|
|
|
Query::equal('functionInternalId', [$document->getInternalId()]),
|
2022-09-03 02:19:36 +12:00
|
|
|
Query::limit(APP_LIMIT_SUBQUERY),
|
|
|
|
]);
|
2022-07-20 19:18:49 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Database::addFilter(
|
|
|
|
'encrypt',
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2021-05-05 09:25:17 +12:00
|
|
|
$key = App::getEnv('_APP_OPENSSL_KEY_V1');
|
|
|
|
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
|
|
|
$tag = null;
|
2022-05-31 00:01:39 +12:00
|
|
|
|
2021-05-05 09:25:17 +12:00
|
|
|
return json_encode([
|
|
|
|
'data' => OpenSSL::encrypt($value, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
|
|
|
|
'method' => OpenSSL::CIPHER_AES_128_GCM,
|
2021-12-15 01:10:38 +13:00
|
|
|
'iv' => \bin2hex($iv),
|
|
|
|
'tag' => \bin2hex($tag ?? ''),
|
2021-05-05 09:25:17 +12:00
|
|
|
'version' => '1',
|
|
|
|
]);
|
|
|
|
},
|
2022-06-02 02:32:59 +12:00
|
|
|
function (mixed $value) {
|
2022-05-24 02:54:50 +12:00
|
|
|
if (is_null($value)) {
|
2021-12-03 20:38:21 +13:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-05 09:25:17 +12:00
|
|
|
$value = json_decode($value, true);
|
2022-05-24 02:54:50 +12:00
|
|
|
$key = App::getEnv('_APP_OPENSSL_KEY_V' . $value['version']);
|
2021-05-05 09:25:17 +12:00
|
|
|
|
|
|
|
return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag']));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-08-20 23:15:17 +12:00
|
|
|
/**
|
|
|
|
* DB Formats
|
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
Structure::addFormat(APP_DATABASE_ATTRIBUTE_EMAIL, function () {
|
2021-07-27 11:10:39 +12:00
|
|
|
return new Email();
|
2021-08-22 03:09:08 +12:00
|
|
|
}, Database::VAR_STRING);
|
2021-07-27 11:10:39 +12:00
|
|
|
|
2022-07-29 00:38:54 +12:00
|
|
|
Structure::addFormat(APP_DATABASE_ATTRIBUTE_DATETIME, function () {
|
|
|
|
return new DatetimeValidator();
|
|
|
|
}, Database::VAR_DATETIME);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Structure::addFormat(APP_DATABASE_ATTRIBUTE_ENUM, function ($attribute) {
|
2021-09-11 08:12:22 +12:00
|
|
|
$elements = $attribute['formatOptions']['elements'];
|
2021-12-16 23:15:55 +13:00
|
|
|
return new WhiteList($elements, true);
|
2021-09-11 08:12:22 +12:00
|
|
|
}, Database::VAR_STRING);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Structure::addFormat(APP_DATABASE_ATTRIBUTE_IP, function () {
|
2021-07-27 11:10:39 +12:00
|
|
|
return new IP();
|
2021-08-22 03:09:08 +12:00
|
|
|
}, Database::VAR_STRING);
|
2021-07-27 11:10:39 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Structure::addFormat(APP_DATABASE_ATTRIBUTE_URL, function () {
|
2021-07-27 11:10:39 +12:00
|
|
|
return new URL();
|
2021-08-22 03:09:08 +12:00
|
|
|
}, Database::VAR_STRING);
|
2021-07-27 11:10:39 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Structure::addFormat(APP_DATABASE_ATTRIBUTE_INT_RANGE, function ($attribute) {
|
2021-08-22 03:09:08 +12:00
|
|
|
$min = $attribute['formatOptions']['min'] ?? -INF;
|
|
|
|
$max = $attribute['formatOptions']['max'] ?? INF;
|
|
|
|
return new Range($min, $max, Range::TYPE_INTEGER);
|
|
|
|
}, Database::VAR_INTEGER);
|
2021-07-28 06:19:37 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Structure::addFormat(APP_DATABASE_ATTRIBUTE_FLOAT_RANGE, function ($attribute) {
|
2021-08-22 03:09:08 +12:00
|
|
|
$min = $attribute['formatOptions']['min'] ?? -INF;
|
|
|
|
$max = $attribute['formatOptions']['max'] ?? INF;
|
|
|
|
return new Range($min, $max, Range::TYPE_FLOAT);
|
|
|
|
}, Database::VAR_FLOAT);
|
2021-07-24 02:59:55 +12:00
|
|
|
|
2019-10-01 17:57:41 +13:00
|
|
|
/*
|
2019-05-09 18:54:39 +12:00
|
|
|
* Registry
|
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
$register->set('logger', function () {
|
2022-06-24 21:48:55 +12:00
|
|
|
// Register error logger
|
2021-11-23 05:12:18 +13:00
|
|
|
$providerName = App::getEnv('_APP_LOGGING_PROVIDER', '');
|
|
|
|
$providerConfig = App::getEnv('_APP_LOGGING_CONFIG', '');
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if (empty($providerName) || empty($providerConfig)) {
|
2021-12-07 02:14:55 +13:00
|
|
|
return null;
|
2021-11-23 05:12:18 +13:00
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if (!Logger::hasProvider($providerName)) {
|
2022-08-14 17:35:25 +12:00
|
|
|
throw new Exception(Exception::GENERAL_SERVER_ERROR, "Logging provider not supported. Logging is disabled");
|
2021-11-23 05:12:18 +13:00
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$classname = '\\Utopia\\Logger\\Adapter\\' . \ucfirst($providerName);
|
2021-11-23 05:12:18 +13:00
|
|
|
$adapter = new $classname($providerConfig);
|
|
|
|
return new Logger($adapter);
|
|
|
|
});
|
2022-11-09 16:21:27 +13:00
|
|
|
$register->set('dbPool', function ($size = APP_DATABASE_DEFAULT_POOL_SIZE) {
|
2022-05-24 02:54:50 +12:00
|
|
|
// Register DB connection
|
2020-06-29 05:31:21 +12:00
|
|
|
$dbHost = App::getEnv('_APP_DB_HOST', '');
|
2021-06-28 19:19:33 +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', '');
|
2021-06-28 19:19:33 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$pool = new PDOPool(
|
|
|
|
(new PDOConfig())
|
2021-06-28 19:19:33 +12:00
|
|
|
->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
|
2022-06-16 00:46:52 +12:00
|
|
|
PDO::ATTR_TIMEOUT => 3, // Seconds
|
|
|
|
PDO::ATTR_PERSISTENT => true,
|
2022-06-14 00:39:11 +12:00
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
|
|
PDO::ATTR_EMULATE_PREPARES => true,
|
2022-06-16 00:46:52 +12:00
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => true,
|
2022-05-24 02:54:50 +12:00
|
|
|
]),
|
2022-11-09 16:21:27 +13:00
|
|
|
$size
|
2022-05-24 02:54:50 +12:00
|
|
|
);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2021-06-12 04:09:46 +12:00
|
|
|
return $pool;
|
|
|
|
});
|
2022-11-09 16:21:27 +13:00
|
|
|
$register->set('redisPool', function ($size = APP_DATABASE_DEFAULT_POOL_SIZE) {
|
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-28 19:19:33 +12:00
|
|
|
$redisAuth = '';
|
2021-06-12 04:09:46 +12:00
|
|
|
|
2021-06-28 19:19:33 +12:00
|
|
|
if ($redisUser && $redisPass) {
|
2022-05-24 02:54:50 +12:00
|
|
|
$redisAuth = $redisUser . ':' . $redisPass;
|
2021-06-12 04:09:46 +12:00
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$pool = new RedisPool(
|
|
|
|
(new RedisConfig())
|
2021-06-28 19:19:33 +12:00
|
|
|
->withHost($redisHost)
|
|
|
|
->withPort($redisPort)
|
|
|
|
->withAuth($redisAuth)
|
2022-05-24 02:54:50 +12:00
|
|
|
->withDbIndex(0),
|
2022-11-09 16:21:27 +13:00
|
|
|
$size
|
2022-05-24 02:54:50 +12:00
|
|
|
);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2021-06-12 04:09:46 +12:00
|
|
|
return $pool;
|
2019-05-09 18:54:39 +12:00
|
|
|
});
|
2022-05-24 02:54:50 +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}");
|
|
|
|
$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;
|
|
|
|
});
|
2022-05-24 02:54:50 +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
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$from = \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server'));
|
2020-06-29 05:31:21 +12:00
|
|
|
$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 () {
|
2022-06-28 00:16:35 +12:00
|
|
|
return new Reader(__DIR__ . '/db/DBIP/dbip-country-lite-2022-06.mmdb');
|
2020-10-30 03:08:09 +13:00
|
|
|
});
|
2022-05-24 02:54:50 +12:00
|
|
|
$register->set('db', function () {
|
|
|
|
// This is usually for our workers or CLI commands scope
|
2021-08-13 20:39:46 +12:00
|
|
|
$dbHost = App::getEnv('_APP_DB_HOST', '');
|
2022-01-27 05:01:49 +13:00
|
|
|
$dbPort = App::getEnv('_APP_DB_PORT', '');
|
2021-08-13 20:39:46 +12:00
|
|
|
$dbUser = App::getEnv('_APP_DB_USER', '');
|
|
|
|
$dbPass = App::getEnv('_APP_DB_PASS', '');
|
|
|
|
$dbScheme = App::getEnv('_APP_DB_SCHEMA', '');
|
|
|
|
|
2022-01-27 05:01:49 +13:00
|
|
|
$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};dbname={$dbScheme};charset=utf8mb4", $dbUser, $dbPass, array(
|
2021-08-13 20:39:46 +12:00
|
|
|
PDO::ATTR_TIMEOUT => 3, // Seconds
|
|
|
|
PDO::ATTR_PERSISTENT => true,
|
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
2022-06-14 00:39:11 +12:00
|
|
|
PDO::ATTR_EMULATE_PREPARES => true,
|
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => true,
|
2021-08-13 20:39:46 +12:00
|
|
|
));
|
|
|
|
|
|
|
|
return $pdo;
|
|
|
|
});
|
2022-05-24 02:54:50 +12:00
|
|
|
$register->set('cache', function () {
|
|
|
|
// This is usually for our workers or CLI commands scope
|
2021-08-13 20:39:46 +12:00
|
|
|
$redis = new Redis();
|
|
|
|
$redis->pconnect(App::getEnv('_APP_REDIS_HOST', ''), App::getEnv('_APP_REDIS_PORT', ''));
|
|
|
|
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
|
|
|
|
|
|
|
|
return $redis;
|
|
|
|
});
|
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;
|
2022-05-24 02:54:50 +12:00
|
|
|
Locale::setLanguageFromJSON('af', __DIR__ . '/config/locale/translations/af.json');
|
|
|
|
Locale::setLanguageFromJSON('ar', __DIR__ . '/config/locale/translations/ar.json');
|
|
|
|
Locale::setLanguageFromJSON('as', __DIR__ . '/config/locale/translations/as.json');
|
|
|
|
Locale::setLanguageFromJSON('az', __DIR__ . '/config/locale/translations/az.json');
|
|
|
|
Locale::setLanguageFromJSON('be', __DIR__ . '/config/locale/translations/be.json');
|
|
|
|
Locale::setLanguageFromJSON('bg', __DIR__ . '/config/locale/translations/bg.json');
|
|
|
|
Locale::setLanguageFromJSON('bh', __DIR__ . '/config/locale/translations/bh.json');
|
|
|
|
Locale::setLanguageFromJSON('bn', __DIR__ . '/config/locale/translations/bn.json');
|
|
|
|
Locale::setLanguageFromJSON('bs', __DIR__ . '/config/locale/translations/bs.json');
|
|
|
|
Locale::setLanguageFromJSON('ca', __DIR__ . '/config/locale/translations/ca.json');
|
|
|
|
Locale::setLanguageFromJSON('cs', __DIR__ . '/config/locale/translations/cs.json');
|
|
|
|
Locale::setLanguageFromJSON('da', __DIR__ . '/config/locale/translations/da.json');
|
|
|
|
Locale::setLanguageFromJSON('de', __DIR__ . '/config/locale/translations/de.json');
|
|
|
|
Locale::setLanguageFromJSON('el', __DIR__ . '/config/locale/translations/el.json');
|
|
|
|
Locale::setLanguageFromJSON('en', __DIR__ . '/config/locale/translations/en.json');
|
|
|
|
Locale::setLanguageFromJSON('eo', __DIR__ . '/config/locale/translations/eo.json');
|
|
|
|
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');
|
|
|
|
Locale::setLanguageFromJSON('ga', __DIR__ . '/config/locale/translations/ga.json');
|
|
|
|
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');
|
|
|
|
Locale::setLanguageFromJSON('hr', __DIR__ . '/config/locale/translations/hr.json');
|
|
|
|
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');
|
|
|
|
Locale::setLanguageFromJSON('kn', __DIR__ . '/config/locale/translations/kn.json');
|
|
|
|
Locale::setLanguageFromJSON('km', __DIR__ . '/config/locale/translations/km.json');
|
|
|
|
Locale::setLanguageFromJSON('ko', __DIR__ . '/config/locale/translations/ko.json');
|
|
|
|
Locale::setLanguageFromJSON('la', __DIR__ . '/config/locale/translations/la.json');
|
|
|
|
Locale::setLanguageFromJSON('lb', __DIR__ . '/config/locale/translations/lb.json');
|
|
|
|
Locale::setLanguageFromJSON('lt', __DIR__ . '/config/locale/translations/lt.json');
|
|
|
|
Locale::setLanguageFromJSON('lv', __DIR__ . '/config/locale/translations/lv.json');
|
|
|
|
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');
|
|
|
|
Locale::setLanguageFromJSON('nb', __DIR__ . '/config/locale/translations/nb.json');
|
|
|
|
Locale::setLanguageFromJSON('ne', __DIR__ . '/config/locale/translations/ne.json');
|
|
|
|
Locale::setLanguageFromJSON('nl', __DIR__ . '/config/locale/translations/nl.json');
|
|
|
|
Locale::setLanguageFromJSON('nn', __DIR__ . '/config/locale/translations/nn.json');
|
|
|
|
Locale::setLanguageFromJSON('or', __DIR__ . '/config/locale/translations/or.json');
|
|
|
|
Locale::setLanguageFromJSON('pa', __DIR__ . '/config/locale/translations/pa.json');
|
|
|
|
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');
|
2021-07-26 19:05:08 +12:00
|
|
|
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');
|
2021-10-12 03:34:26 +13:00
|
|
|
Locale::setLanguageFromJSON('sn', __DIR__ . '/config/locale/translations/sn.json');
|
2021-07-26 19:05:08 +12:00
|
|
|
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');
|
2022-05-24 02:54:50 +12:00
|
|
|
Locale::setLanguageFromJSON('te', __DIR__ . '/config/locale/translations/te.json');
|
|
|
|
Locale::setLanguageFromJSON('th', __DIR__ . '/config/locale/translations/th.json');
|
|
|
|
Locale::setLanguageFromJSON('tl', __DIR__ . '/config/locale/translations/tl.json');
|
|
|
|
Locale::setLanguageFromJSON('tr', __DIR__ . '/config/locale/translations/tr.json');
|
|
|
|
Locale::setLanguageFromJSON('uk', __DIR__ . '/config/locale/translations/uk.json');
|
|
|
|
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',
|
2022-05-24 02:54:50 +12:00
|
|
|
'user_agent' => \sprintf(
|
|
|
|
APP_USERAGENT,
|
2020-06-30 23:09:28 +12:00
|
|
|
App::getEnv('_APP_VERSION', 'UNKNOWN'),
|
2022-05-24 02:54:50 +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
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('logger', function ($register) {
|
2021-11-23 05:12:18 +13:00
|
|
|
return $register->get('logger');
|
|
|
|
}, ['register']);
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('loggerBreadcrumbs', function () {
|
2021-11-24 03:24:25 +13:00
|
|
|
return [];
|
2020-06-30 16:34:13 +12:00
|
|
|
});
|
|
|
|
|
2021-12-17 23:41:26 +13:00
|
|
|
App::setResource('register', fn() => $register);
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('layout', function ($locale) {
|
|
|
|
$layout = new View(__DIR__ . '/views/layouts/default.phtml');
|
2020-06-30 16:34:13 +12:00
|
|
|
$layout->setParam('locale', $locale);
|
|
|
|
|
|
|
|
return $layout;
|
|
|
|
}, ['locale']);
|
|
|
|
|
2021-12-21 23:48:10 +13:00
|
|
|
App::setResource('locale', fn() => new Locale(App::getEnv('_APP_LOCALE', 'en')));
|
2020-06-30 16:34:13 +12:00
|
|
|
|
|
|
|
// Queues
|
2021-12-21 23:48:10 +13:00
|
|
|
App::setResource('events', fn() => new Event('', ''));
|
2022-04-14 00:39:31 +12:00
|
|
|
App::setResource('audits', fn() => new Audit());
|
|
|
|
App::setResource('mails', fn() => new Mail());
|
|
|
|
App::setResource('deletes', fn() => new Delete());
|
|
|
|
App::setResource('database', fn() => new EventDatabase());
|
2022-06-09 01:57:34 +12:00
|
|
|
App::setResource('messaging', fn() => new Phone());
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('usage', function ($register) {
|
2021-08-08 18:39:16 +12:00
|
|
|
return new Stats($register->get('statsd'));
|
2020-06-30 16:34:13 +12:00
|
|
|
}, ['register']);
|
|
|
|
|
2021-12-28 01:45:23 +13:00
|
|
|
App::setResource('clients', function ($request, $console, $project) {
|
2021-08-20 23:15:17 +12:00
|
|
|
$console->setAttribute('platforms', [ // Always allow current host
|
2022-08-14 22:33:36 +12:00
|
|
|
'$collection' => ID::custom('platforms'),
|
2021-01-13 05:36:21 +13:00
|
|
|
'name' => 'Current Host',
|
|
|
|
'type' => 'web',
|
|
|
|
'hostname' => $request->getHostname(),
|
|
|
|
], Document::SET_TYPE_APPEND);
|
2021-12-21 23:48:10 +13:00
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
/**
|
|
|
|
* Get All verified client URLs for both console and current projects
|
|
|
|
* + Filter for duplicated entries
|
|
|
|
*/
|
2021-12-28 01:45:23 +13:00
|
|
|
$clientsConsole = \array_map(
|
|
|
|
fn ($node) => $node['hostname'],
|
|
|
|
\array_filter(
|
|
|
|
$console->getAttribute('platforms', []),
|
|
|
|
fn ($node) => (isset($node['type']) && $node['type'] === 'web' && isset($node['hostname']) && !empty($node['hostname']))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$clients = \array_unique(
|
|
|
|
\array_merge(
|
|
|
|
$clientsConsole,
|
|
|
|
\array_map(
|
|
|
|
fn ($node) => $node['hostname'],
|
|
|
|
\array_filter(
|
|
|
|
$project->getAttribute('platforms', []),
|
|
|
|
fn ($node) => (isset($node['type']) && $node['type'] === 'web' && isset($node['hostname']) && !empty($node['hostname']))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
return $clients;
|
2021-01-13 05:36:21 +13:00
|
|
|
}, ['request', 'console', 'project']);
|
2020-07-01 06:08:02 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('user', function ($mode, $project, $console, $request, $response, $dbForProject, $dbForConsole) {
|
2022-01-01 04:50:07 +13:00
|
|
|
/** @var Appwrite\Utopia\Request $request */
|
2020-10-30 02:50:49 +13:00
|
|
|
/** @var Appwrite\Utopia\Response $response */
|
2021-05-07 10:31:05 +12:00
|
|
|
/** @var Utopia\Database\Document $project */
|
2021-12-28 01:45:23 +13:00
|
|
|
/** @var Utopia\Database\Database $dbForProject */
|
2021-05-07 10:31:05 +12:00
|
|
|
/** @var Utopia\Database\Database $dbForConsole */
|
2021-06-12 04:09:46 +12:00
|
|
|
/** @var string $mode */
|
2020-07-01 06:08:02 +12:00
|
|
|
|
2020-07-03 18:08:37 +12:00
|
|
|
Authorization::setDefaultStatus(true);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
Auth::setCookieName('a_session_' . $project->getId());
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
if (APP_MODE_ADMIN === $mode) {
|
2022-05-24 02:54:50 +12:00
|
|
|
Auth::setCookieName('a_session_' . $console->getId());
|
2020-07-01 06:08:02 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
$session = Auth::decodeSession(
|
2022-05-24 02:54:50 +12:00
|
|
|
$request->getCookie(
|
|
|
|
Auth::$cookieName, // Get sessions
|
|
|
|
$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
|
2022-05-24 02:54:50 +12:00
|
|
|
if ($response) {
|
|
|
|
$response->addHeader('X-Debug-Fallback', 'false');
|
|
|
|
}
|
2020-07-01 06:08:02 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if (empty($session['id']) && empty($session['secret'])) {
|
|
|
|
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
|
|
|
|
2021-05-07 10:31:05 +12:00
|
|
|
Auth::$unique = $session['id'] ?? '';
|
|
|
|
Auth::$secret = $session['secret'] ?? '';
|
2020-07-01 06:08:02 +12:00
|
|
|
|
2021-05-16 22:55:12 +12:00
|
|
|
if (APP_MODE_ADMIN !== $mode) {
|
2021-05-17 21:37:33 +12:00
|
|
|
if ($project->isEmpty()) {
|
2022-08-14 22:33:36 +12:00
|
|
|
$user = new Document(['$id' => ID::custom(''), '$collection' => 'users']);
|
2022-05-24 02:54:50 +12:00
|
|
|
} else {
|
2021-12-28 01:45:23 +13:00
|
|
|
$user = $dbForProject->getDocument('users', Auth::$unique);
|
2021-05-17 21:37:33 +12:00
|
|
|
}
|
2022-05-24 02:54:50 +12:00
|
|
|
} else {
|
2021-05-07 10:31:05 +12:00
|
|
|
$user = $dbForConsole->getDocument('users', Auth::$unique);
|
2020-07-01 06:08:02 +12:00
|
|
|
}
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if (
|
|
|
|
$user->isEmpty() // Check a document has been found in the DB
|
|
|
|
|| !Auth::sessionVerify($user->getAttribute('sessions', []), Auth::$secret)
|
|
|
|
) { // Validate user has valid login token
|
2022-08-14 22:33:36 +12:00
|
|
|
$user = new Document(['$id' => ID::custom(''), '$collection' => 'users']);
|
2020-07-01 06:08:02 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
if (APP_MODE_ADMIN === $mode) {
|
2021-05-16 22:55:12 +12:00
|
|
|
if ($user->find('teamId', $project->getAttribute('teamId'), '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 {
|
2022-08-14 22:33:36 +12:00
|
|
|
$user = new Document(['$id' => ID::custom(''), '$collection' => 'users']);
|
2020-07-01 06:08:02 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 09:31:42 +13:00
|
|
|
$authJWT = $request->getHeader('x-appwrite-jwt', '');
|
|
|
|
|
2021-05-17 21:37:33 +12:00
|
|
|
if (!empty($authJWT) && !$project->isEmpty()) { // 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) {
|
2022-08-14 17:35:25 +12:00
|
|
|
throw new Exception(Exception::USER_JWT_INVALID, 'Failed to verify JWT. ' . $error->getMessage());
|
2020-12-29 09:31:42 +13:00
|
|
|
}
|
2021-08-17 20:58:33 +12:00
|
|
|
|
2020-12-29 09:31:42 +13:00
|
|
|
$jwtUserId = $payload['userId'] ?? '';
|
|
|
|
$jwtSessionId = $payload['sessionId'] ?? '';
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if ($jwtUserId && $jwtSessionId) {
|
2021-12-28 01:45:23 +13:00
|
|
|
$user = $dbForProject->getDocument('users', $jwtUserId);
|
2020-12-29 09:31:42 +13:00
|
|
|
}
|
|
|
|
|
2021-05-16 10:41:42 +12:00
|
|
|
if (empty($user->find('$id', $jwtSessionId, 'sessions'))) { // Match JWT to active token
|
2022-08-14 22:33:36 +12:00
|
|
|
$user = new Document(['$id' => ID::custom(''), '$collection' => 'users']);
|
2020-12-29 09:31:42 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 06:08:02 +12:00
|
|
|
return $user;
|
2021-12-28 01:45:23 +13:00
|
|
|
}, ['mode', 'project', 'console', 'request', 'response', 'dbForProject', 'dbForConsole']);
|
2020-07-01 06:08:02 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('project', function ($dbForConsole, $request, $console) {
|
2022-01-01 04:50:07 +13:00
|
|
|
/** @var Appwrite\Utopia\Request $request */
|
2021-05-17 21:37:33 +12:00
|
|
|
/** @var Utopia\Database\Database $dbForConsole */
|
|
|
|
/** @var Utopia\Database\Document $console */
|
2021-05-16 21:18:34 +12:00
|
|
|
|
2021-12-18 04:25:05 +13:00
|
|
|
$projectId = $request->getParam('project', $request->getHeader('x-appwrite-project', 'console'));
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if ($projectId === 'console') {
|
2021-05-16 21:18:34 +12:00
|
|
|
return $console;
|
|
|
|
}
|
2020-07-01 20:55:14 +12:00
|
|
|
|
2021-12-18 04:25:05 +13:00
|
|
|
$project = Authorization::skip(fn() => $dbForConsole->getDocument('projects', $projectId));
|
2020-07-01 06:08:02 +12:00
|
|
|
|
|
|
|
return $project;
|
2021-05-16 21:18:34 +12:00
|
|
|
}, ['dbForConsole', 'request', 'console']);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('console', function () {
|
2021-08-20 23:15:17 +12:00
|
|
|
return new Document([
|
2022-08-14 22:33:36 +12:00
|
|
|
'$id' => ID::custom('console'),
|
|
|
|
'$internalId' => ID::custom('console'),
|
2021-05-16 21:18:34 +12:00
|
|
|
'name' => 'Appwrite',
|
2022-08-14 22:33:36 +12:00
|
|
|
'$collection' => ID::custom('projects'),
|
2021-05-16 21:18:34 +12:00
|
|
|
'description' => 'Appwrite core engine',
|
|
|
|
'logo' => '',
|
|
|
|
'teamId' => -1,
|
|
|
|
'webhooks' => [],
|
|
|
|
'keys' => [],
|
|
|
|
'platforms' => [
|
|
|
|
[
|
2022-08-14 22:33:36 +12:00
|
|
|
'$collection' => ID::custom('platforms'),
|
2021-05-16 21:18:34 +12:00
|
|
|
'name' => 'Localhost',
|
|
|
|
'type' => 'web',
|
|
|
|
'hostname' => 'localhost',
|
|
|
|
], // Current host is added on app init
|
|
|
|
],
|
|
|
|
'legalName' => '',
|
|
|
|
'legalCountry' => '',
|
|
|
|
'legalState' => '',
|
|
|
|
'legalCity' => '',
|
|
|
|
'legalAddress' => '',
|
|
|
|
'legalTaxId' => '',
|
2021-08-06 20:34:17 +12:00
|
|
|
'auths' => [
|
|
|
|
'limit' => (App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled') === 'enabled') ? 1 : 0, // limit signup to 1 user
|
|
|
|
],
|
2021-05-16 21:18:34 +12:00
|
|
|
'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)) : [],
|
|
|
|
]);
|
|
|
|
}, []);
|
2020-06-30 16:34:13 +12:00
|
|
|
|
2022-06-16 19:50:52 +12:00
|
|
|
App::setResource('dbForProject', function ($db, $cache, Document $project) {
|
2021-07-05 03:14:39 +12:00
|
|
|
$cache = new Cache(new RedisCache($cache));
|
2021-05-03 20:28:31 +12:00
|
|
|
|
2021-08-22 03:09:08 +12:00
|
|
|
$database = new Database(new MariaDB($db), $cache);
|
2021-12-29 01:04:58 +13:00
|
|
|
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
2022-06-16 19:50:52 +12:00
|
|
|
$database->setNamespace("_{$project->getInternalId()}");
|
2021-05-03 20:28:31 +12:00
|
|
|
|
|
|
|
return $database;
|
2021-07-02 05:48:37 +12:00
|
|
|
}, ['db', 'cache', 'project']);
|
2021-05-04 21:31:26 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('dbForConsole', function ($db, $cache) {
|
2021-07-05 03:14:39 +12:00
|
|
|
$cache = new Cache(new RedisCache($cache));
|
2021-05-04 21:31:26 +12:00
|
|
|
|
2021-08-22 03:09:08 +12:00
|
|
|
$database = new Database(new MariaDB($db), $cache);
|
2021-12-29 01:04:58 +13:00
|
|
|
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
2022-02-08 01:25:15 +13:00
|
|
|
$database->setNamespace('_console');
|
2021-05-04 21:31:26 +12:00
|
|
|
|
|
|
|
return $database;
|
2021-07-02 05:48:37 +12:00
|
|
|
}, ['db', 'cache']);
|
2021-05-04 21:31:26 +12:00
|
|
|
|
2022-01-07 23:02:38 +13:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('deviceLocal', function () {
|
2021-12-13 20:34:15 +13:00
|
|
|
return new Local();
|
|
|
|
});
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('deviceFiles', function ($project) {
|
2022-02-17 21:26:24 +13:00
|
|
|
return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId());
|
2021-12-13 20:34:15 +13:00
|
|
|
}, ['project']);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('deviceFunctions', function ($project) {
|
2022-02-17 21:26:24 +13:00
|
|
|
return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId());
|
|
|
|
}, ['project']);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('deviceBuilds', function ($project) {
|
2022-02-17 21:26:24 +13:00
|
|
|
return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId());
|
|
|
|
}, ['project']);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
function getDevice($root): Device
|
|
|
|
{
|
2021-12-13 20:34:15 +13:00
|
|
|
switch (App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL)) {
|
2022-05-24 02:54:50 +12:00
|
|
|
case Storage::DEVICE_LOCAL:
|
|
|
|
default:
|
2022-02-17 21:26:24 +13:00
|
|
|
return new Local($root);
|
2021-12-13 20:34:15 +13:00
|
|
|
case Storage::DEVICE_S3:
|
2022-02-23 01:33:35 +13:00
|
|
|
$s3AccessKey = App::getEnv('_APP_STORAGE_S3_ACCESS_KEY', '');
|
|
|
|
$s3SecretKey = App::getEnv('_APP_STORAGE_S3_SECRET', '');
|
|
|
|
$s3Region = App::getEnv('_APP_STORAGE_S3_REGION', '');
|
|
|
|
$s3Bucket = App::getEnv('_APP_STORAGE_S3_BUCKET', '');
|
2021-12-13 20:34:15 +13:00
|
|
|
$s3Acl = 'private';
|
2022-02-17 21:26:24 +13:00
|
|
|
return new S3($root, $s3AccessKey, $s3SecretKey, $s3Bucket, $s3Region, $s3Acl);
|
2021-12-13 20:34:15 +13:00
|
|
|
case Storage::DEVICE_DO_SPACES:
|
2022-02-23 01:33:35 +13:00
|
|
|
$doSpacesAccessKey = App::getEnv('_APP_STORAGE_DO_SPACES_ACCESS_KEY', '');
|
|
|
|
$doSpacesSecretKey = App::getEnv('_APP_STORAGE_DO_SPACES_SECRET', '');
|
|
|
|
$doSpacesRegion = App::getEnv('_APP_STORAGE_DO_SPACES_REGION', '');
|
|
|
|
$doSpacesBucket = App::getEnv('_APP_STORAGE_DO_SPACES_BUCKET', '');
|
2021-12-13 20:34:15 +13:00
|
|
|
$doSpacesAcl = 'private';
|
2022-02-17 21:26:24 +13:00
|
|
|
return new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl);
|
2022-05-13 10:01:53 +12:00
|
|
|
case Storage::DEVICE_BACKBLAZE:
|
|
|
|
$backblazeAccessKey = App::getEnv('_APP_STORAGE_BACKBLAZE_ACCESS_KEY', '');
|
|
|
|
$backblazeSecretKey = App::getEnv('_APP_STORAGE_BACKBLAZE_SECRET', '');
|
|
|
|
$backblazeRegion = App::getEnv('_APP_STORAGE_BACKBLAZE_REGION', '');
|
|
|
|
$backblazeBucket = App::getEnv('_APP_STORAGE_BACKBLAZE_BUCKET', '');
|
|
|
|
$backblazeAcl = 'private';
|
|
|
|
return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl);
|
2022-03-19 05:03:04 +13:00
|
|
|
case Storage::DEVICE_LINODE:
|
|
|
|
$linodeAccessKey = App::getEnv('_APP_STORAGE_LINODE_ACCESS_KEY', '');
|
|
|
|
$linodeSecretKey = App::getEnv('_APP_STORAGE_LINODE_SECRET', '');
|
|
|
|
$linodeRegion = App::getEnv('_APP_STORAGE_LINODE_REGION', '');
|
|
|
|
$linodeBucket = App::getEnv('_APP_STORAGE_LINODE_BUCKET', '');
|
|
|
|
$linodeAcl = 'private';
|
|
|
|
return new Linode($root, $linodeAccessKey, $linodeSecretKey, $linodeBucket, $linodeRegion, $linodeAcl);
|
2022-03-19 06:17:43 +13:00
|
|
|
case Storage::DEVICE_WASABI:
|
|
|
|
$wasabiAccessKey = App::getEnv('_APP_STORAGE_WASABI_ACCESS_KEY', '');
|
|
|
|
$wasabiSecretKey = App::getEnv('_APP_STORAGE_WASABI_SECRET', '');
|
|
|
|
$wasabiRegion = App::getEnv('_APP_STORAGE_WASABI_REGION', '');
|
|
|
|
$wasabiBucket = App::getEnv('_APP_STORAGE_WASABI_BUCKET', '');
|
|
|
|
$wasabiAcl = 'private';
|
|
|
|
return new Wasabi($root, $wasabiAccessKey, $wasabiSecretKey, $wasabiBucket, $wasabiRegion, $wasabiAcl);
|
2021-12-13 20:34:15 +13:00
|
|
|
}
|
2022-02-17 21:26:24 +13:00
|
|
|
}
|
2021-12-13 20:34:15 +13:00
|
|
|
|
2022-11-09 16:19:01 +13:00
|
|
|
/**
|
|
|
|
* Get database connection pool size for worker processes.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
function getWorkerPoolSize(): int
|
|
|
|
{
|
|
|
|
$reservedConnections = APP_DATABASE_DEFAULT_POOL_SIZE; // Pool of default size is reserved for the HTTP API
|
|
|
|
$workerCount = swoole_cpu_num() * intval(App::getEnv('_APP_WORKER_PER_CORE', 6));
|
|
|
|
$maxConnections = App::getenv('_APP_DB_MAX_CONNECTIONS', 1001);
|
|
|
|
$workerConnections = $maxConnections - $reservedConnections;
|
|
|
|
|
|
|
|
if ($workerCount > $workerConnections) {
|
|
|
|
throw new \Exception('Worker pool size is too small. Increase the number of allowed database connections or decrease the number of workers.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)($workerConnections / $workerCount);
|
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
App::setResource('mode', function ($request) {
|
2022-01-01 04:50:07 +13:00
|
|
|
/** @var Appwrite\Utopia\Request $request */
|
2021-12-15 00:15:00 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the mode for the request:
|
|
|
|
* - 'default' => Requests for Client and Server Side
|
|
|
|
* - 'admin' => Request from the Console on non-console projects
|
|
|
|
*/
|
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
|
|
|
|
2022-05-24 02:54:50 +12: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']);
|
2022-06-08 21:00:38 +12:00
|
|
|
|
2022-08-15 01:43:41 +12:00
|
|
|
App::setResource('sms', function () {
|
|
|
|
$dsn = new DSN(App::getEnv('_APP_SMS_PROVIDER'));
|
2022-06-21 00:22:35 +12:00
|
|
|
$user = $dsn->getUser();
|
|
|
|
$secret = $dsn->getPassword();
|
2022-06-08 21:00:38 +12:00
|
|
|
|
2022-06-21 00:22:35 +12:00
|
|
|
return match ($dsn->getHost()) {
|
2022-09-19 20:09:48 +12:00
|
|
|
'mock' => new Mock($user, $secret), // used for tests
|
2022-06-08 21:00:38 +12:00
|
|
|
'twilio' => new Twilio($user, $secret),
|
|
|
|
'text-magic' => new TextMagic($user, $secret),
|
|
|
|
'telesign' => new Telesign($user, $secret),
|
2022-07-10 02:04:57 +12:00
|
|
|
'msg91' => new Msg91($user, $secret),
|
2022-07-13 03:45:22 +12:00
|
|
|
'vonage' => new Vonage($user, $secret),
|
2022-06-08 21:00:38 +12:00
|
|
|
default => null
|
|
|
|
};
|
2022-06-09 02:18:28 +12:00
|
|
|
});
|
2022-08-11 01:45:04 +12:00
|
|
|
|
|
|
|
App::setResource('servers', function () {
|
|
|
|
$platforms = Config::getParam('platforms');
|
|
|
|
$server = $platforms[APP_PLATFORM_SERVER];
|
|
|
|
|
|
|
|
$languages = array_map(function ($language) {
|
|
|
|
return strtolower($language['name']);
|
|
|
|
}, $server['languages']);
|
|
|
|
|
|
|
|
return $languages;
|
2022-08-11 01:49:56 +12:00
|
|
|
});
|