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/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 @@ + + + + + + + + Pixelfed Installer + + + + + +
+ +
+
+
+

+ + + +

+ + + +

+ Help + v{{config('pixelfed.version')}} +

+
+
+
+
+ + + + + diff --git a/routes/web.php b/routes/web.php index d9436a21e..7445b747c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -529,6 +529,14 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('{username}', 'ProfileController@permalinkRedirect'); }); + Route::group(['prefix' => 'installer'], function() { + Route::get('api/requirements', 'InstallController@getRequirements')->withoutMiddleware(['web']); + Route::post('precheck/database', 'InstallController@precheckDatabase')->withoutMiddleware(['web']); + Route::post('store', 'InstallController@store')->withoutMiddleware(['web']); + Route::get('/', 'InstallController@index')->withoutMiddleware(['web']); + Route::get('/{q}', 'InstallController@index')->withoutMiddleware(['web'])->where('q', '.*'); + }); + Route::get('stories/{username}', 'ProfileController@stories'); Route::get('p/{id}', 'StatusController@shortcodeRedirect'); Route::get('c/{collection}', 'CollectionController@show');