oxonlinelicensecheck.php

Go to the documentation of this file.
00001 <?php
00002 
00011 class oxOnlineLicenseCheck
00012 {
00013 
00017     const CONFIG_VAR_NAME = 'iOlcSuccess';
00018 
00024     protected $_iValidResponseCode = 0;
00025 
00031     protected $_sValidResponseMessage = 'ACK';
00032 
00038     protected $_aSerialKeys = array();
00039 
00045     protected $_sErrorMessage = '';
00046 
00052     protected $_blIsException = false;
00053 
00055     protected $_oCaller = null;
00056 
00058     protected $_oUserCounter = null;
00059 
00061     protected $_oServersManager = null;
00062 
00068     public function setServersManager($oServersManager)
00069     {
00070         $this->_oServersManager = $oServersManager;
00071     }
00072 
00078     public function getServersManager()
00079     {
00080         return $this->_oServersManager;
00081     }
00082 
00088     public function setUserCounter($oUserCounter)
00089     {
00090         $this->_oUserCounter = $oUserCounter;
00091     }
00092 
00098     public function getUserCounter()
00099     {
00100         return $this->_oUserCounter;
00101     }
00102 
00103 
00109     public function __construct($oCaller)
00110     {
00111         $this->_oCaller = $oCaller;
00112     }
00113 
00119     public function getErrorMessage()
00120     {
00121         return $this->_sErrorMessage;
00122     }
00123 
00129     public function isException()
00130     {
00131         return $this->_blIsException;
00132     }
00133 
00139     public function validateShopSerials()
00140     {
00141         $aSerials = oxRegistry::getConfig()->getConfigParam("aSerials");
00142         if (!$this->validate($aSerials) && !$this->isException()) {
00143             $this->_startGracePeriod();
00144         }
00145     }
00146 
00154     public function validateNewSerial($sSerial)
00155     {
00156         $aSerials = oxRegistry::getConfig()->getConfigParam("aSerials");
00157         $aSerials[] = array('attributes' => array('state' => 'new'), 'value' => $sSerial);
00158 
00159         return $this->validate($aSerials);
00160     }
00161 
00169     public function validate($aSerials)
00170     {
00171         $aSerials = (array)$aSerials;
00172         $this->_setIsException(false);
00173 
00174         $blResult = false;
00175         try {
00176             $oRequest = $this->_formRequest($aSerials);
00177 
00178             $oCaller = $this->_getCaller();
00179             $oResponse = $oCaller->doRequest($oRequest);
00180 
00181             $blResult = $this->_validateResponse($oResponse);
00182 
00183             if ($blResult) {
00184                 $this->_logSuccess();
00185             }
00186         } catch (oxException $oEx) {
00187             $this->_setErrorMessage($oEx->getMessage());
00188             $this->_setIsException(true);
00189         }
00190 
00191         return $blResult;
00192     }
00193 
00199     protected function _setErrorMessage($sErrorMessage)
00200     {
00201         $this->_sErrorMessage = $sErrorMessage;
00202     }
00203 
00209     protected function _getCaller()
00210     {
00211         return $this->_oCaller;
00212     }
00213 
00223     protected function _validateResponse($oResponse)
00224     {
00225         if (isset($oResponse->code) && isset($oResponse->message)) {
00226             if ($oResponse->code == $this->_iValidResponseCode &&
00227                 $oResponse->message == $this->_sValidResponseMessage
00228             ) {
00229                 // serial keys are valid
00230                 $blValid = true;
00231             } else {
00232                 // serial keys are not valid
00233                 $this->_setErrorMessage(oxRegistry::getLang()->translateString('OLC_ERROR_SERIAL_NOT_VALID'));
00234                 $blValid = false;
00235             }
00236         } else {
00237             // validation result is unknown
00238             throw new oxException('OLC_ERROR_RESPONSE_NOT_VALID');
00239         }
00240 
00241         return $blValid;
00242     }
00243 
00253     protected function _formRequest($aSerials)
00254     {
00255         $oConfig = oxRegistry::getConfig();
00256 
00258         $oRequest = oxNew('oxOnlineLicenseCheckRequest');
00259 
00260         $oRequest->revision = $oConfig->getRevision();
00261 
00262         $oRequest->keys = array('key' => $aSerials);
00263 
00264         $oRequest->productSpecificInformation = new stdClass();
00265 
00266         if (!is_null($this->getServersManager())) {
00267             $aServers = $this->getServersManager()->getServers();
00268             $oRequest->productSpecificInformation->servers = array('server' => $aServers);
00269         }
00270 
00271         $aCounters = $this->_formCounters();
00272         if (!empty($aCounters)) {
00273             $oRequest->productSpecificInformation->counters = array('counter' => $aCounters);
00274         }
00275 
00276         return $oRequest;
00277     }
00278 
00284     protected function _formCounters()
00285     {
00286         $oUserCounter = $this->_getUserCounter();
00287 
00288         $aCounters = array();
00289 
00290         if (!is_null($this->getUserCounter())) {
00291             $aCounters[] = array(
00292                 'name' => 'admin users',
00293                 'value' => $oUserCounter->getAdminCount(),
00294             );
00295             $aCounters[] = array(
00296                 'name' => 'active admin users',
00297                 'value' => $oUserCounter->getActiveAdminCount(),
00298             );
00299         }
00300 
00301         $aCounters[] = array(
00302             'name' => 'subShops',
00303             'value' => oxRegistry::getConfig()->getMandateCount(),
00304         );
00305 
00306         return $aCounters;
00307     }
00308 
00312     protected function _logSuccess()
00313     {
00314         $iTime = oxRegistry::get("oxUtilsDate")->getTime();
00315         $sBaseShop = oxRegistry::getConfig()->getBaseShopId();
00316         oxRegistry::getConfig()->saveShopConfVar("str", oxOnlineLicenseCheck::CONFIG_VAR_NAME, $iTime, $sBaseShop);
00317     }
00318 
00324     protected function _setIsException($blIsException)
00325     {
00326         $this->_blIsException = $blIsException;
00327     }
00328 
00333     protected function _startGracePeriod()
00334     {
00335     }
00336 
00342     protected function _getUserCounter()
00343     {
00344         return $this->_oUserCounter;
00345     }
00346 }