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.

128 lines
3.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. use Illuminate\Support\Facades\File;
  6. use App\Facades\Install;
  7. use Hackzilla\PasswordGenerator\Generator\ComputerPasswordGenerator;
  8. use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator;
  9. /**
  10. * Add database
  11. *
  12. * @author Björn Hase, Tentakelfabrik
  13. * @license http://opensource.org/licenses/MIT The MIT License
  14. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
  15. *
  16. */
  17. class MariadbAddCommand extends Command
  18. {
  19. // length for password
  20. const NAME_LENGTH = 15;
  21. /**
  22. * The signature of the command.
  23. *
  24. * @var string
  25. */
  26. protected $signature = 'mariadb:add';
  27. /**
  28. * The description of the command.
  29. *
  30. * @var string
  31. */
  32. protected $description = 'Add Mariadb User and Database';
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. // enter root password
  41. $password = $this->secret('Root Password');
  42. // connect database
  43. try {
  44. $mysqli = new \mysqli('127.0.0.1', 'root', $password);
  45. } catch(\ErrorException $exception) {
  46. $this->error('Failed! '.$exception->getMessage());
  47. exit();
  48. }
  49. $this->info('Mariadb Create Database...');
  50. // setting password for root, repeat until
  51. do {
  52. $password = $this->secret('New Password');
  53. $passwordRepeat = $this->secret('Repeat Password');
  54. if ($password !== $passwordRepeat) {
  55. $this->error('Password not equal! Try again!');
  56. }
  57. if (empty($password)) {
  58. $this->error('Password is empty! Try again!');
  59. }
  60. } while ($password !== $passwordRepeat || empty($password));
  61. $generator = new ComputerPasswordGenerator();
  62. $generator->setRandomGenerator(new Php7RandomGenerator())
  63. ->setUppercase(false)
  64. ->setLowercase(false)
  65. ->setUppercase(false)
  66. ->setNumbers()
  67. ->setSymbols(false)
  68. ->setLength(self::NAME_LENGTH);
  69. // getting names
  70. $names = $generator->generatePasswords(2);
  71. $database = 'db'.$names[0];
  72. $username = 'u'.$names[1];
  73. $this->info('Database: '.$database);
  74. $this->info('Username: '.$username);
  75. $mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
  76. // create user for remote and local access
  77. $mysqli->query("CREATE USER $username@'localhost' IDENTIFIED BY '$password'");
  78. $mysqli->query("CREATE USER $username@'%' IDENTIFIED BY '$password'");
  79. if ($mysqli->error) {
  80. $this->error('Failed! '.$mysqli->error);
  81. $mysqli->query("DROP DATABASE IF EXISTS $database");
  82. $mysqli->query("DROP USER $username@localhost");
  83. $mysqli->query("DROP USER $username@'%'");
  84. exit();
  85. }
  86. // grant privleges, for remote acces require ssl
  87. $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost");
  88. $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@'%' require SSL");
  89. if ($mysqli->error) {
  90. $this->error('Failed! '.$mysqli->error);
  91. $mysqli->query("DROP DATABASE IF EXISTS $database");
  92. $mysqli->query("DROP USER $username@localhost");
  93. $mysqli->query("DROP USER $username@'%'");
  94. exit();
  95. }
  96. $mysqli->query("FLUSH PRIVILEGES");
  97. $mysqli->close();
  98. $this->info('Success! \o/');
  99. }
  100. }