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.

137 lines
3.8 KiB

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('Enter 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. // generate password
  55. $generator = new ComputerPasswordGenerator();
  56. $generator->setRandomGenerator(new Php7RandomGenerator())
  57. ->setUppercase()
  58. ->setLowercase()
  59. ->setUppercase()
  60. ->setNumbers()
  61. ->setSymbols(false)
  62. ->setLength(self::PASSWORD_LENGTH);
  63. // getting password
  64. $password = $generator->generatePasswords(1)[0];
  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. // getting option for ssl
  78. $ssl = $this->option('ssl');
  79. $mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
  80. if ($ssl === false) {
  81. $mysqli->query("CREATE USER $username@localhost IDENTIFIED BY '$password'");
  82. } else {
  83. $mysqli->query("CREATE USER $username@'%' IDENTIFIED BY '$password'");
  84. }
  85. if ($mysqli->error) {
  86. $this->error('Failed! '.$mysqli->error);
  87. $mysqli->query("DROP DATABASE IF EXISTS $database");
  88. $mysqli->query("DROP USER $username@localhost");
  89. $mysqli->query("DROP USER $username@'%'");
  90. exit();
  91. }
  92. $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost");
  93. if ($ssl === true) {
  94. $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@'%' require ssl");
  95. }
  96. if ($mysqli->error) {
  97. $this->error('Failed! '.$mysqli->error);
  98. $mysqli->query("DROP DATABASE IF EXISTS $database");
  99. $mysqli->query("DROP USER $username@localhost");
  100. $mysqli->query("DROP USER $username@'%'");
  101. exit();
  102. }
  103. $mysqli->query("FLUSH PRIVILEGES");
  104. $mysqli->close();
  105. $this->info('Success! \o/ Check /root/mcp.log');
  106. file_put_contents(self::MCP_LOG_FILE, "Mariadb $database created\nuser: $username\npassword: $password\n--\n", FILE_APPEND);
  107. }
  108. }