00001 <?php
00002
00016
00017
00018
00019 import('captcha.Captcha');
00020
00021 class CaptchaDAO extends DAO {
00027 function &getCaptchasBySessionId($sessionId) {
00028 $captchas = array();
00029
00030 $result = &$this->retrieve('SELECT * FROM captchas WHERE session_id = ?', $sessionId);
00031
00032 while (!$result->EOF) {
00033 $captchas[] = &$this->_returnCaptchaFromRow($result->GetRowAssoc(false));
00034 $result->moveNext();
00035 }
00036
00037 $result->Close();
00038 unset($result);
00039
00040 return $captchas;
00041 }
00042
00048 function &getExpiredCaptchas($lifespan = 86400) {
00049 $captchas = array();
00050 $threshold = time() - $lifespan;
00051
00052 $result = &$this->retrieve('SELECT c.* FROM captchas c LEFT JOIN sessions s ON (s.session_id = c.session_id) WHERE s.session_id IS NULL OR c.date_created <= ' . $this->datetimeToDB($threshold));
00053
00054 while (!$result->EOF) {
00055 $captchas[] = &$this->_returnCaptchaFromRow($result->GetRowAssoc(false));
00056 $result->moveNext();
00057 }
00058
00059 $result->Close();
00060 unset($result);
00061
00062 return $captchas;
00063 }
00064
00070 function &getCaptcha($captchaId) {
00071 $result = &$this->retrieve(
00072 'SELECT * FROM captchas WHERE captcha_id = ?', $captchaId
00073 );
00074
00075 $captcha = null;
00076 if ($result->RecordCount() != 0) {
00077 $captcha = &$this->_returnCaptchaFromRow($result->GetRowAssoc(false));
00078 }
00079
00080 $result->Close();
00081 unset($result);
00082
00083 return $captcha;
00084 }
00085
00091 function &_returnCaptchaFromRow($row) {
00092 $captcha = &new Captcha();
00093 $captcha->setCaptchaId($row['captcha_id']);
00094 $captcha->setSessionId($row['session_id']);
00095 $captcha->setValue($row['value']);
00096 $captcha->setDateCreated($this->datetimeFromDB($row['date_created']));
00097
00098 HookRegistry::call('CaptchaDAO::_returnCaptchaFromRow', array(&$captcha, &$row));
00099
00100 return $captcha;
00101 }
00102
00108 function insertCaptcha(&$captcha) {
00109 $captcha->setDateCreated(Core::getCurrentDate());
00110 $this->update(
00111 sprintf('INSERT INTO captchas
00112 (session_id, value, date_created)
00113 VALUES
00114 (?, ?, %s)',
00115 $this->datetimeToDB($captcha->getDateCreated())),
00116 array(
00117 $captcha->getSessionId(),
00118 $captcha->getValue()
00119 )
00120 );
00121
00122 $captcha->setCaptchaId($this->getInsertCaptchaId());
00123 return $captcha->getCaptchaId();
00124 }
00125
00130 function getInsertCaptchaId() {
00131 return $this->getInsertId('captchas', 'captcha_id');
00132 }
00133
00138 function deleteCaptcha(&$captcha) {
00139 $result = $this->update('DELETE FROM captchas WHERE captcha_id = ?', $captcha->getCaptchaId());
00140 }
00141
00146 function updateCaptcha(&$captcha) {
00147 $this->update(
00148 sprintf('UPDATE article_captchas
00149 SET
00150 session_id = ?,
00151 value = ?,
00152 date_created = %s
00153 WHERE captcha_id = ?',
00154 $this->datetimeToDB($captcha->getDateCreated())),
00155 array(
00156 $captcha->getSessionId(),
00157 $captcha->getValue(),
00158 $captcha->getCaptchaId()
00159 )
00160 );
00161 }
00162 }
00163
00164 ?>