Go to the documentation of this file.00001 <?php
00002
00008 class oxCaptcha extends oxSuperCfg
00009 {
00015 protected $_iMacLength = 5;
00016
00022 protected $_sText = null;
00023
00029 private $_sMacChars = 'abcdefghijkmnpqrstuvwxyz23456789';
00030
00036 protected $_iTimeout = 300;
00037
00043 public function getText()
00044 {
00045 if ( !$this->_sText ) {
00046 $this->_sText = '';
00047 for ( $i=0; $i < $this->_iMacLength; $i++ ) {
00048 $this->_sText .= strtolower( $this->_sMacChars{ rand( 0, strlen( $this->_sMacChars ) - 1 ) } );
00049 }
00050 }
00051
00052 return $this->_sText;
00053 }
00054
00062 public function getHash($sText = null)
00063 {
00064
00065 $iTime = time() + $this->_iTimeout;
00066 $sTextHash = $this->getTextHash( $sText );
00067
00068
00069 if ( $this->getSession()->isSessionStarted() ) {
00070 $sHash = oxUtilsObject::getInstance()->generateUID();
00071 $aHash = oxSession::getVar( "aCaptchaHash" );
00072 $aHash[$sHash] = array( $sTextHash => $iTime );
00073 oxSession::setVar( "aCaptchaHash", $aHash );
00074 } else {
00075 $oDb = oxDb::getDb();
00076 $sQ = "insert into oxcaptcha ( oxhash, oxtime ) values ( '{$sTextHash}', '{$iTime}' )";
00077 $oDb->execute( $sQ );
00078 $sHash = $oDb->getOne( "select LAST_INSERT_ID()", false, false );
00079 }
00080 return $sHash;
00081 }
00082
00090 public function getTextHash( $sText )
00091 {
00092 if (!$sText) {
00093 $sText = $this->getText();
00094 }
00095
00096 $sText = strtolower($sText);
00097 return md5( "ox{$sText}" );
00098 }
00099
00105 public function getImageUrl()
00106 {
00107 $sUrl = $this->getConfig()->getCoreUtilsURL() . "verificationimg.php?e_mac=";
00108 $sUrl .= oxUtils::getInstance()->strMan( $this->getText() );
00109
00110 return $sUrl;
00111 }
00112
00118 public function isImageVisible()
00119 {
00120 return ( ( function_exists( 'imagecreatetruecolor' ) || function_exists( 'imagecreate' ) ) && $this->getConfig()->getConfigParam( 'iUseGDVersion' ) > 1 );
00121 }
00122
00132 protected function _passFromSession( $sMacHash, $sHash, $iTime )
00133 {
00134 $blPass = null;
00135 if ( ( $aHash = oxSession::getVar( "aCaptchaHash" ) ) ) {
00136 $blPass = ( isset( $aHash[$sMacHash][$sHash] ) && $aHash[$sMacHash][$sHash] >= $iTime ) ? true : false;
00137 unset( $aHash[$sMacHash] );
00138 if ( !empty( $aHash ) ) {
00139 oxSession::setVar( "aCaptchaHash", $aHash );
00140 } else {
00141 oxSession::deleteVar( "aCaptchaHash" );
00142 }
00143 }
00144 return $blPass;
00145 }
00146
00156 protected function _passFromDb( $iMacHash, $sHash, $iTime )
00157 {
00158 $blPass = false;
00159
00160 $oDb = oxDb::getDb();
00161 $sQ = "select 1 from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
00162 if ( ( $blPass = (bool) $oDb->getOne( $sQ, false, false ) ) ) {
00163
00164 $sQ = "delete from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
00165 $oDb->execute( $sQ );
00166 }
00167
00168
00169 $sQ = "delete from oxcaptcha where oxtime < $iTime";
00170 $oDb->execute( $sQ );
00171
00172 return $blPass;
00173 }
00174
00183 public function pass( $sMac, $sMacHash )
00184 {
00185 $iTime = time();
00186 $sHash = $this->getTextHash( $sMac );
00187
00188 $blPass = $this->_passFromSession( $sMacHash, $sHash, $iTime );
00189
00190
00191 if ( $blPass === null ) {
00192 $blPass = $this->_passFromDb( (int) $sMacHash, $sHash, $iTime );
00193 }
00194
00195 return (bool) $blPass;
00196 }
00197 }