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