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 oxSession::setVar( "aCaptchaHash", array( $sHash => array( $sTextHash => $iTime ) ) );
00072 } else {
00073 $sQ = "insert into oxcaptcha ( oxhash, oxtime ) values ( '{$sTextHash}', '{$iTime}' )";
00074 oxDb::getDb()->execute( $sQ );
00075 $sHash = oxDb::getDb()->getOne( "select LAST_INSERT_ID()" );
00076 }
00077 return $sHash;
00078 }
00079
00087 public function getTextHash( $sText )
00088 {
00089 if (!$sText) {
00090 $sText = $this->getText();
00091 }
00092
00093 $sText = strtolower($sText);
00094 return md5( "ox{$sText}" );
00095 }
00096
00102 public function getImageUrl()
00103 {
00104 $sUrl = $this->getConfig()->getCoreUtilsURL() . "verificationimg.php?e_mac=";
00105 $sUrl .= oxUtils::getInstance()->strMan( $this->getText() );
00106
00107 return $sUrl;
00108 }
00109
00115 public function isImageVisible()
00116 {
00117 return ( ( function_exists( 'imagecreatetruecolor' ) || function_exists( 'imagecreate' ) ) && $this->getConfig()->getConfigParam( 'iUseGDVersion' ) > 1 );
00118 }
00119
00129 protected function _passFromSession( $sMacHash, $sHash, $iTime )
00130 {
00131 $blPass = null;
00132 if ( ( $aHash = oxSession::getVar( "aCaptchaHash" ) ) ) {
00133 $blPass = ( isset( $aHash[$sMacHash][$sHash] ) && $aHash[$sMacHash][$sHash] >= $iTime ) ? true : false;
00134 oxSession::deleteVar( "aCaptchaHash" );
00135 }
00136 return $blPass;
00137 }
00138
00148 protected function _passFromDb( $iMacHash, $sHash, $iTime )
00149 {
00150 $blPass = false;
00151
00152 $oDb = oxDb::getDb();
00153 $sQ = "select 1 from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
00154 if ( ( $blPass = (bool) $oDb->getOne( $sQ ) ) ) {
00155
00156 $sQ = "delete from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
00157 $oDb->execute( $sQ );
00158 }
00159
00160
00161 $sQ = "delete from oxcaptcha where oxtime < $iTime";
00162 $oDb->execute( $sQ );
00163
00164 return $blPass;
00165 }
00166
00175 public function pass( $sMac, $sMacHash )
00176 {
00177 $iTime = time();
00178 $sHash = $this->getTextHash( $sMac );
00179
00180 $blPass = $this->_passFromSession( $sMacHash, $sHash, $iTime );
00181
00182
00183 if ( $blPass === null ) {
00184 $blPass = $this->_passFromDb( (int) $sMacHash, $sHash, $iTime );
00185 }
00186
00187 return (bool) $blPass;
00188 }
00189 }