OXID eShop CE  4.9.6
 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  $sUrl .= oxRegistry::getUtils()->strMan($this->getText());
113 
114  return $sUrl;
115  }
116 
122  public function isImageVisible()
123  {
124  return ((function_exists('imagecreatetruecolor') || function_exists('imagecreate')) && $this->getConfig()->getConfigParam('iUseGDVersion') > 1);
125  }
126 
136  protected function _passFromSession($sMacHash, $sHash, $iTime)
137  {
138  $blPass = null;
139  $oSession = $this->getSession();
140  if (($aHash = $oSession->getVariable("aCaptchaHash"))) {
141  $blPass = (isset($aHash[$sMacHash][$sHash]) && $aHash[$sMacHash][$sHash] >= $iTime) ? true : false;
142  unset($aHash[$sMacHash]);
143  if (!empty($aHash)) {
144  $oSession->setVariable("aCaptchaHash", $aHash);
145  } else {
146  $oSession->deleteVariable("aCaptchaHash");
147  }
148  }
149 
150  return $blPass;
151  }
152 
162  protected function _passFromDb($iMacHash, $sHash, $iTime)
163  {
164  $blPass = false;
165 
166  $oDb = oxDb::getDb();
167  $sQ = "select 1 from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
168  if (($blPass = (bool) $oDb->getOne($sQ, false, false))) {
169  // cleanup
170  $sQ = "delete from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
171  $oDb->execute($sQ);
172  }
173 
174  // garbage cleanup
175  $sQ = "delete from oxcaptcha where oxtime < $iTime";
176  $oDb->execute($sQ);
177 
178  return $blPass;
179  }
180 
189  public function pass($sMac, $sMacHash)
190  {
191  $iTime = time();
192  $sHash = $this->getTextHash($sMac);
193 
194  $blPass = $this->_passFromSession($sMacHash, $sHash, $iTime);
195 
196  // if captha info was NOT stored in session
197  if ($blPass === null) {
198  $blPass = $this->_passFromDb((int) $sMacHash, $sHash, $iTime);
199  }
200 
201  return (bool) $blPass;
202  }
203 }