Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
CaptchaDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.captcha.Captcha');
19 
20 class CaptchaDAO extends DAO {
24  function CaptchaDAO() {
25  parent::DAO();
26  }
27 
33  function &getCaptchasBySessionId($sessionId) {
34  $captchas = array();
35 
36  $result =& $this->retrieve(
37  'SELECT * FROM captchas WHERE session_id = ?',
38  array((int) $sessionId)
39  );
40 
41  while (!$result->EOF) {
42  $captchas[] =& $this->_returnCaptchaFromRow($result->GetRowAssoc(false));
43  $result->MoveNext();
44  }
45 
46  $result->Close();
47  unset($result);
48 
49  return $captchas;
50  }
51 
57  function &getExpiredCaptchas($lifespan = 86400) {
58  $captchas = array();
59  $threshold = time() - $lifespan;
60 
61  $result =& $this->retrieve(
62  'SELECT c.*
63  FROM captchas c
64  LEFT JOIN sessions s ON (s.session_id = c.session_id)
65  WHERE s.session_id IS NULL OR
66  c.date_created <= ' . $this->datetimeToDB($threshold)
67  );
68 
69  while (!$result->EOF) {
70  $captchas[] =& $this->_returnCaptchaFromRow($result->GetRowAssoc(false));
71  $result->MoveNext();
72  }
73 
74  $result->Close();
75  unset($result);
76 
77  return $captchas;
78  }
79 
85  function &getCaptcha($captchaId) {
86  $result =& $this->retrieve(
87  'SELECT * FROM captchas WHERE captcha_id = ?',
88  array((int) $captchaId)
89  );
90 
91  $captcha = null;
92  if ($result->RecordCount() != 0) {
93  $captcha =& $this->_returnCaptchaFromRow($result->GetRowAssoc(false));
94  }
95 
96  $result->Close();
97  unset($result);
98 
99  return $captcha;
100  }
101 
106  function newDataObject() {
107  return new Captcha();
108  }
109 
115  function &_returnCaptchaFromRow($row) {
116  $captcha = $this->newDataObject();
117  $captcha->setId($row['captcha_id']);
118  $captcha->setSessionId($row['session_id']);
119  $captcha->setValue($row['value']);
120  $captcha->setDateCreated($this->datetimeFromDB($row['date_created']));
121 
122  HookRegistry::call('CaptchaDAO::_returnCaptchaFromRow', array(&$captcha, &$row));
123 
124  return $captcha;
125  }
126 
132  function insertCaptcha(&$captcha) {
133  $captcha->setDateCreated(Core::getCurrentDate());
134  $this->update(
135  sprintf('INSERT INTO captchas
136  (session_id, value, date_created)
137  VALUES
138  (?, ?, %s)',
139  $this->datetimeToDB($captcha->getDateCreated())),
140  array(
141  (int) $captcha->getSessionId(),
142  $captcha->getValue()
143  )
144  );
145 
146  $captcha->setId($this->getInsertCaptchaId());
147  return $captcha->getId();
148  }
149 
154  function getInsertCaptchaId() {
155  return $this->getInsertId('captchas', 'captcha_id');
156  }
157 
162  function deleteObject(&$captcha) {
163  $result = $this->update(
164  'DELETE FROM captchas WHERE captcha_id = ?',
165  array((int) $captcha->getId())
166  );
167  }
168 
169  function deleteCaptcha(&$captcha) {
170  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
171  return $this->deleteObject($captcha);
172  }
173 
178  function updateObject(&$captcha) {
179  $this->update(
180  sprintf('UPDATE captchas
181  SET
182  session_id = ?,
183  value = ?,
184  date_created = %s
185  WHERE captcha_id = ?',
186  $this->datetimeToDB($captcha->getDateCreated())),
187  array(
188  (int) $captcha->getSessionId(),
189  $captcha->getValue(),
190  (int) $captcha->getId()
191  )
192  );
193  }
194 
195  function updateCaptcha(&$captcha) {
196  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
197  return $this->updateObject($captcha);
198  }
199 }
200 
201 ?>
& getExpiredCaptchas($lifespan=86400)
updateObject(&$captcha)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
deleteObject(&$captcha)
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
& _returnCaptchaFromRow($row)
getCurrentDate($ts=null)
Definition: Core.inc.php:94
datetimeFromDB($dt)
Definition: DAO.inc.php:316
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
insertCaptcha(&$captcha)
call($hookName, $args=null)
Operations for retrieving and modifying Captcha keys.
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getCaptchasBySessionId($sessionId)
Class for Captcha verifiers.
Definition: Captcha.inc.php:23
& getCaptcha($captchaId)