|
|
- <?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
- {
- // length for password
- const NAME_LENGTH = 15;
-
- /**
- * The signature of the command.
- *
- * @var string
- */
- protected $signature = 'mariadb:add';
-
- /**
- * 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);
-
-
- $mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
-
- // create user for remote and local access
- $mysqli->query("CREATE USER $username@'localhost' IDENTIFIED BY '$password'");
- $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();
- }
-
- // grant privleges, for remote acces require ssl
- $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost");
- $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/');
- }
- }
|