Пример #1
0
 /**
  * @return \Illuminate\Http\JsonResponse
  */
 public function auth()
 {
     //TODO rate limit
     //TODO Validation
     $udid = Input::get('udid');
     $phone = Input::get('phone');
     if (strlen($phone) < 9) {
         return $this->respondInsufficientPrivileges('Номер телефона слишком короткий');
     }
     if (intval($phone[2]) == 0) {
         return $this->respondInsufficientPrivileges('Вы пытаетесь зарегистрировать несуществующий номер.');
     }
     $providerCode = intval($phone[0] . $phone[1]);
     if (!in_array($providerCode, [99, 50, 51, 55, 70, 77])) {
         return $this->respondInsufficientPrivileges('Таких операторов не существует.');
     }
     $type = Input::has('type') ? Input::get('type') : 'verify';
     $phoneModel = Phone::withTrashed()->where('number', $phone)->first();
     if ($phoneModel && $type != 'reset') {
         if (!$phoneModel->deleted_at) {
             return $this->respondInsufficientPrivileges("Номер занят");
         }
         if (Carbon::now()->lte(Carbon::parse($phoneModel->deleted_at)->addDays(30))) {
             $allowedTime = Carbon::parse($phoneModel->deleted_at)->addDays(30);
             return $this->respondInsufficientPrivileges("Данный номер телефона не доступен для регистрации до {$allowedTime}");
         }
     }
     if ($type == 'changePhone') {
         if ($phoneModel) {
             return $this->respondInsufficientPrivileges("Number is already occupied");
         }
         $header = Request::header('Authorization');
         if (is_null($header)) {
             return $this->respondInvalidApi('Unauthorized');
         }
         if (!isset(explode(' ', $header)[1])) {
             return $this->respondInvalidApi('Unauthorized');
         }
         $token = explode(' ', $header)[1];
         if ($token) {
             $device = Device::where('auth_token', $token)->orderBy('id', 'desc')->first();
             $user = $device->user;
             if (!$user->checkPasswordAttribute(Input::get('password'))) {
                 return $this->respondInsufficientPrivileges('Wrong password');
             }
         }
     }
     if (!$phoneModel && $type == 'reset') {
         return $this->respondInsufficientPrivileges('Number not found');
     }
     $tempSMS = SMS::where('device', $udid)->where('phone', $phone)->orderBy('id')->first();
     if ($tempSMS) {
         $minTime = Carbon::parse($tempSMS->sent_at)->addMinutes(3);
         $maxTime = Carbon::parse($tempSMS->sent_at)->addHour();
         if ($tempSMS->verified) {
             if (Carbon::now()->lte($maxTime)) {
                 return $this->respond(['status' => 3, 'token' => $tempSMS->token]);
             }
         }
         if (Carbon::now()->lte($minTime)) {
             return $this->respond(['message' => 'Please wait ' . Carbon::now()->diffInSeconds($minTime) . ' seconds', 'timeout' => Carbon::now()->diffInSeconds($minTime), 'token' => $tempSMS->token, 'status' => 2]);
         }
         $tempSMS->code = rand(1000, 10000);
         $tempSMS->sent_at = Carbon::now()->toDateTimeString();
         // TODO Send sms
         $smsSender = new smsSender(fetchNumber($phone), $tempSMS->code);
         $smsSender->send(function ($res) {
             //				dd($res->xml());
         });
         $tempSMS->save();
         return $this->respond(['timeout' => 180, 'token' => $tempSMS->token, 'status' => 2]);
     } else {
         $code = rand(1000, 10000);
         $newSMSEntry = SMS::create(['phone' => $phone, 'code' => $code, 'token' => base64_encode(openssl_random_pseudo_bytes(32)), 'device' => $udid, 'sent_at' => Carbon::now()->toDateTimeString()]);
     }
     //TODO Send an SMS
     $smsSender = new smsSender(fetchNumber($phone), $code);
     $smsSender->send(function ($res) {
         //			dd($res->xml());
     });
     //SMS Logic
     if (isset($newSMSEntry)) {
         return $this->respond(['timeout' => 180, 'token' => $newSMSEntry->token, 'status' => 2]);
     }
     return $this->respondServerError('Something went wrong');
 }