A
A
Andrey Nikitin2019-06-06 03:02:35
PHP
Andrey Nikitin, 2019-06-06 03:02:35

Authorization through VK stopped working, what could be the problem?

The site drike.online
The day before yesterday, authorization through VK stopped working, gives an error: ErrorException in LoginController.php line 27:
Undefined index: response
What could be the problem, can anyone come across?
Authorization code:

<?php


namespace App\Http\Controllers;

use Auth;
use App\User;
use DB;
use Carbon\Carbon;
use App\Settings;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function vklogin(Request $r)
    {
    $settings = Settings::where('id', 1)->first();
        $client_id = '*******';
        $client_secret = '********************';
        $redirect_uri = 'Drike.online';
        if (!is_null($r->code)) 
    {
      $obj = json_decode($this->curl('https://oauth.vk.com/access_token?v=5.95&client_id=' . $client_id . '&client_secret=' . $client_secret . '&redirect_uri=http://' . $redirect_uri . '/login&code=' . $r->code));
      if (isset($obj->access_token))
      {
        $info = json_decode($this->curl('https://api.vk.com/method/users.get?v=5.95&user_ids&fields=photo_200&access_token=' . $obj->access_token . '&v=5.95'), true);
        $user = User::where('login2', $info['response'][0]['uid'])->first();
        if($user == NULL) 
        {
          if(array_key_exists('photo_200', $info['response'][0]))
          {
            $photo = $info['response'][0]['photo_200'];
          }else
          {
            $photo = 'http://vk.com/images/camera_200.png';
          }
          if ($r->session()->has('ref')) {
            $has = DB::table('users')->where('ref_code', session('ref'))->first();
            if(!empty($has))
            {
              $ref_use = session('ref');
              $money = $settings->ref_sum;
            }
            else
            {
              $ref_use = NULL;
              $money = 0;
            }
          }
          else
          {
            $ref_use = NULL;
            $money = 0;
          }
          $user = User::create([
            'username' => $info['response'][0]['first_name'] . ' ' . $info['response'][0]['last_name'],
            'avatar' => $photo,
            'login' => 'id'.$info['response'][0]['uid'],
            'money' => $money,
            'login2' => $info['response'][0]['uid'],
            'ref_code' => $this->generate(),
            'ref_use' => $ref_use,
            'nick' => $this->generate_name()
          ]);
          
        } 
        else 
        {
          if(array_key_exists('photo_200', $info['response'][0]))
          {
            $photo = $info['response'][0]['photo_200'];
          }
          else
          {
            $photo = 'http://vk.com/images/camera_200.png';
          }
          $user->username = $info['response'][0]['first_name'] . ' ' . $info['response'][0]['last_name'];
          $user->avatar = $photo;
          $user->login = 'id'.$info['response'][0]['uid'];
          $user->login2 = $info['response'][0]['uid'];
          $user->save();
        }
        Auth::login($user, true);
        return redirect('/');
      }
    } 
    else 
    {
      return redirect('https://oauth.vk.com/authorize?v=5.95&client_id=' . $client_id . '&display=page&redirect_uri=http://' . $redirect_uri . '/login&scope=friends,photos,status,offline,&response_type=code&v=5.95');
    }
  }

    public function logout()
    {
        Auth::logout();
        return redirect('/');
    }

    public function curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function generate_name()
    {
        $length = 8;
        $chars = 'abdefhiknrstyzABDEFGHKNQRSTYZ23456789';
        $numChars = strlen($chars);
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= substr($chars, rand(1, $numChars) - 1, 1);
        }
        return $string;
    }

    public function generate()
    {
        $length = 13;
        $chars = 'abdefhiknrstyzABDEFGHKNQRSTYZ23456789';
        $numChars = strlen($chars);
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= substr($chars, rand(1, $numChars) - 1, 1);
        }
        return $string;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question