Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
SessionDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.session.Session');
19 
20 class SessionDAO extends DAO {
24  function SessionDAO() {
25  parent::DAO();
26  }
27 
31  function newDataObject() {
32  return new Session();
33  }
34 
40  function &getSession($sessionId) {
41  $result = $this->retrieve(
42  'SELECT * FROM sessions WHERE session_id = ?',
43  array($sessionId)
44  );
45 
46  $session = null;
47  if ($result->RecordCount() != 0) {
48  $row = $result->GetRowAssoc(false);
49 
50  $session = $this->newDataObject();
51  $session->setId($row['session_id']);
52  $session->setUserId($row['user_id']);
53  $session->setIpAddress($row['ip_address']);
54  $session->setUserAgent($row['user_agent']);
55  $session->setSecondsCreated($row['created']);
56  $session->setSecondsLastUsed($row['last_used']);
57  $session->setRemember($row['remember']);
58  $session->setSessionData($row['data']);
59  }
60 
61  $result->Close();
62  return $session;
63  }
64 
69  function insertObject($session) {
70  return $this->update(
71  'INSERT INTO sessions
72  (session_id, ip_address, user_agent, created, last_used, remember, data)
73  VALUES
74  (?, ?, ?, ?, ?, ?, ?)',
75  array(
76  $session->getId(),
77  $session->getIpAddress(),
78  substr($session->getUserAgent(), 0, 255),
79  (int) $session->getSecondsCreated(),
80  (int) $session->getSecondsLastUsed(),
81  $session->getRemember() ? 1 : 0,
82  $session->getSessionData()
83  )
84  );
85  }
86 
91  function updateObject(&$session) {
92  return $this->update(
93  'UPDATE sessions
94  SET
95  user_id = ?,
96  ip_address = ?,
97  user_agent = ?,
98  created = ?,
99  last_used = ?,
100  remember = ?,
101  data = ?
102  WHERE session_id = ?',
103  array(
104  $session->getUserId()==''?null:(int) $session->getUserId(),
105  $session->getIpAddress(),
106  substr($session->getUserAgent(), 0, 255),
107  (int) $session->getSecondsCreated(),
108  (int) $session->getSecondsLastUsed(),
109  $session->getRemember() ? 1 : 0,
110  $session->getSessionData(),
111  $session->getId()
112  )
113  );
114  }
115 
120  function deleteObject(&$session) {
121  return $this->deleteById($session->getId());
122  }
123 
128  function deleteById($sessionId) {
129  return $this->update(
130  'DELETE FROM sessions WHERE session_id = ?',
131  array($sessionId)
132  );
133  }
134 
139  function deleteByUserId($userId) {
140  return $this->update(
141  'DELETE FROM sessions WHERE user_id = ?',
142  array((int) $userId)
143  );
144  }
145 
151  function deleteByLastUsed($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)
156  );
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)
161  );
162  }
163  }
164 
168  function deleteAllSessions() {
169  return $this->update('DELETE FROM sessions');
170  }
171 
177  function sessionExistsById($sessionId) {
178  $result = $this->retrieve(
179  'SELECT COUNT(*) FROM sessions WHERE session_id = ?',
180  array($sessionId)
181  );
182  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
183 
184  $result->Close();
185  return $returner;
186  }
187 }
188 
189 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
sessionExistsById($sessionId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
deleteById($sessionId)
deleteByLastUsed($lastUsed, $lastUsedRemember=0)
insertObject($session)
& getSession($sessionId)
updateObject(&$session)
Operations for retrieving and modifying Session objects.
Maintains user state information from one request to the next.
Definition: Session.inc.php:22
deleteObject(&$session)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
deleteByUserId($userId)