123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Redis;
- class Installer extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'install';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'CLI Installer';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->welcome();
- }
- protected function welcome()
- {
- $this->info(' ____ _ ______ __ ');
- $this->info(' / __ \(_) _____ / / __/__ ____/ / ');
- $this->info(' / /_/ / / |/_/ _ \/ / /_/ _ \/ __ / ');
- $this->info(' / ____/ /> </ __/ / __/ __/ /_/ / ');
- $this->info(' /_/ /_/_/|_|\___/_/_/ \___/\__,_/ ');
- $this->info(' ');
- $this->info(' Welcome to the Pixelfed Installer!');
- $this->info(' ');
- $this->info(' ');
- $this->info('Pixelfed version: ' . config('pixelfed.version'));
- $this->line(' ');
- $this->info('Scanning system...');
- $this->preflightCheck();
- }
- protected function preflightCheck()
- {
- $this->line(' ');
- $this->info('Checking for installed dependencies...');
- $redis = Redis::connection();
- if($redis->ping()) {
- $this->info('- Found redis!');
- } else {
- $this->error('- Redis not found, aborting installation');
- exit;
- }
- $this->checkPhpDependencies();
- $this->checkPermissions();
- $this->envCheck();
- }
- protected function checkPhpDependencies()
- {
- $extensions = [
- 'bcmath',
- 'ctype',
- 'curl',
- 'json',
- 'mbstring',
- 'openssl'
- ];
- $ffmpeg = exec('which ffmpeg');
- if(empty($ffmpeg)) {
- $this->error('FFmpeg not found, please install it.');
- $this->error('Cancelling installation.');
- exit;
- } else {
- $this->info('- Found FFmpeg!');
- }
- $this->line('');
- $this->info('Checking for required php extensions...');
- foreach($extensions as $ext) {
- if(extension_loaded($ext) == false) {
- $this->error("- {$ext} extension not found, aborting installation");
- exit;
- } else {
- }
- }
- $this->info("- Required PHP extensions found!");
- }
- protected function checkPermissions()
- {
- $this->line('');
- $this->info('Checking for proper filesystem permissions...');
- $paths = [
- base_path('bootstrap'),
- base_path('storage')
- ];
- foreach($paths as $path) {
- if(is_writeable($path) == false) {
- $this->error("- Invalid permission found! Aborting installation.");
- $this->error(" Please make the following path writeable by the web server:");
- $this->error(" $path");
- exit;
- } else {
- $this->info("- Found valid permissions for {$path}");
- }
- }
- }
- protected function envCheck()
- {
- if(!file_exists(base_path('.env')) || filesize(base_path('.env')) == 0) {
- $this->line('');
- $this->info('No .env configuration file found. We will create one now!');
- $this->createEnv();
- } else {
- $confirm = $this->confirm('Found .env file, do you want to overwrite it?');
- if(!$confirm) {
- $this->info('Cancelling installation.');
- exit;
- }
- $confirm = $this->confirm('Are you really sure you want to overwrite it?');
- if(!$confirm) {
- $this->info('Cancelling installation.');
- exit;
- }
- $this->error('Warning ... if you did not backup your .env before its overwritten it will be permanently deleted.');
- $confirm = $this->confirm('The application may be installed already, are you really sure you want to overwrite it?');
- if(!$confirm) {
- $this->info('Cancelling installation.');
- exit;
- }
- }
- $this->postInstall();
- }
- protected function createEnv()
- {
- $this->line('');
- // copy env
- if(!file_exists(app()->environmentFilePath())) {
- exec('cp .env.example .env');
- $this->call('key:generate');
- }
- $name = $this->ask('Site name [ex: Pixelfed]');
- $this->updateEnvFile('APP_NAME', $name ?? 'pixelfed');
- $domain = $this->ask('Site Domain [ex: pixelfed.com]');
- $this->updateEnvFile('APP_DOMAIN', $domain ?? 'example.org');
- $this->updateEnvFile('ADMIN_DOMAIN', $domain ?? 'example.org');
- $this->updateEnvFile('SESSION_DOMAIN', $domain ?? 'example.org');
- $this->updateEnvFile('APP_URL', 'https://' . $domain ?? 'https://example.org');
- $database = $this->choice('Select database driver', ['mysql', 'pgsql'], 0);
- $this->updateEnvFile('DB_CONNECTION', $database ?? 'mysql');
- switch ($database) {
- case 'mysql':
- $database_host = $this->ask('Select database host', '127.0.0.1');
- $this->updateEnvFile('DB_HOST', $database_host ?? 'mysql');
- $database_port = $this->ask('Select database port', 3306);
- $this->updateEnvFile('DB_PORT', $database_port ?? 3306);
- $database_db = $this->ask('Select database', 'pixelfed');
- $this->updateEnvFile('DB_DATABASE', $database_db ?? 'pixelfed');
- $database_username = $this->ask('Select database username', 'pixelfed');
- $this->updateEnvFile('DB_USERNAME', $database_username ?? 'pixelfed');
- $db_pass = str_random(64);
- $database_password = $this->secret('Select database password', $db_pass);
- $this->updateEnvFile('DB_PASSWORD', $database_password);
- break;
-
- }
- $cache = $this->choice('Select cache driver', ["redis", "apc", "array", "database", "file", "memcached"], 0);
- $this->updateEnvFile('CACHE_DRIVER', $cache ?? 'redis');
- $session = $this->choice('Select session driver', ["redis", "file", "cookie", "database", "apc", "memcached", "array"], 0);
- $this->updateEnvFile('SESSION_DRIVER', $session ?? 'redis');
- $redis_host = $this->ask('Set redis host', 'localhost');
- $this->updateEnvFile('REDIS_HOST', $redis_host);
- $redis_password = $this->ask('Set redis password', 'null');
- $this->updateEnvFile('REDIS_PASSWORD', $redis_password);
- $redis_port = $this->ask('Set redis port', 6379);
- $this->updateEnvFile('REDIS_PORT', $redis_port);
- $open_registration = $this->choice('Allow new registrations?', ['true', 'false'], 1);
- $this->updateEnvFile('OPEN_REGISTRATION', $open_registration);
- $enforce_email_verification = $this->choice('Enforce email verification?', ['true', 'false'], 0);
- $this->updateEnvFile('ENFORCE_EMAIL_VERIFICATION', $enforce_email_verification);
- }
- protected function updateEnvFile($key, $value)
- {
- $envPath = app()->environmentFilePath();
- $payload = file_get_contents($envPath);
- if ($existing = $this->existingEnv($key, $payload)) {
- $payload = str_replace("{$key}={$existing}", "{$key}=\"{$value}\"", $payload);
- $this->storeEnv($payload);
- } else {
- $payload = $payload . "\n{$key}=\"{$value}\"\n";
- $this->storeEnv($payload);
- }
- }
- protected function existingEnv($needle, $haystack)
- {
- preg_match("/^{$needle}=[^\r\n]*/m", $haystack, $matches);
- if ($matches && count($matches)) {
- return substr($matches[0], strlen($needle) + 1);
- }
- return false;
- }
- protected function storeEnv($payload)
- {
- $file = fopen(app()->environmentFilePath(), 'w');
- fwrite($file, $payload);
- fclose($file);
- }
- protected function postInstall()
- {
- $this->callSilent('config:cache');
- //$this->callSilent('route:cache');
- $this->info('Pixelfed has been successfully installed!');
- }
- }
|