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.

134 lines
3.7 KiB

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