Add web installer

This commit is contained in:
Daniel Supernault 2022-04-10 05:50:44 -06:00
parent 488561c270
commit 6cf671cb23
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
6 changed files with 302 additions and 0 deletions

View file

@ -0,0 +1,214 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use App\User;
class InstallController extends Controller
{
public function index(Request $request)
{
abort_if(file_exists(base_path('.env')), 404);
return view('installer.index');
}
public function getRequirements()
{
abort_if(file_exists(base_path('.env')), 404);
$reqs = [];
$phpExtensions = [
'bcmath',
'ctype',
'curl',
'intl',
'json',
'mbstring',
'openssl',
'gd',
'redis',
'pdo_mysql',
'pdo_pgsql'
];
$dirs = [
'bootstrap',
'storage'
];
$reqs['php'] = [
'version' => 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);
}
}
}

BIN
public/js/installer.js vendored Normal file

Binary file not shown.

View file

@ -0,0 +1 @@
/*! @source http://purl.eligrey.com/github/canvas-toBlob.js/blob/master/canvas-toBlob.js */

Binary file not shown.

View file

@ -0,0 +1,79 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<title>Pixelfed Installer</title>
<link rel="manifest" href="/manifest.json">
<link href="/css/app.css" rel="stylesheet" data-stylesheet="light">
<style type="text/css">
body {
background: linear-gradient(rgba(29, 38, 113, 0.8), rgba(195, 55, 100, 0.8));
}
.btn {
min-width: 100px;
}
.btn-primary {
min-width: 200px;
font-weight: bold;
}
.form-group {
margin-bottom: 1.5rem;
}
.form-group label {
font-weight: bold;
}
.form-control::placeholder {
color: #cbd5e1;
}
.form-control {
background:#f5f8fa;
border-color:#f5f8fa;
}
.form-control:focus {
color: #000;
background:#f5f8fa;
border-color:#f5f8fa;
box-shadow: none
}
</style>
</head>
<body class="loggedIn">
<main id="content">
<noscript>
<div class="container">
<p class="pt-5 text-center lead">Please enable javascript to view this content.</p>
</div>
</noscript>
<div class="container w-100 h-100">
<div class="row w-100 h-100 d-flex align-items-center justify-content-center">
<div class="col-12">
<p class="text-center py-3">
<a href="/installer">
<img src="/img/pixelfed-icon-color.svg" width="60" height="60">
</a>
</p>
<router-view></router-view>
<p class="d-flex justify-content-between text-white pt-2 px-2">
<a class="font-weight-bold text-white" href="https://docs.pixelfed.org/running-pixelfed/installation.html" target="_blank">Help</a>
<span class="font-weight-bold">v{{config('pixelfed.version')}}</span>
</p>
</div>
</div>
</div>
</main>
<script type="text/javascript" src="/js/manifest.js"></script>
<script type="text/javascript" src="/js/vendor.js"></script>
<script type="text/javascript" src="/js/installer.js"></script>
</body>
</html>

View file

@ -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');