OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

141 lines
3.8 KiB

<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use Illuminate\Support\Facades\File;
use App\Facades\Install;
use Hackzilla\PasswordGenerator\Generator\ComputerPasswordGenerator;
use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator;
/**
* Add database
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
*
*/
class MariadbAddCommand extends Command
{
// destination for username and password
const MCP_LOG_FILE = '/root/mcp.log';
// length for password
const NAME_LENGTH = 15;
// length for password
const PASSWORD_LENGTH = 40;
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'mariadb:add {--ssl}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Add Mariadb User and Database';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// enter root password
$password = $this->secret('Root Password');
// connect database
try {
$mysqli = new \mysqli('127.0.0.1', 'root', $password);
} catch(\ErrorException $exception) {
$this->error('Failed! '.$exception->getMessage());
exit();
}
$this->info('Mariadb Create Database...');
// setting password for root, repeat until
do {
$password = $this->secret('New Password');
$passwordRepeat = $this->secret('Repeat Password');
if ($password !== $passwordRepeat) {
$this->error('Password not equal! Try again!');
}
if (empty($password)) {
$this->error('Password is empty! Try again!');
}
} while ($password !== $passwordRepeat || empty($password));
$generator = new ComputerPasswordGenerator();
$generator->setRandomGenerator(new Php7RandomGenerator())
->setUppercase(false)
->setLowercase(false)
->setUppercase(false)
->setNumbers()
->setSymbols(false)
->setLength(self::NAME_LENGTH);
// getting names
$names = $generator->generatePasswords(2);
$database = 'db'.$names[0];
$username = 'u'.$names[1];
$this->info('Database: '.$database);
$this->info('Username: '.$username);
// getting option for ssl
$ssl = $this->option('ssl');
$mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
if ($ssl === false) {
$mysqli->query("CREATE USER $username@localhost IDENTIFIED BY '$password'");
} else {
$mysqli->query("CREATE USER $username@'%' IDENTIFIED BY '$password'");
}
if ($mysqli->error) {
$this->error('Failed! '.$mysqli->error);
$mysqli->query("DROP DATABASE IF EXISTS $database");
$mysqli->query("DROP USER $username@localhost");
$mysqli->query("DROP USER $username@'%'");
exit();
}
$mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost");
if ($ssl === true) {
$mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@'%' require ssl");
}
if ($mysqli->error) {
$this->error('Failed! '.$mysqli->error);
$mysqli->query("DROP DATABASE IF EXISTS $database");
$mysqli->query("DROP USER $username@localhost");
$mysqli->query("DROP USER $username@'%'");
exit();
}
$mysqli->query("FLUSH PRIVILEGES");
$mysqli->close();
$this->info('Success! \o/');
}
}