public function updateLastTime($force = false) { $time = $this->storage->read('user_last_datetime'); if (!$time || $force || $time == '0000-00-00 00:00:00' || time() - strtotime($time) > 120) { try { $login_log_model = new waLoginLogModel(); $last_activity = $login_log_model->getCurrent($this->id); } catch (waDbException $e) { if ($e->getCode() == 1146) { waSystem::getInstance()->getAuth()->clearAuth(); header("Location: " . wa()->getConfig()->getBackendUrl(true)); exit; } } $contact_model = new waContactModel(); $contact_info = $contact_model->getById($this->id); $auth = waSystem::getInstance()->getAuth(); if (!$auth->checkAuth($contact_info)) { header("Location: " . wa()->getConfig()->getRequestUrl(false)); exit; } if (!$contact_info || waSystem::getInstance()->getEnv() == 'backend' && !$contact_info['is_user']) { waSystem::getInstance()->getAuth()->clearAuth(); header("Location: " . wa()->getConfig()->getBackendUrl(true)); exit; } else { $this->setCache($contact_info); } if (!$time) { $time = $contact_info['last_datetime']; } if (!$last_activity) { $login_log_model->insert(array('contact_id' => $this->id, 'datetime_in' => date("Y-m-d H:i:s"), 'datetime_out' => $force == 'logout' ? date("Y-m-d H:i:s") : null)); // TODO: insert record in waLog } else { if ($force == 'logout') { $login_log_model->updateById($last_activity['id'], array('datetime_out' => date("Y-m-d H:i:s"))); } elseif ($last_datetime = strtotime($time)) { if (time() - $last_datetime > self::$options['activity_timeout']) { $login_log_model->updateById($last_activity['id'], array('datetime_out' => $time)); $login_log_model->insert(array('contact_id' => $this->id, 'datetime_in' => date("Y-m-d H:i:s"), 'datetime_out' => null)); // TODO: insert record in waLog } } } $t = date("Y-m-d H:i:s"); $contact_model->updateById($this->id, array('last_datetime' => $t)); $this->storage->write('user_last_datetime', $t); } }
public function init($options = null) { $cookie_defaults = session_get_cookie_params(); if (!isset($options['session_cookie_path']) && class_exists("waSystem")) { $options['session_cookie_path'] = waSystem::getInstance()->getRootUrl(); } $options = array_merge(array('session_id' => null, 'auto_start' => true, 'session_cookie_lifetime' => $cookie_defaults['lifetime'], 'session_cookie_path' => $cookie_defaults['path'], 'session_cookie_domain' => $cookie_defaults['domain'], 'session_cookie_secure' => $cookie_defaults['secure'], 'session_cookie_httponly' => true, 'session_cache_limiter' => 'none'), $options); // initialize parent parent::init($options); if (isset($this->options['session_name'])) { session_name($this->options['session_name']); } if (!(bool) ini_get('session.use_cookies') && ($session_id = $this->options['session_id'])) { session_id($session_id); } $lifetime = $this->options['session_cookie_lifetime']; $path = $this->options['session_cookie_path']; $domain = $this->options['session_cookie_domain']; $secure = $this->options['session_cookie_secure']; $http_only = $this->options['session_cookie_httponly']; session_set_cookie_params($lifetime, $path, $domain, $secure, $http_only); if (null !== $this->options['session_cache_limiter']) { session_cache_limiter($this->options['session_cache_limiter']); } if ($this->options['auto_start']) { if (isset($_COOKIE[session_name()])) { $this->open(); } } }