From 6e5eee8d50d75e16aea131a9b2ad0a123606c343 Mon Sep 17 00:00:00 2001 From: Shlee Date: Sun, 9 Nov 2025 01:58:48 +1300 Subject: [PATCH] Update Installer.php --- app/Console/Commands/Installer.php | 361 +++++++++++++++++------------ 1 file changed, 217 insertions(+), 144 deletions(-) diff --git a/app/Console/Commands/Installer.php b/app/Console/Commands/Installer.php index 6d967aa69..53aaa14d2 100644 --- a/app/Console/Commands/Installer.php +++ b/app/Console/Commands/Installer.php @@ -13,7 +13,20 @@ class Installer extends Command * * @var string */ - protected $signature = 'install {--dangerously-overwrite-env : Re-run installation and overwrite current .env }'; + protected $signature = 'install + {--dangerously-overwrite-env : Re-run installation and overwrite current .env} + {--domain= : Pre-fill site domain} + {--name= : Pre-fill site name} + {--email= : Pre-fill admin email} + {--db-driver= : Pre-fill database driver (mysql/pgsql)} + {--db-host= : Pre-fill database host} + {--db-port= : Pre-fill database port} + {--db-database= : Pre-fill database name} + {--db-username= : Pre-fill database username} + {--db-password= : Pre-fill database password} + {--redis-host= : Pre-fill Redis host} + {--redis-port= : Pre-fill Redis port} + {--redis-password= : Pre-fill Redis password}'; /** * The console command description. @@ -22,8 +35,7 @@ class Installer extends Command */ protected $description = 'CLI Installer'; - public $installType = 'Simple'; - public $continue; + protected $migrationsRan = false; /** * Create a new command instance. @@ -65,38 +77,22 @@ class Installer extends Command { $this->envCheck(); $this->envCreate(); - $this->installType(); - - if ($this->installType === 'Advanced') { - $this->info('Installer: Advanced...'); - $this->checkPHPRequiredDependencies(); - $this->checkFFmpegDependencies(); - $this->checkOptimiseDependencies(); - $this->checkDiskPermissions(); - $this->envProd(); - $this->instanceDB(); - $this->instanceRedis(); - $this->instanceURL(); - $this->activityPubSettings(); - $this->laravelSettings(); - $this->instanceSettings(); - $this->mediaSettings(); - $this->dbMigrations(); - $this->validateEnv(); - $this->resetArtisanCache(); - } else { - $this->info('Installer: Simple...'); - $this->checkDiskPermissions(); - $this->envProd(); - $this->instanceDB(); - $this->instanceRedis(); - $this->instanceURL(); - $this->activityPubSettings(); - $this->instanceSettings(); - $this->dbMigrations(); - $this->validateEnv(); - $this->resetArtisanCache(); - } + $this->checkPHPRequiredDependencies(); + $this->checkFFmpegDependencies(); + $this->checkOptimiseDependencies(); + $this->checkDiskPermissions(); + $this->envProd(); + $this->instanceDB(); + $this->instanceRedis(); + $this->instanceURL(); + $this->activityPubSettings(); + $this->laravelSettings(); + $this->instanceSettings(); + $this->mediaSettings(); + $this->dbMigrations(); + $this->setupPrep(); + $this->validateEnv(); + $this->resetArtisanCache(); } protected function envCheck() @@ -118,16 +114,10 @@ class Installer extends Command $this->line(''); $this->info('Creating .env if required'); if (!file_exists(app()->environmentFilePath())) { - exec('cp .env.example .env'); + copy(base_path('.env.example'), app()->environmentFilePath()); } } - protected function installType() - { - $type = $this->choice('Select installation type', ['Simple', 'Advanced'], 1); - $this->installType = $type; - } - protected function checkPHPRequiredDependencies() { $this->line(' '); @@ -145,21 +135,25 @@ class Installer extends Command 'xml', 'zip', 'redis', + 'vips', ]; + $missing = []; foreach ($extensions as $ext) { if (extension_loaded($ext) == false) { $this->error("- \"{$ext}\" not found"); + $missing[] = $ext; } else { $this->info("- \"{$ext}\" found"); } } - $continue = $this->choice('Do you wish to continue?', ['yes', 'no'], 0); - $this->continue = $continue; - if ($this->continue === 'no') { - $this->info('Exiting Installer.'); - exit; + if (!empty($missing)) { + $continue = $this->choice('Some extensions are missing. Do you wish to continue?', ['yes', 'no'], 1); + if ($continue === 'no') { + $this->info('Exiting Installer.'); + return 1; + } } } @@ -167,12 +161,17 @@ class Installer extends Command protected function checkFFmpegDependencies() { $this->line(' '); - $this->info('Checking for Required FFmpeg dependencies...'); + $this->info('Checking for FFmpeg (required for video processing)...'); $ffmpeg = exec('which ffmpeg'); if (empty($ffmpeg)) { - $this->error('- FFmpeg not found, aborting installation'); - exit; + $this->warn('- FFmpeg not found'); + $this->warn(' Video uploads will not work without FFmpeg'); + $continue = $this->choice('Do you want to continue without FFmpeg?', ['yes', 'no'], 1); + if ($continue === 'no') { + $this->info('Exiting Installer. Please install FFmpeg and try again.'); + return 1; + } } else { $this->info('- Found FFmpeg!'); } @@ -212,7 +211,7 @@ class Installer extends Command ]; foreach ($paths as $path) { - if (is_writeable($path) == false) { + if (is_writable($path) == false) { $this->error("- Invalid permission found! Aborting installation."); $this->error(" Please make the following path writeable by the web server:"); $this->error(" $path"); @@ -237,14 +236,15 @@ class Installer extends Command { $this->line(''); $this->info('Database Settings:'); - $database = $this->choice('Select database driver', ['mysql', 'pgsql'], 0); - $database_host = $this->ask('Select database host', '127.0.0.1'); + + $database = $this->choice('Select database driver', ['mysql', 'pgsql'], $this->option('db-driver') ?: 0); + $database_host = $this->ask('Select database host', $this->option('db-host') ?: '127.0.0.1'); $database_port_default = $database === 'mysql' ? 3306 : 5432; - $database_port = $this->ask('Select database port', $database_port_default); + $database_port = $this->ask('Select database port', $this->option('db-port') ?: $database_port_default); - $database_db = $this->ask('Select database', 'pixelfed'); - $database_username = $this->ask('Select database username', 'pixelfed'); - $database_password = $this->secret('Select database password'); + $database_db = $this->ask('Select database', $this->option('db-database') ?: 'pixelfed'); + $database_username = $this->ask('Select database username', $this->option('db-username') ?: 'pixelfed'); + $database_password = $this->ask('Select database password', $this->option('db-password') ?: null); $this->updateEnvFile('DB_CONNECTION', $database); $this->updateEnvFile('DB_HOST', $database_host); @@ -257,8 +257,10 @@ class Installer extends Command $dsn = "{$database}:dbname={$database_db};host={$database_host};port={$database_port};"; try { $dbh = new PDO($dsn, $database_username, $database_password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); + $dbh = null; // Close connection } catch (\PDOException $e) { $this->error('Cannot connect to database, check your details and try again'); + $this->error('Error: ' . $e->getMessage()); exit; } $this->info('- Connected to DB Successfully'); @@ -269,22 +271,29 @@ class Installer extends Command $this->line(''); $this->info('Redis Settings:'); $redis_client = $this->choice('Set redis client (PHP extension)', ['phpredis', 'predis'], 0); - $redis_host = $this->ask('Set redis host', 'localhost'); - $redis_password = $this->ask('Set redis password', 'null'); - $redis_port = $this->ask('Set redis port', 6379); + $redis_host = $this->ask('Set redis host', $this->option('redis-host') ?: 'localhost'); + $redis_password = $this->ask('Set redis password (leave empty for none)', $this->option('redis-password') ?? ''); + $redis_port = $this->ask('Set redis port', $this->option('redis-port') ?: 6379); $this->updateEnvFile('REDIS_CLIENT', $redis_client); $this->updateEnvFile('REDIS_SCHEME', 'tcp'); $this->updateEnvFile('REDIS_HOST', $redis_host); - $this->updateEnvFile('REDIS_PASSWORD', $redis_password); + $this->updateEnvFile('REDIS_PASSWORD', empty($redis_password) ? 'null' : $redis_password); $this->updateEnvFile('REDIS_PORT', $redis_port); $this->info('Testing Redis...'); - $redis = Redis::connection(); - if ($redis->ping()) { - $this->info('- Connected to Redis Successfully!'); - } else { + $this->call('config:clear'); + try { + $redis = Redis::connection(); + if ($redis->ping()) { + $this->info('- Connected to Redis Successfully!'); + } else { + $this->error('Cannot connect to Redis, check your details and try again'); + exit; + } + } catch (\Exception $e) { $this->error('Cannot connect to Redis, check your details and try again'); + $this->error('Error: ' . $e->getMessage()); exit; } } @@ -293,21 +302,30 @@ class Installer extends Command { $this->line(''); $this->info('Instance URL Settings:'); - $name = $this->ask('Site name [ex: Pixelfed]', 'Pixelfed'); - - $domain = $this->ask('Site Domain [ex: pixelfed.com]'); - $domain = strtolower($domain); - if (empty($domain)) { - $this->error('You must set the site domain'); - exit; - } - if (starts_with($domain, 'http')) { - $this->error('The site domain cannot start with https://, you must use the FQDN (eg: example.org)'); - exit; - } - if (strpos($domain, '.') == false) { - $this->error('You must enter a valid site domain'); - exit; + $name = $this->ask('Site name [ex: Pixelfed]', $this->option('name') ?: 'Pixelfed'); + + $domain = ''; + while (empty($domain)) { + $domain = $this->ask('Site Domain [ex: pixelfed.com]', $this->option('domain') ?: null); + $domain = strtolower(trim($domain)); + + if (empty($domain)) { + $this->error('You must set the site domain'); + continue; + } + + if (str_starts_with($domain, 'http://') || str_starts_with($domain, 'https://')) { + $this->error('The site domain cannot start with http:// or https://, you must use the FQDN (eg: example.org)'); + $domain = ''; + continue; + } + + // Better domain validation + if (!preg_match('/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$/i', $domain)) { + $this->error('Invalid domain format. Please enter a valid domain (eg: example.org)'); + $domain = ''; + continue; + } } $this->updateEnvFile('APP_NAME', $name); @@ -317,6 +335,19 @@ class Installer extends Command $this->updateEnvFile('SESSION_DOMAIN', $domain); } + protected function activityPubSettings() + { + $this->line(''); + $this->info('Federation Settings:'); + $activitypub_federation = $this->choice('Enable ActivityPub federation?', ['false', 'true'], 1); + + $this->updateEnvFile('ACTIVITY_PUB', $activitypub_federation); + $this->updateEnvFile('AP_REMOTE_FOLLOW', $activitypub_federation); + $this->updateEnvFile('AP_INBOX', $activitypub_federation); + $this->updateEnvFile('AP_OUTBOX', $activitypub_federation); + $this->updateEnvFile('AP_SHAREDINBOX', $activitypub_federation); + } + protected function laravelSettings() { $this->line(''); @@ -340,7 +371,15 @@ class Installer extends Command { $this->line(''); $this->info('Instance Settings:'); - $max_registration = $this->ask('Set Maximum users on this instance.', '1000'); + + $max_registration = ''; + while (!is_numeric($max_registration) || $max_registration < 1) { + $max_registration = $this->ask('Set Maximum users on this instance', '1000'); + if (!is_numeric($max_registration) || $max_registration < 1) { + $this->error('Please enter a valid number greater than 0'); + } + } + $open_registration = $this->choice('Allow new registrations?', ['false', 'true'], 0); $enforce_email_verification = $this->choice('Enforce email verification?', ['false', 'true'], 0); $enable_mobile_apis = $this->choice('Enable mobile app/apis support?', ['false', 'true'], 1); @@ -352,50 +391,43 @@ class Installer extends Command $this->updateEnvFile('EXP_EMC', $enable_mobile_apis); } - protected function activityPubSettings() - { - $this->line(''); - $this->info('Federation Settings:'); - $activitypub_federation = $this->choice('Enable ActivityPub federation?', ['false', 'true'], 1); - - $this->updateEnvFile('ACTIVITY_PUB', $activitypub_federation); - $this->updateEnvFile('AP_REMOTE_FOLLOW', $activitypub_federation); - $this->updateEnvFile('AP_INBOX', $activitypub_federation); - $this->updateEnvFile('AP_OUTBOX', $activitypub_federation); - $this->updateEnvFile('AP_SHAREDINBOX', $activitypub_federation); - } - protected function mediaSettings() { $this->line(''); $this->info('Media Settings:'); $optimize_media = $this->choice('Optimize media uploads? Requires jpegoptim and other dependencies!', ['false', 'true'], 1); - $image_quality = $this->ask('Set image optimization quality between 1-100. Default is 80%, lower values use less disk space at the expense of image quality.', '80'); - if ($image_quality < 1) { - $this->error('Min image quality is 1. You should avoid such a low value, 60 at minimum is recommended.'); - exit; - } - if ($image_quality > 100) { - $this->error('Max image quality is 100'); - exit; + + $image_quality = ''; + while (!is_numeric($image_quality) || $image_quality < 1 || $image_quality > 100) { + $image_quality = $this->ask('Set image optimization quality between 1-100 (default: 80)', '80'); + if (!is_numeric($image_quality) || $image_quality < 1 || $image_quality > 100) { + $this->error('Please enter a number between 1 and 100'); + } } + $this->info('Note: Max photo size cannot exceed `post_max_size` in php.ini.'); - $max_photo_size = $this->ask('Max photo upload size in kilobytes. Default 15000 which is equal to 15MB', '15000'); - - $max_caption_length = $this->ask('Max caption limit. Default to 500, max 5000.', '500'); - if ($max_caption_length > 5000) { - $this->error('Max caption length is 5000 characters.'); - exit; + $max_photo_size = ''; + while (!is_numeric($max_photo_size) || $max_photo_size < 1) { + $max_photo_size = $this->ask('Max photo upload size in kilobytes (default: 15000 = 15MB)', '15000'); + if (!is_numeric($max_photo_size) || $max_photo_size < 1) { + $this->error('Please enter a valid number greater than 0'); + } } - $max_album_length = $this->ask('Max photos allowed per album. Choose a value between 1 and 10.', '4'); - if ($max_album_length < 1) { - $this->error('Min album length is 1 photos per album.'); - exit; + $max_caption_length = ''; + while (!is_numeric($max_caption_length) || $max_caption_length < 1 || $max_caption_length > 5000) { + $max_caption_length = $this->ask('Max caption limit (1-5000, default: 500)', '500'); + if (!is_numeric($max_caption_length) || $max_caption_length < 1 || $max_caption_length > 5000) { + $this->error('Please enter a number between 1 and 5000'); + } } - if ($max_album_length > 10) { - $this->error('Max album length is 10 photos per album.'); - exit; + + $max_album_length = ''; + while (!is_numeric($max_album_length) || $max_album_length < 1 || $max_album_length > 10) { + $max_album_length = $this->ask('Max photos per album (1-10, default: 4)', '4'); + if (!is_numeric($max_album_length) || $max_album_length < 1 || $max_album_length > 10) { + $this->error('Please enter a number between 1 and 10'); + } } $this->updateEnvFile('PF_OPTIMIZE_IMAGES', $optimize_media); @@ -413,32 +445,58 @@ class Installer extends Command if ($confirm === 'Yes') { sleep(3); + + // Clear any cached config + $this->call('config:clear'); + + // Force reload environment variables + $app = app(); + $app->bootstrapWith([ + \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, + ]); + + // Purge database connections to force reconnect with new credentials + $app->forgetInstance('db'); + $app->forgetInstance('db.connection'); + \Illuminate\Support\Facades\DB::purge(); + + // Rebuild config cache $this->call('config:cache'); + $this->line(''); $this->info('Migrating DB:'); $this->call('migrate', ['--force' => true]); - $this->line(''); - $this->info('Importing Cities:'); - $this->call('import:cities'); - $this->line(''); - $this->info('Creating Federation Instance Actor:'); - $this->call('instance:actor'); - $this->line(''); - $this->info('Creating Password Keys for API:'); - $this->call('passport:keys', ['--force' => true]); - - $confirm = $this->choice('Do you want to create an admin account?', ['Yes', 'No'], 0); - if ($confirm === 'Yes') { - $this->call('user:create'); - } + $this->migrationsRan = true; } } - protected function resetArtisanCache() + protected function setupPrep() { - $this->call('config:cache'); - $this->call('route:cache'); - $this->call('view:cache'); + if (!$this->migrationsRan) { + $this->warn('Skipping setup tasks because migrations were not run.'); + $this->warn('You can run these commands manually later:'); + $this->warn(' php artisan import:cities'); + $this->warn(' php artisan instance:actor'); + $this->warn(' php artisan passport:keys'); + return; + } + + $this->line(''); + $this->info('Running setup tasks...'); + $this->line(''); + $this->info('Importing Cities:'); + $this->call('import:cities'); + $this->line(''); + $this->info('Creating Federation Instance Actor:'); + $this->call('instance:actor'); + $this->line(''); + $this->info('Creating Password Keys for API:'); + $this->call('passport:keys', ['--force' => true]); + + $confirm = $this->choice('Do you want to create an admin account?', ['Yes', 'No'], 0); + if ($confirm === 'Yes') { + $this->call('user:create'); + } } protected function validateEnv() @@ -448,6 +506,15 @@ class Installer extends Command $this->checkEnvKeys('APP_DEBUG', "APP_DEBUG value should be false"); } + protected function resetArtisanCache() + { + $this->call('config:clear'); + $this->call('config:cache'); + $this->call('route:cache'); + $this->call('view:cache'); + $this->line(''); + } + ##### # Installer Functions ##### @@ -467,6 +534,9 @@ class Installer extends Command { $envPath = app()->environmentFilePath(); $payload = file_get_contents($envPath); + + // Escape special characters for .env format + $value = str_replace(['\\', '"', "\n", "\r"], ['\\\\', '\\"', '\\n', '\\r'], $value); if ($existing = $this->existingEnv($key, $payload)) { $payload = str_replace("{$key}={$existing}", "{$key}=\"{$value}\"", $payload); @@ -488,19 +558,22 @@ class Installer extends Command protected function storeEnv($payload) { - $file = fopen(app()->environmentFilePath(), 'w'); + $envPath = app()->environmentFilePath(); + $tempPath = $envPath . '.tmp'; + + // Write to temp file first + $file = fopen($tempPath, 'w'); + if ($file === false) { + throw new \RuntimeException("Cannot write to {$tempPath}"); + } + fwrite($file, $payload); fclose($file); - } - - protected function parseSize($size) - { - $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); - $size = preg_replace('/[^0-9\.]/', '', $size); - if ($unit) { - return round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); - } else { - return round($size); + + // Atomic rename + if (!rename($tempPath, $envPath)) { + @unlink($tempPath); + throw new \RuntimeException("Cannot update .env file"); } } }