Пример #1
0
 /**
  * Construct the session handler
  * Fetch data from request headers and authenticate the user
  */
 public function __construct()
 {
     $config = Application::get_instance()->get_config();
     if (!is_null($config->get('runtime', 'token_lifespan'))) {
         $this->token_lifespan = (int) $config->get('runtime', 'token_lifespan');
     }
     $request = Request::get_instance();
     if (isset($request->get_request_headers()['Authorization'])) {
         $authorization_string = $request->get_request_headers()['Authorization'];
         $authorization_array = explode(':', $authorization_string);
         $name = $authorization_array[0];
         $token = $authorization_array[1];
         $referer = isset($request->server()['REMOTE_ADDR']) ? $request->server()['REMOTE_ADDR'] : '';
         $agent = isset($request->server()['HTTP_USER_AGENT']) ? $request->server()['HTTP_USER_AGENT'] : '';
         $token_id = Apine\User\Factory\UserTokenFactory::authentication($name, $token, $this->token_lifespan);
         $token = Apine\User\Factory\UserTokenFactory::create_by_id($token_id);
         if ($token_id && $token->get_origin() == $referer . $agent) {
             $this->logged_in = true;
             $this->token = $token;
             $this->session_type = $this->token->get_user()->get_type();
             $this->token->set_last_access_date(date('d M Y H:i:s', time() + $this->token_lifespan));
             $this->token->save();
         }
     } else {
         if (isset($_COOKIE['apine_session'])) {
             $session = new WebSession();
             $data = $session->data();
             if ($data != null) {
                 $user_id = $data->get_var('apine_user_id');
                 if ($user_id != null) {
                     $user = UserFactory::create_by_id($user_id);
                     $token = new UserToken();
                     $token->set_user($user);
                     $this->logged_in = true;
                     $this->token = $token;
                     $this->session_type = $data->get_var('apine_user_type');
                     $this->token->set_last_access_date(date('d M Y H:i:s', time() + $this->token_lifespan));
                 }
             }
         }
     }
 }