Internal: Migrate and improve settings from configuration.php to .env and hosting_limits.yml - refs #5702
parent
c910deed85
commit
913d723d0a
@ -0,0 +1,11 @@ |
||||
parameters: |
||||
hosting_limits: |
||||
urls: |
||||
1: |
||||
- hosting_limit_users: 100 |
||||
- hosting_limit_teachers: 0 |
||||
- hosting_limit_courses: 0 |
||||
- hosting_limit_sessions: 0 |
||||
- hosting_limit_disk_space: 500 |
||||
- hosting_limit_active_courses: 0 |
||||
- hosting_total_size_limit: 0 |
||||
@ -1,63 +0,0 @@ |
||||
<?php |
||||
// Chamilo version {NEW_VERSION} |
||||
// File generated by /install/index.php script - {DATE_GENERATED} |
||||
/* For licensing terms, see /license.txt */ |
||||
/** |
||||
* This file contains a list of variables that can be modified by the campus site's server administrator. |
||||
* Pay attention when changing these variables, some changes may cause Chamilo to stop working. |
||||
* If you changed some settings and want to restore them, please have a look at |
||||
* configuration.dist.php. That file is an exact copy of the config file at install time. |
||||
* Besides the $_configuration, a $_settings array also exists, that |
||||
* contains variables that can be changed and will not break the platform. |
||||
* These optional settings are defined in the database, now |
||||
* (table settings_current). |
||||
*/ |
||||
|
||||
// Enable access to database management for platform admins. |
||||
$_configuration['db_manager_enabled'] = false; |
||||
|
||||
/** |
||||
* Hosting settings - Allows you to set limits to the Chamilo portal when |
||||
* hosting it for a third party. These settings can be overwritten by an |
||||
* optionally-loaded extension file with only the settings (no comments). |
||||
* The settings use an index at the first level to represent the ID of the |
||||
* URL in case you use multi-url (otherwise it will always use 1, which is |
||||
* the ID of the only URL inside the access_url table). |
||||
*/ |
||||
// Set a maximum number of users. Default (0) = no limit |
||||
$_configuration[1]['hosting_limit_users'] = 0; |
||||
// Set a maximum number of teachers. Default (0) = no limit |
||||
$_configuration[1]['hosting_limit_teachers'] = 0; |
||||
// Set a maximum number of courses. Default (0) = no limit |
||||
$_configuration[1]['hosting_limit_courses'] = 0; |
||||
// Set a maximum number of sessions. Default (0) = no limit |
||||
$_configuration[1]['hosting_limit_sessions'] = 0; |
||||
// Set a maximum disk space used, in MB (set to 1024 for 1GB, 5120 for 5GB, etc) |
||||
// Default (0) = no limit |
||||
$_configuration[1]['hosting_limit_disk_space'] = 0; |
||||
// Set a maximum number of usable courses. Default (0) = no limit. |
||||
// Should always be lower than the hosting_limit_courses. |
||||
// If set, defining a course as "hidden" will free room for |
||||
// new courses (up to the hosting_limit_courses, if any value is set there). |
||||
// hosting_limit_enabled_courses is the maximum number of courses that are *not* hidden. |
||||
$_configuration[1]['hosting_limit_active_courses'] = 0; |
||||
// Email to warn if limit was reached. |
||||
//$_configuration[1]['hosting_contact_mail'] = 'example@example.org'; |
||||
// Portal size limit in MB (set to 1024 for 1GB, 5120 for 5GB, etc). |
||||
// Check main/cron/hosting_total_size_limit.php for how to use this limit. |
||||
$_configuration['hosting_total_size_limit'] = 0; |
||||
|
||||
// Security word for password recovery |
||||
$_configuration['security_key'] = '{SECURITY_KEY}'; |
||||
$_configuration['software_name'] = 'Chamilo'; |
||||
$_configuration['software_url'] = 'https://chamilo.org/'; |
||||
// Deny the elimination of users |
||||
$_configuration['deny_delete_users'] = false; |
||||
|
||||
// KEEP THIS AT THE END |
||||
// -------- Custom DB changes |
||||
// Add user activation by confirmation email |
||||
// This option prevents the new user to login in the platform if your account is not confirmed via email |
||||
// You need add a new option called "confirmation" to the registration settings |
||||
//INSERT INTO settings_options (variable, value, display_text) VALUES ('allow_registration', 'confirmation', 'MailConfirmation'); |
||||
// ------ (End) Custom DB changes |
||||
@ -0,0 +1,123 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
final class Version20240806120000 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Migrate settings from configuration.php to .env and hosting_limits.yml files'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
global $_configuration; |
||||
|
||||
$rootPath = $this->getRootPath(); |
||||
$oldConfigPath = $rootPath . '/app/config/configuration.php'; |
||||
if (!\in_array($oldConfigPath, get_included_files(), true)) { |
||||
include_once $oldConfigPath; |
||||
} |
||||
|
||||
// Update .env and .env.local files |
||||
$this->updateEnvFiles($rootPath, [ |
||||
"DB_MANAGER_ENABLED" => $_configuration['db_manager_enabled'] ? '1' : '0', |
||||
"SOFTWARE_NAME" => $_configuration['software_name'], |
||||
"SOFTWARE_URL" => $_configuration['software_url'], |
||||
"DENY_DELETE_USERS" => $_configuration['deny_delete_users'] ? '1' : '0', |
||||
"HOSTING_TOTAL_SIZE_LIMIT" => $_configuration['hosting_total_size_limit'] ?? 0 |
||||
]); |
||||
|
||||
// Ensure the hosting_limits.yml file exists |
||||
$hostingLimitsFile = $rootPath . '/config/hosting_limits.yml'; |
||||
$hostingLimits = ['hosting_limits' => ['urls' => []]]; |
||||
|
||||
// Prepare hosting limits |
||||
foreach ($_configuration as $key => $config) { |
||||
if (is_numeric($key) && is_array($config)) { |
||||
// Handle configurations specific to URL IDs |
||||
$hostingLimits['hosting_limits']['urls'][$key] = [ |
||||
['hosting_limit_users' => $config['hosting_limit_users'] ?? 0], |
||||
['hosting_limit_teachers' => $config['hosting_limit_teachers'] ?? 0], |
||||
['hosting_limit_courses' => $config['hosting_limit_courses'] ?? 0], |
||||
['hosting_limit_sessions' => $config['hosting_limit_sessions'] ?? 0], |
||||
['hosting_limit_disk_space' => $config['hosting_limit_disk_space'] ?? 0], |
||||
['hosting_limit_active_courses' => $config['hosting_limit_active_courses'] ?? 0], |
||||
['hosting_total_size_limit' => $_configuration['hosting_total_size_limit'] ?? 0] |
||||
]; |
||||
} |
||||
} |
||||
|
||||
// Format hosting limits as YAML |
||||
$yamlContent = "parameters:\n hosting_limits:\n urls:\n"; |
||||
foreach ($hostingLimits['hosting_limits']['urls'] as $urlId => $limits) { |
||||
$yamlContent .= " {$urlId}:\n"; |
||||
foreach ($limits as $limit) { |
||||
foreach ($limit as $key => $value) { |
||||
$yamlContent .= " - {$key}: {$value}\n"; |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Write hosting limits to hosting_limits.yml |
||||
file_put_contents($hostingLimitsFile, $yamlContent); |
||||
} |
||||
|
||||
public function down(Schema $schema): void {} |
||||
|
||||
private function getRootPath(): string |
||||
{ |
||||
return $this->container->getParameter('kernel.project_dir'); |
||||
} |
||||
|
||||
private function updateEnvFiles(string $rootPath, array $envSettings): void |
||||
{ |
||||
$envFiles = [$rootPath . '/.env', $rootPath . '/.env.local']; |
||||
|
||||
foreach ($envFiles as $envFile) { |
||||
if (file_exists($envFile)) { |
||||
$lines = file($envFile, FILE_IGNORE_NEW_LINES); |
||||
$updatedLines = []; |
||||
$existingKeys = []; |
||||
|
||||
foreach ($lines as $line) { |
||||
if (strpos($line, '=') !== false) { |
||||
[$key, $value] = explode('=', $line, 2); |
||||
$key = trim($key); |
||||
if (array_key_exists($key, $envSettings)) { |
||||
$value = $envSettings[$key]; |
||||
unset($envSettings[$key]); |
||||
} |
||||
$updatedLines[] = "{$key}={$value}"; |
||||
$existingKeys[] = $key; |
||||
} else { |
||||
$updatedLines[] = $line; |
||||
} |
||||
} |
||||
|
||||
// Add remaining new settings |
||||
foreach ($envSettings as $key => $value) { |
||||
if (!in_array($key, $existingKeys)) { |
||||
$updatedLines[] = "{$key}={$value}"; |
||||
} |
||||
} |
||||
|
||||
file_put_contents($envFile, implode(PHP_EOL, $updatedLines) . PHP_EOL); |
||||
} else { |
||||
// If the file does not exist, create it with the settings |
||||
$newContent = []; |
||||
foreach ($envSettings as $key => $value) { |
||||
$newContent[] = "{$key}={$value}"; |
||||
} |
||||
file_put_contents($envFile, implode(PHP_EOL, $newContent) . PHP_EOL); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue