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