From 5e95090defff64bc8cd7a318a73aa930948fb66d Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Mon, 15 Nov 2004 09:20:11 +0000 Subject: Initial import --- web/include/User.inc | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 web/include/User.inc (limited to 'web/include/User.inc') diff --git a/web/include/User.inc b/web/include/User.inc new file mode 100644 index 0000000..84d36b3 --- /dev/null +++ b/web/include/User.inc @@ -0,0 +1,150 @@ +session + * + * @param object Database Object + * @param object Session Object + * @param integer Login timout in seconds + */ + function User(&$database, &$session, $auth_timeout) { + assert($session); + assert($database); + assert($auth_timeout); + $this->session = &$session; + $this->auth_timeout = $auth_timeout; + $this->db = &$database; + + $last_seen = $this->session->data['user']['last_seen']; + $ref = $this->session->data['user']['ref']; + $authenticated = false; + + if ( $last_seen && + ( $last_seen + $this->auth_timeout >= time()) && + ( $time_seen <= time() ) && + $this->session->data['user']['authenticated'] && + $this->check_ref_session($ref) ) + $authenticated = true; + else { + $ref = false; + $authenticated = false; + $last_seen = false; + } + + $this->session->data['user']['authenticated'] = $authenticated; + $this->session->data['user']['last_seen'] = $authenticated ? time() : false; + $this->session->data['user']['ref'] = $ref; + } + + /** + * Check if given Ref is a valid user ref + * + * checks if the ref and current session are from the same user. + * + * @param integer user ref + * @returns boolean true if success + */ + function check_ref_session($ref) { + assert($this->session); + assert($this->session->ref); + assert($this->db); + assert($ref); + + $row=$this->db->query_row('SELECT ref FROM account WHERE session_ref=? AND ref=?', array($this->session->ref, $ref)); + + if ( $row === false ) + return false; + else + return true; + } + + /** + * Do the login of a user + * + * Check if username and password are a valid pair, and update session_ref + * in database, set timestamp. + * + * @param string supplied username from user + * @param string supplied password from user + * @returns boolean true if success + */ + function do_login($username, $password) + { + assert($this->session); + assert($this->session->ref); + assert($this->db); +# assert($username); +# assert($password); + if ( ! $username ) + return false; + if ( ! $password ) + return false; + + $row=$this->db->query_row('SELECT ref FROM account WHERE username=? AND password=?', array($username,$password)); + + if ( $row === false ) + return false; + + $update = $this->db->update('account', $row['ref'], array(session_ref=>$this->session->ref)); + if ( ! $update ) + return false; + + $this->session->data['user']['authenticated'] = true; + $this->session->data['user']['last_seen'] = time(); + $this->session->data['user']['ref'] = $row['ref']; + $this->session->data['user']['username'] = $username; + + # FIXME: old session is destroyed + return true; + } + + /** + * Logout the User + * + * logout the user, do not check if has logged in. + * + * @returns boolean true on success, false otherwhise + */ + function do_logout() + { + assert($this->session); + assert($this->session->ref); + $this->session->data['user']['authenticated'] = false; + $this->session->data['user']['last_seen'] = false; + $this->session->data['user']['ref'] = false; + $this->session->data['user']['username'] = false; + return true; + } + + /** + * Check for login status + * + * Checks if user has logged in correctly. + * + * @returns boolean true on authenticated, false otherwhise + */ + function check_login() + { + assert($this->session); + if (! $this->session->get_id()) + return false; + assert($this->session->ref); + return $this->session->data['user']['authenticated']; + } +} + +?> -- cgit v1.2.3