RegisterController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Classes\Pterodactyl;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Settings;
  6. use App\Models\User;
  7. use App\Notifications\ReferralNotification;
  8. use App\Providers\RouteServiceProvider;
  9. use Carbon\Carbon;
  10. use Illuminate\Foundation\Auth\RegistersUsers;
  11. use Illuminate\Support\Facades\App;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Hash;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Validator;
  17. use Illuminate\Support\Str;
  18. use Illuminate\Validation\ValidationException;
  19. class RegisterController extends Controller
  20. {
  21. /*
  22. |--------------------------------------------------------------------------
  23. | Register Controller
  24. |--------------------------------------------------------------------------
  25. |
  26. | This controller handles the registration of new users as well as their
  27. | validation and creation. By default this controller uses a trait to
  28. | provide this functionality without requiring any additional code.
  29. |
  30. */
  31. use RegistersUsers;
  32. /**
  33. * Where to redirect users after registration.
  34. *
  35. * @var string
  36. */
  37. protected $redirectTo = RouteServiceProvider::HOME;
  38. /**
  39. * Create a new controller instance.
  40. *
  41. * @return void
  42. */
  43. public function __construct()
  44. {
  45. $this->middleware('guest');
  46. }
  47. /**
  48. * Get a validator for an incoming registration request.
  49. *
  50. * @param array $data
  51. * @return \Illuminate\Contracts\Validation\Validator
  52. */
  53. protected function validator(array $data)
  54. {
  55. $validationRules = [
  56. 'name' => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
  57. 'email' => ['required', 'string', 'email', 'max:64', 'unique:users'],
  58. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  59. ];
  60. if (config('SETTINGS::RECAPTCHA:ENABLED') == 'true') {
  61. $validationRules['g-recaptcha-response'] = ['required', 'recaptcha'];
  62. }
  63. if (config('SETTINGS::SYSTEM:SHOW_TOS') == 'true') {
  64. $validationRules['terms'] = ['required'];
  65. }
  66. if (config('SETTINGS::SYSTEM:REGISTER_IP_CHECK', 'true') == 'true') {
  67. //check if ip has already made an account
  68. $data['ip'] = session()->get('ip') ?? request()->ip();
  69. if (User::where('ip', '=', request()->ip())->exists()) session()->put('ip', request()->ip());
  70. $validationRules['ip'] = ['unique:users'];
  71. return Validator::make($data, $validationRules, [
  72. 'ip.unique' => "You have already made an account! Please contact support if you think this is incorrect."
  73. ]);
  74. }
  75. return Validator::make($data, $validationRules);
  76. }
  77. /**
  78. * Create a unique Referral Code for User
  79. * @return string
  80. */
  81. protected function createReferralCode(){
  82. $referralcode = STR::random(8);
  83. if (User::where('referral_code', '=', $referralcode)->exists()) {
  84. $this->createReferralCode();
  85. }
  86. return $referralcode;
  87. }
  88. /**
  89. * Create a new user instance after a valid registration.
  90. *
  91. * @param array $data
  92. * @return User
  93. */
  94. protected function create(array $data)
  95. {
  96. $user = User::create([
  97. 'name' => $data['name'],
  98. 'email' => $data['email'],
  99. 'credits' => config('SETTINGS::USER:INITIAL_CREDITS', 150),
  100. 'server_limit' => config('SETTINGS::USER:INITIAL_SERVER_LIMIT', 1),
  101. 'password' => Hash::make($data['password']),
  102. 'referral_code' => $this->createReferralCode(),
  103. ]);
  104. $response = Pterodactyl::client()->post('/application/users', [
  105. "external_id" => App::environment('local') ? Str::random(16) : (string)$user->id,
  106. "username" => $user->name,
  107. "email" => $user->email,
  108. "first_name" => $user->name,
  109. "last_name" => $user->name,
  110. "password" => $data['password'],
  111. "root_admin" => false,
  112. "language" => "en"
  113. ]);
  114. if ($response->failed()) {
  115. $user->delete();
  116. throw ValidationException::withMessages([
  117. 'ptero_registration_error' => [__('Account already exists on Pterodactyl. Please contact the Support!')],
  118. ]);
  119. }
  120. $user->update([
  121. 'pterodactyl_id' => $response->json()['attributes']['id']
  122. ]);
  123. //INCREMENT REFERRAL-USER CREDITS
  124. if(!empty($data['referral_code'])){
  125. $ref_code = $data['referral_code'];
  126. $new_user = $user->id;
  127. if($ref_user = User::query()->where('referral_code', '=', $ref_code)->first()) {
  128. if(config("SETTINGS::REFERRAL:MODE") == "sign-up" || config("SETTINGS::REFERRAL:MODE") == "both") {
  129. $ref_user->increment('credits', config("SETTINGS::REFERRAL::REWARD"));
  130. $ref_user->notify(new ReferralNotification($ref_user->id, $new_user));
  131. //LOGS REFERRALS IN THE ACTIVITY LOG
  132. activity()
  133. ->performedOn($user)
  134. ->causedBy($ref_user)
  135. ->log('gained '. config("SETTINGS::REFERRAL::REWARD").' '.config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME").' for sign-up-referral of '.$user->name.' (ID:'.$user->id.')');
  136. }
  137. //INSERT INTO USER_REFERRALS TABLE
  138. DB::table('user_referrals')->insert([
  139. 'referral_id' => $ref_user->id,
  140. 'registered_user_id' => $user->id,
  141. 'created_at' => Carbon::now(),
  142. 'updated_at' => Carbon::now()
  143. ]);
  144. }
  145. }
  146. return $user;
  147. }
  148. }