Advertisement
roarijo89

Untitled

Apr 28th, 2024
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.55 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\User\Auth;
  4.  
  5. use App\Http\Controllers\Controller;
  6. use App\Models\AdminNotification;
  7. use App\Models\User;
  8. use App\Models\UserLogin;
  9. use Illuminate\Auth\Events\Registered;
  10. use Illuminate\Foundation\Auth\RegistersUsers;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Hash;
  13. use Illuminate\Support\Facades\Http;
  14. use Illuminate\Support\Facades\Validator;
  15. use Illuminate\Validation\Rules\Password;
  16.  
  17. class RegisterController extends Controller
  18. {
  19.     use RegistersUsers;
  20.  
  21.     public function __construct()
  22.     {
  23.         $this->middleware('guest');
  24.         $this->middleware('registration.status')->except('registrationNotAllowed');
  25.         $this->activeTemplate = activeTemplate();
  26.     }
  27.  
  28.     public function showRegistrationForm()
  29.     {
  30.         $pageTitle = "Register";
  31.         $info = json_decode(json_encode(getIpInfo()), true);
  32.         $mobileCode = @implode(',', $info['code']);
  33.         $countries = json_decode(file_get_contents(resource_path('views/includes/country.json')));
  34.         return view($this->activeTemplate . 'user.auth.register', compact('pageTitle', 'mobileCode', 'countries'));
  35.     }
  36.  
  37.     protected function isDisposableEmail($email)
  38.     {
  39.         $response = Http::withHeaders([
  40.             'APITOKEN' => '111-111-111-111-111', # get api token here for free :  https://dashboard.api-aries.online/
  41.        ])->get('https://api.api-aries.online/v1/checkers/proxy/email/?email=' . urlencode($email));
  42.  
  43.         $data = $response->json();
  44.  
  45.         return $data['disposable'] === 'yes';
  46.     }
  47.  
  48.     protected function validator(array $data)
  49. {
  50.     $general = gs();
  51.     $passwordValidation = Password::min(6);
  52.     if ($general->secure_password) {
  53.         $passwordValidation = $passwordValidation->mixedCase()->numbers()->symbols()->uncompromised();
  54.     }
  55.     $agree = 'nullable';
  56.     if ($general->agree) {
  57.         $agree = 'required';
  58.     }
  59.     $countryData = (array) json_decode(file_get_contents(resource_path('views/includes/country.json')));
  60.     $countryCodes = implode(',', array_keys($countryData));
  61.     $mobileCodes = implode(',', array_column($countryData, 'dial_code'));
  62.     $countries = implode(',', array_column($countryData, 'country'));
  63.     $validate = Validator::make($data, [
  64.         'email' => ['required', 'string', 'email', 'unique:users', function ($attribute, $value, $fail) {
  65.             if ($this->isDisposableEmail($value)) {
  66.                 $fail('The email is disposable.');
  67.             }
  68.         }],
  69.         'mobile' => 'required|regex:/^([0-9]*)$/',
  70.         'password' => ['required', 'confirmed', $passwordValidation],
  71.         'username' => 'required|unique:users|min:6',
  72.         'captcha' => 'sometimes|required',
  73.         'mobile_code' => 'required|in:' . $mobileCodes,
  74.         'country_code' => 'required|in:' . $countryCodes,
  75.         'country' => 'required|in:' . $countries,
  76.         'agree' => $agree
  77.     ]);
  78.  
  79.     // Check if validation fails
  80.     if ($validate->fails()) {
  81.         return $validate;
  82.     }
  83.  
  84.     // Additional check for mobile uniqueness
  85.     $exist = User::where('mobile', $data['mobile_code'] . $data['mobile'])->first();
  86.     if ($exist) {
  87.         $validate->errors()->add('mobile', 'The mobile number already exists');
  88.     }
  89.  
  90.     return $validate;
  91. }
  92.  
  93.  
  94.     public function register(Request $request)
  95.     {
  96.         $this->validator($request->all())->validate();
  97.  
  98.         $request->session()->regenerateToken();
  99.  
  100.         if (preg_match("/[^a-z0-9_]/", trim($request->username))) {
  101.             $notify[] = ['info', 'Username can contain only small letters, numbers and underscore.'];
  102.             $notify[] = ['error', 'No special character, space or capital letters in username.'];
  103.             return back()->withNotify($notify)->withInput($request->all());
  104.         }
  105.  
  106.         if (!verifyCaptcha()) {
  107.             $notify[] = ['error', 'Invalid captcha provided'];
  108.             return back()->withNotify($notify);
  109.         }
  110.  
  111.         $exist = User::where('mobile', $request->mobile_code . $request->mobile)->first();
  112.         if ($exist) {
  113.             $notify[] = ['error', 'The mobile number already exists'];
  114.             return back()->withNotify($notify)->withInput();
  115.         }
  116.  
  117.         event(new Registered($user = $this->create($request->all())));
  118.  
  119.         $this->guard()->login($user);
  120.  
  121.         return $this->registered($request, $user)
  122.             ?: redirect($this->redirectPath());
  123.     }
  124.  
  125.     protected function create(array $data)
  126.     {
  127.         $general = gs();
  128.  
  129.         $referUser = null;
  130.         //User Create
  131.         $user = new User();
  132.         $user->email = strtolower(trim($data['email']));
  133.         $user->password = Hash::make($data['password']);
  134.         $user->username = trim($data['username']);
  135.         $user->ref_by = $referUser ? $referUser->id : 0;
  136.         $user->country_code = $data['country_code'];
  137.         $user->mobile = $data['mobile_code'] . $data['mobile'];
  138.         $user->address = [
  139.             'address' => '',
  140.             'state' => '',
  141.             'zip' => '',
  142.             'country' => isset($data['country']) ? $data['country'] : null,
  143.             'city' => ''
  144.         ];
  145.         $user->status = 1;
  146.         $user->kv = $general->kv ? 0 : 1;
  147.         $user->ev = $general->ev ? 0 : 1;
  148.         $user->sv = $general->sv ? 0 : 1;
  149.         $user->ts = 0;
  150.         $user->tv = 1;
  151.         $user->save();
  152.  
  153.  
  154.         $adminNotification = new AdminNotification();
  155.         $adminNotification->user_id = $user->id;
  156.         $adminNotification->title = 'New member registered';
  157.         $adminNotification->click_url = urlPath('admin.users.detail', $user->id);
  158.         $adminNotification->save();
  159.  
  160.  
  161.         //Login Log Create
  162.         $ip = getRealIP();
  163.         $exist = UserLogin::where('user_ip', $ip)->first();
  164.         $userLogin = new UserLogin();
  165.  
  166.         //Check exist or not
  167.         if ($exist) {
  168.             $userLogin->longitude =  $exist->longitude;
  169.             $userLogin->latitude =  $exist->latitude;
  170.             $userLogin->city =  $exist->city;
  171.             $userLogin->country_code = $exist->country_code;
  172.             $userLogin->country =  $exist->country;
  173.         } else {
  174.             $info = json_decode(json_encode(getIpInfo()), true);
  175.             $userLogin->longitude =  @implode(',', $info['long']);
  176.             $userLogin->latitude =  @implode(',', $info['lat']);
  177.             $userLogin->city =  @implode(',', $info['city']);
  178.             $userLogin->country_code = @implode(',', $info['code']);
  179.             $userLogin->country =  @implode(',', $info['country']);
  180.         }
  181.  
  182.         $userAgent = osBrowser();
  183.         $userLogin->user_id = $user->id;
  184.         $userLogin->user_ip =  $ip;
  185.  
  186.         $userLogin->browser = @$userAgent['browser'];
  187.         $userLogin->os = @$userAgent['os_platform'];
  188.         $userLogin->save();
  189.  
  190.         return $user;
  191.     }
  192.  
  193.     public function checkUser(Request $request)
  194.     {
  195.         $exist['data'] = false;
  196.         $exist['type'] = null;
  197.         if ($request->email) {
  198.             $exist['data'] = User::where('email', $request->email)->exists();
  199.             $exist['type'] = 'email';
  200.         }
  201.         if ($request->mobile) {
  202.             $exist['data'] = User::where('mobile', $request->mobile)->exists();
  203.             $exist['type'] = 'mobile';
  204.         }
  205.         if ($request->username) {
  206.             $exist['data'] = User::where('username', $request->username)->exists();
  207.             $exist['type'] = 'username';
  208.         }
  209.         return response($exist);
  210.     }
  211.  
  212.     public function registered()
  213.     {
  214.         return to_route('user.home');
  215.     }
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement