forms.php 11 KB

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