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

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. // destination for username and password
  20. const MCP_LOG_FILE = '/root/mcp.log';
  21. // length for password
  22. const NAME_LENGTH = 15;
  23. // length for password
  24. const PASSWORD_LENGTH = 40;
  25. /**
  26. * The signature of the command.
  27. *
  28. * @var string
  29. */
  30. protected $signature = 'mariadb:add {--ssl}';
  31. /**
  32. * The description of the command.
  33. *
  34. * @var string
  35. */
  36. protected $description = 'Add Mariadb User and Database';
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. // enter root password
  45. $password = $this->secret('Root Password');
  46. // connect database
  47. try {
  48. $mysqli = new \mysqli('127.0.0.1', 'root', $password);
  49. } catch(\ErrorException $exception) {
  50. $this->error('Failed! '.$exception->getMessage());
  51. exit();
  52. }
  53. $this->info('Mariadb Create Database...');
  54. // setting password for root, repeat until
  55. do {
  56. $password = $this->secret('New Password');
  57. $passwordRepeat = $this->secret('Repeat Password');
  58. if ($password !== $passwordRepeat) {
  59. $this->error('Password not equal! Try again!');
  60. }
  61. if (empty($password)) {
  62. $this->error('Password is empty! Try again!');
  63. }
  64. } while ($password !== $passwordRepeat || empty($password));
  65. $generator = new ComputerPasswordGenerator();
  66. $generator->setRandomGenerator(new Php7RandomGenerator())
  67. ->setUppercase(false)
  68. ->setLowercase(false)
  69. ->setUppercase(false)
  70. ->setNumbers()
  71. ->setSymbols(false)
  72. ->setLength(self::NAME_LENGTH);
  73. // getting names
  74. $names = $generator->generatePasswords(2);
  75. $database = 'db'.$names[0];
  76. $username = 'u'.$names[1];
  77. $this->info('Database: '.$database);
  78. $this->info('Username: '.$username);
  79. // getting option for ssl
  80. $ssl = $this->option('ssl');
  81. $mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
  82. if ($ssl === false) {
  83. $mysqli->query("CREATE USER $username@localhost IDENTIFIED BY '$password'");
  84. } else {
  85. $mysqli->query("CREATE USER $username@'%' IDENTIFIED BY '$password'");
  86. }
  87. if ($mysqli->error) {
  88. $this->error('Failed! '.$mysqli->error);
  89. $mysqli->query("DROP DATABASE IF EXISTS $database");
  90. $mysqli->query("DROP USER $username@localhost");
  91. $mysqli->query("DROP USER $username@'%'");
  92. exit();
  93. }
  94. $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost");
  95. if ($ssl === true) {
  96. $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@'%' require ssl");
  97. }
  98. if ($mysqli->error) {
  99. $this->error('Failed! '.$mysqli->error);
  100. $mysqli->query("DROP DATABASE IF EXISTS $database");
  101. $mysqli->query("DROP USER $username@localhost");
  102. $mysqli->query("DROP USER $username@'%'");
  103. exit();
  104. }
  105. $mysqli->query("FLUSH PRIVILEGES");
  106. $mysqli->close();
  107. $this->info('Success! \o/');
  108. }
  109. }