diff --git a/app/Http/Controllers/InstallController.php b/app/Http/Controllers/InstallController.php new file mode 100644 index 000000000..0204f16f0 --- /dev/null +++ b/app/Http/Controllers/InstallController.php @@ -0,0 +1,214 @@ + PHP_VERSION, + 'supported' => (bool) version_compare(PHP_VERSION, 7.4), + 'min_version' => '7.4', + 'memory_limit' => [ + 'recommended' => '256M', + 'actual' => ini_get('memory_limit'), + ], + 'upload_max_filesize' => [ + 'recommended' => '15M', + 'actual' => ini_get('upload_max_filesize') + ], + 'post_max_size' => [ + 'recommended' => '15M', + 'actual' => ini_get('post_max_size') + ] + ]; + + $reqs['php']['extensions'] = collect($phpExtensions) + ->map(function($ext) { + return [ 'name' => $ext, 'loaded' => extension_loaded($ext) ]; + }); + + $reqs['permissions'] = collect($dirs) + ->map(function($path) { + return [ + 'name' => $path, + 'writable' => is_writable(base_path($path)), + 'mode' => substr(sprintf('%o', fileperms(base_path($path))), -4), + 'path' => base_path($path) + ]; + }); + return $reqs; + } + + public function store(Request $request) + { + abort_if(file_exists(base_path('.env')), 404, 'The .env configuration file already exists.'); + return $this->checkPermissions($request); + return response()->json($request->all(), 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); + } + + protected function checkPermissions($request) + { + abort_if(file_exists(base_path('.env')), 404); + + if( file_exists(base_path('.env'))) { + return response('Found existing .env file, aborting installation', 400); + } + + if( !is_writable(base_path('storage/')) ) { + return response('Permission Problem(s), cannot write to bootstrap or storage directories', 400); + } + return $this->checkDatabase($request); + } + + protected function checkDatabase($request) + { + abort_if(file_exists(base_path('.env')), 404); + + $driver = $request->input('db_driver', 'mysql'); + $name = $request->input('db_name', 'pixelfed'); + $host = $request->input('db_host', 'localhost'); + $username = $request->input('db_username'); + $password = $request->input('db_password'); + $dsn = "{$driver}:dbname={$name};host={$host}"; + $dbh = new \PDO($dsn, $username, $password); + + try { + $dbh->query('SELECT count(*) from INFORMATION_SCHEMA.TABLES'); + } catch (\PDOException $e) { + return response($e, 400); + } + + $this->createConfiguration($request); + } + + protected function createConfiguration($request) + { + abort_if(file_exists(base_path('.env')), 404); + $source = base_path('.env.example'); + $target = base_path('.env'); + @copy($source, $target); + + $this->updateConfig('APP_URL=http://localhost', 'APP_URL=https://'.$request->input('domain')); + $this->updateConfig('APP_NAME="Pixelfed Prod"', 'APP_NAME="'.$request->input('name').'"'); + $this->updateConfig('APP_DOMAIN="localhost"', 'APP_DOMAIN="'.$request->input('domain').'"'); + $this->updateConfig('ADMIN_DOMAIN="localhost"', 'ADMIN_DOMAIN="'.$request->input('domain').'"'); + $this->updateConfig('SESSION_DOMAIN="localhost"', 'SESSION_DOMAIN="'.$request->input('domain').'"'); + + $this->updateConfig('DB_CONNECTION=mysql', 'DB_CONNECTION='.$request->input('db_driver')); + $this->updateConfig('DB_HOST=127.0.0.1', 'DB_HOST='.$request->input('db_host')); + $this->updateConfig('DB_PORT=3306', 'DB_PORT='.$request->input('db_port')); + $this->updateConfig('DB_DATABASE=pixelfed', 'DB_DATABASE='.$request->input('db_name')); + $this->updateConfig('DB_USERNAME=pixelfed', 'DB_USERNAME='.$request->input('db_username')); + $this->updateConfig('DB_PASSWORD=pixelfed', 'DB_PASSWORD='.$request->input('db_password')); + + $this->updateConfig('CACHE_DRIVER=redis', 'CACHE_DRIVER='.$request->input('cache_driver')); + $this->updateConfig('QUEUE_DRIVER=redis', 'QUEUE_DRIVER='.$request->input('queue_driver')); + $this->updateConfig('REDIS_SCHEME=tcp', 'REDIS_SCHEME='.$request->input('redis_scheme')); + $this->updateConfig('REDIS_HOST=127.0.0.1', 'REDIS_HOST='.$request->input('redis_host')); + $this->updateConfig('REDIS_PORT=6379', 'REDIS_PORT='.$request->input('redis_port')); + $this->updateConfig('REDIS_PASSWORD=null', 'REDIS_PASSWORD='.$request->input('redis_password')); + + $this->updateConfig('ACTIVITY_PUB=false', 'ACTIVITY_PUB=' .($request->input('features.activitypub') ? 'true' : 'false')); + $this->updateConfig('AP_INBOX=false', 'AP_INBOX=' .($request->input('features.activitypub') ? 'true' : 'false')); + $this->updateConfig('AP_REMOTE_FOLLOW=false', 'AP_REMOTE_FOLLOW=' .($request->input('features.activitypub') ? 'true' : 'false')); + $this->updateConfig('OPEN_REGISTRATION=true', 'OPEN_REGISTRATION=' .($request->input('features.open_registration') ? 'true' : 'false')); + + $this->updateConfig('ENFORCE_EMAIL_VERIFICATION=true', 'ENFORCE_EMAIL_VERIFICATION=' .($request->input('mail_address_verify') ? 'true' : 'false')); + $this->updateConfig('PF_OPTIMIZE_IMAGES=true', 'PF_OPTIMIZE_IMAGES=' .($request->input('optimize_media') ? 'true' : 'false')); + $this->updateConfig('MAX_PHOTO_SIZE=15000', 'MAX_PHOTO_SIZE=' .($request->input('max_upload_size') * 1000)); + $this->updateConfig('MEDIA_TYPES=image/jpeg,image/png,image/gif', 'MEDIA_TYPES=' .implode(',', $request->input('mime_types'))); + $this->updateConfig('OAUTH_ENABLED=true', 'OAUTH_ENABLED=true'); + + if($request->input('optimize_media') == true) { + $this->updateConfig('IMAGE_QUALITY=80', 'IMAGE_QUALITY=' .$request->input('image_quality')); + } + + sleep(1); + Artisan::call('config:cache'); + sleep(1); + Artisan::call('key:generate --force'); + sleep(1); + Artisan::call('migrate --force'); + sleep(1); + Artisan::call('config:cache'); + + if($request->has('features.activitypub') && $request->input('features.activitypub') == true) { + Artisan::call('instance:actor'); + } + + if($request->filled(['admin_username', 'admin_password', 'admin_email'])) { + $user = new User; + $user->username = $request->input('admin_username', 'admin'); + $user->name = $request->input('admin_username', 'admin'); + $user->email = $request->input('admin_email'); + $user->password = bcrypt($request->input('admin_password')); + $user->is_admin = true; + $user->email_verified_at = now(); + $user->save(); + } + } + + protected function updateConfig($key, $value) + { + $f = file_get_contents(base_path('.env')); + if(strpos($f, $key) !== false) { + $u = str_replace($key, $value, $f); + } else { + $u = $f; + $u .= $value . PHP_EOL; + } + sleep(1); + file_put_contents(base_path('.env'), $u); + } + + public function precheckDatabase(Request $request) + { + $driver = $request->input('db_driver', 'mysql'); + $name = $request->input('db_name', 'pixelfed'); + $host = $request->input('db_host', 'localhost'); + $username = $request->input('db_username'); + $password = $request->input('db_password'); + $dsn = "{$driver}:dbname={$name};host={$host}"; + $dbh = new \PDO($dsn, $username, $password); + + try { + $dbh->query('SELECT count(*) from INFORMATION_SCHEMA.TABLES'); + } catch (\PDOException $e) { + return response($e, 400); + } + } +} diff --git a/app/Jobs/ImageOptimizePipeline/ImageOptimize.php b/app/Jobs/ImageOptimizePipeline/ImageOptimize.php index 6b7fd4885..e7c1b211e 100644 --- a/app/Jobs/ImageOptimizePipeline/ImageOptimize.php +++ b/app/Jobs/ImageOptimizePipeline/ImageOptimize.php @@ -39,6 +39,10 @@ class ImageOptimize implements ShouldQueue */ public function handle() { + if(config('pixelfed.optimize_image') == false) { + return; + } + $media = $this->media; $path = storage_path('app/'.$media->media_path); if (!is_file($path) || $media->skip_optimize) { diff --git a/app/Services/ConfigCacheService.php b/app/Services/ConfigCacheService.php index 65c57882b..91d644ee7 100644 --- a/app/Services/ConfigCacheService.php +++ b/app/Services/ConfigCacheService.php @@ -14,6 +14,9 @@ class ConfigCacheService { $cacheKey = self::CACHE_KEY . $key; $ttl = now()->addHours(12); + if(!config('instance.enable_cc')) { + return config($key); + } return Cache::remember($cacheKey, $ttl, function() use($key) { diff --git a/database/migrations/2020_12_13_203646_add_providers_column_to_oauth_clients_table.php b/database/migrations/2020_12_13_203646_add_providers_column_to_oauth_clients_table.php index 5b2a7ae7e..6c828b24d 100644 --- a/database/migrations/2020_12_13_203646_add_providers_column_to_oauth_clients_table.php +++ b/database/migrations/2020_12_13_203646_add_providers_column_to_oauth_clients_table.php @@ -14,7 +14,7 @@ class AddProvidersColumnToOauthClientsTable extends Migration public function up() { Schema::table('oauth_clients', function (Blueprint $table) { - if(Schema::hasColumn('oauth_clients', 'provider') == false) { + if(Schema::hasTable('oauth_clients') && Schema::hasColumn('oauth_clients', 'provider') == false) { $table->string('provider')->after('secret')->nullable(); } }); @@ -27,8 +27,10 @@ class AddProvidersColumnToOauthClientsTable extends Migration */ public function down() { - Schema::table('oauth_clients', function (Blueprint $table) { - $table->dropColumn('provider'); - }); + if(Schema::hasTable('oauth_clients')) { + Schema::table('oauth_clients', function (Blueprint $table) { + $table->dropColumn('provider'); + }); + } } } diff --git a/database/migrations/2022_04_08_065311_create_cache_table.php b/database/migrations/2022_04_08_065311_create_cache_table.php new file mode 100644 index 000000000..dfa90155d --- /dev/null +++ b/database/migrations/2022_04_08_065311_create_cache_table.php @@ -0,0 +1,32 @@ +string('key')->primary(); + $table->mediumText('value'); + $table->integer('expiration'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('cache'); + } +} diff --git a/public/js/installer.js b/public/js/installer.js new file mode 100644 index 000000000..2ad4f3084 Binary files /dev/null and b/public/js/installer.js differ diff --git a/public/js/installer.js.LICENSE.txt b/public/js/installer.js.LICENSE.txt new file mode 100644 index 000000000..ba8e2aeda --- /dev/null +++ b/public/js/installer.js.LICENSE.txt @@ -0,0 +1 @@ +/*! @source http://purl.eligrey.com/github/canvas-toBlob.js/blob/master/canvas-toBlob.js */ diff --git a/public/mix-manifest.json b/public/mix-manifest.json index f532b3088..f31c59478 100644 Binary files a/public/mix-manifest.json and b/public/mix-manifest.json differ diff --git a/resources/views/installer/index.blade.php b/resources/views/installer/index.blade.php new file mode 100644 index 000000000..c2855077c --- /dev/null +++ b/resources/views/installer/index.blade.php @@ -0,0 +1,79 @@ + + +
+ + + + +