forms.php 12 KB

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