baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / session / SessionDAO.inc.php
blob2895b867a6c47ad3d3a485522171e0687b93f54c
1 <?php
3 /**
4 * @file classes/session/SessionDAO.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class SessionDAO
10 * @ingroup session
11 * @see Session
13 * @brief Operations for retrieving and modifying Session objects.
16 // $Id: SessionDAO.inc.php,v 1.6 2009/05/13 16:35:48 asmecher Exp $
19 import('session.Session');
21 class SessionDAO extends DAO {
22 /**
23 * Retrieve a session by ID.
24 * @param $sessionId string
25 * @return Session
27 function &getSession($sessionId) {
28 $result =& $this->retrieve(
29 'SELECT * FROM sessions WHERE session_id = ?',
30 array($sessionId)
33 $session = null;
34 if ($result->RecordCount() != 0) {
35 $row =& $result->GetRowAssoc(false);
37 $session = new Session();
38 $session->setId($row['session_id']);
39 $session->setUserId($row['user_id']);
40 $session->setIpAddress($row['ip_address']);
41 $session->setUserAgent($row['user_agent']);
42 $session->setSecondsCreated($row['created']);
43 $session->setSecondsLastUsed($row['last_used']);
44 $session->setRemember($row['remember']);
45 $session->setSessionData($row['data']);
49 $result->Close();
50 unset($result);
52 return $session;
55 /**
56 * Insert a new session.
57 * @param $session Session
59 function insertSession(&$session) {
60 return $this->update(
61 'INSERT INTO sessions
62 (session_id, ip_address, user_agent, created, last_used, remember, data)
63 VALUES
64 (?, ?, ?, ?, ?, ?, ?)',
65 array(
66 $session->getId(),
67 $session->getIpAddress(),
68 $session->getUserAgent(),
69 (int) $session->getSecondsCreated(),
70 (int) $session->getSecondsLastUsed(),
71 $session->getRemember() ? 1 : 0,
72 $session->getSessionData()
77 /**
78 * Update an existing session.
79 * @param $session Session
81 function updateObject(&$session) {
82 return $this->update(
83 'UPDATE sessions
84 SET
85 user_id = ?,
86 ip_address = ?,
87 user_agent = ?,
88 created = ?,
89 last_used = ?,
90 remember = ?,
91 data = ?
92 WHERE session_id = ?',
93 array(
94 $session->getUserId()==''?null:(int) $session->getUserId(),
95 $session->getIpAddress(),
96 $session->getUserAgent(),
97 (int) $session->getSecondsCreated(),
98 (int) $session->getSecondsLastUsed(),
99 $session->getRemember() ? 1 : 0,
100 $session->getSessionData(),
101 $session->getId()
106 function updateSession(&$session) {
107 trigger_error('Deprecated function.');
108 return $this->updateObject($session);
112 * Delete a session.
113 * @param $session Session
115 function deleteObject(&$session) {
116 return $this->deleteSessionById($session->getId());
119 function deleteSession(&$session) {
120 trigger_error('Deprecated function.');
121 return $this->deleteObject($session);
125 * Delete a session by ID.
126 * @param $sessionId string
128 function deleteSessionById($sessionId) {
129 return $this->update(
130 'DELETE FROM sessions WHERE session_id = ?',
131 array($sessionId)
136 * Delete sessions by user ID.
137 * @param $userId string
139 function deleteSessionsByUserId($userId) {
140 return $this->update(
141 'DELETE FROM sessions WHERE user_id = ?',
142 array((int) $userId)
147 * Delete all sessions older than the specified time.
148 * @param $lastUsed int cut-off time in seconds for not-remembered sessions
149 * @param $lastUsedRemember int optional, cut-off time in seconds for remembered sessions
151 function deleteSessionByLastUsed($lastUsed, $lastUsedRemember = 0) {
152 if ($lastUsedRemember == 0) {
153 return $this->update(
154 'DELETE FROM sessions WHERE (last_used < ? AND remember = 0)',
155 array((int) $lastUsed)
157 } else {
158 return $this->update(
159 'DELETE FROM sessions WHERE (last_used < ? AND remember = 0) OR (last_used < ? AND remember = 1)',
160 array((int) $lastUsed, (int) $lastUsedRemember)
166 * Delete all sessions.
168 function deleteAllSessions() {
169 return $this->update('DELETE FROM sessions');
173 * Check if a session exists with the specified ID.
174 * @param $sessionId string
175 * @return boolean
177 function sessionExistsById($sessionId) {
178 $result =& $this->retrieve(
179 'SELECT COUNT(*) FROM sessions WHERE session_id = ?',
180 array($sessionId)
182 $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
184 $result->Close();
185 unset($result);
187 return $returner;