public function check() { $key = !empty($_SESSION['ts_sid']) ? $_SESSION['ts_sid'] : !1; if (!$key) { $cookies = Application::$request_variables['cookie']; $cookie_key = !empty($cookies['ts_sid']) ? $cookies['ts_sid'] : !1; if ($cookie_key) { $key_manager = new KeyManager(); list($user_id, $access_key) = $key_manager->getPair($cookie_key); if (!is_numeric($user_id)) { $this->removeCookie('ts_sid'); return; } $user = new User($this->db); $user->allocateUserById($user_id); if (!$user->accessKeyExists($access_key)) { $this->removeCookie('ts_sid'); return; } $this->setSession('ts_sid', $cookie_key); $this->user_row = $user->getObject(); $this->result = !$user->isEmpty(); } } else { $key_manager = new KeyManager(); $user_id = $key_manager->getPair($key)[0]; $user = new User($this->db); $user->allocateUserById($user_id); $this->user_row = $user->getObject(); $this->result = !$user->isEmpty(); } if ($this->result) { $user = new User($this->db, $this->user_row); if ($user->hasSubscription()) { setcookie('hs_sid', $this->generateCode(), time() + 365 * 24 * 3600, '/', 'twosphere.ru'); } else { if (isset($_COOKIE['hs_sid'])) { setcookie('hs_sid', '', 0, '/', 'twosphere.ru'); } } } }
public function run($method, $params) { $this->params = $params[$this->request_type]; $api_user = $this->getApiUser($this->params); $user = new User($this->db); $user->allocateUserById($api_user['user_id']); if (!$user->hasSubscription() && !in_array($method, self::$methods_without_sub)) { throw new NoSubscriptionException($this->params); } $microtime = microtime(true); if ($this->isBannedUser($api_user) && $this->expiredBanUser($api_user)) { $this->unbanUser($api_user); } if (!$this->isBannedUser($api_user)) { if ($api_user['recent_activity'] + self::DEFAULT_PERIOD >= $microtime) { $api_user['recent_count_activity']++; } else { $api_user['recent_count_activity'] = 1; $api_user['recent_activity'] = $microtime; $this->clearUserState($api_user); } if ($api_user['recent_count_activity'] > self::DEFAULT_MAX_QUERIES) { $api_user['ban'] = !0; $api_user['unlock_time'] = time() + self::DEFAULT_BAN_TIME; $this->banUser($api_user); } else { $this->incrementUserCountActivity($api_user); } if (!$this->isBannedUser($api_user)) { $this->updateUserStats($api_user, $method); $this->params['user'] = $user; $methods = new RequestHandler($this->db); return array("response" => $methods->{$method}($this->params)); } else { throw new TooManyRequestException($this->params, $api_user['unlock_time']); } } else { throw new TooManyRequestException($this->params, $api_user['unlock_time']); } }