Files
newznab-tmux/_install/install_nntmux.php
T
2018-01-20 23:50:31 +01:00

177 lines
6.3 KiB
PHP

<?php
if (! defined('NN_INSTALLER')) {
define('NN_INSTALLER', true);
}
require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'bootstrap/autoload.php';
use App\Models\Settings;
use App\Models\Tmux;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use nntmux\ColorCLI;
use nntmux\config\Configure;
use App\Extensions\util\Versions;
$config = new Configure('install');
$pdo = DB::connection()->getPdo();
$error = false;
if (file_exists(NN_ROOT.'_install/install.lock')) {
ColorCLI::doEcho(ColorCLI::notice('Installation is locked. If you want to reinstall NNTmux, please remove install.lock file from _install folder. '.PHP_EOL.ColorCLI::warning('This will wipe your database!')));
exit();
}
// Check if user selected right DB type.
if (env('DB_SYSTEM') !== 'mysql') {
ColorCLI::doEcho(ColorCLI::error('Invalid database system. Must be: mysql ; Not: '.env('DB_SYSTEM')));
$error = true;
} else {
// Check if the MySQL version is correct.
$goodVersion = false;
if (! $error) {
try {
$goodVersion = (new Settings())->isDbVersionAtLeast(NN_MINIMUM_MYSQL_VERSION);
} catch (\PDOException $e) {
$goodVersion = false;
$error = true;
ColorCLI::doEcho(ColorCLI::error('Could not get version from MySQL server.'));
}
if ($goodVersion === false) {
$error = true;
ColorCLI::doEcho(
ColorCLI::error(
'You are using an unsupported version of '.
env('DB_SYSTEM').
' the minimum allowed version is '.
NN_MINIMUM_MYSQL_VERSION
)
);
}
}
}
// Start inserting data into the DB.
if (! $error) {
$pdo->exec('SET FOREIGN_KEY_CHECKS=0;');
ColorCLI::doEcho(ColorCLI::header('Migrating tables and populating them'));
passthru('php '.NN_ROOT.'artisan migrate');
passthru('php '.NN_ROOT.'artisan db:seed');
//Insert admin user into database
if (env('ADMIN_USER') === '' || env('ADMIN_PASS') === '' || env('ADMIN_EMAIL') === '') {
$error = true;
ColorCLI::doEcho(ColorCLI::error('Admin user data cannot be empty! Please edit .env file and fill in admin user details and run this script again!'));
exit();
}
User::add(env('ADMIN_USER'), env('ADMIN_PASS'), env('ADMIN_EMAIL'), 2, '', '', '', '');
if (! $error) {
// Check one of the standard tables was created and has data.
$dbInstallWorked = false;
$reschk = Tmux::query()->count(['id']);
if ($reschk === null) {
$dbCreateCheck = false;
$error = true;
ColorCLI::doEcho(ColorCLI::warningOver('Could not select data from your database, check that tables and data are properly created/inserted.'));
} else {
if ($reschk > 0) {
$dbInstallWorked = true;
}
}
$ver = new Versions();
$patch = $ver->getSQLPatchFromFile();
if ($dbInstallWorked) {
$updateSettings = false;
if ($patch > 0) {
$updateSettings = Settings::query()->where(['section' => '', 'subsection' => '', 'name' => 'sqlpatch'])->update(['value' => $patch]);
}
// If it all worked, continue the install process.
if ($updateSettings) {
ColorCLI::doEcho(ColorCLI::info('Database updated successfully'));
} else {
$error = true;
ColorCLI::doEcho(ColorCLI::error('Could not update sqlpatch to '.$patch.' for your database.'));
}
} else {
$dbCreateCheck = false;
$error = true;
ColorCLI::doEcho(ColorCLI::warning('Could not select data from your database.'));
}
}
}
if (! $error) {
$doCheck = true;
$covers_path = NN_RES.'covers'.DS;
$nzb_path = NN_RES.'nzb'.DS;
$tmp_path = NN_RES.'tmp'.DS;
$unrar_path = $tmp_path.'unrar'.DS;
$nzbPathCheck = is_writable($nzb_path);
if ($nzbPathCheck === false) {
$error = true;
ColorCLI::doEcho(ColorCLI::warning($nzb_path.' is not writable. Please fix folder permissions'));
}
$lastchar = substr($nzb_path, strlen($nzb_path) - 1);
if ($lastchar !== '/') {
$nzb_path .= '/';
}
if (! file_exists($unrar_path)) {
ColorCLI::doEcho(ColorCLI::primary('Creating missing '.$unrar_path.' folder'));
if (! @mkdir($unrar_path) && ! is_dir($unrar_path)) {
throw new RuntimeException('Unable to create '.$unrar_path.' folder');
}
ColorCLI::doEcho(ColorCLI::primary('Folder '.$unrar_path.' successfully created'));
}
$unrarPathCheck = is_writable($unrar_path);
if ($unrarPathCheck === false) {
$error = true;
ColorCLI::doEcho(ColorCLI::warning($unrar_path.' is not writable. Please fix folder permissions'));
}
$lastchar = substr($unrar_path, strlen($unrar_path) - 1);
if ($lastchar !== '/') {
$unrar_path .= '/';
}
$coversPathCheck = is_writable($covers_path);
if ($coversPathCheck === false) {
$error = true;
ColorCLI::doEcho(ColorCLI::warning($covers_path.' is not writable. Please fix folder permissions'));
}
$lastchar = substr($covers_path, strlen($covers_path) - 1);
if ($lastchar !== '/') {
$covers_path .= '/';
}
if (! $error) {
$sql1 = Settings::query()->where('setting', '=','nzbpath')->update(['value' => $nzb_path]);
$sql2 = Settings::query()->where('setting', '=','tmpunrarpath')->update(['value' => $unrar_path]);
$sql3 = Settings::query()->where('setting', '=','coverspath')->update(['value' => $covers_path]);
if ($sql1 === null || $sql2 === null || $sql3 === null) {
$error = true;
} else {
ColorCLI::doEcho(ColorCLI::info('Settings table updated successfully'));
}
}
}
if (! $error) {
@file_put_contents(NN_ROOT.'_install/install.lock', '');
ColorCLI::doEcho(ColorCLI::header('Generating application key'));
passthru('php '.NN_ROOT.'artisan key:generate');
$pdo->exec('SET FOREIGN_KEY_CHECKS=1;');
ColorCLI::doEcho(ColorCLI::alternate('NNTmux installation completed successfully'));
exit();
}
ColorCLI::doEcho(ColorCLI::error('NNTmux installation failed. Fix reported problems and try again'));