oxcaptcha.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxCaptcha extends oxSuperCfg
00009 {
00010 
00016     protected $_iMacLength = 5;
00017 
00023     protected $_sText = null;
00024 
00030     private $_sMacChars = 'abcdefghijkmnpqrstuvwxyz23456789';
00031 
00037     protected $_iTimeout = 300;
00038 
00044     public function getText()
00045     {
00046         if (!$this->_sText) {
00047             $this->_sText = '';
00048             for ($i = 0; $i < $this->_iMacLength; $i++) {
00049                 $this->_sText .= strtolower($this->_sMacChars{rand(0, strlen($this->_sMacChars) - 1)});
00050             }
00051         }
00052 
00053         return $this->_sText;
00054     }
00055 
00063     public function getHash($sText = null)
00064     {
00065         // inserting captcha record
00066         $iTime = time() + $this->_iTimeout;
00067         $sTextHash = $this->getTextHash($sText);
00068 
00069         // if session is started - storing captcha info here
00070         $session = $this->getSession();
00071         if ($session->isSessionStarted()) {
00072             $sHash = oxUtilsObject::getInstance()->generateUID();
00073             $aHash = $session->getVariable("aCaptchaHash");
00074             $aHash[$sHash] = array($sTextHash => $iTime);
00075             $session->setVariable("aCaptchaHash", $aHash);
00076         } else {
00077             $oDb = oxDb::getDb();
00078             $sQ = "insert into oxcaptcha ( oxhash, oxtime ) values ( '{$sTextHash}', '{$iTime}' )";
00079             $oDb->execute($sQ);
00080             $sHash = $oDb->getOne("select LAST_INSERT_ID()", false, false);
00081         }
00082 
00083         return $sHash;
00084     }
00085 
00093     public function getTextHash($sText)
00094     {
00095         if (!$sText) {
00096             $sText = $this->getText();
00097         }
00098 
00099         $sText = strtolower($sText);
00100 
00101         return md5("ox{$sText}");
00102     }
00103 
00109     public function getImageUrl()
00110     {
00111         $sUrl = $this->getConfig()->getCoreUtilsURL() . "verificationimg.php?e_mac=";
00112         $sUrl .= oxRegistry::getUtils()->strMan($this->getText());
00113 
00114         return $sUrl;
00115     }
00116 
00122     public function isImageVisible()
00123     {
00124         return ((function_exists('imagecreatetruecolor') || function_exists('imagecreate')) && $this->getConfig()->getConfigParam('iUseGDVersion') > 1);
00125     }
00126 
00136     protected function _passFromSession($sMacHash, $sHash, $iTime)
00137     {
00138         $blPass = null;
00139         $oSession = $this->getSession();
00140         if (($aHash = $oSession->getVariable("aCaptchaHash"))) {
00141             $blPass = (isset($aHash[$sMacHash][$sHash]) && $aHash[$sMacHash][$sHash] >= $iTime) ? true : false;
00142             unset($aHash[$sMacHash]);
00143             if (!empty($aHash)) {
00144                 $oSession->setVariable("aCaptchaHash", $aHash);
00145             } else {
00146                 $oSession->deleteVariable("aCaptchaHash");
00147             }
00148         }
00149 
00150         return $blPass;
00151     }
00152 
00162     protected function _passFromDb($iMacHash, $sHash, $iTime)
00163     {
00164         $blPass = false;
00165 
00166         $oDb = oxDb::getDb();
00167         $sQ = "select 1 from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
00168         if (($blPass = (bool) $oDb->getOne($sQ, false, false))) {
00169             // cleanup
00170             $sQ = "delete from oxcaptcha where oxid = {$iMacHash} and oxhash = '{$sHash}'";
00171             $oDb->execute($sQ);
00172         }
00173 
00174         // garbage cleanup
00175         $sQ = "delete from oxcaptcha where oxtime < $iTime";
00176         $oDb->execute($sQ);
00177 
00178         return $blPass;
00179     }
00180 
00189     public function pass($sMac, $sMacHash)
00190     {
00191         $iTime = time();
00192         $sHash = $this->getTextHash($sMac);
00193 
00194         $blPass = $this->_passFromSession($sMacHash, $sHash, $iTime);
00195 
00196         // if captha info was NOT stored in session
00197         if ($blPass === null) {
00198             $blPass = $this->_passFromDb((int) $sMacHash, $sHash, $iTime);
00199         }
00200 
00201         return (bool) $blPass;
00202     }
00203 }