|
|
- <?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;
-
- /**
- *
- *
- *
- */
- 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('Enter 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...');
-
- // generate password
- $generator = new ComputerPasswordGenerator();
- $generator->setRandomGenerator(new Php7RandomGenerator())
- ->setUppercase()
- ->setLowercase()
- ->setUppercase()
- ->setNumbers()
- ->setSymbols(false)
- ->setLength(self::PASSWORD_LENGTH);
-
- // getting password
- $password = $generator->generatePasswords(1)[0];
-
- $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];
-
- // 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/ Check /root/mcp.log');
- file_put_contents(self::MCP_LOG_FILE, "Mariadb $database created\nuser: $username\npassword: $password\n--\n", FILE_APPEND);
- }
- }
|