2021-06-05 09:26:32 +00:00
< ? php
namespace App\Http\Controllers\Auth ;
2023-04-25 07:22:30 +00:00
use App\Classes\Pterodactyl ;
2021-06-05 09:26:32 +00:00
use App\Http\Controllers\Controller ;
use App\Models\User ;
2022-06-02 14:11:24 +00:00
use App\Notifications\ReferralNotification ;
2021-06-05 09:26:32 +00:00
use App\Providers\RouteServiceProvider ;
2023-01-29 22:12:34 +00:00
use App\Traits\Referral ;
2022-06-02 14:11:24 +00:00
use Carbon\Carbon ;
2021-06-05 09:26:32 +00:00
use Illuminate\Foundation\Auth\RegistersUsers ;
2021-06-27 13:53:49 +00:00
use Illuminate\Support\Facades\App ;
2022-06-02 14:11:24 +00:00
use Illuminate\Support\Facades\DB ;
2021-06-05 09:26:32 +00:00
use Illuminate\Support\Facades\Hash ;
2023-01-23 20:34:22 +00:00
use Illuminate\Support\Facades\Log ;
2021-06-05 09:26:32 +00:00
use Illuminate\Support\Facades\Validator ;
2021-06-27 13:53:49 +00:00
use Illuminate\Support\Str ;
2021-12-26 22:37:16 +00:00
use Illuminate\Validation\ValidationException ;
2021-06-05 09:26:32 +00:00
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation . By default this controller uses a trait to
| provide this functionality without requiring any additional code .
|
*/
2023-01-29 22:12:34 +00:00
use RegistersUsers , Referral ;
2021-06-05 09:26:32 +00:00
/**
* Where to redirect users after registration .
*
* @ var string
*/
protected $redirectTo = RouteServiceProvider :: HOME ;
/**
* Create a new controller instance .
*
* @ return void
*/
2023-04-25 07:22:30 +00:00
public function __construct ()
2021-06-05 09:26:32 +00:00
{
$this -> middleware ( 'guest' );
}
/**
* Get a validator for an incoming registration request .
*
2023-01-05 17:01:42 +00:00
* @ param array $data
2021-06-05 09:26:32 +00:00
* @ return \Illuminate\Contracts\Validation\Validator
*/
2023-02-07 15:35:04 +00:00
protected function validator ( array $data )
2021-06-05 09:26:32 +00:00
{
2022-01-14 23:48:38 +00:00
$validationRules = [
2023-01-05 17:01:42 +00:00
'name' => [ 'required' , 'string' , 'max:30' , 'min:4' , 'alpha_num' , 'unique:users' ],
'email' => [ 'required' , 'string' , 'email' , 'max:64' , 'unique:users' ],
'password' => [ 'required' , 'string' , 'min:8' , 'confirmed' ],
2022-01-14 23:48:38 +00:00
];
2023-04-25 07:22:30 +00:00
if ( config ( 'SETTINGS::RECAPTCHA:ENABLED' ) == 'true' ) {
2022-01-14 23:48:38 +00:00
$validationRules [ 'g-recaptcha-response' ] = [ 'required' , 'recaptcha' ];
}
2023-04-25 07:22:30 +00:00
if ( config ( 'SETTINGS::SYSTEM:SHOW_TOS' ) == 'true' ) {
2023-01-04 10:51:35 +00:00
$validationRules [ 'terms' ] = [ 'required' ];
}
2022-01-14 23:48:38 +00:00
2023-04-25 07:22:30 +00:00
if ( config ( 'SETTINGS::SYSTEM:REGISTER_IP_CHECK' , 'true' ) == 'true' ) {
2021-06-27 13:53:49 +00:00
//check if ip has already made an account
$data [ 'ip' ] = session () -> get ( 'ip' ) ? ? request () -> ip ();
2023-01-05 17:01:42 +00:00
if ( User :: where ( 'ip' , '=' , request () -> ip ()) -> exists ()) {
session () -> put ( 'ip' , request () -> ip ());
}
$validationRules [ 'ip' ] = [ 'unique:users' ];
2022-01-14 23:48:38 +00:00
return Validator :: make ( $data , $validationRules , [
2023-01-05 17:01:42 +00:00
'ip.unique' => 'You have already made an account! Please contact support if you think this is incorrect.' ,
2021-06-27 13:53:49 +00:00
]);
}
2021-06-05 09:26:32 +00:00
2022-01-14 23:48:38 +00:00
return Validator :: make ( $data , $validationRules );
2021-06-05 09:26:32 +00:00
}
/**
* Create a new user instance after a valid registration .
*
2023-01-05 17:01:42 +00:00
* @ param array $data
2021-06-27 13:53:49 +00:00
* @ return User
2021-06-05 09:26:32 +00:00
*/
2023-02-07 15:35:04 +00:00
protected function create ( array $data )
2021-06-05 09:26:32 +00:00
{
$user = User :: create ([
2023-01-05 17:01:42 +00:00
'name' => $data [ 'name' ],
'email' => $data [ 'email' ],
2023-04-25 07:22:30 +00:00
'credits' => config ( 'SETTINGS::USER:INITIAL_CREDITS' , 150 ),
'server_limit' => config ( 'SETTINGS::USER:INITIAL_SERVER_LIMIT' , 1 ),
2023-01-05 17:01:42 +00:00
'password' => Hash :: make ( $data [ 'password' ]),
2022-06-02 14:11:24 +00:00
'referral_code' => $this -> createReferralCode (),
2021-06-05 09:26:32 +00:00
]);
2023-04-25 07:22:30 +00:00
$response = Pterodactyl :: client () -> post ( '/application/users' , [
'external_id' => App :: environment ( 'local' ) ? Str :: random ( 16 ) : ( string ) $user -> id ,
2023-01-05 17:01:42 +00:00
'username' => $user -> name ,
'email' => $user -> email ,
'first_name' => $user -> name ,
'last_name' => $user -> name ,
'password' => $data [ 'password' ],
'root_admin' => false ,
'language' => 'en' ,
2021-06-05 09:26:32 +00:00
]);
if ( $response -> failed ()) {
$user -> delete ();
2023-01-23 20:34:22 +00:00
Log :: error ( 'Pterodactyl Registration Error: ' . $response -> json ()[ 'errors' ][ 0 ][ 'detail' ]);
2021-12-26 22:45:40 +00:00
throw ValidationException :: withMessages ([
2021-12-27 06:58:46 +00:00
'ptero_registration_error' => [ __ ( 'Account already exists on Pterodactyl. Please contact the Support!' )],
2021-12-26 22:45:40 +00:00
]);
2021-06-05 09:26:32 +00:00
}
2023-04-25 07:22:30 +00:00
$user -> update ([
'pterodactyl_id' => $response -> json ()[ 'attributes' ][ 'id' ],
]);
2021-06-05 09:26:32 +00:00
2022-06-02 14:11:24 +00:00
//INCREMENT REFERRAL-USER CREDITS
2023-01-23 20:34:22 +00:00
if ( ! empty ( $data [ 'referral_code' ])) {
2022-06-02 14:11:24 +00:00
$ref_code = $data [ 'referral_code' ];
$new_user = $user -> id ;
2023-01-05 17:01:42 +00:00
if ( $ref_user = User :: query () -> where ( 'referral_code' , '=' , $ref_code ) -> first ()) {
2023-04-25 07:22:30 +00:00
if ( config ( 'SETTINGS::REFERRAL:MODE' ) == 'sign-up' || config ( 'SETTINGS::REFERRAL:MODE' ) == 'both' ) {
$ref_user -> increment ( 'credits' , config ( 'SETTINGS::REFERRAL::REWARD' ));
2022-06-07 08:05:19 +00:00
$ref_user -> notify ( new ReferralNotification ( $ref_user -> id , $new_user ));
2022-06-08 08:11:55 +00:00
//LOGS REFERRALS IN THE ACTIVITY LOG
activity ()
-> performedOn ( $user )
-> causedBy ( $ref_user )
2023-04-25 07:22:30 +00:00
-> log ( 'gained ' . config ( 'SETTINGS::REFERRAL::REWARD' ) . ' ' . config ( 'SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME' ) . ' for sign-up-referral of ' . $user -> name . ' (ID:' . $user -> id . ')' );
2022-06-07 08:05:19 +00:00
}
2022-06-02 14:11:24 +00:00
//INSERT INTO USER_REFERRALS TABLE
DB :: table ( 'user_referrals' ) -> insert ([
'referral_id' => $ref_user -> id ,
'registered_user_id' => $user -> id ,
2023-01-05 17:01:42 +00:00
'created_at' => Carbon :: now (),
'updated_at' => Carbon :: now (),
2022-06-02 14:11:24 +00:00
]);
}
}
2021-12-26 22:37:16 +00:00
2021-06-05 09:26:32 +00:00
return $user ;
}
}