forms.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. use PHPMailer\PHPMailer\Exception;
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. require 'phpmailer/Exception.php';
  8. require 'phpmailer/PHPMailer.php';
  9. require 'phpmailer/SMTP.php';
  10. include 'functions.php';
  11. mysqli_report(MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ALL);
  12. if (isset($_POST['checkDB'])) {
  13. $values = [
  14. //SETTINGS::VALUE => REQUEST-VALUE (coming from the html-form)
  15. 'DB_HOST' => 'databasehost',
  16. 'DB_DATABASE' => 'database',
  17. 'DB_USERNAME' => 'databaseuser',
  18. 'DB_PASSWORD' => 'databaseuserpass',
  19. 'DB_PORT' => 'databaseport',
  20. 'DB_CONNECTION' => 'databasedriver',
  21. ];
  22. wh_log('Trying to connect to the Database', 'debug');
  23. try {
  24. $db = new mysqli($_POST['databasehost'], $_POST['databaseuser'], $_POST['databaseuserpass'], $_POST['database'], $_POST['databaseport']);
  25. } catch (mysqli_sql_exception $e) {
  26. wh_log($e->getMessage(), 'error');
  27. header('LOCATION: index.php?step=2&message=' . $e->getMessage());
  28. exit();
  29. }
  30. foreach ($values as $key => $value) {
  31. $param = $_POST[$value];
  32. // if ($key == "DB_PASSWORD") {
  33. // $param = '"' . $_POST[$value] . '"';
  34. // }
  35. setenv($key, $param);
  36. }
  37. wh_log('Database connection successful', 'debug');
  38. header('LOCATION: index.php?step=2.5');
  39. }
  40. if (isset($_POST['checkGeneral'])) {
  41. wh_log('setting app settings', 'debug');
  42. $appname = '"' . $_POST['name'] . '"';
  43. $appurl = $_POST['url'];
  44. if (substr($appurl, -1) === '/') {
  45. $appurl = substr_replace($appurl, '', -1);
  46. }
  47. setenv('APP_NAME', $appname);
  48. setenv('APP_URL', $appurl);
  49. wh_log('App settings set', 'debug');
  50. header('LOCATION: index.php?step=4');
  51. }
  52. if (isset($_POST['feedDB'])) {
  53. wh_log('Feeding the Database', 'debug');
  54. $logs = '';
  55. try {
  56. //$logs .= run_console(setenv('COMPOSER_HOME', dirname(__FILE__, 3) . '/vendor/bin/composer'));
  57. //$logs .= run_console('composer install --no-dev --optimize-autoloader');
  58. if (!str_contains(getenv('APP_KEY'), 'base64')) {
  59. $logs .= run_console('php artisan key:generate --force');
  60. } else {
  61. $logs .= "Key already exists. Skipping\n";
  62. }
  63. $logs .= run_console('php artisan storage:link');
  64. $logs .= run_console('php artisan migrate --seed --force');
  65. $logs .= run_console('php artisan db:seed --class=ExampleItemsSeeder --force');
  66. $logs .= run_console('php artisan db:seed --class=PermissionsSeeder --force');
  67. wh_log($logs, 'debug');
  68. wh_log('Feeding the Database successful', 'debug');
  69. header('LOCATION: index.php?step=3');
  70. } catch (\Throwable $th) {
  71. wh_log('Feeding the Database failed', 'error');
  72. header("LOCATION: index.php?step=2.5&message=" . $th->getMessage() . " <br>Please check the installer.log file in /var/www/controlpanel/storage/logs !");
  73. }
  74. }
  75. if (isset($_POST['checkSMTP'])) {
  76. wh_log('Checking SMTP Settings', 'debug');
  77. try {
  78. $mail = new PHPMailer(true);
  79. //Server settings
  80. $mail->isSMTP(); // Send using SMTP
  81. $mail->Host = $_POST['host']; // Set the SMTP server to send through
  82. $mail->SMTPAuth = true; // Enable SMTP authentication
  83. $mail->Username = $_POST['user']; // SMTP username
  84. $mail->Password = $_POST['pass']; // SMTP password
  85. $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
  86. $mail->Port = $_POST['port']; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS`
  87. //Recipients
  88. $mail->setFrom($_POST['user'], $_POST['user']);
  89. $mail->addAddress($_POST['user'], $_POST['user']); // Add a recipient
  90. // Content
  91. $mail->isHTML(true); // Set email format to HTML
  92. $mail->Subject = 'It Worked!';
  93. $mail->Body = 'Your E-Mail Settings are correct!';
  94. $mail->send();
  95. } catch (Exception $e) {
  96. wh_log($mail->ErrorInfo, 'error');
  97. header('LOCATION: index.php?step=4&message=Something wasnt right when sending the E-Mail!');
  98. exit();
  99. }
  100. wh_log('SMTP Settings are correct', 'debug');
  101. wh_log('Updating Database', 'debug');
  102. $db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
  103. if ($db->connect_error) {
  104. wh_log($db->connect_error, 'error');
  105. header('LOCATION: index.php?step=4&message=Could not connect to the Database: ');
  106. exit();
  107. }
  108. $values = [
  109. 'mail_mailer' => $_POST['method'],
  110. 'mail_host' => $_POST['host'],
  111. 'mail_port' => $_POST['port'],
  112. 'mail_username' => $_POST['user'],
  113. 'mail_password' => $_POST['pass'],
  114. 'mail_encryption' => $_POST['encryption'],
  115. 'mail_from_address' => $_POST['user'],
  116. ];
  117. foreach ($values as $key => $value) {
  118. $query = 'UPDATE `' . getenv('DB_DATABASE') . "`.`settings` SET `payload` = '$value' WHERE `name` = '$key' AND `group` = 'mail'";
  119. $db->query($query);
  120. }
  121. wh_log('Database updated', 'debug');
  122. header('LOCATION: index.php?step=5');
  123. }
  124. if (isset($_POST['checkPtero'])) {
  125. wh_log('Checking Pterodactyl Settings', 'debug');
  126. $url = $_POST['url'];
  127. $key = $_POST['key'];
  128. $clientkey = $_POST['clientkey'];
  129. if (substr($url, -1) === '/') {
  130. $url = substr_replace($url, '', -1);
  131. }
  132. $callpteroURL = $url . '/api/client/account';
  133. $call = curl_init();
  134. curl_setopt($call, CURLOPT_URL, $callpteroURL);
  135. curl_setopt($call, CURLOPT_RETURNTRANSFER, true);
  136. curl_setopt($call, CURLOPT_HTTPHEADER, [
  137. 'Accept: Application/vnd.pterodactyl.v1+json',
  138. 'Content-Type: application/json',
  139. 'Authorization: Bearer ' . $clientkey,
  140. ]);
  141. $callresponse = curl_exec($call);
  142. $callresult = json_decode($callresponse, true);
  143. curl_close($call); // Close the connection
  144. $pteroURL = $url . '/api/application/users';
  145. $ch = curl_init();
  146. curl_setopt($ch, CURLOPT_URL, $pteroURL);
  147. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  148. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  149. 'Accept: Application/vnd.pterodactyl.v1+json',
  150. 'Content-Type: application/json',
  151. 'Authorization: Bearer ' . $key,
  152. ]);
  153. $response = curl_exec($ch);
  154. $result = json_decode($response, true);
  155. curl_close($ch); // Close the connection
  156. if (!is_array($result) and $result['errors'][0] !== null) {
  157. header('LOCATION: index.php?step=5&message=Couldn\'t connect to Pterodactyl. Make sure your API key has all read and write permissions!');
  158. wh_log('API CALL ERROR: ' . $result['errors'][0]['code'], 'error');
  159. exit();
  160. } elseif (!is_array($callresult) and $callresult['errors'][0] !== null or $callresult['attributes']['admin'] == false) {
  161. header('LOCATION: index.php?step=5&message=Your ClientAPI Key is wrong or the account is not an admin!');
  162. wh_log('API CALL ERROR: ' . $callresult['errors'][0]['code'], 'error');
  163. exit();
  164. } else {
  165. wh_log('Pterodactyl Settings are correct', 'debug');
  166. wh_log('Updating Database', 'debug');
  167. $key = $key;
  168. $clientkey = $clientkey;
  169. $query1 = 'UPDATE `' . getenv('DB_DATABASE') . "`.`settings` SET `payload` = '" . json_encode($url) . "' WHERE (`name` = 'panel_url' AND `group` = 'pterodactyl')";
  170. $query2 = 'UPDATE `' . getenv('DB_DATABASE') . "`.`settings` SET `payload` = '" . json_encode($key) . "' WHERE (`name` = 'admin_token' AND `group` = 'pterodactyl')";
  171. $query3 = 'UPDATE `' . getenv('DB_DATABASE') . "`.`settings` SET `payload` = '" . json_encode($clientkey) . "' WHERE (`name` = 'user_token' AND `group` = 'pterodactyl')";
  172. $db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
  173. if ($db->connect_error) {
  174. wh_log($db->connect_error, 'error');
  175. header('LOCATION: index.php?step=5&message=Could not connect to the Database');
  176. exit();
  177. }
  178. if ($db->query($query1) && $db->query($query2) && $db->query($query3)) {
  179. wh_log('Database updated', 'debug');
  180. header('LOCATION: index.php?step=6');
  181. } else {
  182. wh_log($db->error, 'error');
  183. header('LOCATION: index.php?step=5&message=Something went wrong when communicating with the Database!');
  184. }
  185. }
  186. }
  187. if (isset($_POST['createUser'])) {
  188. wh_log('Creating User', 'debug');
  189. $db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
  190. if ($db->connect_error) {
  191. wh_log($db->connect_error, 'error');
  192. header('LOCATION: index.php?step=6&message=Could not connect to the Database');
  193. exit();
  194. }
  195. $pteroID = $_POST['pteroID'];
  196. $pass = $_POST['pass'];
  197. $repass = $_POST['repass'];
  198. $key = $db->query('SELECT `payload` FROM `' . getenv('DB_DATABASE') . "`.`settings` WHERE `name` = 'admin_token' AND `group` = 'pterodactyl'")->fetch_assoc();
  199. $key = removeQuotes($key['payload']);
  200. $pterobaseurl = $db->query('SELECT `payload` FROM `' . getenv('DB_DATABASE') . "`.`settings` WHERE `name` = 'panel_url' AND `group` = 'pterodactyl'")->fetch_assoc();
  201. $pteroURL = removeQuotes($pterobaseurl['payload']) . '/api/application/users/' . $pteroID;
  202. $ch = curl_init();
  203. curl_setopt($ch, CURLOPT_URL, $pteroURL);
  204. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  205. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  206. 'Accept: application/json',
  207. 'Content-Type: application/json',
  208. 'Authorization: Bearer ' . $key,
  209. ]);
  210. $response = curl_exec($ch);
  211. $result = json_decode($response, true);
  212. curl_close($ch); // Close the connection
  213. if (!$result['attributes']['email']) {
  214. header('LOCATION: index.php?step=6&message=Could not find the user with pterodactyl ID ' . $pteroID);
  215. exit();
  216. }
  217. if ($pass !== $repass) {
  218. header('LOCATION: index.php?step=6&message=The Passwords did not match!');
  219. exit();
  220. }
  221. $mail = $result['attributes']['email'];
  222. $name = $result['attributes']['username'];
  223. $pass = password_hash($pass, PASSWORD_DEFAULT);
  224. $pteroURL = removeQuotes($pterobaseurl['payload']) . '/api/application/users/' . $pteroID;
  225. $ch = curl_init();
  226. curl_setopt($ch, CURLOPT_URL, $pteroURL);
  227. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  228. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  229. 'Accept: application/json',
  230. 'Content-Type: application/json',
  231. 'Authorization: Bearer ' . $key,
  232. ]);
  233. curl_setopt($ch, CURLOPT_POSTFIELDS, [
  234. 'email' => $mail,
  235. 'username' => $name,
  236. 'first_name' => $name,
  237. 'last_name' => $name,
  238. 'password' => $pass,
  239. ]);
  240. $response = curl_exec($ch);
  241. $result = json_decode($response, true);
  242. curl_close($ch); // Close the connection
  243. if (!is_array($result) or in_array($result['errors'][0]['code'], $result)) {
  244. header('LOCATION: index.php?step=5&message=Couldn\'t connect to Pterodactyl. Make sure your API key has all read and write permissions!');
  245. exit();
  246. }
  247. $random = generateRandomString();
  248. $query1 = 'INSERT INTO `' . getenv('DB_DATABASE') . "`.`users` (`name`, `role`, `credits`, `server_limit`, `pterodactyl_id`, `email`, `password`, `created_at`, `referral_code`) VALUES ('$name', 'admin', '250', '1', '$pteroID', '$mail', '$pass', CURRENT_TIMESTAMP, '$random')";
  249. $query2 = "INSERT INTO `" . getenv('DB_DATABASE') . "`.`model_has_roles` (`role_id`, `model_type`, `model_id`) VALUES ('1', 'App\\\Models\\\User', '1')";
  250. if ($db->query($query1) && $db->query($query2)) {
  251. wh_log('Created user with Email ' . $mail . ' and pterodactyl ID ' . $pteroID, 'info');
  252. header('LOCATION: index.php?step=7');
  253. } else {
  254. wh_log($db->error, 'error');
  255. header('LOCATION: index.php?step=6&message=Something went wrong when communicating with the Database');
  256. }
  257. }