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