2024-09-24 14:20:49 -04:00
|
|
|
<?php
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
namespace Socialbox\Classes;
|
2024-09-24 14:20:49 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
use Socialbox\Classes\Configuration\CacheConfiguration;
|
2025-01-03 12:27:04 -05:00
|
|
|
use Socialbox\Classes\Configuration\CryptographyConfiguration;
|
2024-12-23 19:02:37 -05:00
|
|
|
use Socialbox\Classes\Configuration\DatabaseConfiguration;
|
|
|
|
use Socialbox\Classes\Configuration\InstanceConfiguration;
|
|
|
|
use Socialbox\Classes\Configuration\LoggingConfiguration;
|
|
|
|
use Socialbox\Classes\Configuration\RegistrationConfiguration;
|
|
|
|
use Socialbox\Classes\Configuration\SecurityConfiguration;
|
2025-01-03 12:27:04 -05:00
|
|
|
use Socialbox\Classes\Configuration\StorageConfiguration;
|
2024-10-30 15:12:55 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
class Configuration
|
2024-10-30 15:12:55 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
private static ?\ConfigLib\Configuration $configuration = null;
|
|
|
|
private static ?InstanceConfiguration $instanceConfiguration = null;
|
|
|
|
private static ?SecurityConfiguration $securityConfiguration = null;
|
2025-01-03 12:27:04 -05:00
|
|
|
private static ?CryptographyConfiguration $cryptographyConfiguration = null;
|
2024-12-23 19:02:37 -05:00
|
|
|
private static ?DatabaseConfiguration $databaseConfiguration = null;
|
|
|
|
private static ?LoggingConfiguration $loggingConfiguration = null;
|
|
|
|
private static ?CacheConfiguration $cacheConfiguration = null;
|
|
|
|
private static ?RegistrationConfiguration $registrationConfiguration = null;
|
|
|
|
private static ?StorageConfiguration $storageConfiguration = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the configuration settings for the application. This includes
|
|
|
|
* settings for the instance, security, database, cache layer, and registration.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function initializeConfiguration(): void
|
2024-10-30 15:12:55 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
$config = new \ConfigLib\Configuration('socialbox');
|
2024-10-30 15:12:55 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// Instance configuration
|
|
|
|
$config->setDefault('instance.enabled', false); // False by default, requires the user to enable it.
|
2025-01-03 12:27:04 -05:00
|
|
|
$config->setDefault('instance.name', "Socialbox Server");
|
2024-12-23 19:02:37 -05:00
|
|
|
$config->setDefault('instance.domain', null);
|
|
|
|
$config->setDefault('instance.rpc_endpoint', null);
|
2024-10-24 15:15:14 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// Security Configuration
|
|
|
|
$config->setDefault('security.display_internal_exceptions', false);
|
|
|
|
$config->setDefault('security.resolved_servers_ttl', 600);
|
|
|
|
$config->setDefault('security.captcha_ttl', 200);
|
|
|
|
|
2025-01-03 12:27:04 -05:00
|
|
|
// Cryptography Configuration
|
|
|
|
// The Unix Timestamp for when the host's keypair should expire
|
|
|
|
// Setting this value to 0 means the keypair never expires
|
|
|
|
// Setting this value to null will automatically set the current unix timestamp + 1 year as the value
|
|
|
|
// This means at initialization, the key is automatically set to expire in a year.
|
|
|
|
$config->setDefault('cryptography.host_keypair_expires', null);
|
|
|
|
// The host's public/private keypair in base64 encoding, when null; the initialization process
|
|
|
|
// will automatically generate a new keypair
|
|
|
|
$config->setDefault('cryptography.host_public_key', null);
|
|
|
|
$config->setDefault('cryptography.host_private_key', null);
|
|
|
|
|
|
|
|
// The internal encryption keys used for encrypting data in the database when needed.
|
|
|
|
// When null, the initialization process will automatically generate a set of keys
|
|
|
|
// based on the `encryption_keys_count` and `encryption_keys_algorithm` configuration.
|
|
|
|
// This is an array of base64 encoded keys.
|
|
|
|
$config->setDefault('cryptography.internal_encryption_keys', null);
|
|
|
|
|
|
|
|
// The number of encryption keys to generate and set to `instance.encryption_keys` this will be used
|
|
|
|
// to randomly encrypt/decrypt sensitive data in the database, this includes hashes.
|
|
|
|
// The higher the number the higher performance impact it will have on the server
|
|
|
|
$config->setDefault('cryptography.encryption_keys_count', 10);
|
|
|
|
// The host's encryption algorithm, this will be used to generate a set of encryption keys
|
|
|
|
// This is for internal encryption, these keys are never shared outside this configuration.
|
|
|
|
// Recommendation: Higher security over performance
|
|
|
|
$config->setDefault('cryptography.encryption_keys_algorithm', 'xchacha20');
|
|
|
|
|
|
|
|
// The encryption algorithm to use for encrypted message transport between the client aand the server
|
|
|
|
// This is the encryption the server tells the client to use and the client must support it.
|
|
|
|
// Recommendation: Good balance between security and performance
|
|
|
|
// For universal support & performance, use aes256gcm for best performance or for best security use xchacha20
|
|
|
|
$config->setDefault('cryptography.transport_encryption_algorithm', 'chacha20');
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// Database configuration
|
|
|
|
$config->setDefault('database.host', '127.0.0.1');
|
|
|
|
$config->setDefault('database.port', 3306);
|
|
|
|
$config->setDefault('database.username', 'root');
|
|
|
|
$config->setDefault('database.password', 'root');
|
|
|
|
$config->setDefault('database.name', 'test');
|
|
|
|
|
|
|
|
// Logging configuration
|
|
|
|
$config->setDefault('logging.console_logging_enabled', true);
|
|
|
|
$config->setDefault('logging.console_logging_level', 'info');
|
|
|
|
$config->setDefault('logging.file_logging_enabled', true);
|
|
|
|
$config->setDefault('logging.file_logging_level', 'error');
|
|
|
|
|
|
|
|
// Cache layer configuration
|
|
|
|
$config->setDefault('cache.enabled', false);
|
|
|
|
$config->setDefault('cache.engine', 'redis');
|
|
|
|
$config->setDefault('cache.host', '127.0.0.1');
|
|
|
|
$config->setDefault('cache.port', 6379);
|
|
|
|
$config->setDefault('cache.username', null);
|
|
|
|
$config->setDefault('cache.password', null);
|
|
|
|
$config->setDefault('cache.database', 0);
|
|
|
|
$config->setDefault('cache.sessions.enabled', true);
|
|
|
|
$config->setDefault('cache.sessions.ttl', 3600);
|
|
|
|
$config->setDefault('cache.sessions.max', 1000);
|
|
|
|
|
|
|
|
// Registration configuration
|
|
|
|
$config->setDefault('registration.enabled', true);
|
|
|
|
$config->setDefault('registration.privacy_policy_document', null);
|
|
|
|
$config->setDefault('registration.privacy_policy_date', 1734985525);
|
|
|
|
$config->setDefault('registration.accept_privacy_policy', true);
|
|
|
|
$config->setDefault('registration.terms_of_service_document', null);
|
|
|
|
$config->setDefault('registration.terms_of_service_date', 1734985525);
|
|
|
|
$config->setDefault('registration.accept_terms_of_service', true);
|
|
|
|
$config->setDefault('registration.community_guidelines_document', null);
|
|
|
|
$config->setDefault('registration.community_guidelines_date', 1734985525);
|
|
|
|
$config->setDefault('registration.accept_community_guidelines', true);
|
|
|
|
$config->setDefault('registration.password_required', true);
|
|
|
|
$config->setDefault('registration.otp_required', false);
|
|
|
|
$config->setDefault('registration.display_name_required', true);
|
|
|
|
$config->setDefault('registration.display_picture_required', false);
|
|
|
|
$config->setDefault('registration.image_captcha_verification_required', true);
|
|
|
|
|
|
|
|
// Storage configuration
|
|
|
|
$config->setDefault('storage.path', '/etc/socialbox'); // The main path for file storage
|
|
|
|
$config->setDefault('storage.user_display_images_path', 'user_profiles'); // eg; `/etc/socialbox/user_profiles`
|
|
|
|
$config->setDefault('storage.user_display_images_max_size', 3145728); // 3MB
|
|
|
|
|
|
|
|
$config->save();
|
|
|
|
|
|
|
|
self::$configuration = $config;
|
|
|
|
self::$instanceConfiguration = new InstanceConfiguration(self::$configuration->getConfiguration()['instance']);
|
|
|
|
self::$securityConfiguration = new SecurityConfiguration(self::$configuration->getConfiguration()['security']);
|
2025-01-03 12:27:04 -05:00
|
|
|
self::$cryptographyConfiguration = new CryptographyConfiguration(self::$configuration->getConfiguration()['cryptography']);
|
2024-12-23 19:02:37 -05:00
|
|
|
self::$databaseConfiguration = new DatabaseConfiguration(self::$configuration->getConfiguration()['database']);
|
|
|
|
self::$loggingConfiguration = new LoggingConfiguration(self::$configuration->getConfiguration()['logging']);
|
|
|
|
self::$cacheConfiguration = new CacheConfiguration(self::$configuration->getConfiguration()['cache']);
|
|
|
|
self::$registrationConfiguration = new RegistrationConfiguration(self::$configuration->getConfiguration()['registration']);
|
|
|
|
self::$storageConfiguration = new StorageConfiguration(self::$configuration->getConfiguration()['storage']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets all configuration instances by setting them to null and then
|
|
|
|
* reinitializes the configurations.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function reload(): void
|
2024-10-24 15:15:14 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
self::$configuration = null;
|
|
|
|
self::$instanceConfiguration = null;
|
|
|
|
self::$securityConfiguration = null;
|
|
|
|
self::$databaseConfiguration = null;
|
|
|
|
self::$loggingConfiguration = null;
|
|
|
|
self::$cacheConfiguration = null;
|
|
|
|
self::$registrationConfiguration = null;
|
|
|
|
|
2024-10-24 15:15:14 -04:00
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the current configuration array. If the configuration is not initialized,
|
|
|
|
* it triggers the initialization process.
|
|
|
|
*
|
|
|
|
* @return array The current configuration array.
|
|
|
|
*/
|
|
|
|
public static function getConfiguration(): array
|
2024-10-30 15:12:55 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
if(self::$configuration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$configuration->getConfiguration();
|
2024-10-30 15:12:55 -04:00
|
|
|
}
|
|
|
|
|
2025-01-03 12:27:04 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the configuration library instance.
|
|
|
|
*
|
|
|
|
* This method returns the current Configuration instance from the ConfigLib namespace.
|
|
|
|
* If the configuration has not been initialized yet, it initializes it first.
|
|
|
|
*
|
|
|
|
* @return \ConfigLib\Configuration The configuration library instance.
|
|
|
|
*/
|
2024-12-23 19:02:37 -05:00
|
|
|
public static function getConfigurationLib(): \ConfigLib\Configuration
|
|
|
|
{
|
|
|
|
if(self::$configuration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
2024-10-30 21:36:20 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
return self::$configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the current instance configuration.
|
|
|
|
*
|
|
|
|
* @return InstanceConfiguration The current instance configuration instance.
|
|
|
|
*/
|
|
|
|
public static function getInstanceConfiguration(): InstanceConfiguration
|
2024-10-30 21:36:20 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
if(self::$instanceConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instanceConfiguration;
|
2024-10-30 21:36:20 -04:00
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the current security configuration.
|
|
|
|
*
|
|
|
|
* @return SecurityConfiguration The current security configuration instance.
|
|
|
|
*/
|
|
|
|
public static function getSecurityConfiguration(): SecurityConfiguration
|
|
|
|
{
|
|
|
|
if(self::$securityConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
2024-10-30 15:12:55 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
return self::$securityConfiguration;
|
|
|
|
}
|
|
|
|
|
2025-01-03 12:27:04 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the cryptography configuration.
|
|
|
|
*
|
|
|
|
* This method returns the current CryptographyConfiguration instance.
|
|
|
|
* If the configuration has not been initialized yet, it initializes it first.
|
|
|
|
*
|
|
|
|
* @return CryptographyConfiguration|null The cryptography configuration instance or null if not available.
|
|
|
|
*/
|
|
|
|
public static function getCryptographyConfiguration(): ?CryptographyConfiguration
|
|
|
|
{
|
|
|
|
if(self::$cryptographyConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$cryptographyConfiguration;
|
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the current database configuration.
|
|
|
|
*
|
|
|
|
* @return DatabaseConfiguration The configuration settings for the database.
|
|
|
|
*/
|
|
|
|
public static function getDatabaseConfiguration(): DatabaseConfiguration
|
2024-10-30 15:12:55 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
if(self::$databaseConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$databaseConfiguration;
|
2024-10-30 15:12:55 -04:00
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the current logging configuration.
|
|
|
|
*
|
|
|
|
* @return LoggingConfiguration The current logging configuration instance.
|
|
|
|
*/
|
|
|
|
public static function getLoggingConfiguration(): LoggingConfiguration
|
|
|
|
{
|
|
|
|
if(self::$loggingConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
2024-10-30 15:12:55 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
return self::$loggingConfiguration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the current cache configuration. If the cache configuration
|
|
|
|
* has not been initialized, it will initialize it first.
|
|
|
|
*
|
|
|
|
* @return CacheConfiguration The current cache configuration instance.
|
|
|
|
*/
|
|
|
|
public static function getCacheConfiguration(): CacheConfiguration
|
2024-10-24 15:15:14 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
if(self::$cacheConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$cacheConfiguration;
|
2024-10-24 15:15:14 -04:00
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the registration configuration.
|
|
|
|
*
|
|
|
|
* This method returns the current RegistrationConfiguration instance.
|
|
|
|
* If the configuration has not been initialized yet, it initializes it first.
|
|
|
|
*
|
|
|
|
* @return RegistrationConfiguration The registration configuration instance.
|
|
|
|
*/
|
|
|
|
public static function getRegistrationConfiguration(): RegistrationConfiguration
|
2024-10-25 13:37:09 -04:00
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
if(self::$registrationConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$registrationConfiguration;
|
2024-10-25 13:37:09 -04:00
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
/**
|
|
|
|
* Retrieves the storage configuration.
|
|
|
|
*
|
|
|
|
* This method returns the current StorageConfiguration instance.
|
|
|
|
* If the configuration has not been initialized yet, it initializes it first.
|
|
|
|
*
|
|
|
|
* @return StorageConfiguration The storage configuration instance.
|
|
|
|
*/
|
|
|
|
public static function getStorageConfiguration(): StorageConfiguration
|
|
|
|
{
|
|
|
|
if(self::$storageConfiguration === null)
|
|
|
|
{
|
|
|
|
self::initializeConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$storageConfiguration;
|
|
|
|
}
|
|
|
|
}
|