OXID eShop CE  4.9.8
 All Classes Files Functions Variables Pages
oxcaptcha.php
Go to the documentation of this file.
1 <?php
2 
8 class oxCaptcha extends oxSuperCfg
9 {
10 
16  protected $_iMacLength = 5;
17 
23  protected $_sText = null;
24 
30  private $_sMacChars = 'abcdefghijkmnpqrstuvwxyz23456789';
31 
37  protected $_iTimeout = 300;
38 
44  public function getText()
45  {
46  if (!$this->_sText) {
47  $this->_sText = '';
48  for ($i = 0; $i < $this->_iMacLength; $i++) {
49  $this->_sText .= strtolower($this->_sMacChars{rand(0, strlen($this->_sMacChars) - 1)});
50  }
51  }
52 
53  return $this->_sText;
54  }
55 
63  public function getHash($sText = null)
64  {
65  // inserting captcha record
66  $iTime = time() + $this->_iTimeout;
67  $sTextHash = $this->getTextHash($sText);
68 
69  // if session is started - storing captcha info here
70  $session = $this->getSession();
71  if ($session->isSessionStarted()) {
72  $sHash = oxUtilsObject::getInstance()->generateUID();
73  $aHash = $session->getVariable("aCaptchaHash");
74  $aHash[$sHash] = array($sTextHash => $iTime);
75  $session->setVariable("aCaptchaHash", $aHash);
76  } else {
77  $oDb = oxDb::getDb();
78  $sQ = "insert into oxcaptcha ( oxhash, oxtime ) values ( '{$sTextHash}', '{$iTime}' )";
79  $oDb->execute($sQ);
80  $sHash = $oDb->getOne("select LAST_INSERT_ID()", false, false);
81  }
82 
83  return $sHash;
84  }
85 
93  public function getTextHash($sText)
94  {
95  if (!$sText) {
96  $sText = $this->getText();
97  }
98 
99  $sText = strtolower($sText);
100 
101  return md5("ox{$sText}");
102  }
103 
109  public function getImageUrl()
110  {
111  $sUrl = $this->getConfig()->getCoreUtilsURL() . "verificationimg.php?e_mac=";
112  $sKey = $this->getConfig()->getConfigParam('captchaKey');
113  $sKey = empty($sKey) ? null : $sKey;
114  $sUrl .= oxRegistry::getUtils()->strMan($this->getText(), $sKey);
115 
116  return $sUrl;
117  }
118 
124  public function isImageVisible()
125  {
126  return ((function_exists('imagecreatetruecolor') || function_exists('imagecreate')) && $this->getConfig()->getConfigParam('iUseGDVersion') > 1);
127  }
128 
138  protected function _passFromSession($sMacHash, $sHash, $iTime)
139  {
140  $blPass = null;
141  $oSession = $this->getSession();
142  if (($aHash = $oSession->getVariable("aCaptchaHash"))) {
143  $blPass = (isset($aHash[$sMacHash][$sHash]) && $aHash[$sMacHash][$sHash] >= $iTime) ? true : false;
144  unset($aHash[$sMacHash]);
145  if (!empty($aHash)) {
146  $oSession->setVariable("aCaptchaHash", $aHash);
147  } else {
148  $oSession->deleteVariable("aCaptchaHash");
149  }
150  }
151 
152  return $blPass;
153  }
154 
164  protected function _passFromDb($iMacHash, $sHash, $iTime)
165  {
166  $blPass = false;
167 
168  $oDb = oxDb::getDb();
169  $sQ = "select 1 from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
170  if (($blPass = (bool) $oDb->getOne($sQ, false, false))) {
171  // cleanup
172  $sQ = "delete from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
173  $oDb->execute($sQ);
174  }
175 
176  // garbage cleanup
177  $sQ = "delete from oxcaptcha where oxtime < $iTime";
178  $oDb->execute($sQ);
179 
180  return $blPass;
181  }
182 
191  public function pass($sMac, $sMacHash)
192  {
193  $iTime = time();
194  $sHash = $this->getTextHash($sMac);
195 
196  $blPass = $this->_passFromSession($sMacHash, $sHash, $iTime);
197 
198  // if captha info was NOT stored in session
199  if ($blPass === null) {
200  $blPass = $this->_passFromDb((int) $sMacHash, $sHash, $iTime);
201  }
202 
203  return (bool) $blPass;
204  }
205 }